2 * Copyright 2013 Advanced Micro Devices, Inc.
3 * Copyright 2014 Rafał Miłecki
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 #include <linux/hdmi.h>
26 #include "radeon_asic.h"
29 static void dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
31 struct radeon_device *rdev = encoder->dev->dev_private;
32 struct drm_connector *connector;
33 struct radeon_connector *radeon_connector = NULL;
38 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
39 if (connector->encoder == encoder) {
40 radeon_connector = to_radeon_connector(connector);
45 if (!radeon_connector) {
46 DRM_ERROR("Couldn't find encoder's connector\n");
50 sad_count = drm_edid_to_speaker_allocation(radeon_connector->edid, &sadb);
52 DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
56 /* program the speaker allocation */
57 tmp = RREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER);
58 tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
60 tmp |= HDMI_CONNECTION;
62 tmp |= SPEAKER_ALLOCATION(sadb[0]);
64 tmp |= SPEAKER_ALLOCATION(5); /* stereo */
65 WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
70 static void dce3_2_afmt_write_sad_regs(struct drm_encoder *encoder)
72 struct radeon_device *rdev = encoder->dev->dev_private;
73 struct drm_connector *connector;
74 struct radeon_connector *radeon_connector = NULL;
78 static const u16 eld_reg_to_type[][2] = {
79 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
80 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
81 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
82 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
83 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
84 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
85 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
86 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
87 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
88 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
89 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
90 { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
93 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
94 if (connector->encoder == encoder) {
95 radeon_connector = to_radeon_connector(connector);
100 if (!radeon_connector) {
101 DRM_ERROR("Couldn't find encoder's connector\n");
105 sad_count = drm_edid_to_sad(radeon_connector->edid, &sads);
107 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
112 for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
115 int max_channels = -1;
118 for (j = 0; j < sad_count; j++) {
119 struct cea_sad *sad = &sads[j];
121 if (sad->format == eld_reg_to_type[i][1]) {
122 if (sad->channels > max_channels) {
123 value = MAX_CHANNELS(sad->channels) |
124 DESCRIPTOR_BYTE_2(sad->byte2) |
125 SUPPORTED_FREQUENCIES(sad->freq);
126 max_channels = sad->channels;
129 if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
130 stereo_freqs |= sad->freq;
136 value |= SUPPORTED_FREQUENCIES_STEREO(stereo_freqs);
138 WREG32(eld_reg_to_type[i][0], value);
145 * update the info frames with the data from the current display mode
147 void dce3_1_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
149 struct drm_device *dev = encoder->dev;
150 struct radeon_device *rdev = dev->dev_private;
151 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
152 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
153 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
154 struct hdmi_avi_infoframe frame;
158 if (!dig || !dig->afmt)
161 /* Silent, r600_hdmi_enable will raise WARN for us */
162 if (!dig->afmt->enabled)
164 offset = dig->afmt->offset;
166 /* disable audio prior to setting up hw */
167 dig->afmt->pin = r600_audio_get_pin(rdev);
168 r600_audio_enable(rdev, dig->afmt->pin, 0);
170 r600_audio_set_dto(encoder, mode->clock);
172 WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
173 HDMI0_NULL_SEND); /* send null packets when required */
175 WREG32(HDMI0_AUDIO_CRC_CONTROL + offset, 0x1000);
177 if (ASIC_IS_DCE32(rdev)) {
178 WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
179 HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
180 HDMI0_AUDIO_PACKETS_PER_LINE(3)); /* should be suffient for all audio modes and small enough for all hblanks */
181 WREG32(AFMT_AUDIO_PACKET_CONTROL + offset,
182 AFMT_AUDIO_SAMPLE_SEND | /* send audio packets */
183 AFMT_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
185 WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
186 HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
187 HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
188 HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
189 HDMI0_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
192 if (ASIC_IS_DCE32(rdev)) {
193 dce3_2_afmt_write_speaker_allocation(encoder);
194 dce3_2_afmt_write_sad_regs(encoder);
197 WREG32(HDMI0_ACR_PACKET_CONTROL + offset,
198 HDMI0_ACR_SOURCE | /* select SW CTS value - XXX verify that hw CTS works on all families */
199 HDMI0_ACR_AUTO_SEND); /* allow hw to sent ACR packets when required */
201 WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
202 HDMI0_NULL_SEND | /* send null packets when required */
203 HDMI0_GC_SEND | /* send general control packets */
204 HDMI0_GC_CONT); /* send general control packets every frame */
206 /* TODO: HDMI0_AUDIO_INFO_UPDATE */
207 WREG32(HDMI0_INFOFRAME_CONTROL0 + offset,
208 HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
209 HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
210 HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
211 HDMI0_AUDIO_INFO_CONT); /* send audio info frames every frame/field */
213 WREG32(HDMI0_INFOFRAME_CONTROL1 + offset,
214 HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
215 HDMI0_AUDIO_INFO_LINE(2)); /* anything other than 0 */
217 WREG32(HDMI0_GC + offset, 0); /* unset HDMI0_GC_AVMUTE */
219 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
221 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
225 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
227 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
231 r600_hdmi_update_avi_infoframe(encoder, buffer, sizeof(buffer));
232 r600_hdmi_update_ACR(encoder, mode->clock);
234 /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
235 WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
236 WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
237 WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
238 WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
240 r600_hdmi_audio_workaround(encoder);
242 /* enable audio after to setting up hw */
243 r600_audio_enable(rdev, dig->afmt->pin, 0xf);