blob: 7d24753108fa9fe745fcd825f302dfd3efa88b3b [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 */
26#include "drmP.h"
27#include "radeon_drm.h"
28#include "radeon.h"
Daniel Vetter3574dda2011-02-18 17:59:19 +010029#include "radeon_asic.h"
Rafał Miłeckic6543a62012-04-28 23:35:24 +020030#include "r600d.h"
Christian Koenigdafc3bd2009-10-11 23:49:13 +020031#include "atom.h"
32
33/*
34 * HDMI color format
35 */
36enum r600_hdmi_color_format {
37 RGB = 0,
38 YCC_422 = 1,
39 YCC_444 = 2
40};
41
42/*
43 * IEC60958 status bits
44 */
45enum r600_hdmi_iec_status_bits {
46 AUDIO_STATUS_DIG_ENABLE = 0x01,
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000047 AUDIO_STATUS_V = 0x02,
48 AUDIO_STATUS_VCFG = 0x04,
Christian Koenigdafc3bd2009-10-11 23:49:13 +020049 AUDIO_STATUS_EMPHASIS = 0x08,
50 AUDIO_STATUS_COPYRIGHT = 0x10,
51 AUDIO_STATUS_NONAUDIO = 0x20,
52 AUDIO_STATUS_PROFESSIONAL = 0x40,
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000053 AUDIO_STATUS_LEVEL = 0x80
Christian Koenigdafc3bd2009-10-11 23:49:13 +020054};
55
56struct {
57 uint32_t Clock;
58
59 int N_32kHz;
60 int CTS_32kHz;
61
62 int N_44_1kHz;
63 int CTS_44_1kHz;
64
65 int N_48kHz;
66 int CTS_48kHz;
67
68} r600_hdmi_ACR[] = {
69 /* 32kHz 44.1kHz 48kHz */
70 /* Clock N CTS N CTS N CTS */
71 { 25174, 4576, 28125, 7007, 31250, 6864, 28125 }, /* 25,20/1.001 MHz */
72 { 25200, 4096, 25200, 6272, 28000, 6144, 25200 }, /* 25.20 MHz */
73 { 27000, 4096, 27000, 6272, 30000, 6144, 27000 }, /* 27.00 MHz */
74 { 27027, 4096, 27027, 6272, 30030, 6144, 27027 }, /* 27.00*1.001 MHz */
75 { 54000, 4096, 54000, 6272, 60000, 6144, 54000 }, /* 54.00 MHz */
76 { 54054, 4096, 54054, 6272, 60060, 6144, 54054 }, /* 54.00*1.001 MHz */
77 { 74175, 11648, 210937, 17836, 234375, 11648, 140625 }, /* 74.25/1.001 MHz */
78 { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */
79 { 148351, 11648, 421875, 8918, 234375, 5824, 140625 }, /* 148.50/1.001 MHz */
80 { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */
81 { 0, 4096, 0, 6272, 0, 6144, 0 } /* Other */
82};
83
84/*
85 * calculate CTS value if it's not found in the table
86 */
87static void r600_hdmi_calc_CTS(uint32_t clock, int *CTS, int N, int freq)
88{
89 if (*CTS == 0)
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000090 *CTS = clock * N / (128 * freq) * 1000;
Christian Koenigdafc3bd2009-10-11 23:49:13 +020091 DRM_DEBUG("Using ACR timing N=%d CTS=%d for frequency %d\n",
92 N, *CTS, freq);
93}
94
95/*
96 * update the N and CTS parameters for a given pixel clock rate
97 */
98static void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
99{
100 struct drm_device *dev = encoder->dev;
101 struct radeon_device *rdev = dev->dev_private;
102 uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
103 int CTS;
104 int N;
105 int i;
106
107 for (i = 0; r600_hdmi_ACR[i].Clock != clock && r600_hdmi_ACR[i].Clock != 0; i++);
108
109 CTS = r600_hdmi_ACR[i].CTS_32kHz;
110 N = r600_hdmi_ACR[i].N_32kHz;
111 r600_hdmi_calc_CTS(clock, &CTS, N, 32000);
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200112 WREG32(HDMI0_ACR_32_0 + offset, HDMI0_ACR_CTS_32(CTS));
113 WREG32(HDMI0_ACR_32_1 + offset, N);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200114
115 CTS = r600_hdmi_ACR[i].CTS_44_1kHz;
116 N = r600_hdmi_ACR[i].N_44_1kHz;
117 r600_hdmi_calc_CTS(clock, &CTS, N, 44100);
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200118 WREG32(HDMI0_ACR_44_0 + offset, HDMI0_ACR_CTS_44(CTS));
119 WREG32(HDMI0_ACR_44_1 + offset, N);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200120
121 CTS = r600_hdmi_ACR[i].CTS_48kHz;
122 N = r600_hdmi_ACR[i].N_48kHz;
123 r600_hdmi_calc_CTS(clock, &CTS, N, 48000);
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200124 WREG32(HDMI0_ACR_48_0 + offset, HDMI0_ACR_CTS_48(CTS));
125 WREG32(HDMI0_ACR_48_1 + offset, N);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200126}
127
128/*
129 * calculate the crc for a given info frame
130 */
131static void r600_hdmi_infoframe_checksum(uint8_t packetType,
132 uint8_t versionNumber,
133 uint8_t length,
134 uint8_t *frame)
135{
Rafał Miłecki3fe373d2010-03-06 13:03:38 +0000136 int i;
137 frame[0] = packetType + versionNumber + length;
138 for (i = 1; i <= length; i++)
139 frame[0] += frame[i];
140 frame[0] = 0x100 - frame[0];
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200141}
142
143/*
144 * build a HDMI Video Info Frame
145 */
146static void r600_hdmi_videoinfoframe(
147 struct drm_encoder *encoder,
148 enum r600_hdmi_color_format color_format,
149 int active_information_present,
150 uint8_t active_format_aspect_ratio,
151 uint8_t scan_information,
152 uint8_t colorimetry,
153 uint8_t ex_colorimetry,
154 uint8_t quantization,
155 int ITC,
156 uint8_t picture_aspect_ratio,
157 uint8_t video_format_identification,
158 uint8_t pixel_repetition,
159 uint8_t non_uniform_picture_scaling,
160 uint8_t bar_info_data_valid,
161 uint16_t top_bar,
162 uint16_t bottom_bar,
163 uint16_t left_bar,
164 uint16_t right_bar
165)
166{
167 struct drm_device *dev = encoder->dev;
168 struct radeon_device *rdev = dev->dev_private;
169 uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
170
171 uint8_t frame[14];
172
173 frame[0x0] = 0;
174 frame[0x1] =
175 (scan_information & 0x3) |
176 ((bar_info_data_valid & 0x3) << 2) |
177 ((active_information_present & 0x1) << 4) |
178 ((color_format & 0x3) << 5);
179 frame[0x2] =
180 (active_format_aspect_ratio & 0xF) |
181 ((picture_aspect_ratio & 0x3) << 4) |
182 ((colorimetry & 0x3) << 6);
183 frame[0x3] =
184 (non_uniform_picture_scaling & 0x3) |
185 ((quantization & 0x3) << 2) |
186 ((ex_colorimetry & 0x7) << 4) |
187 ((ITC & 0x1) << 7);
188 frame[0x4] = (video_format_identification & 0x7F);
189 frame[0x5] = (pixel_repetition & 0xF);
190 frame[0x6] = (top_bar & 0xFF);
191 frame[0x7] = (top_bar >> 8);
192 frame[0x8] = (bottom_bar & 0xFF);
193 frame[0x9] = (bottom_bar >> 8);
194 frame[0xA] = (left_bar & 0xFF);
195 frame[0xB] = (left_bar >> 8);
196 frame[0xC] = (right_bar & 0xFF);
197 frame[0xD] = (right_bar >> 8);
198
199 r600_hdmi_infoframe_checksum(0x82, 0x02, 0x0D, frame);
Rafał Miłecki92db7f62011-12-23 20:32:18 +0100200 /* Our header values (type, version, length) should be alright, Intel
201 * is using the same. Checksum function also seems to be OK, it works
202 * fine for audio infoframe. However calculated value is always lower
203 * by 2 in comparison to fglrx. It breaks displaying anything in case
204 * of TVs that strictly check the checksum. Hack it manually here to
205 * workaround this issue. */
206 frame[0x0] += 2;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200207
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200208 WREG32(HDMI0_AVI_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200209 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200210 WREG32(HDMI0_AVI_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200211 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200212 WREG32(HDMI0_AVI_INFO2 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200213 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200214 WREG32(HDMI0_AVI_INFO3 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200215 frame[0xC] | (frame[0xD] << 8));
216}
217
218/*
219 * build a Audio Info Frame
220 */
221static void r600_hdmi_audioinfoframe(
222 struct drm_encoder *encoder,
223 uint8_t channel_count,
224 uint8_t coding_type,
225 uint8_t sample_size,
226 uint8_t sample_frequency,
227 uint8_t format,
228 uint8_t channel_allocation,
229 uint8_t level_shift,
230 int downmix_inhibit
231)
232{
233 struct drm_device *dev = encoder->dev;
234 struct radeon_device *rdev = dev->dev_private;
235 uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
236
237 uint8_t frame[11];
238
239 frame[0x0] = 0;
240 frame[0x1] = (channel_count & 0x7) | ((coding_type & 0xF) << 4);
241 frame[0x2] = (sample_size & 0x3) | ((sample_frequency & 0x7) << 2);
242 frame[0x3] = format;
243 frame[0x4] = channel_allocation;
244 frame[0x5] = ((level_shift & 0xF) << 3) | ((downmix_inhibit & 0x1) << 7);
245 frame[0x6] = 0;
246 frame[0x7] = 0;
247 frame[0x8] = 0;
248 frame[0x9] = 0;
249 frame[0xA] = 0;
250
251 r600_hdmi_infoframe_checksum(0x84, 0x01, 0x0A, frame);
252
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200253 WREG32(HDMI0_AUDIO_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200254 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200255 WREG32(HDMI0_AUDIO_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200256 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
257}
258
259/*
260 * test if audio buffer is filled enough to start playing
261 */
262static int r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
263{
264 struct drm_device *dev = encoder->dev;
265 struct radeon_device *rdev = dev->dev_private;
266 uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
267
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200268 return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200269}
270
271/*
272 * have buffer status changed since last call?
273 */
274int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
275{
276 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
277 int status, result;
278
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200279 if (!radeon_encoder->hdmi_enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200280 return 0;
281
282 status = r600_hdmi_is_audio_buffer_filled(encoder);
283 result = radeon_encoder->hdmi_buffer_status != status;
284 radeon_encoder->hdmi_buffer_status = status;
285
286 return result;
287}
288
289/*
290 * write the audio workaround status to the hardware
291 */
292void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
293{
294 struct drm_device *dev = encoder->dev;
295 struct radeon_device *rdev = dev->dev_private;
296 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
297 uint32_t offset = radeon_encoder->hdmi_offset;
298
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200299 if (!radeon_encoder->hdmi_enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200300 return;
301
Christian Koenigf2594932010-04-10 03:13:16 +0200302 if (!radeon_encoder->hdmi_audio_workaround ||
303 r600_hdmi_is_audio_buffer_filled(encoder)) {
304
305 /* disable audio workaround */
Rafał Miłeckia273a902012-04-30 15:44:52 +0200306 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
307 0, ~HDMI0_AUDIO_TEST_EN);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200308
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200309 } else {
Christian Koenigf2594932010-04-10 03:13:16 +0200310 /* enable audio workaround */
Rafał Miłeckia273a902012-04-30 15:44:52 +0200311 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
312 HDMI0_AUDIO_TEST_EN, ~HDMI0_AUDIO_TEST_EN);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200313 }
314}
315
316
317/*
318 * update the info frames with the data from the current display mode
319 */
320void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
321{
322 struct drm_device *dev = encoder->dev;
323 struct radeon_device *rdev = dev->dev_private;
324 uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
325
Rafał Miłeckif83d9262011-12-23 20:36:06 +0100326 if (ASIC_IS_DCE5(rdev))
Alex Deucher16823d12010-04-16 11:35:30 -0400327 return;
328
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200329 if (!to_radeon_encoder(encoder)->hdmi_enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200330 return;
331
332 r600_audio_set_clock(encoder, mode->clock);
333
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200334 WREG32(HDMI0_AUDIO_CRC_CONTROL + offset, 0x1000);
335 WREG32(HDMI0_GC + offset, 0x0);
Rafał Miłeckia273a902012-04-30 15:44:52 +0200336
337 /* Send audio packets */
338 if (ASIC_IS_DCE4(rdev))
339 WREG32_P(0x74fc + offset,
340 AFMT_AUDIO_SAMPLE_SEND, ~AFMT_AUDIO_SAMPLE_SEND);
341 else if (ASIC_IS_DCE32(rdev))
342 WREG32_P(AFMT_AUDIO_PACKET_CONTROL + offset,
343 AFMT_AUDIO_SAMPLE_SEND, ~AFMT_AUDIO_SAMPLE_SEND);
344 else
345 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
346 HDMI0_AUDIO_SAMPLE_SEND, ~HDMI0_AUDIO_SAMPLE_SEND);
347
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200348 WREG32(HDMI0_ACR_PACKET_CONTROL + offset, 0x1000);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200349
350 r600_hdmi_update_ACR(encoder, mode->clock);
351
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200352 WREG32(HDMI0_INFOFRAME_CONTROL0 + offset, 0x13);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200353
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200354 WREG32(HDMI0_INFOFRAME_CONTROL1 + offset, 0x202);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200355
356 r600_hdmi_videoinfoframe(encoder, RGB, 0, 0, 0, 0,
357 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
358
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300359 /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200360 WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
361 WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
362 WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
363 WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200364
365 r600_hdmi_audio_workaround(encoder);
366
367 /* audio packets per line, does anyone know how to calc this ? */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200368 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset, 0x00040000, ~0x001F0000);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200369}
370
371/*
372 * update settings with current parameters from audio engine
373 */
Christian König58bd0862010-04-05 22:14:55 +0200374void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200375{
376 struct drm_device *dev = encoder->dev;
377 struct radeon_device *rdev = dev->dev_private;
378 uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
379
Christian König58bd0862010-04-05 22:14:55 +0200380 int channels = r600_audio_channels(rdev);
381 int rate = r600_audio_rate(rdev);
382 int bps = r600_audio_bits_per_sample(rdev);
383 uint8_t status_bits = r600_audio_status_bits(rdev);
384 uint8_t category_code = r600_audio_category_code(rdev);
385
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200386 uint32_t iec;
387
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200388 if (!to_radeon_encoder(encoder)->hdmi_enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200389 return;
390
391 DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
392 r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
393 channels, rate, bps);
394 DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
395 (int)status_bits, (int)category_code);
396
397 iec = 0;
398 if (status_bits & AUDIO_STATUS_PROFESSIONAL)
399 iec |= 1 << 0;
400 if (status_bits & AUDIO_STATUS_NONAUDIO)
401 iec |= 1 << 1;
402 if (status_bits & AUDIO_STATUS_COPYRIGHT)
403 iec |= 1 << 2;
404 if (status_bits & AUDIO_STATUS_EMPHASIS)
405 iec |= 1 << 3;
406
407 iec |= category_code << 8;
408
409 switch (rate) {
410 case 32000: iec |= 0x3 << 24; break;
411 case 44100: iec |= 0x0 << 24; break;
412 case 88200: iec |= 0x8 << 24; break;
413 case 176400: iec |= 0xc << 24; break;
414 case 48000: iec |= 0x2 << 24; break;
415 case 96000: iec |= 0xa << 24; break;
416 case 192000: iec |= 0xe << 24; break;
417 }
418
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200419 WREG32(HDMI0_60958_0 + offset, iec);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200420
421 iec = 0;
422 switch (bps) {
423 case 16: iec |= 0x2; break;
424 case 20: iec |= 0x3; break;
425 case 24: iec |= 0xb; break;
426 }
427 if (status_bits & AUDIO_STATUS_V)
428 iec |= 0x5 << 16;
429
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200430 WREG32_P(HDMI0_60958_1 + offset, iec, ~0x5000f);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200431
432 /* 0x021 or 0x031 sets the audio frame length */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200433 WREG32(HDMI0_VBI_PACKET_CONTROL + offset, 0x31);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200434 r600_hdmi_audioinfoframe(encoder, channels-1, 0, 0, 0, 0, 0, 0, 0);
435
436 r600_hdmi_audio_workaround(encoder);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200437}
438
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000439static void r600_hdmi_assign_block(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200440{
441 struct drm_device *dev = encoder->dev;
442 struct radeon_device *rdev = dev->dev_private;
443 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000444 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200445
Rafał Miłeckif83d9262011-12-23 20:36:06 +0100446 u16 eg_offsets[] = {
447 EVERGREEN_CRTC0_REGISTER_OFFSET,
448 EVERGREEN_CRTC1_REGISTER_OFFSET,
449 EVERGREEN_CRTC2_REGISTER_OFFSET,
450 EVERGREEN_CRTC3_REGISTER_OFFSET,
451 EVERGREEN_CRTC4_REGISTER_OFFSET,
452 EVERGREEN_CRTC5_REGISTER_OFFSET,
453 };
454
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000455 if (!dig) {
456 dev_err(rdev->dev, "Enabling HDMI on non-dig encoder\n");
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200457 return;
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000458 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200459
Rafał Miłeckiebcb7962011-12-04 11:23:51 +0100460 if (ASIC_IS_DCE5(rdev)) {
461 /* TODO */
462 } else if (ASIC_IS_DCE4(rdev)) {
Rafał Miłeckif83d9262011-12-23 20:36:06 +0100463 if (dig->dig_encoder >= ARRAY_SIZE(eg_offsets)) {
464 dev_err(rdev->dev, "Enabling HDMI on unknown dig\n");
465 return;
466 }
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200467 radeon_encoder->hdmi_offset = eg_offsets[dig->dig_encoder];
468 /* Temp hack for Evergreen until we split r600_hdmi.c
469 * Evergreen first block is 0x7030 instead of 0x7400.
470 */
471 radeon_encoder->hdmi_offset -= 0x3d0;
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000472 } else if (ASIC_IS_DCE3(rdev)) {
473 radeon_encoder->hdmi_offset = dig->dig_encoder ?
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200474 DCE3_HDMI_OFFSET1 : DCE3_HDMI_OFFSET0;
Rafał Miłecki816ce432012-04-28 23:35:22 +0200475 } else if (rdev->family >= CHIP_R600) {
476 /* 2 routable blocks, but using dig_encoder should be fine */
477 radeon_encoder->hdmi_offset = dig->dig_encoder ?
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200478 DCE2_HDMI_OFFSET1 : DCE2_HDMI_OFFSET0;
Rafał Miłecki816ce432012-04-28 23:35:22 +0200479 } else if (rdev->family == CHIP_RS600 || rdev->family == CHIP_RS690 ||
480 rdev->family == CHIP_RS740) {
481 /* Only 1 routable block */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200482 radeon_encoder->hdmi_offset = DCE2_HDMI_OFFSET0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200483 }
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200484 radeon_encoder->hdmi_enabled = true;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200485}
486
487/*
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000488 * enable the HDMI engine
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200489 */
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000490void r600_hdmi_enable(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200491{
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000492 struct drm_device *dev = encoder->dev;
493 struct radeon_device *rdev = dev->dev_private;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200494 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Christian Koenigf2594932010-04-10 03:13:16 +0200495 uint32_t offset;
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200496 u32 hdmi;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200497
Rafał Miłeckif83d9262011-12-23 20:36:06 +0100498 if (ASIC_IS_DCE5(rdev))
Alex Deucher16823d12010-04-16 11:35:30 -0400499 return;
500
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200501 if (!radeon_encoder->hdmi_enabled) {
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000502 r600_hdmi_assign_block(encoder);
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200503 if (!radeon_encoder->hdmi_enabled) {
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000504 dev_warn(rdev->dev, "Could not find HDMI block for "
505 "0x%x encoder\n", radeon_encoder->encoder_id);
506 return;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200507 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200508 }
509
Christian Koenigf2594932010-04-10 03:13:16 +0200510 offset = radeon_encoder->hdmi_offset;
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200511
512 /* Older chipsets require setting HDMI and routing manually */
513 if (rdev->family >= CHIP_R600 && !ASIC_IS_DCE3(rdev)) {
514 hdmi = HDMI0_ERROR_ACK | HDMI0_ENABLE;
Rafał Miłecki5715f672010-03-06 13:03:35 +0000515 switch (radeon_encoder->encoder_id) {
516 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
Rafał Miłecki93a4ed82011-12-24 12:25:36 +0100517 WREG32_P(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN,
518 ~AVIVO_TMDSA_CNTL_HDMI_EN);
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200519 hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000520 break;
521 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
Rafał Miłecki93a4ed82011-12-24 12:25:36 +0100522 WREG32_P(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN,
523 ~AVIVO_LVTMA_CNTL_HDMI_EN);
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200524 hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
525 break;
526 case ENCODER_OBJECT_ID_INTERNAL_DDI:
527 WREG32_P(DDIA_CNTL, DDIA_HDMI_EN, ~DDIA_HDMI_EN);
528 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
529 break;
530 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
531 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000532 break;
533 default:
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200534 dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
535 radeon_encoder->encoder_id);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000536 break;
537 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200538 WREG32(HDMI0_CONTROL + offset, hdmi);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000539 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200540
Alex Deucherf122c612012-03-30 08:59:57 -0400541 if (rdev->irq.installed) {
Christian Koenigf2594932010-04-10 03:13:16 +0200542 /* if irq is available use it */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200543 rdev->irq.afmt[offset == 0 ? 0 : 1] = true;
Christian Koenigf2594932010-04-10 03:13:16 +0200544 radeon_irq_set(rdev);
Christian Koenigf2594932010-04-10 03:13:16 +0200545 }
Christian König58bd0862010-04-05 22:14:55 +0200546
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000547 DRM_DEBUG("Enabling HDMI interface @ 0x%04X for encoder 0x%x\n",
548 radeon_encoder->hdmi_offset, radeon_encoder->encoder_id);
549}
550
551/*
552 * disable the HDMI engine
553 */
554void r600_hdmi_disable(struct drm_encoder *encoder)
555{
556 struct drm_device *dev = encoder->dev;
557 struct radeon_device *rdev = dev->dev_private;
558 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Dave Airlie66989982010-05-19 10:35:02 +1000559 uint32_t offset;
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000560
Rafał Miłeckif83d9262011-12-23 20:36:06 +0100561 if (ASIC_IS_DCE5(rdev))
Alex Deucher16823d12010-04-16 11:35:30 -0400562 return;
563
Christian Koenigf2594932010-04-10 03:13:16 +0200564 offset = radeon_encoder->hdmi_offset;
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200565 if (!radeon_encoder->hdmi_enabled) {
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000566 dev_err(rdev->dev, "Disabling not enabled HDMI\n");
567 return;
568 }
569
570 DRM_DEBUG("Disabling HDMI interface @ 0x%04X for encoder 0x%x\n",
Christian Koenigf2594932010-04-10 03:13:16 +0200571 offset, radeon_encoder->encoder_id);
572
573 /* disable irq */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200574 rdev->irq.afmt[offset == 0 ? 0 : 1] = false;
Christian Koenigf2594932010-04-10 03:13:16 +0200575 radeon_irq_set(rdev);
576
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200577 /* Older chipsets not handled by AtomBIOS */
578 if (rdev->family >= CHIP_R600 && !ASIC_IS_DCE3(rdev)) {
Rafał Miłecki5715f672010-03-06 13:03:35 +0000579 switch (radeon_encoder->encoder_id) {
580 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
Rafał Miłecki93a4ed82011-12-24 12:25:36 +0100581 WREG32_P(AVIVO_TMDSA_CNTL, 0,
582 ~AVIVO_TMDSA_CNTL_HDMI_EN);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000583 break;
584 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
Rafał Miłecki93a4ed82011-12-24 12:25:36 +0100585 WREG32_P(AVIVO_LVTMA_CNTL, 0,
586 ~AVIVO_LVTMA_CNTL_HDMI_EN);
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200587 break;
588 case ENCODER_OBJECT_ID_INTERNAL_DDI:
589 WREG32_P(DDIA_CNTL, 0, ~DDIA_HDMI_EN);
590 break;
591 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
Rafał Miłecki5715f672010-03-06 13:03:35 +0000592 break;
593 default:
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200594 dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
595 radeon_encoder->encoder_id);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000596 break;
597 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200598 WREG32(HDMI0_CONTROL + offset, HDMI0_ERROR_ACK);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000599 }
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000600
Rafał Miłeckiaf0b5742012-04-28 23:35:23 +0200601 radeon_encoder->hdmi_enabled = false;
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000602 radeon_encoder->hdmi_offset = 0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200603}