blob: 53159a2c789946ae4ab7b49a8ad1052a03c09d0c [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"
Slava Grigorev3cdde022014-12-02 15:22:43 -050032#include "radeon_audio.h"
Rafał Miłeckic6543a62012-04-28 23:35:24 +020033#include "r600d.h"
Christian Koenigdafc3bd2009-10-11 23:49:13 +020034#include "atom.h"
35
36/*
37 * HDMI color format
38 */
39enum r600_hdmi_color_format {
40 RGB = 0,
41 YCC_422 = 1,
42 YCC_444 = 2
43};
44
45/*
46 * IEC60958 status bits
47 */
48enum r600_hdmi_iec_status_bits {
49 AUDIO_STATUS_DIG_ENABLE = 0x01,
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000050 AUDIO_STATUS_V = 0x02,
51 AUDIO_STATUS_VCFG = 0x04,
Christian Koenigdafc3bd2009-10-11 23:49:13 +020052 AUDIO_STATUS_EMPHASIS = 0x08,
53 AUDIO_STATUS_COPYRIGHT = 0x10,
54 AUDIO_STATUS_NONAUDIO = 0x20,
55 AUDIO_STATUS_PROFESSIONAL = 0x40,
Rafał Miłecki3fe373d2010-03-06 13:03:38 +000056 AUDIO_STATUS_LEVEL = 0x80
Christian Koenigdafc3bd2009-10-11 23:49:13 +020057};
58
Lauri Kasanen1109ca02012-08-31 13:43:50 -040059static const struct radeon_hdmi_acr r600_hdmi_predefined_acr[] = {
Christian Koenigdafc3bd2009-10-11 23:49:13 +020060 /* 32kHz 44.1kHz 48kHz */
61 /* Clock N CTS N CTS N CTS */
Pierre Ossman3e719852013-11-06 20:00:32 +010062 { 25175, 4096, 25175, 28224, 125875, 6144, 25175 }, /* 25,20/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020063 { 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 */
Pierre Ossman3e719852013-11-06 20:00:32 +010068 { 74176, 4096, 74176, 5733, 75335, 6144, 74176 }, /* 74.25/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020069 { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */
Pierre Ossman3e719852013-11-06 20:00:32 +010070 { 148352, 4096, 148352, 5733, 150670, 6144, 148352 }, /* 148.50/1.001 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020071 { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */
Christian Koenigdafc3bd2009-10-11 23:49:13 +020072};
73
Alex Deucher72156672014-09-18 16:36:08 -040074static struct r600_audio_pin r600_audio_status(struct radeon_device *rdev)
75{
76 struct r600_audio_pin status;
77 uint32_t value;
78
79 value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
80
81 /* number of channels */
82 status.channels = (value & 0x7) + 1;
83
84 /* bits per sample */
85 switch ((value & 0xF0) >> 4) {
86 case 0x0:
87 status.bits_per_sample = 8;
88 break;
89 case 0x1:
90 status.bits_per_sample = 16;
91 break;
92 case 0x2:
93 status.bits_per_sample = 20;
94 break;
95 case 0x3:
96 status.bits_per_sample = 24;
97 break;
98 case 0x4:
99 status.bits_per_sample = 32;
100 break;
101 default:
102 dev_err(rdev->dev, "Unknown bits per sample 0x%x, using 16\n",
103 (int)value);
104 status.bits_per_sample = 16;
105 }
106
107 /* current sampling rate in HZ */
108 if (value & 0x4000)
109 status.rate = 44100;
110 else
111 status.rate = 48000;
112 status.rate *= ((value >> 11) & 0x7) + 1;
113 status.rate /= ((value >> 8) & 0x7) + 1;
114
115 value = RREG32(R600_AUDIO_STATUS_BITS);
116
117 /* iec 60958 status bits */
118 status.status_bits = value & 0xff;
119
120 /* iec 60958 category code */
121 status.category_code = (value >> 8) & 0xff;
122
123 return status;
124}
125
126/*
127 * update all hdmi interfaces with current audio parameters
128 */
129void r600_audio_update_hdmi(struct work_struct *work)
130{
131 struct radeon_device *rdev = container_of(work, struct radeon_device,
132 audio_work);
133 struct drm_device *dev = rdev->ddev;
134 struct r600_audio_pin audio_status = r600_audio_status(rdev);
135 struct drm_encoder *encoder;
136 bool changed = false;
137
138 if (rdev->audio.pin[0].channels != audio_status.channels ||
139 rdev->audio.pin[0].rate != audio_status.rate ||
140 rdev->audio.pin[0].bits_per_sample != audio_status.bits_per_sample ||
141 rdev->audio.pin[0].status_bits != audio_status.status_bits ||
142 rdev->audio.pin[0].category_code != audio_status.category_code) {
143 rdev->audio.pin[0] = audio_status;
144 changed = true;
145 }
146
147 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
148 if (!radeon_encoder_is_digital(encoder))
149 continue;
150 if (changed || r600_hdmi_buffer_status_changed(encoder))
151 r600_hdmi_update_audio_settings(encoder);
152 }
153}
154
155/* enable the audio stream */
156void r600_audio_enable(struct radeon_device *rdev,
157 struct r600_audio_pin *pin,
Alex Deucherd3d8c142014-09-18 17:26:39 -0400158 u8 enable_mask)
Alex Deucher72156672014-09-18 16:36:08 -0400159{
Alex Deucherd3d8c142014-09-18 17:26:39 -0400160 u32 tmp = RREG32(AZ_HOT_PLUG_CONTROL);
Alex Deucher72156672014-09-18 16:36:08 -0400161
162 if (!pin)
163 return;
164
Alex Deucherd3d8c142014-09-18 17:26:39 -0400165 if (enable_mask) {
166 tmp |= AUDIO_ENABLED;
167 if (enable_mask & 1)
168 tmp |= PIN0_AUDIO_ENABLED;
169 if (enable_mask & 2)
170 tmp |= PIN1_AUDIO_ENABLED;
171 if (enable_mask & 4)
172 tmp |= PIN2_AUDIO_ENABLED;
173 if (enable_mask & 8)
174 tmp |= PIN3_AUDIO_ENABLED;
Alex Deucher72156672014-09-18 16:36:08 -0400175 } else {
Alex Deucherd3d8c142014-09-18 17:26:39 -0400176 tmp &= ~(AUDIO_ENABLED |
177 PIN0_AUDIO_ENABLED |
178 PIN1_AUDIO_ENABLED |
179 PIN2_AUDIO_ENABLED |
180 PIN3_AUDIO_ENABLED);
Alex Deucher72156672014-09-18 16:36:08 -0400181 }
Alex Deucherd3d8c142014-09-18 17:26:39 -0400182
183 WREG32(AZ_HOT_PLUG_CONTROL, tmp);
Alex Deucher72156672014-09-18 16:36:08 -0400184}
185
186/*
Alex Deucher72156672014-09-18 16:36:08 -0400187 * release the audio timer
188 * TODO: How to do this correctly on SMP systems?
189 */
190void r600_audio_fini(struct radeon_device *rdev)
191{
192 if (!rdev->audio.enabled)
193 return;
194
Alex Deucherd3d8c142014-09-18 17:26:39 -0400195 r600_audio_enable(rdev, &rdev->audio.pin[0], 0);
Alex Deucher72156672014-09-18 16:36:08 -0400196
197 rdev->audio.enabled = false;
198}
199
200struct r600_audio_pin *r600_audio_get_pin(struct radeon_device *rdev)
201{
202 /* only one pin on 6xx-NI */
203 return &rdev->audio.pin[0];
204}
205
206/*
Pierre Ossmana2098252013-11-06 20:09:08 +0100207 * calculate CTS and N values if they are not found in the table
208 */
209static void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int *N, int freq)
210{
211 int n, cts;
212 unsigned long div, mul;
213
214 /* Safe, but overly large values */
215 n = 128 * freq;
216 cts = clock * 1000;
217
218 /* Smallest valid fraction */
219 div = gcd(n, cts);
220
221 n /= div;
222 cts /= div;
223
224 /*
225 * The optimal N is 128*freq/1000. Calculate the closest larger
226 * value that doesn't truncate any bits.
227 */
228 mul = ((128*freq/1000) + (n-1))/n;
229
230 n *= mul;
231 cts *= mul;
232
233 /* Check that we are in spec (not always possible) */
234 if (n < (128*freq/1500))
235 printk(KERN_WARNING "Calculated ACR N value is too small. You may experience audio problems.\n");
236 if (n > (128*freq/300))
237 printk(KERN_WARNING "Calculated ACR N value is too large. You may experience audio problems.\n");
238
239 *N = n;
240 *CTS = cts;
241
242 DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
243 *N, *CTS, freq);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200244}
245
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200246struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
247{
248 struct radeon_hdmi_acr res;
249 u8 i;
250
Pierre Ossmana2098252013-11-06 20:09:08 +0100251 /* Precalculated values for common clocks */
252 for (i = 0; i < ARRAY_SIZE(r600_hdmi_predefined_acr); i++) {
253 if (r600_hdmi_predefined_acr[i].clock == clock)
254 return r600_hdmi_predefined_acr[i];
255 }
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200256
Pierre Ossmana2098252013-11-06 20:09:08 +0100257 /* And odd clocks get manually calculated */
258 r600_hdmi_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
259 r600_hdmi_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
260 r600_hdmi_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200261
262 return res;
263}
264
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200265/*
266 * update the N and CTS parameters for a given pixel clock rate
267 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200268void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200269{
270 struct drm_device *dev = encoder->dev;
271 struct radeon_device *rdev = dev->dev_private;
Rafał Miłecki1b688d02012-04-30 15:44:54 +0200272 struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200273 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
274 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
275 uint32_t offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200276
Rafał Miłecki68706332014-05-16 11:10:30 +0200277 WREG32_P(HDMI0_ACR_32_0 + offset,
278 HDMI0_ACR_CTS_32(acr.cts_32khz),
279 ~HDMI0_ACR_CTS_32_MASK);
280 WREG32_P(HDMI0_ACR_32_1 + offset,
281 HDMI0_ACR_N_32(acr.n_32khz),
282 ~HDMI0_ACR_N_32_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200283
Rafał Miłecki68706332014-05-16 11:10:30 +0200284 WREG32_P(HDMI0_ACR_44_0 + offset,
285 HDMI0_ACR_CTS_44(acr.cts_44_1khz),
286 ~HDMI0_ACR_CTS_44_MASK);
287 WREG32_P(HDMI0_ACR_44_1 + offset,
288 HDMI0_ACR_N_44(acr.n_44_1khz),
289 ~HDMI0_ACR_N_44_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200290
Rafał Miłecki68706332014-05-16 11:10:30 +0200291 WREG32_P(HDMI0_ACR_48_0 + offset,
292 HDMI0_ACR_CTS_48(acr.cts_48khz),
293 ~HDMI0_ACR_CTS_48_MASK);
294 WREG32_P(HDMI0_ACR_48_1 + offset,
295 HDMI0_ACR_N_48(acr.n_48khz),
296 ~HDMI0_ACR_N_48_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200297}
298
299/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200300 * build a HDMI Video Info Frame
301 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200302void r600_hdmi_update_avi_infoframe(struct drm_encoder *encoder, void *buffer,
303 size_t size)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200304{
305 struct drm_device *dev = encoder->dev;
306 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200307 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
308 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
309 uint32_t offset = dig->afmt->offset;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100310 uint8_t *frame = buffer + 3;
Alex Deucherf1003802013-06-07 10:41:03 -0400311 uint8_t *header = buffer;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200312
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200313 WREG32(HDMI0_AVI_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200314 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200315 WREG32(HDMI0_AVI_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200316 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200317 WREG32(HDMI0_AVI_INFO2 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200318 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200319 WREG32(HDMI0_AVI_INFO3 + offset,
Alex Deucherf1003802013-06-07 10:41:03 -0400320 frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200321}
322
323/*
324 * build a Audio Info Frame
325 */
Thierry Redinge3b2e032013-01-14 13:36:30 +0100326static void r600_hdmi_update_audio_infoframe(struct drm_encoder *encoder,
327 const void *buffer, size_t size)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200328{
329 struct drm_device *dev = encoder->dev;
330 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200331 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
332 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
333 uint32_t offset = dig->afmt->offset;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100334 const u8 *frame = buffer + 3;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200335
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200336 WREG32(HDMI0_AUDIO_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200337 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200338 WREG32(HDMI0_AUDIO_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200339 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
340}
341
342/*
343 * test if audio buffer is filled enough to start playing
344 */
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200345static bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200346{
347 struct drm_device *dev = encoder->dev;
348 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200349 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
350 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
351 uint32_t offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200352
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200353 return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200354}
355
356/*
357 * have buffer status changed since last call?
358 */
359int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
360{
361 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200362 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200363 int status, result;
364
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200365 if (!dig->afmt || !dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200366 return 0;
367
368 status = r600_hdmi_is_audio_buffer_filled(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200369 result = dig->afmt->last_buffer_filled_status != status;
370 dig->afmt->last_buffer_filled_status = status;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200371
372 return result;
373}
374
375/*
376 * write the audio workaround status to the hardware
377 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200378void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200379{
380 struct drm_device *dev = encoder->dev;
381 struct radeon_device *rdev = dev->dev_private;
382 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200383 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
384 uint32_t offset = dig->afmt->offset;
385 bool hdmi_audio_workaround = false; /* FIXME */
386 u32 value;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200387
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200388 if (!hdmi_audio_workaround ||
389 r600_hdmi_is_audio_buffer_filled(encoder))
390 value = 0; /* disable workaround */
391 else
392 value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
393 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
394 value, ~HDMI0_AUDIO_TEST_EN);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200395}
396
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200397void r600_audio_set_dto(struct drm_encoder *encoder, u32 clock)
Alex Deucherb1f6f472013-04-18 10:50:55 -0400398{
399 struct drm_device *dev = encoder->dev;
400 struct radeon_device *rdev = dev->dev_private;
401 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
402 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucher731da212013-05-13 11:35:26 -0400403 u32 base_rate = 24000;
Alex Deucher1518dd82013-07-30 17:31:07 -0400404 u32 max_ratio = clock / base_rate;
405 u32 dto_phase;
406 u32 dto_modulo = clock;
407 u32 wallclock_ratio;
408 u32 dto_cntl;
Alex Deucherb1f6f472013-04-18 10:50:55 -0400409
410 if (!dig || !dig->afmt)
411 return;
412
Alex Deucher1518dd82013-07-30 17:31:07 -0400413 if (max_ratio >= 8) {
414 dto_phase = 192 * 1000;
415 wallclock_ratio = 3;
416 } else if (max_ratio >= 4) {
417 dto_phase = 96 * 1000;
418 wallclock_ratio = 2;
419 } else if (max_ratio >= 2) {
420 dto_phase = 48 * 1000;
421 wallclock_ratio = 1;
422 } else {
423 dto_phase = 24 * 1000;
424 wallclock_ratio = 0;
425 }
426
Alex Deucherb1f6f472013-04-18 10:50:55 -0400427 /* there are two DTOs selected by DCCG_AUDIO_DTO_SELECT.
428 * doesn't matter which one you use. Just use the first one.
429 */
Alex Deucherb1f6f472013-04-18 10:50:55 -0400430 /* XXX two dtos; generally use dto0 for hdmi */
431 /* Express [24MHz / target pixel clock] as an exact rational
432 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
433 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
434 */
Alex Deucher58d327d2013-09-25 12:04:37 -0400435 if (ASIC_IS_DCE32(rdev)) {
Alex Deuchere1accbf2013-07-29 18:56:13 -0400436 if (dig->dig_encoder == 0) {
Alex Deucher1518dd82013-07-30 17:31:07 -0400437 dto_cntl = RREG32(DCCG_AUDIO_DTO0_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
438 dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
439 WREG32(DCCG_AUDIO_DTO0_CNTL, dto_cntl);
440 WREG32(DCCG_AUDIO_DTO0_PHASE, dto_phase);
441 WREG32(DCCG_AUDIO_DTO0_MODULE, dto_modulo);
Alex Deuchere1accbf2013-07-29 18:56:13 -0400442 WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
443 } else {
Alex Deucher1518dd82013-07-30 17:31:07 -0400444 dto_cntl = RREG32(DCCG_AUDIO_DTO1_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
445 dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
446 WREG32(DCCG_AUDIO_DTO1_CNTL, dto_cntl);
447 WREG32(DCCG_AUDIO_DTO1_PHASE, dto_phase);
448 WREG32(DCCG_AUDIO_DTO1_MODULE, dto_modulo);
Alex Deuchere1accbf2013-07-29 18:56:13 -0400449 WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
450 }
Alex Deucher55d4e022013-11-25 13:20:59 -0500451 } else {
Alex Deucher58d327d2013-09-25 12:04:37 -0400452 /* according to the reg specs, this should DCE3.2 only, but in
Alex Deucher55d4e022013-11-25 13:20:59 -0500453 * practice it seems to cover DCE2.0/3.0/3.1 as well.
Alex Deucher58d327d2013-09-25 12:04:37 -0400454 */
455 if (dig->dig_encoder == 0) {
456 WREG32(DCCG_AUDIO_DTO0_PHASE, base_rate * 100);
457 WREG32(DCCG_AUDIO_DTO0_MODULE, clock * 100);
458 WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
459 } else {
460 WREG32(DCCG_AUDIO_DTO1_PHASE, base_rate * 100);
461 WREG32(DCCG_AUDIO_DTO1_MODULE, clock * 100);
462 WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
463 }
Alex Deucher15865052013-04-22 09:42:07 -0400464 }
Alex Deucherb1f6f472013-04-18 10:50:55 -0400465}
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200466
467/*
468 * update the info frames with the data from the current display mode
469 */
470void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
471{
472 struct drm_device *dev = encoder->dev;
473 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200474 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
475 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100476 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
477 struct hdmi_avi_infoframe frame;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200478 uint32_t offset;
Rafał Miłecki2e93cac2014-05-16 11:10:29 +0200479 uint32_t acr_ctl;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100480 ssize_t err;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200481
Alex Deucherc2b4cacf2013-07-08 18:16:56 -0400482 if (!dig || !dig->afmt)
483 return;
484
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200485 /* Silent, r600_hdmi_enable will raise WARN for us */
486 if (!dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200487 return;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200488 offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200489
Alex Deucher832eafa2014-02-18 11:07:55 -0500490 /* disable audio prior to setting up hw */
Slava Grigorev3cdde022014-12-02 15:22:43 -0500491 dig->afmt->pin = radeon_audio_get_pin(encoder);
Alex Deucherd3d8c142014-09-18 17:26:39 -0400492 r600_audio_enable(rdev, dig->afmt->pin, 0xf);
Alex Deucher832eafa2014-02-18 11:07:55 -0500493
Alex Deucherb1f6f472013-04-18 10:50:55 -0400494 r600_audio_set_dto(encoder, mode->clock);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200495
Rafał Miłecki68706332014-05-16 11:10:30 +0200496 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
497 HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
498 HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
499 HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
500 HDMI0_60958_CS_UPDATE, /* allow 60958 channel status fields to be updated */
501 ~(HDMI0_AUDIO_SAMPLE_SEND |
502 HDMI0_AUDIO_DELAY_EN_MASK |
503 HDMI0_AUDIO_PACKETS_PER_LINE_MASK |
504 HDMI0_60958_CS_UPDATE));
Alex Deucher0ffae602013-08-15 12:03:37 -0400505
Rafał Miłecki2e93cac2014-05-16 11:10:29 +0200506 /* DCE 3.0 uses register that's normally for CRC_CONTROL */
507 acr_ctl = ASIC_IS_DCE3(rdev) ? DCE3_HDMI0_ACR_PACKET_CONTROL :
508 HDMI0_ACR_PACKET_CONTROL;
Rafał Miłecki68706332014-05-16 11:10:30 +0200509 WREG32_P(acr_ctl + offset,
510 HDMI0_ACR_SOURCE | /* select SW CTS value - XXX verify that hw CTS works on all families */
511 HDMI0_ACR_AUTO_SEND, /* allow hw to sent ACR packets when required */
512 ~(HDMI0_ACR_SOURCE |
513 HDMI0_ACR_AUTO_SEND));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200514
Rafał Miłecki68706332014-05-16 11:10:30 +0200515 WREG32_OR(HDMI0_VBI_PACKET_CONTROL + offset,
516 HDMI0_NULL_SEND | /* send null packets when required */
517 HDMI0_GC_SEND | /* send general control packets */
518 HDMI0_GC_CONT); /* send general control packets every frame */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200519
Rafał Miłecki68706332014-05-16 11:10:30 +0200520 WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
521 HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
522 HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
523 HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
524 HDMI0_AUDIO_INFO_UPDATE); /* required for audio info values to be updated */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200525
Rafał Miłecki68706332014-05-16 11:10:30 +0200526 WREG32_P(HDMI0_INFOFRAME_CONTROL1 + offset,
527 HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
528 HDMI0_AUDIO_INFO_LINE(2), /* anything other than 0 */
529 ~(HDMI0_AVI_INFO_LINE_MASK |
530 HDMI0_AUDIO_INFO_LINE_MASK));
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200531
Rafał Miłecki68706332014-05-16 11:10:30 +0200532 WREG32_AND(HDMI0_GC + offset,
533 ~HDMI0_GC_AVMUTE); /* unset HDMI0_GC_AVMUTE */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200534
Thierry Redinge3b2e032013-01-14 13:36:30 +0100535 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
536 if (err < 0) {
537 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
538 return;
539 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200540
Thierry Redinge3b2e032013-01-14 13:36:30 +0100541 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
542 if (err < 0) {
543 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
544 return;
545 }
546
547 r600_hdmi_update_avi_infoframe(encoder, buffer, sizeof(buffer));
Rafał Miłecki68706332014-05-16 11:10:30 +0200548
549 /* fglrx duplicates INFOFRAME_CONTROL0 & INFOFRAME_CONTROL1 ops here */
550
551 WREG32_AND(HDMI0_GENERIC_PACKET_CONTROL + offset,
552 ~(HDMI0_GENERIC0_SEND |
553 HDMI0_GENERIC0_CONT |
554 HDMI0_GENERIC0_UPDATE |
555 HDMI0_GENERIC1_SEND |
556 HDMI0_GENERIC1_CONT |
557 HDMI0_GENERIC0_LINE_MASK |
558 HDMI0_GENERIC1_LINE_MASK));
559
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200560 r600_hdmi_update_ACR(encoder, mode->clock);
561
Rafał Miłecki68706332014-05-16 11:10:30 +0200562 WREG32_P(HDMI0_60958_0 + offset,
563 HDMI0_60958_CS_CHANNEL_NUMBER_L(1),
564 ~(HDMI0_60958_CS_CHANNEL_NUMBER_L_MASK |
565 HDMI0_60958_CS_CLOCK_ACCURACY_MASK));
566
567 WREG32_P(HDMI0_60958_1 + offset,
568 HDMI0_60958_CS_CHANNEL_NUMBER_R(2),
569 ~HDMI0_60958_CS_CHANNEL_NUMBER_R_MASK);
570
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300571 /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200572 WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
573 WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
574 WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
575 WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200576
Alex Deucher832eafa2014-02-18 11:07:55 -0500577 /* enable audio after to setting up hw */
Alex Deucherd3d8c142014-09-18 17:26:39 -0400578 r600_audio_enable(rdev, dig->afmt->pin, 0xf);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200579}
580
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200581/**
582 * r600_hdmi_update_audio_settings - Update audio infoframe
583 *
584 * @encoder: drm encoder
585 *
586 * Gets info about current audio stream and updates audio infoframe.
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200587 */
Christian König58bd0862010-04-05 22:14:55 +0200588void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200589{
590 struct drm_device *dev = encoder->dev;
591 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200592 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
593 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb5306022013-07-31 16:51:33 -0400594 struct r600_audio_pin audio = r600_audio_status(rdev);
Thierry Redinge3b2e032013-01-14 13:36:30 +0100595 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
596 struct hdmi_audio_infoframe frame;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200597 uint32_t offset;
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200598 uint32_t value;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100599 ssize_t err;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200600
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200601 if (!dig->afmt || !dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200602 return;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200603 offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200604
605 DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
606 r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
Rafał Miłecki3299de92012-05-14 21:25:57 +0200607 audio.channels, audio.rate, audio.bits_per_sample);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200608 DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
Rafał Miłecki3299de92012-05-14 21:25:57 +0200609 (int)audio.status_bits, (int)audio.category_code);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200610
Thierry Redinge3b2e032013-01-14 13:36:30 +0100611 err = hdmi_audio_infoframe_init(&frame);
612 if (err < 0) {
613 DRM_ERROR("failed to setup audio infoframe\n");
614 return;
615 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200616
Thierry Redinge3b2e032013-01-14 13:36:30 +0100617 frame.channels = audio.channels;
618
619 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
620 if (err < 0) {
621 DRM_ERROR("failed to pack audio infoframe\n");
622 return;
623 }
624
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200625 value = RREG32(HDMI0_AUDIO_PACKET_CONTROL + offset);
626 if (value & HDMI0_AUDIO_TEST_EN)
627 WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
628 value & ~HDMI0_AUDIO_TEST_EN);
629
630 WREG32_OR(HDMI0_CONTROL + offset,
631 HDMI0_ERROR_ACK);
632
633 WREG32_AND(HDMI0_INFOFRAME_CONTROL0 + offset,
634 ~HDMI0_AUDIO_INFO_SOURCE);
635
Thierry Redinge3b2e032013-01-14 13:36:30 +0100636 r600_hdmi_update_audio_infoframe(encoder, buffer, sizeof(buffer));
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200637
638 WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
639 HDMI0_AUDIO_INFO_CONT |
640 HDMI0_AUDIO_INFO_UPDATE);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200641}
642
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200643/*
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000644 * enable the HDMI engine
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200645 */
Alex Deuchera973bea2013-04-18 11:32:16 -0400646void r600_hdmi_enable(struct drm_encoder *encoder, bool enable)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200647{
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000648 struct drm_device *dev = encoder->dev;
649 struct radeon_device *rdev = dev->dev_private;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200650 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200651 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deuchera973bea2013-04-18 11:32:16 -0400652 u32 hdmi = HDMI0_ERROR_ACK;
Alex Deucher16823d12010-04-16 11:35:30 -0400653
Alex Deucherc2b4cacf2013-07-08 18:16:56 -0400654 if (!dig || !dig->afmt)
655 return;
656
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200657 /* Silent, r600_hdmi_enable will raise WARN for us */
Alex Deuchera973bea2013-04-18 11:32:16 -0400658 if (enable && dig->afmt->enabled)
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200659 return;
Alex Deuchera973bea2013-04-18 11:32:16 -0400660 if (!enable && !dig->afmt->enabled)
661 return;
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200662
Alex Deucher4adb34e2014-09-18 18:07:08 -0400663 if (!enable && dig->afmt->pin) {
664 r600_audio_enable(rdev, dig->afmt->pin, 0);
665 dig->afmt->pin = NULL;
666 }
667
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200668 /* Older chipsets require setting HDMI and routing manually */
Alex Deuchera973bea2013-04-18 11:32:16 -0400669 if (!ASIC_IS_DCE3(rdev)) {
670 if (enable)
671 hdmi |= HDMI0_ENABLE;
Rafał Miłecki5715f672010-03-06 13:03:35 +0000672 switch (radeon_encoder->encoder_id) {
673 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400674 if (enable) {
675 WREG32_OR(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN);
676 hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
677 } else {
678 WREG32_AND(AVIVO_TMDSA_CNTL, ~AVIVO_TMDSA_CNTL_HDMI_EN);
679 }
Rafał Miłecki5715f672010-03-06 13:03:35 +0000680 break;
681 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400682 if (enable) {
683 WREG32_OR(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN);
684 hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
685 } else {
686 WREG32_AND(AVIVO_LVTMA_CNTL, ~AVIVO_LVTMA_CNTL_HDMI_EN);
687 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200688 break;
689 case ENCODER_OBJECT_ID_INTERNAL_DDI:
Alex Deuchera973bea2013-04-18 11:32:16 -0400690 if (enable) {
691 WREG32_OR(DDIA_CNTL, DDIA_HDMI_EN);
692 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
693 } else {
694 WREG32_AND(DDIA_CNTL, ~DDIA_HDMI_EN);
695 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200696 break;
697 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400698 if (enable)
699 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000700 break;
701 default:
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200702 dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
703 radeon_encoder->encoder_id);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000704 break;
705 }
Alex Deuchera973bea2013-04-18 11:32:16 -0400706 WREG32(HDMI0_CONTROL + dig->afmt->offset, hdmi);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000707 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200708
Alex Deucherf122c612012-03-30 08:59:57 -0400709 if (rdev->irq.installed) {
Christian Koenigf2594932010-04-10 03:13:16 +0200710 /* if irq is available use it */
Alex Deucher9054ae12013-04-18 09:42:13 -0400711 /* XXX: shouldn't need this on any asics. Double check DCE2/3 */
Alex Deuchera973bea2013-04-18 11:32:16 -0400712 if (enable)
Alex Deucher9054ae12013-04-18 09:42:13 -0400713 radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
Alex Deuchera973bea2013-04-18 11:32:16 -0400714 else
715 radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
Christian Koenigf2594932010-04-10 03:13:16 +0200716 }
Christian König58bd0862010-04-05 22:14:55 +0200717
Alex Deuchera973bea2013-04-18 11:32:16 -0400718 dig->afmt->enabled = enable;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200719
Alex Deuchera973bea2013-04-18 11:32:16 -0400720 DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
721 enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000722}
723