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_to_drm(rdev)->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_connector *radeon_connector = to_radeon_connector(connector); 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, &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_connector *radeon_connector = to_radeon_connector(connector); 331 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); 332 u8 *sadb = NULL; 333 int sad_count; 334 335 if (!connector) 336 return; 337 338 sad_count = drm_edid_to_speaker_allocation(radeon_connector->edid, &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 (connector->display_info.has_audio) { 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 (connector->display_info.has_audio) { 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 (connector->display_info.has_audio) { 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_to_drm(rdev)->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