1 /*
2  * Copyright (C) 2012 Avionic Design GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sub license,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef __LINUX_HDMI_H_
25 #define __LINUX_HDMI_H_
26 
27 #include <linux/types.h>
28 #include <linux/device.h>
29 
30 enum hdmi_packet_type {
31 	HDMI_PACKET_TYPE_NULL = 0x00,
32 	HDMI_PACKET_TYPE_AUDIO_CLOCK_REGEN = 0x01,
33 	HDMI_PACKET_TYPE_AUDIO_SAMPLE = 0x02,
34 	HDMI_PACKET_TYPE_GENERAL_CONTROL = 0x03,
35 	HDMI_PACKET_TYPE_ACP = 0x04,
36 	HDMI_PACKET_TYPE_ISRC1 = 0x05,
37 	HDMI_PACKET_TYPE_ISRC2 = 0x06,
38 	HDMI_PACKET_TYPE_ONE_BIT_AUDIO_SAMPLE = 0x07,
39 	HDMI_PACKET_TYPE_DST_AUDIO = 0x08,
40 	HDMI_PACKET_TYPE_HBR_AUDIO_STREAM = 0x09,
41 	HDMI_PACKET_TYPE_GAMUT_METADATA = 0x0a,
42 	/* + enum hdmi_infoframe_type */
43 };
44 
45 enum hdmi_infoframe_type {
46 	HDMI_INFOFRAME_TYPE_VENDOR = 0x81,
47 	HDMI_INFOFRAME_TYPE_AVI = 0x82,
48 	HDMI_INFOFRAME_TYPE_SPD = 0x83,
49 	HDMI_INFOFRAME_TYPE_AUDIO = 0x84,
50 	HDMI_INFOFRAME_TYPE_DRM = 0x87,
51 };
52 
53 #define HDMI_IEEE_OUI 0x000c03
54 #define HDMI_FORUM_IEEE_OUI 0xc45dd8
55 #define HDMI_INFOFRAME_HEADER_SIZE  4
56 #define HDMI_AVI_INFOFRAME_SIZE    13
57 #define HDMI_SPD_INFOFRAME_SIZE    25
58 #define HDMI_AUDIO_INFOFRAME_SIZE  10
59 #define HDMI_DRM_INFOFRAME_SIZE    26
60 #define HDMI_VENDOR_INFOFRAME_SIZE  4
61 
62 /*
63  * HDMI 1.3a table 5-14 states that the largest InfoFrame_length is 27,
64  * not including the packet header or checksum byte. We include the
65  * checksum byte in HDMI_INFOFRAME_HEADER_SIZE, so this should allow
66  * HDMI_INFOFRAME_SIZE(MAX) to be the largest buffer we could ever need
67  * for any HDMI infoframe.
68  */
69 #define HDMI_MAX_INFOFRAME_SIZE    27
70 
71 #define HDMI_INFOFRAME_SIZE(type)	\
72 	(HDMI_INFOFRAME_HEADER_SIZE + HDMI_ ## type ## _INFOFRAME_SIZE)
73 
74 struct hdmi_any_infoframe {
75 	enum hdmi_infoframe_type type;
76 	unsigned char version;
77 	unsigned char length;
78 };
79 
80 enum hdmi_colorspace {
81 	HDMI_COLORSPACE_RGB,
82 	HDMI_COLORSPACE_YUV422,
83 	HDMI_COLORSPACE_YUV444,
84 	HDMI_COLORSPACE_YUV420,
85 	HDMI_COLORSPACE_RESERVED4,
86 	HDMI_COLORSPACE_RESERVED5,
87 	HDMI_COLORSPACE_RESERVED6,
88 	HDMI_COLORSPACE_IDO_DEFINED,
89 };
90 
91 enum hdmi_scan_mode {
92 	HDMI_SCAN_MODE_NONE,
93 	HDMI_SCAN_MODE_OVERSCAN,
94 	HDMI_SCAN_MODE_UNDERSCAN,
95 	HDMI_SCAN_MODE_RESERVED,
96 };
97 
98 enum hdmi_colorimetry {
99 	HDMI_COLORIMETRY_NONE,
100 	HDMI_COLORIMETRY_ITU_601,
101 	HDMI_COLORIMETRY_ITU_709,
102 	HDMI_COLORIMETRY_EXTENDED,
103 };
104 
105 enum hdmi_picture_aspect {
106 	HDMI_PICTURE_ASPECT_NONE,
107 	HDMI_PICTURE_ASPECT_4_3,
108 	HDMI_PICTURE_ASPECT_16_9,
109 	HDMI_PICTURE_ASPECT_64_27,
110 	HDMI_PICTURE_ASPECT_256_135,
111 	HDMI_PICTURE_ASPECT_RESERVED,
112 };
113 
114 enum hdmi_active_aspect {
115 	HDMI_ACTIVE_ASPECT_16_9_TOP = 2,
116 	HDMI_ACTIVE_ASPECT_14_9_TOP = 3,
117 	HDMI_ACTIVE_ASPECT_16_9_CENTER = 4,
118 	HDMI_ACTIVE_ASPECT_PICTURE = 8,
119 	HDMI_ACTIVE_ASPECT_4_3 = 9,
120 	HDMI_ACTIVE_ASPECT_16_9 = 10,
121 	HDMI_ACTIVE_ASPECT_14_9 = 11,
122 	HDMI_ACTIVE_ASPECT_4_3_SP_14_9 = 13,
123 	HDMI_ACTIVE_ASPECT_16_9_SP_14_9 = 14,
124 	HDMI_ACTIVE_ASPECT_16_9_SP_4_3 = 15,
125 };
126 
127 enum hdmi_extended_colorimetry {
128 	HDMI_EXTENDED_COLORIMETRY_XV_YCC_601,
129 	HDMI_EXTENDED_COLORIMETRY_XV_YCC_709,
130 	HDMI_EXTENDED_COLORIMETRY_S_YCC_601,
131 	HDMI_EXTENDED_COLORIMETRY_OPYCC_601,
132 	HDMI_EXTENDED_COLORIMETRY_OPRGB,
133 
134 	/* The following EC values are only defined in CEA-861-F. */
135 	HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM,
136 	HDMI_EXTENDED_COLORIMETRY_BT2020,
137 	HDMI_EXTENDED_COLORIMETRY_RESERVED,
138 };
139 
140 enum hdmi_quantization_range {
141 	HDMI_QUANTIZATION_RANGE_DEFAULT,
142 	HDMI_QUANTIZATION_RANGE_LIMITED,
143 	HDMI_QUANTIZATION_RANGE_FULL,
144 	HDMI_QUANTIZATION_RANGE_RESERVED,
145 };
146 
147 /* non-uniform picture scaling */
148 enum hdmi_nups {
149 	HDMI_NUPS_UNKNOWN,
150 	HDMI_NUPS_HORIZONTAL,
151 	HDMI_NUPS_VERTICAL,
152 	HDMI_NUPS_BOTH,
153 };
154 
155 enum hdmi_ycc_quantization_range {
156 	HDMI_YCC_QUANTIZATION_RANGE_LIMITED,
157 	HDMI_YCC_QUANTIZATION_RANGE_FULL,
158 };
159 
160 enum hdmi_content_type {
161 	HDMI_CONTENT_TYPE_GRAPHICS,
162 	HDMI_CONTENT_TYPE_PHOTO,
163 	HDMI_CONTENT_TYPE_CINEMA,
164 	HDMI_CONTENT_TYPE_GAME,
165 };
166 
167 enum hdmi_metadata_type {
168 	HDMI_STATIC_METADATA_TYPE1 = 0,
169 };
170 
171 enum hdmi_eotf {
172 	HDMI_EOTF_TRADITIONAL_GAMMA_SDR,
173 	HDMI_EOTF_TRADITIONAL_GAMMA_HDR,
174 	HDMI_EOTF_SMPTE_ST2084,
175 	HDMI_EOTF_BT_2100_HLG,
176 };
177 
178 struct hdmi_avi_infoframe {
179 	enum hdmi_infoframe_type type;
180 	unsigned char version;
181 	unsigned char length;
182 	bool itc;
183 	unsigned char pixel_repeat;
184 	enum hdmi_colorspace colorspace;
185 	enum hdmi_scan_mode scan_mode;
186 	enum hdmi_colorimetry colorimetry;
187 	enum hdmi_picture_aspect picture_aspect;
188 	enum hdmi_active_aspect active_aspect;
189 	enum hdmi_extended_colorimetry extended_colorimetry;
190 	enum hdmi_quantization_range quantization_range;
191 	enum hdmi_nups nups;
192 	unsigned char video_code;
193 	enum hdmi_ycc_quantization_range ycc_quantization_range;
194 	enum hdmi_content_type content_type;
195 	unsigned short top_bar;
196 	unsigned short bottom_bar;
197 	unsigned short left_bar;
198 	unsigned short right_bar;
199 };
200 
201 /* DRM Infoframe as per CTA 861.G spec */
202 struct hdmi_drm_infoframe {
203 	enum hdmi_infoframe_type type;
204 	unsigned char version;
205 	unsigned char length;
206 	enum hdmi_eotf eotf;
207 	enum hdmi_metadata_type metadata_type;
208 	struct {
209 		u16 x, y;
210 	} display_primaries[3];
211 	struct {
212 		u16 x, y;
213 	} white_point;
214 	u16 max_display_mastering_luminance;
215 	u16 min_display_mastering_luminance;
216 	u16 max_cll;
217 	u16 max_fall;
218 };
219 
220 void hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame);
221 ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame, void *buffer,
222 				size_t size);
223 ssize_t hdmi_avi_infoframe_pack_only(const struct hdmi_avi_infoframe *frame,
224 				     void *buffer, size_t size);
225 int hdmi_avi_infoframe_check(struct hdmi_avi_infoframe *frame);
226 int hdmi_drm_infoframe_init(struct hdmi_drm_infoframe *frame);
227 ssize_t hdmi_drm_infoframe_pack(struct hdmi_drm_infoframe *frame, void *buffer,
228 				size_t size);
229 ssize_t hdmi_drm_infoframe_pack_only(const struct hdmi_drm_infoframe *frame,
230 				     void *buffer, size_t size);
231 int hdmi_drm_infoframe_check(struct hdmi_drm_infoframe *frame);
232 int hdmi_drm_infoframe_unpack_only(struct hdmi_drm_infoframe *frame,
233 				   const void *buffer, size_t size);
234 
235 enum hdmi_spd_sdi {
236 	HDMI_SPD_SDI_UNKNOWN,
237 	HDMI_SPD_SDI_DSTB,
238 	HDMI_SPD_SDI_DVDP,
239 	HDMI_SPD_SDI_DVHS,
240 	HDMI_SPD_SDI_HDDVR,
241 	HDMI_SPD_SDI_DVC,
242 	HDMI_SPD_SDI_DSC,
243 	HDMI_SPD_SDI_VCD,
244 	HDMI_SPD_SDI_GAME,
245 	HDMI_SPD_SDI_PC,
246 	HDMI_SPD_SDI_BD,
247 	HDMI_SPD_SDI_SACD,
248 	HDMI_SPD_SDI_HDDVD,
249 	HDMI_SPD_SDI_PMP,
250 };
251 
252 struct hdmi_spd_infoframe {
253 	enum hdmi_infoframe_type type;
254 	unsigned char version;
255 	unsigned char length;
256 	char vendor[8];
257 	char product[16];
258 	enum hdmi_spd_sdi sdi;
259 };
260 
261 int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
262 			    const char *vendor, const char *product);
263 ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame, void *buffer,
264 				size_t size);
265 ssize_t hdmi_spd_infoframe_pack_only(const struct hdmi_spd_infoframe *frame,
266 				     void *buffer, size_t size);
267 int hdmi_spd_infoframe_check(struct hdmi_spd_infoframe *frame);
268 
269 enum hdmi_audio_coding_type {
270 	HDMI_AUDIO_CODING_TYPE_STREAM,
271 	HDMI_AUDIO_CODING_TYPE_PCM,
272 	HDMI_AUDIO_CODING_TYPE_AC3,
273 	HDMI_AUDIO_CODING_TYPE_MPEG1,
274 	HDMI_AUDIO_CODING_TYPE_MP3,
275 	HDMI_AUDIO_CODING_TYPE_MPEG2,
276 	HDMI_AUDIO_CODING_TYPE_AAC_LC,
277 	HDMI_AUDIO_CODING_TYPE_DTS,
278 	HDMI_AUDIO_CODING_TYPE_ATRAC,
279 	HDMI_AUDIO_CODING_TYPE_DSD,
280 	HDMI_AUDIO_CODING_TYPE_EAC3,
281 	HDMI_AUDIO_CODING_TYPE_DTS_HD,
282 	HDMI_AUDIO_CODING_TYPE_MLP,
283 	HDMI_AUDIO_CODING_TYPE_DST,
284 	HDMI_AUDIO_CODING_TYPE_WMA_PRO,
285 	HDMI_AUDIO_CODING_TYPE_CXT,
286 };
287 
288 enum hdmi_audio_sample_size {
289 	HDMI_AUDIO_SAMPLE_SIZE_STREAM,
290 	HDMI_AUDIO_SAMPLE_SIZE_16,
291 	HDMI_AUDIO_SAMPLE_SIZE_20,
292 	HDMI_AUDIO_SAMPLE_SIZE_24,
293 };
294 
295 enum hdmi_audio_sample_frequency {
296 	HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM,
297 	HDMI_AUDIO_SAMPLE_FREQUENCY_32000,
298 	HDMI_AUDIO_SAMPLE_FREQUENCY_44100,
299 	HDMI_AUDIO_SAMPLE_FREQUENCY_48000,
300 	HDMI_AUDIO_SAMPLE_FREQUENCY_88200,
301 	HDMI_AUDIO_SAMPLE_FREQUENCY_96000,
302 	HDMI_AUDIO_SAMPLE_FREQUENCY_176400,
303 	HDMI_AUDIO_SAMPLE_FREQUENCY_192000,
304 };
305 
306 enum hdmi_audio_coding_type_ext {
307 	/* Refer to Audio Coding Type (CT) field in Data Byte 1 */
308 	HDMI_AUDIO_CODING_TYPE_EXT_CT,
309 
310 	/*
311 	 * The next three CXT values are defined in CEA-861-E only.
312 	 * They do not exist in older versions, and in CEA-861-F they are
313 	 * defined as 'Not in use'.
314 	 */
315 	HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC,
316 	HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2,
317 	HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND,
318 
319 	/* The following CXT values are only defined in CEA-861-F. */
320 	HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC,
321 	HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_V2,
322 	HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC,
323 	HDMI_AUDIO_CODING_TYPE_EXT_DRA,
324 	HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_HE_AAC_SURROUND,
325 	HDMI_AUDIO_CODING_TYPE_EXT_MPEG4_AAC_LC_SURROUND = 10,
326 };
327 
328 struct hdmi_audio_infoframe {
329 	enum hdmi_infoframe_type type;
330 	unsigned char version;
331 	unsigned char length;
332 	unsigned char channels;
333 	enum hdmi_audio_coding_type coding_type;
334 	enum hdmi_audio_sample_size sample_size;
335 	enum hdmi_audio_sample_frequency sample_frequency;
336 	enum hdmi_audio_coding_type_ext coding_type_ext;
337 	unsigned char channel_allocation;
338 	unsigned char level_shift_value;
339 	bool downmix_inhibit;
340 
341 };
342 
343 int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame);
344 ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
345 				  void *buffer, size_t size);
346 ssize_t hdmi_audio_infoframe_pack_only(const struct hdmi_audio_infoframe *frame,
347 				       void *buffer, size_t size);
348 int hdmi_audio_infoframe_check(const struct hdmi_audio_infoframe *frame);
349 
350 struct dp_sdp;
351 ssize_t
352 hdmi_audio_infoframe_pack_for_dp(const struct hdmi_audio_infoframe *frame,
353 				 struct dp_sdp *sdp, u8 dp_version);
354 
355 enum hdmi_3d_structure {
356 	HDMI_3D_STRUCTURE_INVALID = -1,
357 	HDMI_3D_STRUCTURE_FRAME_PACKING = 0,
358 	HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE,
359 	HDMI_3D_STRUCTURE_LINE_ALTERNATIVE,
360 	HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL,
361 	HDMI_3D_STRUCTURE_L_DEPTH,
362 	HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH,
363 	HDMI_3D_STRUCTURE_TOP_AND_BOTTOM,
364 	HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF = 8,
365 };
366 
367 
368 struct hdmi_vendor_infoframe {
369 	enum hdmi_infoframe_type type;
370 	unsigned char version;
371 	unsigned char length;
372 	unsigned int oui;
373 	u8 vic;
374 	enum hdmi_3d_structure s3d_struct;
375 	unsigned int s3d_ext_data;
376 };
377 
378 /* HDR Metadata as per 861.G spec */
379 struct hdr_static_metadata {
380 	__u8 eotf;
381 	__u8 metadata_type;
382 	__u16 max_cll;
383 	__u16 max_fall;
384 	__u16 min_cll;
385 };
386 
387 /**
388  * struct hdr_sink_metadata - HDR sink metadata
389  *
390  * Metadata Information read from Sink's EDID
391  */
392 struct hdr_sink_metadata {
393 	/**
394 	 * @metadata_type: Static_Metadata_Descriptor_ID.
395 	 */
396 	__u32 metadata_type;
397 	/**
398 	 * @hdmi_type1: HDR Metadata Infoframe.
399 	 */
400 	union {
401 		struct hdr_static_metadata hdmi_type1;
402 	};
403 };
404 
405 int hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe *frame);
406 ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
407 				   void *buffer, size_t size);
408 ssize_t hdmi_vendor_infoframe_pack_only(const struct hdmi_vendor_infoframe *frame,
409 					void *buffer, size_t size);
410 int hdmi_vendor_infoframe_check(struct hdmi_vendor_infoframe *frame);
411 
412 union hdmi_vendor_any_infoframe {
413 	struct {
414 		enum hdmi_infoframe_type type;
415 		unsigned char version;
416 		unsigned char length;
417 		unsigned int oui;
418 	} any;
419 	struct hdmi_vendor_infoframe hdmi;
420 };
421 
422 /**
423  * union hdmi_infoframe - overall union of all abstract infoframe representations
424  * @any: generic infoframe
425  * @avi: avi infoframe
426  * @spd: spd infoframe
427  * @vendor: union of all vendor infoframes
428  * @audio: audio infoframe
429  * @drm: Dynamic Range and Mastering infoframe
430  *
431  * This is used by the generic pack function. This works since all infoframes
432  * have the same header which also indicates which type of infoframe should be
433  * packed.
434  */
435 union hdmi_infoframe {
436 	struct hdmi_any_infoframe any;
437 	struct hdmi_avi_infoframe avi;
438 	struct hdmi_spd_infoframe spd;
439 	union hdmi_vendor_any_infoframe vendor;
440 	struct hdmi_audio_infoframe audio;
441 	struct hdmi_drm_infoframe drm;
442 };
443 
444 ssize_t hdmi_infoframe_pack(union hdmi_infoframe *frame, void *buffer,
445 			    size_t size);
446 ssize_t hdmi_infoframe_pack_only(const union hdmi_infoframe *frame,
447 				 void *buffer, size_t size);
448 int hdmi_infoframe_check(union hdmi_infoframe *frame);
449 int hdmi_infoframe_unpack(union hdmi_infoframe *frame,
450 			  const void *buffer, size_t size);
451 void hdmi_infoframe_log(const char *level, struct device *dev,
452 			const union hdmi_infoframe *frame);
453 
454 #endif /* _DRM_HDMI_H */
455