1 // SPDX-License-Identifier: MIT 2 3 #include <drm/drm_atomic.h> 4 #include <drm/drm_connector.h> 5 #include <drm/drm_edid.h> 6 #include <drm/drm_print.h> 7 8 #include <drm/display/drm_hdmi_audio_helper.h> 9 #include <drm/display/drm_hdmi_helper.h> 10 #include <drm/display/drm_hdmi_state_helper.h> 11 12 /** 13 * DOC: hdmi helpers 14 * 15 * These functions contain an implementation of the HDMI specification 16 * in the form of KMS helpers. 17 * 18 * It contains TMDS character rate computation, automatic selection of 19 * output formats, infoframes generation, etc. 20 * 21 * Infoframes Compliance 22 * ~~~~~~~~~~~~~~~~~~~~~ 23 * 24 * Drivers using the helpers will expose the various infoframes 25 * generated according to the HDMI specification in debugfs. 26 * 27 * Compliance can then be tested using ``edid-decode`` from the ``v4l-utils`` project 28 * (https://git.linuxtv.org/v4l-utils.git/). A sample run would look like: 29 * 30 * .. code-block:: bash 31 * 32 * # edid-decode \ 33 * -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/audio \ 34 * -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/avi \ 35 * -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdmi \ 36 * -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdr_drm \ 37 * -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/spd \ 38 * /sys/class/drm/card1-HDMI-A-1/edid \ 39 * -c 40 * 41 * edid-decode (hex): 42 * 43 * 00 ff ff ff ff ff ff 00 1e 6d f4 5b 1e ef 06 00 44 * 07 20 01 03 80 2f 34 78 ea 24 05 af 4f 42 ab 25 45 * 0f 50 54 21 08 00 d1 c0 61 40 45 40 01 01 01 01 46 * 01 01 01 01 01 01 98 d0 00 40 a1 40 d4 b0 30 20 47 * 3a 00 d1 0b 12 00 00 1a 00 00 00 fd 00 3b 3d 1e 48 * b2 31 00 0a 20 20 20 20 20 20 00 00 00 fc 00 4c 49 * 47 20 53 44 51 48 44 0a 20 20 20 20 00 00 00 ff 50 * 00 32 30 37 4e 54 52 4c 44 43 34 33 30 0a 01 46 51 * 52 * 02 03 42 72 23 09 07 07 4d 01 03 04 90 12 13 1f 53 * 22 5d 5e 5f 60 61 83 01 00 00 6d 03 0c 00 10 00 54 * b8 3c 20 00 60 01 02 03 67 d8 5d c4 01 78 80 03 55 * e3 0f 00 18 e2 00 6a e3 05 c0 00 e6 06 05 01 52 56 * 52 51 11 5d 00 a0 a0 40 29 b0 30 20 3a 00 d1 0b 57 * 12 00 00 1a 00 00 00 00 00 00 00 00 00 00 00 00 58 * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 59 * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c3 60 * 61 * ---------------- 62 * 63 * Block 0, Base EDID: 64 * EDID Structure Version & Revision: 1.3 65 * Vendor & Product Identification: 66 * Manufacturer: GSM 67 * Model: 23540 68 * Serial Number: 454430 (0x0006ef1e) 69 * Made in: week 7 of 2022 70 * Basic Display Parameters & Features: 71 * Digital display 72 * Maximum image size: 47 cm x 52 cm 73 * Gamma: 2.20 74 * DPMS levels: Standby Suspend Off 75 * RGB color display 76 * First detailed timing is the preferred timing 77 * Color Characteristics: 78 * Red : 0.6835, 0.3105 79 * Green: 0.2587, 0.6679 80 * Blue : 0.1445, 0.0585 81 * White: 0.3134, 0.3291 82 * Established Timings I & II: 83 * DMT 0x04: 640x480 59.940476 Hz 4:3 31.469 kHz 25.175000 MHz 84 * DMT 0x09: 800x600 60.316541 Hz 4:3 37.879 kHz 40.000000 MHz 85 * DMT 0x10: 1024x768 60.003840 Hz 4:3 48.363 kHz 65.000000 MHz 86 * Standard Timings: 87 * DMT 0x52: 1920x1080 60.000000 Hz 16:9 67.500 kHz 148.500000 MHz 88 * DMT 0x10: 1024x768 60.003840 Hz 4:3 48.363 kHz 65.000000 MHz 89 * DMT 0x09: 800x600 60.316541 Hz 4:3 37.879 kHz 40.000000 MHz 90 * Detailed Timing Descriptors: 91 * DTD 1: 2560x2880 59.966580 Hz 8:9 185.417 kHz 534.000000 MHz (465 mm x 523 mm) 92 * Hfront 48 Hsync 32 Hback 240 Hpol P 93 * Vfront 3 Vsync 10 Vback 199 Vpol N 94 * Display Range Limits: 95 * Monitor ranges (GTF): 59-61 Hz V, 30-178 kHz H, max dotclock 490 MHz 96 * Display Product Name: 'LG SDQHD' 97 * Display Product Serial Number: '207NTRLDC430' 98 * Extension blocks: 1 99 * Checksum: 0x46 100 * 101 * ---------------- 102 * 103 * Block 1, CTA-861 Extension Block: 104 * Revision: 3 105 * Basic audio support 106 * Supports YCbCr 4:4:4 107 * Supports YCbCr 4:2:2 108 * Native detailed modes: 2 109 * Audio Data Block: 110 * Linear PCM: 111 * Max channels: 2 112 * Supported sample rates (kHz): 48 44.1 32 113 * Supported sample sizes (bits): 24 20 16 114 * Video Data Block: 115 * VIC 1: 640x480 59.940476 Hz 4:3 31.469 kHz 25.175000 MHz 116 * VIC 3: 720x480 59.940060 Hz 16:9 31.469 kHz 27.000000 MHz 117 * VIC 4: 1280x720 60.000000 Hz 16:9 45.000 kHz 74.250000 MHz 118 * VIC 16: 1920x1080 60.000000 Hz 16:9 67.500 kHz 148.500000 MHz (native) 119 * VIC 18: 720x576 50.000000 Hz 16:9 31.250 kHz 27.000000 MHz 120 * VIC 19: 1280x720 50.000000 Hz 16:9 37.500 kHz 74.250000 MHz 121 * VIC 31: 1920x1080 50.000000 Hz 16:9 56.250 kHz 148.500000 MHz 122 * VIC 34: 1920x1080 30.000000 Hz 16:9 33.750 kHz 74.250000 MHz 123 * VIC 93: 3840x2160 24.000000 Hz 16:9 54.000 kHz 297.000000 MHz 124 * VIC 94: 3840x2160 25.000000 Hz 16:9 56.250 kHz 297.000000 MHz 125 * VIC 95: 3840x2160 30.000000 Hz 16:9 67.500 kHz 297.000000 MHz 126 * VIC 96: 3840x2160 50.000000 Hz 16:9 112.500 kHz 594.000000 MHz 127 * VIC 97: 3840x2160 60.000000 Hz 16:9 135.000 kHz 594.000000 MHz 128 * Speaker Allocation Data Block: 129 * FL/FR - Front Left/Right 130 * Vendor-Specific Data Block (HDMI), OUI 00-0C-03: 131 * Source physical address: 1.0.0.0 132 * Supports_AI 133 * DC_36bit 134 * DC_30bit 135 * DC_Y444 136 * Maximum TMDS clock: 300 MHz 137 * Extended HDMI video details: 138 * HDMI VICs: 139 * HDMI VIC 1: 3840x2160 30.000000 Hz 16:9 67.500 kHz 297.000000 MHz 140 * HDMI VIC 2: 3840x2160 25.000000 Hz 16:9 56.250 kHz 297.000000 MHz 141 * HDMI VIC 3: 3840x2160 24.000000 Hz 16:9 54.000 kHz 297.000000 MHz 142 * Vendor-Specific Data Block (HDMI Forum), OUI C4-5D-D8: 143 * Version: 1 144 * Maximum TMDS Character Rate: 600 MHz 145 * SCDC Present 146 * Supports 12-bits/component Deep Color 4:2:0 Pixel Encoding 147 * Supports 10-bits/component Deep Color 4:2:0 Pixel Encoding 148 * YCbCr 4:2:0 Capability Map Data Block: 149 * VIC 96: 3840x2160 50.000000 Hz 16:9 112.500 kHz 594.000000 MHz 150 * VIC 97: 3840x2160 60.000000 Hz 16:9 135.000 kHz 594.000000 MHz 151 * Video Capability Data Block: 152 * YCbCr quantization: No Data 153 * RGB quantization: Selectable (via AVI Q) 154 * PT scan behavior: Always Underscanned 155 * IT scan behavior: Always Underscanned 156 * CE scan behavior: Always Underscanned 157 * Colorimetry Data Block: 158 * BT2020YCC 159 * BT2020RGB 160 * HDR Static Metadata Data Block: 161 * Electro optical transfer functions: 162 * Traditional gamma - SDR luminance range 163 * SMPTE ST2084 164 * Supported static metadata descriptors: 165 * Static metadata type 1 166 * Desired content max luminance: 82 (295.365 cd/m^2) 167 * Desired content max frame-average luminance: 82 (295.365 cd/m^2) 168 * Desired content min luminance: 81 (0.298 cd/m^2) 169 * Detailed Timing Descriptors: 170 * DTD 2: 2560x2880 29.986961 Hz 8:9 87.592 kHz 238.250000 MHz (465 mm x 523 mm) 171 * Hfront 48 Hsync 32 Hback 80 Hpol P 172 * Vfront 3 Vsync 10 Vback 28 Vpol N 173 * Checksum: 0xc3 Unused space in Extension Block: 43 bytes 174 * 175 * ---------------- 176 * 177 * edid-decode 1.29.0-5346 178 * edid-decode SHA: c363e9aa6d70 2025-03-11 11:41:18 179 * 180 * Warnings: 181 * 182 * Block 1, CTA-861 Extension Block: 183 * IT Video Formats are overscanned by default, but normally this should be underscanned. 184 * Video Data Block: VIC 1 and the first DTD are not identical. Is this intended? 185 * Video Data Block: All VICs are in ascending order, and the first (preferred) VIC <= 4, is that intended? 186 * Video Capability Data Block: Set Selectable YCbCr Quantization to avoid interop issues. 187 * Video Capability Data Block: S_PT is equal to S_IT and S_CE, so should be set to 0 instead. 188 * Colorimetry Data Block: Set the sRGB colorimetry bit to avoid interop issues. 189 * Display Product Serial Number is set, so the Serial Number in the Base EDID should be 0. 190 * EDID: 191 * Base EDID: Some timings are out of range of the Monitor Ranges: 192 * Vertical Freq: 24.000 - 60.317 Hz (Monitor: 59.000 - 61.000 Hz) 193 * Horizontal Freq: 31.250 - 185.416 kHz (Monitor: 30.000 - 178.000 kHz) 194 * Maximum Clock: 594.000 MHz (Monitor: 490.000 MHz) 195 * 196 * Failures: 197 * 198 * Block 1, CTA-861 Extension Block: 199 * Video Capability Data Block: IT video formats are always underscanned, but bit 7 of Byte 3 of the CTA-861 Extension header is set to overscanned. 200 * EDID: 201 * CTA-861: Native progressive timings are a mix of several resolutions. 202 * 203 * EDID conformity: FAIL 204 * 205 * ================ 206 * 207 * InfoFrame of '/sys/kernel/debug/dri/1/HDMI-A-1/infoframes/audio' was empty. 208 * 209 * ================ 210 * 211 * edid-decode InfoFrame (hex): 212 * 213 * 82 02 0d 31 12 28 04 00 00 00 00 00 00 00 00 00 214 * 00 215 * 216 * ---------------- 217 * 218 * HDMI InfoFrame Checksum: 0x31 219 * 220 * AVI InfoFrame 221 * Version: 2 222 * Length: 13 223 * Y: Color Component Sample Format: RGB 224 * A: Active Format Information Present: Yes 225 * B: Bar Data Present: Bar Data not present 226 * S: Scan Information: Composed for an underscanned display 227 * C: Colorimetry: No Data 228 * M: Picture Aspect Ratio: 16:9 229 * R: Active Portion Aspect Ratio: 8 230 * ITC: IT Content: No Data 231 * EC: Extended Colorimetry: xvYCC601 232 * Q: RGB Quantization Range: Limited Range 233 * SC: Non-Uniform Picture Scaling: No Known non-uniform scaling 234 * YQ: YCC Quantization Range: Limited Range 235 * CN: IT Content Type: Graphics 236 * PR: Pixel Data Repetition Count: 0 237 * Line Number of End of Top Bar: 0 238 * Line Number of Start of Bottom Bar: 0 239 * Pixel Number of End of Left Bar: 0 240 * Pixel Number of Start of Right Bar: 0 241 * 242 * ---------------- 243 * 244 * AVI InfoFrame conformity: PASS 245 * 246 * ================ 247 * 248 * edid-decode InfoFrame (hex): 249 * 250 * 81 01 05 49 03 0c 00 20 01 251 * 252 * ---------------- 253 * 254 * HDMI InfoFrame Checksum: 0x49 255 * 256 * Vendor-Specific InfoFrame (HDMI), OUI 00-0C-03 257 * Version: 1 258 * Length: 5 259 * HDMI Video Format: HDMI_VIC is present 260 * HDMI VIC 1: 3840x2160 30.000000 Hz 16:9 67.500 kHz 297.000000 MHz 261 * 262 * ---------------- 263 * 264 * Vendor-Specific InfoFrame (HDMI), OUI 00-0C-03 conformity: PASS 265 * 266 * ================ 267 * 268 * InfoFrame of '/sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdr_drm' was empty. 269 * 270 * ================ 271 * 272 * edid-decode InfoFrame (hex): 273 * 274 * 83 01 19 93 42 72 6f 61 64 63 6f 6d 56 69 64 65 275 * 6f 63 6f 72 65 00 00 00 00 00 00 00 09 276 * 277 * ---------------- 278 * 279 * HDMI InfoFrame Checksum: 0x93 280 * 281 * Source Product Description InfoFrame 282 * Version: 1 283 * Length: 25 284 * Vendor Name: 'Broadcom' 285 * Product Description: 'Videocore' 286 * Source Information: PC general 287 * 288 * ---------------- 289 * 290 * Source Product Description InfoFrame conformity: PASS 291 * 292 * Testing 293 * ~~~~~~~ 294 * 295 * The helpers have unit testing and can be tested using kunit with: 296 * 297 * .. code-block:: bash 298 * 299 * $ ./tools/testing/kunit/kunit.py run \ 300 * --kunitconfig=drivers/gpu/drm/tests \ 301 * drm_atomic_helper_connector_hdmi_* 302 */ 303 304 /** 305 * __drm_atomic_helper_connector_hdmi_reset() - Initializes all HDMI @drm_connector_state resources 306 * @connector: DRM connector 307 * @new_conn_state: connector state to reset 308 * 309 * Initializes all HDMI resources from a @drm_connector_state without 310 * actually allocating it. This is useful for HDMI drivers, in 311 * combination with __drm_atomic_helper_connector_reset() or 312 * drm_atomic_helper_connector_reset(). 313 */ 314 void __drm_atomic_helper_connector_hdmi_reset(struct drm_connector *connector, 315 struct drm_connector_state *new_conn_state) 316 { 317 unsigned int max_bpc = connector->max_bpc; 318 319 new_conn_state->max_bpc = max_bpc; 320 new_conn_state->max_requested_bpc = max_bpc; 321 new_conn_state->hdmi.broadcast_rgb = DRM_HDMI_BROADCAST_RGB_AUTO; 322 } 323 EXPORT_SYMBOL(__drm_atomic_helper_connector_hdmi_reset); 324 325 static const struct drm_display_mode * 326 connector_state_get_mode(const struct drm_connector_state *conn_state) 327 { 328 struct drm_atomic_state *state; 329 struct drm_crtc_state *crtc_state; 330 struct drm_crtc *crtc; 331 332 state = conn_state->state; 333 if (!state) 334 return NULL; 335 336 crtc = conn_state->crtc; 337 if (!crtc) 338 return NULL; 339 340 crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 341 if (!crtc_state) 342 return NULL; 343 344 return &crtc_state->mode; 345 } 346 347 static bool hdmi_is_limited_range(const struct drm_connector *connector, 348 const struct drm_connector_state *conn_state) 349 { 350 const struct drm_display_info *info = &connector->display_info; 351 const struct drm_display_mode *mode = 352 connector_state_get_mode(conn_state); 353 354 /* 355 * The Broadcast RGB property only applies to RGB format, and 356 * i915 just assumes limited range for YCbCr output, so let's 357 * just do the same. 358 */ 359 if (conn_state->hdmi.output_format != HDMI_COLORSPACE_RGB) 360 return true; 361 362 if (conn_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_FULL) 363 return false; 364 365 if (conn_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_LIMITED) 366 return true; 367 368 if (!info->is_hdmi) 369 return false; 370 371 return drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED; 372 } 373 374 static bool 375 sink_supports_format_bpc(const struct drm_connector *connector, 376 const struct drm_display_info *info, 377 const struct drm_display_mode *mode, 378 unsigned int format, unsigned int bpc) 379 { 380 struct drm_device *dev = connector->dev; 381 u8 vic = drm_match_cea_mode(mode); 382 383 /* 384 * CTA-861-F, section 5.4 - Color Coding & Quantization states 385 * that the bpc must be 8, 10, 12 or 16 except for the default 386 * 640x480 VIC1 where the value must be 8. 387 * 388 * The definition of default here is ambiguous but the spec 389 * refers to VIC1 being the default timing in several occasions 390 * so our understanding is that for the default timing (ie, 391 * VIC1), the bpc must be 8. 392 */ 393 if (vic == 1 && bpc != 8) { 394 drm_dbg_kms(dev, "VIC1 requires a bpc of 8, got %u\n", bpc); 395 return false; 396 } 397 398 if (!info->is_hdmi && 399 (format != HDMI_COLORSPACE_RGB || bpc != 8)) { 400 drm_dbg_kms(dev, "DVI Monitors require an RGB output at 8 bpc\n"); 401 return false; 402 } 403 404 if (!(connector->hdmi.supported_formats & BIT(format))) { 405 drm_dbg_kms(dev, "%s format unsupported by the connector.\n", 406 drm_hdmi_connector_get_output_format_name(format)); 407 return false; 408 } 409 410 switch (format) { 411 case HDMI_COLORSPACE_RGB: 412 drm_dbg_kms(dev, "RGB Format, checking the constraints.\n"); 413 414 /* 415 * In some cases, like when the EDID readout fails, or 416 * is not an HDMI compliant EDID for some reason, the 417 * color_formats field will be blank and not report any 418 * format supported. In such a case, assume that RGB is 419 * supported so we can keep things going and light up 420 * the display. 421 */ 422 if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444)) 423 drm_warn(dev, "HDMI Sink doesn't support RGB, something's wrong.\n"); 424 425 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) { 426 drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n"); 427 return false; 428 } 429 430 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) { 431 drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n"); 432 return false; 433 } 434 435 drm_dbg_kms(dev, "RGB format supported in that configuration.\n"); 436 437 return true; 438 439 case HDMI_COLORSPACE_YUV420: 440 /* TODO: YUV420 is unsupported at the moment. */ 441 drm_dbg_kms(dev, "YUV420 format isn't supported yet.\n"); 442 return false; 443 444 case HDMI_COLORSPACE_YUV422: 445 drm_dbg_kms(dev, "YUV422 format, checking the constraints.\n"); 446 447 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) { 448 drm_dbg_kms(dev, "Sink doesn't support YUV422.\n"); 449 return false; 450 } 451 452 if (bpc > 12) { 453 drm_dbg_kms(dev, "YUV422 only supports 12 bpc or lower.\n"); 454 return false; 455 } 456 457 /* 458 * HDMI Spec 1.3 - Section 6.5 Pixel Encodings and Color Depth 459 * states that Deep Color is not relevant for YUV422 so we 460 * don't need to check the Deep Color bits in the EDIDs here. 461 */ 462 463 drm_dbg_kms(dev, "YUV422 format supported in that configuration.\n"); 464 465 return true; 466 467 case HDMI_COLORSPACE_YUV444: 468 drm_dbg_kms(dev, "YUV444 format, checking the constraints.\n"); 469 470 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) { 471 drm_dbg_kms(dev, "Sink doesn't support YUV444.\n"); 472 return false; 473 } 474 475 if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) { 476 drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n"); 477 return false; 478 } 479 480 if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) { 481 drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n"); 482 return false; 483 } 484 485 drm_dbg_kms(dev, "YUV444 format supported in that configuration.\n"); 486 487 return true; 488 } 489 490 drm_dbg_kms(dev, "Unsupported pixel format.\n"); 491 return false; 492 } 493 494 static enum drm_mode_status 495 hdmi_clock_valid(const struct drm_connector *connector, 496 const struct drm_display_mode *mode, 497 unsigned long long clock) 498 { 499 const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs; 500 const struct drm_display_info *info = &connector->display_info; 501 502 if (info->max_tmds_clock && clock > info->max_tmds_clock * 1000) 503 return MODE_CLOCK_HIGH; 504 505 if (funcs && funcs->tmds_char_rate_valid) { 506 enum drm_mode_status status; 507 508 status = funcs->tmds_char_rate_valid(connector, mode, clock); 509 if (status != MODE_OK) 510 return status; 511 } 512 513 return MODE_OK; 514 } 515 516 static int 517 hdmi_compute_clock(const struct drm_connector *connector, 518 struct drm_connector_state *conn_state, 519 const struct drm_display_mode *mode, 520 unsigned int bpc, enum hdmi_colorspace fmt) 521 { 522 enum drm_mode_status status; 523 unsigned long long clock; 524 525 clock = drm_hdmi_compute_mode_clock(mode, bpc, fmt); 526 if (!clock) 527 return -EINVAL; 528 529 status = hdmi_clock_valid(connector, mode, clock); 530 if (status != MODE_OK) 531 return -EINVAL; 532 533 conn_state->hdmi.tmds_char_rate = clock; 534 535 return 0; 536 } 537 538 static bool 539 hdmi_try_format_bpc(const struct drm_connector *connector, 540 struct drm_connector_state *conn_state, 541 const struct drm_display_mode *mode, 542 unsigned int bpc, enum hdmi_colorspace fmt) 543 { 544 const struct drm_display_info *info = &connector->display_info; 545 struct drm_device *dev = connector->dev; 546 int ret; 547 548 drm_dbg_kms(dev, "Trying %s output format\n", 549 drm_hdmi_connector_get_output_format_name(fmt)); 550 551 if (!sink_supports_format_bpc(connector, info, mode, fmt, bpc)) { 552 drm_dbg_kms(dev, "%s output format not supported with %u bpc\n", 553 drm_hdmi_connector_get_output_format_name(fmt), 554 bpc); 555 return false; 556 } 557 558 ret = hdmi_compute_clock(connector, conn_state, mode, bpc, fmt); 559 if (ret) { 560 drm_dbg_kms(dev, "Couldn't compute clock for %s output format and %u bpc\n", 561 drm_hdmi_connector_get_output_format_name(fmt), 562 bpc); 563 return false; 564 } 565 566 drm_dbg_kms(dev, "%s output format supported with %u (TMDS char rate: %llu Hz)\n", 567 drm_hdmi_connector_get_output_format_name(fmt), 568 bpc, conn_state->hdmi.tmds_char_rate); 569 570 return true; 571 } 572 573 static int 574 hdmi_compute_format(const struct drm_connector *connector, 575 struct drm_connector_state *conn_state, 576 const struct drm_display_mode *mode, 577 unsigned int bpc) 578 { 579 struct drm_device *dev = connector->dev; 580 581 /* 582 * TODO: Add support for YCbCr420 output for HDMI 2.0 capable 583 * devices, for modes that only support YCbCr420. 584 */ 585 if (hdmi_try_format_bpc(connector, conn_state, mode, bpc, HDMI_COLORSPACE_RGB)) { 586 conn_state->hdmi.output_format = HDMI_COLORSPACE_RGB; 587 return 0; 588 } 589 590 drm_dbg_kms(dev, "Failed. No Format Supported for that bpc count.\n"); 591 592 return -EINVAL; 593 } 594 595 static int 596 hdmi_compute_config(const struct drm_connector *connector, 597 struct drm_connector_state *conn_state, 598 const struct drm_display_mode *mode) 599 { 600 struct drm_device *dev = connector->dev; 601 unsigned int max_bpc = clamp_t(unsigned int, 602 conn_state->max_bpc, 603 8, connector->max_bpc); 604 unsigned int bpc; 605 int ret; 606 607 for (bpc = max_bpc; bpc >= 8; bpc -= 2) { 608 drm_dbg_kms(dev, "Trying with a %d bpc output\n", bpc); 609 610 ret = hdmi_compute_format(connector, conn_state, mode, bpc); 611 if (ret) 612 continue; 613 614 conn_state->hdmi.output_bpc = bpc; 615 616 drm_dbg_kms(dev, 617 "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n", 618 mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode), 619 conn_state->hdmi.output_bpc, 620 drm_hdmi_connector_get_output_format_name(conn_state->hdmi.output_format), 621 conn_state->hdmi.tmds_char_rate); 622 623 return 0; 624 } 625 626 return -EINVAL; 627 } 628 629 static int hdmi_generate_avi_infoframe(const struct drm_connector *connector, 630 struct drm_connector_state *conn_state) 631 { 632 const struct drm_display_mode *mode = 633 connector_state_get_mode(conn_state); 634 struct drm_connector_hdmi_infoframe *infoframe = 635 &conn_state->hdmi.infoframes.avi; 636 struct hdmi_avi_infoframe *frame = 637 &infoframe->data.avi; 638 bool is_limited_range = conn_state->hdmi.is_limited_range; 639 enum hdmi_quantization_range rgb_quant_range = 640 is_limited_range ? HDMI_QUANTIZATION_RANGE_LIMITED : HDMI_QUANTIZATION_RANGE_FULL; 641 int ret; 642 643 infoframe->set = false; 644 645 ret = drm_hdmi_avi_infoframe_from_display_mode(frame, connector, mode); 646 if (ret) 647 return ret; 648 649 frame->colorspace = conn_state->hdmi.output_format; 650 651 /* 652 * FIXME: drm_hdmi_avi_infoframe_quant_range() doesn't handle 653 * YUV formats at all at the moment, so if we ever support YUV 654 * formats this needs to be revised. 655 */ 656 drm_hdmi_avi_infoframe_quant_range(frame, connector, mode, rgb_quant_range); 657 drm_hdmi_avi_infoframe_colorimetry(frame, conn_state); 658 drm_hdmi_avi_infoframe_bars(frame, conn_state); 659 660 infoframe->set = true; 661 662 return 0; 663 } 664 665 static int hdmi_generate_spd_infoframe(const struct drm_connector *connector, 666 struct drm_connector_state *conn_state) 667 { 668 struct drm_connector_hdmi_infoframe *infoframe = 669 &conn_state->hdmi.infoframes.spd; 670 struct hdmi_spd_infoframe *frame = 671 &infoframe->data.spd; 672 int ret; 673 674 infoframe->set = false; 675 676 ret = hdmi_spd_infoframe_init(frame, 677 connector->hdmi.vendor, 678 connector->hdmi.product); 679 if (ret) 680 return ret; 681 682 frame->sdi = HDMI_SPD_SDI_PC; 683 684 infoframe->set = true; 685 686 return 0; 687 } 688 689 static int hdmi_generate_hdr_infoframe(const struct drm_connector *connector, 690 struct drm_connector_state *conn_state) 691 { 692 struct drm_connector_hdmi_infoframe *infoframe = 693 &conn_state->hdmi.infoframes.hdr_drm; 694 struct hdmi_drm_infoframe *frame = 695 &infoframe->data.drm; 696 int ret; 697 698 infoframe->set = false; 699 700 if (connector->max_bpc < 10) 701 return 0; 702 703 if (!conn_state->hdr_output_metadata) 704 return 0; 705 706 ret = drm_hdmi_infoframe_set_hdr_metadata(frame, conn_state); 707 if (ret) 708 return ret; 709 710 infoframe->set = true; 711 712 return 0; 713 } 714 715 static int hdmi_generate_hdmi_vendor_infoframe(const struct drm_connector *connector, 716 struct drm_connector_state *conn_state) 717 { 718 const struct drm_display_info *info = &connector->display_info; 719 const struct drm_display_mode *mode = 720 connector_state_get_mode(conn_state); 721 struct drm_connector_hdmi_infoframe *infoframe = 722 &conn_state->hdmi.infoframes.hdmi; 723 struct hdmi_vendor_infoframe *frame = 724 &infoframe->data.vendor.hdmi; 725 int ret; 726 727 infoframe->set = false; 728 729 if (!info->has_hdmi_infoframe) 730 return 0; 731 732 ret = drm_hdmi_vendor_infoframe_from_display_mode(frame, connector, mode); 733 if (ret) 734 return ret; 735 736 infoframe->set = true; 737 738 return 0; 739 } 740 741 static int 742 hdmi_generate_infoframes(const struct drm_connector *connector, 743 struct drm_connector_state *conn_state) 744 { 745 const struct drm_display_info *info = &connector->display_info; 746 int ret; 747 748 if (!info->is_hdmi) 749 return 0; 750 751 ret = hdmi_generate_avi_infoframe(connector, conn_state); 752 if (ret) 753 return ret; 754 755 ret = hdmi_generate_spd_infoframe(connector, conn_state); 756 if (ret) 757 return ret; 758 759 /* 760 * Audio Infoframes will be generated by ALSA, and updated by 761 * drm_atomic_helper_connector_hdmi_update_audio_infoframe(). 762 */ 763 764 ret = hdmi_generate_hdr_infoframe(connector, conn_state); 765 if (ret) 766 return ret; 767 768 ret = hdmi_generate_hdmi_vendor_infoframe(connector, conn_state); 769 if (ret) 770 return ret; 771 772 return 0; 773 } 774 775 /** 776 * drm_atomic_helper_connector_hdmi_check() - Helper to check HDMI connector atomic state 777 * @connector: DRM Connector 778 * @state: the DRM State object 779 * 780 * Provides a default connector state check handler for HDMI connectors. 781 * Checks that a desired connector update is valid, and updates various 782 * fields of derived state. 783 * 784 * RETURNS: 785 * Zero on success, or an errno code otherwise. 786 */ 787 int drm_atomic_helper_connector_hdmi_check(struct drm_connector *connector, 788 struct drm_atomic_state *state) 789 { 790 struct drm_connector_state *old_conn_state = 791 drm_atomic_get_old_connector_state(state, connector); 792 struct drm_connector_state *new_conn_state = 793 drm_atomic_get_new_connector_state(state, connector); 794 const struct drm_display_mode *mode = 795 connector_state_get_mode(new_conn_state); 796 int ret; 797 798 if (!new_conn_state->crtc || !new_conn_state->best_encoder) 799 return 0; 800 801 new_conn_state->hdmi.is_limited_range = hdmi_is_limited_range(connector, new_conn_state); 802 803 ret = hdmi_compute_config(connector, new_conn_state, mode); 804 if (ret) 805 return ret; 806 807 ret = hdmi_generate_infoframes(connector, new_conn_state); 808 if (ret) 809 return ret; 810 811 if (old_conn_state->hdmi.broadcast_rgb != new_conn_state->hdmi.broadcast_rgb || 812 old_conn_state->hdmi.output_bpc != new_conn_state->hdmi.output_bpc || 813 old_conn_state->hdmi.output_format != new_conn_state->hdmi.output_format) { 814 struct drm_crtc *crtc = new_conn_state->crtc; 815 struct drm_crtc_state *crtc_state; 816 817 crtc_state = drm_atomic_get_crtc_state(state, crtc); 818 if (IS_ERR(crtc_state)) 819 return PTR_ERR(crtc_state); 820 821 crtc_state->mode_changed = true; 822 } 823 824 return 0; 825 } 826 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_check); 827 828 /** 829 * drm_hdmi_connector_mode_valid() - Check if mode is valid for HDMI connector 830 * @connector: DRM connector to validate the mode 831 * @mode: Display mode to validate 832 * 833 * Generic .mode_valid implementation for HDMI connectors. 834 */ 835 enum drm_mode_status 836 drm_hdmi_connector_mode_valid(struct drm_connector *connector, 837 const struct drm_display_mode *mode) 838 { 839 unsigned long long clock; 840 841 clock = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_RGB); 842 if (!clock) 843 return MODE_ERROR; 844 845 return hdmi_clock_valid(connector, mode, clock); 846 } 847 EXPORT_SYMBOL(drm_hdmi_connector_mode_valid); 848 849 static int clear_device_infoframe(struct drm_connector *connector, 850 enum hdmi_infoframe_type type) 851 { 852 const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs; 853 struct drm_device *dev = connector->dev; 854 int ret; 855 856 drm_dbg_kms(dev, "Clearing infoframe type 0x%x\n", type); 857 858 if (!funcs || !funcs->clear_infoframe) { 859 drm_dbg_kms(dev, "Function not implemented, bailing.\n"); 860 return 0; 861 } 862 863 ret = funcs->clear_infoframe(connector, type); 864 if (ret) { 865 drm_dbg_kms(dev, "Call failed: %d\n", ret); 866 return ret; 867 } 868 869 return 0; 870 } 871 872 static int clear_infoframe(struct drm_connector *connector, 873 struct drm_connector_hdmi_infoframe *old_frame) 874 { 875 int ret; 876 877 ret = clear_device_infoframe(connector, old_frame->data.any.type); 878 if (ret) 879 return ret; 880 881 return 0; 882 } 883 884 static int write_device_infoframe(struct drm_connector *connector, 885 union hdmi_infoframe *frame) 886 { 887 const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs; 888 struct drm_device *dev = connector->dev; 889 u8 buffer[HDMI_INFOFRAME_SIZE(MAX)]; 890 int ret; 891 int len; 892 893 drm_dbg_kms(dev, "Writing infoframe type %x\n", frame->any.type); 894 895 if (!funcs || !funcs->write_infoframe) { 896 drm_dbg_kms(dev, "Function not implemented, bailing.\n"); 897 return -EINVAL; 898 } 899 900 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer)); 901 if (len < 0) 902 return len; 903 904 ret = funcs->write_infoframe(connector, frame->any.type, buffer, len); 905 if (ret) { 906 drm_dbg_kms(dev, "Call failed: %d\n", ret); 907 return ret; 908 } 909 910 return 0; 911 } 912 913 static int write_infoframe(struct drm_connector *connector, 914 struct drm_connector_hdmi_infoframe *new_frame) 915 { 916 int ret; 917 918 ret = write_device_infoframe(connector, &new_frame->data); 919 if (ret) 920 return ret; 921 922 return 0; 923 } 924 925 static int write_or_clear_infoframe(struct drm_connector *connector, 926 struct drm_connector_hdmi_infoframe *old_frame, 927 struct drm_connector_hdmi_infoframe *new_frame) 928 { 929 if (new_frame->set) 930 return write_infoframe(connector, new_frame); 931 932 if (old_frame->set && !new_frame->set) 933 return clear_infoframe(connector, old_frame); 934 935 return 0; 936 } 937 938 /** 939 * drm_atomic_helper_connector_hdmi_update_infoframes - Update the Infoframes 940 * @connector: A pointer to the HDMI connector 941 * @state: The HDMI connector state to generate the infoframe from 942 * 943 * This function is meant for HDMI connector drivers to write their 944 * infoframes. It will typically be used in a 945 * @drm_connector_helper_funcs.atomic_enable implementation. 946 * 947 * Returns: 948 * Zero on success, error code on failure. 949 */ 950 int drm_atomic_helper_connector_hdmi_update_infoframes(struct drm_connector *connector, 951 struct drm_atomic_state *state) 952 { 953 struct drm_connector_state *old_conn_state = 954 drm_atomic_get_old_connector_state(state, connector); 955 struct drm_connector_state *new_conn_state = 956 drm_atomic_get_new_connector_state(state, connector); 957 struct drm_display_info *info = &connector->display_info; 958 int ret; 959 960 if (!info->is_hdmi) 961 return 0; 962 963 mutex_lock(&connector->hdmi.infoframes.lock); 964 965 ret = write_or_clear_infoframe(connector, 966 &old_conn_state->hdmi.infoframes.avi, 967 &new_conn_state->hdmi.infoframes.avi); 968 if (ret) 969 goto out; 970 971 if (connector->hdmi.infoframes.audio.set) { 972 ret = write_infoframe(connector, 973 &connector->hdmi.infoframes.audio); 974 if (ret) 975 goto out; 976 } 977 978 ret = write_or_clear_infoframe(connector, 979 &old_conn_state->hdmi.infoframes.hdr_drm, 980 &new_conn_state->hdmi.infoframes.hdr_drm); 981 if (ret) 982 goto out; 983 984 ret = write_or_clear_infoframe(connector, 985 &old_conn_state->hdmi.infoframes.spd, 986 &new_conn_state->hdmi.infoframes.spd); 987 if (ret) 988 goto out; 989 990 if (info->has_hdmi_infoframe) { 991 ret = write_or_clear_infoframe(connector, 992 &old_conn_state->hdmi.infoframes.hdmi, 993 &new_conn_state->hdmi.infoframes.hdmi); 994 if (ret) 995 goto out; 996 } 997 998 out: 999 mutex_unlock(&connector->hdmi.infoframes.lock); 1000 return ret; 1001 } 1002 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_update_infoframes); 1003 1004 /** 1005 * drm_atomic_helper_connector_hdmi_update_audio_infoframe - Update the Audio Infoframe 1006 * @connector: A pointer to the HDMI connector 1007 * @frame: A pointer to the audio infoframe to write 1008 * 1009 * This function is meant for HDMI connector drivers to update their 1010 * audio infoframe. It will typically be used in one of the ALSA hooks 1011 * (most likely prepare). 1012 * 1013 * Returns: 1014 * Zero on success, error code on failure. 1015 */ 1016 int 1017 drm_atomic_helper_connector_hdmi_update_audio_infoframe(struct drm_connector *connector, 1018 struct hdmi_audio_infoframe *frame) 1019 { 1020 struct drm_connector_hdmi_infoframe *infoframe = 1021 &connector->hdmi.infoframes.audio; 1022 struct drm_display_info *info = &connector->display_info; 1023 int ret; 1024 1025 if (!info->is_hdmi) 1026 return 0; 1027 1028 mutex_lock(&connector->hdmi.infoframes.lock); 1029 1030 memcpy(&infoframe->data, frame, sizeof(infoframe->data)); 1031 infoframe->set = true; 1032 1033 ret = write_infoframe(connector, infoframe); 1034 1035 mutex_unlock(&connector->hdmi.infoframes.lock); 1036 1037 return ret; 1038 } 1039 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_update_audio_infoframe); 1040 1041 /** 1042 * drm_atomic_helper_connector_hdmi_clear_audio_infoframe - Stop sending the Audio Infoframe 1043 * @connector: A pointer to the HDMI connector 1044 * 1045 * This function is meant for HDMI connector drivers to stop sending their 1046 * audio infoframe. It will typically be used in one of the ALSA hooks 1047 * (most likely shutdown). 1048 * 1049 * Returns: 1050 * Zero on success, error code on failure. 1051 */ 1052 int 1053 drm_atomic_helper_connector_hdmi_clear_audio_infoframe(struct drm_connector *connector) 1054 { 1055 struct drm_connector_hdmi_infoframe *infoframe = 1056 &connector->hdmi.infoframes.audio; 1057 struct drm_display_info *info = &connector->display_info; 1058 int ret; 1059 1060 if (!info->is_hdmi) 1061 return 0; 1062 1063 mutex_lock(&connector->hdmi.infoframes.lock); 1064 1065 infoframe->set = false; 1066 1067 ret = clear_infoframe(connector, infoframe); 1068 1069 memset(&infoframe->data, 0, sizeof(infoframe->data)); 1070 1071 mutex_unlock(&connector->hdmi.infoframes.lock); 1072 1073 return ret; 1074 } 1075 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_clear_audio_infoframe); 1076 1077 static void 1078 drm_atomic_helper_connector_hdmi_update(struct drm_connector *connector, 1079 enum drm_connector_status status) 1080 { 1081 const struct drm_edid *drm_edid; 1082 1083 if (status == connector_status_disconnected) { 1084 // TODO: also handle CEC and scramber, HDMI sink disconnected. 1085 drm_connector_hdmi_audio_plugged_notify(connector, false); 1086 drm_edid_connector_update(connector, NULL); 1087 return; 1088 } 1089 1090 if (connector->hdmi.funcs->read_edid) 1091 drm_edid = connector->hdmi.funcs->read_edid(connector); 1092 else 1093 drm_edid = drm_edid_read(connector); 1094 1095 drm_edid_connector_update(connector, drm_edid); 1096 1097 drm_edid_free(drm_edid); 1098 1099 if (status == connector_status_connected) { 1100 // TODO: also handle CEC and scramber, HDMI sink is now connected. 1101 drm_connector_hdmi_audio_plugged_notify(connector, true); 1102 } 1103 } 1104 1105 /** 1106 * drm_atomic_helper_connector_hdmi_hotplug - Handle the hotplug event for the HDMI connector 1107 * @connector: A pointer to the HDMI connector 1108 * @status: Connection status 1109 * 1110 * This function should be called as a part of the .detect() / .detect_ctx() 1111 * callbacks for all status changes. 1112 */ 1113 void drm_atomic_helper_connector_hdmi_hotplug(struct drm_connector *connector, 1114 enum drm_connector_status status) 1115 { 1116 drm_atomic_helper_connector_hdmi_update(connector, status); 1117 } 1118 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_hotplug); 1119 1120 /** 1121 * drm_atomic_helper_connector_hdmi_force - HDMI Connector implementation of the force callback 1122 * @connector: A pointer to the HDMI connector 1123 * 1124 * This function implements the .force() callback for the HDMI connectors. It 1125 * can either be used directly as the callback or should be called from within 1126 * the .force() callback implementation to maintain the HDMI-specific 1127 * connector's data. 1128 */ 1129 void drm_atomic_helper_connector_hdmi_force(struct drm_connector *connector) 1130 { 1131 drm_atomic_helper_connector_hdmi_update(connector, connector->status); 1132 } 1133 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_force); 1134