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