xref: /linux/drivers/gpu/drm/display/drm_hdmi_helper.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 // SPDX-License-Identifier: MIT
2 
3 #include <linux/module.h>
4 
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>
11 
12 static inline bool is_eotf_supported(u8 output_eotf, u8 sink_eotf)
13 {
14 	return sink_eotf & BIT(output_eotf);
15 }
16 
17 /**
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
22  *
23  * Return: 0 on success or a negative error code on failure.
24  */
25 int drm_hdmi_infoframe_set_hdr_metadata(struct hdmi_drm_infoframe *frame,
26 					const struct drm_connector_state *conn_state)
27 {
28 	struct drm_connector *connector;
29 	struct hdr_output_metadata *hdr_metadata;
30 	int err;
31 
32 	if (!frame || !conn_state)
33 		return -EINVAL;
34 
35 	connector = conn_state->connector;
36 
37 	if (!conn_state->hdr_output_metadata)
38 		return -EINVAL;
39 
40 	hdr_metadata = conn_state->hdr_output_metadata->data;
41 
42 	if (!hdr_metadata || !connector)
43 		return -EINVAL;
44 
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");
49 		return -EINVAL;
50 	}
51 
52 	err = hdmi_drm_infoframe_init(frame);
53 	if (err < 0)
54 		return err;
55 
56 	frame->eotf = hdr_metadata->hdmi_metadata_type1.eotf;
57 	frame->metadata_type = hdr_metadata->hdmi_metadata_type1.metadata_type;
58 
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));
63 
64 	memcpy(&frame->display_primaries,
65 	       &hdr_metadata->hdmi_metadata_type1.display_primaries,
66 	       sizeof(frame->display_primaries));
67 
68 	memcpy(&frame->white_point,
69 	       &hdr_metadata->hdmi_metadata_type1.white_point,
70 	       sizeof(frame->white_point));
71 
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;
78 
79 	return 0;
80 }
81 EXPORT_SYMBOL(drm_hdmi_infoframe_set_hdr_metadata);
82 
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
88 
89 #define C(x) ((x) << 0)
90 #define EC(x) ((x) << 2)
91 #define ACE(x) ((x) << 5)
92 
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))
106 
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,
119 };
120 
121 #undef C
122 #undef EC
123 #undef ACE
124 
125 /**
126  * drm_hdmi_avi_infoframe_colorimetry() - fill the HDMI AVI infoframe
127  *                                       colorimetry information
128  * @frame: HDMI AVI infoframe
129  * @conn_state: connector state
130  */
131 void drm_hdmi_avi_infoframe_colorimetry(struct hdmi_avi_infoframe *frame,
132 					const struct drm_connector_state *conn_state)
133 {
134 	u32 colorimetry_val;
135 	u32 colorimetry_index = conn_state->colorspace & FULL_COLORIMETRY_MASK;
136 
137 	if (colorimetry_index >= ARRAY_SIZE(hdmi_colorimetry_val))
138 		colorimetry_val = HDMI_COLORIMETRY_NO_DATA;
139 	else
140 		colorimetry_val = hdmi_colorimetry_val[colorimetry_index];
141 
142 	frame->colorimetry = colorimetry_val & NORMAL_COLORIMETRY_MASK;
143 	/*
144 	 * ToDo: Extend it for ACE formats as well. Modify the infoframe
145 	 * structure and extend it in drivers/video/hdmi
146 	 */
147 	frame->extended_colorimetry = (colorimetry_val >> 2) &
148 					EXTENDED_COLORIMETRY_MASK;
149 }
150 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_colorimetry);
151 
152 /**
153  * drm_hdmi_avi_infoframe_bars() - fill the HDMI AVI infoframe
154  *                                 bar information
155  * @frame: HDMI AVI infoframe
156  * @conn_state: connector state
157  */
158 void drm_hdmi_avi_infoframe_bars(struct hdmi_avi_infoframe *frame,
159 				 const struct drm_connector_state *conn_state)
160 {
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;
165 }
166 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_bars);
167 
168 /**
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
174  *
175  */
176 void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
177 					 const struct drm_connector_state *conn_state)
178 {
179 	switch (conn_state->content_type) {
180 	case DRM_MODE_CONTENT_TYPE_GRAPHICS:
181 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
182 		break;
183 	case DRM_MODE_CONTENT_TYPE_CINEMA:
184 		frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
185 		break;
186 	case DRM_MODE_CONTENT_TYPE_GAME:
187 		frame->content_type = HDMI_CONTENT_TYPE_GAME;
188 		break;
189 	case DRM_MODE_CONTENT_TYPE_PHOTO:
190 		frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
191 		break;
192 	default:
193 		/* Graphics is the default(0) */
194 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
195 	}
196 
197 	frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
198 }
199 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
200