1 From a2e09555e32742320fdaa511e1ccf6aafbf29485 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Mon, 7 Mar 2022 15:19:38 +0000
4 Subject: [PATCH] drm/vc4: hdmi: Add CSC for BT601/709/2020 limited and
7 The HVS always composes in the RGB domain, but there is a colourspace
8 conversion block on the output to allow for sending YCbCr over the
10 The colourspace on that link is configurable via the "Colorspace"
11 property on the connector, and that updates the infoframes. There
12 is also selection of limited or full range based on the mode selected
15 Add code to update the CSC as well so that the metadata matches the
18 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
20 drivers/gpu/drm/vc4/vc4_hdmi.c | 198 ++++++++++++++++++++++++---------
21 1 file changed, 146 insertions(+), 52 deletions(-)
23 --- a/drivers/gpu/drm/vc4/vc4_hdmi.c
24 +++ b/drivers/gpu/drm/vc4/vc4_hdmi.c
25 @@ -139,8 +139,8 @@ static bool vc4_hdmi_mode_needs_scrambli
26 return clock > HDMI_14_MAX_TMDS_CLK;
29 -static bool vc4_hdmi_is_full_range_rgb(struct vc4_hdmi *vc4_hdmi,
30 - const struct drm_display_mode *mode)
31 +static bool vc4_hdmi_is_full_range(struct vc4_hdmi *vc4_hdmi,
32 + const struct drm_display_mode *mode)
34 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
36 @@ -680,7 +680,7 @@ static void vc4_hdmi_set_avi_infoframe(s
38 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
40 - vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode) ?
41 + vc4_hdmi_is_full_range(vc4_hdmi, mode) ?
42 HDMI_QUANTIZATION_RANGE_FULL :
43 HDMI_QUANTIZATION_RANGE_LIMITED);
44 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
45 @@ -914,7 +914,7 @@ static void vc4_hdmi_csc_setup(struct vc
46 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
47 VC4_HD_CSC_CTL_ORDER);
49 - if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode)) {
50 + if (!vc4_hdmi_is_full_range(vc4_hdmi, mode)) {
51 /* CEA VICs other than #1 requre limited range RGB
52 * output unless overridden by an AVI infoframe.
53 * Apply a colorspace conversion to squash 0-255 down
54 @@ -944,7 +944,6 @@ static void vc4_hdmi_csc_setup(struct vc
55 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
60 * If we need to output Full Range RGB, then use the unity matrix
62 @@ -952,15 +951,6 @@ static void vc4_hdmi_csc_setup(struct vc
66 - * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
68 -static const u16 vc5_hdmi_csc_full_rgb_unity[3][4] = {
69 - { 0x2000, 0x0000, 0x0000, 0x0000 },
70 - { 0x0000, 0x2000, 0x0000, 0x0000 },
71 - { 0x0000, 0x0000, 0x2000, 0x0000 },
75 * CEA VICs other than #1 require limited range RGB output unless
76 * overridden by an AVI infoframe. Apply a colorspace conversion to
77 * squash 0-255 down to 16-235. The matrix here is:
78 @@ -971,42 +961,105 @@ static const u16 vc5_hdmi_csc_full_rgb_u
80 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
82 -static const u16 vc5_hdmi_csc_full_rgb_to_limited_rgb[3][4] = {
83 - { 0x1b80, 0x0000, 0x0000, 0x0400 },
84 - { 0x0000, 0x1b80, 0x0000, 0x0400 },
85 - { 0x0000, 0x0000, 0x1b80, 0x0400 },
86 +static const u16 vc5_hdmi_csc_full_rgb_to_rgb[2][3][4] = {
88 + /* Full range - unity */
89 + { 0x2000, 0x0000, 0x0000, 0x0000 },
90 + { 0x0000, 0x2000, 0x0000, 0x0000 },
91 + { 0x0000, 0x0000, 0x2000, 0x0000 },
94 + { 0x1b80, 0x0000, 0x0000, 0x0400 },
95 + { 0x0000, 0x1b80, 0x0000, 0x0400 },
96 + { 0x0000, 0x0000, 0x1b80, 0x0400 },
101 + * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
104 + * [ 0.299000 0.587000 0.114000 0.000000 ]
105 + * [ -0.168736 -0.331264 0.500000 128.000000 ]
106 + * [ 0.500000 -0.418688 -0.081312 128.000000 ]
109 + * [ 0.255785 0.502160 0.097523 16.000000 ]
110 + * [ -0.147644 -0.289856 0.437500 128.000000 ]
111 + * [ 0.437500 -0.366352 -0.071148 128.000000 ]
113 + * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
115 +static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = {
118 + { 0x0991, 0x12c9, 0x03a6, 0x0000 },
119 + { 0xfa9b, 0xf567, 0x1000, 0x2000 },
120 + { 0x1000, 0xf29b, 0xfd67, 0x2000 },
122 + /* Limited range */
123 + { 0x082f, 0x1012, 0x031f, 0x0400 },
124 + { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
125 + { 0x0e00, 0xf448, 0xfdba, 0x2000 },
130 - * Conversion between Full Range RGB and Full Range YUV422 using the
131 - * BT.709 Colorspace
132 + * Conversion between Full Range RGB and YUV using the BT.709 Colorspace
135 + * [ 0.212600 0.715200 0.072200 0.000000 ]
136 + * [ -0.114572 -0.385428 0.500000 128.000000 ]
137 + * [ 0.500000 -0.454153 -0.045847 128.000000 ]
139 - * [ 0.212639 0.715169 0.072192 0 ]
140 - * [ -0.117208 -0.394207 0.511416 128 ]
141 - * [ 0.511416 -0.464524 -0.046891 128 ]
143 + * [ 0.181873 0.611831 0.061765 16.000000 ]
144 + * [ -0.100251 -0.337249 0.437500 128.000000 ]
145 + * [ 0.437500 -0.397384 -0.040116 128.000000 ]
147 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
149 -static const u16 vc5_hdmi_csc_full_rgb_to_full_yuv422_bt709[3][4] = {
150 - { 0x06ce, 0x16e3, 0x024f, 0x0000 },
151 - { 0xfc41, 0xf364, 0x105e, 0x2000 },
152 - { 0x105e, 0xf124, 0xfe81, 0x2000 },
153 +static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709[2][3][4] = {
156 + { 0x06ce, 0x16e3, 0x024f, 0x0000 },
157 + { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
158 + { 0x1000, 0xf179, 0xfe89, 0x2000 },
160 + /* Limited range */
161 + { 0x05d2, 0x1394, 0x01fa, 0x0400 },
162 + { 0xfccc, 0xf536, 0x0e00, 0x2000 },
163 + { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
168 - * Conversion between Full Range RGB and Full Range YUV444 using the
169 - * BT.709 Colorspace
170 + * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace
173 + * [ 0.262700 0.678000 0.059300 0.000000 ]
174 + * [ -0.139630 -0.360370 0.500000 128.000000 ]
175 + * [ 0.500000 -0.459786 -0.040214 128.000000 ]
177 - * [ -0.117208 -0.394207 0.511416 128 ]
178 - * [ 0.511416 -0.464524 -0.046891 128 ]
179 - * [ 0.212639 0.715169 0.072192 0 ]
181 + * [ 0.224732 0.580008 0.050729 16.000000 ]
182 + * [ -0.122176 -0.315324 0.437500 128.000000 ]
183 + * [ 0.437500 -0.402312 -0.035188 128.000000 ]
185 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
187 -static const u16 vc5_hdmi_csc_full_rgb_to_full_yuv444_bt709[3][4] = {
188 - { 0xfc41, 0xf364, 0x105e, 0x2000 },
189 - { 0x105e, 0xf124, 0xfe81, 0x2000 },
190 - { 0x06ce, 0x16e3, 0x024f, 0x0000 },
191 +static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020[2][3][4] = {
194 + { 0x0868, 0x15b2, 0x01e6, 0x0000 },
195 + { 0xfb89, 0xf479, 0x1000, 0x2000 },
196 + { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
198 + /* Limited range */
199 + { 0x0731, 0x128f, 0x01a0, 0x0400 },
200 + { 0xfc18, 0xf5ea, 0x0e00, 0x2000 },
201 + { 0x0e00, 0xf321, 0xfee1, 0x2000 },
205 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
206 @@ -1022,12 +1075,28 @@ static void vc5_hdmi_set_csc_coeffs(stru
207 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
210 +static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi,
211 + const u16 coeffs[3][4])
213 + lockdep_assert_held(&vc4_hdmi->hw_lock);
215 + /* YUV444 needs the CSC matrices using the channels in a different order */
216 + HDMI_WRITE(HDMI_CSC_12_11, (coeffs[2][1] << 16) | coeffs[2][0]);
217 + HDMI_WRITE(HDMI_CSC_14_13, (coeffs[2][3] << 16) | coeffs[2][2]);
218 + HDMI_WRITE(HDMI_CSC_22_21, (coeffs[0][1] << 16) | coeffs[0][0]);
219 + HDMI_WRITE(HDMI_CSC_24_23, (coeffs[0][3] << 16) | coeffs[0][2]);
220 + HDMI_WRITE(HDMI_CSC_32_31, (coeffs[1][1] << 16) | coeffs[1][0]);
221 + HDMI_WRITE(HDMI_CSC_34_33, (coeffs[1][3] << 16) | coeffs[1][2]);
224 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
225 struct drm_connector_state *state,
226 const struct drm_display_mode *mode)
228 struct vc4_hdmi_connector_state *vc4_state =
229 conn_state_to_vc4_hdmi_conn_state(state);
230 + unsigned int lim_range = vc4_hdmi_is_full_range(vc4_hdmi, mode) ? 0 : 1;
231 + const u16 (*csc)[4];
234 u32 if_xbar = 0x543210;
235 @@ -1039,31 +1108,56 @@ static void vc5_hdmi_csc_setup(struct vc
237 switch (vc4_state->output_format) {
238 case VC4_HDMI_OUTPUT_YUV444:
239 - vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_full_yuv444_bt709);
242 case VC4_HDMI_OUTPUT_YUV422:
243 - csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
244 - VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
245 - VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
246 - VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
248 - csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
249 - VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
250 + switch (state->colorspace) {
252 + case DRM_MODE_COLORIMETRY_NO_DATA:
253 + case DRM_MODE_COLORIMETRY_BT709_YCC:
254 + case DRM_MODE_COLORIMETRY_XVYCC_709:
255 + case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED:
256 + case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT:
257 + csc = vc5_hdmi_csc_full_rgb_to_yuv_bt709[lim_range];
259 + case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC:
260 + case DRM_MODE_COLORIMETRY_XVYCC_601:
261 + case DRM_MODE_COLORIMETRY_SYCC_601:
262 + case DRM_MODE_COLORIMETRY_OPYCC_601:
263 + case DRM_MODE_COLORIMETRY_BT601_YCC:
264 + csc = vc5_hdmi_csc_full_rgb_to_yuv_bt601[lim_range];
266 + case DRM_MODE_COLORIMETRY_BT2020_CYCC:
267 + case DRM_MODE_COLORIMETRY_BT2020_YCC:
268 + case DRM_MODE_COLORIMETRY_BT2020_RGB:
269 + case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
270 + case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
271 + csc = vc5_hdmi_csc_full_rgb_to_yuv_bt2020[lim_range];
275 - if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
276 - VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
277 + if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
278 + csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
279 + VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
280 + VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
281 + VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
283 + csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
284 + VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
286 + if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
287 + VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
289 + vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc);
291 + vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc);
294 - vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_full_yuv422_bt709);
297 case VC4_HDMI_OUTPUT_RGB:
300 - if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode))
301 - vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_rgb);
303 - vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_unity);
304 + vc5_hdmi_set_csc_coeffs(vc4_hdmi,
305 + vc5_hdmi_csc_full_rgb_to_rgb[lim_range]);