xref: /openbsd-src/sys/dev/pci/drm/radeon/r600_hdmi.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: r600_hdmi.c,v 1.2 2014/02/09 13:43:02 jsg Exp $	*/
2 /*
3  * Copyright 2008 Advanced Micro Devices, Inc.
4  * Copyright 2008 Red Hat Inc.
5  * Copyright 2009 Christian König.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors: Christian König
26  */
27 #include <dev/pci/drm/drmP.h>
28 #include <dev/pci/drm/radeon_drm.h>
29 #include "radeon.h"
30 #include "radeon_asic.h"
31 #include "r600d.h"
32 #include "atom.h"
33 
34 /*
35  * HDMI color format
36  */
37 enum r600_hdmi_color_format {
38 	RGB = 0,
39 	YCC_422 = 1,
40 	YCC_444 = 2
41 };
42 
43 /*
44  * IEC60958 status bits
45  */
46 enum r600_hdmi_iec_status_bits {
47 	AUDIO_STATUS_DIG_ENABLE   = 0x01,
48 	AUDIO_STATUS_V            = 0x02,
49 	AUDIO_STATUS_VCFG         = 0x04,
50 	AUDIO_STATUS_EMPHASIS     = 0x08,
51 	AUDIO_STATUS_COPYRIGHT    = 0x10,
52 	AUDIO_STATUS_NONAUDIO     = 0x20,
53 	AUDIO_STATUS_PROFESSIONAL = 0x40,
54 	AUDIO_STATUS_LEVEL        = 0x80
55 };
56 
57 static const struct radeon_hdmi_acr r600_hdmi_predefined_acr[] = {
58     /*	     32kHz	  44.1kHz	48kHz    */
59     /* Clock      N     CTS      N     CTS      N     CTS */
60     {  25174,  4576,  28125,  7007,  31250,  6864,  28125 }, /*  25,20/1.001 MHz */
61     {  25200,  4096,  25200,  6272,  28000,  6144,  25200 }, /*  25.20       MHz */
62     {  27000,  4096,  27000,  6272,  30000,  6144,  27000 }, /*  27.00       MHz */
63     {  27027,  4096,  27027,  6272,  30030,  6144,  27027 }, /*  27.00*1.001 MHz */
64     {  54000,  4096,  54000,  6272,  60000,  6144,  54000 }, /*  54.00       MHz */
65     {  54054,  4096,  54054,  6272,  60060,  6144,  54054 }, /*  54.00*1.001 MHz */
66     {  74175, 11648, 210937, 17836, 234375, 11648, 140625 }, /*  74.25/1.001 MHz */
67     {  74250,  4096,  74250,  6272,  82500,  6144,  74250 }, /*  74.25       MHz */
68     { 148351, 11648, 421875,  8918, 234375,  5824, 140625 }, /* 148.50/1.001 MHz */
69     { 148500,  4096, 148500,  6272, 165000,  6144, 148500 }, /* 148.50       MHz */
70     {      0,  4096,      0,  6272,      0,  6144,      0 }  /* Other */
71 };
72 
73 /*
74  * calculate CTS value if it's not found in the table
75  */
76 static void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int N, int freq)
77 {
78 	if (*CTS == 0)
79 		*CTS = clock * N / (128 * freq) * 1000;
80 	DRM_DEBUG("Using ACR timing N=%d CTS=%d for frequency %d\n",
81 		  N, *CTS, freq);
82 }
83 
84 struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
85 {
86 	struct radeon_hdmi_acr res;
87 	u8 i;
88 
89 	for (i = 0; r600_hdmi_predefined_acr[i].clock != clock &&
90 	     r600_hdmi_predefined_acr[i].clock != 0; i++)
91 		;
92 	res = r600_hdmi_predefined_acr[i];
93 
94 	/* In case some CTS are missing */
95 	r600_hdmi_calc_cts(clock, &res.cts_32khz, res.n_32khz, 32000);
96 	r600_hdmi_calc_cts(clock, &res.cts_44_1khz, res.n_44_1khz, 44100);
97 	r600_hdmi_calc_cts(clock, &res.cts_48khz, res.n_48khz, 48000);
98 
99 	return res;
100 }
101 
102 /*
103  * update the N and CTS parameters for a given pixel clock rate
104  */
105 static void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
106 {
107 	struct drm_device *dev = encoder->dev;
108 	struct radeon_device *rdev = dev->dev_private;
109 	struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
110 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
111 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
112 	uint32_t offset = dig->afmt->offset;
113 
114 	WREG32(HDMI0_ACR_32_0 + offset, HDMI0_ACR_CTS_32(acr.cts_32khz));
115 	WREG32(HDMI0_ACR_32_1 + offset, acr.n_32khz);
116 
117 	WREG32(HDMI0_ACR_44_0 + offset, HDMI0_ACR_CTS_44(acr.cts_44_1khz));
118 	WREG32(HDMI0_ACR_44_1 + offset, acr.n_44_1khz);
119 
120 	WREG32(HDMI0_ACR_48_0 + offset, HDMI0_ACR_CTS_48(acr.cts_48khz));
121 	WREG32(HDMI0_ACR_48_1 + offset, acr.n_48khz);
122 }
123 
124 /*
125  * calculate the crc for a given info frame
126  */
127 static void r600_hdmi_infoframe_checksum(uint8_t packetType,
128 					 uint8_t versionNumber,
129 					 uint8_t length,
130 					 uint8_t *frame)
131 {
132 	int i;
133 	frame[0] = packetType + versionNumber + length;
134 	for (i = 1; i <= length; i++)
135 		frame[0] += frame[i];
136 	frame[0] = 0x100 - frame[0];
137 }
138 
139 /*
140  * build a HDMI Video Info Frame
141  */
142 static void r600_hdmi_videoinfoframe(
143 	struct drm_encoder *encoder,
144 	enum r600_hdmi_color_format color_format,
145 	int active_information_present,
146 	uint8_t active_format_aspect_ratio,
147 	uint8_t scan_information,
148 	uint8_t colorimetry,
149 	uint8_t ex_colorimetry,
150 	uint8_t quantization,
151 	int ITC,
152 	uint8_t picture_aspect_ratio,
153 	uint8_t video_format_identification,
154 	uint8_t pixel_repetition,
155 	uint8_t non_uniform_picture_scaling,
156 	uint8_t bar_info_data_valid,
157 	uint16_t top_bar,
158 	uint16_t bottom_bar,
159 	uint16_t left_bar,
160 	uint16_t right_bar
161 )
162 {
163 	struct drm_device *dev = encoder->dev;
164 	struct radeon_device *rdev = dev->dev_private;
165 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
166 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
167 	uint32_t offset = dig->afmt->offset;
168 
169 	uint8_t frame[14];
170 
171 	frame[0x0] = 0;
172 	frame[0x1] =
173 		(scan_information & 0x3) |
174 		((bar_info_data_valid & 0x3) << 2) |
175 		((active_information_present & 0x1) << 4) |
176 		((color_format & 0x3) << 5);
177 	frame[0x2] =
178 		(active_format_aspect_ratio & 0xF) |
179 		((picture_aspect_ratio & 0x3) << 4) |
180 		((colorimetry & 0x3) << 6);
181 	frame[0x3] =
182 		(non_uniform_picture_scaling & 0x3) |
183 		((quantization & 0x3) << 2) |
184 		((ex_colorimetry & 0x7) << 4) |
185 		((ITC & 0x1) << 7);
186 	frame[0x4] = (video_format_identification & 0x7F);
187 	frame[0x5] = (pixel_repetition & 0xF);
188 	frame[0x6] = (top_bar & 0xFF);
189 	frame[0x7] = (top_bar >> 8);
190 	frame[0x8] = (bottom_bar & 0xFF);
191 	frame[0x9] = (bottom_bar >> 8);
192 	frame[0xA] = (left_bar & 0xFF);
193 	frame[0xB] = (left_bar >> 8);
194 	frame[0xC] = (right_bar & 0xFF);
195 	frame[0xD] = (right_bar >> 8);
196 
197 	r600_hdmi_infoframe_checksum(0x82, 0x02, 0x0D, frame);
198 	/* Our header values (type, version, length) should be alright, Intel
199 	 * is using the same. Checksum function also seems to be OK, it works
200 	 * fine for audio infoframe. However calculated value is always lower
201 	 * by 2 in comparison to fglrx. It breaks displaying anything in case
202 	 * of TVs that strictly check the checksum. Hack it manually here to
203 	 * workaround this issue. */
204 	frame[0x0] += 2;
205 
206 	WREG32(HDMI0_AVI_INFO0 + offset,
207 		frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
208 	WREG32(HDMI0_AVI_INFO1 + offset,
209 		frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
210 	WREG32(HDMI0_AVI_INFO2 + offset,
211 		frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
212 	WREG32(HDMI0_AVI_INFO3 + offset,
213 		frame[0xC] | (frame[0xD] << 8));
214 }
215 
216 /*
217  * build a Audio Info Frame
218  */
219 static void r600_hdmi_audioinfoframe(
220 	struct drm_encoder *encoder,
221 	uint8_t channel_count,
222 	uint8_t coding_type,
223 	uint8_t sample_size,
224 	uint8_t sample_frequency,
225 	uint8_t format,
226 	uint8_t channel_allocation,
227 	uint8_t level_shift,
228 	int downmix_inhibit
229 )
230 {
231 	struct drm_device *dev = encoder->dev;
232 	struct radeon_device *rdev = dev->dev_private;
233 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
234 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
235 	uint32_t offset = dig->afmt->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 
253 	WREG32(HDMI0_AUDIO_INFO0 + offset,
254 		frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
255 	WREG32(HDMI0_AUDIO_INFO1 + offset,
256 		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  */
262 static bool 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 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
267 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
268 	uint32_t offset = dig->afmt->offset;
269 
270 	return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
271 }
272 
273 /*
274  * have buffer status changed since last call?
275  */
276 int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
277 {
278 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
279 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
280 	int status, result;
281 
282 	if (!dig->afmt || !dig->afmt->enabled)
283 		return 0;
284 
285 	status = r600_hdmi_is_audio_buffer_filled(encoder);
286 	result = dig->afmt->last_buffer_filled_status != status;
287 	dig->afmt->last_buffer_filled_status = status;
288 
289 	return result;
290 }
291 
292 /*
293  * write the audio workaround status to the hardware
294  */
295 static void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
296 {
297 	struct drm_device *dev = encoder->dev;
298 	struct radeon_device *rdev = dev->dev_private;
299 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
300 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
301 	uint32_t offset = dig->afmt->offset;
302 	bool hdmi_audio_workaround = false; /* FIXME */
303 	u32 value;
304 
305 	if (!hdmi_audio_workaround ||
306 	    r600_hdmi_is_audio_buffer_filled(encoder))
307 		value = 0; /* disable workaround */
308 	else
309 		value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
310 	WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
311 		 value, ~HDMI0_AUDIO_TEST_EN);
312 }
313 
314 
315 /*
316  * update the info frames with the data from the current display mode
317  */
318 void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
319 {
320 	struct drm_device *dev = encoder->dev;
321 	struct radeon_device *rdev = dev->dev_private;
322 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
323 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
324 	uint32_t offset;
325 
326 	if (!dig || !dig->afmt)
327 		return;
328 
329 	/* Silent, r600_hdmi_enable will raise WARN for us */
330 	if (!dig->afmt->enabled)
331 		return;
332 	offset = dig->afmt->offset;
333 
334 	r600_audio_set_clock(encoder, mode->clock);
335 
336 	WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
337 	       HDMI0_NULL_SEND); /* send null packets when required */
338 
339 	WREG32(HDMI0_AUDIO_CRC_CONTROL + offset, 0x1000);
340 
341 	if (ASIC_IS_DCE32(rdev)) {
342 		WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
343 		       HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
344 		       HDMI0_AUDIO_PACKETS_PER_LINE(3)); /* should be suffient for all audio modes and small enough for all hblanks */
345 		WREG32(AFMT_AUDIO_PACKET_CONTROL + offset,
346 		       AFMT_AUDIO_SAMPLE_SEND | /* send audio packets */
347 		       AFMT_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
348 	} else {
349 		WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
350 		       HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
351 		       HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
352 		       HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
353 		       HDMI0_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
354 	}
355 
356 	WREG32(HDMI0_ACR_PACKET_CONTROL + offset,
357 	       HDMI0_ACR_AUTO_SEND | /* allow hw to sent ACR packets when required */
358 	       HDMI0_ACR_SOURCE); /* select SW CTS value */
359 
360 	WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
361 	       HDMI0_NULL_SEND | /* send null packets when required */
362 	       HDMI0_GC_SEND | /* send general control packets */
363 	       HDMI0_GC_CONT); /* send general control packets every frame */
364 
365 	/* TODO: HDMI0_AUDIO_INFO_UPDATE */
366 	WREG32(HDMI0_INFOFRAME_CONTROL0 + offset,
367 	       HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
368 	       HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
369 	       HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
370 	       HDMI0_AUDIO_INFO_CONT); /* send audio info frames every frame/field */
371 
372 	WREG32(HDMI0_INFOFRAME_CONTROL1 + offset,
373 	       HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
374 	       HDMI0_AUDIO_INFO_LINE(2)); /* anything other than 0 */
375 
376 	WREG32(HDMI0_GC + offset, 0); /* unset HDMI0_GC_AVMUTE */
377 
378 	r600_hdmi_videoinfoframe(encoder, RGB, 0, 0, 0, 0,
379 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
380 
381 	r600_hdmi_update_ACR(encoder, mode->clock);
382 
383 	/* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
384 	WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
385 	WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
386 	WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
387 	WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
388 
389 	r600_hdmi_audio_workaround(encoder);
390 }
391 
392 /*
393  * update settings with current parameters from audio engine
394  */
395 void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
396 {
397 	struct drm_device *dev = encoder->dev;
398 	struct radeon_device *rdev = dev->dev_private;
399 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
400 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
401 	struct r600_audio audio = r600_audio_status(rdev);
402 	uint32_t offset;
403 	uint32_t iec;
404 
405 	if (!dig->afmt || !dig->afmt->enabled)
406 		return;
407 	offset = dig->afmt->offset;
408 
409 	DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
410 		 r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
411 		  audio.channels, audio.rate, audio.bits_per_sample);
412 	DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
413 		  (int)audio.status_bits, (int)audio.category_code);
414 
415 	iec = 0;
416 	if (audio.status_bits & AUDIO_STATUS_PROFESSIONAL)
417 		iec |= 1 << 0;
418 	if (audio.status_bits & AUDIO_STATUS_NONAUDIO)
419 		iec |= 1 << 1;
420 	if (audio.status_bits & AUDIO_STATUS_COPYRIGHT)
421 		iec |= 1 << 2;
422 	if (audio.status_bits & AUDIO_STATUS_EMPHASIS)
423 		iec |= 1 << 3;
424 
425 	iec |= HDMI0_60958_CS_CATEGORY_CODE(audio.category_code);
426 
427 	switch (audio.rate) {
428 	case 32000:
429 		iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x3);
430 		break;
431 	case 44100:
432 		iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x0);
433 		break;
434 	case 48000:
435 		iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x2);
436 		break;
437 	case 88200:
438 		iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x8);
439 		break;
440 	case 96000:
441 		iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xa);
442 		break;
443 	case 176400:
444 		iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xc);
445 		break;
446 	case 192000:
447 		iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xe);
448 		break;
449 	}
450 
451 	WREG32(HDMI0_60958_0 + offset, iec);
452 
453 	iec = 0;
454 	switch (audio.bits_per_sample) {
455 	case 16:
456 		iec |= HDMI0_60958_CS_WORD_LENGTH(0x2);
457 		break;
458 	case 20:
459 		iec |= HDMI0_60958_CS_WORD_LENGTH(0x3);
460 		break;
461 	case 24:
462 		iec |= HDMI0_60958_CS_WORD_LENGTH(0xb);
463 		break;
464 	}
465 	if (audio.status_bits & AUDIO_STATUS_V)
466 		iec |= 0x5 << 16;
467 	WREG32_P(HDMI0_60958_1 + offset, iec, ~0x5000f);
468 
469 	r600_hdmi_audioinfoframe(encoder, audio.channels - 1, 0, 0, 0, 0, 0, 0,
470 				 0);
471 
472 	r600_hdmi_audio_workaround(encoder);
473 }
474 
475 /*
476  * enable the HDMI engine
477  */
478 void r600_hdmi_enable(struct drm_encoder *encoder)
479 {
480 	struct drm_device *dev = encoder->dev;
481 	struct radeon_device *rdev = dev->dev_private;
482 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
483 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
484 	uint32_t offset;
485 	u32 hdmi;
486 
487 	if (!dig || !dig->afmt)
488 		return;
489 
490 	if (ASIC_IS_DCE6(rdev))
491 		return;
492 
493 	/* Silent, r600_hdmi_enable will raise WARN for us */
494 	if (dig->afmt->enabled)
495 		return;
496 	offset = dig->afmt->offset;
497 
498 	/* Older chipsets require setting HDMI and routing manually */
499 	if (ASIC_IS_DCE2(rdev) && !ASIC_IS_DCE3(rdev)) {
500 		hdmi = HDMI0_ERROR_ACK | HDMI0_ENABLE;
501 		switch (radeon_encoder->encoder_id) {
502 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
503 			WREG32_P(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN,
504 				 ~AVIVO_TMDSA_CNTL_HDMI_EN);
505 			hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
506 			break;
507 		case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
508 			WREG32_P(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN,
509 				 ~AVIVO_LVTMA_CNTL_HDMI_EN);
510 			hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
511 			break;
512 		case ENCODER_OBJECT_ID_INTERNAL_DDI:
513 			WREG32_P(DDIA_CNTL, DDIA_HDMI_EN, ~DDIA_HDMI_EN);
514 			hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
515 			break;
516 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
517 			hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
518 			break;
519 		default:
520 			dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
521 				radeon_encoder->encoder_id);
522 			break;
523 		}
524 		WREG32(HDMI0_CONTROL + offset, hdmi);
525 	}
526 
527 	if (rdev->irq.installed) {
528 		/* if irq is available use it */
529 		radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
530 	}
531 
532 	dig->afmt->enabled = true;
533 
534 	DRM_DEBUG("Enabling HDMI interface @ 0x%04X for encoder 0x%x\n",
535 		  offset, radeon_encoder->encoder_id);
536 }
537 
538 /*
539  * disable the HDMI engine
540  */
541 void r600_hdmi_disable(struct drm_encoder *encoder)
542 {
543 	struct drm_device *dev = encoder->dev;
544 	struct radeon_device *rdev = dev->dev_private;
545 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
546 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
547 	uint32_t offset;
548 
549 	if (ASIC_IS_DCE6(rdev))
550 		return;
551 
552 	/* Called for ATOM_ENCODER_MODE_HDMI only */
553 	if (!dig || !dig->afmt) {
554 		return;
555 	}
556 	if (!dig->afmt->enabled)
557 		return;
558 	offset = dig->afmt->offset;
559 
560 	DRM_DEBUG("Disabling HDMI interface @ 0x%04X for encoder 0x%x\n",
561 		  offset, radeon_encoder->encoder_id);
562 
563 	/* disable irq */
564 	radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
565 
566 	/* Older chipsets not handled by AtomBIOS */
567 	if (ASIC_IS_DCE2(rdev) && !ASIC_IS_DCE3(rdev)) {
568 		switch (radeon_encoder->encoder_id) {
569 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
570 			WREG32_P(AVIVO_TMDSA_CNTL, 0,
571 				 ~AVIVO_TMDSA_CNTL_HDMI_EN);
572 			break;
573 		case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
574 			WREG32_P(AVIVO_LVTMA_CNTL, 0,
575 				 ~AVIVO_LVTMA_CNTL_HDMI_EN);
576 			break;
577 		case ENCODER_OBJECT_ID_INTERNAL_DDI:
578 			WREG32_P(DDIA_CNTL, 0, ~DDIA_HDMI_EN);
579 			break;
580 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
581 			break;
582 		default:
583 			dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
584 				radeon_encoder->encoder_id);
585 			break;
586 		}
587 		WREG32(HDMI0_CONTROL + offset, HDMI0_ERROR_ACK);
588 	}
589 
590 	dig->afmt->enabled = false;
591 }
592