2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Christian König.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Christian König
26 * $FreeBSD: head/sys/dev/drm2/radeon/r600_hdmi.c 254885 2013-08-25 19:37:15Z dumbbell $
30 #include <uapi_drm/radeon_drm.h>
32 #include "radeon_asic.h"
39 enum r600_hdmi_color_format {
46 * IEC60958 status bits
48 enum r600_hdmi_iec_status_bits {
49 AUDIO_STATUS_DIG_ENABLE = 0x01,
50 AUDIO_STATUS_V = 0x02,
51 AUDIO_STATUS_VCFG = 0x04,
52 AUDIO_STATUS_EMPHASIS = 0x08,
53 AUDIO_STATUS_COPYRIGHT = 0x10,
54 AUDIO_STATUS_NONAUDIO = 0x20,
55 AUDIO_STATUS_PROFESSIONAL = 0x40,
56 AUDIO_STATUS_LEVEL = 0x80
59 static const struct radeon_hdmi_acr r600_hdmi_predefined_acr[] = {
60 /* 32kHz 44.1kHz 48kHz */
61 /* Clock N CTS N CTS N CTS */
62 { 25174, 4576, 28125, 7007, 31250, 6864, 28125 }, /* 25,20/1.001 MHz */
63 { 25200, 4096, 25200, 6272, 28000, 6144, 25200 }, /* 25.20 MHz */
64 { 27000, 4096, 27000, 6272, 30000, 6144, 27000 }, /* 27.00 MHz */
65 { 27027, 4096, 27027, 6272, 30030, 6144, 27027 }, /* 27.00*1.001 MHz */
66 { 54000, 4096, 54000, 6272, 60000, 6144, 54000 }, /* 54.00 MHz */
67 { 54054, 4096, 54054, 6272, 60060, 6144, 54054 }, /* 54.00*1.001 MHz */
68 { 74175, 11648, 210937, 17836, 234375, 11648, 140625 }, /* 74.25/1.001 MHz */
69 { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */
70 { 148351, 11648, 421875, 8918, 234375, 5824, 140625 }, /* 148.50/1.001 MHz */
71 { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */
72 { 0, 4096, 0, 6272, 0, 6144, 0 } /* Other */
76 * calculate CTS value if it's not found in the table
78 static void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int N, int freq)
81 *CTS = clock * N / (128 * freq) * 1000;
82 DRM_DEBUG("Using ACR timing N=%d CTS=%d for frequency %d\n",
86 struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
88 struct radeon_hdmi_acr res;
91 for (i = 0; r600_hdmi_predefined_acr[i].clock != clock &&
92 r600_hdmi_predefined_acr[i].clock != 0; i++)
94 res = r600_hdmi_predefined_acr[i];
96 /* In case some CTS are missing */
97 r600_hdmi_calc_cts(clock, &res.cts_32khz, res.n_32khz, 32000);
98 r600_hdmi_calc_cts(clock, &res.cts_44_1khz, res.n_44_1khz, 44100);
99 r600_hdmi_calc_cts(clock, &res.cts_48khz, res.n_48khz, 48000);
105 * update the N and CTS parameters for a given pixel clock rate
107 static void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
109 struct drm_device *dev = encoder->dev;
110 struct radeon_device *rdev = dev->dev_private;
111 struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
112 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
113 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
114 uint32_t offset = dig->afmt->offset;
116 WREG32(HDMI0_ACR_32_0 + offset, HDMI0_ACR_CTS_32(acr.cts_32khz));
117 WREG32(HDMI0_ACR_32_1 + offset, acr.n_32khz);
119 WREG32(HDMI0_ACR_44_0 + offset, HDMI0_ACR_CTS_44(acr.cts_44_1khz));
120 WREG32(HDMI0_ACR_44_1 + offset, acr.n_44_1khz);
122 WREG32(HDMI0_ACR_48_0 + offset, HDMI0_ACR_CTS_48(acr.cts_48khz));
123 WREG32(HDMI0_ACR_48_1 + offset, acr.n_48khz);
127 * calculate the crc for a given info frame
129 static void r600_hdmi_infoframe_checksum(uint8_t packetType,
130 uint8_t versionNumber,
135 frame[0] = packetType + versionNumber + length;
136 for (i = 1; i <= length; i++)
137 frame[0] += frame[i];
138 frame[0] = 0x100 - frame[0];
142 * build a HDMI Video Info Frame
144 static void r600_hdmi_videoinfoframe(
145 struct drm_encoder *encoder,
146 enum r600_hdmi_color_format color_format,
147 int active_information_present,
148 uint8_t active_format_aspect_ratio,
149 uint8_t scan_information,
151 uint8_t ex_colorimetry,
152 uint8_t quantization,
154 uint8_t picture_aspect_ratio,
155 uint8_t video_format_identification,
156 uint8_t pixel_repetition,
157 uint8_t non_uniform_picture_scaling,
158 uint8_t bar_info_data_valid,
165 struct drm_device *dev = encoder->dev;
166 struct radeon_device *rdev = dev->dev_private;
167 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
168 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
169 uint32_t offset = dig->afmt->offset;
175 (scan_information & 0x3) |
176 ((bar_info_data_valid & 0x3) << 2) |
177 ((active_information_present & 0x1) << 4) |
178 ((color_format & 0x3) << 5);
180 (active_format_aspect_ratio & 0xF) |
181 ((picture_aspect_ratio & 0x3) << 4) |
182 ((colorimetry & 0x3) << 6);
184 (non_uniform_picture_scaling & 0x3) |
185 ((quantization & 0x3) << 2) |
186 ((ex_colorimetry & 0x7) << 4) |
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);
199 r600_hdmi_infoframe_checksum(0x82, 0x02, 0x0D, frame);
200 /* 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. */
208 WREG32(HDMI0_AVI_INFO0 + offset,
209 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
210 WREG32(HDMI0_AVI_INFO1 + offset,
211 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
212 WREG32(HDMI0_AVI_INFO2 + offset,
213 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
214 WREG32(HDMI0_AVI_INFO3 + offset,
215 frame[0xC] | (frame[0xD] << 8));
219 * build a Audio Info Frame
221 static void r600_hdmi_audioinfoframe(
222 struct drm_encoder *encoder,
223 uint8_t channel_count,
226 uint8_t sample_frequency,
228 uint8_t channel_allocation,
233 struct drm_device *dev = encoder->dev;
234 struct radeon_device *rdev = dev->dev_private;
235 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
236 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
237 uint32_t offset = dig->afmt->offset;
242 frame[0x1] = (channel_count & 0x7) | ((coding_type & 0xF) << 4);
243 frame[0x2] = (sample_size & 0x3) | ((sample_frequency & 0x7) << 2);
245 frame[0x4] = channel_allocation;
246 frame[0x5] = ((level_shift & 0xF) << 3) | ((downmix_inhibit & 0x1) << 7);
253 r600_hdmi_infoframe_checksum(0x84, 0x01, 0x0A, frame);
255 WREG32(HDMI0_AUDIO_INFO0 + offset,
256 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
257 WREG32(HDMI0_AUDIO_INFO1 + offset,
258 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
262 * test if audio buffer is filled enough to start playing
264 static bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
266 struct drm_device *dev = encoder->dev;
267 struct radeon_device *rdev = dev->dev_private;
268 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
269 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
270 uint32_t offset = dig->afmt->offset;
272 return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
276 * have buffer status changed since last call?
278 int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
280 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
281 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
284 if (!dig->afmt || !dig->afmt->enabled)
287 status = r600_hdmi_is_audio_buffer_filled(encoder);
288 result = dig->afmt->last_buffer_filled_status != status;
289 dig->afmt->last_buffer_filled_status = status;
295 * write the audio workaround status to the hardware
297 static void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
299 struct drm_device *dev = encoder->dev;
300 struct radeon_device *rdev = dev->dev_private;
301 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
302 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
303 uint32_t offset = dig->afmt->offset;
304 bool hdmi_audio_workaround = false; /* FIXME */
307 if (!hdmi_audio_workaround ||
308 r600_hdmi_is_audio_buffer_filled(encoder))
309 value = 0; /* disable workaround */
311 value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
312 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
313 value, ~HDMI0_AUDIO_TEST_EN);
318 * update the info frames with the data from the current display mode
320 void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
322 struct drm_device *dev = encoder->dev;
323 struct radeon_device *rdev = dev->dev_private;
324 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
325 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
328 /* Silent, r600_hdmi_enable will raise WARN for us */
329 if (!dig->afmt->enabled)
331 offset = dig->afmt->offset;
333 r600_audio_set_clock(encoder, mode->clock);
335 WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
336 HDMI0_NULL_SEND); /* send null packets when required */
338 WREG32(HDMI0_AUDIO_CRC_CONTROL + offset, 0x1000);
340 if (ASIC_IS_DCE32(rdev)) {
341 WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
342 HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
343 HDMI0_AUDIO_PACKETS_PER_LINE(3)); /* should be suffient for all audio modes and small enough for all hblanks */
344 WREG32(AFMT_AUDIO_PACKET_CONTROL + offset,
345 AFMT_AUDIO_SAMPLE_SEND | /* send audio packets */
346 AFMT_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
348 WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
349 HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
350 HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
351 HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
352 HDMI0_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
355 WREG32(HDMI0_ACR_PACKET_CONTROL + offset,
356 HDMI0_ACR_AUTO_SEND | /* allow hw to sent ACR packets when required */
357 HDMI0_ACR_SOURCE); /* select SW CTS value */
359 WREG32(HDMI0_VBI_PACKET_CONTROL + offset,
360 HDMI0_NULL_SEND | /* send null packets when required */
361 HDMI0_GC_SEND | /* send general control packets */
362 HDMI0_GC_CONT); /* send general control packets every frame */
364 /* TODO: HDMI0_AUDIO_INFO_UPDATE */
365 WREG32(HDMI0_INFOFRAME_CONTROL0 + offset,
366 HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
367 HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
368 HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
369 HDMI0_AUDIO_INFO_CONT); /* send audio info frames every frame/field */
371 WREG32(HDMI0_INFOFRAME_CONTROL1 + offset,
372 HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
373 HDMI0_AUDIO_INFO_LINE(2)); /* anything other than 0 */
375 WREG32(HDMI0_GC + offset, 0); /* unset HDMI0_GC_AVMUTE */
377 r600_hdmi_videoinfoframe(encoder, RGB, 0, 0, 0, 0,
378 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
380 r600_hdmi_update_ACR(encoder, mode->clock);
382 /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
383 WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
384 WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
385 WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
386 WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
388 r600_hdmi_audio_workaround(encoder);
392 * update settings with current parameters from audio engine
394 void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
396 struct drm_device *dev = encoder->dev;
397 struct radeon_device *rdev = dev->dev_private;
398 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
399 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
400 struct r600_audio audio = r600_audio_status(rdev);
404 if (!dig->afmt || !dig->afmt->enabled)
406 offset = dig->afmt->offset;
408 DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
409 r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
410 audio.channels, audio.rate, audio.bits_per_sample);
411 DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
412 (int)audio.status_bits, (int)audio.category_code);
415 if (audio.status_bits & AUDIO_STATUS_PROFESSIONAL)
417 if (audio.status_bits & AUDIO_STATUS_NONAUDIO)
419 if (audio.status_bits & AUDIO_STATUS_COPYRIGHT)
421 if (audio.status_bits & AUDIO_STATUS_EMPHASIS)
424 iec |= HDMI0_60958_CS_CATEGORY_CODE(audio.category_code);
426 switch (audio.rate) {
428 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x3);
431 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x0);
434 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x2);
437 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0x8);
440 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xa);
443 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xc);
446 iec |= HDMI0_60958_CS_SAMPLING_FREQUENCY(0xe);
450 WREG32(HDMI0_60958_0 + offset, iec);
453 switch (audio.bits_per_sample) {
455 iec |= HDMI0_60958_CS_WORD_LENGTH(0x2);
458 iec |= HDMI0_60958_CS_WORD_LENGTH(0x3);
461 iec |= HDMI0_60958_CS_WORD_LENGTH(0xb);
464 if (audio.status_bits & AUDIO_STATUS_V)
466 WREG32_P(HDMI0_60958_1 + offset, iec, ~0x5000f);
468 r600_hdmi_audioinfoframe(encoder, audio.channels - 1, 0, 0, 0, 0, 0, 0,
471 r600_hdmi_audio_workaround(encoder);
475 * enable the HDMI engine
477 void r600_hdmi_enable(struct drm_encoder *encoder)
479 struct drm_device *dev = encoder->dev;
480 struct radeon_device *rdev = dev->dev_private;
481 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
482 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
486 if (ASIC_IS_DCE6(rdev))
489 /* Silent, r600_hdmi_enable will raise WARN for us */
490 if (dig->afmt->enabled)
492 offset = dig->afmt->offset;
494 /* Older chipsets require setting HDMI and routing manually */
495 if (rdev->family >= CHIP_R600 && !ASIC_IS_DCE3(rdev)) {
496 hdmi = HDMI0_ERROR_ACK | HDMI0_ENABLE;
497 switch (radeon_encoder->encoder_id) {
498 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
499 WREG32_P(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN,
500 ~AVIVO_TMDSA_CNTL_HDMI_EN);
501 hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
503 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
504 WREG32_P(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN,
505 ~AVIVO_LVTMA_CNTL_HDMI_EN);
506 hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
508 case ENCODER_OBJECT_ID_INTERNAL_DDI:
509 WREG32_P(DDIA_CNTL, DDIA_HDMI_EN, ~DDIA_HDMI_EN);
510 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
512 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
513 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
516 dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
517 radeon_encoder->encoder_id);
520 WREG32(HDMI0_CONTROL + offset, hdmi);
523 if (rdev->irq.installed) {
524 /* if irq is available use it */
525 radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
528 dig->afmt->enabled = true;
530 DRM_DEBUG("Enabling HDMI interface @ 0x%04X for encoder 0x%x\n",
531 offset, radeon_encoder->encoder_id);
535 * disable the HDMI engine
537 void r600_hdmi_disable(struct drm_encoder *encoder)
539 struct drm_device *dev = encoder->dev;
540 struct radeon_device *rdev = dev->dev_private;
541 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
542 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
545 if (ASIC_IS_DCE6(rdev))
548 /* Called for ATOM_ENCODER_MODE_HDMI only */
549 if (!dig || !dig->afmt) {
550 DRM_ERROR("%s: !dig || !dig->afmt", __func__);
553 if (!dig->afmt->enabled)
555 offset = dig->afmt->offset;
557 DRM_DEBUG("Disabling HDMI interface @ 0x%04X for encoder 0x%x\n",
558 offset, radeon_encoder->encoder_id);
561 radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
563 /* Older chipsets not handled by AtomBIOS */
564 if (rdev->family >= CHIP_R600 && !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);
570 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
571 WREG32_P(AVIVO_LVTMA_CNTL, 0,
572 ~AVIVO_LVTMA_CNTL_HDMI_EN);
574 case ENCODER_OBJECT_ID_INTERNAL_DDI:
575 WREG32_P(DDIA_CNTL, 0, ~DDIA_HDMI_EN);
577 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
580 dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
581 radeon_encoder->encoder_id);
584 WREG32(HDMI0_CONTROL + offset, HDMI0_ERROR_ACK);
587 dig->afmt->enabled = false;