1 // SPDX-License-Identifier: MIT
3 #include <linux/module.h>
5 #include <drm/display/drm_hdmi_helper.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 #include <drm/drm_property.h>
12 static inline bool is_eotf_supported(u8 output_eotf, u8 sink_eotf)
14 return sink_eotf & BIT(output_eotf);
18 * drm_hdmi_infoframe_set_hdr_metadata() - fill an HDMI DRM infoframe with
19 * HDR metadata from userspace
20 * @frame: HDMI DRM infoframe
21 * @conn_state: Connector state containing HDR metadata
23 * Return: 0 on success or a negative error code on failure.
25 int drm_hdmi_infoframe_set_hdr_metadata(struct hdmi_drm_infoframe *frame,
26 const struct drm_connector_state *conn_state)
28 struct drm_connector *connector;
29 struct hdr_output_metadata *hdr_metadata;
32 if (!frame || !conn_state)
35 connector = conn_state->connector;
37 if (!conn_state->hdr_output_metadata)
40 hdr_metadata = conn_state->hdr_output_metadata->data;
42 if (!hdr_metadata || !connector)
45 /* Sink EOTF is Bit map while infoframe is absolute values */
46 if (!is_eotf_supported(hdr_metadata->hdmi_metadata_type1.eotf,
47 connector->hdr_sink_metadata.hdmi_type1.eotf)) {
48 DRM_DEBUG_KMS("EOTF Not Supported\n");
52 err = hdmi_drm_infoframe_init(frame);
56 frame->eotf = hdr_metadata->hdmi_metadata_type1.eotf;
57 frame->metadata_type = hdr_metadata->hdmi_metadata_type1.metadata_type;
59 BUILD_BUG_ON(sizeof(frame->display_primaries) !=
60 sizeof(hdr_metadata->hdmi_metadata_type1.display_primaries));
61 BUILD_BUG_ON(sizeof(frame->white_point) !=
62 sizeof(hdr_metadata->hdmi_metadata_type1.white_point));
64 memcpy(&frame->display_primaries,
65 &hdr_metadata->hdmi_metadata_type1.display_primaries,
66 sizeof(frame->display_primaries));
68 memcpy(&frame->white_point,
69 &hdr_metadata->hdmi_metadata_type1.white_point,
70 sizeof(frame->white_point));
72 frame->max_display_mastering_luminance =
73 hdr_metadata->hdmi_metadata_type1.max_display_mastering_luminance;
74 frame->min_display_mastering_luminance =
75 hdr_metadata->hdmi_metadata_type1.min_display_mastering_luminance;
76 frame->max_fall = hdr_metadata->hdmi_metadata_type1.max_fall;
77 frame->max_cll = hdr_metadata->hdmi_metadata_type1.max_cll;
81 EXPORT_SYMBOL(drm_hdmi_infoframe_set_hdr_metadata);
83 /* HDMI Colorspace Spec Definitions */
84 #define FULL_COLORIMETRY_MASK 0x1FF
85 #define NORMAL_COLORIMETRY_MASK 0x3
86 #define EXTENDED_COLORIMETRY_MASK 0x7
87 #define EXTENDED_ACE_COLORIMETRY_MASK 0xF
89 #define C(x) ((x) << 0)
90 #define EC(x) ((x) << 2)
91 #define ACE(x) ((x) << 5)
93 #define HDMI_COLORIMETRY_NO_DATA 0x0
94 #define HDMI_COLORIMETRY_SMPTE_170M_YCC (C(1) | EC(0) | ACE(0))
95 #define HDMI_COLORIMETRY_BT709_YCC (C(2) | EC(0) | ACE(0))
96 #define HDMI_COLORIMETRY_XVYCC_601 (C(3) | EC(0) | ACE(0))
97 #define HDMI_COLORIMETRY_XVYCC_709 (C(3) | EC(1) | ACE(0))
98 #define HDMI_COLORIMETRY_SYCC_601 (C(3) | EC(2) | ACE(0))
99 #define HDMI_COLORIMETRY_OPYCC_601 (C(3) | EC(3) | ACE(0))
100 #define HDMI_COLORIMETRY_OPRGB (C(3) | EC(4) | ACE(0))
101 #define HDMI_COLORIMETRY_BT2020_CYCC (C(3) | EC(5) | ACE(0))
102 #define HDMI_COLORIMETRY_BT2020_RGB (C(3) | EC(6) | ACE(0))
103 #define HDMI_COLORIMETRY_BT2020_YCC (C(3) | EC(6) | ACE(0))
104 #define HDMI_COLORIMETRY_DCI_P3_RGB_D65 (C(3) | EC(7) | ACE(0))
105 #define HDMI_COLORIMETRY_DCI_P3_RGB_THEATER (C(3) | EC(7) | ACE(1))
107 static const u32 hdmi_colorimetry_val[] = {
108 [DRM_MODE_COLORIMETRY_NO_DATA] = HDMI_COLORIMETRY_NO_DATA,
109 [DRM_MODE_COLORIMETRY_SMPTE_170M_YCC] = HDMI_COLORIMETRY_SMPTE_170M_YCC,
110 [DRM_MODE_COLORIMETRY_BT709_YCC] = HDMI_COLORIMETRY_BT709_YCC,
111 [DRM_MODE_COLORIMETRY_XVYCC_601] = HDMI_COLORIMETRY_XVYCC_601,
112 [DRM_MODE_COLORIMETRY_XVYCC_709] = HDMI_COLORIMETRY_XVYCC_709,
113 [DRM_MODE_COLORIMETRY_SYCC_601] = HDMI_COLORIMETRY_SYCC_601,
114 [DRM_MODE_COLORIMETRY_OPYCC_601] = HDMI_COLORIMETRY_OPYCC_601,
115 [DRM_MODE_COLORIMETRY_OPRGB] = HDMI_COLORIMETRY_OPRGB,
116 [DRM_MODE_COLORIMETRY_BT2020_CYCC] = HDMI_COLORIMETRY_BT2020_CYCC,
117 [DRM_MODE_COLORIMETRY_BT2020_RGB] = HDMI_COLORIMETRY_BT2020_RGB,
118 [DRM_MODE_COLORIMETRY_BT2020_YCC] = HDMI_COLORIMETRY_BT2020_YCC,
126 * drm_hdmi_avi_infoframe_colorimetry() - fill the HDMI AVI infoframe
127 * colorimetry information
128 * @frame: HDMI AVI infoframe
129 * @conn_state: connector state
131 void drm_hdmi_avi_infoframe_colorimetry(struct hdmi_avi_infoframe *frame,
132 const struct drm_connector_state *conn_state)
135 u32 colorimetry_index = conn_state->colorspace & FULL_COLORIMETRY_MASK;
137 if (colorimetry_index >= ARRAY_SIZE(hdmi_colorimetry_val))
138 colorimetry_val = HDMI_COLORIMETRY_NO_DATA;
140 colorimetry_val = hdmi_colorimetry_val[colorimetry_index];
142 frame->colorimetry = colorimetry_val & NORMAL_COLORIMETRY_MASK;
144 * ToDo: Extend it for ACE formats as well. Modify the infoframe
145 * structure and extend it in drivers/video/hdmi
147 frame->extended_colorimetry = (colorimetry_val >> 2) &
148 EXTENDED_COLORIMETRY_MASK;
150 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_colorimetry);
153 * drm_hdmi_avi_infoframe_bars() - fill the HDMI AVI infoframe
155 * @frame: HDMI AVI infoframe
156 * @conn_state: connector state
158 void drm_hdmi_avi_infoframe_bars(struct hdmi_avi_infoframe *frame,
159 const struct drm_connector_state *conn_state)
161 frame->right_bar = conn_state->tv.margins.right;
162 frame->left_bar = conn_state->tv.margins.left;
163 frame->top_bar = conn_state->tv.margins.top;
164 frame->bottom_bar = conn_state->tv.margins.bottom;
166 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_bars);
169 * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
170 * content type information, based
171 * on correspondent DRM property.
172 * @frame: HDMI AVI infoframe
173 * @conn_state: DRM display connector state
176 void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
177 const struct drm_connector_state *conn_state)
179 switch (conn_state->content_type) {
180 case DRM_MODE_CONTENT_TYPE_GRAPHICS:
181 frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
183 case DRM_MODE_CONTENT_TYPE_CINEMA:
184 frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
186 case DRM_MODE_CONTENT_TYPE_GAME:
187 frame->content_type = HDMI_CONTENT_TYPE_GAME;
189 case DRM_MODE_CONTENT_TYPE_PHOTO:
190 frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
193 /* Graphics is the default(0) */
194 frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
197 frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
199 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);