1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Slava Grigorev <slava.grigorev@amd.com>
23 */
24
25 #include <linux/gcd.h>
26 #include <linux/component.h>
27
28 #include <drm/drm_crtc.h>
29 #include "dce6_afmt.h"
30 #include "evergreen_hdmi.h"
31 #include "radeon.h"
32 #include "atom.h"
33 #include "r600.h"
34 #include "radeon_audio.h"
35
36 void dce6_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
37 u8 enable_mask);
38 struct r600_audio_pin* r600_audio_get_pin(struct radeon_device *rdev);
39 struct r600_audio_pin* dce6_audio_get_pin(struct radeon_device *rdev);
40 static void radeon_audio_hdmi_mode_set(struct drm_encoder *encoder,
41 struct drm_display_mode *mode);
42 static void radeon_audio_dp_mode_set(struct drm_encoder *encoder,
43 struct drm_display_mode *mode);
44
45 static const u32 pin_offsets[7] =
46 {
47 (0x5e00 - 0x5e00),
48 (0x5e18 - 0x5e00),
49 (0x5e30 - 0x5e00),
50 (0x5e48 - 0x5e00),
51 (0x5e60 - 0x5e00),
52 (0x5e78 - 0x5e00),
53 (0x5e90 - 0x5e00),
54 };
55
radeon_audio_rreg(struct radeon_device * rdev,u32 offset,u32 reg)56 static u32 radeon_audio_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
57 {
58 return RREG32(reg);
59 }
60
radeon_audio_wreg(struct radeon_device * rdev,u32 offset,u32 reg,u32 v)61 static void radeon_audio_wreg(struct radeon_device *rdev, u32 offset,
62 u32 reg, u32 v)
63 {
64 WREG32(reg, v);
65 }
66
67 static struct radeon_audio_basic_funcs r600_funcs = {
68 .endpoint_rreg = radeon_audio_rreg,
69 .endpoint_wreg = radeon_audio_wreg,
70 .enable = r600_audio_enable,
71 };
72
73 static struct radeon_audio_basic_funcs dce32_funcs = {
74 .endpoint_rreg = radeon_audio_rreg,
75 .endpoint_wreg = radeon_audio_wreg,
76 .enable = r600_audio_enable,
77 };
78
79 static struct radeon_audio_basic_funcs dce4_funcs = {
80 .endpoint_rreg = radeon_audio_rreg,
81 .endpoint_wreg = radeon_audio_wreg,
82 .enable = dce4_audio_enable,
83 };
84
85 static struct radeon_audio_basic_funcs dce6_funcs = {
86 .endpoint_rreg = dce6_endpoint_rreg,
87 .endpoint_wreg = dce6_endpoint_wreg,
88 .enable = dce6_audio_enable,
89 };
90
91 static struct radeon_audio_funcs r600_hdmi_funcs = {
92 .get_pin = r600_audio_get_pin,
93 .set_dto = r600_hdmi_audio_set_dto,
94 .update_acr = r600_hdmi_update_acr,
95 .set_vbi_packet = r600_set_vbi_packet,
96 .set_avi_packet = r600_set_avi_packet,
97 .set_audio_packet = r600_set_audio_packet,
98 .set_mute = r600_set_mute,
99 .mode_set = radeon_audio_hdmi_mode_set,
100 .dpms = r600_hdmi_enable,
101 };
102
103 static struct radeon_audio_funcs dce32_hdmi_funcs = {
104 .get_pin = r600_audio_get_pin,
105 .write_sad_regs = dce3_2_afmt_write_sad_regs,
106 .write_speaker_allocation = dce3_2_afmt_hdmi_write_speaker_allocation,
107 .set_dto = dce3_2_audio_set_dto,
108 .update_acr = dce3_2_hdmi_update_acr,
109 .set_vbi_packet = r600_set_vbi_packet,
110 .set_avi_packet = r600_set_avi_packet,
111 .set_audio_packet = dce3_2_set_audio_packet,
112 .set_mute = dce3_2_set_mute,
113 .mode_set = radeon_audio_hdmi_mode_set,
114 .dpms = r600_hdmi_enable,
115 };
116
117 static struct radeon_audio_funcs dce32_dp_funcs = {
118 .get_pin = r600_audio_get_pin,
119 .write_sad_regs = dce3_2_afmt_write_sad_regs,
120 .write_speaker_allocation = dce3_2_afmt_dp_write_speaker_allocation,
121 .set_dto = dce3_2_audio_set_dto,
122 .set_avi_packet = r600_set_avi_packet,
123 .set_audio_packet = dce3_2_set_audio_packet,
124 };
125
126 static struct radeon_audio_funcs dce4_hdmi_funcs = {
127 .get_pin = r600_audio_get_pin,
128 .write_sad_regs = evergreen_hdmi_write_sad_regs,
129 .write_speaker_allocation = dce4_afmt_hdmi_write_speaker_allocation,
130 .write_latency_fields = dce4_afmt_write_latency_fields,
131 .set_dto = dce4_hdmi_audio_set_dto,
132 .update_acr = evergreen_hdmi_update_acr,
133 .set_vbi_packet = dce4_set_vbi_packet,
134 .set_color_depth = dce4_hdmi_set_color_depth,
135 .set_avi_packet = evergreen_set_avi_packet,
136 .set_audio_packet = dce4_set_audio_packet,
137 .set_mute = dce4_set_mute,
138 .mode_set = radeon_audio_hdmi_mode_set,
139 .dpms = evergreen_hdmi_enable,
140 };
141
142 static struct radeon_audio_funcs dce4_dp_funcs = {
143 .get_pin = r600_audio_get_pin,
144 .write_sad_regs = evergreen_hdmi_write_sad_regs,
145 .write_speaker_allocation = dce4_afmt_dp_write_speaker_allocation,
146 .write_latency_fields = dce4_afmt_write_latency_fields,
147 .set_dto = dce4_dp_audio_set_dto,
148 .set_avi_packet = evergreen_set_avi_packet,
149 .set_audio_packet = dce4_set_audio_packet,
150 .mode_set = radeon_audio_dp_mode_set,
151 .dpms = evergreen_dp_enable,
152 };
153
154 static struct radeon_audio_funcs dce6_hdmi_funcs = {
155 .select_pin = dce6_afmt_select_pin,
156 .get_pin = dce6_audio_get_pin,
157 .write_sad_regs = dce6_afmt_write_sad_regs,
158 .write_speaker_allocation = dce6_afmt_hdmi_write_speaker_allocation,
159 .write_latency_fields = dce6_afmt_write_latency_fields,
160 .set_dto = dce6_hdmi_audio_set_dto,
161 .update_acr = evergreen_hdmi_update_acr,
162 .set_vbi_packet = dce4_set_vbi_packet,
163 .set_color_depth = dce4_hdmi_set_color_depth,
164 .set_avi_packet = evergreen_set_avi_packet,
165 .set_audio_packet = dce4_set_audio_packet,
166 .set_mute = dce4_set_mute,
167 .mode_set = radeon_audio_hdmi_mode_set,
168 .dpms = evergreen_hdmi_enable,
169 };
170
171 static struct radeon_audio_funcs dce6_dp_funcs = {
172 .select_pin = dce6_afmt_select_pin,
173 .get_pin = dce6_audio_get_pin,
174 .write_sad_regs = dce6_afmt_write_sad_regs,
175 .write_speaker_allocation = dce6_afmt_dp_write_speaker_allocation,
176 .write_latency_fields = dce6_afmt_write_latency_fields,
177 .set_dto = dce6_dp_audio_set_dto,
178 .set_avi_packet = evergreen_set_avi_packet,
179 .set_audio_packet = dce4_set_audio_packet,
180 .mode_set = radeon_audio_dp_mode_set,
181 .dpms = evergreen_dp_enable,
182 };
183
184 static void radeon_audio_component_notify(struct radeon_device *rdev, int port);
185
radeon_audio_enable(struct radeon_device * rdev,struct r600_audio_pin * pin,u8 enable_mask)186 static void radeon_audio_enable(struct radeon_device *rdev,
187 struct r600_audio_pin *pin, u8 enable_mask)
188 {
189 struct drm_encoder *encoder;
190 struct radeon_encoder *radeon_encoder;
191 struct radeon_encoder_atom_dig *dig;
192 int pin_count = 0;
193
194 if (!pin)
195 return;
196
197 if (rdev->mode_info.mode_config_initialized) {
198 list_for_each_entry(encoder, &rdev->ddev->mode_config.encoder_list, head) {
199 if (radeon_encoder_is_digital(encoder)) {
200 radeon_encoder = to_radeon_encoder(encoder);
201 dig = radeon_encoder->enc_priv;
202 if (dig->pin == pin)
203 pin_count++;
204 }
205 }
206
207 if ((pin_count > 1) && (enable_mask == 0))
208 return;
209 }
210
211 if (rdev->audio.funcs->enable)
212 rdev->audio.funcs->enable(rdev, pin, enable_mask);
213
214 radeon_audio_component_notify(rdev, pin->id);
215 }
216
radeon_audio_interface_init(struct radeon_device * rdev)217 static void radeon_audio_interface_init(struct radeon_device *rdev)
218 {
219 if (ASIC_IS_DCE6(rdev)) {
220 rdev->audio.funcs = &dce6_funcs;
221 rdev->audio.hdmi_funcs = &dce6_hdmi_funcs;
222 rdev->audio.dp_funcs = &dce6_dp_funcs;
223 } else if (ASIC_IS_DCE4(rdev)) {
224 rdev->audio.funcs = &dce4_funcs;
225 rdev->audio.hdmi_funcs = &dce4_hdmi_funcs;
226 rdev->audio.dp_funcs = &dce4_dp_funcs;
227 } else if (ASIC_IS_DCE32(rdev)) {
228 rdev->audio.funcs = &dce32_funcs;
229 rdev->audio.hdmi_funcs = &dce32_hdmi_funcs;
230 rdev->audio.dp_funcs = &dce32_dp_funcs;
231 } else {
232 rdev->audio.funcs = &r600_funcs;
233 rdev->audio.hdmi_funcs = &r600_hdmi_funcs;
234 rdev->audio.dp_funcs = NULL;
235 }
236 }
237
radeon_audio_chipset_supported(struct radeon_device * rdev)238 static int radeon_audio_chipset_supported(struct radeon_device *rdev)
239 {
240 return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev);
241 }
242
radeon_audio_init(struct radeon_device * rdev)243 int radeon_audio_init(struct radeon_device *rdev)
244 {
245 int i;
246
247 if (!radeon_audio || !radeon_audio_chipset_supported(rdev))
248 return 0;
249
250 rdev->audio.enabled = true;
251
252 if (ASIC_IS_DCE83(rdev)) /* KB: 2 streams, 3 endpoints */
253 rdev->audio.num_pins = 3;
254 else if (ASIC_IS_DCE81(rdev)) /* KV: 4 streams, 7 endpoints */
255 rdev->audio.num_pins = 7;
256 else if (ASIC_IS_DCE8(rdev)) /* BN/HW: 6 streams, 7 endpoints */
257 rdev->audio.num_pins = 7;
258 else if (ASIC_IS_DCE64(rdev)) /* OL: 2 streams, 2 endpoints */
259 rdev->audio.num_pins = 2;
260 else if (ASIC_IS_DCE61(rdev)) /* TN: 4 streams, 6 endpoints */
261 rdev->audio.num_pins = 6;
262 else if (ASIC_IS_DCE6(rdev)) /* SI: 6 streams, 6 endpoints */
263 rdev->audio.num_pins = 6;
264 else
265 rdev->audio.num_pins = 1;
266
267 for (i = 0; i < rdev->audio.num_pins; i++) {
268 rdev->audio.pin[i].channels = -1;
269 rdev->audio.pin[i].rate = -1;
270 rdev->audio.pin[i].bits_per_sample = -1;
271 rdev->audio.pin[i].status_bits = 0;
272 rdev->audio.pin[i].category_code = 0;
273 rdev->audio.pin[i].connected = false;
274 rdev->audio.pin[i].offset = pin_offsets[i];
275 rdev->audio.pin[i].id = i;
276 }
277
278 radeon_audio_interface_init(rdev);
279
280 /* disable audio. it will be set up later */
281 for (i = 0; i < rdev->audio.num_pins; i++)
282 radeon_audio_enable(rdev, &rdev->audio.pin[i], 0);
283
284 return 0;
285 }
286
radeon_audio_endpoint_rreg(struct radeon_device * rdev,u32 offset,u32 reg)287 u32 radeon_audio_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
288 {
289 if (rdev->audio.funcs->endpoint_rreg)
290 return rdev->audio.funcs->endpoint_rreg(rdev, offset, reg);
291
292 return 0;
293 }
294
radeon_audio_endpoint_wreg(struct radeon_device * rdev,u32 offset,u32 reg,u32 v)295 void radeon_audio_endpoint_wreg(struct radeon_device *rdev, u32 offset,
296 u32 reg, u32 v)
297 {
298 if (rdev->audio.funcs->endpoint_wreg)
299 rdev->audio.funcs->endpoint_wreg(rdev, offset, reg, v);
300 }
301
radeon_audio_write_sad_regs(struct drm_encoder * encoder)302 static void radeon_audio_write_sad_regs(struct drm_encoder *encoder)
303 {
304 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
305 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
306 struct cea_sad *sads;
307 int sad_count;
308
309 if (!connector)
310 return;
311
312 sad_count = drm_edid_to_sad(radeon_connector_edid(connector), &sads);
313 if (sad_count < 0)
314 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
315 if (sad_count <= 0)
316 return;
317 BUG_ON(!sads);
318
319 if (radeon_encoder->audio && radeon_encoder->audio->write_sad_regs)
320 radeon_encoder->audio->write_sad_regs(encoder, sads, sad_count);
321
322 kfree(sads);
323 }
324
radeon_audio_write_speaker_allocation(struct drm_encoder * encoder)325 static void radeon_audio_write_speaker_allocation(struct drm_encoder *encoder)
326 {
327 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
328 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
329 u8 *sadb = NULL;
330 int sad_count;
331
332 if (!connector)
333 return;
334
335 sad_count = drm_edid_to_speaker_allocation(radeon_connector_edid(connector),
336 &sadb);
337 if (sad_count < 0) {
338 DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n",
339 sad_count);
340 sad_count = 0;
341 }
342
343 if (radeon_encoder->audio && radeon_encoder->audio->write_speaker_allocation)
344 radeon_encoder->audio->write_speaker_allocation(encoder, sadb, sad_count);
345
346 kfree(sadb);
347 }
348
radeon_audio_write_latency_fields(struct drm_encoder * encoder,struct drm_display_mode * mode)349 static void radeon_audio_write_latency_fields(struct drm_encoder *encoder,
350 struct drm_display_mode *mode)
351 {
352 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
353 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
354
355 if (!connector)
356 return;
357
358 if (radeon_encoder->audio && radeon_encoder->audio->write_latency_fields)
359 radeon_encoder->audio->write_latency_fields(encoder, connector, mode);
360 }
361
radeon_audio_get_pin(struct drm_encoder * encoder)362 struct r600_audio_pin* radeon_audio_get_pin(struct drm_encoder *encoder)
363 {
364 struct radeon_device *rdev = encoder->dev->dev_private;
365 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
366
367 if (radeon_encoder->audio && radeon_encoder->audio->get_pin)
368 return radeon_encoder->audio->get_pin(rdev);
369
370 return NULL;
371 }
372
radeon_audio_select_pin(struct drm_encoder * encoder)373 static void radeon_audio_select_pin(struct drm_encoder *encoder)
374 {
375 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
376
377 if (radeon_encoder->audio && radeon_encoder->audio->select_pin)
378 radeon_encoder->audio->select_pin(encoder);
379 }
380
radeon_audio_detect(struct drm_connector * connector,struct drm_encoder * encoder,enum drm_connector_status status)381 void radeon_audio_detect(struct drm_connector *connector,
382 struct drm_encoder *encoder,
383 enum drm_connector_status status)
384 {
385 struct drm_device *dev = connector->dev;
386 struct radeon_device *rdev = dev->dev_private;
387 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
388 struct radeon_encoder_atom_dig *dig;
389
390 if (!radeon_audio_chipset_supported(rdev))
391 return;
392
393 if (!radeon_encoder_is_digital(encoder))
394 return;
395
396 dig = radeon_encoder->enc_priv;
397
398 if (status == connector_status_connected) {
399 if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
400 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
401
402 if (radeon_dp_getsinktype(radeon_connector) ==
403 CONNECTOR_OBJECT_ID_DISPLAYPORT)
404 radeon_encoder->audio = rdev->audio.dp_funcs;
405 else
406 radeon_encoder->audio = rdev->audio.hdmi_funcs;
407 } else {
408 radeon_encoder->audio = rdev->audio.hdmi_funcs;
409 }
410
411 if (drm_detect_monitor_audio(radeon_connector_edid(connector))) {
412 if (!dig->pin)
413 dig->pin = radeon_audio_get_pin(encoder);
414 radeon_audio_enable(rdev, dig->pin, 0xf);
415 } else {
416 radeon_audio_enable(rdev, dig->pin, 0);
417 dig->pin = NULL;
418 }
419 } else {
420 radeon_audio_enable(rdev, dig->pin, 0);
421 dig->pin = NULL;
422 }
423 }
424
radeon_audio_fini(struct radeon_device * rdev)425 void radeon_audio_fini(struct radeon_device *rdev)
426 {
427 int i;
428
429 if (!rdev->audio.enabled)
430 return;
431
432 for (i = 0; i < rdev->audio.num_pins; i++)
433 radeon_audio_enable(rdev, &rdev->audio.pin[i], 0);
434
435 rdev->audio.enabled = false;
436 }
437
radeon_audio_set_dto(struct drm_encoder * encoder,unsigned int clock)438 static void radeon_audio_set_dto(struct drm_encoder *encoder, unsigned int clock)
439 {
440 struct radeon_device *rdev = encoder->dev->dev_private;
441 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
442 struct radeon_crtc *crtc = to_radeon_crtc(encoder->crtc);
443
444 if (radeon_encoder->audio && radeon_encoder->audio->set_dto)
445 radeon_encoder->audio->set_dto(rdev, crtc, clock);
446 }
447
radeon_audio_set_avi_packet(struct drm_encoder * encoder,struct drm_display_mode * mode)448 static int radeon_audio_set_avi_packet(struct drm_encoder *encoder,
449 struct drm_display_mode *mode)
450 {
451 struct radeon_device *rdev = encoder->dev->dev_private;
452 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
453 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
454 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
455 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
456 struct hdmi_avi_infoframe frame;
457 int err;
458
459 if (!connector)
460 return -EINVAL;
461
462 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
463 if (err < 0) {
464 DRM_ERROR("failed to setup AVI infoframe: %d\n", err);
465 return err;
466 }
467
468 if (radeon_encoder->output_csc != RADEON_OUTPUT_CSC_BYPASS) {
469 drm_hdmi_avi_infoframe_quant_range(&frame, connector, mode,
470 radeon_encoder->output_csc == RADEON_OUTPUT_CSC_TVRGB ?
471 HDMI_QUANTIZATION_RANGE_LIMITED :
472 HDMI_QUANTIZATION_RANGE_FULL);
473 }
474
475 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
476 if (err < 0) {
477 DRM_ERROR("failed to pack AVI infoframe: %d\n", err);
478 return err;
479 }
480
481 if (dig && dig->afmt && radeon_encoder->audio &&
482 radeon_encoder->audio->set_avi_packet)
483 radeon_encoder->audio->set_avi_packet(rdev, dig->afmt->offset,
484 buffer, sizeof(buffer));
485
486 return 0;
487 }
488
489 /*
490 * calculate CTS and N values if they are not found in the table
491 */
radeon_audio_calc_cts(unsigned int clock,int * CTS,int * N,int freq)492 static void radeon_audio_calc_cts(unsigned int clock, int *CTS, int *N, int freq)
493 {
494 int n, cts;
495 unsigned long div, mul;
496
497 /* Safe, but overly large values */
498 n = 128 * freq;
499 cts = clock * 1000;
500
501 /* Smallest valid fraction */
502 div = gcd(n, cts);
503
504 n /= div;
505 cts /= div;
506
507 /*
508 * The optimal N is 128*freq/1000. Calculate the closest larger
509 * value that doesn't truncate any bits.
510 */
511 mul = ((128*freq/1000) + (n-1))/n;
512
513 n *= mul;
514 cts *= mul;
515
516 /* Check that we are in spec (not always possible) */
517 if (n < (128*freq/1500))
518 pr_warn("Calculated ACR N value is too small. You may experience audio problems.\n");
519 if (n > (128*freq/300))
520 pr_warn("Calculated ACR N value is too large. You may experience audio problems.\n");
521
522 *N = n;
523 *CTS = cts;
524
525 DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
526 *N, *CTS, freq);
527 }
528
radeon_audio_acr(unsigned int clock)529 static const struct radeon_hdmi_acr* radeon_audio_acr(unsigned int clock)
530 {
531 static struct radeon_hdmi_acr res;
532 u8 i;
533
534 static const struct radeon_hdmi_acr hdmi_predefined_acr[] = {
535 /* 32kHz 44.1kHz 48kHz */
536 /* Clock N CTS N CTS N CTS */
537 { 25175, 4096, 25175, 28224, 125875, 6144, 25175 }, /* 25,20/1.001 MHz */
538 { 25200, 4096, 25200, 6272, 28000, 6144, 25200 }, /* 25.20 MHz */
539 { 27000, 4096, 27000, 6272, 30000, 6144, 27000 }, /* 27.00 MHz */
540 { 27027, 4096, 27027, 6272, 30030, 6144, 27027 }, /* 27.00*1.001 MHz */
541 { 54000, 4096, 54000, 6272, 60000, 6144, 54000 }, /* 54.00 MHz */
542 { 54054, 4096, 54054, 6272, 60060, 6144, 54054 }, /* 54.00*1.001 MHz */
543 { 74176, 4096, 74176, 5733, 75335, 6144, 74176 }, /* 74.25/1.001 MHz */
544 { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */
545 { 148352, 4096, 148352, 5733, 150670, 6144, 148352 }, /* 148.50/1.001 MHz */
546 { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */
547 };
548
549 /* Precalculated values for common clocks */
550 for (i = 0; i < ARRAY_SIZE(hdmi_predefined_acr); i++)
551 if (hdmi_predefined_acr[i].clock == clock)
552 return &hdmi_predefined_acr[i];
553
554 /* And odd clocks get manually calculated */
555 radeon_audio_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
556 radeon_audio_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
557 radeon_audio_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
558
559 return &res;
560 }
561
562 /*
563 * update the N and CTS parameters for a given pixel clock rate
564 */
radeon_audio_update_acr(struct drm_encoder * encoder,unsigned int clock)565 static void radeon_audio_update_acr(struct drm_encoder *encoder, unsigned int clock)
566 {
567 const struct radeon_hdmi_acr *acr = radeon_audio_acr(clock);
568 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
569 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
570
571 if (!dig || !dig->afmt)
572 return;
573
574 if (radeon_encoder->audio && radeon_encoder->audio->update_acr)
575 radeon_encoder->audio->update_acr(encoder, dig->afmt->offset, acr);
576 }
577
radeon_audio_set_vbi_packet(struct drm_encoder * encoder)578 static void radeon_audio_set_vbi_packet(struct drm_encoder *encoder)
579 {
580 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
581 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
582
583 if (!dig || !dig->afmt)
584 return;
585
586 if (radeon_encoder->audio && radeon_encoder->audio->set_vbi_packet)
587 radeon_encoder->audio->set_vbi_packet(encoder, dig->afmt->offset);
588 }
589
radeon_hdmi_set_color_depth(struct drm_encoder * encoder)590 static void radeon_hdmi_set_color_depth(struct drm_encoder *encoder)
591 {
592 int bpc = 8;
593 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
594 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
595
596 if (!dig || !dig->afmt)
597 return;
598
599 if (encoder->crtc) {
600 struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
601 bpc = radeon_crtc->bpc;
602 }
603
604 if (radeon_encoder->audio && radeon_encoder->audio->set_color_depth)
605 radeon_encoder->audio->set_color_depth(encoder, dig->afmt->offset, bpc);
606 }
607
radeon_audio_set_audio_packet(struct drm_encoder * encoder)608 static void radeon_audio_set_audio_packet(struct drm_encoder *encoder)
609 {
610 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
611 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
612
613 if (!dig || !dig->afmt)
614 return;
615
616 if (radeon_encoder->audio && radeon_encoder->audio->set_audio_packet)
617 radeon_encoder->audio->set_audio_packet(encoder, dig->afmt->offset);
618 }
619
radeon_audio_set_mute(struct drm_encoder * encoder,bool mute)620 static void radeon_audio_set_mute(struct drm_encoder *encoder, bool mute)
621 {
622 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
623 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
624
625 if (!dig || !dig->afmt)
626 return;
627
628 if (radeon_encoder->audio && radeon_encoder->audio->set_mute)
629 radeon_encoder->audio->set_mute(encoder, dig->afmt->offset, mute);
630 }
631
632 /*
633 * update the info frames with the data from the current display mode
634 */
radeon_audio_hdmi_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode)635 static void radeon_audio_hdmi_mode_set(struct drm_encoder *encoder,
636 struct drm_display_mode *mode)
637 {
638 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
639 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
640 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
641
642 if (!dig || !dig->afmt)
643 return;
644
645 if (!connector)
646 return;
647
648 if (drm_detect_monitor_audio(radeon_connector_edid(connector))) {
649 radeon_audio_set_mute(encoder, true);
650
651 radeon_audio_write_speaker_allocation(encoder);
652 radeon_audio_write_sad_regs(encoder);
653 radeon_audio_write_latency_fields(encoder, mode);
654 radeon_audio_set_dto(encoder, mode->clock);
655 radeon_audio_set_vbi_packet(encoder);
656 radeon_hdmi_set_color_depth(encoder);
657 radeon_audio_update_acr(encoder, mode->clock);
658 radeon_audio_set_audio_packet(encoder);
659 radeon_audio_select_pin(encoder);
660
661 if (radeon_audio_set_avi_packet(encoder, mode) < 0)
662 return;
663
664 radeon_audio_set_mute(encoder, false);
665 } else {
666 radeon_hdmi_set_color_depth(encoder);
667
668 if (radeon_audio_set_avi_packet(encoder, mode) < 0)
669 return;
670 }
671 }
672
radeon_audio_dp_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode)673 static void radeon_audio_dp_mode_set(struct drm_encoder *encoder,
674 struct drm_display_mode *mode)
675 {
676 struct drm_device *dev = encoder->dev;
677 struct radeon_device *rdev = dev->dev_private;
678 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
679 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
680 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
681
682 if (!dig || !dig->afmt)
683 return;
684
685 if (!connector)
686 return;
687
688 if (drm_detect_monitor_audio(radeon_connector_edid(connector))) {
689 radeon_audio_write_speaker_allocation(encoder);
690 radeon_audio_write_sad_regs(encoder);
691 radeon_audio_write_latency_fields(encoder, mode);
692 radeon_audio_set_dto(encoder, rdev->clock.vco_freq * 10);
693 radeon_audio_set_audio_packet(encoder);
694 radeon_audio_select_pin(encoder);
695
696 if (radeon_audio_set_avi_packet(encoder, mode) < 0)
697 return;
698 }
699 }
700
radeon_audio_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode)701 void radeon_audio_mode_set(struct drm_encoder *encoder,
702 struct drm_display_mode *mode)
703 {
704 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
705
706 if (radeon_encoder->audio && radeon_encoder->audio->mode_set)
707 radeon_encoder->audio->mode_set(encoder, mode);
708 }
709
radeon_audio_dpms(struct drm_encoder * encoder,int mode)710 void radeon_audio_dpms(struct drm_encoder *encoder, int mode)
711 {
712 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
713
714 if (radeon_encoder->audio && radeon_encoder->audio->dpms)
715 radeon_encoder->audio->dpms(encoder, mode == DRM_MODE_DPMS_ON);
716 }
717
radeon_audio_decode_dfs_div(unsigned int div)718 unsigned int radeon_audio_decode_dfs_div(unsigned int div)
719 {
720 if (div >= 8 && div < 64)
721 return (div - 8) * 25 + 200;
722 else if (div >= 64 && div < 96)
723 return (div - 64) * 50 + 1600;
724 else if (div >= 96 && div < 128)
725 return (div - 96) * 100 + 3200;
726 else
727 return 0;
728 }
729
730 /*
731 * Audio component support
732 */
radeon_audio_component_notify(struct radeon_device * rdev,int port)733 static void radeon_audio_component_notify(struct radeon_device *rdev, int port)
734 {
735 struct drm_audio_component *acomp;
736
737 mutex_lock(&rdev->audio.component_mutex);
738 acomp = rdev->audio.component;
739 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
740 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
741 port, -1);
742 mutex_unlock(&rdev->audio.component_mutex);
743 }
744
745 #ifdef notyet
746
radeon_audio_component_get_eld(struct device * kdev,int port,int pipe,bool * enabled,unsigned char * buf,int max_bytes)747 static int radeon_audio_component_get_eld(struct device *kdev, int port,
748 int pipe, bool *enabled,
749 unsigned char *buf, int max_bytes)
750 {
751 struct drm_device *dev = dev_get_drvdata(kdev);
752 struct radeon_device *rdev = dev->dev_private;
753 struct drm_encoder *encoder;
754 struct radeon_encoder *radeon_encoder;
755 struct radeon_encoder_atom_dig *dig;
756 struct drm_connector *connector;
757 int ret = 0;
758
759 *enabled = false;
760 if (!rdev->audio.enabled || !rdev->mode_info.mode_config_initialized)
761 return 0;
762
763 list_for_each_entry(encoder, &rdev->ddev->mode_config.encoder_list, head) {
764 if (!radeon_encoder_is_digital(encoder))
765 continue;
766 radeon_encoder = to_radeon_encoder(encoder);
767 dig = radeon_encoder->enc_priv;
768 if (!dig->pin || dig->pin->id != port)
769 continue;
770 connector = radeon_get_connector_for_encoder(encoder);
771 if (!connector)
772 continue;
773 *enabled = true;
774 ret = drm_eld_size(connector->eld);
775 memcpy(buf, connector->eld, min(max_bytes, ret));
776 break;
777 }
778
779 return ret;
780 }
781
782 static const struct drm_audio_component_ops radeon_audio_component_ops = {
783 .get_eld = radeon_audio_component_get_eld,
784 };
785
radeon_audio_component_bind(struct device * kdev,struct device * hda_kdev,void * data)786 static int radeon_audio_component_bind(struct device *kdev,
787 struct device *hda_kdev, void *data)
788 {
789 struct drm_device *dev = dev_get_drvdata(kdev);
790 struct radeon_device *rdev = dev->dev_private;
791 struct drm_audio_component *acomp = data;
792
793 if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
794 return -ENOMEM;
795
796 mutex_lock(&rdev->audio.component_mutex);
797 acomp->ops = &radeon_audio_component_ops;
798 acomp->dev = kdev;
799 rdev->audio.component = acomp;
800 mutex_unlock(&rdev->audio.component_mutex);
801
802 return 0;
803 }
804
radeon_audio_component_unbind(struct device * kdev,struct device * hda_kdev,void * data)805 static void radeon_audio_component_unbind(struct device *kdev,
806 struct device *hda_kdev, void *data)
807 {
808 struct drm_device *dev = dev_get_drvdata(kdev);
809 struct radeon_device *rdev = dev->dev_private;
810 struct drm_audio_component *acomp = data;
811
812 device_link_remove(hda_kdev, kdev);
813
814 mutex_lock(&rdev->audio.component_mutex);
815 rdev->audio.component = NULL;
816 acomp->ops = NULL;
817 acomp->dev = NULL;
818 mutex_unlock(&rdev->audio.component_mutex);
819 }
820
821 static const struct component_ops radeon_audio_component_bind_ops = {
822 .bind = radeon_audio_component_bind,
823 .unbind = radeon_audio_component_unbind,
824 };
825
826 #endif
827
radeon_audio_component_init(struct radeon_device * rdev)828 void radeon_audio_component_init(struct radeon_device *rdev)
829 {
830 if (rdev->audio.component_registered ||
831 !radeon_audio || !radeon_audio_chipset_supported(rdev))
832 return;
833 #ifdef notyet
834 if (!component_add(rdev->dev, &radeon_audio_component_bind_ops))
835 rdev->audio.component_registered = true;
836 #else
837 STUB();
838 #endif
839 }
840
radeon_audio_component_fini(struct radeon_device * rdev)841 void radeon_audio_component_fini(struct radeon_device *rdev)
842 {
843 if (rdev->audio.component_registered) {
844 #ifdef notyet
845 component_del(rdev->dev, &radeon_audio_component_bind_ops);
846 rdev->audio.component_registered = false;
847 #else
848 STUB();
849 #endif
850 }
851 }
852