xref: /linux/drivers/gpu/drm/display/drm_hdmi_state_helper.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: MIT
2 
3 #include <linux/export.h>
4 
5 #include <drm/drm_atomic.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 
11 #include <drm/display/drm_hdmi_audio_helper.h>
12 #include <drm/display/drm_hdmi_cec_helper.h>
13 #include <drm/display/drm_hdmi_helper.h>
14 #include <drm/display/drm_hdmi_state_helper.h>
15 
16 /**
17  * DOC: hdmi helpers
18  *
19  * These functions contain an implementation of the HDMI specification
20  * in the form of KMS helpers.
21  *
22  * It contains TMDS character rate computation, automatic selection of
23  * output formats, infoframes generation, etc.
24  *
25  * Infoframes Compliance
26  * ~~~~~~~~~~~~~~~~~~~~~
27  *
28  * Drivers using the helpers will expose the various infoframes
29  * generated according to the HDMI specification in debugfs.
30  *
31  * Compliance can then be tested using ``edid-decode`` from the ``v4l-utils`` project
32  * (https://git.linuxtv.org/v4l-utils.git/). A sample run would look like:
33  *
34  * .. code-block:: bash
35  *
36  *	# edid-decode \
37  *		-I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/audio \
38  *		-I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/avi \
39  *		-I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdmi \
40  *		-I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdr_drm \
41  *		-I /sys/kernel/debug/dri/1/HDMI-A-1/infoframes/spd \
42  *		/sys/class/drm/card1-HDMI-A-1/edid \
43  *		-c
44  *
45  *	edid-decode (hex):
46  *
47  *	00 ff ff ff ff ff ff 00 1e 6d f4 5b 1e ef 06 00
48  *	07 20 01 03 80 2f 34 78 ea 24 05 af 4f 42 ab 25
49  *	0f 50 54 21 08 00 d1 c0 61 40 45 40 01 01 01 01
50  *	01 01 01 01 01 01 98 d0 00 40 a1 40 d4 b0 30 20
51  *	3a 00 d1 0b 12 00 00 1a 00 00 00 fd 00 3b 3d 1e
52  *	b2 31 00 0a 20 20 20 20 20 20 00 00 00 fc 00 4c
53  *	47 20 53 44 51 48 44 0a 20 20 20 20 00 00 00 ff
54  *	00 32 30 37 4e 54 52 4c 44 43 34 33 30 0a 01 46
55  *
56  *	02 03 42 72 23 09 07 07 4d 01 03 04 90 12 13 1f
57  *	22 5d 5e 5f 60 61 83 01 00 00 6d 03 0c 00 10 00
58  *	b8 3c 20 00 60 01 02 03 67 d8 5d c4 01 78 80 03
59  *	e3 0f 00 18 e2 00 6a e3 05 c0 00 e6 06 05 01 52
60  *	52 51 11 5d 00 a0 a0 40 29 b0 30 20 3a 00 d1 0b
61  *	12 00 00 1a 00 00 00 00 00 00 00 00 00 00 00 00
62  *	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
63  *	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c3
64  *
65  *	----------------
66  *
67  *	Block 0, Base EDID:
68  *	  EDID Structure Version & Revision: 1.3
69  *	  Vendor & Product Identification:
70  *	    Manufacturer: GSM
71  *	    Model: 23540
72  *	    Serial Number: 454430 (0x0006ef1e)
73  *	    Made in: week 7 of 2022
74  *	  Basic Display Parameters & Features:
75  *	    Digital display
76  *	    Maximum image size: 47 cm x 52 cm
77  *	    Gamma: 2.20
78  *	    DPMS levels: Standby Suspend Off
79  *	    RGB color display
80  *	    First detailed timing is the preferred timing
81  *	  Color Characteristics:
82  *	    Red  : 0.6835, 0.3105
83  *	    Green: 0.2587, 0.6679
84  *	    Blue : 0.1445, 0.0585
85  *	    White: 0.3134, 0.3291
86  *	  Established Timings I & II:
87  *	    DMT 0x04:   640x480    59.940476 Hz   4:3     31.469 kHz     25.175000 MHz
88  *	    DMT 0x09:   800x600    60.316541 Hz   4:3     37.879 kHz     40.000000 MHz
89  *	    DMT 0x10:  1024x768    60.003840 Hz   4:3     48.363 kHz     65.000000 MHz
90  *	  Standard Timings:
91  *	    DMT 0x52:  1920x1080   60.000000 Hz  16:9     67.500 kHz    148.500000 MHz
92  *	    DMT 0x10:  1024x768    60.003840 Hz   4:3     48.363 kHz     65.000000 MHz
93  *	    DMT 0x09:   800x600    60.316541 Hz   4:3     37.879 kHz     40.000000 MHz
94  *	  Detailed Timing Descriptors:
95  *	    DTD 1:  2560x2880   59.966580 Hz   8:9    185.417 kHz    534.000000 MHz (465 mm x 523 mm)
96  *	                 Hfront   48 Hsync  32 Hback  240 Hpol P
97  *	                 Vfront    3 Vsync  10 Vback  199 Vpol N
98  *	    Display Range Limits:
99  *	      Monitor ranges (GTF): 59-61 Hz V, 30-178 kHz H, max dotclock 490 MHz
100  *	    Display Product Name: 'LG SDQHD'
101  *	    Display Product Serial Number: '207NTRLDC430'
102  *	  Extension blocks: 1
103  *	Checksum: 0x46
104  *
105  *	----------------
106  *
107  *	Block 1, CTA-861 Extension Block:
108  *	  Revision: 3
109  *	  Basic audio support
110  *	  Supports YCbCr 4:4:4
111  *	  Supports YCbCr 4:2:2
112  *	  Native detailed modes: 2
113  *	  Audio Data Block:
114  *	    Linear PCM:
115  *	      Max channels: 2
116  *	      Supported sample rates (kHz): 48 44.1 32
117  *	      Supported sample sizes (bits): 24 20 16
118  *	  Video Data Block:
119  *	    VIC   1:   640x480    59.940476 Hz   4:3     31.469 kHz     25.175000 MHz
120  *	    VIC   3:   720x480    59.940060 Hz  16:9     31.469 kHz     27.000000 MHz
121  *	    VIC   4:  1280x720    60.000000 Hz  16:9     45.000 kHz     74.250000 MHz
122  *	    VIC  16:  1920x1080   60.000000 Hz  16:9     67.500 kHz    148.500000 MHz (native)
123  *	    VIC  18:   720x576    50.000000 Hz  16:9     31.250 kHz     27.000000 MHz
124  *	    VIC  19:  1280x720    50.000000 Hz  16:9     37.500 kHz     74.250000 MHz
125  *	    VIC  31:  1920x1080   50.000000 Hz  16:9     56.250 kHz    148.500000 MHz
126  *	    VIC  34:  1920x1080   30.000000 Hz  16:9     33.750 kHz     74.250000 MHz
127  *	    VIC  93:  3840x2160   24.000000 Hz  16:9     54.000 kHz    297.000000 MHz
128  *	    VIC  94:  3840x2160   25.000000 Hz  16:9     56.250 kHz    297.000000 MHz
129  *	    VIC  95:  3840x2160   30.000000 Hz  16:9     67.500 kHz    297.000000 MHz
130  *	    VIC  96:  3840x2160   50.000000 Hz  16:9    112.500 kHz    594.000000 MHz
131  *	    VIC  97:  3840x2160   60.000000 Hz  16:9    135.000 kHz    594.000000 MHz
132  *	  Speaker Allocation Data Block:
133  *	    FL/FR - Front Left/Right
134  *	  Vendor-Specific Data Block (HDMI), OUI 00-0C-03:
135  *	    Source physical address: 1.0.0.0
136  *	    Supports_AI
137  *	    DC_36bit
138  *	    DC_30bit
139  *	    DC_Y444
140  *	    Maximum TMDS clock: 300 MHz
141  *	    Extended HDMI video details:
142  *	      HDMI VICs:
143  *	        HDMI VIC 1:  3840x2160   30.000000 Hz  16:9     67.500 kHz    297.000000 MHz
144  *	        HDMI VIC 2:  3840x2160   25.000000 Hz  16:9     56.250 kHz    297.000000 MHz
145  *	        HDMI VIC 3:  3840x2160   24.000000 Hz  16:9     54.000 kHz    297.000000 MHz
146  *	  Vendor-Specific Data Block (HDMI Forum), OUI C4-5D-D8:
147  *	    Version: 1
148  *	    Maximum TMDS Character Rate: 600 MHz
149  *	    SCDC Present
150  *	    Supports 12-bits/component Deep Color 4:2:0 Pixel Encoding
151  *	    Supports 10-bits/component Deep Color 4:2:0 Pixel Encoding
152  *	  YCbCr 4:2:0 Capability Map Data Block:
153  *	    VIC  96:  3840x2160   50.000000 Hz  16:9    112.500 kHz    594.000000 MHz
154  *	    VIC  97:  3840x2160   60.000000 Hz  16:9    135.000 kHz    594.000000 MHz
155  *	  Video Capability Data Block:
156  *	    YCbCr quantization: No Data
157  *	    RGB quantization: Selectable (via AVI Q)
158  *	    PT scan behavior: Always Underscanned
159  *	    IT scan behavior: Always Underscanned
160  *	    CE scan behavior: Always Underscanned
161  *	  Colorimetry Data Block:
162  *	    BT2020YCC
163  *	    BT2020RGB
164  *	  HDR Static Metadata Data Block:
165  *	    Electro optical transfer functions:
166  *	      Traditional gamma - SDR luminance range
167  *	      SMPTE ST2084
168  *	    Supported static metadata descriptors:
169  *	      Static metadata type 1
170  *	    Desired content max luminance: 82 (295.365 cd/m^2)
171  *	    Desired content max frame-average luminance: 82 (295.365 cd/m^2)
172  *	    Desired content min luminance: 81 (0.298 cd/m^2)
173  *	  Detailed Timing Descriptors:
174  *	    DTD 2:  2560x2880   29.986961 Hz   8:9     87.592 kHz    238.250000 MHz (465 mm x 523 mm)
175  *	                 Hfront   48 Hsync  32 Hback   80 Hpol P
176  *	                 Vfront    3 Vsync  10 Vback   28 Vpol N
177  *	Checksum: 0xc3  Unused space in Extension Block: 43 bytes
178  *
179  *	----------------
180  *
181  *	edid-decode 1.29.0-5346
182  *	edid-decode SHA: c363e9aa6d70 2025-03-11 11:41:18
183  *
184  *	Warnings:
185  *
186  *	Block 1, CTA-861 Extension Block:
187  *	  IT Video Formats are overscanned by default, but normally this should be underscanned.
188  *	  Video Data Block: VIC 1 and the first DTD are not identical. Is this intended?
189  *	  Video Data Block: All VICs are in ascending order, and the first (preferred) VIC <= 4, is that intended?
190  *	  Video Capability Data Block: Set Selectable YCbCr Quantization to avoid interop issues.
191  *	  Video Capability Data Block: S_PT is equal to S_IT and S_CE, so should be set to 0 instead.
192  *	  Colorimetry Data Block: Set the sRGB colorimetry bit to avoid interop issues.
193  *	  Display Product Serial Number is set, so the Serial Number in the Base EDID should be 0.
194  *	EDID:
195  *	  Base EDID: Some timings are out of range of the Monitor Ranges:
196  *	    Vertical Freq: 24.000 - 60.317 Hz (Monitor: 59.000 - 61.000 Hz)
197  *	    Horizontal Freq: 31.250 - 185.416 kHz (Monitor: 30.000 - 178.000 kHz)
198  *	    Maximum Clock: 594.000 MHz (Monitor: 490.000 MHz)
199  *
200  *	Failures:
201  *
202  *	Block 1, CTA-861 Extension Block:
203  *	  Video Capability Data Block: IT video formats are always underscanned, but bit 7 of Byte 3 of the CTA-861 Extension header is set to overscanned.
204  *	EDID:
205  *	  CTA-861: Native progressive timings are a mix of several resolutions.
206  *
207  *	EDID conformity: FAIL
208  *
209  *	================
210  *
211  *	InfoFrame of '/sys/kernel/debug/dri/1/HDMI-A-1/infoframes/audio' was empty.
212  *
213  *	================
214  *
215  *	edid-decode InfoFrame (hex):
216  *
217  *	82 02 0d 31 12 28 04 00 00 00 00 00 00 00 00 00
218  *	00
219  *
220  *	----------------
221  *
222  *	HDMI InfoFrame Checksum: 0x31
223  *
224  *	AVI InfoFrame
225  *	  Version: 2
226  *	  Length: 13
227  *	  Y: Color Component Sample Format: RGB
228  *	  A: Active Format Information Present: Yes
229  *	  B: Bar Data Present: Bar Data not present
230  *	  S: Scan Information: Composed for an underscanned display
231  *	  C: Colorimetry: No Data
232  *	  M: Picture Aspect Ratio: 16:9
233  *	  R: Active Portion Aspect Ratio: 8
234  *	  ITC: IT Content: No Data
235  *	  EC: Extended Colorimetry: xvYCC601
236  *	  Q: RGB Quantization Range: Limited Range
237  *	  SC: Non-Uniform Picture Scaling: No Known non-uniform scaling
238  *	  YQ: YCC Quantization Range: Limited Range
239  *	  CN: IT Content Type: Graphics
240  *	  PR: Pixel Data Repetition Count: 0
241  *	  Line Number of End of Top Bar: 0
242  *	  Line Number of Start of Bottom Bar: 0
243  *	  Pixel Number of End of Left Bar: 0
244  *	  Pixel Number of Start of Right Bar: 0
245  *
246  *	----------------
247  *
248  *	AVI InfoFrame conformity: PASS
249  *
250  *	================
251  *
252  *	edid-decode InfoFrame (hex):
253  *
254  *	81 01 05 49 03 0c 00 20 01
255  *
256  *	----------------
257  *
258  *	HDMI InfoFrame Checksum: 0x49
259  *
260  *	Vendor-Specific InfoFrame (HDMI), OUI 00-0C-03
261  *	  Version: 1
262  *	  Length: 5
263  *	  HDMI Video Format: HDMI_VIC is present
264  *	  HDMI VIC 1:  3840x2160   30.000000 Hz  16:9     67.500 kHz    297.000000 MHz
265  *
266  *	----------------
267  *
268  *	Vendor-Specific InfoFrame (HDMI), OUI 00-0C-03 conformity: PASS
269  *
270  *	================
271  *
272  *	InfoFrame of '/sys/kernel/debug/dri/1/HDMI-A-1/infoframes/hdr_drm' was empty.
273  *
274  *	================
275  *
276  *	edid-decode InfoFrame (hex):
277  *
278  *	83 01 19 93 42 72 6f 61 64 63 6f 6d 56 69 64 65
279  *	6f 63 6f 72 65 00 00 00 00 00 00 00 09
280  *
281  *	----------------
282  *
283  *	HDMI InfoFrame Checksum: 0x93
284  *
285  *	Source Product Description InfoFrame
286  *	  Version: 1
287  *	  Length: 25
288  *	  Vendor Name: 'Broadcom'
289  *	  Product Description: 'Videocore'
290  *	  Source Information: PC general
291  *
292  *	----------------
293  *
294  *	Source Product Description InfoFrame conformity: PASS
295  *
296  * Testing
297  * ~~~~~~~
298  *
299  * The helpers have unit testing and can be tested using kunit with:
300  *
301  * .. code-block:: bash
302  *
303  *	$ ./tools/testing/kunit/kunit.py run \
304  *		--kunitconfig=drivers/gpu/drm/tests \
305  *		drm_atomic_helper_connector_hdmi_*
306  */
307 
308 /**
309  * __drm_atomic_helper_connector_hdmi_state_init() - Initialize all HDMI @drm_connector_state resources
310  * @connector: DRM connector
311  * @new_conn_state: connector state to initialize
312  *
313  * Initializes all HDMI resources from a @drm_connector_state without
314  * actually allocating it. This is useful for HDMI drivers, in
315  * combination with __drm_atomic_helper_connector_state_init(),
316  * drm_atomic_helper_connector_reset(), or
317  * drm_atomic_helper_connector_create_state().
318  */
319 void __drm_atomic_helper_connector_hdmi_state_init(struct drm_connector *connector,
320 						   struct drm_connector_state *new_conn_state)
321 {
322 	unsigned int max_bpc = connector->max_bpc;
323 
324 	new_conn_state->max_bpc = max_bpc;
325 	new_conn_state->max_requested_bpc = max_bpc;
326 	new_conn_state->hdmi.broadcast_rgb = DRM_HDMI_BROADCAST_RGB_AUTO;
327 }
328 EXPORT_SYMBOL(__drm_atomic_helper_connector_hdmi_state_init);
329 
330 static enum hdmi_colorspace
331 output_color_format_to_hdmi_colorspace(const struct drm_connector *connector,
332 				       enum drm_output_color_format fmt)
333 {
334 	switch (fmt) {
335 	case DRM_OUTPUT_COLOR_FORMAT_YCBCR420:
336 		return HDMI_COLORSPACE_YUV420;
337 	case DRM_OUTPUT_COLOR_FORMAT_YCBCR422:
338 		return HDMI_COLORSPACE_YUV422;
339 	case DRM_OUTPUT_COLOR_FORMAT_YCBCR444:
340 		return HDMI_COLORSPACE_YUV444;
341 	default:
342 		drm_warn(connector->dev, "Unsupported output color format. Defaulting to RGB.");
343 		fallthrough;
344 	case DRM_OUTPUT_COLOR_FORMAT_RGB444:
345 		return HDMI_COLORSPACE_RGB;
346 	}
347 }
348 
349 static const struct drm_display_mode *
350 connector_state_get_mode(const struct drm_connector_state *conn_state)
351 {
352 	struct drm_atomic_commit *state;
353 	struct drm_crtc_state *crtc_state;
354 	struct drm_crtc *crtc;
355 
356 	state = conn_state->state;
357 	if (!state)
358 		return NULL;
359 
360 	crtc = conn_state->crtc;
361 	if (!crtc)
362 		return NULL;
363 
364 	crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
365 	if (!crtc_state)
366 		return NULL;
367 
368 	return &crtc_state->mode;
369 }
370 
371 static bool hdmi_is_limited_range(const struct drm_connector *connector,
372 				  const struct drm_connector_state *conn_state)
373 {
374 	const struct drm_display_info *info = &connector->display_info;
375 	const struct drm_display_mode *mode =
376 		connector_state_get_mode(conn_state);
377 
378 	/*
379 	 * The Broadcast RGB property only applies to RGB format, and
380 	 * i915 just assumes limited range for YCbCr output, so let's
381 	 * just do the same.
382 	 */
383 	if (conn_state->hdmi.output_format != DRM_OUTPUT_COLOR_FORMAT_RGB444)
384 		return true;
385 
386 	if (conn_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_FULL)
387 		return false;
388 
389 	if (conn_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_LIMITED)
390 		return true;
391 
392 	if (!info->is_hdmi)
393 		return false;
394 
395 	return drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED;
396 }
397 
398 static bool
399 sink_supports_format_bpc(const struct drm_connector *connector,
400 			 const struct drm_display_info *info,
401 			 const struct drm_display_mode *mode,
402 			 enum drm_output_color_format format,
403 			 unsigned int bpc)
404 {
405 	struct drm_device *dev = connector->dev;
406 	u8 vic = drm_match_cea_mode(mode);
407 
408 	/*
409 	 * CTA-861-F, section 5.4 - Color Coding & Quantization states
410 	 * that the bpc must be 8, 10, 12 or 16 except for the default
411 	 * 640x480 VIC1 where the value must be 8.
412 	 *
413 	 * The definition of default here is ambiguous but the spec
414 	 * refers to VIC1 being the default timing in several occasions
415 	 * so our understanding is that for the default timing (ie,
416 	 * VIC1), the bpc must be 8.
417 	 */
418 	if (vic == 1 && bpc != 8) {
419 		drm_dbg_kms(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
420 		return false;
421 	}
422 
423 	if (!info->is_hdmi &&
424 	    (format != DRM_OUTPUT_COLOR_FORMAT_RGB444 || bpc != 8)) {
425 		drm_dbg_kms(dev, "DVI Monitors require an RGB output at 8 bpc\n");
426 		return false;
427 	}
428 
429 	if (!(connector->hdmi.supported_formats & BIT(format))) {
430 		drm_dbg_kms(dev, "%s format unsupported by the connector.\n",
431 			    drm_hdmi_connector_get_output_format_name(format));
432 		return false;
433 	}
434 
435 	if (drm_mode_is_420_only(info, mode) && format != DRM_OUTPUT_COLOR_FORMAT_YCBCR420) {
436 		drm_dbg_kms(dev, "Mode can be only supported in YUV420 format.\n");
437 		return false;
438 	}
439 
440 	switch (format) {
441 	case DRM_OUTPUT_COLOR_FORMAT_RGB444:
442 		drm_dbg_kms(dev, "RGB Format, checking the constraints.\n");
443 
444 		/*
445 		 * In some cases, like when the EDID readout fails, or
446 		 * is not an HDMI compliant EDID for some reason, the
447 		 * color_formats field will be blank and not report any
448 		 * format supported. In such a case, assume that RGB is
449 		 * supported so we can keep things going and light up
450 		 * the display.
451 		 */
452 		if (!(info->color_formats & BIT(DRM_OUTPUT_COLOR_FORMAT_RGB444)))
453 			drm_warn(dev, "HDMI Sink doesn't support RGB, something's wrong.\n");
454 
455 		if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
456 			drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
457 			return false;
458 		}
459 
460 		if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
461 			drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
462 			return false;
463 		}
464 
465 		drm_dbg_kms(dev, "RGB format supported in that configuration.\n");
466 
467 		return true;
468 
469 	case DRM_OUTPUT_COLOR_FORMAT_YCBCR420:
470 		drm_dbg_kms(dev, "YUV420 format, checking the constraints.\n");
471 
472 		if (!(info->color_formats & BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR420))) {
473 			drm_dbg_kms(dev, "Sink doesn't support YUV420.\n");
474 			return false;
475 		}
476 
477 		if (!drm_mode_is_420(info, mode)) {
478 			drm_dbg_kms(dev, "Mode cannot be supported in YUV420 format.\n");
479 			return false;
480 		}
481 
482 		if (bpc == 10 && !(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)) {
483 			drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
484 			return false;
485 		}
486 
487 		if (bpc == 12 && !(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)) {
488 			drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
489 			return false;
490 		}
491 
492 		if (bpc == 16 && !(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)) {
493 			drm_dbg_kms(dev, "16 BPC but sink doesn't support Deep Color 48.\n");
494 			return false;
495 		}
496 
497 		drm_dbg_kms(dev, "YUV420 format supported in that configuration.\n");
498 
499 		return true;
500 
501 	case DRM_OUTPUT_COLOR_FORMAT_YCBCR422:
502 		drm_dbg_kms(dev, "YUV422 format, checking the constraints.\n");
503 
504 		if (!(info->color_formats & BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR422))) {
505 			drm_dbg_kms(dev, "Sink doesn't support YUV422.\n");
506 			return false;
507 		}
508 
509 		if (bpc > 12) {
510 			drm_dbg_kms(dev, "YUV422 only supports 12 bpc or lower.\n");
511 			return false;
512 		}
513 
514 		/*
515 		 * HDMI Spec 1.3 - Section 6.5 Pixel Encodings and Color Depth
516 		 * states that Deep Color is not relevant for YUV422 so we
517 		 * don't need to check the Deep Color bits in the EDIDs here.
518 		 */
519 
520 		drm_dbg_kms(dev, "YUV422 format supported in that configuration.\n");
521 
522 		return true;
523 
524 	case DRM_OUTPUT_COLOR_FORMAT_YCBCR444:
525 		drm_dbg_kms(dev, "YUV444 format, checking the constraints.\n");
526 
527 		if (!(info->color_formats & BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR444))) {
528 			drm_dbg_kms(dev, "Sink doesn't support YUV444.\n");
529 			return false;
530 		}
531 
532 		if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {
533 			drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
534 			return false;
535 		}
536 
537 		if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {
538 			drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
539 			return false;
540 		}
541 
542 		drm_dbg_kms(dev, "YUV444 format supported in that configuration.\n");
543 
544 		return true;
545 
546 	default:
547 		drm_dbg_kms(dev, "Unsupported pixel format.\n");
548 		return false;
549 	}
550 }
551 
552 static enum drm_mode_status
553 hdmi_clock_valid(const struct drm_connector *connector,
554 		 const struct drm_display_mode *mode,
555 		 unsigned long long clock)
556 {
557 	const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;
558 	const struct drm_display_info *info = &connector->display_info;
559 
560 	if (info->max_tmds_clock && clock > info->max_tmds_clock * 1000)
561 		return MODE_CLOCK_HIGH;
562 
563 	if (funcs && funcs->tmds_char_rate_valid) {
564 		enum drm_mode_status status;
565 
566 		status = funcs->tmds_char_rate_valid(connector, mode, clock);
567 		if (status != MODE_OK)
568 			return status;
569 	}
570 
571 	return MODE_OK;
572 }
573 
574 static int
575 hdmi_compute_clock(const struct drm_connector *connector,
576 		   struct drm_connector_state *conn_state,
577 		   const struct drm_display_mode *mode,
578 		   unsigned int bpc, enum drm_output_color_format fmt)
579 {
580 	enum drm_mode_status status;
581 	unsigned long long clock;
582 
583 	clock = drm_hdmi_compute_mode_clock(mode, bpc, fmt);
584 	if (!clock)
585 		return -EINVAL;
586 
587 	status = hdmi_clock_valid(connector, mode, clock);
588 	if (status != MODE_OK)
589 		return -EINVAL;
590 
591 	conn_state->hdmi.tmds_char_rate = clock;
592 
593 	return 0;
594 }
595 
596 static bool
597 hdmi_try_format_bpc(const struct drm_connector *connector,
598 		    struct drm_connector_state *conn_state,
599 		    const struct drm_display_mode *mode,
600 		    unsigned int bpc, enum drm_output_color_format fmt)
601 {
602 	const struct drm_display_info *info = &connector->display_info;
603 	struct drm_device *dev = connector->dev;
604 	int ret;
605 
606 	drm_dbg_kms(dev, "Trying %s output format with %u bpc\n",
607 		    drm_hdmi_connector_get_output_format_name(fmt),
608 		    bpc);
609 
610 	if (!sink_supports_format_bpc(connector, info, mode, fmt, bpc)) {
611 		drm_dbg_kms(dev, "%s output format not supported with %u bpc\n",
612 			    drm_hdmi_connector_get_output_format_name(fmt),
613 			    bpc);
614 		return false;
615 	}
616 
617 	ret = hdmi_compute_clock(connector, conn_state, mode, bpc, fmt);
618 	if (ret) {
619 		drm_dbg_kms(dev, "Couldn't compute clock for %s output format and %u bpc\n",
620 			    drm_hdmi_connector_get_output_format_name(fmt),
621 			    bpc);
622 		return false;
623 	}
624 
625 	drm_dbg_kms(dev, "%s output format supported with %u bpc (TMDS char rate: %llu Hz)\n",
626 		    drm_hdmi_connector_get_output_format_name(fmt),
627 		    bpc, conn_state->hdmi.tmds_char_rate);
628 
629 	return true;
630 }
631 
632 static int
633 hdmi_compute_format_bpc(const struct drm_connector *connector,
634 			struct drm_connector_state *conn_state,
635 			const struct drm_display_mode *mode,
636 			unsigned int max_bpc, enum drm_output_color_format fmt)
637 {
638 	struct drm_device *dev = connector->dev;
639 	unsigned int bpc;
640 	int ret;
641 
642 	for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
643 		ret = hdmi_try_format_bpc(connector, conn_state, mode, bpc, fmt);
644 		if (!ret)
645 			continue;
646 
647 		conn_state->hdmi.output_bpc = bpc;
648 		conn_state->hdmi.output_format = fmt;
649 
650 		drm_dbg_kms(dev,
651 			    "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
652 			    mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
653 			    conn_state->hdmi.output_bpc,
654 			    drm_hdmi_connector_get_output_format_name(conn_state->hdmi.output_format),
655 			    conn_state->hdmi.tmds_char_rate);
656 
657 		return 0;
658 	}
659 
660 	drm_dbg_kms(dev, "Failed. %s output format not supported for any bpc count.\n",
661 		    drm_hdmi_connector_get_output_format_name(fmt));
662 
663 	return -EINVAL;
664 }
665 
666 static int
667 hdmi_compute_config(const struct drm_connector *connector,
668 		    struct drm_connector_state *conn_state,
669 		    const struct drm_display_mode *mode)
670 {
671 	unsigned int max_bpc = clamp_t(unsigned int,
672 				       conn_state->max_bpc,
673 				       8, connector->max_bpc);
674 	enum drm_output_color_format fmt;
675 	int ret;
676 
677 	if (conn_state->color_format != DRM_CONNECTOR_COLOR_FORMAT_AUTO) {
678 		switch (conn_state->color_format) {
679 		case DRM_CONNECTOR_COLOR_FORMAT_AUTO:
680 			drm_warn(connector->dev, "AUTO format in non-AUTO path.\n");
681 			fallthrough;
682 		case DRM_CONNECTOR_COLOR_FORMAT_RGB444:
683 			fmt = DRM_OUTPUT_COLOR_FORMAT_RGB444;
684 			break;
685 		case DRM_CONNECTOR_COLOR_FORMAT_YCBCR444:
686 			fmt = DRM_OUTPUT_COLOR_FORMAT_YCBCR444;
687 			break;
688 		case DRM_CONNECTOR_COLOR_FORMAT_YCBCR422:
689 			fmt = DRM_OUTPUT_COLOR_FORMAT_YCBCR422;
690 			break;
691 		case DRM_CONNECTOR_COLOR_FORMAT_YCBCR420:
692 			fmt = DRM_OUTPUT_COLOR_FORMAT_YCBCR420;
693 			break;
694 		default:
695 			drm_dbg_kms(connector->dev, "HDMI does not support color format '%d'.\n",
696 				conn_state->color_format);
697 			return -EINVAL;
698 		}
699 
700 		return hdmi_compute_format_bpc(connector, conn_state, mode, max_bpc, fmt);
701 	}
702 
703 	/*
704 	 * For %DRM_CONNECTOR_COLOR_FORMAT_AUTO, try RGB first, and fall back
705 	 * to the less bandwidth-intensive YCBCR420 if RGB fails.
706 	 */
707 	ret = hdmi_compute_format_bpc(connector, conn_state, mode, max_bpc,
708 				      DRM_OUTPUT_COLOR_FORMAT_RGB444);
709 	if (ret) {
710 		if (connector->ycbcr_420_allowed) {
711 			ret = hdmi_compute_format_bpc(connector, conn_state,
712 						      mode, max_bpc,
713 						      DRM_OUTPUT_COLOR_FORMAT_YCBCR420);
714 			if (ret)
715 				drm_dbg_kms(connector->dev,
716 					    "YUV420 output format doesn't work.\n");
717 		} else {
718 			drm_dbg_kms(connector->dev,
719 				    "YUV420 output format not allowed for connector.\n");
720 			ret = -EINVAL;
721 		}
722 	}
723 
724 	return ret;
725 }
726 
727 static int hdmi_generate_avi_infoframe(const struct drm_connector *connector,
728 				       struct drm_connector_state *conn_state)
729 {
730 	const struct drm_display_mode *mode =
731 		connector_state_get_mode(conn_state);
732 	struct drm_connector_hdmi_infoframe *infoframe =
733 		&conn_state->hdmi.infoframes.avi;
734 	struct hdmi_avi_infoframe *frame =
735 		&infoframe->data.avi;
736 	bool is_limited_range = conn_state->hdmi.is_limited_range;
737 	enum hdmi_quantization_range rgb_quant_range =
738 		is_limited_range ? HDMI_QUANTIZATION_RANGE_LIMITED : HDMI_QUANTIZATION_RANGE_FULL;
739 	int ret;
740 
741 	infoframe->set = false;
742 
743 	ret = drm_hdmi_avi_infoframe_from_display_mode(frame, connector, mode);
744 	if (ret)
745 		return ret;
746 
747 	frame->colorspace =
748 		output_color_format_to_hdmi_colorspace(connector,
749 						       conn_state->hdmi.output_format);
750 
751 	/*
752 	 * FIXME: drm_hdmi_avi_infoframe_quant_range() doesn't handle
753 	 * YUV formats at all at the moment, so if we ever support YUV
754 	 * formats this needs to be revised.
755 	 */
756 	drm_hdmi_avi_infoframe_quant_range(frame, connector, mode, rgb_quant_range);
757 	drm_hdmi_avi_infoframe_colorimetry(frame, conn_state);
758 	drm_hdmi_avi_infoframe_bars(frame, conn_state);
759 
760 	infoframe->set = true;
761 
762 	return 0;
763 }
764 
765 static int hdmi_generate_spd_infoframe(const struct drm_connector *connector,
766 				       struct drm_connector_state *conn_state)
767 {
768 	struct drm_connector_hdmi_infoframe *infoframe =
769 		&conn_state->hdmi.infoframes.spd;
770 	struct hdmi_spd_infoframe *frame =
771 		&infoframe->data.spd;
772 	int ret;
773 
774 	infoframe->set = false;
775 
776 	if (!connector->hdmi.funcs->spd.write_infoframe)
777 		return 0;
778 
779 	ret = hdmi_spd_infoframe_init(frame,
780 				      connector->hdmi.vendor,
781 				      connector->hdmi.product);
782 	if (ret)
783 		return ret;
784 
785 	frame->sdi = HDMI_SPD_SDI_PC;
786 
787 	infoframe->set = true;
788 
789 	return 0;
790 }
791 
792 static int hdmi_generate_hdr_infoframe(const struct drm_connector *connector,
793 				       struct drm_connector_state *conn_state)
794 {
795 	struct drm_connector_hdmi_infoframe *infoframe =
796 		&conn_state->hdmi.infoframes.hdr_drm;
797 	struct hdmi_drm_infoframe *frame =
798 		&infoframe->data.drm;
799 	int ret;
800 
801 	infoframe->set = false;
802 
803 	if (!connector->hdmi.funcs->hdr_drm.write_infoframe)
804 		return 0;
805 
806 	if (connector->max_bpc < 10)
807 		return 0;
808 
809 	if (!conn_state->hdr_output_metadata)
810 		return 0;
811 
812 	ret = drm_hdmi_infoframe_set_hdr_metadata(frame, conn_state);
813 	if (ret)
814 		return ret;
815 
816 	infoframe->set = true;
817 
818 	return 0;
819 }
820 
821 static int hdmi_generate_hdmi_vendor_infoframe(const struct drm_connector *connector,
822 					       struct drm_connector_state *conn_state)
823 {
824 	const struct drm_display_info *info = &connector->display_info;
825 	const struct drm_display_mode *mode =
826 		connector_state_get_mode(conn_state);
827 	struct drm_connector_hdmi_infoframe *infoframe =
828 		&conn_state->hdmi.infoframes.hdmi;
829 	struct hdmi_vendor_infoframe *frame =
830 		&infoframe->data.vendor.hdmi;
831 	int ret;
832 
833 	infoframe->set = false;
834 
835 	if (!info->has_hdmi_infoframe)
836 		return 0;
837 
838 	ret = drm_hdmi_vendor_infoframe_from_display_mode(frame, connector, mode);
839 	if (ret)
840 		return ret;
841 
842 	infoframe->set = true;
843 
844 	return 0;
845 }
846 
847 static int
848 hdmi_generate_infoframes(const struct drm_connector *connector,
849 			 struct drm_connector_state *conn_state)
850 {
851 	const struct drm_display_info *info = &connector->display_info;
852 	int ret;
853 
854 	if (!info->is_hdmi)
855 		return 0;
856 
857 	ret = hdmi_generate_avi_infoframe(connector, conn_state);
858 	if (ret)
859 		return ret;
860 
861 	ret = hdmi_generate_spd_infoframe(connector, conn_state);
862 	if (ret)
863 		return ret;
864 
865 	/*
866 	 * Audio Infoframes will be generated by ALSA, and updated by
867 	 * drm_atomic_helper_connector_hdmi_update_audio_infoframe().
868 	 */
869 
870 	ret = hdmi_generate_hdr_infoframe(connector, conn_state);
871 	if (ret)
872 		return ret;
873 
874 	ret = hdmi_generate_hdmi_vendor_infoframe(connector, conn_state);
875 	if (ret)
876 		return ret;
877 
878 	return 0;
879 }
880 
881 /**
882  * drm_atomic_helper_connector_hdmi_check() - Helper to check HDMI connector atomic state
883  * @connector: DRM Connector
884  * @state: the DRM State object
885  *
886  * Provides a default connector state check handler for HDMI connectors.
887  * Checks that a desired connector update is valid, and updates various
888  * fields of derived state.
889  *
890  * RETURNS:
891  * Zero on success, or an errno code otherwise.
892  */
893 int drm_atomic_helper_connector_hdmi_check(struct drm_connector *connector,
894 					   struct drm_atomic_commit *state)
895 {
896 	struct drm_connector_state *old_conn_state =
897 		drm_atomic_get_old_connector_state(state, connector);
898 	struct drm_connector_state *new_conn_state =
899 		drm_atomic_get_new_connector_state(state, connector);
900 	const struct drm_display_mode *mode =
901 		connector_state_get_mode(new_conn_state);
902 	int ret;
903 
904 	if (!new_conn_state->crtc || !new_conn_state->best_encoder)
905 		return 0;
906 
907 	ret = hdmi_compute_config(connector, new_conn_state, mode);
908 	if (ret)
909 		return ret;
910 
911 	new_conn_state->hdmi.is_limited_range = hdmi_is_limited_range(connector, new_conn_state);
912 
913 	ret = hdmi_generate_infoframes(connector, new_conn_state);
914 	if (ret)
915 		return ret;
916 
917 	if (old_conn_state->hdmi.broadcast_rgb != new_conn_state->hdmi.broadcast_rgb ||
918 	    old_conn_state->hdmi.output_bpc != new_conn_state->hdmi.output_bpc ||
919 	    old_conn_state->hdmi.output_format != new_conn_state->hdmi.output_format) {
920 		struct drm_crtc *crtc = new_conn_state->crtc;
921 		struct drm_crtc_state *crtc_state;
922 
923 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
924 		if (IS_ERR(crtc_state))
925 			return PTR_ERR(crtc_state);
926 
927 		crtc_state->mode_changed = true;
928 	}
929 
930 	return 0;
931 }
932 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_check);
933 
934 /**
935  * drm_hdmi_connector_mode_valid() - Check if mode is valid for HDMI connector
936  * @connector: DRM connector to validate the mode
937  * @mode: Display mode to validate
938  *
939  * Generic .mode_valid implementation for HDMI connectors.
940  */
941 enum drm_mode_status
942 drm_hdmi_connector_mode_valid(struct drm_connector *connector,
943 			      const struct drm_display_mode *mode)
944 {
945 	unsigned long long clock;
946 	enum drm_output_color_format fmt;
947 
948 	if (drm_mode_is_420_only(&connector->display_info, mode)) {
949 		if (connector->ycbcr_420_allowed)
950 			fmt = DRM_OUTPUT_COLOR_FORMAT_YCBCR420;
951 		else
952 			return MODE_NO_420;
953 	} else if (drm_mode_is_420_also(&connector->display_info, mode) &&
954 		   connector->ycbcr_420_allowed) {
955 		fmt = DRM_OUTPUT_COLOR_FORMAT_YCBCR420;
956 	} else {
957 		fmt = DRM_OUTPUT_COLOR_FORMAT_RGB444;
958 	}
959 
960 	clock = drm_hdmi_compute_mode_clock(mode, 8, fmt);
961 	if (!clock)
962 		return MODE_ERROR;
963 
964 	return hdmi_clock_valid(connector, mode, clock);
965 }
966 EXPORT_SYMBOL(drm_hdmi_connector_mode_valid);
967 
968 static int clear_infoframe(struct drm_connector *connector,
969 			   const struct drm_connector_infoframe_funcs *funcs,
970 			   const char *type)
971 {
972 	struct drm_device *dev = connector->dev;
973 	int ret;
974 
975 	drm_dbg_kms(dev, "Clearing %s InfoFrame\n", type);
976 
977 	if (!funcs->clear_infoframe) {
978 		drm_dbg_kms(dev, "Function not implemented, bailing.\n");
979 		return -EOPNOTSUPP;
980 	}
981 
982 	ret = funcs->clear_infoframe(connector);
983 	if (ret) {
984 		drm_dbg_kms(dev, "Call failed: %d\n", ret);
985 		return ret;
986 	}
987 
988 	return 0;
989 }
990 
991 static int write_infoframe(struct drm_connector *connector,
992 			   const struct drm_connector_infoframe_funcs *funcs,
993 			   const char *type,
994 			   struct drm_connector_hdmi_infoframe *new_frame)
995 {
996 	struct drm_device *dev = connector->dev;
997 	u8 buffer[HDMI_INFOFRAME_SIZE(MAX)];
998 	int ret;
999 	int len;
1000 
1001 	drm_dbg_kms(dev, "Writing %s InfoFrame\n", type);
1002 
1003 	if (!funcs->write_infoframe) {
1004 		drm_dbg_kms(dev, "Function not implemented, bailing.\n");
1005 		return -EOPNOTSUPP;
1006 	}
1007 
1008 	len = hdmi_infoframe_pack(&new_frame->data, buffer, sizeof(buffer));
1009 	if (len < 0)
1010 		return len;
1011 
1012 	ret = funcs->write_infoframe(connector, buffer, len);
1013 	if (ret) {
1014 		drm_dbg_kms(dev, "Call failed: %d\n", ret);
1015 		return ret;
1016 	}
1017 
1018 	return 0;
1019 }
1020 
1021 static int write_or_clear_infoframe(struct drm_connector *connector,
1022 				    const struct drm_connector_infoframe_funcs *funcs,
1023 				    const char *type,
1024 				    struct drm_connector_hdmi_infoframe *old_frame,
1025 				    struct drm_connector_hdmi_infoframe *new_frame)
1026 {
1027 	if (new_frame->set)
1028 		return write_infoframe(connector, funcs, type, new_frame);
1029 
1030 	if (old_frame->set && !new_frame->set)
1031 		return clear_infoframe(connector, funcs, type);
1032 
1033 	return 0;
1034 }
1035 
1036 /**
1037  * drm_atomic_helper_connector_hdmi_update_infoframes - Update the Infoframes
1038  * @connector: A pointer to the HDMI connector
1039  * @state: The HDMI connector state to generate the infoframe from
1040  *
1041  * This function is meant for HDMI connector drivers to write their
1042  * infoframes. It will typically be used in a
1043  * @drm_connector_helper_funcs.atomic_enable implementation.
1044  *
1045  * Returns:
1046  * Zero on success, error code on failure.
1047  */
1048 int drm_atomic_helper_connector_hdmi_update_infoframes(struct drm_connector *connector,
1049 						       struct drm_atomic_commit *state)
1050 {
1051 	const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;
1052 	struct drm_connector_state *old_conn_state =
1053 		drm_atomic_get_old_connector_state(state, connector);
1054 	struct drm_connector_state *new_conn_state =
1055 		drm_atomic_get_new_connector_state(state, connector);
1056 	struct drm_display_info *info = &connector->display_info;
1057 	int ret;
1058 
1059 	if (!info->is_hdmi)
1060 		return 0;
1061 
1062 	if (!funcs) {
1063 		drm_dbg_kms(connector->dev, "Function not implemented, bailing.\n");
1064 		return -EINVAL;
1065 	}
1066 
1067 	mutex_lock(&connector->hdmi.infoframes.lock);
1068 
1069 	ret = write_or_clear_infoframe(connector,
1070 				       &funcs->avi, "AVI",
1071 				       &old_conn_state->hdmi.infoframes.avi,
1072 				       &new_conn_state->hdmi.infoframes.avi);
1073 	if (ret)
1074 		goto out;
1075 
1076 	if (connector->hdmi.infoframes.audio.set) {
1077 		ret = write_infoframe(connector,
1078 				      &funcs->audio, "Audio",
1079 				      &connector->hdmi.infoframes.audio);
1080 		if (ret)
1081 			goto out;
1082 	}
1083 
1084 	ret = write_or_clear_infoframe(connector,
1085 				       &funcs->hdr_drm, "HDR DRM",
1086 				       &old_conn_state->hdmi.infoframes.hdr_drm,
1087 				       &new_conn_state->hdmi.infoframes.hdr_drm);
1088 	if (ret)
1089 		goto out;
1090 
1091 	ret = write_or_clear_infoframe(connector,
1092 				       &funcs->spd, "SPD",
1093 				       &old_conn_state->hdmi.infoframes.spd,
1094 				       &new_conn_state->hdmi.infoframes.spd);
1095 	if (ret)
1096 		goto out;
1097 
1098 	if (info->has_hdmi_infoframe) {
1099 		ret = write_or_clear_infoframe(connector,
1100 					       &funcs->hdmi, "HDMI-VS",
1101 					       &old_conn_state->hdmi.infoframes.hdmi,
1102 					       &new_conn_state->hdmi.infoframes.hdmi);
1103 		if (ret)
1104 			goto out;
1105 	}
1106 
1107 out:
1108 	mutex_unlock(&connector->hdmi.infoframes.lock);
1109 	return ret;
1110 }
1111 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_update_infoframes);
1112 
1113 /**
1114  * drm_atomic_helper_connector_hdmi_update_audio_infoframe - Update the Audio Infoframe
1115  * @connector: A pointer to the HDMI connector
1116  * @frame: A pointer to the audio infoframe to write
1117  *
1118  * This function is meant for HDMI connector drivers to update their
1119  * audio infoframe. It will typically be used in one of the ALSA hooks
1120  * (most likely prepare).
1121  *
1122  * Returns:
1123  * Zero on success, error code on failure.
1124  */
1125 int
1126 drm_atomic_helper_connector_hdmi_update_audio_infoframe(struct drm_connector *connector,
1127 							struct hdmi_audio_infoframe *frame)
1128 {
1129 	const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;
1130 	struct drm_connector_hdmi_infoframe *infoframe =
1131 		&connector->hdmi.infoframes.audio;
1132 	struct drm_display_info *info = &connector->display_info;
1133 	int ret;
1134 
1135 	if (!info->is_hdmi)
1136 		return 0;
1137 
1138 	if (!funcs || !funcs->audio.write_infoframe) {
1139 		drm_dbg_kms(connector->dev, "Function not implemented, bailing.\n");
1140 		return -EINVAL;
1141 	}
1142 
1143 	mutex_lock(&connector->hdmi.infoframes.lock);
1144 
1145 	BUILD_BUG_ON(sizeof(*frame) > sizeof(infoframe->data));
1146 	memcpy(&infoframe->data, frame, sizeof(*frame));
1147 	infoframe->set = true;
1148 
1149 	ret = write_infoframe(connector, &funcs->audio, "Audio", infoframe);
1150 
1151 	mutex_unlock(&connector->hdmi.infoframes.lock);
1152 
1153 	return ret;
1154 }
1155 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_update_audio_infoframe);
1156 
1157 /**
1158  * drm_atomic_helper_connector_hdmi_clear_audio_infoframe - Stop sending the Audio Infoframe
1159  * @connector: A pointer to the HDMI connector
1160  *
1161  * This function is meant for HDMI connector drivers to stop sending their
1162  * audio infoframe. It will typically be used in one of the ALSA hooks
1163  * (most likely shutdown).
1164  *
1165  * Returns:
1166  * Zero on success, error code on failure.
1167  */
1168 int
1169 drm_atomic_helper_connector_hdmi_clear_audio_infoframe(struct drm_connector *connector)
1170 {
1171 	const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;
1172 	struct drm_connector_hdmi_infoframe *infoframe =
1173 		&connector->hdmi.infoframes.audio;
1174 	struct drm_display_info *info = &connector->display_info;
1175 	int ret;
1176 
1177 	if (!info->is_hdmi)
1178 		return 0;
1179 
1180 	if (!funcs || !funcs->audio.write_infoframe) {
1181 		drm_dbg_kms(connector->dev, "Function not implemented, bailing.\n");
1182 		return -EINVAL;
1183 	}
1184 
1185 	mutex_lock(&connector->hdmi.infoframes.lock);
1186 
1187 	infoframe->set = false;
1188 
1189 	ret = clear_infoframe(connector, &funcs->audio, "Audio");
1190 
1191 	memset(&infoframe->data, 0, sizeof(infoframe->data));
1192 
1193 	mutex_unlock(&connector->hdmi.infoframes.lock);
1194 
1195 	return ret;
1196 }
1197 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_clear_audio_infoframe);
1198 
1199 static void
1200 drm_atomic_helper_connector_hdmi_update(struct drm_connector *connector,
1201 					enum drm_connector_status status)
1202 {
1203 	const struct drm_edid *drm_edid;
1204 
1205 	if (status == connector_status_disconnected) {
1206 		// TODO: also handle scramber, HDMI sink disconnected.
1207 		drm_connector_hdmi_audio_plugged_notify(connector, false);
1208 		drm_edid_connector_update(connector, NULL);
1209 		drm_connector_cec_phys_addr_invalidate(connector);
1210 		return;
1211 	}
1212 
1213 	if (connector->hdmi.funcs->read_edid)
1214 		drm_edid = connector->hdmi.funcs->read_edid(connector);
1215 	else
1216 		drm_edid = drm_edid_read(connector);
1217 
1218 	drm_edid_connector_update(connector, drm_edid);
1219 
1220 	drm_edid_free(drm_edid);
1221 
1222 	if (status == connector_status_connected) {
1223 		// TODO: also handle scramber, HDMI sink is now connected.
1224 		drm_connector_hdmi_audio_plugged_notify(connector, true);
1225 		drm_connector_cec_phys_addr_set(connector);
1226 	}
1227 }
1228 
1229 /**
1230  * drm_atomic_helper_connector_hdmi_hotplug - Handle the hotplug event for the HDMI connector
1231  * @connector: A pointer to the HDMI connector
1232  * @status: Connection status
1233  *
1234  * This function should be called as a part of the .detect() / .detect_ctx()
1235  * callbacks for all status changes.
1236  */
1237 void drm_atomic_helper_connector_hdmi_hotplug(struct drm_connector *connector,
1238 					      enum drm_connector_status status)
1239 {
1240 	drm_atomic_helper_connector_hdmi_update(connector, status);
1241 }
1242 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_hotplug);
1243 
1244 /**
1245  * drm_atomic_helper_connector_hdmi_force - HDMI Connector implementation of the force callback
1246  * @connector: A pointer to the HDMI connector
1247  *
1248  * This function implements the .force() callback for the HDMI connectors. It
1249  * can either be used directly as the callback or should be called from within
1250  * the .force() callback implementation to maintain the HDMI-specific
1251  * connector's data.
1252  */
1253 void drm_atomic_helper_connector_hdmi_force(struct drm_connector *connector)
1254 {
1255 	drm_atomic_helper_connector_hdmi_update(connector, connector->status);
1256 }
1257 EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_force);
1258