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