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