blob: 53ee7ad9612c47dbe3da6e205c646efb1e3bc2c9 [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
Alex Deucher72156672014-09-18 16:36:08 -0400186struct r600_audio_pin *r600_audio_get_pin(struct radeon_device *rdev)
187{
188 /* only one pin on 6xx-NI */
189 return &rdev->audio.pin[0];
190}
191
192/*
Pierre Ossmana2098252013-11-06 20:09:08 +0100193 * calculate CTS and N values if they are not found in the table
194 */
195static void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int *N, int freq)
196{
197 int n, cts;
198 unsigned long div, mul;
199
200 /* Safe, but overly large values */
201 n = 128 * freq;
202 cts = clock * 1000;
203
204 /* Smallest valid fraction */
205 div = gcd(n, cts);
206
207 n /= div;
208 cts /= div;
209
210 /*
211 * The optimal N is 128*freq/1000. Calculate the closest larger
212 * value that doesn't truncate any bits.
213 */
214 mul = ((128*freq/1000) + (n-1))/n;
215
216 n *= mul;
217 cts *= mul;
218
219 /* Check that we are in spec (not always possible) */
220 if (n < (128*freq/1500))
221 printk(KERN_WARNING "Calculated ACR N value is too small. You may experience audio problems.\n");
222 if (n > (128*freq/300))
223 printk(KERN_WARNING "Calculated ACR N value is too large. You may experience audio problems.\n");
224
225 *N = n;
226 *CTS = cts;
227
228 DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
229 *N, *CTS, freq);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200230}
231
Rafał Miłecki1b688d082012-04-30 15:44:54 +0200232struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
233{
234 struct radeon_hdmi_acr res;
235 u8 i;
236
Pierre Ossmana2098252013-11-06 20:09:08 +0100237 /* Precalculated values for common clocks */
238 for (i = 0; i < ARRAY_SIZE(r600_hdmi_predefined_acr); i++) {
239 if (r600_hdmi_predefined_acr[i].clock == clock)
240 return r600_hdmi_predefined_acr[i];
241 }
Rafał Miłecki1b688d082012-04-30 15:44:54 +0200242
Pierre Ossmana2098252013-11-06 20:09:08 +0100243 /* And odd clocks get manually calculated */
244 r600_hdmi_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
245 r600_hdmi_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
246 r600_hdmi_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
Rafał Miłecki1b688d082012-04-30 15:44:54 +0200247
248 return res;
249}
250
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200251/*
252 * update the N and CTS parameters for a given pixel clock rate
253 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200254void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200255{
256 struct drm_device *dev = encoder->dev;
257 struct radeon_device *rdev = dev->dev_private;
Rafał Miłecki1b688d082012-04-30 15:44:54 +0200258 struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200259 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
260 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
261 uint32_t offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200262
Rafał Miłecki68706332014-05-16 11:10:30 +0200263 WREG32_P(HDMI0_ACR_32_0 + offset,
264 HDMI0_ACR_CTS_32(acr.cts_32khz),
265 ~HDMI0_ACR_CTS_32_MASK);
266 WREG32_P(HDMI0_ACR_32_1 + offset,
267 HDMI0_ACR_N_32(acr.n_32khz),
268 ~HDMI0_ACR_N_32_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200269
Rafał Miłecki68706332014-05-16 11:10:30 +0200270 WREG32_P(HDMI0_ACR_44_0 + offset,
271 HDMI0_ACR_CTS_44(acr.cts_44_1khz),
272 ~HDMI0_ACR_CTS_44_MASK);
273 WREG32_P(HDMI0_ACR_44_1 + offset,
274 HDMI0_ACR_N_44(acr.n_44_1khz),
275 ~HDMI0_ACR_N_44_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200276
Rafał Miłecki68706332014-05-16 11:10:30 +0200277 WREG32_P(HDMI0_ACR_48_0 + offset,
278 HDMI0_ACR_CTS_48(acr.cts_48khz),
279 ~HDMI0_ACR_CTS_48_MASK);
280 WREG32_P(HDMI0_ACR_48_1 + offset,
281 HDMI0_ACR_N_48(acr.n_48khz),
282 ~HDMI0_ACR_N_48_MASK);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200283}
284
285/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200286 * build a HDMI Video Info Frame
287 */
Slava Grigorev96ea7af2014-12-05 17:59:56 -0500288void r600_update_avi_infoframe(struct radeon_device *rdev, u32 offset,
289 unsigned char *buffer, size_t size)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200290{
Thierry Redinge3b2e032013-01-14 13:36:30 +0100291 uint8_t *frame = buffer + 3;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200292
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200293 WREG32(HDMI0_AVI_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200294 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200295 WREG32(HDMI0_AVI_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200296 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200297 WREG32(HDMI0_AVI_INFO2 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200298 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200299 WREG32(HDMI0_AVI_INFO3 + offset,
Slava Grigorev96ea7af2014-12-05 17:59:56 -0500300 frame[0xC] | (frame[0xD] << 8) | (buffer[1] << 24));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200301}
302
303/*
304 * build a Audio Info Frame
305 */
Thierry Redinge3b2e032013-01-14 13:36:30 +0100306static void r600_hdmi_update_audio_infoframe(struct drm_encoder *encoder,
307 const void *buffer, size_t size)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200308{
309 struct drm_device *dev = encoder->dev;
310 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200311 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
312 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
313 uint32_t offset = dig->afmt->offset;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100314 const u8 *frame = buffer + 3;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200315
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200316 WREG32(HDMI0_AUDIO_INFO0 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200317 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200318 WREG32(HDMI0_AUDIO_INFO1 + offset,
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200319 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
320}
321
322/*
323 * test if audio buffer is filled enough to start playing
324 */
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200325static bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
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;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200332
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200333 return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200334}
335
336/*
337 * have buffer status changed since last call?
338 */
339int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
340{
341 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200342 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200343 int status, result;
344
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200345 if (!dig->afmt || !dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200346 return 0;
347
348 status = r600_hdmi_is_audio_buffer_filled(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200349 result = dig->afmt->last_buffer_filled_status != status;
350 dig->afmt->last_buffer_filled_status = status;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200351
352 return result;
353}
354
355/*
356 * write the audio workaround status to the hardware
357 */
Rafał Miłecki8f33a152014-05-16 11:36:24 +0200358void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200359{
360 struct drm_device *dev = encoder->dev;
361 struct radeon_device *rdev = dev->dev_private;
362 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200363 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
364 uint32_t offset = dig->afmt->offset;
365 bool hdmi_audio_workaround = false; /* FIXME */
366 u32 value;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200367
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200368 if (!hdmi_audio_workaround ||
369 r600_hdmi_is_audio_buffer_filled(encoder))
370 value = 0; /* disable workaround */
371 else
372 value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
373 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
374 value, ~HDMI0_AUDIO_TEST_EN);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200375}
376
Slava Grigoreva85d6822014-12-05 13:38:31 -0500377void r600_hdmi_audio_set_dto(struct radeon_device *rdev,
378 struct radeon_crtc *crtc, unsigned int clock)
Alex Deucherb1f6f472013-04-18 10:50:55 -0400379{
Slava Grigoreva85d6822014-12-05 13:38:31 -0500380 struct radeon_encoder *radeon_encoder;
381 struct radeon_encoder_atom_dig *dig;
Alex Deucherb1f6f472013-04-18 10:50:55 -0400382
Slava Grigoreva85d6822014-12-05 13:38:31 -0500383 if (!crtc)
Alex Deucherb1f6f472013-04-18 10:50:55 -0400384 return;
385
Slava Grigoreva85d6822014-12-05 13:38:31 -0500386 radeon_encoder = to_radeon_encoder(crtc->encoder);
387 dig = radeon_encoder->enc_priv;
Alex Deucher1518dd82013-07-30 17:31:07 -0400388
Slava Grigoreva85d6822014-12-05 13:38:31 -0500389 if (!dig)
390 return;
391
392 if (dig->dig_encoder == 0) {
393 WREG32(DCCG_AUDIO_DTO0_PHASE, 24000 * 100);
394 WREG32(DCCG_AUDIO_DTO0_MODULE, clock * 100);
395 WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
Alex Deucher55d4e022013-11-25 13:20:59 -0500396 } else {
Slava Grigoreva85d6822014-12-05 13:38:31 -0500397 WREG32(DCCG_AUDIO_DTO1_PHASE, 24000 * 100);
398 WREG32(DCCG_AUDIO_DTO1_MODULE, clock * 100);
399 WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
Alex Deucher15865052013-04-22 09:42:07 -0400400 }
Alex Deucherb1f6f472013-04-18 10:50:55 -0400401}
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200402
403/*
404 * update the info frames with the data from the current display mode
405 */
406void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
407{
408 struct drm_device *dev = encoder->dev;
409 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200410 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
411 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100412 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
413 struct hdmi_avi_infoframe frame;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200414 uint32_t offset;
Rafał Miłecki2e93cac2014-05-16 11:10:29 +0200415 uint32_t acr_ctl;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100416 ssize_t err;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200417
Alex Deucherc2b4cac2013-07-08 18:16:56 -0400418 if (!dig || !dig->afmt)
419 return;
420
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200421 /* Silent, r600_hdmi_enable will raise WARN for us */
422 if (!dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200423 return;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200424 offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200425
Alex Deucher832eafa2014-02-18 11:07:55 -0500426 /* disable audio prior to setting up hw */
Slava Grigorev3cdde022014-12-02 15:22:43 -0500427 dig->afmt->pin = radeon_audio_get_pin(encoder);
Slava Grigorev8bf59822014-12-03 15:29:53 -0500428 radeon_audio_enable(rdev, dig->afmt->pin, 0);
Alex Deucher832eafa2014-02-18 11:07:55 -0500429
Slava Grigoreva85d6822014-12-05 13:38:31 -0500430 radeon_audio_set_dto(encoder, mode->clock);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200431
Rafał Miłecki68706332014-05-16 11:10:30 +0200432 WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
433 HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
434 HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
435 HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
436 HDMI0_60958_CS_UPDATE, /* allow 60958 channel status fields to be updated */
437 ~(HDMI0_AUDIO_SAMPLE_SEND |
438 HDMI0_AUDIO_DELAY_EN_MASK |
439 HDMI0_AUDIO_PACKETS_PER_LINE_MASK |
440 HDMI0_60958_CS_UPDATE));
Alex Deucher0ffae602013-08-15 12:03:37 -0400441
Rafał Miłecki2e93cac2014-05-16 11:10:29 +0200442 /* DCE 3.0 uses register that's normally for CRC_CONTROL */
443 acr_ctl = ASIC_IS_DCE3(rdev) ? DCE3_HDMI0_ACR_PACKET_CONTROL :
444 HDMI0_ACR_PACKET_CONTROL;
Rafał Miłecki68706332014-05-16 11:10:30 +0200445 WREG32_P(acr_ctl + offset,
446 HDMI0_ACR_SOURCE | /* select SW CTS value - XXX verify that hw CTS works on all families */
447 HDMI0_ACR_AUTO_SEND, /* allow hw to sent ACR packets when required */
448 ~(HDMI0_ACR_SOURCE |
449 HDMI0_ACR_AUTO_SEND));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200450
Rafał Miłecki68706332014-05-16 11:10:30 +0200451 WREG32_OR(HDMI0_VBI_PACKET_CONTROL + offset,
452 HDMI0_NULL_SEND | /* send null packets when required */
453 HDMI0_GC_SEND | /* send general control packets */
454 HDMI0_GC_CONT); /* send general control packets every frame */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200455
Rafał Miłecki68706332014-05-16 11:10:30 +0200456 WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
457 HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
458 HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
459 HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
460 HDMI0_AUDIO_INFO_UPDATE); /* required for audio info values to be updated */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200461
Rafał Miłecki68706332014-05-16 11:10:30 +0200462 WREG32_P(HDMI0_INFOFRAME_CONTROL1 + offset,
463 HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
464 HDMI0_AUDIO_INFO_LINE(2), /* anything other than 0 */
465 ~(HDMI0_AVI_INFO_LINE_MASK |
466 HDMI0_AUDIO_INFO_LINE_MASK));
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200467
Rafał Miłecki68706332014-05-16 11:10:30 +0200468 WREG32_AND(HDMI0_GC + offset,
469 ~HDMI0_GC_AVMUTE); /* unset HDMI0_GC_AVMUTE */
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200470
Thierry Redinge3b2e032013-01-14 13:36:30 +0100471 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
472 if (err < 0) {
473 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
474 return;
475 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200476
Thierry Redinge3b2e032013-01-14 13:36:30 +0100477 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
478 if (err < 0) {
479 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
480 return;
481 }
482
Slava Grigorev96ea7af2014-12-05 17:59:56 -0500483 radeon_update_avi_infoframe(encoder, buffer, sizeof(buffer));
Rafał Miłecki68706332014-05-16 11:10:30 +0200484
485 /* fglrx duplicates INFOFRAME_CONTROL0 & INFOFRAME_CONTROL1 ops here */
486
487 WREG32_AND(HDMI0_GENERIC_PACKET_CONTROL + offset,
488 ~(HDMI0_GENERIC0_SEND |
489 HDMI0_GENERIC0_CONT |
490 HDMI0_GENERIC0_UPDATE |
491 HDMI0_GENERIC1_SEND |
492 HDMI0_GENERIC1_CONT |
493 HDMI0_GENERIC0_LINE_MASK |
494 HDMI0_GENERIC1_LINE_MASK));
495
Rafał Miłecki1c3439f2012-05-06 17:29:45 +0200496 r600_hdmi_update_ACR(encoder, mode->clock);
497
Rafał Miłecki68706332014-05-16 11:10:30 +0200498 WREG32_P(HDMI0_60958_0 + offset,
499 HDMI0_60958_CS_CHANNEL_NUMBER_L(1),
500 ~(HDMI0_60958_CS_CHANNEL_NUMBER_L_MASK |
501 HDMI0_60958_CS_CLOCK_ACCURACY_MASK));
502
503 WREG32_P(HDMI0_60958_1 + offset,
504 HDMI0_60958_CS_CHANNEL_NUMBER_R(2),
505 ~HDMI0_60958_CS_CHANNEL_NUMBER_R_MASK);
506
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300507 /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
Rafał Miłeckic6543a62012-04-28 23:35:24 +0200508 WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
509 WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
510 WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
511 WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200512
Alex Deucher832eafa2014-02-18 11:07:55 -0500513 /* enable audio after to setting up hw */
Slava Grigorev8bf59822014-12-03 15:29:53 -0500514 radeon_audio_enable(rdev, dig->afmt->pin, 0xf);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200515}
516
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200517/**
518 * r600_hdmi_update_audio_settings - Update audio infoframe
519 *
520 * @encoder: drm encoder
521 *
522 * Gets info about current audio stream and updates audio infoframe.
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200523 */
Christian König58bd0862010-04-05 22:14:55 +0200524void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200525{
526 struct drm_device *dev = encoder->dev;
527 struct radeon_device *rdev = dev->dev_private;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200528 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
529 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb5306022013-07-31 16:51:33 -0400530 struct r600_audio_pin audio = r600_audio_status(rdev);
Thierry Redinge3b2e032013-01-14 13:36:30 +0100531 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
532 struct hdmi_audio_infoframe frame;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200533 uint32_t offset;
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200534 uint32_t value;
Thierry Redinge3b2e032013-01-14 13:36:30 +0100535 ssize_t err;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200536
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200537 if (!dig->afmt || !dig->afmt->enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200538 return;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200539 offset = dig->afmt->offset;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200540
541 DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
542 r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
Rafał Miłecki3299de92012-05-14 21:25:57 +0200543 audio.channels, audio.rate, audio.bits_per_sample);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200544 DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
Rafał Miłecki3299de92012-05-14 21:25:57 +0200545 (int)audio.status_bits, (int)audio.category_code);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200546
Thierry Redinge3b2e032013-01-14 13:36:30 +0100547 err = hdmi_audio_infoframe_init(&frame);
548 if (err < 0) {
549 DRM_ERROR("failed to setup audio infoframe\n");
550 return;
551 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200552
Thierry Redinge3b2e032013-01-14 13:36:30 +0100553 frame.channels = audio.channels;
554
555 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
556 if (err < 0) {
557 DRM_ERROR("failed to pack audio infoframe\n");
558 return;
559 }
560
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200561 value = RREG32(HDMI0_AUDIO_PACKET_CONTROL + offset);
562 if (value & HDMI0_AUDIO_TEST_EN)
563 WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
564 value & ~HDMI0_AUDIO_TEST_EN);
565
566 WREG32_OR(HDMI0_CONTROL + offset,
567 HDMI0_ERROR_ACK);
568
569 WREG32_AND(HDMI0_INFOFRAME_CONTROL0 + offset,
570 ~HDMI0_AUDIO_INFO_SOURCE);
571
Thierry Redinge3b2e032013-01-14 13:36:30 +0100572 r600_hdmi_update_audio_infoframe(encoder, buffer, sizeof(buffer));
Rafał Miłecki8e4d9f82014-05-16 11:10:31 +0200573
574 WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
575 HDMI0_AUDIO_INFO_CONT |
576 HDMI0_AUDIO_INFO_UPDATE);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200577}
578
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200579/*
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000580 * enable the HDMI engine
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200581 */
Alex Deuchera973bea2013-04-18 11:32:16 -0400582void r600_hdmi_enable(struct drm_encoder *encoder, bool enable)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200583{
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000584 struct drm_device *dev = encoder->dev;
585 struct radeon_device *rdev = dev->dev_private;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200586 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200587 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deuchera973bea2013-04-18 11:32:16 -0400588 u32 hdmi = HDMI0_ERROR_ACK;
Alex Deucher16823d12010-04-16 11:35:30 -0400589
Alex Deucherc2b4cac2013-07-08 18:16:56 -0400590 if (!dig || !dig->afmt)
591 return;
592
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200593 /* Silent, r600_hdmi_enable will raise WARN for us */
Alex Deuchera973bea2013-04-18 11:32:16 -0400594 if (enable && dig->afmt->enabled)
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200595 return;
Alex Deuchera973bea2013-04-18 11:32:16 -0400596 if (!enable && !dig->afmt->enabled)
597 return;
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200598
Alex Deucher4adb34e2014-09-18 18:07:08 -0400599 if (!enable && dig->afmt->pin) {
Slava Grigorev8bf59822014-12-03 15:29:53 -0500600 radeon_audio_enable(rdev, dig->afmt->pin, 0);
Alex Deucher4adb34e2014-09-18 18:07:08 -0400601 dig->afmt->pin = NULL;
602 }
603
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200604 /* Older chipsets require setting HDMI and routing manually */
Alex Deuchera973bea2013-04-18 11:32:16 -0400605 if (!ASIC_IS_DCE3(rdev)) {
606 if (enable)
607 hdmi |= HDMI0_ENABLE;
Rafał Miłecki5715f672010-03-06 13:03:35 +0000608 switch (radeon_encoder->encoder_id) {
609 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400610 if (enable) {
611 WREG32_OR(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN);
612 hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
613 } else {
614 WREG32_AND(AVIVO_TMDSA_CNTL, ~AVIVO_TMDSA_CNTL_HDMI_EN);
615 }
Rafał Miłecki5715f672010-03-06 13:03:35 +0000616 break;
617 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400618 if (enable) {
619 WREG32_OR(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN);
620 hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
621 } else {
622 WREG32_AND(AVIVO_LVTMA_CNTL, ~AVIVO_LVTMA_CNTL_HDMI_EN);
623 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200624 break;
625 case ENCODER_OBJECT_ID_INTERNAL_DDI:
Alex Deuchera973bea2013-04-18 11:32:16 -0400626 if (enable) {
627 WREG32_OR(DDIA_CNTL, DDIA_HDMI_EN);
628 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
629 } else {
630 WREG32_AND(DDIA_CNTL, ~DDIA_HDMI_EN);
631 }
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200632 break;
633 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
Alex Deuchera973bea2013-04-18 11:32:16 -0400634 if (enable)
635 hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000636 break;
637 default:
Rafał Miłecki64fb4fb2012-04-30 15:44:53 +0200638 dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
639 radeon_encoder->encoder_id);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000640 break;
641 }
Alex Deuchera973bea2013-04-18 11:32:16 -0400642 WREG32(HDMI0_CONTROL + dig->afmt->offset, hdmi);
Rafał Miłecki5715f672010-03-06 13:03:35 +0000643 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200644
Alex Deucherf122c612012-03-30 08:59:57 -0400645 if (rdev->irq.installed) {
Christian Koenigf2594932010-04-10 03:13:16 +0200646 /* if irq is available use it */
Alex Deucher9054ae12013-04-18 09:42:13 -0400647 /* XXX: shouldn't need this on any asics. Double check DCE2/3 */
Alex Deuchera973bea2013-04-18 11:32:16 -0400648 if (enable)
Alex Deucher9054ae12013-04-18 09:42:13 -0400649 radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
Alex Deuchera973bea2013-04-18 11:32:16 -0400650 else
651 radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
Christian Koenigf2594932010-04-10 03:13:16 +0200652 }
Christian König58bd0862010-04-05 22:14:55 +0200653
Alex Deuchera973bea2013-04-18 11:32:16 -0400654 dig->afmt->enabled = enable;
Rafał Miłeckicfcbd6d2012-05-14 16:52:30 +0200655
Alex Deuchera973bea2013-04-18 11:32:16 -0400656 DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
657 enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000658}
659