xref: /rk3399_rockchip-uboot/include/edid.h (revision c879435248277e3f357c3d9f45f12cde1b408085)
1 /*
2  * Copyright (c) 2012 The Chromium OS Authors.
3  *
4  * (C) Copyright 2010
5  * Petr Stetiar <ynezz@true.cz>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  *
9  * Contains stolen code from ddcprobe project which is:
10  * Copyright (C) Nalin Dahyabhai <bigfun@pobox.com>
11  * (C) Copyright 2008-2017 Fuzhou Rockchip Electronics Co., Ltd
12  */
13 
14 #ifndef __EDID_H_
15 #define __EDID_H_
16 
17 #include <div64.h>
18 #include <linux/types.h>
19 #include <drm_modes.h>
20 #include <i2c.h>
21 
22 /* Size of the EDID data */
23 #define EDID_SIZE	128
24 #define EDID_EXT_SIZE	256
25 #define MODE_LEN	240
26 
27 #define CEA_EXT	    0x02
28 #define VTB_EXT	    0x10
29 #define DI_EXT	    0x40
30 #define LS_EXT	    0x50
31 #define MI_EXT	    0x60
32 #define DISPLAYID_EXT 0x70
33 
34 #define EDID_TIMING_ASPECT_SHIFT 6
35 #define EDID_TIMING_ASPECT_MASK  (0x3 << EDID_TIMING_ASPECT_SHIFT)
36 
37 /* need to add 60 */
38 #define EDID_TIMING_VFREQ_SHIFT  0
39 #define EDID_TIMING_VFREQ_MASK   (0x3f << EDID_TIMING_VFREQ_SHIFT)
40 
41 /* OUI of HDMI vendor specific data block */
42 #define HDMI_IEEE_OUI 0x000c03
43 
44 /* drm mode 4k and 3d */
45 #define DRM_MODE_FLAG_420_MASK			(0x03 << 23)
46 #define  DRM_MODE_FLAG_420			BIT(23)
47 #define  DRM_MODE_FLAG_420_ONLY			BIT(24)
48 
49 #define DRM_MODE_FLAG_3D_MASK                  (0x1f << 14)
50 #define  DRM_MODE_FLAG_3D_NONE                 (0 << 14)
51 #define  DRM_MODE_FLAG_3D_FRAME_PACKING                BIT(14)
52 #define  DRM_MODE_FLAG_3D_FIELD_ALTERNATIVE    (2 << 14)
53 #define  DRM_MODE_FLAG_3D_LINE_ALTERNATIVE     (3 << 14)
54 #define  DRM_MODE_FLAG_3D_SIDE_BY_SIDE_FULL    (4 << 14)
55 #define  DRM_MODE_FLAG_3D_L_DEPTH              (5 << 14)
56 #define  DRM_MODE_FLAG_3D_L_DEPTH_GFX_GFX_DEPTH        (6 << 14)
57 #define  DRM_MODE_FLAG_3D_TOP_AND_BOTTOM       (7 << 14)
58 #define  DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF    (8 << 14)
59 
60 #define BITS_PER_BYTE         8
61 #define BITS_TO_LONGS(nr)     DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
62 #define GET_BIT(_x, _pos) \
63 	(((_x) >> (_pos)) & 1)
64 #define GET_BITS(_x, _pos_msb, _pos_lsb) \
65 	(((_x) >> (_pos_lsb)) & ((1 << ((_pos_msb) - (_pos_lsb) + 1)) - 1))
66 #define DRM_MODE(t, c, hd, hss, hse, ht, vd, vss, vse, vt, vs, f) \
67 	.clock = (c), .type = (t),\
68 	.hdisplay = (hd), .hsync_start = (hss), .hsync_end = (hse), \
69 	.htotal = (ht), .vdisplay = (vd), \
70 	.vsync_start = (vss), .vsync_end = (vse), .vtotal = (vt), \
71 	.vscan = (vs), .flags = (f)
72 
73 #define DDC_SEGMENT_ADDR 0x30
74 #define DDC_ADDR 0x50
75 #define HDMI_EDID_BLOCK_SIZE 128
76 #define SCDC_I2C_SLAVE_ADDRESS 0x54
77 
78 /* Aspect ratios used in EDID info. */
79 enum edid_aspect {
80 	ASPECT_625 = 0,
81 	ASPECT_75,
82 	ASPECT_8,
83 	ASPECT_5625,
84 };
85 
86 struct est_timings {
87 	u8 t1;
88 	u8 t2;
89 	u8 mfg_rsvd;
90 } __packed;
91 
92 /* 00=16:10, 01=4:3, 10=5:4, 11=16:9 */
93 #define EDID_TIMING_ASPECT_SHIFT 6
94 #define EDID_TIMING_ASPECT_MASK  (0x3 << EDID_TIMING_ASPECT_SHIFT)
95 
96 /* need to add 60 */
97 #define EDID_TIMING_VFREQ_SHIFT  0
98 #define EDID_TIMING_VFREQ_MASK   (0x3f << EDID_TIMING_VFREQ_SHIFT)
99 
100 struct std_timing {
101 	u8 hsize; /* need to multiply by 8 then add 248 */
102 	u8 vfreq_aspect;
103 } __packed;
104 
105 struct detailed_pixel_timing {
106 	u8 hactive_lo;
107 	u8 hblank_lo;
108 	u8 hactive_hblank_hi;
109 	u8 vactive_lo;
110 	u8 vblank_lo;
111 	u8 vactive_vblank_hi;
112 	u8 hsync_offset_lo;
113 	u8 hsync_pulse_width_lo;
114 	u8 vsync_offset_pulse_width_lo;
115 	u8 hsync_vsync_offset_pulse_width_hi;
116 	u8 width_mm_lo;
117 	u8 height_mm_lo;
118 	u8 width_height_mm_hi;
119 	u8 hborder;
120 	u8 vborder;
121 	u8 misc;
122 } __packed;
123 
124 /* If it's not pixel timing, it'll be one of the below */
125 struct detailed_data_string {
126 	u8 str[13];
127 } __packed;
128 
129 struct detailed_data_monitor_range {
130 	u8 min_vfreq;
131 	u8 max_vfreq;
132 	u8 min_hfreq_khz;
133 	u8 max_hfreq_khz;
134 	u8 pixel_clock_mhz; /* need to multiply by 10 */
135 	u8 flags;
136 	union {
137 		struct {
138 			u8 reserved;
139 			u8 hfreq_start_khz; /* need to multiply by 2 */
140 			u8 c; /* need to divide by 2 */
141 			__le16 m;
142 			u8 k;
143 			u8 j; /* need to divide by 2 */
144 		} __packed gtf2;
145 		struct {
146 			u8 version;
147 			u8 data1; /* high 6 bits: extra clock resolution */
148 			u8 data2; /* plus low 2 of above: max hactive */
149 			u8 supported_aspects;
150 			u8 flags; /* preferred aspect and blanking support */
151 			u8 supported_scalings;
152 			u8 preferred_refresh;
153 		} __packed cvt;
154 	} formula;
155 } __packed;
156 
157 struct detailed_data_wpindex {
158 	u8 white_yx_lo; /* Lower 2 bits each */
159 	u8 white_x_hi;
160 	u8 white_y_hi;
161 	u8 gamma; /* need to divide by 100 then add 1 */
162 } __packed;
163 
164 struct detailed_data_color_point {
165 	u8 windex1;
166 	u8 wpindex1[3];
167 	u8 windex2;
168 	u8 wpindex2[3];
169 } __packed;
170 
171 struct cvt_timing {
172 	u8 code[3];
173 } __packed;
174 
175 struct detailed_non_pixel {
176 	u8 pad1;
177 	u8 type; /* ff=serial, fe=string, fd=monitor range, fc=monitor name
178 		  * fb=color point data, fa=standard timing data,
179 		  * f9=undefined, f8=mfg. reserved
180 		  */
181 	u8 pad2;
182 	union {
183 		struct detailed_data_string str;
184 		struct detailed_data_monitor_range range;
185 		struct detailed_data_wpindex color;
186 		struct std_timing timings[6];
187 		struct cvt_timing cvt[4];
188 	} data;
189 } __packed;
190 
191 #define EDID_DETAIL_EST_TIMINGS 0xf7
192 #define EDID_DETAIL_CVT_3BYTE 0xf8
193 #define EDID_DETAIL_COLOR_MGMT_DATA 0xf9
194 #define EDID_DETAIL_STD_MODES 0xfa
195 #define EDID_DETAIL_MONITOR_CPDATA 0xfb
196 #define EDID_DETAIL_MONITOR_NAME 0xfc
197 #define EDID_DETAIL_MONITOR_RANGE 0xfd
198 #define EDID_DETAIL_MONITOR_STRING 0xfe
199 #define EDID_DETAIL_MONITOR_SERIAL 0xff
200 
201 struct detailed_timing {
202 	__le16 pixel_clock; /* need to multiply by 10 KHz */
203 	union {
204 		struct detailed_pixel_timing pixel_data;
205 		struct detailed_non_pixel other_data;
206 	} data;
207 } __packed;
208 
209 /* Detailed timing information used in EDID v1.x */
210 struct edid_detailed_timing {
211 	unsigned char pixel_clock[2];
212 #define EDID_DETAILED_TIMING_PIXEL_CLOCK(_x) \
213 	(((((uint32_t)(_x).pixel_clock[1]) << 8) + \
214 	 (_x).pixel_clock[0]) * 10000)
215 	unsigned char horizontal_active;
216 	unsigned char horizontal_blanking;
217 	unsigned char horizontal_active_blanking_hi;
218 #define EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(_x) \
219 	((GET_BITS((_x).horizontal_active_blanking_hi, 7, 4) << 8) + \
220 	 (_x).horizontal_active)
221 #define EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(_x) \
222 	((GET_BITS((_x).horizontal_active_blanking_hi, 3, 0) << 8) + \
223 	 (_x).horizontal_blanking)
224 	unsigned char vertical_active;
225 	unsigned char vertical_blanking;
226 	unsigned char vertical_active_blanking_hi;
227 #define EDID_DETAILED_TIMING_VERTICAL_ACTIVE(_x) \
228 	((GET_BITS((_x).vertical_active_blanking_hi, 7, 4) << 8) + \
229 	 (_x).vertical_active)
230 #define EDID_DETAILED_TIMING_VERTICAL_BLANKING(_x) \
231 	((GET_BITS((_x).vertical_active_blanking_hi, 3, 0) << 8) + \
232 	 (_x).vertical_blanking)
233 	unsigned char hsync_offset;
234 	unsigned char hsync_pulse_width;
235 	unsigned char vsync_offset_pulse_width;
236 	unsigned char hsync_vsync_offset_pulse_width_hi;
237 #define EDID_DETAILED_TIMING_HSYNC_OFFSET(_x) \
238 	((GET_BITS((_x).hsync_vsync_offset_pulse_width_hi, 7, 6) << 8) + \
239 	 (_x).hsync_offset)
240 #define EDID_DETAILED_TIMING_HSYNC_PULSE_WIDTH(_x) \
241 	((GET_BITS((_x).hsync_vsync_offset_pulse_width_hi, 5, 4) << 8) + \
242 	 (_x).hsync_pulse_width)
243 #define EDID_DETAILED_TIMING_VSYNC_OFFSET(_x) \
244 	((GET_BITS((_x).hsync_vsync_offset_pulse_width_hi, 3, 2) << 4) + \
245 	 GET_BITS((_x).vsync_offset_pulse_width, 7, 4))
246 #define EDID_DETAILED_TIMING_VSYNC_PULSE_WIDTH(_x) \
247 	((GET_BITS((_x).hsync_vsync_offset_pulse_width_hi, 1, 0) << 4) + \
248 	 GET_BITS((_x).vsync_offset_pulse_width, 3, 0))
249 	unsigned char himage_size;
250 	unsigned char vimage_size;
251 	unsigned char himage_vimage_size_hi;
252 #define EDID_DETAILED_TIMING_HIMAGE_SIZE(_x) \
253 	((GET_BITS((_x).himage_vimage_size_hi, 7, 4) << 8) + (_x).himage_size)
254 #define EDID_DETAILED_TIMING_VIMAGE_SIZE(_x) \
255 	((GET_BITS((_x).himage_vimage_size_hi, 3, 0) << 8) + (_x).vimage_size)
256 	unsigned char hborder;
257 	unsigned char vborder;
258 	unsigned char flags;
259 #define EDID_DETAILED_TIMING_FLAG_INTERLACED(_x) \
260 	GET_BIT((_x).flags, 7)
261 #define EDID_DETAILED_TIMING_FLAG_STEREO(_x) \
262 	GET_BITS((_x).flags, 6, 5)
263 #define EDID_DETAILED_TIMING_FLAG_DIGITAL_COMPOSITE(_x) \
264 	GET_BITS((_x).flags, 4, 3)
265 #define EDID_DETAILED_TIMING_FLAG_POLARITY(_x) \
266 	GET_BITS((_x).flags, 2, 1)
267 #define EDID_DETAILED_TIMING_FLAG_VSYNC_POLARITY(_x) \
268 	GET_BIT((_x).flags, 2)
269 #define EDID_DETAILED_TIMING_FLAG_HSYNC_POLARITY(_x) \
270 	GET_BIT((_x).flags, 1)
271 #define EDID_DETAILED_TIMING_FLAG_INTERLEAVED(_x) \
272 	GET_BIT((_x).flags, 0)
273 } __attribute__ ((__packed__));
274 
275 enum edid_monitor_descriptor_types {
276 	EDID_MONITOR_DESCRIPTOR_SERIAL = 0xff,
277 	EDID_MONITOR_DESCRIPTOR_ASCII = 0xfe,
278 	EDID_MONITOR_DESCRIPTOR_RANGE = 0xfd,
279 	EDID_MONITOR_DESCRIPTOR_NAME = 0xfc,
280 };
281 
282 struct edid_monitor_descriptor {
283 	uint16_t zero_flag_1;
284 	unsigned char zero_flag_2;
285 	unsigned char type;
286 	unsigned char zero_flag_3;
287 	union {
288 		char string[13];
289 		struct {
290 			unsigned char vertical_min;
291 			unsigned char vertical_max;
292 			unsigned char horizontal_min;
293 			unsigned char horizontal_max;
294 			unsigned char pixel_clock_max;
295 			unsigned char gtf_data[8];
296 		} range_data;
297 	} data;
298 } __attribute__ ((__packed__));
299 
300 #define DRM_EDID_INPUT_SERRATION_VSYNC (1 << 0)
301 #define DRM_EDID_INPUT_SYNC_ON_GREEN   (1 << 1)
302 #define DRM_EDID_INPUT_COMPOSITE_SYNC  (1 << 2)
303 #define DRM_EDID_INPUT_SEPARATE_SYNCS  (1 << 3)
304 #define DRM_EDID_INPUT_BLANK_TO_BLACK  (1 << 4)
305 #define DRM_EDID_INPUT_VIDEO_LEVEL     (3 << 5)
306 #define DRM_EDID_INPUT_DIGITAL         (1 << 7)
307 #define DRM_EDID_DIGITAL_DEPTH_MASK    (7 << 4)
308 #define DRM_EDID_DIGITAL_DEPTH_UNDEF   (0 << 4)
309 #define DRM_EDID_DIGITAL_DEPTH_6       (1 << 4)
310 #define DRM_EDID_DIGITAL_DEPTH_8       (2 << 4)
311 #define DRM_EDID_DIGITAL_DEPTH_10      (3 << 4)
312 #define DRM_EDID_DIGITAL_DEPTH_12      (4 << 4)
313 #define DRM_EDID_DIGITAL_DEPTH_14      (5 << 4)
314 #define DRM_EDID_DIGITAL_DEPTH_16      (6 << 4)
315 #define DRM_EDID_DIGITAL_DEPTH_RSVD    (7 << 4)
316 #define DRM_EDID_DIGITAL_TYPE_UNDEF    (0)
317 #define DRM_EDID_DIGITAL_TYPE_DVI      (1)
318 #define DRM_EDID_DIGITAL_TYPE_HDMI_A   (2)
319 #define DRM_EDID_DIGITAL_TYPE_HDMI_B   (3)
320 #define DRM_EDID_DIGITAL_TYPE_MDDI     (4)
321 #define DRM_EDID_DIGITAL_TYPE_DP       (5)
322 
323 #define DRM_EDID_FEATURE_DEFAULT_GTF      (1 << 0)
324 #define DRM_EDID_FEATURE_PREFERRED_TIMING (1 << 1)
325 #define DRM_EDID_FEATURE_STANDARD_COLOR   (1 << 2)
326 /* If analog */
327 /* 00=mono, 01=rgb, 10=non-rgb, 11=unknown */
328 #define DRM_EDID_FEATURE_DISPLAY_TYPE     (3 << 3)
329 /* If digital */
330 #define DRM_EDID_FEATURE_COLOR_MASK	  (3 << 3)
331 #define DRM_EDID_FEATURE_RGB		  (0 << 3)
332 #define DRM_EDID_FEATURE_RGB_YCRCB444	  (1 << 3)
333 #define DRM_EDID_FEATURE_RGB_YCRCB422	  (2 << 3)
334 /* both 4:4:4 and 4:2:2 */
335 #define DRM_EDID_FEATURE_RGB_YCRCB	  (3 << 3)
336 
337 #define DRM_EDID_FEATURE_PM_ACTIVE_OFF    (1 << 5)
338 #define DRM_EDID_FEATURE_PM_SUSPEND       (1 << 6)
339 #define DRM_EDID_FEATURE_PM_STANDBY       (1 << 7)
340 
341 #define DRM_EDID_HDMI_DC_48               (1 << 6)
342 #define DRM_EDID_HDMI_DC_36               (1 << 5)
343 #define DRM_EDID_HDMI_DC_30               (1 << 4)
344 #define DRM_EDID_HDMI_DC_Y444             (1 << 3)
345 
346 /* YCBCR 420 deep color modes */
347 #define DRM_EDID_YCBCR420_DC_48		  (1 << 2)
348 #define DRM_EDID_YCBCR420_DC_36		  (1 << 1)
349 #define DRM_EDID_YCBCR420_DC_30		  (1 << 0)
350 #define DRM_EDID_YCBCR420_DC_MASK (DRM_EDID_YCBCR420_DC_48 | \
351 				    DRM_EDID_YCBCR420_DC_36 | \
352 				    DRM_EDID_YCBCR420_DC_30)
353 
354 struct edid1_info {
355 	unsigned char header[8];
356 	unsigned char manufacturer_name[2];
357 #define EDID1_INFO_MANUFACTURER_NAME_ZERO(_x) \
358 	GET_BIT(((_x).manufacturer_name[0]), 7)
359 #define EDID1_INFO_MANUFACTURER_NAME_CHAR1(_x) \
360 	GET_BITS(((_x).manufacturer_name[0]), 6, 2)
361 #define EDID1_INFO_MANUFACTURER_NAME_CHAR2(_x) \
362 	((GET_BITS(((_x).manufacturer_name[0]), 1, 0) << 3) + \
363 	 GET_BITS(((_x).manufacturer_name[1]), 7, 5))
364 #define EDID1_INFO_MANUFACTURER_NAME_CHAR3(_x) \
365 	GET_BITS(((_x).manufacturer_name[1]), 4, 0)
366 	unsigned char product_code[2];
367 #define EDID1_INFO_PRODUCT_CODE(_x) \
368 	(((uint16_t)(_x).product_code[1] << 8) + (_x).product_code[0])
369 	unsigned char serial_number[4];
370 #define EDID1_INFO_SERIAL_NUMBER(_x) \
371 	(((uint32_t)(_x).serial_number[3] << 24) + \
372 	 ((_x).serial_number[2] << 16) + ((_x).serial_number[1] << 8) + \
373 	 (_x).serial_number[0])
374 	unsigned char week;
375 	unsigned char year;
376 	unsigned char version;
377 	unsigned char revision;
378 	unsigned char video_input_definition;
379 #define EDID1_INFO_VIDEO_INPUT_DIGITAL(_x) \
380 	GET_BIT(((_x).video_input_definition), 7)
381 #define EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(_x) \
382 	GET_BITS(((_x).video_input_definition), 6, 5)
383 #define EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(_x) \
384 	GET_BIT(((_x).video_input_definition), 4)
385 #define EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(_x) \
386 	GET_BIT(((_x).video_input_definition), 3)
387 #define EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(_x) \
388 	GET_BIT(((_x).video_input_definition), 2)
389 #define EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(_x) \
390 	GET_BIT(((_x).video_input_definition), 1)
391 #define EDID1_INFO_VIDEO_INPUT_SERRATION_V(_x) \
392 	GET_BIT(((_x).video_input_definition), 0)
393 	unsigned char max_size_horizontal;
394 	unsigned char max_size_vertical;
395 	unsigned char gamma;
396 	unsigned char feature_support;
397 #define EDID1_INFO_FEATURE_STANDBY(_x) \
398 	GET_BIT(((_x).feature_support), 7)
399 #define EDID1_INFO_FEATURE_SUSPEND(_x) \
400 	GET_BIT(((_x).feature_support), 6)
401 #define EDID1_INFO_FEATURE_ACTIVE_OFF(_x) \
402 	GET_BIT(((_x).feature_support), 5)
403 #define EDID1_INFO_FEATURE_DISPLAY_TYPE(_x) \
404 	GET_BITS(((_x).feature_support), 4, 3)
405 #define EDID1_INFO_FEATURE_RGB(_x) \
406 	GET_BIT(((_x).feature_support), 2)
407 #define EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(_x) \
408 	GET_BIT(((_x).feature_support), 1)
409 #define EDID1_INFO_FEATURE_DEFAULT_GTF_SUPPORT(_x) \
410 	GET_BIT(((_x).feature_support), 0)
411 	unsigned char color_characteristics[10];
412 	unsigned char established_timings[3];
413 #define EDID1_INFO_ESTABLISHED_TIMING_720X400_70(_x) \
414 	GET_BIT(((_x).established_timings[0]), 7)
415 #define EDID1_INFO_ESTABLISHED_TIMING_720X400_88(_x) \
416 	GET_BIT(((_x).established_timings[0]), 6)
417 #define EDID1_INFO_ESTABLISHED_TIMING_640X480_60(_x) \
418 	GET_BIT(((_x).established_timings[0]), 5)
419 #define EDID1_INFO_ESTABLISHED_TIMING_640X480_67(_x) \
420 	GET_BIT(((_x).established_timings[0]), 4)
421 #define EDID1_INFO_ESTABLISHED_TIMING_640X480_72(_x) \
422 	GET_BIT(((_x).established_timings[0]), 3)
423 #define EDID1_INFO_ESTABLISHED_TIMING_640X480_75(_x) \
424 	GET_BIT(((_x).established_timings[0]), 2)
425 #define EDID1_INFO_ESTABLISHED_TIMING_800X600_56(_x) \
426 	GET_BIT(((_x).established_timings[0]), 1)
427 #define EDID1_INFO_ESTABLISHED_TIMING_800X600_60(_x) \
428 	GET_BIT(((_x).established_timings[0]), 0)
429 #define EDID1_INFO_ESTABLISHED_TIMING_800X600_72(_x) \
430 	GET_BIT(((_x).established_timings[1]), 7)
431 #define EDID1_INFO_ESTABLISHED_TIMING_800X600_75(_x) \
432 	GET_BIT(((_x).established_timings[1]), 6)
433 #define EDID1_INFO_ESTABLISHED_TIMING_832X624_75(_x) \
434 	GET_BIT(((_x).established_timings[1]), 5)
435 #define EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(_x) \
436 	GET_BIT(((_x).established_timings[1]), 4)
437 #define EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(_x) \
438 	GET_BIT(((_x).established_timings[1]), 3)
439 #define EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(_x) \
440 	GET_BIT(((_x).established_timings[1]), 2)
441 #define EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(_x) \
442 	GET_BIT(((_x).established_timings[1]), 1)
443 #define EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(_x) \
444 	GET_BIT(((_x).established_timings[1]), 0)
445 #define EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(_x) \
446 	GET_BIT(((_x).established_timings[2]), 7)
447 	struct {
448 		unsigned char xresolution;
449 		unsigned char aspect_vfreq;
450 	} __attribute__((__packed__)) standard_timings[8];
451 #define EDID1_INFO_STANDARD_TIMING_XRESOLUTION(_x, _i) \
452 	(((_x).standard_timings[_i]).xresolution)
453 #define EDID1_INFO_STANDARD_TIMING_ASPECT(_x, _i) \
454 	GET_BITS(((_x).standard_timings[_i].aspect_vfreq), 7, 6)
455 #define EDID1_INFO_STANDARD_TIMING_VFREQ(_x, _i) \
456 	GET_BITS(((_x).standard_timings[_i].aspect_vfreq), 5, 0)
457 	union {
458 		unsigned char timing[72];
459 		struct edid_monitor_descriptor descriptor[4];
460 	} monitor_details;
461 	unsigned char extension_flag;
462 	unsigned char checksum;
463 } __attribute__ ((__packed__));
464 
465 enum edid_cea861_db_types {
466 	EDID_CEA861_DB_AUDIO = 0x01,
467 	EDID_CEA861_DB_VIDEO = 0x02,
468 	EDID_CEA861_DB_VENDOR = 0x03,
469 	EDID_CEA861_DB_SPEAKER = 0x04,
470 	EDID_CEA861_DB_USE_EXTENDED = 0x07,
471 };
472 
473 #define EXT_VIDEO_CAPABILITY_BLOCK 0x00
474 #define EXT_VIDEO_DATA_BLOCK_420        0x0E
475 #define EXT_VIDEO_CAP_BLOCK_Y420CMDB 0x0F
476 #define EDID_BASIC_AUDIO        BIT(6)
477 #define EDID_CEA_YCRCB444       BIT(5)
478 #define EDID_CEA_YCRCB422       BIT(4)
479 #define EDID_CEA_VCDB_QS        BIT(6)
480 
481 #define EXT_VIDEO_DATA_BLOCK_420 0x0E
482 
483 struct edid_cea861_info {
484 	unsigned char extension_tag;
485 #define EDID_CEA861_EXTENSION_TAG	0x02
486 	unsigned char revision;
487 	unsigned char dtd_offset;
488 	unsigned char dtd_count;
489 #define EDID_CEA861_SUPPORTS_UNDERSCAN(_x) \
490 	GET_BIT(((_x).dtd_count), 7)
491 #define EDID_CEA861_SUPPORTS_BASIC_AUDIO(_x) \
492 	GET_BIT(((_x).dtd_count), 6)
493 #define EDID_CEA861_SUPPORTS_YUV444(_x) \
494 	GET_BIT(((_x).dtd_count), 5)
495 #define EDID_CEA861_SUPPORTS_YUV422(_x) \
496 	GET_BIT(((_x).dtd_count), 4)
497 #define EDID_CEA861_DTD_COUNT(_x) \
498 	GET_BITS(((_x).dtd_count), 3, 0)
499 	unsigned char data[124];
500 #define EDID_CEA861_DB_TYPE(_x, offset) \
501 	GET_BITS((_x).data[offset], 7, 5)
502 #define EDID_CEA861_DB_LEN(_x, offset) \
503 	GET_BITS((_x).data[offset], 4, 0)
504 } __attribute__ ((__packed__));
505 
506 #define DATA_BLOCK_PRODUCT_ID 0x00
507 #define DATA_BLOCK_DISPLAY_PARAMETERS 0x01
508 #define DATA_BLOCK_COLOR_CHARACTERISTICS 0x02
509 #define DATA_BLOCK_TYPE_1_DETAILED_TIMING 0x03
510 #define DATA_BLOCK_TYPE_2_DETAILED_TIMING 0x04
511 #define DATA_BLOCK_TYPE_3_SHORT_TIMING 0x05
512 #define DATA_BLOCK_TYPE_4_DMT_TIMING 0x06
513 #define DATA_BLOCK_VESA_TIMING 0x07
514 #define DATA_BLOCK_CEA_TIMING 0x08
515 #define DATA_BLOCK_VIDEO_TIMING_RANGE 0x09
516 #define DATA_BLOCK_PRODUCT_SERIAL_NUMBER 0x0a
517 #define DATA_BLOCK_GP_ASCII_STRING 0x0b
518 #define DATA_BLOCK_DISPLAY_DEVICE_DATA 0x0c
519 #define DATA_BLOCK_INTERFACE_POWER_SEQUENCING 0x0d
520 #define DATA_BLOCK_TRANSFER_CHARACTERISTICS 0x0e
521 #define DATA_BLOCK_DISPLAY_INTERFACE 0x0f
522 #define DATA_BLOCK_STEREO_DISPLAY_INTERFACE 0x10
523 #define DATA_BLOCK_TILED_DISPLAY 0x12
524 
525 struct displayid_hdr {
526 	u8 rev;
527 	u8 bytes;
528 	u8 prod_id;
529 	u8 ext_count;
530 } __packed;
531 
532 struct displayid_block {
533 	u8 tag;
534 	u8 rev;
535 	u8 num_bytes;
536 } __packed;
537 
538 struct displayid_detailed_timings_1 {
539 	u8 pixel_clock[3];
540 	u8 flags;
541 	u8 hactive[2];
542 	u8 hblank[2];
543 	u8 hsync[2];
544 	u8 hsw[2];
545 	u8 vactive[2];
546 	u8 vblank[2];
547 	u8 vsync[2];
548 	u8 vsw[2];
549 } __packed;
550 
551 struct displayid_detailed_timing_block {
552 	struct displayid_block base;
553 	struct displayid_detailed_timings_1 timings[0];
554 };
555 
556 /**
557  * struct drm_scrambling: sink's scrambling support.
558  */
559 struct drm_scrambling {
560 	/**
561 	 * @supported: scrambling supported for rates > 340 Mhz.
562 	 */
563 	bool supported;
564 	/**
565 	 * @low_rates: scrambling supported for rates <= 340 Mhz.
566 	 */
567 	bool low_rates;
568 };
569 
570 /**
571  * struct drm_scdc - Information about scdc capabilities of a HDMI 2.0 sink
572  *
573  * Provides SCDC register support and capabilities related information on a
574  * HDMI 2.0 sink. In case of a HDMI 1.4 sink, all parameter must be 0.
575  */
576 
577 struct drm_scdc {
578 	/**
579 	 * @supported: status control & data channel present.
580 	 */
581 	bool supported;
582 	/**
583 	 * @read_request: sink is capable of generating scdc read request.
584 	 */
585 	bool read_request;
586 	/**
587 	 * @scrambling: sink's scrambling capabilities
588 	 */
589 	struct drm_scrambling scrambling;
590 };
591 
592 /**
593  * struct drm_hdmi_info - runtime information about the connected HDMI sink
594  *
595  * Describes if a given display supports advanced HDMI 2.0 features.
596  * This information is available in CEA-861-F extension blocks (like HF-VSDB).
597  */
598 struct drm_hdmi_info {
599 	struct drm_scdc scdc;
600 
601 	/**
602 	 * @y420_vdb_modes: bitmap of modes which can support ycbcr420
603 	 * output only (not normal RGB/YCBCR444/422 outputs). There are total
604 	 * 107 VICs defined by CEA-861-F spec, so the size is 128 bits to map
605 	 * upto 128 VICs;
606 	 */
607 	unsigned long y420_vdb_modes[BITS_TO_LONGS(128)];
608 
609 	/**
610 	 * @y420_cmdb_modes: bitmap of modes which can support ycbcr420
611 	 * output also, along with normal HDMI outputs. There are total 107
612 	 * VICs defined by CEA-861-F spec, so the size is 128 bits to map upto
613 	 * 128 VICs;
614 	 */
615 	unsigned long y420_cmdb_modes[BITS_TO_LONGS(128)];
616 
617 	/** @y420_cmdb_map: bitmap of SVD index, to extraxt vcb modes */
618 	u64 y420_cmdb_map;
619 
620 	/** @y420_dc_modes: bitmap of deep color support index */
621 	u8 y420_dc_modes;
622 };
623 
624 enum subpixel_order {
625 	subpixelunknown = 0,
626 	subpixelhorizontalrgb,
627 	subpixelhorizontalbgr,
628 	subpixelverticalrgb,
629 	subpixelverticalbgr,
630 	subpixelnone,
631 };
632 
633 #define DRM_COLOR_FORMAT_RGB444         BIT(0)
634 #define DRM_COLOR_FORMAT_YCRCB444       BIT(1)
635 #define DRM_COLOR_FORMAT_YCRCB422       BIT(2)
636 #define DRM_COLOR_FORMAT_YCRCB420       BIT(3)
637 
638 /*
639  * Describes a given display (e.g. CRT or flat panel) and its limitations.
640  */
641 struct drm_display_info {
642 	char name[32];
643 
644 	/* Physical size */
645 	unsigned int width_mm;
646 	unsigned int height_mm;
647 
648 	/* Clock limits FIXME: storage format */
649 	unsigned int min_vfreq, max_vfreq;
650 	unsigned int min_hfreq, max_hfreq;
651 	unsigned int pixel_clock;
652 	unsigned int bpc;
653 
654 	enum subpixel_order subpixel_order;
655 	u32 color_formats;
656 
657 	const u32 *bus_formats;
658 	unsigned int num_bus_formats;
659 
660 	/**
661 	 * @max_tmds_clock: Maximum TMDS clock rate supported by the
662 	 * sink in kHz. 0 means undefined.
663 	 */
664 	int max_tmds_clock;
665 
666 	/**
667 	 * @dvi_dual: Dual-link DVI sink?
668 	 */
669 	bool dvi_dual;
670 
671 	/* Mask of supported hdmi deep color modes */
672 	u8 edid_hdmi_dc_modes;
673 
674 	u8 cea_rev;
675 
676 	/**
677 	 * @hdmi: advance features of a HDMI sink.
678 	 */
679 	struct drm_hdmi_info hdmi;
680 };
681 
682 struct edid {
683 	u8 header[8];
684 	/* Vendor & product info */
685 	u8 mfg_id[2];
686 	u8 prod_code[2];
687 	u32 serial; /* FIXME: byte order */
688 	u8 mfg_week;
689 	u8 mfg_year;
690 	/* EDID version */
691 	u8 version;
692 	u8 revision;
693 	/* Display info: */
694 	u8 input;
695 	u8 width_cm;
696 	u8 height_cm;
697 	u8 gamma;
698 	u8 features;
699 	/* Color characteristics */
700 	u8 red_green_lo;
701 	u8 black_white_lo;
702 	u8 red_x;
703 	u8 red_y;
704 	u8 green_x;
705 	u8 green_y;
706 	u8 blue_x;
707 	u8 blue_y;
708 	u8 white_x;
709 	u8 white_y;
710 	/* Est. timings and mfg rsvd timings*/
711 	struct est_timings established_timings;
712 	/* Standard timings 1-8*/
713 	struct std_timing standard_timings[8];
714 	/* Detailing timings 1-4 */
715 	struct detailed_timing detailed_timings[4];
716 	/* Number of 128 byte ext. blocks */
717 	u8 extensions;
718 	/* Checksum */
719 	u8 checksum;
720 } __packed;
721 
722 enum base_output_format {
723 	DRM_HDMI_OUTPUT_DEFAULT_RGB, /* default RGB */
724 	DRM_HDMI_OUTPUT_YCBCR444, /* YCBCR 444 */
725 	DRM_HDMI_OUTPUT_YCBCR422, /* YCBCR 422 */
726 	DRM_HDMI_OUTPUT_YCBCR420, /* YCBCR 420 */
727 	/* (YCbCr444 > YCbCr422 > YCbCr420 > RGB) */
728 	DRM_HDMI_OUTPUT_YCBCR_HQ,
729 	/* (YCbCr420 > YCbCr422 > YCbCr444 > RGB) */
730 	DRM_HDMI_OUTPUT_YCBCR_LQ,
731 	DRM_HDMI_OUTPUT_INVALID, /* Guess what ? */
732 };
733 
734 enum  base_output_depth {
735 	AUTOMATIC = 0,
736 	DEPTH_24BIT = 8,
737 	DEPTH_30BIT = 10,
738 };
739 
740 struct base_bcsh_info {
741 	unsigned short brightness;
742 	unsigned short contrast;
743 	unsigned short saturation;
744 	unsigned short hue;
745 };
746 
747 struct base_overscan {
748 	unsigned int maxvalue;
749 	unsigned short leftscale;
750 	unsigned short rightscale;
751 	unsigned short topscale;
752 	unsigned short bottomscale;
753 };
754 
755 struct base_drm_display_mode {
756 	int clock;		/* in kHz */
757 	int hdisplay;
758 	int hsync_start;
759 	int hsync_end;
760 	int htotal;
761 	int vdisplay;
762 	int vsync_start;
763 	int vsync_end;
764 	int vtotal;
765 	int vrefresh;
766 	int vscan;
767 	unsigned int flags;
768 	int picture_aspect_ratio;
769 };
770 
771 struct base_screen_info {
772 	int type;
773 	struct base_drm_display_mode mode;	/* 52 bytes */
774 	enum base_output_format  format;	/* 4 bytes */
775 	enum base_output_depth depth;		/* 4 bytes */
776 	unsigned int feature;			/* 4 bytes */
777 };
778 
779 struct base_disp_info {
780 	struct base_screen_info screen_list[5];
781 	struct base_overscan scan;		/* 12 bytes */
782 };
783 
784 struct base2_cubic_lut_data {
785 	u16 size;
786 	u16 lred[4913];
787 	u16 lgreen[4913];
788 	u16 lblue[4913];
789 };
790 
791 struct base2_screen_info {
792 	u32 type;
793 	u32 id;
794 	struct base_drm_display_mode resolution;
795 	enum base_output_format  format;
796 	enum base_output_depth depthc;
797 	u32 feature;
798 };
799 
800 struct base2_gamma_lut_data {
801 	u16 size;
802 	u16 lred[1024];
803 	u16 lgreen[1024];
804 	u16 lblue[1024];
805 };
806 
807 struct framebuffer_info {
808 	u32 framebuffer_width;
809 	u32 framebuffer_height;
810 	u32 fps;
811 };
812 
813 struct base2_disp_info {
814 	char disp_head_flag[6];
815 	struct base2_screen_info screen_info[4];
816 	struct base_bcsh_info bcsh_info;
817 	struct base_overscan overscan_info;
818 	struct base2_gamma_lut_data gamma_lut_data;
819 	struct base2_cubic_lut_data cubic_lut_data;
820 	struct framebuffer_info framebuffer_info;
821 	u32 reserved[244];
822 	u32 crc;
823 };
824 
825 struct base2_disp_header {
826 	u32 connector_type;
827 	u32 connector_id;
828 	u32 offset;
829 };
830 
831 struct base2_info {
832 	char head_flag[4];
833 	u16 major_version;
834 	u16 minor_version;
835 	struct base2_disp_header disp_header[8];
836 	struct base2_disp_info disp_info[8];
837 };
838 
839 /**
840  * Print the EDID info.
841  *
842  * @param edid_info	The EDID info to be printed
843  */
844 void edid_print_info(struct edid1_info *edid_info);
845 
846 /**
847  * Check the EDID info.
848  *
849  * @param info  The EDID info to be checked
850  * @return 0 on valid, or -1 on invalid
851  */
852 int edid_check_info(struct edid1_info *info);
853 
854 /**
855  * Check checksum of a 128 bytes EDID data block
856  *
857  * @param edid_block	EDID block data
858  *
859  * @return 0 on success, or a negative errno on error
860  */
861 int edid_check_checksum(u8 *edid_block);
862 
863 /**
864  * Get the horizontal and vertical rate ranges of the monitor.
865  *
866  * @param edid	The EDID info
867  * @param hmin	Returns the minimum horizontal rate
868  * @param hmax	Returns the maximum horizontal rate
869  * @param vmin	Returns the minimum vertical rate
870  * @param vmax	Returns the maximum vertical rate
871  * @return 0 on success, or -1 on error
872  */
873 int edid_get_ranges(struct edid1_info *edid, unsigned int *hmin,
874 		    unsigned int *hmax, unsigned int *vmin,
875 		    unsigned int *vmax);
876 
877 struct drm_display_mode;
878 struct display_timing;
879 
880 struct hdmi_edid_data {
881 	struct drm_display_mode *preferred_mode;
882 	int modes;
883 	struct drm_display_mode *mode_buf;
884 	struct drm_display_info display_info;
885 };
886 
887 struct ddc_adapter {
888 	int (*ddc_xfer)(struct ddc_adapter *adap, struct i2c_msg *msgs,
889 			int num);
890 	struct udevice *i2c_bus;
891 	struct dm_i2c_ops *ops;
892 };
893 
894 /**
895  * edid_get_timing() - Get basic digital display parameters
896  *
897  * @param buf		Buffer containing EDID data
898  * @param buf_size	Size of buffer in bytes
899  * @param timing	Place to put preferring timing information
900  * @param panel_bits_per_colourp	Place to put the number of bits per
901  *			colour supported by the panel. This will be set to
902  *			-1 if not available
903  * @return 0 if timings are OK, -ve on error
904  */
905 int edid_get_timing(u8 *buf, int buf_size, struct display_timing *timing,
906 		    int *panel_bits_per_colourp);
907 int edid_get_drm_mode(u8 *buf, int buf_size, struct drm_display_mode *mode,
908 		      int *panel_bits_per_colourp);
909 int drm_add_edid_modes(struct hdmi_edid_data *data, u8 *edid);
910 bool drm_detect_hdmi_monitor(struct edid *edid);
911 bool drm_detect_monitor_audio(struct edid *edid);
912 int do_cea_modes(struct hdmi_edid_data *data, const u8 *db, u8 len);
913 int drm_do_get_edid(struct ddc_adapter *adap, u8 *edid);
914 enum hdmi_quantization_range
915 drm_default_rgb_quant_range(struct drm_display_mode *mode);
916 u8 drm_scdc_readb(struct ddc_adapter *adap, u8 offset,
917 		  u8 *value);
918 u8 drm_scdc_writeb(struct ddc_adapter *adap, u8 offset,
919 		   u8 value);
920 void drm_mode_sort(struct hdmi_edid_data *edid_data);
921 int drm_mode_prune_invalid(struct hdmi_edid_data *edid_data);
922 void drm_rk_filter_whitelist(struct hdmi_edid_data *edid_data);
923 void drm_rk_select_mode(struct hdmi_edid_data *edid_data,
924 			struct base_screen_info *screen_info);
925 
926 #endif /* __EDID_H_ */
927