1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
6 * Petr Stetiar <ynezz@true.cz>
8 * Contains stolen code from ddcprobe project which is:
9 * Copyright (C) Nalin Dahyabhai <bigfun@pobox.com>
17 #include <linux/ctype.h>
18 #include <linux/string.h>
20 int edid_check_info(struct edid1_info *edid_info)
22 if ((edid_info == NULL) || (edid_info->version == 0))
25 if (memcmp(edid_info->header, "\x0\xff\xff\xff\xff\xff\xff\x0", 8))
28 if (edid_info->version == 0xff && edid_info->revision == 0xff)
34 int edid_check_checksum(u8 *edid_block)
39 for (i = 0; i < 128; i++)
40 checksum += edid_block[i];
42 return (checksum == 0) ? 0 : -EINVAL;
45 int edid_get_ranges(struct edid1_info *edid, unsigned int *hmin,
46 unsigned int *hmax, unsigned int *vmin,
50 struct edid_monitor_descriptor *monitor;
52 *hmin = *hmax = *vmin = *vmax = 0;
53 if (edid_check_info(edid))
56 for (i = 0; i < ARRAY_SIZE(edid->monitor_details.descriptor); i++) {
57 monitor = &edid->monitor_details.descriptor[i];
58 if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE) {
59 *hmin = monitor->data.range_data.horizontal_min;
60 *hmax = monitor->data.range_data.horizontal_max;
61 *vmin = monitor->data.range_data.vertical_min;
62 *vmax = monitor->data.range_data.vertical_max;
69 /* Set all parts of a timing entry to the same value */
70 static void set_entry(struct timing_entry *entry, u32 value)
78 * decode_timing() - Decoding an 18-byte detailed timing record
80 * @buf: Pointer to EDID detailed timing record
81 * @timing: Place to put timing
83 static void decode_timing(u8 *buf, struct display_timing *timing)
86 unsigned int ha, hbl, hso, hspw, hborder;
87 unsigned int va, vbl, vso, vspw, vborder;
88 struct edid_detailed_timing *t = (struct edid_detailed_timing *)buf;
90 /* Edid contains pixel clock in terms of 10KHz */
91 set_entry(&timing->pixelclock, (buf[0] + (buf[1] << 8)) * 10000);
92 x_mm = (buf[12] + ((buf[14] & 0xf0) << 4));
93 y_mm = (buf[13] + ((buf[14] & 0x0f) << 8));
94 ha = (buf[2] + ((buf[4] & 0xf0) << 4));
95 hbl = (buf[3] + ((buf[4] & 0x0f) << 8));
96 hso = (buf[8] + ((buf[11] & 0xc0) << 2));
97 hspw = (buf[9] + ((buf[11] & 0x30) << 4));
99 va = (buf[5] + ((buf[7] & 0xf0) << 4));
100 vbl = (buf[6] + ((buf[7] & 0x0f) << 8));
101 vso = ((buf[10] >> 4) + ((buf[11] & 0x0c) << 2));
102 vspw = ((buf[10] & 0x0f) + ((buf[11] & 0x03) << 4));
105 set_entry(&timing->hactive, ha);
106 set_entry(&timing->hfront_porch, hso);
107 set_entry(&timing->hback_porch, hbl - hso - hspw);
108 set_entry(&timing->hsync_len, hspw);
110 set_entry(&timing->vactive, va);
111 set_entry(&timing->vfront_porch, vso);
112 set_entry(&timing->vback_porch, vbl - vso - vspw);
113 set_entry(&timing->vsync_len, vspw);
116 if (EDID_DETAILED_TIMING_FLAG_HSYNC_POLARITY(*t))
117 timing->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
119 timing->flags |= DISPLAY_FLAGS_HSYNC_LOW;
120 if (EDID_DETAILED_TIMING_FLAG_VSYNC_POLARITY(*t))
121 timing->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
123 timing->flags |= DISPLAY_FLAGS_VSYNC_LOW;
125 if (EDID_DETAILED_TIMING_FLAG_INTERLACED(*t))
126 timing->flags = DISPLAY_FLAGS_INTERLACED;
128 debug("Detailed mode clock %u Hz, %d mm x %d mm\n"
129 " %04x %04x %04x %04x hborder %x\n"
130 " %04x %04x %04x %04x vborder %x\n",
131 timing->pixelclock.typ,
133 ha, ha + hso, ha + hso + hspw,
135 va, va + vso, va + vso + vspw,
140 * Check if HDMI vendor specific data block is present in CEA block
141 * @param info CEA extension block
142 * @return true if block is found
144 static bool cea_is_hdmi_vsdb_present(struct edid_cea861_info *info)
148 /* check for end of data block */
149 end = info->dtd_offset;
151 end = sizeof(info->data);
152 if (end < 4 || end > sizeof(info->data))
157 /* Look for vendor specific data block of appropriate size */
158 if ((EDID_CEA861_DB_TYPE(*info, i) == EDID_CEA861_DB_VENDOR) &&
159 (EDID_CEA861_DB_LEN(*info, i) >= 5)) {
160 u8 *db = &info->data[i + 1];
161 u32 oui = db[0] | (db[1] << 8) | (db[2] << 16);
163 if (oui == HDMI_IEEE_OUI)
166 i += EDID_CEA861_DB_LEN(*info, i) + 1;
172 int edid_get_timing_validate(u8 *buf, int buf_size,
173 struct display_timing *timing,
174 int *panel_bits_per_colourp,
175 bool (*mode_valid)(void *priv,
176 const struct display_timing *timing),
177 void *mode_valid_priv)
179 struct edid1_info *edid = (struct edid1_info *)buf;
183 if (buf_size < sizeof(*edid) || edid_check_info(edid)) {
184 debug("%s: Invalid buffer\n", __func__);
188 if (!EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid)) {
189 debug("%s: Not a digital display\n", __func__);
193 if (!EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(*edid)) {
194 debug("%s: No preferred timing\n", __func__);
198 /* Look for detailed timing */
200 for (i = 0; i < 4; i++) {
201 struct edid_monitor_descriptor *desc;
203 desc = &edid->monitor_details.descriptor[i];
204 if (desc->zero_flag_1 != 0) {
205 decode_timing((u8 *)desc, timing);
207 timing_done = mode_valid(mode_valid_priv,
219 if (edid->version != 1 || edid->revision < 4) {
220 debug("%s: EDID version %d.%d does not have required info\n",
221 __func__, edid->version, edid->revision);
222 *panel_bits_per_colourp = -1;
224 *panel_bits_per_colourp =
225 ((edid->video_input_definition & 0x70) >> 3) + 4;
228 timing->hdmi_monitor = false;
229 if (edid->extension_flag && (buf_size >= EDID_EXT_SIZE)) {
230 struct edid_cea861_info *info =
231 (struct edid_cea861_info *)(buf + sizeof(*edid));
233 if (info->extension_tag == EDID_CEA861_EXTENSION_TAG)
234 timing->hdmi_monitor = cea_is_hdmi_vsdb_present(info);
240 int edid_get_timing(u8 *buf, int buf_size, struct display_timing *timing,
241 int *panel_bits_per_colourp)
243 return edid_get_timing_validate(buf, buf_size, timing,
244 panel_bits_per_colourp, NULL, NULL);
249 * Snip the tailing whitespace/return of a string.
251 * @param string The string to be snipped
252 * @return the snipped string
254 static char *snip(char *string)
259 * This is always a 13 character buffer
260 * and it's not always terminated.
263 s = &string[strlen(string) - 1];
265 while (s >= string && (isspace(*s) || *s == '\n' || *s == '\r' ||
273 * Print an EDID monitor descriptor block
275 * @param monitor The EDID monitor descriptor block
276 * @have_timing Modifies to 1 if the desciptor contains timing info
278 static void edid_print_dtd(struct edid_monitor_descriptor *monitor,
279 unsigned int *have_timing)
281 unsigned char *bytes = (unsigned char *)monitor;
282 struct edid_detailed_timing *timing =
283 (struct edid_detailed_timing *)monitor;
285 if (bytes[0] == 0 && bytes[1] == 0) {
286 if (monitor->type == EDID_MONITOR_DESCRIPTOR_SERIAL)
287 printf("Monitor serial number: %s\n",
288 snip(monitor->data.string));
289 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_ASCII)
290 printf("Monitor ID: %s\n",
291 snip(monitor->data.string));
292 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_NAME)
293 printf("Monitor name: %s\n",
294 snip(monitor->data.string));
295 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE)
296 printf("Monitor range limits, horizontal sync: "
297 "%d-%d kHz, vertical refresh: "
298 "%d-%d Hz, max pixel clock: "
300 monitor->data.range_data.horizontal_min,
301 monitor->data.range_data.horizontal_max,
302 monitor->data.range_data.vertical_min,
303 monitor->data.range_data.vertical_max,
304 monitor->data.range_data.pixel_clock_max * 10);
306 uint32_t pixclock, h_active, h_blanking, v_active, v_blanking;
307 uint32_t h_total, v_total, vfreq;
309 pixclock = EDID_DETAILED_TIMING_PIXEL_CLOCK(*timing);
310 h_active = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*timing);
311 h_blanking = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*timing);
312 v_active = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*timing);
313 v_blanking = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*timing);
315 h_total = h_active + h_blanking;
316 v_total = v_active + v_blanking;
317 if (v_total > 0 && h_total > 0)
318 vfreq = pixclock / (v_total * h_total);
320 vfreq = 1; /* Error case */
321 printf("\t%dx%d\%c\t%d Hz (detailed)\n", h_active,
322 v_active, h_active > 1000 ? ' ' : '\t', vfreq);
328 * Get the manufacturer name from an EDID info.
330 * @param edid_info The EDID info to be printed
331 * @param name Returns the string of the manufacturer name
333 static void edid_get_manufacturer_name(struct edid1_info *edid, char *name)
335 name[0] = EDID1_INFO_MANUFACTURER_NAME_CHAR1(*edid) + 'A' - 1;
336 name[1] = EDID1_INFO_MANUFACTURER_NAME_CHAR2(*edid) + 'A' - 1;
337 name[2] = EDID1_INFO_MANUFACTURER_NAME_CHAR3(*edid) + 'A' - 1;
341 void edid_print_info(struct edid1_info *edid_info)
344 char manufacturer[4];
345 unsigned int have_timing = 0;
346 uint32_t serial_number;
348 if (edid_check_info(edid_info)) {
349 printf("Not a valid EDID\n");
353 printf("EDID version: %d.%d\n",
354 edid_info->version, edid_info->revision);
356 printf("Product ID code: %04x\n", EDID1_INFO_PRODUCT_CODE(*edid_info));
358 edid_get_manufacturer_name(edid_info, manufacturer);
359 printf("Manufacturer: %s\n", manufacturer);
361 serial_number = EDID1_INFO_SERIAL_NUMBER(*edid_info);
362 if (serial_number != 0xffffffff) {
363 if (strcmp(manufacturer, "MAG") == 0)
364 serial_number -= 0x7000000;
365 if (strcmp(manufacturer, "OQI") == 0)
366 serial_number -= 456150000;
367 if (strcmp(manufacturer, "VSC") == 0)
368 serial_number -= 640000000;
370 printf("Serial number: %08x\n", serial_number);
371 printf("Manufactured in week: %d year: %d\n",
372 edid_info->week, edid_info->year + 1990);
374 printf("Video input definition: %svoltage level %d%s%s%s%s%s\n",
375 EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid_info) ?
376 "digital signal, " : "analog signal, ",
377 EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(*edid_info),
378 EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(*edid_info) ?
379 ", blank to black" : "",
380 EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(*edid_info) ?
381 ", separate sync" : "",
382 EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(*edid_info) ?
383 ", composite sync" : "",
384 EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(*edid_info) ?
385 ", sync on green" : "",
386 EDID1_INFO_VIDEO_INPUT_SERRATION_V(*edid_info) ?
387 ", serration v" : "");
389 printf("Monitor is %s\n",
390 EDID1_INFO_FEATURE_RGB(*edid_info) ? "RGB" : "non-RGB");
392 printf("Maximum visible display size: %d cm x %d cm\n",
393 edid_info->max_size_horizontal,
394 edid_info->max_size_vertical);
396 printf("Power management features: %s%s, %s%s, %s%s\n",
397 EDID1_INFO_FEATURE_ACTIVE_OFF(*edid_info) ?
398 "" : "no ", "active off",
399 EDID1_INFO_FEATURE_SUSPEND(*edid_info) ? "" : "no ", "suspend",
400 EDID1_INFO_FEATURE_STANDBY(*edid_info) ? "" : "no ", "standby");
402 printf("Estabilished timings:\n");
403 if (EDID1_INFO_ESTABLISHED_TIMING_720X400_70(*edid_info))
404 printf("\t720x400\t\t70 Hz (VGA 640x400, IBM)\n");
405 if (EDID1_INFO_ESTABLISHED_TIMING_720X400_88(*edid_info))
406 printf("\t720x400\t\t88 Hz (XGA2)\n");
407 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_60(*edid_info))
408 printf("\t640x480\t\t60 Hz (VGA)\n");
409 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_67(*edid_info))
410 printf("\t640x480\t\t67 Hz (Mac II, Apple)\n");
411 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_72(*edid_info))
412 printf("\t640x480\t\t72 Hz (VESA)\n");
413 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_75(*edid_info))
414 printf("\t640x480\t\t75 Hz (VESA)\n");
415 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_56(*edid_info))
416 printf("\t800x600\t\t56 Hz (VESA)\n");
417 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_60(*edid_info))
418 printf("\t800x600\t\t60 Hz (VESA)\n");
419 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_72(*edid_info))
420 printf("\t800x600\t\t72 Hz (VESA)\n");
421 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_75(*edid_info))
422 printf("\t800x600\t\t75 Hz (VESA)\n");
423 if (EDID1_INFO_ESTABLISHED_TIMING_832X624_75(*edid_info))
424 printf("\t832x624\t\t75 Hz (Mac II)\n");
425 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(*edid_info))
426 printf("\t1024x768\t87 Hz Interlaced (8514A)\n");
427 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(*edid_info))
428 printf("\t1024x768\t60 Hz (VESA)\n");
429 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(*edid_info))
430 printf("\t1024x768\t70 Hz (VESA)\n");
431 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(*edid_info))
432 printf("\t1024x768\t75 Hz (VESA)\n");
433 if (EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(*edid_info))
434 printf("\t1280x1024\t75 (VESA)\n");
435 if (EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(*edid_info))
436 printf("\t1152x870\t75 (Mac II)\n");
438 /* Standard timings. */
439 printf("Standard timings:\n");
440 for (i = 0; i < ARRAY_SIZE(edid_info->standard_timings); i++) {
441 unsigned int aspect = 10000;
443 unsigned char xres, vfreq;
445 xres = EDID1_INFO_STANDARD_TIMING_XRESOLUTION(*edid_info, i);
446 vfreq = EDID1_INFO_STANDARD_TIMING_VFREQ(*edid_info, i);
447 if ((xres != vfreq) ||
448 ((xres != 0) && (xres != 1)) ||
449 ((vfreq != 0) && (vfreq != 1))) {
450 switch (EDID1_INFO_STANDARD_TIMING_ASPECT(*edid_info,
466 y = x * aspect / 10000;
467 printf("\t%dx%d%c\t%d Hz\n", x, y,
468 x > 1000 ? ' ' : '\t', (vfreq & 0x3f) + 60);
473 /* Detailed timing information. */
474 for (i = 0; i < ARRAY_SIZE(edid_info->monitor_details.descriptor);
476 edid_print_dtd(&edid_info->monitor_details.descriptor[i],