Path: blob/master/drivers/gpu/drm/display/drm_hdmi_state_helper.c
26494 views
// SPDX-License-Identifier: MIT12#include <linux/export.h>34#include <drm/drm_atomic.h>5#include <drm/drm_connector.h>6#include <drm/drm_edid.h>7#include <drm/drm_modes.h>8#include <drm/drm_print.h>910#include <drm/display/drm_hdmi_audio_helper.h>11#include <drm/display/drm_hdmi_cec_helper.h>12#include <drm/display/drm_hdmi_helper.h>13#include <drm/display/drm_hdmi_state_helper.h>1415/**16* DOC: hdmi helpers17*18* These functions contain an implementation of the HDMI specification19* in the form of KMS helpers.20*21* It contains TMDS character rate computation, automatic selection of22* output formats, infoframes generation, etc.23*24* Infoframes Compliance25* ~~~~~~~~~~~~~~~~~~~~~26*27* Drivers using the helpers will expose the various infoframes28* generated according to the HDMI specification in debugfs.29*30* Compliance can then be tested using ``edid-decode`` from the ``v4l-utils`` project31* (https://git.linuxtv.org/v4l-utils.git/). A sample run would look like:32*33* .. code-block:: bash34*35* # edid-decode \36* -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/audio \37* -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/avi \38* -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdmi \39* -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdr_drm \40* -I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/spd \41* /sys/class/drm/card1-HDMI-A-1/edid \42* -c43*44* edid-decode (hex):45*46* 00 ff ff ff ff ff ff 00 1e 6d f4 5b 1e ef 06 0047* 07 20 01 03 80 2f 34 78 ea 24 05 af 4f 42 ab 2548* 0f 50 54 21 08 00 d1 c0 61 40 45 40 01 01 01 0149* 01 01 01 01 01 01 98 d0 00 40 a1 40 d4 b0 30 2050* 3a 00 d1 0b 12 00 00 1a 00 00 00 fd 00 3b 3d 1e51* b2 31 00 0a 20 20 20 20 20 20 00 00 00 fc 00 4c52* 47 20 53 44 51 48 44 0a 20 20 20 20 00 00 00 ff53* 00 32 30 37 4e 54 52 4c 44 43 34 33 30 0a 01 4654*55* 02 03 42 72 23 09 07 07 4d 01 03 04 90 12 13 1f56* 22 5d 5e 5f 60 61 83 01 00 00 6d 03 0c 00 10 0057* b8 3c 20 00 60 01 02 03 67 d8 5d c4 01 78 80 0358* e3 0f 00 18 e2 00 6a e3 05 c0 00 e6 06 05 01 5259* 52 51 11 5d 00 a0 a0 40 29 b0 30 20 3a 00 d1 0b60* 12 00 00 1a 00 00 00 00 00 00 00 00 00 00 00 0061* 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0062* 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c363*64* ----------------65*66* Block 0, Base EDID:67* EDID Structure Version & Revision: 1.368* Vendor & Product Identification:69* Manufacturer: GSM70* Model: 2354071* Serial Number: 454430 (0x0006ef1e)72* Made in: week 7 of 202273* Basic Display Parameters & Features:74* Digital display75* Maximum image size: 47 cm x 52 cm76* Gamma: 2.2077* DPMS levels: Standby Suspend Off78* RGB color display79* First detailed timing is the preferred timing80* Color Characteristics:81* Red : 0.6835, 0.310582* Green: 0.2587, 0.667983* Blue : 0.1445, 0.058584* White: 0.3134, 0.329185* Established Timings I & II:86* DMT 0x04: 640x480 59.940476 Hz 4:3 31.469 kHz 25.175000 MHz87* DMT 0x09: 800x600 60.316541 Hz 4:3 37.879 kHz 40.000000 MHz88* DMT 0x10: 1024x768 60.003840 Hz 4:3 48.363 kHz 65.000000 MHz89* Standard Timings:90* DMT 0x52: 1920x1080 60.000000 Hz 16:9 67.500 kHz 148.500000 MHz91* DMT 0x10: 1024x768 60.003840 Hz 4:3 48.363 kHz 65.000000 MHz92* DMT 0x09: 800x600 60.316541 Hz 4:3 37.879 kHz 40.000000 MHz93* Detailed Timing Descriptors:94* DTD 1: 2560x2880 59.966580 Hz 8:9 185.417 kHz 534.000000 MHz (465 mm x 523 mm)95* Hfront 48 Hsync 32 Hback 240 Hpol P96* Vfront 3 Vsync 10 Vback 199 Vpol N97* Display Range Limits:98* Monitor ranges (GTF): 59-61 Hz V, 30-178 kHz H, max dotclock 490 MHz99* Display Product Name: 'LG SDQHD'100* Display Product Serial Number: '207NTRLDC430'101* Extension blocks: 1102* Checksum: 0x46103*104* ----------------105*106* Block 1, CTA-861 Extension Block:107* Revision: 3108* Basic audio support109* Supports YCbCr 4:4:4110* Supports YCbCr 4:2:2111* Native detailed modes: 2112* Audio Data Block:113* Linear PCM:114* Max channels: 2115* Supported sample rates (kHz): 48 44.1 32116* Supported sample sizes (bits): 24 20 16117* Video Data Block:118* VIC 1: 640x480 59.940476 Hz 4:3 31.469 kHz 25.175000 MHz119* VIC 3: 720x480 59.940060 Hz 16:9 31.469 kHz 27.000000 MHz120* VIC 4: 1280x720 60.000000 Hz 16:9 45.000 kHz 74.250000 MHz121* VIC 16: 1920x1080 60.000000 Hz 16:9 67.500 kHz 148.500000 MHz (native)122* VIC 18: 720x576 50.000000 Hz 16:9 31.250 kHz 27.000000 MHz123* VIC 19: 1280x720 50.000000 Hz 16:9 37.500 kHz 74.250000 MHz124* VIC 31: 1920x1080 50.000000 Hz 16:9 56.250 kHz 148.500000 MHz125* VIC 34: 1920x1080 30.000000 Hz 16:9 33.750 kHz 74.250000 MHz126* VIC 93: 3840x2160 24.000000 Hz 16:9 54.000 kHz 297.000000 MHz127* VIC 94: 3840x2160 25.000000 Hz 16:9 56.250 kHz 297.000000 MHz128* VIC 95: 3840x2160 30.000000 Hz 16:9 67.500 kHz 297.000000 MHz129* VIC 96: 3840x2160 50.000000 Hz 16:9 112.500 kHz 594.000000 MHz130* VIC 97: 3840x2160 60.000000 Hz 16:9 135.000 kHz 594.000000 MHz131* Speaker Allocation Data Block:132* FL/FR - Front Left/Right133* Vendor-Specific Data Block (HDMI), OUI 00-0C-03:134* Source physical address: 1.0.0.0135* Supports_AI136* DC_36bit137* DC_30bit138* DC_Y444139* Maximum TMDS clock: 300 MHz140* Extended HDMI video details:141* HDMI VICs:142* HDMI VIC 1: 3840x2160 30.000000 Hz 16:9 67.500 kHz 297.000000 MHz143* HDMI VIC 2: 3840x2160 25.000000 Hz 16:9 56.250 kHz 297.000000 MHz144* HDMI VIC 3: 3840x2160 24.000000 Hz 16:9 54.000 kHz 297.000000 MHz145* Vendor-Specific Data Block (HDMI Forum), OUI C4-5D-D8:146* Version: 1147* Maximum TMDS Character Rate: 600 MHz148* SCDC Present149* Supports 12-bits/component Deep Color 4:2:0 Pixel Encoding150* Supports 10-bits/component Deep Color 4:2:0 Pixel Encoding151* YCbCr 4:2:0 Capability Map Data Block:152* VIC 96: 3840x2160 50.000000 Hz 16:9 112.500 kHz 594.000000 MHz153* VIC 97: 3840x2160 60.000000 Hz 16:9 135.000 kHz 594.000000 MHz154* Video Capability Data Block:155* YCbCr quantization: No Data156* RGB quantization: Selectable (via AVI Q)157* PT scan behavior: Always Underscanned158* IT scan behavior: Always Underscanned159* CE scan behavior: Always Underscanned160* Colorimetry Data Block:161* BT2020YCC162* BT2020RGB163* HDR Static Metadata Data Block:164* Electro optical transfer functions:165* Traditional gamma - SDR luminance range166* SMPTE ST2084167* Supported static metadata descriptors:168* Static metadata type 1169* Desired content max luminance: 82 (295.365 cd/m^2)170* Desired content max frame-average luminance: 82 (295.365 cd/m^2)171* Desired content min luminance: 81 (0.298 cd/m^2)172* Detailed Timing Descriptors:173* DTD 2: 2560x2880 29.986961 Hz 8:9 87.592 kHz 238.250000 MHz (465 mm x 523 mm)174* Hfront 48 Hsync 32 Hback 80 Hpol P175* Vfront 3 Vsync 10 Vback 28 Vpol N176* Checksum: 0xc3 Unused space in Extension Block: 43 bytes177*178* ----------------179*180* edid-decode 1.29.0-5346181* edid-decode SHA: c363e9aa6d70 2025-03-11 11:41:18182*183* Warnings:184*185* Block 1, CTA-861 Extension Block:186* IT Video Formats are overscanned by default, but normally this should be underscanned.187* Video Data Block: VIC 1 and the first DTD are not identical. Is this intended?188* Video Data Block: All VICs are in ascending order, and the first (preferred) VIC <= 4, is that intended?189* Video Capability Data Block: Set Selectable YCbCr Quantization to avoid interop issues.190* Video Capability Data Block: S_PT is equal to S_IT and S_CE, so should be set to 0 instead.191* Colorimetry Data Block: Set the sRGB colorimetry bit to avoid interop issues.192* Display Product Serial Number is set, so the Serial Number in the Base EDID should be 0.193* EDID:194* Base EDID: Some timings are out of range of the Monitor Ranges:195* Vertical Freq: 24.000 - 60.317 Hz (Monitor: 59.000 - 61.000 Hz)196* Horizontal Freq: 31.250 - 185.416 kHz (Monitor: 30.000 - 178.000 kHz)197* Maximum Clock: 594.000 MHz (Monitor: 490.000 MHz)198*199* Failures:200*201* Block 1, CTA-861 Extension Block:202* 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.203* EDID:204* CTA-861: Native progressive timings are a mix of several resolutions.205*206* EDID conformity: FAIL207*208* ================209*210* InfoFrame of '/sys/kernel/debug/dri/1/HDMI-A-1/infoframes/audio' was empty.211*212* ================213*214* edid-decode InfoFrame (hex):215*216* 82 02 0d 31 12 28 04 00 00 00 00 00 00 00 00 00217* 00218*219* ----------------220*221* HDMI InfoFrame Checksum: 0x31222*223* AVI InfoFrame224* Version: 2225* Length: 13226* Y: Color Component Sample Format: RGB227* A: Active Format Information Present: Yes228* B: Bar Data Present: Bar Data not present229* S: Scan Information: Composed for an underscanned display230* C: Colorimetry: No Data231* M: Picture Aspect Ratio: 16:9232* R: Active Portion Aspect Ratio: 8233* ITC: IT Content: No Data234* EC: Extended Colorimetry: xvYCC601235* Q: RGB Quantization Range: Limited Range236* SC: Non-Uniform Picture Scaling: No Known non-uniform scaling237* YQ: YCC Quantization Range: Limited Range238* CN: IT Content Type: Graphics239* PR: Pixel Data Repetition Count: 0240* Line Number of End of Top Bar: 0241* Line Number of Start of Bottom Bar: 0242* Pixel Number of End of Left Bar: 0243* Pixel Number of Start of Right Bar: 0244*245* ----------------246*247* AVI InfoFrame conformity: PASS248*249* ================250*251* edid-decode InfoFrame (hex):252*253* 81 01 05 49 03 0c 00 20 01254*255* ----------------256*257* HDMI InfoFrame Checksum: 0x49258*259* Vendor-Specific InfoFrame (HDMI), OUI 00-0C-03260* Version: 1261* Length: 5262* HDMI Video Format: HDMI_VIC is present263* HDMI VIC 1: 3840x2160 30.000000 Hz 16:9 67.500 kHz 297.000000 MHz264*265* ----------------266*267* Vendor-Specific InfoFrame (HDMI), OUI 00-0C-03 conformity: PASS268*269* ================270*271* InfoFrame of '/sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdr_drm' was empty.272*273* ================274*275* edid-decode InfoFrame (hex):276*277* 83 01 19 93 42 72 6f 61 64 63 6f 6d 56 69 64 65278* 6f 63 6f 72 65 00 00 00 00 00 00 00 09279*280* ----------------281*282* HDMI InfoFrame Checksum: 0x93283*284* Source Product Description InfoFrame285* Version: 1286* Length: 25287* Vendor Name: 'Broadcom'288* Product Description: 'Videocore'289* Source Information: PC general290*291* ----------------292*293* Source Product Description InfoFrame conformity: PASS294*295* Testing296* ~~~~~~~297*298* The helpers have unit testing and can be tested using kunit with:299*300* .. code-block:: bash301*302* $ ./tools/testing/kunit/kunit.py run \303* --kunitconfig=drivers/gpu/drm/tests \304* drm_atomic_helper_connector_hdmi_*305*/306307/**308* __drm_atomic_helper_connector_hdmi_reset() - Initializes all HDMI @drm_connector_state resources309* @connector: DRM connector310* @new_conn_state: connector state to reset311*312* Initializes all HDMI resources from a @drm_connector_state without313* actually allocating it. This is useful for HDMI drivers, in314* combination with __drm_atomic_helper_connector_reset() or315* drm_atomic_helper_connector_reset().316*/317void __drm_atomic_helper_connector_hdmi_reset(struct drm_connector *connector,318struct drm_connector_state *new_conn_state)319{320unsigned int max_bpc = connector->max_bpc;321322new_conn_state->max_bpc = max_bpc;323new_conn_state->max_requested_bpc = max_bpc;324new_conn_state->hdmi.broadcast_rgb = DRM_HDMI_BROADCAST_RGB_AUTO;325}326EXPORT_SYMBOL(__drm_atomic_helper_connector_hdmi_reset);327328static const struct drm_display_mode *329connector_state_get_mode(const struct drm_connector_state *conn_state)330{331struct drm_atomic_state *state;332struct drm_crtc_state *crtc_state;333struct drm_crtc *crtc;334335state = conn_state->state;336if (!state)337return NULL;338339crtc = conn_state->crtc;340if (!crtc)341return NULL;342343crtc_state = drm_atomic_get_new_crtc_state(state, crtc);344if (!crtc_state)345return NULL;346347return &crtc_state->mode;348}349350static bool hdmi_is_limited_range(const struct drm_connector *connector,351const struct drm_connector_state *conn_state)352{353const struct drm_display_info *info = &connector->display_info;354const struct drm_display_mode *mode =355connector_state_get_mode(conn_state);356357/*358* The Broadcast RGB property only applies to RGB format, and359* i915 just assumes limited range for YCbCr output, so let's360* just do the same.361*/362if (conn_state->hdmi.output_format != HDMI_COLORSPACE_RGB)363return true;364365if (conn_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_FULL)366return false;367368if (conn_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_LIMITED)369return true;370371if (!info->is_hdmi)372return false;373374return drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED;375}376377static bool378sink_supports_format_bpc(const struct drm_connector *connector,379const struct drm_display_info *info,380const struct drm_display_mode *mode,381unsigned int format, unsigned int bpc)382{383struct drm_device *dev = connector->dev;384u8 vic = drm_match_cea_mode(mode);385386/*387* CTA-861-F, section 5.4 - Color Coding & Quantization states388* that the bpc must be 8, 10, 12 or 16 except for the default389* 640x480 VIC1 where the value must be 8.390*391* The definition of default here is ambiguous but the spec392* refers to VIC1 being the default timing in several occasions393* so our understanding is that for the default timing (ie,394* VIC1), the bpc must be 8.395*/396if (vic == 1 && bpc != 8) {397drm_dbg_kms(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);398return false;399}400401if (!info->is_hdmi &&402(format != HDMI_COLORSPACE_RGB || bpc != 8)) {403drm_dbg_kms(dev, "DVI Monitors require an RGB output at 8 bpc\n");404return false;405}406407if (!(connector->hdmi.supported_formats & BIT(format))) {408drm_dbg_kms(dev, "%s format unsupported by the connector.\n",409drm_hdmi_connector_get_output_format_name(format));410return false;411}412413if (drm_mode_is_420_only(info, mode) && format != HDMI_COLORSPACE_YUV420) {414drm_dbg_kms(dev, "Mode can be only supported in YUV420 format.\n");415return false;416}417418switch (format) {419case HDMI_COLORSPACE_RGB:420drm_dbg_kms(dev, "RGB Format, checking the constraints.\n");421422/*423* In some cases, like when the EDID readout fails, or424* is not an HDMI compliant EDID for some reason, the425* color_formats field will be blank and not report any426* format supported. In such a case, assume that RGB is427* supported so we can keep things going and light up428* the display.429*/430if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444))431drm_warn(dev, "HDMI Sink doesn't support RGB, something's wrong.\n");432433if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {434drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n");435return false;436}437438if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {439drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n");440return false;441}442443drm_dbg_kms(dev, "RGB format supported in that configuration.\n");444445return true;446447case HDMI_COLORSPACE_YUV420:448drm_dbg_kms(dev, "YUV420 format, checking the constraints.\n");449450if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR420)) {451drm_dbg_kms(dev, "Sink doesn't support YUV420.\n");452return false;453}454455if (!drm_mode_is_420(info, mode)) {456drm_dbg_kms(dev, "Mode cannot be supported in YUV420 format.\n");457return false;458}459460if (bpc == 10 && !(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)) {461drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n");462return false;463}464465if (bpc == 12 && !(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)) {466drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n");467return false;468}469470if (bpc == 16 && !(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)) {471drm_dbg_kms(dev, "16 BPC but sink doesn't support Deep Color 48.\n");472return false;473}474475drm_dbg_kms(dev, "YUV420 format supported in that configuration.\n");476477return true;478479case HDMI_COLORSPACE_YUV422:480drm_dbg_kms(dev, "YUV422 format, checking the constraints.\n");481482if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) {483drm_dbg_kms(dev, "Sink doesn't support YUV422.\n");484return false;485}486487if (bpc > 12) {488drm_dbg_kms(dev, "YUV422 only supports 12 bpc or lower.\n");489return false;490}491492/*493* HDMI Spec 1.3 - Section 6.5 Pixel Encodings and Color Depth494* states that Deep Color is not relevant for YUV422 so we495* don't need to check the Deep Color bits in the EDIDs here.496*/497498drm_dbg_kms(dev, "YUV422 format supported in that configuration.\n");499500return true;501502case HDMI_COLORSPACE_YUV444:503drm_dbg_kms(dev, "YUV444 format, checking the constraints.\n");504505if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) {506drm_dbg_kms(dev, "Sink doesn't support YUV444.\n");507return false;508}509510if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {511drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n");512return false;513}514515if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {516drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n");517return false;518}519520drm_dbg_kms(dev, "YUV444 format supported in that configuration.\n");521522return true;523}524525drm_dbg_kms(dev, "Unsupported pixel format.\n");526return false;527}528529static enum drm_mode_status530hdmi_clock_valid(const struct drm_connector *connector,531const struct drm_display_mode *mode,532unsigned long long clock)533{534const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;535const struct drm_display_info *info = &connector->display_info;536537if (info->max_tmds_clock && clock > info->max_tmds_clock * 1000)538return MODE_CLOCK_HIGH;539540if (funcs && funcs->tmds_char_rate_valid) {541enum drm_mode_status status;542543status = funcs->tmds_char_rate_valid(connector, mode, clock);544if (status != MODE_OK)545return status;546}547548return MODE_OK;549}550551static int552hdmi_compute_clock(const struct drm_connector *connector,553struct drm_connector_state *conn_state,554const struct drm_display_mode *mode,555unsigned int bpc, enum hdmi_colorspace fmt)556{557enum drm_mode_status status;558unsigned long long clock;559560clock = drm_hdmi_compute_mode_clock(mode, bpc, fmt);561if (!clock)562return -EINVAL;563564status = hdmi_clock_valid(connector, mode, clock);565if (status != MODE_OK)566return -EINVAL;567568conn_state->hdmi.tmds_char_rate = clock;569570return 0;571}572573static bool574hdmi_try_format_bpc(const struct drm_connector *connector,575struct drm_connector_state *conn_state,576const struct drm_display_mode *mode,577unsigned int bpc, enum hdmi_colorspace fmt)578{579const struct drm_display_info *info = &connector->display_info;580struct drm_device *dev = connector->dev;581int ret;582583drm_dbg_kms(dev, "Trying %s output format with %u bpc\n",584drm_hdmi_connector_get_output_format_name(fmt),585bpc);586587if (!sink_supports_format_bpc(connector, info, mode, fmt, bpc)) {588drm_dbg_kms(dev, "%s output format not supported with %u bpc\n",589drm_hdmi_connector_get_output_format_name(fmt),590bpc);591return false;592}593594ret = hdmi_compute_clock(connector, conn_state, mode, bpc, fmt);595if (ret) {596drm_dbg_kms(dev, "Couldn't compute clock for %s output format and %u bpc\n",597drm_hdmi_connector_get_output_format_name(fmt),598bpc);599return false;600}601602drm_dbg_kms(dev, "%s output format supported with %u bpc (TMDS char rate: %llu Hz)\n",603drm_hdmi_connector_get_output_format_name(fmt),604bpc, conn_state->hdmi.tmds_char_rate);605606return true;607}608609static int610hdmi_compute_format_bpc(const struct drm_connector *connector,611struct drm_connector_state *conn_state,612const struct drm_display_mode *mode,613unsigned int max_bpc, enum hdmi_colorspace fmt)614{615struct drm_device *dev = connector->dev;616unsigned int bpc;617int ret;618619for (bpc = max_bpc; bpc >= 8; bpc -= 2) {620ret = hdmi_try_format_bpc(connector, conn_state, mode, bpc, fmt);621if (!ret)622continue;623624conn_state->hdmi.output_bpc = bpc;625conn_state->hdmi.output_format = fmt;626627drm_dbg_kms(dev,628"Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",629mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),630conn_state->hdmi.output_bpc,631drm_hdmi_connector_get_output_format_name(conn_state->hdmi.output_format),632conn_state->hdmi.tmds_char_rate);633634return 0;635}636637drm_dbg_kms(dev, "Failed. %s output format not supported for any bpc count.\n",638drm_hdmi_connector_get_output_format_name(fmt));639640return -EINVAL;641}642643static int644hdmi_compute_config(const struct drm_connector *connector,645struct drm_connector_state *conn_state,646const struct drm_display_mode *mode)647{648unsigned int max_bpc = clamp_t(unsigned int,649conn_state->max_bpc,6508, connector->max_bpc);651int ret;652653ret = hdmi_compute_format_bpc(connector, conn_state, mode, max_bpc,654HDMI_COLORSPACE_RGB);655if (ret) {656if (connector->ycbcr_420_allowed) {657ret = hdmi_compute_format_bpc(connector, conn_state,658mode, max_bpc,659HDMI_COLORSPACE_YUV420);660if (ret)661drm_dbg_kms(connector->dev,662"YUV420 output format doesn't work.\n");663} else {664drm_dbg_kms(connector->dev,665"YUV420 output format not allowed for connector.\n");666ret = -EINVAL;667}668}669670return ret;671}672673static int hdmi_generate_avi_infoframe(const struct drm_connector *connector,674struct drm_connector_state *conn_state)675{676const struct drm_display_mode *mode =677connector_state_get_mode(conn_state);678struct drm_connector_hdmi_infoframe *infoframe =679&conn_state->hdmi.infoframes.avi;680struct hdmi_avi_infoframe *frame =681&infoframe->data.avi;682bool is_limited_range = conn_state->hdmi.is_limited_range;683enum hdmi_quantization_range rgb_quant_range =684is_limited_range ? HDMI_QUANTIZATION_RANGE_LIMITED : HDMI_QUANTIZATION_RANGE_FULL;685int ret;686687infoframe->set = false;688689ret = drm_hdmi_avi_infoframe_from_display_mode(frame, connector, mode);690if (ret)691return ret;692693frame->colorspace = conn_state->hdmi.output_format;694695/*696* FIXME: drm_hdmi_avi_infoframe_quant_range() doesn't handle697* YUV formats at all at the moment, so if we ever support YUV698* formats this needs to be revised.699*/700drm_hdmi_avi_infoframe_quant_range(frame, connector, mode, rgb_quant_range);701drm_hdmi_avi_infoframe_colorimetry(frame, conn_state);702drm_hdmi_avi_infoframe_bars(frame, conn_state);703704infoframe->set = true;705706return 0;707}708709static int hdmi_generate_spd_infoframe(const struct drm_connector *connector,710struct drm_connector_state *conn_state)711{712struct drm_connector_hdmi_infoframe *infoframe =713&conn_state->hdmi.infoframes.spd;714struct hdmi_spd_infoframe *frame =715&infoframe->data.spd;716int ret;717718infoframe->set = false;719720ret = hdmi_spd_infoframe_init(frame,721connector->hdmi.vendor,722connector->hdmi.product);723if (ret)724return ret;725726frame->sdi = HDMI_SPD_SDI_PC;727728infoframe->set = true;729730return 0;731}732733static int hdmi_generate_hdr_infoframe(const struct drm_connector *connector,734struct drm_connector_state *conn_state)735{736struct drm_connector_hdmi_infoframe *infoframe =737&conn_state->hdmi.infoframes.hdr_drm;738struct hdmi_drm_infoframe *frame =739&infoframe->data.drm;740int ret;741742infoframe->set = false;743744if (connector->max_bpc < 10)745return 0;746747if (!conn_state->hdr_output_metadata)748return 0;749750ret = drm_hdmi_infoframe_set_hdr_metadata(frame, conn_state);751if (ret)752return ret;753754infoframe->set = true;755756return 0;757}758759static int hdmi_generate_hdmi_vendor_infoframe(const struct drm_connector *connector,760struct drm_connector_state *conn_state)761{762const struct drm_display_info *info = &connector->display_info;763const struct drm_display_mode *mode =764connector_state_get_mode(conn_state);765struct drm_connector_hdmi_infoframe *infoframe =766&conn_state->hdmi.infoframes.hdmi;767struct hdmi_vendor_infoframe *frame =768&infoframe->data.vendor.hdmi;769int ret;770771infoframe->set = false;772773if (!info->has_hdmi_infoframe)774return 0;775776ret = drm_hdmi_vendor_infoframe_from_display_mode(frame, connector, mode);777if (ret)778return ret;779780infoframe->set = true;781782return 0;783}784785static int786hdmi_generate_infoframes(const struct drm_connector *connector,787struct drm_connector_state *conn_state)788{789const struct drm_display_info *info = &connector->display_info;790int ret;791792if (!info->is_hdmi)793return 0;794795ret = hdmi_generate_avi_infoframe(connector, conn_state);796if (ret)797return ret;798799ret = hdmi_generate_spd_infoframe(connector, conn_state);800if (ret)801return ret;802803/*804* Audio Infoframes will be generated by ALSA, and updated by805* drm_atomic_helper_connector_hdmi_update_audio_infoframe().806*/807808ret = hdmi_generate_hdr_infoframe(connector, conn_state);809if (ret)810return ret;811812ret = hdmi_generate_hdmi_vendor_infoframe(connector, conn_state);813if (ret)814return ret;815816return 0;817}818819/**820* drm_atomic_helper_connector_hdmi_check() - Helper to check HDMI connector atomic state821* @connector: DRM Connector822* @state: the DRM State object823*824* Provides a default connector state check handler for HDMI connectors.825* Checks that a desired connector update is valid, and updates various826* fields of derived state.827*828* RETURNS:829* Zero on success, or an errno code otherwise.830*/831int drm_atomic_helper_connector_hdmi_check(struct drm_connector *connector,832struct drm_atomic_state *state)833{834struct drm_connector_state *old_conn_state =835drm_atomic_get_old_connector_state(state, connector);836struct drm_connector_state *new_conn_state =837drm_atomic_get_new_connector_state(state, connector);838const struct drm_display_mode *mode =839connector_state_get_mode(new_conn_state);840int ret;841842if (!new_conn_state->crtc || !new_conn_state->best_encoder)843return 0;844845ret = hdmi_compute_config(connector, new_conn_state, mode);846if (ret)847return ret;848849new_conn_state->hdmi.is_limited_range = hdmi_is_limited_range(connector, new_conn_state);850851ret = hdmi_generate_infoframes(connector, new_conn_state);852if (ret)853return ret;854855if (old_conn_state->hdmi.broadcast_rgb != new_conn_state->hdmi.broadcast_rgb ||856old_conn_state->hdmi.output_bpc != new_conn_state->hdmi.output_bpc ||857old_conn_state->hdmi.output_format != new_conn_state->hdmi.output_format) {858struct drm_crtc *crtc = new_conn_state->crtc;859struct drm_crtc_state *crtc_state;860861crtc_state = drm_atomic_get_crtc_state(state, crtc);862if (IS_ERR(crtc_state))863return PTR_ERR(crtc_state);864865crtc_state->mode_changed = true;866}867868return 0;869}870EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_check);871872/**873* drm_hdmi_connector_mode_valid() - Check if mode is valid for HDMI connector874* @connector: DRM connector to validate the mode875* @mode: Display mode to validate876*877* Generic .mode_valid implementation for HDMI connectors.878*/879enum drm_mode_status880drm_hdmi_connector_mode_valid(struct drm_connector *connector,881const struct drm_display_mode *mode)882{883unsigned long long clock;884885clock = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_RGB);886if (!clock)887return MODE_ERROR;888889return hdmi_clock_valid(connector, mode, clock);890}891EXPORT_SYMBOL(drm_hdmi_connector_mode_valid);892893static int clear_device_infoframe(struct drm_connector *connector,894enum hdmi_infoframe_type type)895{896const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;897struct drm_device *dev = connector->dev;898int ret;899900drm_dbg_kms(dev, "Clearing infoframe type 0x%x\n", type);901902if (!funcs || !funcs->clear_infoframe) {903drm_dbg_kms(dev, "Function not implemented, bailing.\n");904return 0;905}906907ret = funcs->clear_infoframe(connector, type);908if (ret) {909drm_dbg_kms(dev, "Call failed: %d\n", ret);910return ret;911}912913return 0;914}915916static int clear_infoframe(struct drm_connector *connector,917struct drm_connector_hdmi_infoframe *old_frame)918{919int ret;920921ret = clear_device_infoframe(connector, old_frame->data.any.type);922if (ret)923return ret;924925return 0;926}927928static int write_device_infoframe(struct drm_connector *connector,929union hdmi_infoframe *frame)930{931const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;932struct drm_device *dev = connector->dev;933u8 buffer[HDMI_INFOFRAME_SIZE(MAX)];934int ret;935int len;936937drm_dbg_kms(dev, "Writing infoframe type %x\n", frame->any.type);938939if (!funcs || !funcs->write_infoframe) {940drm_dbg_kms(dev, "Function not implemented, bailing.\n");941return -EINVAL;942}943944len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));945if (len < 0)946return len;947948ret = funcs->write_infoframe(connector, frame->any.type, buffer, len);949if (ret) {950drm_dbg_kms(dev, "Call failed: %d\n", ret);951return ret;952}953954return 0;955}956957static int write_infoframe(struct drm_connector *connector,958struct drm_connector_hdmi_infoframe *new_frame)959{960int ret;961962ret = write_device_infoframe(connector, &new_frame->data);963if (ret)964return ret;965966return 0;967}968969static int write_or_clear_infoframe(struct drm_connector *connector,970struct drm_connector_hdmi_infoframe *old_frame,971struct drm_connector_hdmi_infoframe *new_frame)972{973if (new_frame->set)974return write_infoframe(connector, new_frame);975976if (old_frame->set && !new_frame->set)977return clear_infoframe(connector, old_frame);978979return 0;980}981982/**983* drm_atomic_helper_connector_hdmi_update_infoframes - Update the Infoframes984* @connector: A pointer to the HDMI connector985* @state: The HDMI connector state to generate the infoframe from986*987* This function is meant for HDMI connector drivers to write their988* infoframes. It will typically be used in a989* @drm_connector_helper_funcs.atomic_enable implementation.990*991* Returns:992* Zero on success, error code on failure.993*/994int drm_atomic_helper_connector_hdmi_update_infoframes(struct drm_connector *connector,995struct drm_atomic_state *state)996{997struct drm_connector_state *old_conn_state =998drm_atomic_get_old_connector_state(state, connector);999struct drm_connector_state *new_conn_state =1000drm_atomic_get_new_connector_state(state, connector);1001struct drm_display_info *info = &connector->display_info;1002int ret;10031004if (!info->is_hdmi)1005return 0;10061007mutex_lock(&connector->hdmi.infoframes.lock);10081009ret = write_or_clear_infoframe(connector,1010&old_conn_state->hdmi.infoframes.avi,1011&new_conn_state->hdmi.infoframes.avi);1012if (ret)1013goto out;10141015if (connector->hdmi.infoframes.audio.set) {1016ret = write_infoframe(connector,1017&connector->hdmi.infoframes.audio);1018if (ret)1019goto out;1020}10211022ret = write_or_clear_infoframe(connector,1023&old_conn_state->hdmi.infoframes.hdr_drm,1024&new_conn_state->hdmi.infoframes.hdr_drm);1025if (ret)1026goto out;10271028ret = write_or_clear_infoframe(connector,1029&old_conn_state->hdmi.infoframes.spd,1030&new_conn_state->hdmi.infoframes.spd);1031if (ret)1032goto out;10331034if (info->has_hdmi_infoframe) {1035ret = write_or_clear_infoframe(connector,1036&old_conn_state->hdmi.infoframes.hdmi,1037&new_conn_state->hdmi.infoframes.hdmi);1038if (ret)1039goto out;1040}10411042out:1043mutex_unlock(&connector->hdmi.infoframes.lock);1044return ret;1045}1046EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_update_infoframes);10471048/**1049* drm_atomic_helper_connector_hdmi_update_audio_infoframe - Update the Audio Infoframe1050* @connector: A pointer to the HDMI connector1051* @frame: A pointer to the audio infoframe to write1052*1053* This function is meant for HDMI connector drivers to update their1054* audio infoframe. It will typically be used in one of the ALSA hooks1055* (most likely prepare).1056*1057* Returns:1058* Zero on success, error code on failure.1059*/1060int1061drm_atomic_helper_connector_hdmi_update_audio_infoframe(struct drm_connector *connector,1062struct hdmi_audio_infoframe *frame)1063{1064struct drm_connector_hdmi_infoframe *infoframe =1065&connector->hdmi.infoframes.audio;1066struct drm_display_info *info = &connector->display_info;1067int ret;10681069if (!info->is_hdmi)1070return 0;10711072mutex_lock(&connector->hdmi.infoframes.lock);10731074memcpy(&infoframe->data, frame, sizeof(infoframe->data));1075infoframe->set = true;10761077ret = write_infoframe(connector, infoframe);10781079mutex_unlock(&connector->hdmi.infoframes.lock);10801081return ret;1082}1083EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_update_audio_infoframe);10841085/**1086* drm_atomic_helper_connector_hdmi_clear_audio_infoframe - Stop sending the Audio Infoframe1087* @connector: A pointer to the HDMI connector1088*1089* This function is meant for HDMI connector drivers to stop sending their1090* audio infoframe. It will typically be used in one of the ALSA hooks1091* (most likely shutdown).1092*1093* Returns:1094* Zero on success, error code on failure.1095*/1096int1097drm_atomic_helper_connector_hdmi_clear_audio_infoframe(struct drm_connector *connector)1098{1099struct drm_connector_hdmi_infoframe *infoframe =1100&connector->hdmi.infoframes.audio;1101struct drm_display_info *info = &connector->display_info;1102int ret;11031104if (!info->is_hdmi)1105return 0;11061107mutex_lock(&connector->hdmi.infoframes.lock);11081109infoframe->set = false;11101111ret = clear_infoframe(connector, infoframe);11121113memset(&infoframe->data, 0, sizeof(infoframe->data));11141115mutex_unlock(&connector->hdmi.infoframes.lock);11161117return ret;1118}1119EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_clear_audio_infoframe);11201121static void1122drm_atomic_helper_connector_hdmi_update(struct drm_connector *connector,1123enum drm_connector_status status)1124{1125const struct drm_edid *drm_edid;11261127if (status == connector_status_disconnected) {1128// TODO: also handle scramber, HDMI sink disconnected.1129drm_connector_hdmi_audio_plugged_notify(connector, false);1130drm_edid_connector_update(connector, NULL);1131drm_connector_cec_phys_addr_invalidate(connector);1132return;1133}11341135if (connector->hdmi.funcs->read_edid)1136drm_edid = connector->hdmi.funcs->read_edid(connector);1137else1138drm_edid = drm_edid_read(connector);11391140drm_edid_connector_update(connector, drm_edid);11411142drm_edid_free(drm_edid);11431144if (status == connector_status_connected) {1145// TODO: also handle scramber, HDMI sink is now connected.1146drm_connector_hdmi_audio_plugged_notify(connector, true);1147drm_connector_cec_phys_addr_set(connector);1148}1149}11501151/**1152* drm_atomic_helper_connector_hdmi_hotplug - Handle the hotplug event for the HDMI connector1153* @connector: A pointer to the HDMI connector1154* @status: Connection status1155*1156* This function should be called as a part of the .detect() / .detect_ctx()1157* callbacks for all status changes.1158*/1159void drm_atomic_helper_connector_hdmi_hotplug(struct drm_connector *connector,1160enum drm_connector_status status)1161{1162drm_atomic_helper_connector_hdmi_update(connector, status);1163}1164EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_hotplug);11651166/**1167* drm_atomic_helper_connector_hdmi_force - HDMI Connector implementation of the force callback1168* @connector: A pointer to the HDMI connector1169*1170* This function implements the .force() callback for the HDMI connectors. It1171* can either be used directly as the callback or should be called from within1172* the .force() callback implementation to maintain the HDMI-specific1173* connector's data.1174*/1175void drm_atomic_helper_connector_hdmi_force(struct drm_connector *connector)1176{1177drm_atomic_helper_connector_hdmi_update(connector, connector->status);1178}1179EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_force);118011811182