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