blob: 29e5f49334be1f970d897aacdeff6c08b283689a [file] [log] [blame]
Christian Koenigdafc3bd2009-10-11 23:49:13 +02001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Christian König.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors: Christian König
25 */
Thierry Redinge3b2e032013-01-14 13:36:30 +010026#include <linux/hdmi.h>
Pierre Ossmana2098252013-11-06 20:09:08 +010027#include <linux/gcd.h>
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/radeon_drm.h>
Christian Koenigdafc3bd2009-10-11 23:49:13 +020030#include "radeon.h"
Daniel Vetter3574dda2011-02-18 17:59:19 +010031#include "radeon_asic.h"
Rafał Miłeckic6543a62012-04-28 23:35:24 +020032#include "r600d.h"
Christian Koenigdafc3bd2009-10-11 23:49:13 +020033#include "atom.h"
34
35/*
36 * HDMI color format
37 */
38enum r600_hdmi_color_format {
39 RGB = 0,
40 YCC_422 = 1,
41 YCC_444 = 2
42};
43
44/*
45 * IEC60958 status bits
46 */
47enum r600_hdmi_iec_status_bits {
48 AUDIO_STATUS_DIG_ENABLE = 0x01,
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000049 AUDIO_STATUS_V = 0x02,
50 AUDIO_STATUS_VCFG = 0x04,
Christian Koenigdafc3bd2009-10-11 23:49:13 +020051 AUDIO_STATUS_EMPHASIS = 0x08,
52 AUDIO_STATUS_COPYRIGHT = 0x10,
53 AUDIO_STATUS_NONAUDIO = 0x20,
54 AUDIO_STATUS_PROFESSIONAL = 0x40,
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000055 AUDIO_STATUS_LEVEL = 0x80
Christian Koenigdafc3bd2009-10-11 23:49:13 +020056};
57
Lauri Kasanen1109ca02012-08-31 13:43:50 -040058static const struct radeon_hdmi_acr r600_hdmi_predefined_acr[] = {
Christian Koenigdafc3bd2009-10-11 23:49:13 +020059 /* 32kHz 44.1kHz 48kHz */
60 /* Clock N CTS N CTS N CTS */
Pierre Ossman3e719852013-11-06 20:00:32 +010061 { 25175, 4096, 25175, 28224, 125875, 6144, 25175 }, /* 25,20/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020062 { 25200, 4096, 25200, 6272, 28000, 6144, 25200 }, /* 25.20 MHz */
63 { 27000, 4096, 27000, 6272, 30000, 6144, 27000 }, /* 27.00 MHz */
64 { 27027, 4096, 27027, 6272, 30030, 6144, 27027 }, /* 27.00*1.001 MHz */
65 { 54000, 4096, 54000, 6272, 60000, 6144, 54000 }, /* 54.00 MHz */
66 { 54054, 4096, 54054, 6272, 60060, 6144, 54054 }, /* 54.00*1.001 MHz */
Pierre Ossman3e719852013-11-06 20:00:32 +010067 { 74176, 4096, 74176, 5733, 75335, 6144, 74176 }, /* 74.25/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020068 { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */
Pierre Ossman3e719852013-11-06 20:00:32 +010069 { 148352, 4096, 148352, 5733, 150670, 6144, 148352 }, /* 148.50/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020070 { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020071};
72
Alex Deucher062c2e42013-09-27 18:09:54 -040073
Pierre Ossmana2098252013-11-06 20:09:08 +010074/*
Alex Deucher72156672014-09-18 16:36:08 -040075 * check if the chipset is supported
76 */
77static int r600_audio_chipset_supported(struct radeon_device *rdev)
78{
79 return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev);
80}
81
82static struct r600_audio_pin r600_audio_status(struct radeon_device *rdev)
83{
84 struct r600_audio_pin status;
85 uint32_t value;
86
87 value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
88
89 /* number of channels */
90 status.channels = (value & 0x7) + 1;
91
92 /* bits per sample */
93 switch ((value & 0xF0) >> 4) {
94 case 0x0:
95 status.bits_per_sample = 8;
96 break;
97 case 0x1:
98 status.bits_per_sample = 16;
99 break;
100 case 0x2:
101 status.bits_per_sample = 20;
102 break;
103 case 0x3:
104 status.bits_per_sample = 24;
105 break;
106 case 0x4:
107 status.bits_per_sample = 32;
108 break;
109 default:
110 dev_err(rdev->dev, "Unknown bits per sample 0x%x, using 16\n",
111 (int)value);
112 status.bits_per_sample = 16;
113 }
114
115 /* current sampling rate in HZ */
116 if (value & 0x4000)
117 status.rate = 44100;
118 else
119 status.rate = 48000;
120 status.rate *= ((value >> 11) & 0x7) + 1;
121 status.rate /= ((value >> 8) & 0x7) + 1;
122
123 value = RREG32(R600_AUDIO_STATUS_BITS);
124
125 /* iec 60958 status bits */
126 status.status_bits = value & 0xff;
127
128 /* iec 60958 category code */
129 status.category_code = (value >> 8) & 0xff;
130
131 return status;
132}
133
134/*
135 * update all hdmi interfaces with current audio parameters
136 */
137void r600_audio_update_hdmi(struct work_struct *work)
138{
139 struct radeon_device *rdev = container_of(work, struct radeon_device,
140 audio_work);
141 struct drm_device *dev = rdev->ddev;
142 struct r600_audio_pin audio_status = r600_audio_status(rdev);
143 struct drm_encoder *encoder;
144 bool changed = false;
145
146 if (rdev->audio.pin[0].channels != audio_status.channels ||
147 rdev->audio.pin[0].rate != audio_status.rate ||
148 rdev->audio.pin[0].bits_per_sample != audio_status.bits_per_sample ||
149 rdev->audio.pin[0].status_bits != audio_status.status_bits ||
150 rdev->audio.pin[0].category_code != audio_status.category_code) {
151 rdev->audio.pin[0] = audio_status;
152 changed = true;
153 }
154
155 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
156 if (!radeon_encoder_is_digital(encoder))
157 continue;
158 if (changed || r600_hdmi_buffer_status_changed(encoder))
159 r600_hdmi_update_audio_settings(encoder);
160 }
161}
162
163/* enable the audio stream */
164void r600_audio_enable(struct radeon_device *rdev,
165 struct r600_audio_pin *pin,
166 bool enable)
167{
168 u32 value = 0;
169
170 if (!pin)
171 return;
172
173 if (ASIC_IS_DCE4(rdev)) {
174 if (enable) {
175 value |= 0x81000000; /* Required to enable audio */
176 value |= 0x0e1000f0; /* fglrx sets that too */
177 }
178 WREG32(EVERGREEN_AUDIO_ENABLE, value);
179 } else {
180 WREG32_P(R600_AUDIO_ENABLE,
181 enable ? 0x81000000 : 0x0, ~0x81000000);
182 }
183}
184
185/*
186 * initialize the audio vars
187 */
188int r600_audio_init(struct radeon_device *rdev)
189{
190 if (!radeon_audio || !r600_audio_chipset_supported(rdev))
191 return 0;
192
193 rdev->audio.enabled = true;
194
195 rdev->audio.num_pins = 1;
196 rdev->audio.pin[0].channels = -1;
197 rdev->audio.pin[0].rate = -1;
198 rdev->audio.pin[0].bits_per_sample = -1;
199 rdev->audio.pin[0].status_bits = 0;
200 rdev->audio.pin[0].category_code = 0;
201 rdev->audio.pin[0].id = 0;
202 /* disable audio. it will be set up later */
203 r600_audio_enable(rdev, &rdev->audio.pin[0], false);
204
205 return 0;
206}
207
208/*
209 * release the audio timer
210 * TODO: How to do this correctly on SMP systems?
211 */
212void r600_audio_fini(struct radeon_device *rdev)
213{
214 if (!rdev->audio.enabled)
215 return;
216
217 r600_audio_enable(rdev, &rdev->audio.pin[0], false);
218
219 rdev->audio.enabled = false;
220}
221
222struct r600_audio_pin *r600_audio_get_pin(struct radeon_device *rdev)
223{
224 /* only one pin on 6xx-NI */
225 return &rdev->audio.pin[0];
226}
227
228/*
Pierre Ossmana2098252013-11-06 20:09:08 +0100229 * calculate CTS and N values if they are not found in the table
230 */
231static void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int *N, int freq)
232{
233 int n, cts;
234 unsigned long div, mul;
235
236 /* Safe, but overly large values */
237 n = 128 * freq;
238 cts = clock * 1000;
239
240 /* Smallest valid fraction */
241 div = gcd(n, cts);
242
243 n /= div;
244 cts /= div;
245
246 /*
247 * The optimal N is 128*freq/1000. Calculate the closest larger
248 * value that doesn't truncate any bits.
249 */
250 mul = ((128*freq/1000) + (n-1))/n;
251
252 n *= mul;
253 cts *= mul;
254
255 /* Check that we are in spec (not always possible) */
256 if (n < (128*freq/1500))
257 printk(KERN_WARNING "Calculated ACR N value is too small. You may experience audio problems.\n");
258 if (n > (128*freq/300))
259 printk(KERN_WARNING "Calculated ACR N value is too large. You may experience audio problems.\n");
260
261 *N = n;
262 *CTS = cts;
263
264 DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
265 *N, *CTS, freq);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200266}
267
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200268struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
269{
270 struct radeon_hdmi_acr res;
271 u8 i;
272
Pierre Ossmana2098252013-11-06 20:09:08 +0100273 /* Precalculated values for common clocks */
274 for (i = 0; i < ARRAY_SIZE(r600_hdmi_predefined_acr); i++) {
275 if (r600_hdmi_predefined_acr[i].clock == clock)
276 return r600_hdmi_predefined_acr[i];
277 }
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200278
Pierre Ossmana2098252013-11-06 20:09:08 +0100279 /* And odd clocks get manually calculated */
280 r600_hdmi_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
281 r600_hdmi_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
282 r600_hdmi_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200283
284 return res;
285}
286
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200287/*
288 * update the N and CTS parameters for a given pixel clock rate
289 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200290void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200291{
292 struct drm_device *dev = encoder->dev;
293 struct radeon_device *rdev = dev->dev_private;
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200294 struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200295 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
296 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
297 uint32_t offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200298
Rafał Miłecki68706332014-05-16 11:10:30 +0200299 WREG32_P(HDMI0_ACR_32_0 + offset,
300 HDMI0_ACR_CTS_32(acr.cts_32khz),
301 ~HDMI0_ACR_CTS_32_MASK);
302 WREG32_P(HDMI0_ACR_32_1 + offset,
303 HDMI0_ACR_N_32(acr.n_32khz),
304 ~HDMI0_ACR_N_32_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200305
Rafał Miłecki68706332014-05-16 11:10:30 +0200306 WREG32_P(HDMI0_ACR_44_0 + offset,
307 HDMI0_ACR_CTS_44(acr.cts_44_1khz),
308 ~HDMI0_ACR_CTS_44_MASK);
309 WREG32_P(HDMI0_ACR_44_1 + offset,
310 HDMI0_ACR_N_44(acr.n_44_1khz),
311 ~HDMI0_ACR_N_44_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200312
Rafał Miłecki68706332014-05-16 11:10:30 +0200313 WREG32_P(HDMI0_ACR_48_0 + offset,
314 HDMI0_ACR_CTS_48(acr.cts_48khz),
315 ~HDMI0_ACR_CTS_48_MASK);
316 WREG32_P(HDMI0_ACR_48_1 + offset,
317 HDMI0_ACR_N_48(acr.n_48khz),
318 ~HDMI0_ACR_N_48_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200319}
320
321/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200322 * build a HDMI Video Info Frame
323 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200324void r600_hdmi_update_avi_infoframe(struct drm_encoder *encoder, void *buffer,
325 size_t size)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200326{
327 struct drm_device *dev = encoder->dev;
328 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200329 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
330 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
331 uint32_t offset = dig->afmt->offset;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100332 uint8_t *frame = buffer + 3;
Alex Deucherf1003802013-06-07 10:41:03 -0400333 uint8_t *header = buffer;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200334
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200335 WREG32(HDMI0_AVI_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200336 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200337 WREG32(HDMI0_AVI_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200338 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200339 WREG32(HDMI0_AVI_INFO2 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200340 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200341 WREG32(HDMI0_AVI_INFO3 + offset,
Alex Deucherf1003802013-06-07 10:41:03 -0400342 frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200343}
344
345/*
346 * build a Audio Info Frame
347 */
Thierry Redinge3b2e032013-01-14 13:36:30 +0100348static void r600_hdmi_update_audio_infoframe(struct drm_encoder *encoder,
349 const void *buffer, size_t size)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200350{
351 struct drm_device *dev = encoder->dev;
352 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200353 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
354 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
355 uint32_t offset = dig->afmt->offset;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100356 const u8 *frame = buffer + 3;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200357
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200358 WREG32(HDMI0_AUDIO_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200359 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200360 WREG32(HDMI0_AUDIO_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200361 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
362}
363
364/*
365 * test if audio buffer is filled enough to start playing
366 */
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200367static bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200368{
369 struct drm_device *dev = encoder->dev;
370 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200371 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
372 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
373 uint32_t offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200374
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200375 return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200376}
377
378/*
379 * have buffer status changed since last call?
380 */
381int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
382{
383 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200384 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200385 int status, result;
386
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200387 if (!dig->afmt || !dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200388 return 0;
389
390 status = r600_hdmi_is_audio_buffer_filled(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200391 result = dig->afmt->last_buffer_filled_status != status;
392 dig->afmt->last_buffer_filled_status = status;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200393
394 return result;
395}
396
397/*
398 * write the audio workaround status to the hardware
399 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200400void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200401{
402 struct drm_device *dev = encoder->dev;
403 struct radeon_device *rdev = dev->dev_private;
404 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200405 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
406 uint32_t offset = dig->afmt->offset;
407 bool hdmi_audio_workaround = false; /* FIXME */
408 u32 value;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200409
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200410 if (!hdmi_audio_workaround ||
411 r600_hdmi_is_audio_buffer_filled(encoder))
412 value = 0; /* disable workaround */
413 else
414 value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
415 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
416 value, ~HDMI0_AUDIO_TEST_EN);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200417}
418
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200419void r600_audio_set_dto(struct drm_encoder *encoder, u32 clock)
Alex Deucherb1f6f472013-04-18 10:50:55 -0400420{
421 struct drm_device *dev = encoder->dev;
422 struct radeon_device *rdev = dev->dev_private;
423 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
424 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucher731da212013-05-13 11:35:26 -0400425 u32 base_rate = 24000;
Alex Deucher1518dd82013-07-30 17:31:07 -0400426 u32 max_ratio = clock / base_rate;
427 u32 dto_phase;
428 u32 dto_modulo = clock;
429 u32 wallclock_ratio;
430 u32 dto_cntl;
Alex Deucherb1f6f472013-04-18 10:50:55 -0400431
432 if (!dig || !dig->afmt)
433 return;
434
Alex Deucher1518dd82013-07-30 17:31:07 -0400435 if (max_ratio >= 8) {
436 dto_phase = 192 * 1000;
437 wallclock_ratio = 3;
438 } else if (max_ratio >= 4) {
439 dto_phase = 96 * 1000;
440 wallclock_ratio = 2;
441 } else if (max_ratio >= 2) {
442 dto_phase = 48 * 1000;
443 wallclock_ratio = 1;
444 } else {
445 dto_phase = 24 * 1000;
446 wallclock_ratio = 0;
447 }
448
Alex Deucherb1f6f472013-04-18 10:50:55 -0400449 /* there are two DTOs selected by DCCG_AUDIO_DTO_SELECT.
450 * doesn't matter which one you use. Just use the first one.
451 */
Alex Deucherb1f6f472013-04-18 10:50:55 -0400452 /* XXX two dtos; generally use dto0 for hdmi */
453 /* Express [24MHz / target pixel clock] as an exact rational
454 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
455 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
456 */
Alex Deucher58d327d2013-09-25 12:04:37 -0400457 if (ASIC_IS_DCE32(rdev)) {
Alex Deuchere1accbf2013-07-29 18:56:13 -0400458 if (dig->dig_encoder == 0) {
Alex Deucher1518dd82013-07-30 17:31:07 -0400459 dto_cntl = RREG32(DCCG_AUDIO_DTO0_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
460 dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
461 WREG32(DCCG_AUDIO_DTO0_CNTL, dto_cntl);
462 WREG32(DCCG_AUDIO_DTO0_PHASE, dto_phase);
463 WREG32(DCCG_AUDIO_DTO0_MODULE, dto_modulo);
Alex Deuchere1accbf2013-07-29 18:56:13 -0400464 WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
465 } else {
Alex Deucher1518dd82013-07-30 17:31:07 -0400466 dto_cntl = RREG32(DCCG_AUDIO_DTO1_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
467 dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
468 WREG32(DCCG_AUDIO_DTO1_CNTL, dto_cntl);
469 WREG32(DCCG_AUDIO_DTO1_PHASE, dto_phase);
470 WREG32(DCCG_AUDIO_DTO1_MODULE, dto_modulo);
Alex Deuchere1accbf2013-07-29 18:56:13 -0400471 WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
472 }
Alex Deucher55d4e022013-11-25 13:20:59 -0500473 } else {
Alex Deucher58d327d2013-09-25 12:04:37 -0400474 /* according to the reg specs, this should DCE3.2 only, but in
Alex Deucher55d4e022013-11-25 13:20:59 -0500475 * practice it seems to cover DCE2.0/3.0/3.1 as well.
Alex Deucher58d327d2013-09-25 12:04:37 -0400476 */
477 if (dig->dig_encoder == 0) {
478 WREG32(DCCG_AUDIO_DTO0_PHASE, base_rate * 100);
479 WREG32(DCCG_AUDIO_DTO0_MODULE, clock * 100);
480 WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
481 } else {
482 WREG32(DCCG_AUDIO_DTO1_PHASE, base_rate * 100);
483 WREG32(DCCG_AUDIO_DTO1_MODULE, clock * 100);
484 WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
485 }
Alex Deucher15865052013-04-22 09:42:07 -0400486 }
Alex Deucherb1f6f472013-04-18 10:50:55 -0400487}
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200488
489/*
490 * update the info frames with the data from the current display mode
491 */
492void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
493{
494 struct drm_device *dev = encoder->dev;
495 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200496 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
497 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100498 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
499 struct hdmi_avi_infoframe frame;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200500 uint32_t offset;
Rafał Miłecki2e93cac2014-05-16 11:10:29 +0200501 uint32_t acr_ctl;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100502 ssize_t err;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200503
Alex Deucherc2b4cacf2013-07-08 18:16:56 -0400504 if (!dig || !dig->afmt)
505 return;
506
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200507 /* Silent, r600_hdmi_enable will raise WARN for us */
508 if (!dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200509 return;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200510 offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200511
Alex Deucher832eafa2014-02-18 11:07:55 -0500512 /* disable audio prior to setting up hw */
513 dig->afmt->pin = r600_audio_get_pin(rdev);
514 r600_audio_enable(rdev, dig->afmt->pin, false);
515
Alex Deucherb1f6f472013-04-18 10:50:55 -0400516 r600_audio_set_dto(encoder, mode->clock);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200517
Rafał Miłecki68706332014-05-16 11:10:30 +0200518 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
519 HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
520 HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
521 HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
522 HDMI0_60958_CS_UPDATE, /* allow 60958 channel status fields to be updated */
523 ~(HDMI0_AUDIO_SAMPLE_SEND |
524 HDMI0_AUDIO_DELAY_EN_MASK |
525 HDMI0_AUDIO_PACKETS_PER_LINE_MASK |
526 HDMI0_60958_CS_UPDATE));
Alex Deucher0ffae602013-08-15 12:03:37 -0400527
Rafał Miłecki2e93cac2014-05-16 11:10:29 +0200528 /* DCE 3.0 uses register that's normally for CRC_CONTROL */
529 acr_ctl = ASIC_IS_DCE3(rdev) ? DCE3_HDMI0_ACR_PACKET_CONTROL :
530 HDMI0_ACR_PACKET_CONTROL;
Rafał Miłecki68706332014-05-16 11:10:30 +0200531 WREG32_P(acr_ctl + offset,
532 HDMI0_ACR_SOURCE | /* select SW CTS value - XXX verify that hw CTS works on all families */
533 HDMI0_ACR_AUTO_SEND, /* allow hw to sent ACR packets when required */
534 ~(HDMI0_ACR_SOURCE |
535 HDMI0_ACR_AUTO_SEND));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200536
Rafał Miłecki68706332014-05-16 11:10:30 +0200537 WREG32_OR(HDMI0_VBI_PACKET_CONTROL + offset,
538 HDMI0_NULL_SEND | /* send null packets when required */
539 HDMI0_GC_SEND | /* send general control packets */
540 HDMI0_GC_CONT); /* send general control packets every frame */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200541
Rafał Miłecki68706332014-05-16 11:10:30 +0200542 WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
543 HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
544 HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
545 HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
546 HDMI0_AUDIO_INFO_UPDATE); /* required for audio info values to be updated */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200547
Rafał Miłecki68706332014-05-16 11:10:30 +0200548 WREG32_P(HDMI0_INFOFRAME_CONTROL1 + offset,
549 HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
550 HDMI0_AUDIO_INFO_LINE(2), /* anything other than 0 */
551 ~(HDMI0_AVI_INFO_LINE_MASK |
552 HDMI0_AUDIO_INFO_LINE_MASK));
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200553
Rafał Miłecki68706332014-05-16 11:10:30 +0200554 WREG32_AND(HDMI0_GC + offset,
555 ~HDMI0_GC_AVMUTE); /* unset HDMI0_GC_AVMUTE */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200556
Thierry Redinge3b2e032013-01-14 13:36:30 +0100557 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
558 if (err < 0) {
559 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
560 return;
561 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200562
Thierry Redinge3b2e032013-01-14 13:36:30 +0100563 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
564 if (err < 0) {
565 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
566 return;
567 }
568
569 r600_hdmi_update_avi_infoframe(encoder, buffer, sizeof(buffer));
Rafał Miłecki68706332014-05-16 11:10:30 +0200570
571 /* fglrx duplicates INFOFRAME_CONTROL0 & INFOFRAME_CONTROL1 ops here */
572
573 WREG32_AND(HDMI0_GENERIC_PACKET_CONTROL + offset,
574 ~(HDMI0_GENERIC0_SEND |
575 HDMI0_GENERIC0_CONT |
576 HDMI0_GENERIC0_UPDATE |
577 HDMI0_GENERIC1_SEND |
578 HDMI0_GENERIC1_CONT |
579 HDMI0_GENERIC0_LINE_MASK |
580 HDMI0_GENERIC1_LINE_MASK));
581
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200582 r600_hdmi_update_ACR(encoder, mode->clock);
583
Rafał Miłecki68706332014-05-16 11:10:30 +0200584 WREG32_P(HDMI0_60958_0 + offset,
585 HDMI0_60958_CS_CHANNEL_NUMBER_L(1),
586 ~(HDMI0_60958_CS_CHANNEL_NUMBER_L_MASK |
587 HDMI0_60958_CS_CLOCK_ACCURACY_MASK));
588
589 WREG32_P(HDMI0_60958_1 + offset,
590 HDMI0_60958_CS_CHANNEL_NUMBER_R(2),
591 ~HDMI0_60958_CS_CHANNEL_NUMBER_R_MASK);
592
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300593 /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200594 WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
595 WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
596 WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
597 WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200598
Alex Deucher832eafa2014-02-18 11:07:55 -0500599 /* enable audio after to setting up hw */
600 r600_audio_enable(rdev, dig->afmt->pin, true);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200601}
602
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200603/**
604 * r600_hdmi_update_audio_settings - Update audio infoframe
605 *
606 * @encoder: drm encoder
607 *
608 * Gets info about current audio stream and updates audio infoframe.
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200609 */
Christian König58bd0862010-04-05 22:14:55 +0200610void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200611{
612 struct drm_device *dev = encoder->dev;
613 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200614 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
615 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb5306022013-07-31 16:51:33 -0400616 struct r600_audio_pin audio = r600_audio_status(rdev);
Thierry Redinge3b2e032013-01-14 13:36:30 +0100617 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
618 struct hdmi_audio_infoframe frame;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200619 uint32_t offset;
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200620 uint32_t value;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100621 ssize_t err;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200622
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200623 if (!dig->afmt || !dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200624 return;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200625 offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200626
627 DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
628 r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
Rafał Miłecki3299de92012-05-14 21:25:57 +0200629 audio.channels, audio.rate, audio.bits_per_sample);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200630 DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
Rafał Miłecki3299de92012-05-14 21:25:57 +0200631 (int)audio.status_bits, (int)audio.category_code);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200632
Thierry Redinge3b2e032013-01-14 13:36:30 +0100633 err = hdmi_audio_infoframe_init(&frame);
634 if (err < 0) {
635 DRM_ERROR("failed to setup audio infoframe\n");
636 return;
637 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200638
Thierry Redinge3b2e032013-01-14 13:36:30 +0100639 frame.channels = audio.channels;
640
641 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
642 if (err < 0) {
643 DRM_ERROR("failed to pack audio infoframe\n");
644 return;
645 }
646
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200647 value = RREG32(HDMI0_AUDIO_PACKET_CONTROL + offset);
648 if (value & HDMI0_AUDIO_TEST_EN)
649 WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
650 value & ~HDMI0_AUDIO_TEST_EN);
651
652 WREG32_OR(HDMI0_CONTROL + offset,
653 HDMI0_ERROR_ACK);
654
655 WREG32_AND(HDMI0_INFOFRAME_CONTROL0 + offset,
656 ~HDMI0_AUDIO_INFO_SOURCE);
657
Thierry Redinge3b2e032013-01-14 13:36:30 +0100658 r600_hdmi_update_audio_infoframe(encoder, buffer, sizeof(buffer));
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200659
660 WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
661 HDMI0_AUDIO_INFO_CONT |
662 HDMI0_AUDIO_INFO_UPDATE);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200663}
664
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200665/*
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000666 * enable the HDMI engine
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200667 */
Alex Deuchera973bea2013-04-18 11:32:16 -0400668void r600_hdmi_enable(struct drm_encoder *encoder, bool enable)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200669{
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000670 struct drm_device *dev = encoder->dev;
671 struct radeon_device *rdev = dev->dev_private;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200672 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200673 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deuchera973bea2013-04-18 11:32:16 -0400674 u32 hdmi = HDMI0_ERROR_ACK;
Alex Deucher16823d12010-04-16 11:35:30 -0400675
Alex Deucherc2b4cacf2013-07-08 18:16:56 -0400676 if (!dig || !dig->afmt)
677 return;
678
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200679 /* Silent, r600_hdmi_enable will raise WARN for us */
Alex Deuchera973bea2013-04-18 11:32:16 -0400680 if (enable && dig->afmt->enabled)
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200681 return;
Alex Deuchera973bea2013-04-18 11:32:16 -0400682 if (!enable && !dig->afmt->enabled)
683 return;
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200684
685 /* Older chipsets require setting HDMI and routing manually */
Alex Deuchera973bea2013-04-18 11:32:16 -0400686 if (!ASIC_IS_DCE3(rdev)) {
687 if (enable)
688 hdmi |= HDMI0_ENABLE;
Rafał Miłecki5715f672010-03-06 13:03:35 +0000689 switch (radeon_encoder->encoder_id) {
690 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400691 if (enable) {
692 WREG32_OR(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN);
693 hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
694 } else {
695 WREG32_AND(AVIVO_TMDSA_CNTL, ~AVIVO_TMDSA_CNTL_HDMI_EN);
696 }
Rafał Miłecki5715f672010-03-06 13:03:35 +0000697 break;
698 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400699 if (enable) {
700 WREG32_OR(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN);
701 hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
702 } else {
703 WREG32_AND(AVIVO_LVTMA_CNTL, ~AVIVO_LVTMA_CNTL_HDMI_EN);
704 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200705 break;
706 case ENCODER_OBJECT_ID_INTERNAL_DDI:
Alex Deuchera973bea2013-04-18 11:32:16 -0400707 if (enable) {
708 WREG32_OR(DDIA_CNTL, DDIA_HDMI_EN);
709 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
710 } else {
711 WREG32_AND(DDIA_CNTL, ~DDIA_HDMI_EN);
712 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200713 break;
714 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400715 if (enable)
716 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000717 break;
718 default:
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200719 dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
720 radeon_encoder->encoder_id);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000721 break;
722 }
Alex Deuchera973bea2013-04-18 11:32:16 -0400723 WREG32(HDMI0_CONTROL + dig->afmt->offset, hdmi);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000724 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200725
Alex Deucherf122c612012-03-30 08:59:57 -0400726 if (rdev->irq.installed) {
Christian Koenigf2594932010-04-10 03:13:16 +0200727 /* if irq is available use it */
Alex Deucher9054ae12013-04-18 09:42:13 -0400728 /* XXX: shouldn't need this on any asics. Double check DCE2/3 */
Alex Deuchera973bea2013-04-18 11:32:16 -0400729 if (enable)
Alex Deucher9054ae12013-04-18 09:42:13 -0400730 radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
Alex Deuchera973bea2013-04-18 11:32:16 -0400731 else
732 radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
Christian Koenigf2594932010-04-10 03:13:16 +0200733 }
Christian König58bd0862010-04-05 22:14:55 +0200734
Alex Deuchera973bea2013-04-18 11:32:16 -0400735 dig->afmt->enabled = enable;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200736
Alex Deuchera973bea2013-04-18 11:32:16 -0400737 DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
738 enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000739}
740