1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2012 The Chromium OS Authors.
4  *
5  * (C) Copyright 2010
6  * Petr Stetiar <ynezz@true.cz>
7  *
8  * Contains stolen code from ddcprobe project which is:
9  * Copyright (C) Nalin Dahyabhai <bigfun@pobox.com>
10  */
11 
12 #ifndef __EDID_H_
13 #define __EDID_H_
14 
15 #include <linux/types.h>
16 
17 /* Size of the EDID data */
18 #define EDID_SIZE	128
19 #define EDID_EXT_SIZE	256
20 
21 /* OUI of HDMI vendor specific data block */
22 #define HDMI_IEEE_OUI 0x000c03
23 
24 #define GET_BIT(_x, _pos) \
25 	(((_x) >> (_pos)) & 1)
26 #define GET_BITS(_x, _pos_msb, _pos_lsb) \
27 	(((_x) >> (_pos_lsb)) & ((1 << ((_pos_msb) - (_pos_lsb) + 1)) - 1))
28 
29 /* Aspect ratios used in EDID info. */
30 enum edid_aspect {
31 	ASPECT_625 = 0,
32 	ASPECT_75,
33 	ASPECT_8,
34 	ASPECT_5625,
35 };
36 
37 /* Detailed timing information used in EDID v1.x */
38 struct edid_detailed_timing {
39 	unsigned char pixel_clock[2];
40 #define EDID_DETAILED_TIMING_PIXEL_CLOCK(_x) \
41 	(((((uint32_t)(_x).pixel_clock[1]) << 8) + \
42 	 (_x).pixel_clock[0]) * 10000)
43 	unsigned char horizontal_active;
44 	unsigned char horizontal_blanking;
45 	unsigned char horizontal_active_blanking_hi;
46 #define EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(_x) \
47 	((GET_BITS((_x).horizontal_active_blanking_hi, 7, 4) << 8) + \
48 	 (_x).horizontal_active)
49 #define EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(_x) \
50 	((GET_BITS((_x).horizontal_active_blanking_hi, 3, 0) << 8) + \
51 	 (_x).horizontal_blanking)
52 	unsigned char vertical_active;
53 	unsigned char vertical_blanking;
54 	unsigned char vertical_active_blanking_hi;
55 #define EDID_DETAILED_TIMING_VERTICAL_ACTIVE(_x) \
56 	((GET_BITS((_x).vertical_active_blanking_hi, 7, 4) << 8) + \
57 	 (_x).vertical_active)
58 #define EDID_DETAILED_TIMING_VERTICAL_BLANKING(_x) \
59 	((GET_BITS((_x).vertical_active_blanking_hi, 3, 0) << 8) + \
60 	 (_x).vertical_blanking)
61 	unsigned char hsync_offset;
62 	unsigned char hsync_pulse_width;
63 	unsigned char vsync_offset_pulse_width;
64 	unsigned char hsync_vsync_offset_pulse_width_hi;
65 #define EDID_DETAILED_TIMING_HSYNC_OFFSET(_x) \
66 	((GET_BITS((_x).hsync_vsync_offset_pulse_width_hi, 7, 6) << 8) + \
67 	 (_x).hsync_offset)
68 #define EDID_DETAILED_TIMING_HSYNC_PULSE_WIDTH(_x) \
69 	((GET_BITS((_x).hsync_vsync_offset_pulse_width_hi, 5, 4) << 8) + \
70 	 (_x).hsync_pulse_width)
71 #define EDID_DETAILED_TIMING_VSYNC_OFFSET(_x) \
72 	((GET_BITS((_x).hsync_vsync_offset_pulse_width_hi, 3, 2) << 4) + \
73 	 GET_BITS((_x).vsync_offset_pulse_width, 7, 4))
74 #define EDID_DETAILED_TIMING_VSYNC_PULSE_WIDTH(_x) \
75 	((GET_BITS((_x).hsync_vsync_offset_pulse_width_hi, 1, 0) << 4) + \
76 	 GET_BITS((_x).vsync_offset_pulse_width, 3, 0))
77 	unsigned char himage_size;
78 	unsigned char vimage_size;
79 	unsigned char himage_vimage_size_hi;
80 #define EDID_DETAILED_TIMING_HIMAGE_SIZE(_x) \
81 	((GET_BITS((_x).himage_vimage_size_hi, 7, 4) << 8) + (_x).himage_size)
82 #define EDID_DETAILED_TIMING_VIMAGE_SIZE(_x) \
83 	((GET_BITS((_x).himage_vimage_size_hi, 3, 0) << 8) + (_x).vimage_size)
84 	unsigned char hborder;
85 	unsigned char vborder;
86 	unsigned char flags;
87 #define EDID_DETAILED_TIMING_FLAG_INTERLACED(_x) \
88 	GET_BIT((_x).flags, 7)
89 #define EDID_DETAILED_TIMING_FLAG_STEREO(_x) \
90 	GET_BITS((_x).flags, 6, 5)
91 #define EDID_DETAILED_TIMING_FLAG_DIGITAL_COMPOSITE(_x) \
92 	GET_BITS((_x).flags, 4, 3)
93 #define EDID_DETAILED_TIMING_FLAG_POLARITY(_x) \
94 	GET_BITS((_x).flags, 2, 1)
95 #define EDID_DETAILED_TIMING_FLAG_VSYNC_POLARITY(_x) \
96 	GET_BIT((_x).flags, 2)
97 #define EDID_DETAILED_TIMING_FLAG_HSYNC_POLARITY(_x) \
98 	GET_BIT((_x).flags, 1)
99 #define EDID_DETAILED_TIMING_FLAG_INTERLEAVED(_x) \
100 	GET_BIT((_x).flags, 0)
101 } __attribute__ ((__packed__));
102 
103 enum edid_monitor_descriptor_types {
104 	EDID_MONITOR_DESCRIPTOR_SERIAL = 0xff,
105 	EDID_MONITOR_DESCRIPTOR_ASCII = 0xfe,
106 	EDID_MONITOR_DESCRIPTOR_RANGE = 0xfd,
107 	EDID_MONITOR_DESCRIPTOR_NAME = 0xfc,
108 };
109 
110 struct edid_monitor_descriptor {
111 	uint16_t zero_flag_1;
112 	unsigned char zero_flag_2;
113 	unsigned char type;
114 	unsigned char zero_flag_3;
115 	union {
116 		char string[13];
117 		struct {
118 			unsigned char vertical_min;
119 			unsigned char vertical_max;
120 			unsigned char horizontal_min;
121 			unsigned char horizontal_max;
122 			unsigned char pixel_clock_max;
123 			unsigned char gtf_data[8];
124 		} range_data;
125 	} data;
126 } __attribute__ ((__packed__));
127 
128 struct edid1_info {
129 	unsigned char header[8];
130 	unsigned char manufacturer_name[2];
131 #define EDID1_INFO_MANUFACTURER_NAME_ZERO(_x) \
132 	GET_BIT(((_x).manufacturer_name[0]), 7)
133 #define EDID1_INFO_MANUFACTURER_NAME_CHAR1(_x) \
134 	GET_BITS(((_x).manufacturer_name[0]), 6, 2)
135 #define EDID1_INFO_MANUFACTURER_NAME_CHAR2(_x) \
136 	((GET_BITS(((_x).manufacturer_name[0]), 1, 0) << 3) + \
137 	 GET_BITS(((_x).manufacturer_name[1]), 7, 5))
138 #define EDID1_INFO_MANUFACTURER_NAME_CHAR3(_x) \
139 	GET_BITS(((_x).manufacturer_name[1]), 4, 0)
140 	unsigned char product_code[2];
141 #define EDID1_INFO_PRODUCT_CODE(_x) \
142 	(((uint16_t)(_x).product_code[1] << 8) + (_x).product_code[0])
143 	unsigned char serial_number[4];
144 #define EDID1_INFO_SERIAL_NUMBER(_x) \
145 	(((uint32_t)(_x).serial_number[3] << 24) + \
146 	 ((_x).serial_number[2] << 16) + ((_x).serial_number[1] << 8) + \
147 	 (_x).serial_number[0])
148 	unsigned char week;
149 	unsigned char year;
150 	unsigned char version;
151 	unsigned char revision;
152 	unsigned char video_input_definition;
153 #define EDID1_INFO_VIDEO_INPUT_DIGITAL(_x) \
154 	GET_BIT(((_x).video_input_definition), 7)
155 #define EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(_x) \
156 	GET_BITS(((_x).video_input_definition), 6, 5)
157 #define EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(_x) \
158 	GET_BIT(((_x).video_input_definition), 4)
159 #define EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(_x) \
160 	GET_BIT(((_x).video_input_definition), 3)
161 #define EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(_x) \
162 	GET_BIT(((_x).video_input_definition), 2)
163 #define EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(_x) \
164 	GET_BIT(((_x).video_input_definition), 1)
165 #define EDID1_INFO_VIDEO_INPUT_SERRATION_V(_x) \
166 	GET_BIT(((_x).video_input_definition), 0)
167 	unsigned char max_size_horizontal;
168 	unsigned char max_size_vertical;
169 	unsigned char gamma;
170 	unsigned char feature_support;
171 #define EDID1_INFO_FEATURE_STANDBY(_x) \
172 	GET_BIT(((_x).feature_support), 7)
173 #define EDID1_INFO_FEATURE_SUSPEND(_x) \
174 	GET_BIT(((_x).feature_support), 6)
175 #define EDID1_INFO_FEATURE_ACTIVE_OFF(_x) \
176 	GET_BIT(((_x).feature_support), 5)
177 #define EDID1_INFO_FEATURE_DISPLAY_TYPE(_x) \
178 	GET_BITS(((_x).feature_support), 4, 3)
179 #define EDID1_INFO_FEATURE_RGB(_x) \
180 	GET_BIT(((_x).feature_support), 2)
181 #define EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(_x) \
182 	GET_BIT(((_x).feature_support), 1)
183 #define EDID1_INFO_FEATURE_DEFAULT_GTF_SUPPORT(_x) \
184 	GET_BIT(((_x).feature_support), 0)
185 	unsigned char color_characteristics[10];
186 	unsigned char established_timings[3];
187 #define EDID1_INFO_ESTABLISHED_TIMING_720X400_70(_x) \
188 	GET_BIT(((_x).established_timings[0]), 7)
189 #define EDID1_INFO_ESTABLISHED_TIMING_720X400_88(_x) \
190 	GET_BIT(((_x).established_timings[0]), 6)
191 #define EDID1_INFO_ESTABLISHED_TIMING_640X480_60(_x) \
192 	GET_BIT(((_x).established_timings[0]), 5)
193 #define EDID1_INFO_ESTABLISHED_TIMING_640X480_67(_x) \
194 	GET_BIT(((_x).established_timings[0]), 4)
195 #define EDID1_INFO_ESTABLISHED_TIMING_640X480_72(_x) \
196 	GET_BIT(((_x).established_timings[0]), 3)
197 #define EDID1_INFO_ESTABLISHED_TIMING_640X480_75(_x) \
198 	GET_BIT(((_x).established_timings[0]), 2)
199 #define EDID1_INFO_ESTABLISHED_TIMING_800X600_56(_x) \
200 	GET_BIT(((_x).established_timings[0]), 1)
201 #define EDID1_INFO_ESTABLISHED_TIMING_800X600_60(_x) \
202 	GET_BIT(((_x).established_timings[0]), 0)
203 #define EDID1_INFO_ESTABLISHED_TIMING_800X600_72(_x) \
204 	GET_BIT(((_x).established_timings[1]), 7)
205 #define EDID1_INFO_ESTABLISHED_TIMING_800X600_75(_x) \
206 	GET_BIT(((_x).established_timings[1]), 6)
207 #define EDID1_INFO_ESTABLISHED_TIMING_832X624_75(_x) \
208 	GET_BIT(((_x).established_timings[1]), 5)
209 #define EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(_x) \
210 	GET_BIT(((_x).established_timings[1]), 4)
211 #define EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(_x) \
212 	GET_BIT(((_x).established_timings[1]), 3)
213 #define EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(_x) \
214 	GET_BIT(((_x).established_timings[1]), 2)
215 #define EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(_x) \
216 	GET_BIT(((_x).established_timings[1]), 1)
217 #define EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(_x) \
218 	GET_BIT(((_x).established_timings[1]), 0)
219 #define EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(_x) \
220 	GET_BIT(((_x).established_timings[2]), 7)
221 	struct {
222 		unsigned char xresolution;
223 		unsigned char aspect_vfreq;
224 	} __attribute__((__packed__)) standard_timings[8];
225 #define EDID1_INFO_STANDARD_TIMING_XRESOLUTION(_x, _i) \
226 	(((_x).standard_timings[_i]).xresolution)
227 #define EDID1_INFO_STANDARD_TIMING_ASPECT(_x, _i) \
228 	GET_BITS(((_x).standard_timings[_i].aspect_vfreq), 7, 6)
229 #define EDID1_INFO_STANDARD_TIMING_VFREQ(_x, _i) \
230 	GET_BITS(((_x).standard_timings[_i].aspect_vfreq), 5, 0)
231 	union {
232 		unsigned char timing[72];
233 		struct edid_monitor_descriptor descriptor[4];
234 	} monitor_details;
235 	unsigned char extension_flag;
236 	unsigned char checksum;
237 } __attribute__ ((__packed__));
238 
239 enum edid_cea861_db_types {
240 	EDID_CEA861_DB_AUDIO = 0x01,
241 	EDID_CEA861_DB_VIDEO = 0x02,
242 	EDID_CEA861_DB_VENDOR = 0x03,
243 	EDID_CEA861_DB_SPEAKER = 0x04,
244 };
245 
246 struct edid_cea861_info {
247 	unsigned char extension_tag;
248 #define EDID_CEA861_EXTENSION_TAG	0x02
249 	unsigned char revision;
250 	unsigned char dtd_offset;
251 	unsigned char dtd_count;
252 #define EDID_CEA861_SUPPORTS_UNDERSCAN(_x) \
253 	GET_BIT(((_x).dtd_count), 7)
254 #define EDID_CEA861_SUPPORTS_BASIC_AUDIO(_x) \
255 	GET_BIT(((_x).dtd_count), 6)
256 #define EDID_CEA861_SUPPORTS_YUV444(_x) \
257 	GET_BIT(((_x).dtd_count), 5)
258 #define EDID_CEA861_SUPPORTS_YUV422(_x) \
259 	GET_BIT(((_x).dtd_count), 4)
260 #define EDID_CEA861_DTD_COUNT(_x) \
261 	GET_BITS(((_x).dtd_count), 3, 0)
262 	unsigned char data[124];
263 #define EDID_CEA861_DB_TYPE(_x, offset) \
264 	GET_BITS((_x).data[offset], 7, 5)
265 #define EDID_CEA861_DB_LEN(_x, offset) \
266 	GET_BITS((_x).data[offset], 4, 0)
267 } __attribute__ ((__packed__));
268 
269 /**
270  * Print the EDID info.
271  *
272  * @param edid_info	The EDID info to be printed
273  */
274 void edid_print_info(struct edid1_info *edid_info);
275 
276 /**
277  * Check the EDID info.
278  *
279  * @param info  The EDID info to be checked
280  * @return 0 on valid, or -1 on invalid
281  */
282 int edid_check_info(struct edid1_info *info);
283 
284 /**
285  * Check checksum of a 128 bytes EDID data block
286  *
287  * @param edid_block	EDID block data
288  *
289  * @return 0 on success, or a negative errno on error
290  */
291 int edid_check_checksum(u8 *edid_block);
292 
293 /**
294  * Get the horizontal and vertical rate ranges of the monitor.
295  *
296  * @param edid	The EDID info
297  * @param hmin	Returns the minimum horizontal rate
298  * @param hmax	Returns the maxium horizontal rate
299  * @param vmin	Returns the minimum vertical rate
300  * @param vmax	Returns the maxium vertical rate
301  * @return 0 on success, or -1 on error
302  */
303 int edid_get_ranges(struct edid1_info *edid, unsigned int *hmin,
304 		    unsigned int *hmax, unsigned int *vmin,
305 		    unsigned int *vmax);
306 
307 struct display_timing;
308 
309 /**
310  * edid_get_timing_validate() - Get basic digital display parameters with
311  * mode selection callback
312  *
313  * @param buf		Buffer containing EDID data
314  * @param buf_size	Size of buffer in bytes
315  * @param timing	Place to put preferring timing information
316  * @param panel_bits_per_colourp	Place to put the number of bits per
317  *			colour supported by the panel. This will be set to
318  *			-1 if not available
319  * @param mode_valid	Callback validating mode, returning true is mode is
320  *			supported, false otherwise.
321  * @parem valid_priv	Pointer to private data for mode_valid callback
322  * @return 0 if timings are OK, -ve on error
323  */
324 int edid_get_timing_validate(u8 *buf, int buf_size,
325 			     struct display_timing *timing,
326 			     int *panel_bits_per_colourp,
327 			     bool (*mode_valid)(void *priv,
328 					const struct display_timing *timing),
329 			     void *mode_valid_priv);
330 
331 /**
332  * edid_get_timing() - Get basic digital display parameters
333  *
334  * @param buf		Buffer containing EDID data
335  * @param buf_size	Size of buffer in bytes
336  * @param timing	Place to put preferring timing information
337  * @param panel_bits_per_colourp	Place to put the number of bits per
338  *			colour supported by the panel. This will be set to
339  *			-1 if not available
340  * @return 0 if timings are OK, -ve on error
341  */
342 int edid_get_timing(u8 *buf, int buf_size, struct display_timing *timing,
343 		    int *panel_bits_per_colourp);
344 
345 #endif /* __EDID_H_ */
346