blob: a828222e816c1af1686dc7808486d15dd1903a19 [file] [log] [blame]
Christian Koenigdafc3bd2009-10-11 23:49:13 +02001/*
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 */
Thierry Redinge3b2e032013-01-14 13:36:30 +010026#include <linux/hdmi.h>
Pierre Ossmana2098252013-11-06 20:09:08 +010027#include <linux/gcd.h>
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/radeon_drm.h>
Christian Koenigdafc3bd2009-10-11 23:49:13 +020030#include "radeon.h"
Daniel Vetter3574dda2011-02-18 17:59:19 +010031#include "radeon_asic.h"
Rafał Miłeckic6543a62012-04-28 23:35:24 +020032#include "r600d.h"
Christian Koenigdafc3bd2009-10-11 23:49:13 +020033#include "atom.h"
34
35/*
36 * HDMI color format
37 */
38enum r600_hdmi_color_format {
39 RGB = 0,
40 YCC_422 = 1,
41 YCC_444 = 2
42};
43
44/*
45 * IEC60958 status bits
46 */
47enum r600_hdmi_iec_status_bits {
48 AUDIO_STATUS_DIG_ENABLE = 0x01,
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000049 AUDIO_STATUS_V = 0x02,
50 AUDIO_STATUS_VCFG = 0x04,
Christian Koenigdafc3bd2009-10-11 23:49:13 +020051 AUDIO_STATUS_EMPHASIS = 0x08,
52 AUDIO_STATUS_COPYRIGHT = 0x10,
53 AUDIO_STATUS_NONAUDIO = 0x20,
54 AUDIO_STATUS_PROFESSIONAL = 0x40,
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000055 AUDIO_STATUS_LEVEL = 0x80
Christian Koenigdafc3bd2009-10-11 23:49:13 +020056};
57
Lauri Kasanen1109ca02012-08-31 13:43:50 -040058static const struct radeon_hdmi_acr r600_hdmi_predefined_acr[] = {
Christian Koenigdafc3bd2009-10-11 23:49:13 +020059 /* 32kHz 44.1kHz 48kHz */
60 /* Clock N CTS N CTS N CTS */
Pierre Ossman3e719852013-11-06 20:00:32 +010061 { 25175, 4096, 25175, 28224, 125875, 6144, 25175 }, /* 25,20/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020062 { 25200, 4096, 25200, 6272, 28000, 6144, 25200 }, /* 25.20 MHz */
63 { 27000, 4096, 27000, 6272, 30000, 6144, 27000 }, /* 27.00 MHz */
64 { 27027, 4096, 27027, 6272, 30030, 6144, 27027 }, /* 27.00*1.001 MHz */
65 { 54000, 4096, 54000, 6272, 60000, 6144, 54000 }, /* 54.00 MHz */
66 { 54054, 4096, 54054, 6272, 60060, 6144, 54054 }, /* 54.00*1.001 MHz */
Pierre Ossman3e719852013-11-06 20:00:32 +010067 { 74176, 4096, 74176, 5733, 75335, 6144, 74176 }, /* 74.25/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020068 { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */
Pierre Ossman3e719852013-11-06 20:00:32 +010069 { 148352, 4096, 148352, 5733, 150670, 6144, 148352 }, /* 148.50/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020070 { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020071};
72
Alex Deucher062c2e42013-09-27 18:09:54 -040073
Pierre Ossmana2098252013-11-06 20:09:08 +010074/*
75 * calculate CTS and N values if they are not found in the table
76 */
77static void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int *N, int freq)
78{
79 int n, cts;
80 unsigned long div, mul;
81
82 /* Safe, but overly large values */
83 n = 128 * freq;
84 cts = clock * 1000;
85
86 /* Smallest valid fraction */
87 div = gcd(n, cts);
88
89 n /= div;
90 cts /= div;
91
92 /*
93 * The optimal N is 128*freq/1000. Calculate the closest larger
94 * value that doesn't truncate any bits.
95 */
96 mul = ((128*freq/1000) + (n-1))/n;
97
98 n *= mul;
99 cts *= mul;
100
101 /* Check that we are in spec (not always possible) */
102 if (n < (128*freq/1500))
103 printk(KERN_WARNING "Calculated ACR N value is too small. You may experience audio problems.\n");
104 if (n > (128*freq/300))
105 printk(KERN_WARNING "Calculated ACR N value is too large. You may experience audio problems.\n");
106
107 *N = n;
108 *CTS = cts;
109
110 DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
111 *N, *CTS, freq);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200112}
113
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200114struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
115{
116 struct radeon_hdmi_acr res;
117 u8 i;
118
Pierre Ossmana2098252013-11-06 20:09:08 +0100119 /* Precalculated values for common clocks */
120 for (i = 0; i < ARRAY_SIZE(r600_hdmi_predefined_acr); i++) {
121 if (r600_hdmi_predefined_acr[i].clock == clock)
122 return r600_hdmi_predefined_acr[i];
123 }
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200124
Pierre Ossmana2098252013-11-06 20:09:08 +0100125 /* And odd clocks get manually calculated */
126 r600_hdmi_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
127 r600_hdmi_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
128 r600_hdmi_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200129
130 return res;
131}
132
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200133/*
134 * update the N and CTS parameters for a given pixel clock rate
135 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200136void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200137{
138 struct drm_device *dev = encoder->dev;
139 struct radeon_device *rdev = dev->dev_private;
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200140 struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200141 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
142 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
143 uint32_t offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200144
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200145 WREG32(HDMI0_ACR_32_0 + offset, HDMI0_ACR_CTS_32(acr.cts_32khz));
146 WREG32(HDMI0_ACR_32_1 + offset, acr.n_32khz);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200147
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200148 WREG32(HDMI0_ACR_44_0 + offset, HDMI0_ACR_CTS_44(acr.cts_44_1khz));
149 WREG32(HDMI0_ACR_44_1 + offset, acr.n_44_1khz);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200150
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200151 WREG32(HDMI0_ACR_48_0 + offset, HDMI0_ACR_CTS_48(acr.cts_48khz));
152 WREG32(HDMI0_ACR_48_1 + offset, acr.n_48khz);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200153}
154
155/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200156 * build a HDMI Video Info Frame
157 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200158void r600_hdmi_update_avi_infoframe(struct drm_encoder *encoder, void *buffer,
159 size_t size)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200160{
161 struct drm_device *dev = encoder->dev;
162 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200163 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
164 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
165 uint32_t offset = dig->afmt->offset;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100166 uint8_t *frame = buffer + 3;
Alex Deucherf1003802013-06-07 10:41:03 -0400167 uint8_t *header = buffer;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200168
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200169 WREG32(HDMI0_AVI_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200170 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200171 WREG32(HDMI0_AVI_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200172 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200173 WREG32(HDMI0_AVI_INFO2 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200174 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200175 WREG32(HDMI0_AVI_INFO3 + offset,
Alex Deucherf1003802013-06-07 10:41:03 -0400176 frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200177}
178
179/*
180 * build a Audio Info Frame
181 */
Thierry Redinge3b2e032013-01-14 13:36:30 +0100182static void r600_hdmi_update_audio_infoframe(struct drm_encoder *encoder,
183 const void *buffer, size_t size)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200184{
185 struct drm_device *dev = encoder->dev;
186 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200187 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
188 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
189 uint32_t offset = dig->afmt->offset;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100190 const u8 *frame = buffer + 3;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200191
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200192 WREG32(HDMI0_AUDIO_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200193 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200194 WREG32(HDMI0_AUDIO_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200195 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
196}
197
198/*
199 * test if audio buffer is filled enough to start playing
200 */
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200201static bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200202{
203 struct drm_device *dev = encoder->dev;
204 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200205 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
206 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
207 uint32_t offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200208
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200209 return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200210}
211
212/*
213 * have buffer status changed since last call?
214 */
215int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
216{
217 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200218 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200219 int status, result;
220
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200221 if (!dig->afmt || !dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200222 return 0;
223
224 status = r600_hdmi_is_audio_buffer_filled(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200225 result = dig->afmt->last_buffer_filled_status != status;
226 dig->afmt->last_buffer_filled_status = status;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200227
228 return result;
229}
230
231/*
232 * write the audio workaround status to the hardware
233 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200234void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200235{
236 struct drm_device *dev = encoder->dev;
237 struct radeon_device *rdev = dev->dev_private;
238 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200239 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
240 uint32_t offset = dig->afmt->offset;
241 bool hdmi_audio_workaround = false; /* FIXME */
242 u32 value;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200243
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200244 if (!hdmi_audio_workaround ||
245 r600_hdmi_is_audio_buffer_filled(encoder))
246 value = 0; /* disable workaround */
247 else
248 value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
249 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
250 value, ~HDMI0_AUDIO_TEST_EN);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200251}
252
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200253void r600_audio_set_dto(struct drm_encoder *encoder, u32 clock)
Alex Deucherb1f6f472013-04-18 10:50:55 -0400254{
255 struct drm_device *dev = encoder->dev;
256 struct radeon_device *rdev = dev->dev_private;
257 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
258 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucher731da212013-05-13 11:35:26 -0400259 u32 base_rate = 24000;
Alex Deucher1518dd82013-07-30 17:31:07 -0400260 u32 max_ratio = clock / base_rate;
261 u32 dto_phase;
262 u32 dto_modulo = clock;
263 u32 wallclock_ratio;
264 u32 dto_cntl;
Alex Deucherb1f6f472013-04-18 10:50:55 -0400265
266 if (!dig || !dig->afmt)
267 return;
268
Alex Deucher1518dd82013-07-30 17:31:07 -0400269 if (max_ratio >= 8) {
270 dto_phase = 192 * 1000;
271 wallclock_ratio = 3;
272 } else if (max_ratio >= 4) {
273 dto_phase = 96 * 1000;
274 wallclock_ratio = 2;
275 } else if (max_ratio >= 2) {
276 dto_phase = 48 * 1000;
277 wallclock_ratio = 1;
278 } else {
279 dto_phase = 24 * 1000;
280 wallclock_ratio = 0;
281 }
282
Alex Deucherb1f6f472013-04-18 10:50:55 -0400283 /* there are two DTOs selected by DCCG_AUDIO_DTO_SELECT.
284 * doesn't matter which one you use. Just use the first one.
285 */
Alex Deucherb1f6f472013-04-18 10:50:55 -0400286 /* XXX two dtos; generally use dto0 for hdmi */
287 /* Express [24MHz / target pixel clock] as an exact rational
288 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
289 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
290 */
Alex Deucher58d327d2013-09-25 12:04:37 -0400291 if (ASIC_IS_DCE32(rdev)) {
Alex Deuchere1accbf2013-07-29 18:56:13 -0400292 if (dig->dig_encoder == 0) {
Alex Deucher1518dd82013-07-30 17:31:07 -0400293 dto_cntl = RREG32(DCCG_AUDIO_DTO0_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
294 dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
295 WREG32(DCCG_AUDIO_DTO0_CNTL, dto_cntl);
296 WREG32(DCCG_AUDIO_DTO0_PHASE, dto_phase);
297 WREG32(DCCG_AUDIO_DTO0_MODULE, dto_modulo);
Alex Deuchere1accbf2013-07-29 18:56:13 -0400298 WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
299 } else {
Alex Deucher1518dd82013-07-30 17:31:07 -0400300 dto_cntl = RREG32(DCCG_AUDIO_DTO1_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
301 dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
302 WREG32(DCCG_AUDIO_DTO1_CNTL, dto_cntl);
303 WREG32(DCCG_AUDIO_DTO1_PHASE, dto_phase);
304 WREG32(DCCG_AUDIO_DTO1_MODULE, dto_modulo);
Alex Deuchere1accbf2013-07-29 18:56:13 -0400305 WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
306 }
Alex Deucher55d4e022013-11-25 13:20:59 -0500307 } else {
Alex Deucher58d327d2013-09-25 12:04:37 -0400308 /* according to the reg specs, this should DCE3.2 only, but in
Alex Deucher55d4e022013-11-25 13:20:59 -0500309 * practice it seems to cover DCE2.0/3.0/3.1 as well.
Alex Deucher58d327d2013-09-25 12:04:37 -0400310 */
311 if (dig->dig_encoder == 0) {
312 WREG32(DCCG_AUDIO_DTO0_PHASE, base_rate * 100);
313 WREG32(DCCG_AUDIO_DTO0_MODULE, clock * 100);
314 WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
315 } else {
316 WREG32(DCCG_AUDIO_DTO1_PHASE, base_rate * 100);
317 WREG32(DCCG_AUDIO_DTO1_MODULE, clock * 100);
318 WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
319 }
Alex Deucher15865052013-04-22 09:42:07 -0400320 }
Alex Deucherb1f6f472013-04-18 10:50:55 -0400321}
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200322
323/*
324 * update the info frames with the data from the current display mode
325 */
326void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
327{
328 struct drm_device *dev = encoder->dev;
329 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200330 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
331 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100332 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
333 struct hdmi_avi_infoframe frame;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200334 uint32_t offset;
Rafał Miłecki2e93cac2014-05-16 11:10:29 +0200335 uint32_t acr_ctl;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100336 ssize_t err;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200337
Alex Deucherc2b4cacf2013-07-08 18:16:56 -0400338 if (!dig || !dig->afmt)
339 return;
340
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200341 /* Silent, r600_hdmi_enable will raise WARN for us */
342 if (!dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200343 return;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200344 offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200345
Alex Deucher832eafa2014-02-18 11:07:55 -0500346 /* disable audio prior to setting up hw */
347 dig->afmt->pin = r600_audio_get_pin(rdev);
348 r600_audio_enable(rdev, dig->afmt->pin, false);
349
Alex Deucherb1f6f472013-04-18 10:50:55 -0400350 r600_audio_set_dto(encoder, mode->clock);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200351
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200352 WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
353 HDMI0_NULL_SEND); /* send null packets when required */
354
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200355 WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
356 HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
357 HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
358 HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
359 HDMI0_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
Alex Deucher0ffae602013-08-15 12:03:37 -0400360
Rafał Miłecki2e93cac2014-05-16 11:10:29 +0200361 /* DCE 3.0 uses register that's normally for CRC_CONTROL */
362 acr_ctl = ASIC_IS_DCE3(rdev) ? DCE3_HDMI0_ACR_PACKET_CONTROL :
363 HDMI0_ACR_PACKET_CONTROL;
364 WREG32(acr_ctl + offset,
Alex Deucherb852c982013-10-10 11:47:01 -0400365 HDMI0_ACR_SOURCE | /* select SW CTS value - XXX verify that hw CTS works on all families */
Alex Deucheree0fec32013-09-27 18:22:15 -0400366 HDMI0_ACR_AUTO_SEND); /* allow hw to sent ACR packets when required */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200367
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200368 WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
369 HDMI0_NULL_SEND | /* send null packets when required */
370 HDMI0_GC_SEND | /* send general control packets */
371 HDMI0_GC_CONT); /* send general control packets every frame */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200372
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200373 /* TODO: HDMI0_AUDIO_INFO_UPDATE */
374 WREG32(HDMI0_INFOFRAME_CONTROL0 + offset,
375 HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
376 HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
377 HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
378 HDMI0_AUDIO_INFO_CONT); /* send audio info frames every frame/field */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200379
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200380 WREG32(HDMI0_INFOFRAME_CONTROL1 + offset,
381 HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
382 HDMI0_AUDIO_INFO_LINE(2)); /* anything other than 0 */
383
384 WREG32(HDMI0_GC + offset, 0); /* unset HDMI0_GC_AVMUTE */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200385
Thierry Redinge3b2e032013-01-14 13:36:30 +0100386 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
387 if (err < 0) {
388 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
389 return;
390 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200391
Thierry Redinge3b2e032013-01-14 13:36:30 +0100392 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
393 if (err < 0) {
394 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
395 return;
396 }
397
398 r600_hdmi_update_avi_infoframe(encoder, buffer, sizeof(buffer));
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200399 r600_hdmi_update_ACR(encoder, mode->clock);
400
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300401 /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200402 WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
403 WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
404 WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
405 WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200406
407 r600_hdmi_audio_workaround(encoder);
Alex Deucher832eafa2014-02-18 11:07:55 -0500408
409 /* enable audio after to setting up hw */
410 r600_audio_enable(rdev, dig->afmt->pin, true);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200411}
412
413/*
414 * update settings with current parameters from audio engine
415 */
Christian König58bd0862010-04-05 22:14:55 +0200416void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200417{
418 struct drm_device *dev = encoder->dev;
419 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200420 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
421 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb5306022013-07-31 16:51:33 -0400422 struct r600_audio_pin audio = r600_audio_status(rdev);
Thierry Redinge3b2e032013-01-14 13:36:30 +0100423 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
424 struct hdmi_audio_infoframe frame;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200425 uint32_t offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200426 uint32_t iec;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100427 ssize_t err;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200428
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200429 if (!dig->afmt || !dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200430 return;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200431 offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200432
433 DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
434 r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
Rafał Miłecki3299de92012-05-14 21:25:57 +0200435 audio.channels, audio.rate, audio.bits_per_sample);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200436 DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
Rafał Miłecki3299de92012-05-14 21:25:57 +0200437 (int)audio.status_bits, (int)audio.category_code);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200438
439 iec = 0;
Rafał Miłecki3299de92012-05-14 21:25:57 +0200440 if (audio.status_bits & AUDIO_STATUS_PROFESSIONAL)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200441 iec |= 1 << 0;
Rafał Miłecki3299de92012-05-14 21:25:57 +0200442 if (audio.status_bits & AUDIO_STATUS_NONAUDIO)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200443 iec |= 1 << 1;
Rafał Miłecki3299de92012-05-14 21:25:57 +0200444 if (audio.status_bits & AUDIO_STATUS_COPYRIGHT)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200445 iec |= 1 << 2;
Rafał Miłecki3299de92012-05-14 21:25:57 +0200446 if (audio.status_bits & AUDIO_STATUS_EMPHASIS)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200447 iec |= 1 << 3;
448
Rafał Miłecki3299de92012-05-14 21:25:57 +0200449 iec |= HDMI0_60958_CS_CATEGORY_CODE(audio.category_code);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200450
Rafał Miłecki3299de92012-05-14 21:25:57 +0200451 switch (audio.rate) {
Rafał Miłeckia366e392012-05-06 17:29:46 +0200452 case 32000:
453 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x3);
454 break;
455 case 44100:
456 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x0);
457 break;
458 case 48000:
459 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x2);
460 break;
461 case 88200:
462 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x8);
463 break;
464 case 96000:
465 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xa);
466 break;
467 case 176400:
468 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xc);
469 break;
470 case 192000:
471 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xe);
472 break;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200473 }
474
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200475 WREG32(HDMI0_60958_0 + offset, iec);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200476
477 iec = 0;
Rafał Miłecki3299de92012-05-14 21:25:57 +0200478 switch (audio.bits_per_sample) {
Rafał Miłeckia366e392012-05-06 17:29:46 +0200479 case 16:
480 iec |= HDMI0_60958_CS_WORD_LENGTH(0x2);
481 break;
482 case 20:
483 iec |= HDMI0_60958_CS_WORD_LENGTH(0x3);
484 break;
485 case 24:
486 iec |= HDMI0_60958_CS_WORD_LENGTH(0xb);
487 break;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200488 }
Rafał Miłecki3299de92012-05-14 21:25:57 +0200489 if (audio.status_bits & AUDIO_STATUS_V)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200490 iec |= 0x5 << 16;
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200491 WREG32_P(HDMI0_60958_1 + offset, iec, ~0x5000f);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200492
Thierry Redinge3b2e032013-01-14 13:36:30 +0100493 err = hdmi_audio_infoframe_init(&frame);
494 if (err < 0) {
495 DRM_ERROR("failed to setup audio infoframe\n");
496 return;
497 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200498
Thierry Redinge3b2e032013-01-14 13:36:30 +0100499 frame.channels = audio.channels;
500
501 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
502 if (err < 0) {
503 DRM_ERROR("failed to pack audio infoframe\n");
504 return;
505 }
506
507 r600_hdmi_update_audio_infoframe(encoder, buffer, sizeof(buffer));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200508 r600_hdmi_audio_workaround(encoder);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200509}
510
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200511/*
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000512 * enable the HDMI engine
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200513 */
Alex Deuchera973bea2013-04-18 11:32:16 -0400514void r600_hdmi_enable(struct drm_encoder *encoder, bool enable)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200515{
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000516 struct drm_device *dev = encoder->dev;
517 struct radeon_device *rdev = dev->dev_private;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200518 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200519 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deuchera973bea2013-04-18 11:32:16 -0400520 u32 hdmi = HDMI0_ERROR_ACK;
Alex Deucher16823d12010-04-16 11:35:30 -0400521
Alex Deucherc2b4cacf2013-07-08 18:16:56 -0400522 if (!dig || !dig->afmt)
523 return;
524
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200525 /* Silent, r600_hdmi_enable will raise WARN for us */
Alex Deuchera973bea2013-04-18 11:32:16 -0400526 if (enable && dig->afmt->enabled)
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200527 return;
Alex Deuchera973bea2013-04-18 11:32:16 -0400528 if (!enable && !dig->afmt->enabled)
529 return;
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200530
531 /* Older chipsets require setting HDMI and routing manually */
Alex Deuchera973bea2013-04-18 11:32:16 -0400532 if (!ASIC_IS_DCE3(rdev)) {
533 if (enable)
534 hdmi |= HDMI0_ENABLE;
Rafał Miłecki5715f672010-03-06 13:03:35 +0000535 switch (radeon_encoder->encoder_id) {
536 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400537 if (enable) {
538 WREG32_OR(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN);
539 hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
540 } else {
541 WREG32_AND(AVIVO_TMDSA_CNTL, ~AVIVO_TMDSA_CNTL_HDMI_EN);
542 }
Rafał Miłecki5715f672010-03-06 13:03:35 +0000543 break;
544 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400545 if (enable) {
546 WREG32_OR(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN);
547 hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
548 } else {
549 WREG32_AND(AVIVO_LVTMA_CNTL, ~AVIVO_LVTMA_CNTL_HDMI_EN);
550 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200551 break;
552 case ENCODER_OBJECT_ID_INTERNAL_DDI:
Alex Deuchera973bea2013-04-18 11:32:16 -0400553 if (enable) {
554 WREG32_OR(DDIA_CNTL, DDIA_HDMI_EN);
555 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
556 } else {
557 WREG32_AND(DDIA_CNTL, ~DDIA_HDMI_EN);
558 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200559 break;
560 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400561 if (enable)
562 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000563 break;
564 default:
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200565 dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
566 radeon_encoder->encoder_id);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000567 break;
568 }
Alex Deuchera973bea2013-04-18 11:32:16 -0400569 WREG32(HDMI0_CONTROL + dig->afmt->offset, hdmi);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000570 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200571
Alex Deucherf122c612012-03-30 08:59:57 -0400572 if (rdev->irq.installed) {
Christian Koenigf2594932010-04-10 03:13:16 +0200573 /* if irq is available use it */
Alex Deucher9054ae12013-04-18 09:42:13 -0400574 /* XXX: shouldn't need this on any asics. Double check DCE2/3 */
Alex Deuchera973bea2013-04-18 11:32:16 -0400575 if (enable)
Alex Deucher9054ae12013-04-18 09:42:13 -0400576 radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
Alex Deuchera973bea2013-04-18 11:32:16 -0400577 else
578 radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
Christian Koenigf2594932010-04-10 03:13:16 +0200579 }
Christian König58bd0862010-04-05 22:14:55 +0200580
Alex Deuchera973bea2013-04-18 11:32:16 -0400581 dig->afmt->enabled = enable;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200582
Alex Deuchera973bea2013-04-18 11:32:16 -0400583 DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
584 enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000585}
586