xref: /openbmc/u-boot/common/edid.c (revision b5493d17)
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  */
12 
13 #include <common.h>
14 #include <edid.h>
15 #include <errno.h>
16 #include <linux/ctype.h>
17 #include <linux/string.h>
18 
19 int edid_check_info(struct edid1_info *edid_info)
20 {
21 	if ((edid_info == NULL) || (edid_info->version == 0))
22 		return -1;
23 
24 	if (memcmp(edid_info->header, "\x0\xff\xff\xff\xff\xff\xff\x0", 8))
25 		return -1;
26 
27 	if (edid_info->version == 0xff && edid_info->revision == 0xff)
28 		return -1;
29 
30 	return 0;
31 }
32 
33 int edid_check_checksum(u8 *edid_block)
34 {
35 	u8 checksum = 0;
36 	int i;
37 
38 	for (i = 0; i < 128; i++)
39 		checksum += edid_block[i];
40 
41 	return (checksum == 0) ? 0 : -EINVAL;
42 }
43 
44 int edid_get_ranges(struct edid1_info *edid, unsigned int *hmin,
45 		    unsigned int *hmax, unsigned int *vmin,
46 		    unsigned int *vmax)
47 {
48 	int i;
49 	struct edid_monitor_descriptor *monitor;
50 
51 	*hmin = *hmax = *vmin = *vmax = 0;
52 	if (edid_check_info(edid))
53 		return -1;
54 
55 	for (i = 0; i < ARRAY_SIZE(edid->monitor_details.descriptor); i++) {
56 		monitor = &edid->monitor_details.descriptor[i];
57 		if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE) {
58 			*hmin = monitor->data.range_data.horizontal_min;
59 			*hmax = monitor->data.range_data.horizontal_max;
60 			*vmin = monitor->data.range_data.vertical_min;
61 			*vmax = monitor->data.range_data.vertical_max;
62 			return 0;
63 		}
64 	}
65 	return -1;
66 }
67 
68 /**
69  * Snip the tailing whitespace/return of a string.
70  *
71  * @param string	The string to be snipped
72  * @return the snipped string
73  */
74 static char *snip(char *string)
75 {
76 	char *s;
77 
78 	/*
79 	 * This is always a 13 character buffer
80 	 * and it's not always terminated.
81 	 */
82 	string[12] = '\0';
83 	s = &string[strlen(string) - 1];
84 
85 	while (s >= string && (isspace(*s) || *s == '\n' || *s == '\r' ||
86 			*s == '\0'))
87 		*(s--) = '\0';
88 
89 	return string;
90 }
91 
92 /**
93  * Print an EDID monitor descriptor block
94  *
95  * @param monitor	The EDID monitor descriptor block
96  * @have_timing		Modifies to 1 if the desciptor contains timing info
97  */
98 static void edid_print_dtd(struct edid_monitor_descriptor *monitor,
99 			   unsigned int *have_timing)
100 {
101 	unsigned char *bytes = (unsigned char *)monitor;
102 	struct edid_detailed_timing *timing =
103 			(struct edid_detailed_timing *)monitor;
104 
105 	if (bytes[0] == 0 && bytes[1] == 0) {
106 		if (monitor->type == EDID_MONITOR_DESCRIPTOR_SERIAL)
107 			printf("Monitor serial number: %s\n",
108 			       snip(monitor->data.string));
109 		else if (monitor->type == EDID_MONITOR_DESCRIPTOR_ASCII)
110 			printf("Monitor ID: %s\n",
111 			       snip(monitor->data.string));
112 		else if (monitor->type == EDID_MONITOR_DESCRIPTOR_NAME)
113 			printf("Monitor name: %s\n",
114 			       snip(monitor->data.string));
115 		else if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE)
116 			printf("Monitor range limits, horizontal sync: "
117 			       "%d-%d kHz, vertical refresh: "
118 			       "%d-%d Hz, max pixel clock: "
119 			       "%d MHz\n",
120 			       monitor->data.range_data.horizontal_min,
121 			       monitor->data.range_data.horizontal_max,
122 			       monitor->data.range_data.vertical_min,
123 			       monitor->data.range_data.vertical_max,
124 			       monitor->data.range_data.pixel_clock_max * 10);
125 	} else {
126 		uint32_t pixclock, h_active, h_blanking, v_active, v_blanking;
127 		uint32_t h_total, v_total, vfreq;
128 
129 		pixclock = EDID_DETAILED_TIMING_PIXEL_CLOCK(*timing);
130 		h_active = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*timing);
131 		h_blanking = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*timing);
132 		v_active = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*timing);
133 		v_blanking = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*timing);
134 
135 		h_total = h_active + h_blanking;
136 		v_total = v_active + v_blanking;
137 		if (v_total * h_total)
138 			vfreq = pixclock / (v_total * h_total);
139 		else
140 			vfreq = 1; /* Error case */
141 		printf("\t%dx%d\%c\t%d Hz (detailed)\n", h_active,
142 		       v_active, h_active > 1000 ? ' ' : '\t', vfreq);
143 		*have_timing = 1;
144 	}
145 }
146 
147 /**
148  * Get the manufacturer name from an EDID info.
149  *
150  * @param edid_info     The EDID info to be printed
151  * @param name		Returns the string of the manufacturer name
152  */
153 static void edid_get_manufacturer_name(struct edid1_info *edid, char *name)
154 {
155 	name[0] = EDID1_INFO_MANUFACTURER_NAME_CHAR1(*edid) + 'A' - 1;
156 	name[1] = EDID1_INFO_MANUFACTURER_NAME_CHAR2(*edid) + 'A' - 1;
157 	name[2] = EDID1_INFO_MANUFACTURER_NAME_CHAR3(*edid) + 'A' - 1;
158 	name[3] = '\0';
159 }
160 
161 void edid_print_info(struct edid1_info *edid_info)
162 {
163 	int i;
164 	char manufacturer[4];
165 	unsigned int have_timing = 0;
166 	uint32_t serial_number;
167 
168 	if (edid_check_info(edid_info)) {
169 		printf("Not a valid EDID\n");
170 		return;
171 	}
172 
173 	printf("EDID version: %d.%d\n",
174 	       edid_info->version, edid_info->revision);
175 
176 	printf("Product ID code: %04x\n", EDID1_INFO_PRODUCT_CODE(*edid_info));
177 
178 	edid_get_manufacturer_name(edid_info, manufacturer);
179 	printf("Manufacturer: %s\n", manufacturer);
180 
181 	serial_number = EDID1_INFO_SERIAL_NUMBER(*edid_info);
182 	if (serial_number != 0xffffffff) {
183 		if (strcmp(manufacturer, "MAG") == 0)
184 			serial_number -= 0x7000000;
185 		if (strcmp(manufacturer, "OQI") == 0)
186 			serial_number -= 456150000;
187 		if (strcmp(manufacturer, "VSC") == 0)
188 			serial_number -= 640000000;
189 	}
190 	printf("Serial number: %08x\n", serial_number);
191 	printf("Manufactured in week: %d year: %d\n",
192 	       edid_info->week, edid_info->year + 1990);
193 
194 	printf("Video input definition: %svoltage level %d%s%s%s%s%s\n",
195 	       EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid_info) ?
196 	       "digital signal, " : "analog signal, ",
197 	       EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(*edid_info),
198 	       EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(*edid_info) ?
199 	       ", blank to black" : "",
200 	       EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(*edid_info) ?
201 	       ", separate sync" : "",
202 	       EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(*edid_info) ?
203 	       ", composite sync" : "",
204 	       EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(*edid_info) ?
205 	       ", sync on green" : "",
206 	       EDID1_INFO_VIDEO_INPUT_SERRATION_V(*edid_info) ?
207 	       ", serration v" : "");
208 
209 	printf("Monitor is %s\n",
210 	       EDID1_INFO_FEATURE_RGB(*edid_info) ? "RGB" : "non-RGB");
211 
212 	printf("Maximum visible display size: %d cm x %d cm\n",
213 	       edid_info->max_size_horizontal,
214 	       edid_info->max_size_vertical);
215 
216 	printf("Power management features: %s%s, %s%s, %s%s\n",
217 	       EDID1_INFO_FEATURE_ACTIVE_OFF(*edid_info) ?
218 	       "" : "no ", "active off",
219 	       EDID1_INFO_FEATURE_SUSPEND(*edid_info) ? "" : "no ", "suspend",
220 	       EDID1_INFO_FEATURE_STANDBY(*edid_info) ? "" : "no ", "standby");
221 
222 	printf("Estabilished timings:\n");
223 	if (EDID1_INFO_ESTABLISHED_TIMING_720X400_70(*edid_info))
224 		printf("\t720x400\t\t70 Hz (VGA 640x400, IBM)\n");
225 	if (EDID1_INFO_ESTABLISHED_TIMING_720X400_88(*edid_info))
226 		printf("\t720x400\t\t88 Hz (XGA2)\n");
227 	if (EDID1_INFO_ESTABLISHED_TIMING_640X480_60(*edid_info))
228 		printf("\t640x480\t\t60 Hz (VGA)\n");
229 	if (EDID1_INFO_ESTABLISHED_TIMING_640X480_67(*edid_info))
230 		printf("\t640x480\t\t67 Hz (Mac II, Apple)\n");
231 	if (EDID1_INFO_ESTABLISHED_TIMING_640X480_72(*edid_info))
232 		printf("\t640x480\t\t72 Hz (VESA)\n");
233 	if (EDID1_INFO_ESTABLISHED_TIMING_640X480_75(*edid_info))
234 		printf("\t640x480\t\t75 Hz (VESA)\n");
235 	if (EDID1_INFO_ESTABLISHED_TIMING_800X600_56(*edid_info))
236 		printf("\t800x600\t\t56 Hz (VESA)\n");
237 	if (EDID1_INFO_ESTABLISHED_TIMING_800X600_60(*edid_info))
238 		printf("\t800x600\t\t60 Hz (VESA)\n");
239 	if (EDID1_INFO_ESTABLISHED_TIMING_800X600_72(*edid_info))
240 		printf("\t800x600\t\t72 Hz (VESA)\n");
241 	if (EDID1_INFO_ESTABLISHED_TIMING_800X600_75(*edid_info))
242 		printf("\t800x600\t\t75 Hz (VESA)\n");
243 	if (EDID1_INFO_ESTABLISHED_TIMING_832X624_75(*edid_info))
244 		printf("\t832x624\t\t75 Hz (Mac II)\n");
245 	if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(*edid_info))
246 		printf("\t1024x768\t87 Hz Interlaced (8514A)\n");
247 	if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(*edid_info))
248 		printf("\t1024x768\t60 Hz (VESA)\n");
249 	if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(*edid_info))
250 		printf("\t1024x768\t70 Hz (VESA)\n");
251 	if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(*edid_info))
252 		printf("\t1024x768\t75 Hz (VESA)\n");
253 	if (EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(*edid_info))
254 		printf("\t1280x1024\t75 (VESA)\n");
255 	if (EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(*edid_info))
256 		printf("\t1152x870\t75 (Mac II)\n");
257 
258 	/* Standard timings. */
259 	printf("Standard timings:\n");
260 	for (i = 0; i < ARRAY_SIZE(edid_info->standard_timings); i++) {
261 		unsigned int aspect = 10000;
262 		unsigned int x, y;
263 		unsigned char xres, vfreq;
264 
265 		xres = EDID1_INFO_STANDARD_TIMING_XRESOLUTION(*edid_info, i);
266 		vfreq = EDID1_INFO_STANDARD_TIMING_VFREQ(*edid_info, i);
267 		if ((xres != vfreq) ||
268 		    ((xres != 0) && (xres != 1)) ||
269 		    ((vfreq != 0) && (vfreq != 1))) {
270 			switch (EDID1_INFO_STANDARD_TIMING_ASPECT(*edid_info,
271 					i)) {
272 			case ASPECT_625:
273 				aspect = 6250;
274 				break;
275 			case ASPECT_75:
276 				aspect = 7500;
277 				break;
278 			case ASPECT_8:
279 				aspect = 8000;
280 				break;
281 			case ASPECT_5625:
282 				aspect = 5625;
283 				break;
284 			}
285 			x = (xres + 31) * 8;
286 			y = x * aspect / 10000;
287 			printf("\t%dx%d%c\t%d Hz\n", x, y,
288 			       x > 1000 ? ' ' : '\t', (vfreq & 0x3f) + 60);
289 			have_timing = 1;
290 		}
291 	}
292 
293 	/* Detailed timing information. */
294 	for (i = 0; i < ARRAY_SIZE(edid_info->monitor_details.descriptor);
295 			i++) {
296 		edid_print_dtd(&edid_info->monitor_details.descriptor[i],
297 			       &have_timing);
298 	}
299 
300 	if (!have_timing)
301 		printf("\tNone\n");
302 }
303