r600_hdmi.c revision 282199
1155192Srwatson/*
2155192Srwatson * Copyright 2008 Advanced Micro Devices, Inc.
3155192Srwatson * Copyright 2008 Red Hat Inc.
4155192Srwatson * Copyright 2009 Christian K��nig.
5155192Srwatson *
6155192Srwatson * Permission is hereby granted, free of charge, to any person obtaining a
7155192Srwatson * copy of this software and associated documentation files (the "Software"),
8155192Srwatson * to deal in the Software without restriction, including without limitation
9155192Srwatson * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10155192Srwatson * and/or sell copies of the Software, and to permit persons to whom the
11155192Srwatson * Software is furnished to do so, subject to the following conditions:
12155192Srwatson *
13155192Srwatson * The above copyright notice and this permission notice shall be included in
14155192Srwatson * all copies or substantial portions of the Software.
15155192Srwatson *
16155192Srwatson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17155192Srwatson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18155192Srwatson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19155192Srwatson * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20155192Srwatson * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21155192Srwatson * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22155192Srwatson * OTHER DEALINGS IN THE SOFTWARE.
23155192Srwatson *
24155192Srwatson * Authors: Christian K��nig
25155192Srwatson */
26155192Srwatson
27155192Srwatson#include <sys/cdefs.h>
28155192Srwatson__FBSDID("$FreeBSD: stable/10/sys/dev/drm2/radeon/r600_hdmi.c 282199 2015-04-28 19:35:05Z dumbbell $");
29155192Srwatson
30155192Srwatson#include <dev/drm2/drmP.h>
31155192Srwatson#include <dev/drm2/radeon/radeon_drm.h>
32155192Srwatson#include "radeon.h"
33155192Srwatson#include "radeon_asic.h"
34155192Srwatson#include "r600d.h"
35155192Srwatson#include "atom.h"
36155192Srwatson
37156882Srwatson/*
38156882Srwatson * HDMI color format
39155192Srwatson */
40155192Srwatsonenum r600_hdmi_color_format {
41155192Srwatson	RGB = 0,
42155192Srwatson	YCC_422 = 1,
43155192Srwatson	YCC_444 = 2
44155192Srwatson};
45155192Srwatson
46155192Srwatson/*
47155192Srwatson * IEC60958 status bits
48155192Srwatson */
49155192Srwatsonenum r600_hdmi_iec_status_bits {
50155192Srwatson	AUDIO_STATUS_DIG_ENABLE   = 0x01,
51155192Srwatson	AUDIO_STATUS_V            = 0x02,
52155192Srwatson	AUDIO_STATUS_VCFG         = 0x04,
53155192Srwatson	AUDIO_STATUS_EMPHASIS     = 0x08,
54155192Srwatson	AUDIO_STATUS_COPYRIGHT    = 0x10,
55155192Srwatson	AUDIO_STATUS_NONAUDIO     = 0x20,
56156888Srwatson	AUDIO_STATUS_PROFESSIONAL = 0x40,
57156888Srwatson	AUDIO_STATUS_LEVEL        = 0x80
58156888Srwatson};
59156888Srwatson
60156888Srwatsonstatic const struct radeon_hdmi_acr r600_hdmi_predefined_acr[] = {
61156888Srwatson    /*	     32kHz	  44.1kHz	48kHz    */
62156888Srwatson    /* Clock      N     CTS      N     CTS      N     CTS */
63156888Srwatson    {  25174,  4576,  28125,  7007,  31250,  6864,  28125 }, /*  25,20/1.001 MHz */
64156888Srwatson    {  25200,  4096,  25200,  6272,  28000,  6144,  25200 }, /*  25.20       MHz */
65156888Srwatson    {  27000,  4096,  27000,  6272,  30000,  6144,  27000 }, /*  27.00       MHz */
66156888Srwatson    {  27027,  4096,  27027,  6272,  30030,  6144,  27027 }, /*  27.00*1.001 MHz */
67156888Srwatson    {  54000,  4096,  54000,  6272,  60000,  6144,  54000 }, /*  54.00       MHz */
68156888Srwatson    {  54054,  4096,  54054,  6272,  60060,  6144,  54054 }, /*  54.00*1.001 MHz */
69156889Srwatson    {  74175, 11648, 210937, 17836, 234375, 11648, 140625 }, /*  74.25/1.001 MHz */
70156889Srwatson    {  74250,  4096,  74250,  6272,  82500,  6144,  74250 }, /*  74.25       MHz */
71155192Srwatson    { 148351, 11648, 421875,  8918, 234375,  5824, 140625 }, /* 148.50/1.001 MHz */
72155192Srwatson    { 148500,  4096, 148500,  6272, 165000,  6144, 148500 }, /* 148.50       MHz */
73155192Srwatson    {      0,  4096,      0,  6272,      0,  6144,      0 }  /* Other */
74155192Srwatson};
75155192Srwatson
76155192Srwatson/*
77161813Swsalamon * calculate CTS value if it's not found in the table
78161813Swsalamon */
79155192Srwatsonstatic void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int N, int freq)
80155192Srwatson{
81155192Srwatson	if (*CTS == 0)
82155192Srwatson		*CTS = clock * N / (128 * freq) * 1000;
83155192Srwatson	DRM_DEBUG("Using ACR timing N=%d CTS=%d for frequency %d\n",
84156889Srwatson		  N, *CTS, freq);
85156889Srwatson}
86156889Srwatson
87155192Srwatsonstruct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
88155192Srwatson{
89159269Srwatson	struct radeon_hdmi_acr res;
90159269Srwatson	u8 i;
91159269Srwatson
92155192Srwatson	for (i = 0; r600_hdmi_predefined_acr[i].clock != clock &&
93155192Srwatson	     r600_hdmi_predefined_acr[i].clock != 0; i++)
94155192Srwatson		;
95155192Srwatson	res = r600_hdmi_predefined_acr[i];
96159269Srwatson
97159269Srwatson	/* In case some CTS are missing */
98159269Srwatson	r600_hdmi_calc_cts(clock, &res.cts_32khz, res.n_32khz, 32000);
99162380Scsjp	r600_hdmi_calc_cts(clock, &res.cts_44_1khz, res.n_44_1khz, 44100);
100162380Scsjp	r600_hdmi_calc_cts(clock, &res.cts_48khz, res.n_48khz, 48000);
101162380Scsjp
102155192Srwatson	return res;
103155192Srwatson}
104155192Srwatson
105155192Srwatson/*
106155192Srwatson * update the N and CTS parameters for a given pixel clock rate
107155192Srwatson */
108155192Srwatsonstatic void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
109155192Srwatson{
110156889Srwatson	struct drm_device *dev = encoder->dev;
111156889Srwatson	struct radeon_device *rdev = dev->dev_private;
112156889Srwatson	struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
113156889Srwatson	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
114156889Srwatson	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
115156889Srwatson	uint32_t offset = dig->afmt->offset;
116156889Srwatson
117155192Srwatson	WREG32(HDMI0_ACR_32_0 + offset, HDMI0_ACR_CTS_32(acr.cts_32khz));
118155192Srwatson	WREG32(HDMI0_ACR_32_1 + offset, acr.n_32khz);
119155192Srwatson
120155192Srwatson	WREG32(HDMI0_ACR_44_0 + offset, HDMI0_ACR_CTS_44(acr.cts_44_1khz));
121155192Srwatson	WREG32(HDMI0_ACR_44_1 + offset, acr.n_44_1khz);
122155192Srwatson
123155192Srwatson	WREG32(HDMI0_ACR_48_0 + offset, HDMI0_ACR_CTS_48(acr.cts_48khz));
124155192Srwatson	WREG32(HDMI0_ACR_48_1 + offset, acr.n_48khz);
125155192Srwatson}
126155192Srwatson
127155192Srwatson/*
128156889Srwatson * calculate the crc for a given info frame
129156889Srwatson */
130156889Srwatsonstatic void r600_hdmi_infoframe_checksum(uint8_t packetType,
131156889Srwatson					 uint8_t versionNumber,
132155192Srwatson					 uint8_t length,
133155192Srwatson					 uint8_t *frame)
134155192Srwatson{
135155192Srwatson	int i;
136155192Srwatson	frame[0] = packetType + versionNumber + length;
137155192Srwatson	for (i = 1; i <= length; i++)
138155192Srwatson		frame[0] += frame[i];
139155192Srwatson	frame[0] = 0x100 - frame[0];
140155192Srwatson}
141155192Srwatson
142155192Srwatson/*
143155192Srwatson * build a HDMI Video Info Frame
144155192Srwatson */
145155192Srwatsonstatic void r600_hdmi_videoinfoframe(
146155192Srwatson	struct drm_encoder *encoder,
147155192Srwatson	enum r600_hdmi_color_format color_format,
148155192Srwatson	int active_information_present,
149155192Srwatson	uint8_t active_format_aspect_ratio,
150155192Srwatson	uint8_t scan_information,
151156889Srwatson	uint8_t colorimetry,
152156889Srwatson	uint8_t ex_colorimetry,
153156889Srwatson	uint8_t quantization,
154155192Srwatson	int ITC,
155155192Srwatson	uint8_t picture_aspect_ratio,
156155192Srwatson	uint8_t video_format_identification,
157155192Srwatson	uint8_t pixel_repetition,
158155192Srwatson	uint8_t non_uniform_picture_scaling,
159155192Srwatson	uint8_t bar_info_data_valid,
160155192Srwatson	uint16_t top_bar,
161155192Srwatson	uint16_t bottom_bar,
162155192Srwatson	uint16_t left_bar,
163155192Srwatson	uint16_t right_bar
164155192Srwatson)
165155192Srwatson{
166155192Srwatson	struct drm_device *dev = encoder->dev;
167156889Srwatson	struct radeon_device *rdev = dev->dev_private;
168156889Srwatson	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
169156889Srwatson	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
170156889Srwatson	uint32_t offset = dig->afmt->offset;
171156889Srwatson
172156889Srwatson	uint8_t frame[14];
173156889Srwatson
174156889Srwatson	frame[0x0] = 0;
175168688Scsjp	frame[0x1] =
176156889Srwatson		(scan_information & 0x3) |
177156889Srwatson		((bar_info_data_valid & 0x3) << 2) |
178155192Srwatson		((active_information_present & 0x1) << 4) |
179155192Srwatson		((color_format & 0x3) << 5);
180156889Srwatson	frame[0x2] =
181156889Srwatson		(active_format_aspect_ratio & 0xF) |
182156889Srwatson		((picture_aspect_ratio & 0x3) << 4) |
183156889Srwatson		((colorimetry & 0x3) << 6);
184156889Srwatson	frame[0x3] =
185156889Srwatson		(non_uniform_picture_scaling & 0x3) |
186156889Srwatson		((quantization & 0x3) << 2) |
187156889Srwatson		((ex_colorimetry & 0x7) << 4) |
188156889Srwatson		((ITC & 0x1) << 7);
189168688Scsjp	frame[0x4] = (video_format_identification & 0x7F);
190156889Srwatson	frame[0x5] = (pixel_repetition & 0xF);
191156889Srwatson	frame[0x6] = (top_bar & 0xFF);
192156889Srwatson	frame[0x7] = (top_bar >> 8);
193156889Srwatson	frame[0x8] = (bottom_bar & 0xFF);
194156889Srwatson	frame[0x9] = (bottom_bar >> 8);
195156889Srwatson	frame[0xA] = (left_bar & 0xFF);
196156889Srwatson	frame[0xB] = (left_bar >> 8);
197156889Srwatson	frame[0xC] = (right_bar & 0xFF);
198156889Srwatson	frame[0xD] = (right_bar >> 8);
199156889Srwatson
200156889Srwatson	r600_hdmi_infoframe_checksum(0x82, 0x02, 0x0D, frame);
201156889Srwatson	/* Our header values (type, version, length) should be alright, Intel
202156889Srwatson	 * is using the same. Checksum function also seems to be OK, it works
203156889Srwatson	 * fine for audio infoframe. However calculated value is always lower
204156889Srwatson	 * by 2 in comparison to fglrx. It breaks displaying anything in case
205156889Srwatson	 * of TVs that strictly check the checksum. Hack it manually here to
206156889Srwatson	 * workaround this issue. */
207156889Srwatson	frame[0x0] += 2;
208156889Srwatson
209156889Srwatson	WREG32(HDMI0_AVI_INFO0 + offset,
210156889Srwatson		frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
211156889Srwatson	WREG32(HDMI0_AVI_INFO1 + offset,
212156889Srwatson		frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
213156889Srwatson	WREG32(HDMI0_AVI_INFO2 + offset,
214156889Srwatson		frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
215156889Srwatson	WREG32(HDMI0_AVI_INFO3 + offset,
216156889Srwatson		frame[0xC] | (frame[0xD] << 8));
217156889Srwatson}
218156889Srwatson
219161813Swsalamon/*
220161813Swsalamon * build a Audio Info Frame
221161813Swsalamon */
222161813Swsalamonstatic void r600_hdmi_audioinfoframe(
223156889Srwatson	struct drm_encoder *encoder,
224156889Srwatson	uint8_t channel_count,
225164011Scsjp	uint8_t coding_type,
226155192Srwatson	uint8_t sample_size,
227155192Srwatson	uint8_t sample_frequency,
228155192Srwatson	uint8_t format,
229155192Srwatson	uint8_t channel_allocation,
230155192Srwatson	uint8_t level_shift,
231155192Srwatson	int downmix_inhibit
232155192Srwatson)
233155192Srwatson{
234155192Srwatson	struct drm_device *dev = encoder->dev;
235155192Srwatson	struct radeon_device *rdev = dev->dev_private;
236155192Srwatson	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
237155192Srwatson	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
238155192Srwatson	uint32_t offset = dig->afmt->offset;
239155192Srwatson
240156889Srwatson	uint8_t frame[11];
241156889Srwatson
242155192Srwatson	frame[0x0] = 0;
243155192Srwatson	frame[0x1] = (channel_count & 0x7) | ((coding_type & 0xF) << 4);
244156889Srwatson	frame[0x2] = (sample_size & 0x3) | ((sample_frequency & 0x7) << 2);
245156889Srwatson	frame[0x3] = format;
246156889Srwatson	frame[0x4] = channel_allocation;
247156889Srwatson	frame[0x5] = ((level_shift & 0xF) << 3) | ((downmix_inhibit & 0x1) << 7);
248156889Srwatson	frame[0x6] = 0;
249156889Srwatson	frame[0x7] = 0;
250155192Srwatson	frame[0x8] = 0;
251156888Srwatson	frame[0x9] = 0;
252155192Srwatson	frame[0xA] = 0;
253155192Srwatson
254155192Srwatson	r600_hdmi_infoframe_checksum(0x84, 0x01, 0x0A, frame);
255155192Srwatson
256155192Srwatson	WREG32(HDMI0_AUDIO_INFO0 + offset,
257155192Srwatson		frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
258156888Srwatson	WREG32(HDMI0_AUDIO_INFO1 + offset,
259155192Srwatson		frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
260155192Srwatson}
261155192Srwatson
262155192Srwatson/*
263155192Srwatson * test if audio buffer is filled enough to start playing
264155192Srwatson */
265155192Srwatsonstatic bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
266156291Srwatson{
267156889Srwatson	struct drm_device *dev = encoder->dev;
268156889Srwatson	struct radeon_device *rdev = dev->dev_private;
269155192Srwatson	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
270155192Srwatson	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
271155192Srwatson	uint32_t offset = dig->afmt->offset;
272155192Srwatson
273156889Srwatson	return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
274156889Srwatson}
275155192Srwatson
276155192Srwatson/*
277155192Srwatson * have buffer status changed since last call?
278155192Srwatson */
279156889Srwatsonint r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
280156889Srwatson{
281156889Srwatson	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
282155192Srwatson	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
283156888Srwatson	int status, result;
284156888Srwatson
285156888Srwatson	if (!dig->afmt || !dig->afmt->enabled)
286156888Srwatson		return 0;
287156889Srwatson
288159261Srwatson	status = r600_hdmi_is_audio_buffer_filled(encoder);
289159261Srwatson	result = dig->afmt->last_buffer_filled_status != status;
290156889Srwatson	dig->afmt->last_buffer_filled_status = status;
291156889Srwatson
292156889Srwatson	return result;
293156889Srwatson}
294156888Srwatson
295156888Srwatson/*
296155192Srwatson * write the audio workaround status to the hardware
297155192Srwatson */
298155192Srwatsonstatic void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
299155192Srwatson{
300155192Srwatson	struct drm_device *dev = encoder->dev;
301155192Srwatson	struct radeon_device *rdev = dev->dev_private;
302155192Srwatson	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
303155192Srwatson	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
304155192Srwatson	uint32_t offset = dig->afmt->offset;
305155192Srwatson	bool hdmi_audio_workaround = false; /* FIXME */
306155192Srwatson	u32 value;
307156889Srwatson
308156889Srwatson	if (!hdmi_audio_workaround ||
309155192Srwatson	    r600_hdmi_is_audio_buffer_filled(encoder))
310155192Srwatson		value = 0; /* disable workaround */
311155192Srwatson	else
312155192Srwatson		value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
313155192Srwatson	WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
314155192Srwatson		 value, ~HDMI0_AUDIO_TEST_EN);
315159269Srwatson}
316159269Srwatson
317155192Srwatson
318155192Srwatson/*
319155192Srwatson * update the info frames with the data from the current display mode
320155192Srwatson */
321155192Srwatsonvoid r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
322155192Srwatson{
323155192Srwatson	struct drm_device *dev = encoder->dev;
324155192Srwatson	struct radeon_device *rdev = dev->dev_private;
325155192Srwatson	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
326155192Srwatson	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
327155192Srwatson	uint32_t offset;
328155192Srwatson
329155192Srwatson	/* Silent, r600_hdmi_enable will raise WARN for us */
330155192Srwatson	if (!dig->afmt->enabled)
331155192Srwatson		return;
332156845Srwatson	offset = dig->afmt->offset;
333155192Srwatson
334155192Srwatson	r600_audio_set_clock(encoder, mode->clock);
335155192Srwatson
336155192Srwatson	WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
337155192Srwatson	       HDMI0_NULL_SEND); /* send null packets when required */
338156888Srwatson
339155192Srwatson	WREG32(HDMI0_AUDIO_CRC_CONTROL + offset, 0x1000);
340155192Srwatson
341155192Srwatson	if (ASIC_IS_DCE32(rdev)) {
342156888Srwatson		WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
343155192Srwatson		       HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
344155408Srwatson		       HDMI0_AUDIO_PACKETS_PER_LINE(3)); /* should be suffient for all audio modes and small enough for all hblanks */
345155408Srwatson		WREG32(AFMT_AUDIO_PACKET_CONTROL + offset,
346155408Srwatson		       AFMT_AUDIO_SAMPLE_SEND | /* send audio packets */
347159269Srwatson		       AFMT_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
348159269Srwatson	} else {
349159269Srwatson		WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
350159269Srwatson		       HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
351159269Srwatson		       HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
352155408Srwatson		       HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
353156882Srwatson		       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 */
395void 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 */
478void 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 (ASIC_IS_DCE6(rdev))
488		return;
489
490	/* Silent, r600_hdmi_enable will raise WARN for us */
491	if (dig->afmt->enabled)
492		return;
493	offset = dig->afmt->offset;
494
495	/* Older chipsets require setting HDMI and routing manually */
496	if (ASIC_IS_DCE2(rdev) && !ASIC_IS_DCE3(rdev)) {
497		hdmi = HDMI0_ERROR_ACK | HDMI0_ENABLE;
498		switch (radeon_encoder->encoder_id) {
499		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
500			WREG32_P(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN,
501				 ~AVIVO_TMDSA_CNTL_HDMI_EN);
502			hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
503			break;
504		case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
505			WREG32_P(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN,
506				 ~AVIVO_LVTMA_CNTL_HDMI_EN);
507			hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
508			break;
509		case ENCODER_OBJECT_ID_INTERNAL_DDI:
510			WREG32_P(DDIA_CNTL, DDIA_HDMI_EN, ~DDIA_HDMI_EN);
511			hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
512			break;
513		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
514			hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
515			break;
516		default:
517			dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
518				radeon_encoder->encoder_id);
519			break;
520		}
521		WREG32(HDMI0_CONTROL + offset, hdmi);
522	}
523
524	if (rdev->irq.installed) {
525		/* if irq is available use it */
526		radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
527	}
528
529	dig->afmt->enabled = true;
530
531	DRM_DEBUG("Enabling HDMI interface @ 0x%04X for encoder 0x%x\n",
532		  offset, radeon_encoder->encoder_id);
533}
534
535/*
536 * disable the HDMI engine
537 */
538void r600_hdmi_disable(struct drm_encoder *encoder)
539{
540	struct drm_device *dev = encoder->dev;
541	struct radeon_device *rdev = dev->dev_private;
542	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
543	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
544	uint32_t offset;
545
546	if (ASIC_IS_DCE6(rdev))
547		return;
548
549	/* Called for ATOM_ENCODER_MODE_HDMI only */
550	if (!dig || !dig->afmt) {
551		return;
552	}
553	if (!dig->afmt->enabled)
554		return;
555	offset = dig->afmt->offset;
556
557	DRM_DEBUG("Disabling HDMI interface @ 0x%04X for encoder 0x%x\n",
558		  offset, radeon_encoder->encoder_id);
559
560	/* disable irq */
561	radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
562
563	/* Older chipsets not handled by AtomBIOS */
564	if (ASIC_IS_DCE2(rdev) && !ASIC_IS_DCE3(rdev)) {
565		switch (radeon_encoder->encoder_id) {
566		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
567			WREG32_P(AVIVO_TMDSA_CNTL, 0,
568				 ~AVIVO_TMDSA_CNTL_HDMI_EN);
569			break;
570		case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
571			WREG32_P(AVIVO_LVTMA_CNTL, 0,
572				 ~AVIVO_LVTMA_CNTL_HDMI_EN);
573			break;
574		case ENCODER_OBJECT_ID_INTERNAL_DDI:
575			WREG32_P(DDIA_CNTL, 0, ~DDIA_HDMI_EN);
576			break;
577		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
578			break;
579		default:
580			dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
581				radeon_encoder->encoder_id);
582			break;
583		}
584		WREG32(HDMI0_CONTROL + offset, HDMI0_ERROR_ACK);
585	}
586
587	dig->afmt->enabled = false;
588}
589