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