xref: /linux/drivers/gpu/drm/display/drm_hdmi_helper.c (revision aa8a8c7e4641ebcef02dd6fa387ca334857367f4)
1 // SPDX-License-Identifier: MIT
2 
3 #include <linux/export.h>
4 #include <linux/module.h>
5 
6 #include <drm/display/drm_hdmi_helper.h>
7 #include <drm/display/drm_scdc_helper.h>
8 
9 #include <drm/drm_atomic.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_connector.h>
12 #include <drm/drm_crtc.h>
13 #include <drm/drm_edid.h>
14 #include <drm/drm_modes.h>
15 #include <drm/drm_modeset_lock.h>
16 #include <drm/drm_print.h>
17 #include <drm/drm_property.h>
18 
19 #define DRM_HDMI_SCDC_SOURCE_VERSION	1
20 #define DRM_HDMI_SCDC_POLL_DELAY_MS	1000
21 
22 static inline bool is_eotf_supported(u8 output_eotf, u8 sink_eotf)
23 {
24 	return sink_eotf & BIT(output_eotf);
25 }
26 
27 /**
28  * drm_hdmi_infoframe_set_hdr_metadata() - fill an HDMI DRM infoframe with
29  *                                         HDR metadata from userspace
30  * @frame: HDMI DRM infoframe
31  * @conn_state: Connector state containing HDR metadata
32  *
33  * Return: 0 on success or a negative error code on failure.
34  */
35 int drm_hdmi_infoframe_set_hdr_metadata(struct hdmi_drm_infoframe *frame,
36 					const struct drm_connector_state *conn_state)
37 {
38 	struct drm_connector *connector;
39 	struct hdr_output_metadata *hdr_metadata;
40 	int err;
41 
42 	if (!frame || !conn_state)
43 		return -EINVAL;
44 
45 	connector = conn_state->connector;
46 
47 	if (!conn_state->hdr_output_metadata)
48 		return -EINVAL;
49 
50 	hdr_metadata = conn_state->hdr_output_metadata->data;
51 
52 	if (!hdr_metadata || !connector)
53 		return -EINVAL;
54 
55 	/* Sink EOTF is Bit map while infoframe is absolute values */
56 	if (!is_eotf_supported(hdr_metadata->hdmi_metadata_type1.eotf,
57 			       connector->display_info.hdr_sink_metadata.hdmi_type1.eotf))
58 		DRM_DEBUG_KMS("Unknown EOTF %d\n", hdr_metadata->hdmi_metadata_type1.eotf);
59 
60 	err = hdmi_drm_infoframe_init(frame);
61 	if (err < 0)
62 		return err;
63 
64 	frame->eotf = hdr_metadata->hdmi_metadata_type1.eotf;
65 	frame->metadata_type = hdr_metadata->hdmi_metadata_type1.metadata_type;
66 
67 	BUILD_BUG_ON(sizeof(frame->display_primaries) !=
68 		     sizeof(hdr_metadata->hdmi_metadata_type1.display_primaries));
69 	BUILD_BUG_ON(sizeof(frame->white_point) !=
70 		     sizeof(hdr_metadata->hdmi_metadata_type1.white_point));
71 
72 	memcpy(&frame->display_primaries,
73 	       &hdr_metadata->hdmi_metadata_type1.display_primaries,
74 	       sizeof(frame->display_primaries));
75 
76 	memcpy(&frame->white_point,
77 	       &hdr_metadata->hdmi_metadata_type1.white_point,
78 	       sizeof(frame->white_point));
79 
80 	frame->max_display_mastering_luminance =
81 		hdr_metadata->hdmi_metadata_type1.max_display_mastering_luminance;
82 	frame->min_display_mastering_luminance =
83 		hdr_metadata->hdmi_metadata_type1.min_display_mastering_luminance;
84 	frame->max_fall = hdr_metadata->hdmi_metadata_type1.max_fall;
85 	frame->max_cll = hdr_metadata->hdmi_metadata_type1.max_cll;
86 
87 	return 0;
88 }
89 EXPORT_SYMBOL(drm_hdmi_infoframe_set_hdr_metadata);
90 
91 /* HDMI Colorspace Spec Definitions */
92 #define FULL_COLORIMETRY_MASK		0x1FF
93 #define NORMAL_COLORIMETRY_MASK		0x3
94 #define EXTENDED_COLORIMETRY_MASK	0x7
95 #define EXTENDED_ACE_COLORIMETRY_MASK	0xF
96 
97 #define C(x) ((x) << 0)
98 #define EC(x) ((x) << 2)
99 #define ACE(x) ((x) << 5)
100 
101 #define HDMI_COLORIMETRY_NO_DATA		0x0
102 #define HDMI_COLORIMETRY_SMPTE_170M_YCC		(C(1) | EC(0) | ACE(0))
103 #define HDMI_COLORIMETRY_BT709_YCC		(C(2) | EC(0) | ACE(0))
104 #define HDMI_COLORIMETRY_XVYCC_601		(C(3) | EC(0) | ACE(0))
105 #define HDMI_COLORIMETRY_XVYCC_709		(C(3) | EC(1) | ACE(0))
106 #define HDMI_COLORIMETRY_SYCC_601		(C(3) | EC(2) | ACE(0))
107 #define HDMI_COLORIMETRY_OPYCC_601		(C(3) | EC(3) | ACE(0))
108 #define HDMI_COLORIMETRY_OPRGB			(C(3) | EC(4) | ACE(0))
109 #define HDMI_COLORIMETRY_BT2020_CYCC		(C(3) | EC(5) | ACE(0))
110 #define HDMI_COLORIMETRY_BT2020_RGB		(C(3) | EC(6) | ACE(0))
111 #define HDMI_COLORIMETRY_BT2020_YCC		(C(3) | EC(6) | ACE(0))
112 #define HDMI_COLORIMETRY_DCI_P3_RGB_D65		(C(3) | EC(7) | ACE(0))
113 #define HDMI_COLORIMETRY_DCI_P3_RGB_THEATER	(C(3) | EC(7) | ACE(1))
114 
115 static const u32 hdmi_colorimetry_val[] = {
116 	[DRM_MODE_COLORIMETRY_NO_DATA] = HDMI_COLORIMETRY_NO_DATA,
117 	[DRM_MODE_COLORIMETRY_SMPTE_170M_YCC] = HDMI_COLORIMETRY_SMPTE_170M_YCC,
118 	[DRM_MODE_COLORIMETRY_BT709_YCC] = HDMI_COLORIMETRY_BT709_YCC,
119 	[DRM_MODE_COLORIMETRY_XVYCC_601] = HDMI_COLORIMETRY_XVYCC_601,
120 	[DRM_MODE_COLORIMETRY_XVYCC_709] = HDMI_COLORIMETRY_XVYCC_709,
121 	[DRM_MODE_COLORIMETRY_SYCC_601] = HDMI_COLORIMETRY_SYCC_601,
122 	[DRM_MODE_COLORIMETRY_OPYCC_601] = HDMI_COLORIMETRY_OPYCC_601,
123 	[DRM_MODE_COLORIMETRY_OPRGB] = HDMI_COLORIMETRY_OPRGB,
124 	[DRM_MODE_COLORIMETRY_BT2020_CYCC] = HDMI_COLORIMETRY_BT2020_CYCC,
125 	[DRM_MODE_COLORIMETRY_BT2020_RGB] = HDMI_COLORIMETRY_BT2020_RGB,
126 	[DRM_MODE_COLORIMETRY_BT2020_YCC] = HDMI_COLORIMETRY_BT2020_YCC,
127 };
128 
129 #undef C
130 #undef EC
131 #undef ACE
132 
133 /**
134  * drm_hdmi_avi_infoframe_colorimetry() - fill the HDMI AVI infoframe
135  *                                       colorimetry information
136  * @frame: HDMI AVI infoframe
137  * @conn_state: connector state
138  */
139 void drm_hdmi_avi_infoframe_colorimetry(struct hdmi_avi_infoframe *frame,
140 					const struct drm_connector_state *conn_state)
141 {
142 	u32 colorimetry_val;
143 	u32 colorimetry_index = conn_state->colorspace & FULL_COLORIMETRY_MASK;
144 
145 	if (colorimetry_index >= ARRAY_SIZE(hdmi_colorimetry_val))
146 		colorimetry_val = HDMI_COLORIMETRY_NO_DATA;
147 	else
148 		colorimetry_val = hdmi_colorimetry_val[colorimetry_index];
149 
150 	frame->colorimetry = colorimetry_val & NORMAL_COLORIMETRY_MASK;
151 	/*
152 	 * ToDo: Extend it for ACE formats as well. Modify the infoframe
153 	 * structure and extend it in drivers/video/hdmi
154 	 */
155 	frame->extended_colorimetry = (colorimetry_val >> 2) &
156 					EXTENDED_COLORIMETRY_MASK;
157 }
158 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_colorimetry);
159 
160 /**
161  * drm_hdmi_avi_infoframe_bars() - fill the HDMI AVI infoframe
162  *                                 bar information
163  * @frame: HDMI AVI infoframe
164  * @conn_state: connector state
165  */
166 void drm_hdmi_avi_infoframe_bars(struct hdmi_avi_infoframe *frame,
167 				 const struct drm_connector_state *conn_state)
168 {
169 	frame->right_bar = conn_state->tv.margins.right;
170 	frame->left_bar = conn_state->tv.margins.left;
171 	frame->top_bar = conn_state->tv.margins.top;
172 	frame->bottom_bar = conn_state->tv.margins.bottom;
173 }
174 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_bars);
175 
176 /**
177  * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
178  *                                         content type information, based
179  *                                         on correspondent DRM property.
180  * @frame: HDMI AVI infoframe
181  * @conn_state: DRM display connector state
182  *
183  */
184 void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
185 					 const struct drm_connector_state *conn_state)
186 {
187 	switch (conn_state->content_type) {
188 	case DRM_MODE_CONTENT_TYPE_GRAPHICS:
189 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
190 		break;
191 	case DRM_MODE_CONTENT_TYPE_CINEMA:
192 		frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
193 		break;
194 	case DRM_MODE_CONTENT_TYPE_GAME:
195 		frame->content_type = HDMI_CONTENT_TYPE_GAME;
196 		break;
197 	case DRM_MODE_CONTENT_TYPE_PHOTO:
198 		frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
199 		break;
200 	default:
201 		/* Graphics is the default(0) */
202 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
203 	}
204 
205 	frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
206 }
207 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
208 
209 /**
210  * drm_hdmi_compute_mode_clock() - Computes the TMDS Character Rate
211  * @mode: Display mode to compute the clock for
212  * @bpc: Bits per character
213  * @fmt: Output Pixel Format used
214  *
215  * Returns the TMDS Character Rate for a given mode, bpc count and output format.
216  *
217  * RETURNS:
218  * The TMDS Character Rate, in Hertz, or 0 on error.
219  */
220 unsigned long long
221 drm_hdmi_compute_mode_clock(const struct drm_display_mode *mode,
222 			    unsigned int bpc,
223 			    enum drm_output_color_format fmt)
224 {
225 	unsigned long long clock = mode->clock * 1000ULL;
226 	unsigned int vic = drm_match_cea_mode(mode);
227 
228 	/*
229 	 * CTA-861-G Spec, section 5.4 - Color Coding and Quantization
230 	 * mandates that VIC 1 always uses 8 bpc.
231 	 */
232 	if (vic == 1 && bpc != 8)
233 		return 0;
234 
235 	if (fmt == DRM_OUTPUT_COLOR_FORMAT_YCBCR422) {
236 		/*
237 		 * HDMI 1.0 Spec, section 6.5 - Pixel Encoding states that
238 		 * YUV422 sends 24 bits over three channels, with Cb and Cr
239 		 * components being sent on odd and even pixels, respectively.
240 		 *
241 		 * If fewer than 12 bpc are sent, data are left justified.
242 		 */
243 		if (bpc > 12)
244 			return 0;
245 
246 		/*
247 		 * HDMI 1.0 Spec, section 6.5 - Pixel Encoding
248 		 * specifies that YUV422 sends two 12-bits components over
249 		 * three TMDS channels per pixel clock, which is equivalent to
250 		 * three 8-bits components over three channels used by RGB as
251 		 * far as the clock rate goes.
252 		 */
253 		bpc = 8;
254 	}
255 
256 	/*
257 	 * HDMI 2.0 Spec, Section 7.1 - YCbCr 4:2:0 Pixel Encoding
258 	 * specifies that YUV420 encoding is carried at a TMDS Character Rate
259 	 * equal to half the pixel clock rate.
260 	 */
261 	if (fmt == DRM_OUTPUT_COLOR_FORMAT_YCBCR420)
262 		clock = clock / 2;
263 
264 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
265 		clock = clock * 2;
266 
267 	return DIV_ROUND_CLOSEST_ULL(clock * bpc, 8);
268 }
269 EXPORT_SYMBOL(drm_hdmi_compute_mode_clock);
270 
271 struct drm_hdmi_acr_n_cts_entry {
272 	unsigned int n;
273 	unsigned int cts;
274 };
275 
276 struct drm_hdmi_acr_data {
277 	unsigned long tmds_clock_khz;
278 	struct drm_hdmi_acr_n_cts_entry n_cts_32k,
279 					n_cts_44k1,
280 					n_cts_48k;
281 };
282 
283 static const struct drm_hdmi_acr_data hdmi_acr_n_cts[] = {
284 	{
285 		/* "Other" entry */
286 		.n_cts_32k =  { .n = 4096, },
287 		.n_cts_44k1 = { .n = 6272, },
288 		.n_cts_48k =  { .n = 6144, },
289 	}, {
290 		.tmds_clock_khz = 25175,
291 		.n_cts_32k =  { .n = 4576,  .cts = 28125, },
292 		.n_cts_44k1 = { .n = 7007,  .cts = 31250, },
293 		.n_cts_48k =  { .n = 6864,  .cts = 28125, },
294 	}, {
295 		.tmds_clock_khz = 25200,
296 		.n_cts_32k =  { .n = 4096,  .cts = 25200, },
297 		.n_cts_44k1 = { .n = 6272,  .cts = 28000, },
298 		.n_cts_48k =  { .n = 6144,  .cts = 25200, },
299 	}, {
300 		.tmds_clock_khz = 27000,
301 		.n_cts_32k =  { .n = 4096,  .cts = 27000, },
302 		.n_cts_44k1 = { .n = 6272,  .cts = 30000, },
303 		.n_cts_48k =  { .n = 6144,  .cts = 27000, },
304 	}, {
305 		.tmds_clock_khz = 27027,
306 		.n_cts_32k =  { .n = 4096,  .cts = 27027, },
307 		.n_cts_44k1 = { .n = 6272,  .cts = 30030, },
308 		.n_cts_48k =  { .n = 6144,  .cts = 27027, },
309 	}, {
310 		.tmds_clock_khz = 54000,
311 		.n_cts_32k =  { .n = 4096,  .cts = 54000, },
312 		.n_cts_44k1 = { .n = 6272,  .cts = 60000, },
313 		.n_cts_48k =  { .n = 6144,  .cts = 54000, },
314 	}, {
315 		.tmds_clock_khz = 54054,
316 		.n_cts_32k =  { .n = 4096,  .cts = 54054, },
317 		.n_cts_44k1 = { .n = 6272,  .cts = 60060, },
318 		.n_cts_48k =  { .n = 6144,  .cts = 54054, },
319 	}, {
320 		.tmds_clock_khz = 74176,
321 		.n_cts_32k =  { .n = 11648, .cts = 210937, }, /* and 210938 */
322 		.n_cts_44k1 = { .n = 17836, .cts = 234375, },
323 		.n_cts_48k =  { .n = 11648, .cts = 140625, },
324 	}, {
325 		.tmds_clock_khz = 74250,
326 		.n_cts_32k =  { .n = 4096,  .cts = 74250, },
327 		.n_cts_44k1 = { .n = 6272,  .cts = 82500, },
328 		.n_cts_48k =  { .n = 6144,  .cts = 74250, },
329 	}, {
330 		.tmds_clock_khz = 148352,
331 		.n_cts_32k =  { .n = 11648, .cts = 421875, },
332 		.n_cts_44k1 = { .n = 8918,  .cts = 234375, },
333 		.n_cts_48k =  { .n = 5824,  .cts = 140625, },
334 	}, {
335 		.tmds_clock_khz = 148500,
336 		.n_cts_32k =  { .n = 4096,  .cts = 148500, },
337 		.n_cts_44k1 = { .n = 6272,  .cts = 165000, },
338 		.n_cts_48k =  { .n = 6144,  .cts = 148500, },
339 	}, {
340 		.tmds_clock_khz = 296703,
341 		.n_cts_32k =  { .n = 5824,  .cts = 421875, },
342 		.n_cts_44k1 = { .n = 4459,  .cts = 234375, },
343 		.n_cts_48k =  { .n = 5824,  .cts = 281250, },
344 	}, {
345 		.tmds_clock_khz = 297000,
346 		.n_cts_32k =  { .n = 3072,  .cts = 222750, },
347 		.n_cts_44k1 = { .n = 4704,  .cts = 247500, },
348 		.n_cts_48k =  { .n = 5120,  .cts = 247500, },
349 	}, {
350 		.tmds_clock_khz = 593407,
351 		.n_cts_32k =  { .n = 5824,  .cts = 843750, },
352 		.n_cts_44k1 = { .n = 8918,  .cts = 937500, },
353 		.n_cts_48k =  { .n = 5824,  .cts = 562500, },
354 	}, {
355 		.tmds_clock_khz = 594000,
356 		.n_cts_32k =  { .n = 3072,  .cts = 445500, },
357 		.n_cts_44k1 = { .n = 9408,  .cts = 990000, },
358 		.n_cts_48k =  { .n = 6144,  .cts = 594000, },
359 	},
360 };
361 
362 static int drm_hdmi_acr_find_tmds_entry(unsigned long tmds_clock_khz)
363 {
364 	int i;
365 
366 	/* skip the "other" entry */
367 	for (i = 1; i < ARRAY_SIZE(hdmi_acr_n_cts); i++) {
368 		if (hdmi_acr_n_cts[i].tmds_clock_khz == tmds_clock_khz)
369 			return i;
370 	}
371 
372 	return 0;
373 }
374 
375 /**
376  * drm_hdmi_acr_get_n_cts() - get N and CTS values for Audio Clock Regeneration
377  *
378  * @tmds_char_rate: TMDS clock (char rate) as used by the HDMI connector
379  * @sample_rate: audio sample rate
380  * @out_n: a pointer to write the N value
381  * @out_cts: a pointer to write the CTS value
382  *
383  * Get the N and CTS values (either by calculating them or by returning data
384  * from the tables. This follows the HDMI 1.4b Section 7.2 "Audio Sample Clock
385  * Capture and Regeneration".
386  *
387  * Note, @sample_rate corresponds to the Fs value, see sections 7.2.4 - 7.2.6
388  * on how to select Fs for non-L-PCM formats.
389  */
390 void
391 drm_hdmi_acr_get_n_cts(unsigned long long tmds_char_rate,
392 		       unsigned int sample_rate,
393 		       unsigned int *out_n,
394 		       unsigned int *out_cts)
395 {
396 	/* be a bit more tolerant, especially for the 1.001 entries */
397 	unsigned long tmds_clock_khz = DIV_ROUND_CLOSEST_ULL(tmds_char_rate, 1000);
398 	const struct drm_hdmi_acr_n_cts_entry *entry;
399 	unsigned int n, cts, mult;
400 	int tmds_idx;
401 
402 	tmds_idx = drm_hdmi_acr_find_tmds_entry(tmds_clock_khz);
403 
404 	/*
405 	 * Don't change the order, 192 kHz is divisible by 48k and 32k, but it
406 	 * should use 48k entry.
407 	 */
408 	if (sample_rate % 48000 == 0) {
409 		entry = &hdmi_acr_n_cts[tmds_idx].n_cts_48k;
410 		mult = sample_rate / 48000;
411 	} else if (sample_rate % 44100 == 0) {
412 		entry = &hdmi_acr_n_cts[tmds_idx].n_cts_44k1;
413 		mult = sample_rate / 44100;
414 	} else if (sample_rate % 32000 == 0) {
415 		entry = &hdmi_acr_n_cts[tmds_idx].n_cts_32k;
416 		mult = sample_rate / 32000;
417 	} else {
418 		entry = NULL;
419 	}
420 
421 	if (entry) {
422 		n = entry->n * mult;
423 		cts = entry->cts;
424 	} else {
425 		/* Recommended optimal value, HDMI 1.4b, Section 7.2.1 */
426 		n = 128 * sample_rate / 1000;
427 		cts = 0;
428 	}
429 
430 	if (!cts)
431 		cts = DIV_ROUND_CLOSEST_ULL(tmds_char_rate * n,
432 					    128 * sample_rate);
433 
434 	*out_n = n;
435 	*out_cts = cts;
436 }
437 EXPORT_SYMBOL(drm_hdmi_acr_get_n_cts);
438 
439 /**
440  * drm_hdmi_mode_needs_scrambling() - Check if an HDMI mode requires scrambling
441  * @mode: Display mode to check
442  * @bpc: Bits per color component
443  * @fmt: Output pixel format
444  *
445  * Computes the TMDS character rate for the given mode, bits per component and
446  * output format, and checks whether it exceeds the HDMI 1.3 maximum TMDS
447  * character rate of 340 MHz.
448  *
449  * Returns:
450  * true if the mode requires scrambling, false otherwise.
451  */
452 bool drm_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
453 				    unsigned int bpc,
454 				    enum drm_output_color_format fmt)
455 {
456 	unsigned long long tmds_char_rate;
457 
458 	tmds_char_rate = drm_hdmi_compute_mode_clock(mode, bpc, fmt);
459 
460 	return tmds_char_rate > HDMI_1_3_TMDS_CHAR_RATE_MAX_HZ;
461 }
462 EXPORT_SYMBOL(drm_hdmi_mode_needs_scrambling);
463 
464 static bool drm_scdc_sink_supports_scrambling(struct drm_connector *connector)
465 {
466 	const struct drm_display_info *info = &connector->display_info;
467 
468 	return info->is_hdmi &&
469 	       info->hdmi.scdc.supported &&
470 	       info->hdmi.scdc.scrambling.supported;
471 }
472 
473 static int drm_connector_hdmi_try_scrambling_setup(struct drm_connector *connector)
474 {
475 	bool done;
476 
477 	done = drm_scdc_set_high_tmds_clock_ratio(connector, true);
478 	if (!done)
479 		return -EIO;
480 
481 	done = drm_scdc_set_scrambling(connector, true);
482 	if (!done)
483 		return -EIO;
484 
485 	if (READ_ONCE(connector->hdmi.scrambler_enabled))
486 		schedule_delayed_work(&connector->hdmi.scdc_work,
487 				      msecs_to_jiffies(DRM_HDMI_SCDC_POLL_DELAY_MS));
488 
489 	return 0;
490 }
491 
492 static void drm_connector_hdmi_scdc_work(struct work_struct *work)
493 {
494 	struct drm_connector *connector;
495 	struct drm_connector_hdmi *hdmi;
496 
497 	hdmi = container_of(to_delayed_work(work), struct drm_connector_hdmi,
498 			    scdc_work);
499 	connector = container_of(hdmi, struct drm_connector, hdmi);
500 
501 	/*
502 	 * The connector, and hence this work item, may outlive the source
503 	 * driver, e.g. when userspace keeps a DRM file descriptor open past
504 	 * unbind. Never poke the DDC bus after the connector got unregistered,
505 	 * as the underlying I2C adapter may already be gone.
506 	 */
507 	if (drm_connector_is_unregistered(connector))
508 		return;
509 
510 	if (READ_ONCE(connector->hdmi.scrambler_enabled) &&
511 	    !drm_scdc_get_scrambling_status(connector))
512 		drm_connector_hdmi_try_scrambling_setup(connector);
513 }
514 
515 /**
516  * drm_connector_hdmi_enable_scrambling() - enable scrambling and monitor SCDC status
517  * @connector: connector
518  * @conn_state: connector state
519  *
520  * Enables scrambling and high TMDS clock ratio on both source and sink sides.
521  * Additionally, use a delayed work item to monitor the scrambling status on
522  * the sink side and retry the operation, as some displays refuse to set the
523  * scrambling bit right away.
524  *
525  * The work item is cancelled by drm_connector_hdmi_disable_scrambling(), hence
526  * source drivers must ensure the display pipeline is shut down on unbind, e.g.
527  * via drm_atomic_helper_shutdown(). Otherwise the work may outlive the driver's
528  * devres-managed resources, since the connector itself is only cleaned up when
529  * the last reference to the DRM device is dropped.
530  *
531  * Returns:
532  * Zero if scrambling is set successfully, an error code otherwise.
533  */
534 int drm_connector_hdmi_enable_scrambling(struct drm_connector *connector,
535 					 const struct drm_connector_state *conn_state)
536 {
537 	struct drm_connector_hdmi *hdmi = &connector->hdmi;
538 	struct drm_device *dev = connector->dev;
539 	int ret;
540 
541 	if (!conn_state)
542 		return -EINVAL;
543 
544 	if (!conn_state->hdmi.scrambler_needed)
545 		return 0;
546 
547 	if (!drm_connector_hdmi_scrambler_supported(connector)) {
548 		drm_dbg_kms(dev, "Source doesn't support scrambling.\n");
549 		return -EINVAL;
550 	}
551 
552 	if (!drm_scdc_sink_supports_scrambling(connector)) {
553 		drm_dbg_kms(dev, "Sink doesn't support scrambling.\n");
554 		return -EINVAL;
555 	}
556 
557 	if (!connector->ddc)
558 		return -EINVAL;
559 
560 	/*
561 	 * Advertise our SCDC source version. This is purely informational and
562 	 * does not gate scrambling, so failures are non-fatal.
563 	 */
564 	drm_scdc_set_source_version(connector, DRM_HDMI_SCDC_SOURCE_VERSION);
565 
566 	drm_dbg_kms(dev, "Enabling scrambling\n");
567 
568 	if (!hdmi->scdc_work_initialized) {
569 		INIT_DELAYED_WORK(&hdmi->scdc_work,
570 				  drm_connector_hdmi_scdc_work);
571 		hdmi->scdc_work_initialized = true;
572 	}
573 
574 	WRITE_ONCE(hdmi->scrambler_enabled, true);
575 
576 	ret = drm_connector_hdmi_try_scrambling_setup(connector);
577 	if (!ret)
578 		ret = hdmi->funcs->scrambler_enable(connector);
579 
580 	if (ret) {
581 		WRITE_ONCE(hdmi->scrambler_enabled, false);
582 		cancel_delayed_work_sync(&hdmi->scdc_work);
583 
584 		drm_scdc_set_scrambling(connector, false);
585 		drm_scdc_set_high_tmds_clock_ratio(connector, false);
586 	}
587 
588 	return ret;
589 }
590 EXPORT_SYMBOL(drm_connector_hdmi_enable_scrambling);
591 
592 /**
593  * drm_connector_hdmi_disable_scrambling() - disable scrambling and SCDC monitoring
594  * @connector: connector
595  *
596  * Disables scrambling and high TMDS clock ratio on both source and sink sides.
597  * Also cancels the SCDC status monitoring work item, if it is still pending.
598  *
599  * Returns:
600  * Zero if scrambling is reset successfully, an error code otherwise.
601  */
602 int drm_connector_hdmi_disable_scrambling(struct drm_connector *connector)
603 {
604 	struct drm_connector_hdmi *hdmi = &connector->hdmi;
605 	struct drm_device *dev = connector->dev;
606 
607 	if (!READ_ONCE(hdmi->scrambler_enabled))
608 		return 0;
609 
610 	drm_dbg_kms(dev, "Disabling scrambling\n");
611 
612 	WRITE_ONCE(hdmi->scrambler_enabled, false);
613 
614 	/*
615 	 * A driver may force scrambler_enabled at init to trigger a disable
616 	 * at boot (e.g. vc4), bypassing the enable path that sets up the work
617 	 * item. Only cancel it once it has actually been initialized.
618 	 */
619 	if (hdmi->scdc_work_initialized)
620 		cancel_delayed_work_sync(&hdmi->scdc_work);
621 
622 	if (connector->ddc &&
623 	    connector->status == connector_status_connected &&
624 	    drm_scdc_sink_supports_scrambling(connector)) {
625 		drm_scdc_set_scrambling(connector, false);
626 		drm_scdc_set_high_tmds_clock_ratio(connector, false);
627 	}
628 
629 	return hdmi->funcs->scrambler_disable(connector);
630 }
631 EXPORT_SYMBOL(drm_connector_hdmi_disable_scrambling);
632 
633 /**
634  * drm_connector_hdmi_sync_scdc() - resync the sink-side SCDC upon reconnect
635  * @connector: connector
636  * @plugged: connector plugged status event
637  * @ctx: initialized lock acquisition context
638  *
639  * When receiving hotplug disconnect/reconnect event, while the display is
640  * still active (CRTC enabled), the SCDC status on the sink side is reset
641  * and must be explicitly restored.
642  *
643  * The typical solution for this is to trigger an empty modeset in
644  * drm_connector_helper_funcs.detect_ctx(), which is what this helper does
645  * by triggering a CRTC reset on reconnection.
646  *
647  * When making use of the HDMI connector framework, this is automatically
648  * triggered via drm_atomic_helper_connector_hdmi_hotplug().
649  *
650  * Returns:
651  * Zero on success, an error code otherwise, including -EDEADLK.
652  */
653 int drm_connector_hdmi_sync_scdc(struct drm_connector *connector, bool plugged,
654 				 struct drm_modeset_acquire_ctx *ctx)
655 {
656 	struct drm_connector_hdmi *hdmi = &connector->hdmi;
657 	struct drm_device *dev = connector->dev;
658 	struct drm_connector_state *conn_state;
659 	struct drm_crtc *crtc;
660 	u8 config;
661 	int ret;
662 
663 	if (!connector || !plugged || !ctx)
664 		return 0;
665 
666 	/*
667 	 * This is normally part of .detect_ctx() call path, which already holds
668 	 * connection_mutex through @ctx.  However, re-acquiring it with the
669 	 * same context is a no-op and makes the helper safe under any caller.
670 	 */
671 	ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, ctx);
672 	if (ret)
673 		return ret;
674 
675 	/* TODO: Also handle HDMI 2.1 FRL link training */
676 
677 	if (!drm_connector_hdmi_scrambler_supported(connector) ||
678 	    !READ_ONCE(hdmi->scrambler_enabled))
679 		return 0;
680 
681 	conn_state = connector->state;
682 	if (!conn_state)
683 		return 0;
684 
685 	crtc = conn_state->crtc;
686 	if (!crtc)
687 		return 0;
688 
689 	ret = drm_modeset_lock(&crtc->mutex, ctx);
690 	if (ret)
691 		return ret;
692 
693 	if (!crtc->state || !crtc->state->active)
694 		return 0;
695 
696 	if (!connector->ddc || !drm_scdc_sink_supports_scrambling(connector))
697 		return 0;
698 
699 	if (conn_state->commit &&
700 	    !try_wait_for_completion(&conn_state->commit->hw_done))
701 		return 0;
702 
703 	ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
704 	if (ret) {
705 		drm_dbg_kms(dev, "Failed to read TMDS config: %d\n", ret);
706 		return ret;
707 	}
708 
709 	if (config & SCDC_SCRAMBLING_ENABLE)
710 		return 0;
711 
712 	/*
713 	 * HDMI 2.0 requires that one should not send scrambled data prior
714 	 * to configuring the sink scrambling, and that TMDS clock/data
715 	 * transmission should be suspended when changing the TMDS clock rate
716 	 * in the sink.  So let's just do a full modeset here, even though some
717 	 * sinks would be perfectly happy if were to just reconfigure the SCDC
718 	 * settings on the fly.
719 	 *
720 	 * The disable/re-enable cycle triggered by the reset should call
721 	 * drm_connector_hdmi_enable_scrambling() during re-enable, thus
722 	 * properly configuring the sink before data transmission resumes.
723 	 */
724 	drm_dbg_kms(dev, "Resetting CRTC to restore SCDC status\n");
725 
726 	ret = drm_atomic_helper_reset_crtc(crtc, ctx);
727 	if (ret && ret != -EDEADLK)
728 		drm_dbg_kms(dev, "Failed to reset CRTC: %d\n", ret);
729 
730 	return ret;
731 }
732 EXPORT_SYMBOL(drm_connector_hdmi_sync_scdc);
733