dbfc1520c1a7ab9f421e122dabe0ff8c2d97a3f8
[openwrt/openwrt.git] /
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
5 full range output
6
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
9 HDMI interface.
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
13 or an override.
14
15 Add code to update the CSC as well so that the metadata matches the
16 image data.
17
18 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
19 ---
20 drivers/gpu/drm/vc4/vc4_hdmi.c | 198 ++++++++++++++++++++++++---------
21 1 file changed, 146 insertions(+), 52 deletions(-)
22
23 --- a/drivers/gpu/drm/vc4/vc4_hdmi.c
24 +++ b/drivers/gpu/drm/vc4/vc4_hdmi.c
25 @@ -138,8 +138,8 @@ static bool vc4_hdmi_mode_needs_scrambli
26 return clock > HDMI_14_MAX_TMDS_CLK;
27 }
28
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)
33 {
34 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
35
36 @@ -679,7 +679,7 @@ static void vc4_hdmi_set_avi_infoframe(s
37
38 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
39 connector, mode,
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 @@ -913,7 +913,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);
48
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 @@ -943,7 +943,6 @@ static void vc4_hdmi_csc_setup(struct vc
55 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
56 }
57
58 -
59 /*
60 * If we need to output Full Range RGB, then use the unity matrix
61 *
62 @@ -951,15 +950,6 @@ static void vc4_hdmi_csc_setup(struct vc
63 * [ 0 1 0 0]
64 * [ 0 0 1 0]
65 *
66 - * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
67 - */
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 },
72 -};
73 -
74 -/*
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 @@ -970,42 +960,105 @@ static const u16 vc5_hdmi_csc_full_rgb_u
79 *
80 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
81 */
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] = {
87 + {
88 + /* Full range - unity */
89 + { 0x2000, 0x0000, 0x0000, 0x0000 },
90 + { 0x0000, 0x2000, 0x0000, 0x0000 },
91 + { 0x0000, 0x0000, 0x2000, 0x0000 },
92 + }, {
93 + /* Limited range */
94 + { 0x1b80, 0x0000, 0x0000, 0x0400 },
95 + { 0x0000, 0x1b80, 0x0000, 0x0400 },
96 + { 0x0000, 0x0000, 0x1b80, 0x0400 },
97 + }
98 +};
99 +
100 +/*
101 + * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
102 + *
103 + * Full range
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 ]
107 + *
108 + * Limited range
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 ]
112 + *
113 + * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
114 + */
115 +static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = {
116 + {
117 + /* Full range */
118 + { 0x0991, 0x12c9, 0x03a6, 0x0000 },
119 + { 0xfa9b, 0xf567, 0x1000, 0x2000 },
120 + { 0x1000, 0xf29b, 0xfd67, 0x2000 },
121 + }, {
122 + /* Limited range */
123 + { 0x082f, 0x1012, 0x031f, 0x0400 },
124 + { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
125 + { 0x0e00, 0xf448, 0xfdba, 0x2000 },
126 + }
127 };
128
129 /*
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
133 + *
134 + * Full range
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 ]
138 *
139 - * [ 0.212639 0.715169 0.072192 0 ]
140 - * [ -0.117208 -0.394207 0.511416 128 ]
141 - * [ 0.511416 -0.464524 -0.046891 128 ]
142 + * Limited range
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 ]
146 *
147 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
148 */
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] = {
154 + {
155 + /* Full range */
156 + { 0x06ce, 0x16e3, 0x024f, 0x0000 },
157 + { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
158 + { 0x1000, 0xf179, 0xfe89, 0x2000 },
159 + }, {
160 + /* Limited range */
161 + { 0x05d2, 0x1394, 0x01fa, 0x0400 },
162 + { 0xfccc, 0xf536, 0x0e00, 0x2000 },
163 + { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
164 + }
165 };
166
167 /*
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
171 + *
172 + * Full range
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 ]
176 *
177 - * [ -0.117208 -0.394207 0.511416 128 ]
178 - * [ 0.511416 -0.464524 -0.046891 128 ]
179 - * [ 0.212639 0.715169 0.072192 0 ]
180 + * Limited range
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 ]
184 *
185 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
186 */
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] = {
192 + {
193 + /* Full range */
194 + { 0x0868, 0x15b2, 0x01e6, 0x0000 },
195 + { 0xfb89, 0xf479, 0x1000, 0x2000 },
196 + { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
197 + }, {
198 + /* Limited range */
199 + { 0x0731, 0x128f, 0x01a0, 0x0400 },
200 + { 0xfc18, 0xf5ea, 0x0e00, 0x2000 },
201 + { 0x0e00, 0xf321, 0xfee1, 0x2000 },
202 + }
203 };
204
205 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
206 @@ -1021,12 +1074,28 @@ static void vc5_hdmi_set_csc_coeffs(stru
207 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
208 }
209
210 +static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi,
211 + const u16 coeffs[3][4])
212 +{
213 + lockdep_assert_held(&vc4_hdmi->hw_lock);
214 +
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]);
222 +}
223 +
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)
227 {
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];
232 unsigned long flags;
233 u32 if_cfg = 0;
234 u32 if_xbar = 0x543210;
235 @@ -1038,31 +1107,56 @@ static void vc5_hdmi_csc_setup(struct vc
236
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);
240 - break;
241 -
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;
247 -
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) {
251 + default:
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];
258 + break;
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];
265 + break;
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];
272 + break;
273 + }
274
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;
282 +
283 + csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
284 + VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
285 +
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);
288 +
289 + vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc);
290 + } else {
291 + vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc);
292 + }
293
294 - vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_full_yuv422_bt709);
295 break;
296
297 case VC4_HDMI_OUTPUT_RGB:
298 if_xbar = 0x354021;
299
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);
302 - else
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]);
306 break;
307
308 default: