blob: 865ba9daa78a8681ea685d1eb73f0d1fc13ce82b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +00002/*
3 * Copyright (c) 2012 The Chromium OS Authors.
4 *
5 * (C) Copyright 2010
6 * Petr Stetiar <ynezz@true.cz>
7 *
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +00008 * Contains stolen code from ddcprobe project which is:
9 * Copyright (C) Nalin Dahyabhai <bigfun@pobox.com>
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +000010 */
11
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +000012#include <edid.h>
Hans de Goede6e354e42014-11-24 13:47:13 +010013#include <errno.h>
Simon Glasseda77322015-04-14 21:03:37 -060014#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +000016#include <linux/ctype.h>
17#include <linux/string.h>
18
19int 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
Hans de Goede6e354e42014-11-24 13:47:13 +010033int 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
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +000044int 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
Simon Glasseda77322015-04-14 21:03:37 -060068/* Set all parts of a timing entry to the same value */
69static void set_entry(struct timing_entry *entry, u32 value)
70{
71 entry->min = value;
72 entry->typ = value;
73 entry->max = value;
74}
75
76/**
77 * decode_timing() - Decoding an 18-byte detailed timing record
78 *
79 * @buf: Pointer to EDID detailed timing record
80 * @timing: Place to put timing
81 */
82static void decode_timing(u8 *buf, struct display_timing *timing)
83{
84 uint x_mm, y_mm;
85 unsigned int ha, hbl, hso, hspw, hborder;
86 unsigned int va, vbl, vso, vspw, vborder;
Jernej Skrabec837595d2017-04-29 14:43:35 +020087 struct edid_detailed_timing *t = (struct edid_detailed_timing *)buf;
Simon Glasseda77322015-04-14 21:03:37 -060088
89 /* Edid contains pixel clock in terms of 10KHz */
90 set_entry(&timing->pixelclock, (buf[0] + (buf[1] << 8)) * 10000);
91 x_mm = (buf[12] + ((buf[14] & 0xf0) << 4));
92 y_mm = (buf[13] + ((buf[14] & 0x0f) << 8));
93 ha = (buf[2] + ((buf[4] & 0xf0) << 4));
94 hbl = (buf[3] + ((buf[4] & 0x0f) << 8));
95 hso = (buf[8] + ((buf[11] & 0xc0) << 2));
96 hspw = (buf[9] + ((buf[11] & 0x30) << 4));
97 hborder = buf[15];
98 va = (buf[5] + ((buf[7] & 0xf0) << 4));
99 vbl = (buf[6] + ((buf[7] & 0x0f) << 8));
100 vso = ((buf[10] >> 4) + ((buf[11] & 0x0c) << 2));
101 vspw = ((buf[10] & 0x0f) + ((buf[11] & 0x03) << 4));
102 vborder = buf[16];
103
104 set_entry(&timing->hactive, ha);
105 set_entry(&timing->hfront_porch, hso);
106 set_entry(&timing->hback_porch, hbl - hso - hspw);
107 set_entry(&timing->hsync_len, hspw);
108
109 set_entry(&timing->vactive, va);
110 set_entry(&timing->vfront_porch, vso);
111 set_entry(&timing->vback_porch, vbl - vso - vspw);
112 set_entry(&timing->vsync_len, vspw);
113
Jernej Skrabec837595d2017-04-29 14:43:35 +0200114 timing->flags = 0;
115 if (EDID_DETAILED_TIMING_FLAG_HSYNC_POLARITY(*t))
116 timing->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
117 else
118 timing->flags |= DISPLAY_FLAGS_HSYNC_LOW;
119 if (EDID_DETAILED_TIMING_FLAG_VSYNC_POLARITY(*t))
120 timing->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
121 else
122 timing->flags |= DISPLAY_FLAGS_VSYNC_LOW;
123
124 if (EDID_DETAILED_TIMING_FLAG_INTERLACED(*t))
125 timing->flags = DISPLAY_FLAGS_INTERLACED;
126
Simon Glasseda77322015-04-14 21:03:37 -0600127 debug("Detailed mode clock %u Hz, %d mm x %d mm\n"
128 " %04x %04x %04x %04x hborder %x\n"
129 " %04x %04x %04x %04x vborder %x\n",
130 timing->pixelclock.typ,
131 x_mm, y_mm,
132 ha, ha + hso, ha + hso + hspw,
133 ha + hbl, hborder,
134 va, va + vso, va + vso + vspw,
135 va + vbl, vborder);
136}
137
Jernej Skrabec55884e52017-04-29 14:43:36 +0200138/**
139 * Check if HDMI vendor specific data block is present in CEA block
140 * @param info CEA extension block
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100141 * Return: true if block is found
Jernej Skrabec55884e52017-04-29 14:43:36 +0200142 */
143static bool cea_is_hdmi_vsdb_present(struct edid_cea861_info *info)
144{
145 u8 end, i = 0;
146
147 /* check for end of data block */
148 end = info->dtd_offset;
149 if (end == 0)
Simon Glassb0e04282017-06-07 10:28:39 -0600150 end = sizeof(info->data);
151 if (end < 4 || end > sizeof(info->data))
Jernej Skrabec55884e52017-04-29 14:43:36 +0200152 return false;
153 end -= 4;
154
155 while (i < end) {
156 /* Look for vendor specific data block of appropriate size */
157 if ((EDID_CEA861_DB_TYPE(*info, i) == EDID_CEA861_DB_VENDOR) &&
158 (EDID_CEA861_DB_LEN(*info, i) >= 5)) {
159 u8 *db = &info->data[i + 1];
160 u32 oui = db[0] | (db[1] << 8) | (db[2] << 16);
161
162 if (oui == HDMI_IEEE_OUI)
163 return true;
164 }
165 i += EDID_CEA861_DB_LEN(*info, i) + 1;
166 }
167
168 return false;
169}
170
Jernej Skrabec89efafe2021-04-22 01:14:28 +0100171static bool edid_find_valid_timing(void *buf, int count,
172 struct display_timing *timing,
173 bool (*mode_valid)(void *priv,
174 const struct display_timing *timing),
175 void *mode_valid_priv)
176{
177 struct edid_detailed_timing *t = buf;
178 bool found = false;
179 int i;
180
181 for (i = 0; i < count && !found; i++, t++)
182 if (EDID_DETAILED_TIMING_PIXEL_CLOCK(*t) != 0) {
183 decode_timing((u8 *)t, timing);
184 if (mode_valid)
185 found = mode_valid(mode_valid_priv,
186 timing);
187 else
188 found = true;
189 }
190
191 return found;
192}
193
Neil Armstrong99fd44e2019-07-04 15:52:06 +0200194int edid_get_timing_validate(u8 *buf, int buf_size,
195 struct display_timing *timing,
196 int *panel_bits_per_colourp,
197 bool (*mode_valid)(void *priv,
198 const struct display_timing *timing),
199 void *mode_valid_priv)
Simon Glasseda77322015-04-14 21:03:37 -0600200{
201 struct edid1_info *edid = (struct edid1_info *)buf;
Jernej Skrabec89efafe2021-04-22 01:14:28 +0100202 bool found;
Simon Glasseda77322015-04-14 21:03:37 -0600203
204 if (buf_size < sizeof(*edid) || edid_check_info(edid)) {
205 debug("%s: Invalid buffer\n", __func__);
206 return -EINVAL;
207 }
208
Jernej Skrabec37d3bc62021-04-22 01:14:27 +0100209 if (!EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid)) {
210 debug("%s: Not a digital display\n", __func__);
211 return -ENOSYS;
212 }
213
Simon Glasseda77322015-04-14 21:03:37 -0600214 if (!EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(*edid)) {
215 debug("%s: No preferred timing\n", __func__);
216 return -ENOENT;
217 }
218
Jernej Skrabec89efafe2021-04-22 01:14:28 +0100219 /* Look for detailed timing in base EDID */
220 found = edid_find_valid_timing(edid->monitor_details.descriptor, 4,
221 timing, mode_valid, mode_valid_priv);
Jernej Skrabec04f173e2021-04-22 01:14:29 +0100222
223 /* Look for detailed timing in CTA-861 Extension Block */
224 if (!found && edid->extension_flag && buf_size >= EDID_EXT_SIZE) {
225 struct edid_cea861_info *info =
226 (struct edid_cea861_info *)(buf + sizeof(*edid));
227
228 if (info->extension_tag == EDID_CEA861_EXTENSION_TAG) {
229 int count = EDID_CEA861_DTD_COUNT(*info);
230 int offset = info->dtd_offset;
231 int size = count * sizeof(struct edid_detailed_timing);
232
233 if (offset >= 4 && offset + size < EDID_SIZE)
234 found = edid_find_valid_timing(
235 (u8 *)info + offset, count, timing,
236 mode_valid, mode_valid_priv);
237 }
238 }
239
Jernej Skrabec89efafe2021-04-22 01:14:28 +0100240 if (!found)
Simon Glasseda77322015-04-14 21:03:37 -0600241 return -EINVAL;
242
Simon Glasseda77322015-04-14 21:03:37 -0600243 if (edid->version != 1 || edid->revision < 4) {
244 debug("%s: EDID version %d.%d does not have required info\n",
245 __func__, edid->version, edid->revision);
246 *panel_bits_per_colourp = -1;
247 } else {
248 *panel_bits_per_colourp =
249 ((edid->video_input_definition & 0x70) >> 3) + 4;
250 }
251
Jernej Skrabec55884e52017-04-29 14:43:36 +0200252 timing->hdmi_monitor = false;
253 if (edid->extension_flag && (buf_size >= EDID_EXT_SIZE)) {
254 struct edid_cea861_info *info =
255 (struct edid_cea861_info *)(buf + sizeof(*edid));
256
257 if (info->extension_tag == EDID_CEA861_EXTENSION_TAG)
258 timing->hdmi_monitor = cea_is_hdmi_vsdb_present(info);
259 }
260
Simon Glasseda77322015-04-14 21:03:37 -0600261 return 0;
262}
263
Neil Armstrong99fd44e2019-07-04 15:52:06 +0200264int edid_get_timing(u8 *buf, int buf_size, struct display_timing *timing,
265 int *panel_bits_per_colourp)
266{
267 return edid_get_timing_validate(buf, buf_size, timing,
268 panel_bits_per_colourp, NULL, NULL);
269}
270
271
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +0000272/**
273 * Snip the tailing whitespace/return of a string.
274 *
275 * @param string The string to be snipped
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100276 * Return: the snipped string
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +0000277 */
278static char *snip(char *string)
279{
280 char *s;
281
282 /*
283 * This is always a 13 character buffer
284 * and it's not always terminated.
285 */
286 string[12] = '\0';
287 s = &string[strlen(string) - 1];
288
289 while (s >= string && (isspace(*s) || *s == '\n' || *s == '\r' ||
290 *s == '\0'))
291 *(s--) = '\0';
292
293 return string;
294}
295
296/**
297 * Print an EDID monitor descriptor block
298 *
299 * @param monitor The EDID monitor descriptor block
300 * @have_timing Modifies to 1 if the desciptor contains timing info
301 */
302static void edid_print_dtd(struct edid_monitor_descriptor *monitor,
303 unsigned int *have_timing)
304{
305 unsigned char *bytes = (unsigned char *)monitor;
306 struct edid_detailed_timing *timing =
307 (struct edid_detailed_timing *)monitor;
308
309 if (bytes[0] == 0 && bytes[1] == 0) {
310 if (monitor->type == EDID_MONITOR_DESCRIPTOR_SERIAL)
311 printf("Monitor serial number: %s\n",
312 snip(monitor->data.string));
313 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_ASCII)
314 printf("Monitor ID: %s\n",
315 snip(monitor->data.string));
316 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_NAME)
317 printf("Monitor name: %s\n",
318 snip(monitor->data.string));
319 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE)
320 printf("Monitor range limits, horizontal sync: "
321 "%d-%d kHz, vertical refresh: "
322 "%d-%d Hz, max pixel clock: "
323 "%d MHz\n",
324 monitor->data.range_data.horizontal_min,
325 monitor->data.range_data.horizontal_max,
326 monitor->data.range_data.vertical_min,
327 monitor->data.range_data.vertical_max,
328 monitor->data.range_data.pixel_clock_max * 10);
329 } else {
330 uint32_t pixclock, h_active, h_blanking, v_active, v_blanking;
331 uint32_t h_total, v_total, vfreq;
332
333 pixclock = EDID_DETAILED_TIMING_PIXEL_CLOCK(*timing);
334 h_active = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*timing);
335 h_blanking = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*timing);
336 v_active = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*timing);
337 v_blanking = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*timing);
338
339 h_total = h_active + h_blanking;
340 v_total = v_active + v_blanking;
Jernej Skrabec6dcd5a52017-05-23 23:05:30 +0200341 if (v_total > 0 && h_total > 0)
Tom Wai-Hong Tambaebbf52012-12-05 14:46:39 +0000342 vfreq = pixclock / (v_total * h_total);
343 else
344 vfreq = 1; /* Error case */
345 printf("\t%dx%d\%c\t%d Hz (detailed)\n", h_active,
346 v_active, h_active > 1000 ? ' ' : '\t', vfreq);
347 *have_timing = 1;
348 }
349}
350
351/**
352 * Get the manufacturer name from an EDID info.
353 *
354 * @param edid_info The EDID info to be printed
355 * @param name Returns the string of the manufacturer name
356 */
357static void edid_get_manufacturer_name(struct edid1_info *edid, char *name)
358{
359 name[0] = EDID1_INFO_MANUFACTURER_NAME_CHAR1(*edid) + 'A' - 1;
360 name[1] = EDID1_INFO_MANUFACTURER_NAME_CHAR2(*edid) + 'A' - 1;
361 name[2] = EDID1_INFO_MANUFACTURER_NAME_CHAR3(*edid) + 'A' - 1;
362 name[3] = '\0';
363}
364
365void edid_print_info(struct edid1_info *edid_info)
366{
367 int i;
368 char manufacturer[4];
369 unsigned int have_timing = 0;
370 uint32_t serial_number;
371
372 if (edid_check_info(edid_info)) {
373 printf("Not a valid EDID\n");
374 return;
375 }
376
377 printf("EDID version: %d.%d\n",
378 edid_info->version, edid_info->revision);
379
380 printf("Product ID code: %04x\n", EDID1_INFO_PRODUCT_CODE(*edid_info));
381
382 edid_get_manufacturer_name(edid_info, manufacturer);
383 printf("Manufacturer: %s\n", manufacturer);
384
385 serial_number = EDID1_INFO_SERIAL_NUMBER(*edid_info);
386 if (serial_number != 0xffffffff) {
387 if (strcmp(manufacturer, "MAG") == 0)
388 serial_number -= 0x7000000;
389 if (strcmp(manufacturer, "OQI") == 0)
390 serial_number -= 456150000;
391 if (strcmp(manufacturer, "VSC") == 0)
392 serial_number -= 640000000;
393 }
394 printf("Serial number: %08x\n", serial_number);
395 printf("Manufactured in week: %d year: %d\n",
396 edid_info->week, edid_info->year + 1990);
397
398 printf("Video input definition: %svoltage level %d%s%s%s%s%s\n",
399 EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid_info) ?
400 "digital signal, " : "analog signal, ",
401 EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(*edid_info),
402 EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(*edid_info) ?
403 ", blank to black" : "",
404 EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(*edid_info) ?
405 ", separate sync" : "",
406 EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(*edid_info) ?
407 ", composite sync" : "",
408 EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(*edid_info) ?
409 ", sync on green" : "",
410 EDID1_INFO_VIDEO_INPUT_SERRATION_V(*edid_info) ?
411 ", serration v" : "");
412
413 printf("Monitor is %s\n",
414 EDID1_INFO_FEATURE_RGB(*edid_info) ? "RGB" : "non-RGB");
415
416 printf("Maximum visible display size: %d cm x %d cm\n",
417 edid_info->max_size_horizontal,
418 edid_info->max_size_vertical);
419
420 printf("Power management features: %s%s, %s%s, %s%s\n",
421 EDID1_INFO_FEATURE_ACTIVE_OFF(*edid_info) ?
422 "" : "no ", "active off",
423 EDID1_INFO_FEATURE_SUSPEND(*edid_info) ? "" : "no ", "suspend",
424 EDID1_INFO_FEATURE_STANDBY(*edid_info) ? "" : "no ", "standby");
425
426 printf("Estabilished timings:\n");
427 if (EDID1_INFO_ESTABLISHED_TIMING_720X400_70(*edid_info))
428 printf("\t720x400\t\t70 Hz (VGA 640x400, IBM)\n");
429 if (EDID1_INFO_ESTABLISHED_TIMING_720X400_88(*edid_info))
430 printf("\t720x400\t\t88 Hz (XGA2)\n");
431 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_60(*edid_info))
432 printf("\t640x480\t\t60 Hz (VGA)\n");
433 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_67(*edid_info))
434 printf("\t640x480\t\t67 Hz (Mac II, Apple)\n");
435 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_72(*edid_info))
436 printf("\t640x480\t\t72 Hz (VESA)\n");
437 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_75(*edid_info))
438 printf("\t640x480\t\t75 Hz (VESA)\n");
439 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_56(*edid_info))
440 printf("\t800x600\t\t56 Hz (VESA)\n");
441 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_60(*edid_info))
442 printf("\t800x600\t\t60 Hz (VESA)\n");
443 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_72(*edid_info))
444 printf("\t800x600\t\t72 Hz (VESA)\n");
445 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_75(*edid_info))
446 printf("\t800x600\t\t75 Hz (VESA)\n");
447 if (EDID1_INFO_ESTABLISHED_TIMING_832X624_75(*edid_info))
448 printf("\t832x624\t\t75 Hz (Mac II)\n");
449 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(*edid_info))
450 printf("\t1024x768\t87 Hz Interlaced (8514A)\n");
451 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(*edid_info))
452 printf("\t1024x768\t60 Hz (VESA)\n");
453 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(*edid_info))
454 printf("\t1024x768\t70 Hz (VESA)\n");
455 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(*edid_info))
456 printf("\t1024x768\t75 Hz (VESA)\n");
457 if (EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(*edid_info))
458 printf("\t1280x1024\t75 (VESA)\n");
459 if (EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(*edid_info))
460 printf("\t1152x870\t75 (Mac II)\n");
461
462 /* Standard timings. */
463 printf("Standard timings:\n");
464 for (i = 0; i < ARRAY_SIZE(edid_info->standard_timings); i++) {
465 unsigned int aspect = 10000;
466 unsigned int x, y;
467 unsigned char xres, vfreq;
468
469 xres = EDID1_INFO_STANDARD_TIMING_XRESOLUTION(*edid_info, i);
470 vfreq = EDID1_INFO_STANDARD_TIMING_VFREQ(*edid_info, i);
471 if ((xres != vfreq) ||
472 ((xres != 0) && (xres != 1)) ||
473 ((vfreq != 0) && (vfreq != 1))) {
474 switch (EDID1_INFO_STANDARD_TIMING_ASPECT(*edid_info,
475 i)) {
476 case ASPECT_625:
477 aspect = 6250;
478 break;
479 case ASPECT_75:
480 aspect = 7500;
481 break;
482 case ASPECT_8:
483 aspect = 8000;
484 break;
485 case ASPECT_5625:
486 aspect = 5625;
487 break;
488 }
489 x = (xres + 31) * 8;
490 y = x * aspect / 10000;
491 printf("\t%dx%d%c\t%d Hz\n", x, y,
492 x > 1000 ? ' ' : '\t', (vfreq & 0x3f) + 60);
493 have_timing = 1;
494 }
495 }
496
497 /* Detailed timing information. */
498 for (i = 0; i < ARRAY_SIZE(edid_info->monitor_details.descriptor);
499 i++) {
500 edid_print_dtd(&edid_info->monitor_details.descriptor[i],
501 &have_timing);
502 }
503
504 if (!have_timing)
505 printf("\tNone\n");
506}