xref: /dflybsd-src/sys/dev/drm/radeon/evergreen_hdmi.c (revision d78d3a2272f5ecf9e0b570e362128240417a1b85)
1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot  * Copyright 2008 Advanced Micro Devices, Inc.
3926deccbSFrançois Tigeot  * Copyright 2008 Red Hat Inc.
4926deccbSFrançois Tigeot  * Copyright 2009 Christian König.
5926deccbSFrançois Tigeot  *
6926deccbSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
7926deccbSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
8926deccbSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
9926deccbSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10926deccbSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
11926deccbSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
12926deccbSFrançois Tigeot  *
13926deccbSFrançois Tigeot  * The above copyright notice and this permission notice shall be included in
14926deccbSFrançois Tigeot  * all copies or substantial portions of the Software.
15926deccbSFrançois Tigeot  *
16926deccbSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17926deccbSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18926deccbSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19926deccbSFrançois Tigeot  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20926deccbSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21926deccbSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22926deccbSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
23926deccbSFrançois Tigeot  *
24926deccbSFrançois Tigeot  * Authors: Christian König
25926deccbSFrançois Tigeot  *          Rafał Miłecki
26926deccbSFrançois Tigeot  */
27b403bed8SMichael Neumann #include <linux/hdmi.h>
28926deccbSFrançois Tigeot #include <drm/drmP.h>
2983b4b9b9SFrançois Tigeot #include <drm/radeon_drm.h>
30926deccbSFrançois Tigeot #include "radeon.h"
31926deccbSFrançois Tigeot #include "radeon_asic.h"
32*c59a5c48SFrançois Tigeot #include "radeon_audio.h"
33926deccbSFrançois Tigeot #include "evergreend.h"
34926deccbSFrançois Tigeot #include "atom.h"
35926deccbSFrançois Tigeot 
36591d5043SFrançois Tigeot /* enable the audio stream */
37*c59a5c48SFrançois Tigeot void dce4_audio_enable(struct radeon_device *rdev,
38*c59a5c48SFrançois Tigeot 			      struct r600_audio_pin *pin,
39*c59a5c48SFrançois Tigeot 			      u8 enable_mask);
dce4_audio_enable(struct radeon_device * rdev,struct r600_audio_pin * pin,u8 enable_mask)40*c59a5c48SFrançois Tigeot void dce4_audio_enable(struct radeon_device *rdev,
41591d5043SFrançois Tigeot 			      struct r600_audio_pin *pin,
42591d5043SFrançois Tigeot 			      u8 enable_mask)
43591d5043SFrançois Tigeot {
44591d5043SFrançois Tigeot 	u32 tmp = RREG32(AZ_HOT_PLUG_CONTROL);
45591d5043SFrançois Tigeot 
46591d5043SFrançois Tigeot 	if (!pin)
47591d5043SFrançois Tigeot 		return;
48591d5043SFrançois Tigeot 
49591d5043SFrançois Tigeot 	if (enable_mask) {
50591d5043SFrançois Tigeot 		tmp |= AUDIO_ENABLED;
51591d5043SFrançois Tigeot 		if (enable_mask & 1)
52591d5043SFrançois Tigeot 			tmp |= PIN0_AUDIO_ENABLED;
53591d5043SFrançois Tigeot 		if (enable_mask & 2)
54591d5043SFrançois Tigeot 			tmp |= PIN1_AUDIO_ENABLED;
55591d5043SFrançois Tigeot 		if (enable_mask & 4)
56591d5043SFrançois Tigeot 			tmp |= PIN2_AUDIO_ENABLED;
57591d5043SFrançois Tigeot 		if (enable_mask & 8)
58591d5043SFrançois Tigeot 			tmp |= PIN3_AUDIO_ENABLED;
59591d5043SFrançois Tigeot 	} else {
60591d5043SFrançois Tigeot 		tmp &= ~(AUDIO_ENABLED |
61591d5043SFrançois Tigeot 			 PIN0_AUDIO_ENABLED |
62591d5043SFrançois Tigeot 			 PIN1_AUDIO_ENABLED |
63591d5043SFrançois Tigeot 			 PIN2_AUDIO_ENABLED |
64591d5043SFrançois Tigeot 			 PIN3_AUDIO_ENABLED);
65591d5043SFrançois Tigeot 	}
66591d5043SFrançois Tigeot 
67591d5043SFrançois Tigeot 	WREG32(AZ_HOT_PLUG_CONTROL, tmp);
68591d5043SFrançois Tigeot }
69591d5043SFrançois Tigeot 
70*c59a5c48SFrançois Tigeot void evergreen_hdmi_update_acr(struct drm_encoder *encoder, long offset,
71*c59a5c48SFrançois Tigeot 	const struct radeon_hdmi_acr *acr);
evergreen_hdmi_update_acr(struct drm_encoder * encoder,long offset,const struct radeon_hdmi_acr * acr)72*c59a5c48SFrançois Tigeot void evergreen_hdmi_update_acr(struct drm_encoder *encoder, long offset,
73*c59a5c48SFrançois Tigeot 	const struct radeon_hdmi_acr *acr)
74926deccbSFrançois Tigeot {
75926deccbSFrançois Tigeot 	struct drm_device *dev = encoder->dev;
76926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
77*c59a5c48SFrançois Tigeot 	int bpc = 8;
78926deccbSFrançois Tigeot 
79*c59a5c48SFrançois Tigeot 	if (encoder->crtc) {
80*c59a5c48SFrançois Tigeot 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
81*c59a5c48SFrançois Tigeot 		bpc = radeon_crtc->bpc;
82926deccbSFrançois Tigeot 	}
83926deccbSFrançois Tigeot 
84*c59a5c48SFrançois Tigeot 	if (bpc > 8)
85*c59a5c48SFrançois Tigeot 		WREG32(HDMI_ACR_PACKET_CONTROL + offset,
86*c59a5c48SFrançois Tigeot 			HDMI_ACR_AUTO_SEND);	/* allow hw to sent ACR packets when required */
87*c59a5c48SFrançois Tigeot 	else
88*c59a5c48SFrançois Tigeot 		WREG32(HDMI_ACR_PACKET_CONTROL + offset,
89*c59a5c48SFrançois Tigeot 			HDMI_ACR_SOURCE |		/* select SW CTS value */
90*c59a5c48SFrançois Tigeot 			HDMI_ACR_AUTO_SEND);	/* allow hw to sent ACR packets when required */
91*c59a5c48SFrançois Tigeot 
92*c59a5c48SFrançois Tigeot 	WREG32(HDMI_ACR_32_0 + offset, HDMI_ACR_CTS_32(acr->cts_32khz));
93*c59a5c48SFrançois Tigeot 	WREG32(HDMI_ACR_32_1 + offset, acr->n_32khz);
94*c59a5c48SFrançois Tigeot 
95*c59a5c48SFrançois Tigeot 	WREG32(HDMI_ACR_44_0 + offset, HDMI_ACR_CTS_44(acr->cts_44_1khz));
96*c59a5c48SFrançois Tigeot 	WREG32(HDMI_ACR_44_1 + offset, acr->n_44_1khz);
97*c59a5c48SFrançois Tigeot 
98*c59a5c48SFrançois Tigeot 	WREG32(HDMI_ACR_48_0 + offset, HDMI_ACR_CTS_48(acr->cts_48khz));
99*c59a5c48SFrançois Tigeot 	WREG32(HDMI_ACR_48_1 + offset, acr->n_48khz);
100*c59a5c48SFrançois Tigeot }
101*c59a5c48SFrançois Tigeot 
102*c59a5c48SFrançois Tigeot void dce4_afmt_write_latency_fields(struct drm_encoder *encoder,
103*c59a5c48SFrançois Tigeot 		struct drm_connector *connector, struct drm_display_mode *mode);
dce4_afmt_write_latency_fields(struct drm_encoder * encoder,struct drm_connector * connector,struct drm_display_mode * mode)104*c59a5c48SFrançois Tigeot void dce4_afmt_write_latency_fields(struct drm_encoder *encoder,
105*c59a5c48SFrançois Tigeot 		struct drm_connector *connector, struct drm_display_mode *mode)
106c6f73aabSFrançois Tigeot {
107c6f73aabSFrançois Tigeot 	struct radeon_device *rdev = encoder->dev->dev_private;
108c6f73aabSFrançois Tigeot 	u32 tmp = 0;
109c6f73aabSFrançois Tigeot 
110c6f73aabSFrançois Tigeot 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
111c6f73aabSFrançois Tigeot 		if (connector->latency_present[1])
112c6f73aabSFrançois Tigeot 			tmp = VIDEO_LIPSYNC(connector->video_latency[1]) |
113c6f73aabSFrançois Tigeot 				AUDIO_LIPSYNC(connector->audio_latency[1]);
114c6f73aabSFrançois Tigeot 		else
115c6f73aabSFrançois Tigeot 			tmp = VIDEO_LIPSYNC(255) | AUDIO_LIPSYNC(255);
116c6f73aabSFrançois Tigeot 	} else {
117c6f73aabSFrançois Tigeot 		if (connector->latency_present[0])
118c6f73aabSFrançois Tigeot 			tmp = VIDEO_LIPSYNC(connector->video_latency[0]) |
119c6f73aabSFrançois Tigeot 				AUDIO_LIPSYNC(connector->audio_latency[0]);
120c6f73aabSFrançois Tigeot 		else
121c6f73aabSFrançois Tigeot 			tmp = VIDEO_LIPSYNC(255) | AUDIO_LIPSYNC(255);
122c6f73aabSFrançois Tigeot 	}
123*c59a5c48SFrançois Tigeot 	WREG32_ENDPOINT(0, AZ_F0_CODEC_PIN0_CONTROL_RESPONSE_LIPSYNC, tmp);
124c6f73aabSFrançois Tigeot }
125c6f73aabSFrançois Tigeot 
126*c59a5c48SFrançois Tigeot void dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
127*c59a5c48SFrançois Tigeot 	u8 *sadb, int sad_count);
dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder * encoder,u8 * sadb,int sad_count)128*c59a5c48SFrançois Tigeot void dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
129*c59a5c48SFrançois Tigeot 	u8 *sadb, int sad_count)
1304cd92098Szrj {
1314cd92098Szrj 	struct radeon_device *rdev = encoder->dev->dev_private;
1324cd92098Szrj 	u32 tmp;
1334cd92098Szrj 
1344cd92098Szrj 	/* program the speaker allocation */
135*c59a5c48SFrançois Tigeot 	tmp = RREG32_ENDPOINT(0, AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER);
1364cd92098Szrj 	tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
1374cd92098Szrj 	/* set HDMI mode */
1384cd92098Szrj 	tmp |= HDMI_CONNECTION;
1394cd92098Szrj 	if (sad_count)
1404cd92098Szrj 		tmp |= SPEAKER_ALLOCATION(sadb[0]);
1414cd92098Szrj 	else
1424cd92098Szrj 		tmp |= SPEAKER_ALLOCATION(5); /* stereo */
143*c59a5c48SFrançois Tigeot 	WREG32_ENDPOINT(0, AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
1444cd92098Szrj }
1454cd92098Szrj 
146*c59a5c48SFrançois Tigeot void dce4_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
147*c59a5c48SFrançois Tigeot 	u8 *sadb, int sad_count);
dce4_afmt_dp_write_speaker_allocation(struct drm_encoder * encoder,u8 * sadb,int sad_count)148*c59a5c48SFrançois Tigeot void dce4_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
149*c59a5c48SFrançois Tigeot 	u8 *sadb, int sad_count)
150f43cf1b1SMichael Neumann {
151f43cf1b1SMichael Neumann 	struct radeon_device *rdev = encoder->dev->dev_private;
152*c59a5c48SFrançois Tigeot 	u32 tmp;
153f43cf1b1SMichael Neumann 
154*c59a5c48SFrançois Tigeot 	/* program the speaker allocation */
155*c59a5c48SFrançois Tigeot 	tmp = RREG32_ENDPOINT(0, AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER);
156*c59a5c48SFrançois Tigeot 	tmp &= ~(HDMI_CONNECTION | SPEAKER_ALLOCATION_MASK);
157*c59a5c48SFrançois Tigeot 	/* set DP mode */
158*c59a5c48SFrançois Tigeot 	tmp |= DP_CONNECTION;
159*c59a5c48SFrançois Tigeot 	if (sad_count)
160*c59a5c48SFrançois Tigeot 		tmp |= SPEAKER_ALLOCATION(sadb[0]);
161*c59a5c48SFrançois Tigeot 	else
162*c59a5c48SFrançois Tigeot 		tmp |= SPEAKER_ALLOCATION(5); /* stereo */
163*c59a5c48SFrançois Tigeot 	WREG32_ENDPOINT(0, AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
164*c59a5c48SFrançois Tigeot }
165*c59a5c48SFrançois Tigeot 
166*c59a5c48SFrançois Tigeot void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder,
167*c59a5c48SFrançois Tigeot 	struct cea_sad *sads, int sad_count);
evergreen_hdmi_write_sad_regs(struct drm_encoder * encoder,struct cea_sad * sads,int sad_count)168*c59a5c48SFrançois Tigeot void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder,
169*c59a5c48SFrançois Tigeot 	struct cea_sad *sads, int sad_count)
170*c59a5c48SFrançois Tigeot {
171*c59a5c48SFrançois Tigeot 	int i;
172*c59a5c48SFrançois Tigeot 	struct radeon_device *rdev = encoder->dev->dev_private;
173f43cf1b1SMichael Neumann 	static const u16 eld_reg_to_type[][2] = {
174f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
175f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
176f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
177f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
178f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
179f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
180f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
181f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
182f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
183f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
184f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
185f43cf1b1SMichael Neumann 		{ AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
186f43cf1b1SMichael Neumann 	};
187f43cf1b1SMichael Neumann 
188f43cf1b1SMichael Neumann 	for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
189f43cf1b1SMichael Neumann 		u32 value = 0;
190c6f73aabSFrançois Tigeot 		u8 stereo_freqs = 0;
191c6f73aabSFrançois Tigeot 		int max_channels = -1;
192f43cf1b1SMichael Neumann 		int j;
193f43cf1b1SMichael Neumann 
194f43cf1b1SMichael Neumann 		for (j = 0; j < sad_count; j++) {
195f43cf1b1SMichael Neumann 			struct cea_sad *sad = &sads[j];
196f43cf1b1SMichael Neumann 
197f43cf1b1SMichael Neumann 			if (sad->format == eld_reg_to_type[i][1]) {
198c6f73aabSFrançois Tigeot 				if (sad->channels > max_channels) {
199f43cf1b1SMichael Neumann 					value = MAX_CHANNELS(sad->channels) |
200f43cf1b1SMichael Neumann 						DESCRIPTOR_BYTE_2(sad->byte2) |
201f43cf1b1SMichael Neumann 						SUPPORTED_FREQUENCIES(sad->freq);
202c6f73aabSFrançois Tigeot 					max_channels = sad->channels;
203c6f73aabSFrançois Tigeot 				}
204c6f73aabSFrançois Tigeot 
205f43cf1b1SMichael Neumann 				if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
206c6f73aabSFrançois Tigeot 					stereo_freqs |= sad->freq;
207c6f73aabSFrançois Tigeot 				else
208f43cf1b1SMichael Neumann 					break;
209f43cf1b1SMichael Neumann 			}
210f43cf1b1SMichael Neumann 		}
211c6f73aabSFrançois Tigeot 
212c6f73aabSFrançois Tigeot 		value |= SUPPORTED_FREQUENCIES_STEREO(stereo_freqs);
213c6f73aabSFrançois Tigeot 
214*c59a5c48SFrançois Tigeot 		WREG32_ENDPOINT(0, eld_reg_to_type[i][0], value);
215f43cf1b1SMichael Neumann 	}
216f43cf1b1SMichael Neumann }
217f43cf1b1SMichael Neumann 
218926deccbSFrançois Tigeot /*
219*c59a5c48SFrançois Tigeot  * build a AVI Info Frame
220926deccbSFrançois Tigeot  */
221*c59a5c48SFrançois Tigeot void evergreen_set_avi_packet(struct radeon_device *rdev, u32 offset,
222*c59a5c48SFrançois Tigeot     unsigned char *buffer, size_t size);
evergreen_set_avi_packet(struct radeon_device * rdev,u32 offset,unsigned char * buffer,size_t size)223*c59a5c48SFrançois Tigeot void evergreen_set_avi_packet(struct radeon_device *rdev, u32 offset,
224*c59a5c48SFrançois Tigeot 			      unsigned char *buffer, size_t size)
225926deccbSFrançois Tigeot {
226*c59a5c48SFrançois Tigeot 	uint8_t *frame = buffer + 3;
227926deccbSFrançois Tigeot 
228926deccbSFrançois Tigeot 	WREG32(AFMT_AVI_INFO0 + offset,
229926deccbSFrançois Tigeot 		frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
230926deccbSFrançois Tigeot 	WREG32(AFMT_AVI_INFO1 + offset,
231926deccbSFrançois Tigeot 		frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
232926deccbSFrançois Tigeot 	WREG32(AFMT_AVI_INFO2 + offset,
233926deccbSFrançois Tigeot 		frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
234926deccbSFrançois Tigeot 	WREG32(AFMT_AVI_INFO3 + offset,
235*c59a5c48SFrançois Tigeot 		frame[0xC] | (frame[0xD] << 8) | (buffer[1] << 24));
236*c59a5c48SFrançois Tigeot 
237*c59a5c48SFrançois Tigeot 	WREG32_P(HDMI_INFOFRAME_CONTROL1 + offset,
238*c59a5c48SFrançois Tigeot 		 HDMI_AVI_INFO_LINE(2),	/* anything other than 0 */
239*c59a5c48SFrançois Tigeot 		 ~HDMI_AVI_INFO_LINE_MASK);
240926deccbSFrançois Tigeot }
241926deccbSFrançois Tigeot 
242*c59a5c48SFrançois Tigeot void dce4_hdmi_audio_set_dto(struct radeon_device *rdev,
243*c59a5c48SFrançois Tigeot 	struct radeon_crtc *crtc, unsigned int clock);
dce4_hdmi_audio_set_dto(struct radeon_device * rdev,struct radeon_crtc * crtc,unsigned int clock)244*c59a5c48SFrançois Tigeot void dce4_hdmi_audio_set_dto(struct radeon_device *rdev,
245*c59a5c48SFrançois Tigeot 	struct radeon_crtc *crtc, unsigned int clock)
246f43cf1b1SMichael Neumann {
247*c59a5c48SFrançois Tigeot 	unsigned int max_ratio = clock / 24000;
24857e252bfSMichael Neumann 	u32 dto_phase;
24957e252bfSMichael Neumann 	u32 wallclock_ratio;
250*c59a5c48SFrançois Tigeot 	u32 value;
251f43cf1b1SMichael Neumann 
25257e252bfSMichael Neumann 	if (max_ratio >= 8) {
25357e252bfSMichael Neumann 		dto_phase = 192 * 1000;
25457e252bfSMichael Neumann 		wallclock_ratio = 3;
25557e252bfSMichael Neumann 	} else if (max_ratio >= 4) {
25657e252bfSMichael Neumann 		dto_phase = 96 * 1000;
25757e252bfSMichael Neumann 		wallclock_ratio = 2;
25857e252bfSMichael Neumann 	} else if (max_ratio >= 2) {
25957e252bfSMichael Neumann 		dto_phase = 48 * 1000;
26057e252bfSMichael Neumann 		wallclock_ratio = 1;
26157e252bfSMichael Neumann 	} else {
26257e252bfSMichael Neumann 		dto_phase = 24 * 1000;
26357e252bfSMichael Neumann 		wallclock_ratio = 0;
26457e252bfSMichael Neumann 	}
26557e252bfSMichael Neumann 
266*c59a5c48SFrançois Tigeot 	value = RREG32(DCCG_AUDIO_DTO0_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
267*c59a5c48SFrançois Tigeot 	value |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
268*c59a5c48SFrançois Tigeot 	value &= ~DCCG_AUDIO_DTO1_USE_512FBR_DTO;
269*c59a5c48SFrançois Tigeot 	WREG32(DCCG_AUDIO_DTO0_CNTL, value);
270*c59a5c48SFrançois Tigeot 
271*c59a5c48SFrançois Tigeot 	/* Two dtos; generally use dto0 for HDMI */
272*c59a5c48SFrançois Tigeot 	value = 0;
273*c59a5c48SFrançois Tigeot 
274*c59a5c48SFrançois Tigeot 	if (crtc)
275*c59a5c48SFrançois Tigeot 		value |= DCCG_AUDIO_DTO0_SOURCE_SEL(crtc->crtc_id);
276*c59a5c48SFrançois Tigeot 
277*c59a5c48SFrançois Tigeot 	WREG32(DCCG_AUDIO_DTO_SOURCE, value);
278*c59a5c48SFrançois Tigeot 
279f43cf1b1SMichael Neumann 	/* Express [24MHz / target pixel clock] as an exact rational
280f43cf1b1SMichael Neumann 	 * number (coefficient of two integer numbers.  DCCG_AUDIO_DTOx_PHASE
281f43cf1b1SMichael Neumann 	 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
282f43cf1b1SMichael Neumann 	 */
28357e252bfSMichael Neumann 	WREG32(DCCG_AUDIO_DTO0_PHASE, dto_phase);
284*c59a5c48SFrançois Tigeot 	WREG32(DCCG_AUDIO_DTO0_MODULE, clock);
285f43cf1b1SMichael Neumann }
286f43cf1b1SMichael Neumann 
287*c59a5c48SFrançois Tigeot void dce4_dp_audio_set_dto(struct radeon_device *rdev,
288*c59a5c48SFrançois Tigeot 	struct radeon_crtc *crtc, unsigned int clock);
dce4_dp_audio_set_dto(struct radeon_device * rdev,struct radeon_crtc * crtc,unsigned int clock)289*c59a5c48SFrançois Tigeot void dce4_dp_audio_set_dto(struct radeon_device *rdev,
290*c59a5c48SFrançois Tigeot 			   struct radeon_crtc *crtc, unsigned int clock)
291*c59a5c48SFrançois Tigeot {
292*c59a5c48SFrançois Tigeot 	u32 value;
293f43cf1b1SMichael Neumann 
294*c59a5c48SFrançois Tigeot 	value = RREG32(DCCG_AUDIO_DTO1_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
295*c59a5c48SFrançois Tigeot 	value |= DCCG_AUDIO_DTO1_USE_512FBR_DTO;
296*c59a5c48SFrançois Tigeot 	WREG32(DCCG_AUDIO_DTO1_CNTL, value);
297*c59a5c48SFrançois Tigeot 
298*c59a5c48SFrançois Tigeot 	/* Two dtos; generally use dto1 for DP */
299*c59a5c48SFrançois Tigeot 	value = 0;
300*c59a5c48SFrançois Tigeot 	value |= DCCG_AUDIO_DTO_SEL;
301*c59a5c48SFrançois Tigeot 
302*c59a5c48SFrançois Tigeot 	if (crtc)
303*c59a5c48SFrançois Tigeot 		value |= DCCG_AUDIO_DTO0_SOURCE_SEL(crtc->crtc_id);
304*c59a5c48SFrançois Tigeot 
305*c59a5c48SFrançois Tigeot 	WREG32(DCCG_AUDIO_DTO_SOURCE, value);
306*c59a5c48SFrançois Tigeot 
307*c59a5c48SFrançois Tigeot 	/* Express [24MHz / target pixel clock] as an exact rational
308*c59a5c48SFrançois Tigeot 	 * number (coefficient of two integer numbers.  DCCG_AUDIO_DTOx_PHASE
309*c59a5c48SFrançois Tigeot 	 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
310926deccbSFrançois Tigeot 	 */
311*c59a5c48SFrançois Tigeot 	if (ASIC_IS_DCE41(rdev)) {
312*c59a5c48SFrançois Tigeot 		unsigned int div = (RREG32(DCE41_DENTIST_DISPCLK_CNTL) &
313*c59a5c48SFrançois Tigeot 			DENTIST_DPREFCLK_WDIVIDER_MASK) >>
314*c59a5c48SFrançois Tigeot 			DENTIST_DPREFCLK_WDIVIDER_SHIFT;
315*c59a5c48SFrançois Tigeot 		div = radeon_audio_decode_dfs_div(div);
316*c59a5c48SFrançois Tigeot 
317*c59a5c48SFrançois Tigeot 		if (div)
318*c59a5c48SFrançois Tigeot 			clock = 100 * clock / div;
319*c59a5c48SFrançois Tigeot 	}
320*c59a5c48SFrançois Tigeot 
321*c59a5c48SFrançois Tigeot 	WREG32(DCCG_AUDIO_DTO1_PHASE, 24000);
322*c59a5c48SFrançois Tigeot 	WREG32(DCCG_AUDIO_DTO1_MODULE, clock);
323*c59a5c48SFrançois Tigeot }
324*c59a5c48SFrançois Tigeot 
325*c59a5c48SFrançois Tigeot void dce4_set_vbi_packet(struct drm_encoder *encoder, u32 offset);
dce4_set_vbi_packet(struct drm_encoder * encoder,u32 offset)326*c59a5c48SFrançois Tigeot void dce4_set_vbi_packet(struct drm_encoder *encoder, u32 offset)
327926deccbSFrançois Tigeot {
328926deccbSFrançois Tigeot 	struct drm_device *dev = encoder->dev;
329926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
330926deccbSFrançois Tigeot 
331926deccbSFrançois Tigeot 	WREG32(HDMI_VBI_PACKET_CONTROL + offset,
332*c59a5c48SFrançois Tigeot 		HDMI_NULL_SEND |	/* send null packets when required */
333*c59a5c48SFrançois Tigeot 		HDMI_GC_SEND |		/* send general control packets */
334*c59a5c48SFrançois Tigeot 		HDMI_GC_CONT);		/* send general control packets every frame */
335*c59a5c48SFrançois Tigeot }
336926deccbSFrançois Tigeot 
337*c59a5c48SFrançois Tigeot void dce4_hdmi_set_color_depth(struct drm_encoder *encoder, u32 offset, int bpc);
dce4_hdmi_set_color_depth(struct drm_encoder * encoder,u32 offset,int bpc)338*c59a5c48SFrançois Tigeot void dce4_hdmi_set_color_depth(struct drm_encoder *encoder, u32 offset, int bpc)
339*c59a5c48SFrançois Tigeot {
340*c59a5c48SFrançois Tigeot 	struct drm_device *dev = encoder->dev;
341*c59a5c48SFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
342*c59a5c48SFrançois Tigeot 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
343*c59a5c48SFrançois Tigeot 	uint32_t val;
344926deccbSFrançois Tigeot 
345c6f73aabSFrançois Tigeot 	val = RREG32(HDMI_CONTROL + offset);
346c6f73aabSFrançois Tigeot 	val &= ~HDMI_DEEP_COLOR_ENABLE;
347c6f73aabSFrançois Tigeot 	val &= ~HDMI_DEEP_COLOR_DEPTH_MASK;
348c6f73aabSFrançois Tigeot 
349c6f73aabSFrançois Tigeot 	switch (bpc) {
350c6f73aabSFrançois Tigeot 		case 0:
351c6f73aabSFrançois Tigeot 		case 6:
352c6f73aabSFrançois Tigeot 		case 8:
353c6f73aabSFrançois Tigeot 		case 16:
354c6f73aabSFrançois Tigeot 		default:
355c6f73aabSFrançois Tigeot 			DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
356c6f73aabSFrançois Tigeot 					 connector->name, bpc);
357c6f73aabSFrançois Tigeot 			break;
358c6f73aabSFrançois Tigeot 		case 10:
359c6f73aabSFrançois Tigeot 			val |= HDMI_DEEP_COLOR_ENABLE;
360c6f73aabSFrançois Tigeot 			val |= HDMI_DEEP_COLOR_DEPTH(HDMI_30BIT_DEEP_COLOR);
361c6f73aabSFrançois Tigeot 			DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
362c6f73aabSFrançois Tigeot 					 connector->name);
363c6f73aabSFrançois Tigeot 			break;
364c6f73aabSFrançois Tigeot 		case 12:
365c6f73aabSFrançois Tigeot 			val |= HDMI_DEEP_COLOR_ENABLE;
366c6f73aabSFrançois Tigeot 			val |= HDMI_DEEP_COLOR_DEPTH(HDMI_36BIT_DEEP_COLOR);
367c6f73aabSFrançois Tigeot 			DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
368c6f73aabSFrançois Tigeot 					 connector->name);
369c6f73aabSFrançois Tigeot 			break;
370c6f73aabSFrançois Tigeot 	}
371c6f73aabSFrançois Tigeot 
372c6f73aabSFrançois Tigeot 	WREG32(HDMI_CONTROL + offset, val);
373*c59a5c48SFrançois Tigeot }
374c6f73aabSFrançois Tigeot 
375*c59a5c48SFrançois Tigeot void dce4_set_audio_packet(struct drm_encoder *encoder, u32 offset);
dce4_set_audio_packet(struct drm_encoder * encoder,u32 offset)376*c59a5c48SFrançois Tigeot void dce4_set_audio_packet(struct drm_encoder *encoder, u32 offset)
377*c59a5c48SFrançois Tigeot {
378*c59a5c48SFrançois Tigeot 	struct drm_device *dev = encoder->dev;
379*c59a5c48SFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
380926deccbSFrançois Tigeot 
381926deccbSFrançois Tigeot 	WREG32(AFMT_INFOFRAME_CONTROL0 + offset,
382926deccbSFrançois Tigeot 		AFMT_AUDIO_INFO_UPDATE); /* required for audio info values to be updated */
383926deccbSFrançois Tigeot 
384f43cf1b1SMichael Neumann 	WREG32(AFMT_60958_0 + offset,
385f43cf1b1SMichael Neumann 		AFMT_60958_CS_CHANNEL_NUMBER_L(1));
386f43cf1b1SMichael Neumann 
387f43cf1b1SMichael Neumann 	WREG32(AFMT_60958_1 + offset,
388f43cf1b1SMichael Neumann 		AFMT_60958_CS_CHANNEL_NUMBER_R(2));
389f43cf1b1SMichael Neumann 
390f43cf1b1SMichael Neumann 	WREG32(AFMT_60958_2 + offset,
391f43cf1b1SMichael Neumann 		AFMT_60958_CS_CHANNEL_NUMBER_2(3) |
392f43cf1b1SMichael Neumann 		AFMT_60958_CS_CHANNEL_NUMBER_3(4) |
393f43cf1b1SMichael Neumann 		AFMT_60958_CS_CHANNEL_NUMBER_4(5) |
394f43cf1b1SMichael Neumann 		AFMT_60958_CS_CHANNEL_NUMBER_5(6) |
395f43cf1b1SMichael Neumann 		AFMT_60958_CS_CHANNEL_NUMBER_6(7) |
396f43cf1b1SMichael Neumann 		AFMT_60958_CS_CHANNEL_NUMBER_7(8));
397f43cf1b1SMichael Neumann 
398f43cf1b1SMichael Neumann 	WREG32(AFMT_AUDIO_PACKET_CONTROL2 + offset,
399f43cf1b1SMichael Neumann 		AFMT_AUDIO_CHANNEL_ENABLE(0xff));
400f43cf1b1SMichael Neumann 
401*c59a5c48SFrançois Tigeot 	WREG32(HDMI_AUDIO_PACKET_CONTROL + offset,
402*c59a5c48SFrançois Tigeot 	       HDMI_AUDIO_DELAY_EN(1) | /* set the default audio delay */
403*c59a5c48SFrançois Tigeot 	       HDMI_AUDIO_PACKETS_PER_LINE(3)); /* should be suffient for all audio modes and small enough for all hblanks */
4044cd92098Szrj 
405*c59a5c48SFrançois Tigeot 	/* allow 60958 channel status and send audio packets fields to be updated */
406ee479021SImre Vadász 	WREG32_OR(AFMT_AUDIO_PACKET_CONTROL + offset,
407*c59a5c48SFrançois Tigeot 		  AFMT_RESET_FIFO_WHEN_AUDIO_DIS | AFMT_60958_CS_UPDATE);
408926deccbSFrançois Tigeot }
409f43cf1b1SMichael Neumann 
410*c59a5c48SFrançois Tigeot 
411*c59a5c48SFrançois Tigeot void dce4_set_mute(struct drm_encoder *encoder, u32 offset, bool mute);
dce4_set_mute(struct drm_encoder * encoder,u32 offset,bool mute)412*c59a5c48SFrançois Tigeot void dce4_set_mute(struct drm_encoder *encoder, u32 offset, bool mute)
413*c59a5c48SFrançois Tigeot {
414*c59a5c48SFrançois Tigeot 	struct drm_device *dev = encoder->dev;
415*c59a5c48SFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
416*c59a5c48SFrançois Tigeot 
417*c59a5c48SFrançois Tigeot 	if (mute)
418*c59a5c48SFrançois Tigeot 		WREG32_OR(HDMI_GC + offset, HDMI_GC_AVMUTE);
419*c59a5c48SFrançois Tigeot 	else
420*c59a5c48SFrançois Tigeot 		WREG32_AND(HDMI_GC + offset, ~HDMI_GC_AVMUTE);
421*c59a5c48SFrançois Tigeot }
422*c59a5c48SFrançois Tigeot 
423*c59a5c48SFrançois Tigeot void evergreen_hdmi_enable(struct drm_encoder *encoder, bool enable);
evergreen_hdmi_enable(struct drm_encoder * encoder,bool enable)424f43cf1b1SMichael Neumann void evergreen_hdmi_enable(struct drm_encoder *encoder, bool enable)
425f43cf1b1SMichael Neumann {
426591d5043SFrançois Tigeot 	struct drm_device *dev = encoder->dev;
427591d5043SFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
428f43cf1b1SMichael Neumann 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
429f43cf1b1SMichael Neumann 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
430f43cf1b1SMichael Neumann 
43157e252bfSMichael Neumann 	if (!dig || !dig->afmt)
43257e252bfSMichael Neumann 		return;
43357e252bfSMichael Neumann 
434*c59a5c48SFrançois Tigeot 	if (enable) {
435*c59a5c48SFrançois Tigeot 		struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
436f43cf1b1SMichael Neumann 
437*c59a5c48SFrançois Tigeot 		if (connector && drm_detect_monitor_audio(radeon_connector_edid(connector))) {
438*c59a5c48SFrançois Tigeot 			WREG32(HDMI_INFOFRAME_CONTROL0 + dig->afmt->offset,
439*c59a5c48SFrançois Tigeot 			       HDMI_AVI_INFO_SEND | /* enable AVI info frames */
440*c59a5c48SFrançois Tigeot 			       HDMI_AVI_INFO_CONT | /* required for audio info values to be updated */
441*c59a5c48SFrançois Tigeot 			       HDMI_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
442*c59a5c48SFrançois Tigeot 			       HDMI_AUDIO_INFO_CONT); /* required for audio info values to be updated */
443*c59a5c48SFrançois Tigeot 			WREG32_OR(AFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset,
444*c59a5c48SFrançois Tigeot 				  AFMT_AUDIO_SAMPLE_SEND);
445*c59a5c48SFrançois Tigeot 		} else {
446*c59a5c48SFrançois Tigeot 			WREG32(HDMI_INFOFRAME_CONTROL0 + dig->afmt->offset,
447*c59a5c48SFrançois Tigeot 			       HDMI_AVI_INFO_SEND | /* enable AVI info frames */
448*c59a5c48SFrançois Tigeot 			       HDMI_AVI_INFO_CONT); /* required for audio info values to be updated */
449*c59a5c48SFrançois Tigeot 			WREG32_AND(AFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset,
450*c59a5c48SFrançois Tigeot 				   ~AFMT_AUDIO_SAMPLE_SEND);
451*c59a5c48SFrançois Tigeot 		}
452*c59a5c48SFrançois Tigeot 	} else {
453*c59a5c48SFrançois Tigeot 		WREG32_AND(AFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset,
454*c59a5c48SFrançois Tigeot 			   ~AFMT_AUDIO_SAMPLE_SEND);
455*c59a5c48SFrançois Tigeot 		WREG32(HDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, 0);
456591d5043SFrançois Tigeot 	}
457591d5043SFrançois Tigeot 
458f43cf1b1SMichael Neumann 	dig->afmt->enabled = enable;
459f43cf1b1SMichael Neumann 
460f43cf1b1SMichael Neumann 	DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
461f43cf1b1SMichael Neumann 		  enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
462f43cf1b1SMichael Neumann }
463*c59a5c48SFrançois Tigeot 
464*c59a5c48SFrançois Tigeot void evergreen_dp_enable(struct drm_encoder *encoder, bool enable);
evergreen_dp_enable(struct drm_encoder * encoder,bool enable)465*c59a5c48SFrançois Tigeot void evergreen_dp_enable(struct drm_encoder *encoder, bool enable)
466*c59a5c48SFrançois Tigeot {
467*c59a5c48SFrançois Tigeot 	struct drm_device *dev = encoder->dev;
468*c59a5c48SFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
469*c59a5c48SFrançois Tigeot 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
470*c59a5c48SFrançois Tigeot 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
471*c59a5c48SFrançois Tigeot 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
472*c59a5c48SFrançois Tigeot 
473*c59a5c48SFrançois Tigeot 	if (!dig || !dig->afmt)
474*c59a5c48SFrançois Tigeot 		return;
475*c59a5c48SFrançois Tigeot 
476*c59a5c48SFrançois Tigeot 	if (enable && connector &&
477*c59a5c48SFrançois Tigeot 	    drm_detect_monitor_audio(radeon_connector_edid(connector))) {
478*c59a5c48SFrançois Tigeot 		struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
479*c59a5c48SFrançois Tigeot 		struct radeon_connector *radeon_connector = to_radeon_connector(connector);
480*c59a5c48SFrançois Tigeot 		struct radeon_connector_atom_dig *dig_connector;
481*c59a5c48SFrançois Tigeot 		uint32_t val;
482*c59a5c48SFrançois Tigeot 
483*c59a5c48SFrançois Tigeot 		WREG32_OR(AFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset,
484*c59a5c48SFrançois Tigeot 			  AFMT_AUDIO_SAMPLE_SEND);
485*c59a5c48SFrançois Tigeot 
486*c59a5c48SFrançois Tigeot 		WREG32(EVERGREEN_DP_SEC_TIMESTAMP + dig->afmt->offset,
487*c59a5c48SFrançois Tigeot 		       EVERGREEN_DP_SEC_TIMESTAMP_MODE(1));
488*c59a5c48SFrançois Tigeot 
489*c59a5c48SFrançois Tigeot 		if (!ASIC_IS_DCE6(rdev) && radeon_connector->con_priv) {
490*c59a5c48SFrançois Tigeot 			dig_connector = radeon_connector->con_priv;
491*c59a5c48SFrançois Tigeot 			val = RREG32(EVERGREEN_DP_SEC_AUD_N + dig->afmt->offset);
492*c59a5c48SFrançois Tigeot 			val &= ~EVERGREEN_DP_SEC_N_BASE_MULTIPLE(0xf);
493*c59a5c48SFrançois Tigeot 
494*c59a5c48SFrançois Tigeot 			if (dig_connector->dp_clock == 162000)
495*c59a5c48SFrançois Tigeot 				val |= EVERGREEN_DP_SEC_N_BASE_MULTIPLE(3);
496*c59a5c48SFrançois Tigeot 			else
497*c59a5c48SFrançois Tigeot 				val |= EVERGREEN_DP_SEC_N_BASE_MULTIPLE(5);
498*c59a5c48SFrançois Tigeot 
499*c59a5c48SFrançois Tigeot 			WREG32(EVERGREEN_DP_SEC_AUD_N + dig->afmt->offset, val);
500*c59a5c48SFrançois Tigeot 		}
501*c59a5c48SFrançois Tigeot 
502*c59a5c48SFrançois Tigeot 		WREG32(EVERGREEN_DP_SEC_CNTL + dig->afmt->offset,
503*c59a5c48SFrançois Tigeot 			EVERGREEN_DP_SEC_ASP_ENABLE |		/* Audio packet transmission */
504*c59a5c48SFrançois Tigeot 			EVERGREEN_DP_SEC_ATP_ENABLE |		/* Audio timestamp packet transmission */
505*c59a5c48SFrançois Tigeot 			EVERGREEN_DP_SEC_AIP_ENABLE |		/* Audio infoframe packet transmission */
506*c59a5c48SFrançois Tigeot 			EVERGREEN_DP_SEC_STREAM_ENABLE);	/* Master enable for secondary stream engine */
507*c59a5c48SFrançois Tigeot 	} else {
508*c59a5c48SFrançois Tigeot 		WREG32(EVERGREEN_DP_SEC_CNTL + dig->afmt->offset, 0);
509*c59a5c48SFrançois Tigeot 		WREG32_AND(AFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset,
510*c59a5c48SFrançois Tigeot 			   ~AFMT_AUDIO_SAMPLE_SEND);
511*c59a5c48SFrançois Tigeot 	}
512*c59a5c48SFrançois Tigeot 
513*c59a5c48SFrançois Tigeot 	dig->afmt->enabled = enable;
514*c59a5c48SFrançois Tigeot }
515