blob: a6c875d9c8e8d81a96e196aad4c4509fe11fe783 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Wai-Hong Tamd46b5f72012-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 Tamd46b5f72012-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 Tamd46b5f72012-12-05 14:46:39 +000010 */
11
12#include <common.h>
13#include <edid.h>
Hans de Goedee745d062014-11-24 13:47:13 +010014#include <errno.h>
Simon Glass00cf1162015-04-14 21:03:37 -060015#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Tom Wai-Hong Tamd46b5f72012-12-05 14:46:39 +000017#include <linux/ctype.h>
18#include <linux/string.h>
19
20int edid_check_info(struct edid1_info *edid_info)
21{
22 if ((edid_info == NULL) || (edid_info->version == 0))
23 return -1;
24
25 if (memcmp(edid_info->header, "\x0\xff\xff\xff\xff\xff\xff\x0", 8))
26 return -1;
27
28 if (edid_info->version == 0xff && edid_info->revision == 0xff)
29 return -1;
30
31 return 0;
32}
33
Hans de Goedee745d062014-11-24 13:47:13 +010034int edid_check_checksum(u8 *edid_block)
35{
36 u8 checksum = 0;
37 int i;
38
39 for (i = 0; i < 128; i++)
40 checksum += edid_block[i];
41
42 return (checksum == 0) ? 0 : -EINVAL;
43}
44
Tom Wai-Hong Tamd46b5f72012-12-05 14:46:39 +000045int edid_get_ranges(struct edid1_info *edid, unsigned int *hmin,
46 unsigned int *hmax, unsigned int *vmin,
47 unsigned int *vmax)
48{
49 int i;
50 struct edid_monitor_descriptor *monitor;
51
52 *hmin = *hmax = *vmin = *vmax = 0;
53 if (edid_check_info(edid))
54 return -1;
55
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;
63 return 0;
64 }
65 }
66 return -1;
67}
68
Simon Glass00cf1162015-04-14 21:03:37 -060069/* Set all parts of a timing entry to the same value */
70static void set_entry(struct timing_entry *entry, u32 value)
71{
72 entry->min = value;
73 entry->typ = value;
74 entry->max = value;
75}
76
77/**
78 * decode_timing() - Decoding an 18-byte detailed timing record
79 *
80 * @buf: Pointer to EDID detailed timing record
81 * @timing: Place to put timing
82 */
83static void decode_timing(u8 *buf, struct display_timing *timing)
84{
85 uint x_mm, y_mm;
86 unsigned int ha, hbl, hso, hspw, hborder;
87 unsigned int va, vbl, vso, vspw, vborder;
Jernej Skrabecdc8cae42017-04-29 14:43:35 +020088 struct edid_detailed_timing *t = (struct edid_detailed_timing *)buf;
Simon Glass00cf1162015-04-14 21:03:37 -060089
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));
98 hborder = buf[15];
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));
103 vborder = buf[16];
104
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);
109
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);
114
Jernej Skrabecdc8cae42017-04-29 14:43:35 +0200115 timing->flags = 0;
116 if (EDID_DETAILED_TIMING_FLAG_HSYNC_POLARITY(*t))
117 timing->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
118 else
119 timing->flags |= DISPLAY_FLAGS_HSYNC_LOW;
120 if (EDID_DETAILED_TIMING_FLAG_VSYNC_POLARITY(*t))
121 timing->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
122 else
123 timing->flags |= DISPLAY_FLAGS_VSYNC_LOW;
124
125 if (EDID_DETAILED_TIMING_FLAG_INTERLACED(*t))
126 timing->flags = DISPLAY_FLAGS_INTERLACED;
127
Simon Glass00cf1162015-04-14 21:03:37 -0600128 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,
132 x_mm, y_mm,
133 ha, ha + hso, ha + hso + hspw,
134 ha + hbl, hborder,
135 va, va + vso, va + vso + vspw,
136 va + vbl, vborder);
137}
138
Jernej Skrabec43c6bdd2017-04-29 14:43:36 +0200139/**
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
143 */
144static bool cea_is_hdmi_vsdb_present(struct edid_cea861_info *info)
145{
146 u8 end, i = 0;
147
148 /* check for end of data block */
149 end = info->dtd_offset;
150 if (end == 0)
Simon Glass66a1b302017-06-07 10:28:39 -0600151 end = sizeof(info->data);
152 if (end < 4 || end > sizeof(info->data))
Jernej Skrabec43c6bdd2017-04-29 14:43:36 +0200153 return false;
154 end -= 4;
155
156 while (i < end) {
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);
162
163 if (oui == HDMI_IEEE_OUI)
164 return true;
165 }
166 i += EDID_CEA861_DB_LEN(*info, i) + 1;
167 }
168
169 return false;
170}
171
Jernej Skrabec3daea862021-04-22 01:14:28 +0100172static bool edid_find_valid_timing(void *buf, int count,
173 struct display_timing *timing,
174 bool (*mode_valid)(void *priv,
175 const struct display_timing *timing),
176 void *mode_valid_priv)
177{
178 struct edid_detailed_timing *t = buf;
179 bool found = false;
180 int i;
181
182 for (i = 0; i < count && !found; i++, t++)
183 if (EDID_DETAILED_TIMING_PIXEL_CLOCK(*t) != 0) {
184 decode_timing((u8 *)t, timing);
185 if (mode_valid)
186 found = mode_valid(mode_valid_priv,
187 timing);
188 else
189 found = true;
190 }
191
192 return found;
193}
194
Neil Armstrong1c1ed442019-07-04 15:52:06 +0200195int edid_get_timing_validate(u8 *buf, int buf_size,
196 struct display_timing *timing,
197 int *panel_bits_per_colourp,
198 bool (*mode_valid)(void *priv,
199 const struct display_timing *timing),
200 void *mode_valid_priv)
Simon Glass00cf1162015-04-14 21:03:37 -0600201{
202 struct edid1_info *edid = (struct edid1_info *)buf;
Jernej Skrabec3daea862021-04-22 01:14:28 +0100203 bool found;
Simon Glass00cf1162015-04-14 21:03:37 -0600204
205 if (buf_size < sizeof(*edid) || edid_check_info(edid)) {
206 debug("%s: Invalid buffer\n", __func__);
207 return -EINVAL;
208 }
209
Jernej Skrabec4fb0c3c2021-04-22 01:14:27 +0100210 if (!EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid)) {
211 debug("%s: Not a digital display\n", __func__);
212 return -ENOSYS;
213 }
214
Simon Glass00cf1162015-04-14 21:03:37 -0600215 if (!EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(*edid)) {
216 debug("%s: No preferred timing\n", __func__);
217 return -ENOENT;
218 }
219
Jernej Skrabec3daea862021-04-22 01:14:28 +0100220 /* Look for detailed timing in base EDID */
221 found = edid_find_valid_timing(edid->monitor_details.descriptor, 4,
222 timing, mode_valid, mode_valid_priv);
223 if (!found)
Simon Glass00cf1162015-04-14 21:03:37 -0600224 return -EINVAL;
225
Simon Glass00cf1162015-04-14 21:03:37 -0600226 if (edid->version != 1 || edid->revision < 4) {
227 debug("%s: EDID version %d.%d does not have required info\n",
228 __func__, edid->version, edid->revision);
229 *panel_bits_per_colourp = -1;
230 } else {
231 *panel_bits_per_colourp =
232 ((edid->video_input_definition & 0x70) >> 3) + 4;
233 }
234
Jernej Skrabec43c6bdd2017-04-29 14:43:36 +0200235 timing->hdmi_monitor = false;
236 if (edid->extension_flag && (buf_size >= EDID_EXT_SIZE)) {
237 struct edid_cea861_info *info =
238 (struct edid_cea861_info *)(buf + sizeof(*edid));
239
240 if (info->extension_tag == EDID_CEA861_EXTENSION_TAG)
241 timing->hdmi_monitor = cea_is_hdmi_vsdb_present(info);
242 }
243
Simon Glass00cf1162015-04-14 21:03:37 -0600244 return 0;
245}
246
Neil Armstrong1c1ed442019-07-04 15:52:06 +0200247int edid_get_timing(u8 *buf, int buf_size, struct display_timing *timing,
248 int *panel_bits_per_colourp)
249{
250 return edid_get_timing_validate(buf, buf_size, timing,
251 panel_bits_per_colourp, NULL, NULL);
252}
253
254
Tom Wai-Hong Tamd46b5f72012-12-05 14:46:39 +0000255/**
256 * Snip the tailing whitespace/return of a string.
257 *
258 * @param string The string to be snipped
259 * @return the snipped string
260 */
261static char *snip(char *string)
262{
263 char *s;
264
265 /*
266 * This is always a 13 character buffer
267 * and it's not always terminated.
268 */
269 string[12] = '\0';
270 s = &string[strlen(string) - 1];
271
272 while (s >= string && (isspace(*s) || *s == '\n' || *s == '\r' ||
273 *s == '\0'))
274 *(s--) = '\0';
275
276 return string;
277}
278
279/**
280 * Print an EDID monitor descriptor block
281 *
282 * @param monitor The EDID monitor descriptor block
283 * @have_timing Modifies to 1 if the desciptor contains timing info
284 */
285static void edid_print_dtd(struct edid_monitor_descriptor *monitor,
286 unsigned int *have_timing)
287{
288 unsigned char *bytes = (unsigned char *)monitor;
289 struct edid_detailed_timing *timing =
290 (struct edid_detailed_timing *)monitor;
291
292 if (bytes[0] == 0 && bytes[1] == 0) {
293 if (monitor->type == EDID_MONITOR_DESCRIPTOR_SERIAL)
294 printf("Monitor serial number: %s\n",
295 snip(monitor->data.string));
296 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_ASCII)
297 printf("Monitor ID: %s\n",
298 snip(monitor->data.string));
299 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_NAME)
300 printf("Monitor name: %s\n",
301 snip(monitor->data.string));
302 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE)
303 printf("Monitor range limits, horizontal sync: "
304 "%d-%d kHz, vertical refresh: "
305 "%d-%d Hz, max pixel clock: "
306 "%d MHz\n",
307 monitor->data.range_data.horizontal_min,
308 monitor->data.range_data.horizontal_max,
309 monitor->data.range_data.vertical_min,
310 monitor->data.range_data.vertical_max,
311 monitor->data.range_data.pixel_clock_max * 10);
312 } else {
313 uint32_t pixclock, h_active, h_blanking, v_active, v_blanking;
314 uint32_t h_total, v_total, vfreq;
315
316 pixclock = EDID_DETAILED_TIMING_PIXEL_CLOCK(*timing);
317 h_active = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*timing);
318 h_blanking = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*timing);
319 v_active = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*timing);
320 v_blanking = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*timing);
321
322 h_total = h_active + h_blanking;
323 v_total = v_active + v_blanking;
Jernej Skrabecbdc906d2017-05-23 23:05:30 +0200324 if (v_total > 0 && h_total > 0)
Tom Wai-Hong Tamd46b5f72012-12-05 14:46:39 +0000325 vfreq = pixclock / (v_total * h_total);
326 else
327 vfreq = 1; /* Error case */
328 printf("\t%dx%d\%c\t%d Hz (detailed)\n", h_active,
329 v_active, h_active > 1000 ? ' ' : '\t', vfreq);
330 *have_timing = 1;
331 }
332}
333
334/**
335 * Get the manufacturer name from an EDID info.
336 *
337 * @param edid_info The EDID info to be printed
338 * @param name Returns the string of the manufacturer name
339 */
340static void edid_get_manufacturer_name(struct edid1_info *edid, char *name)
341{
342 name[0] = EDID1_INFO_MANUFACTURER_NAME_CHAR1(*edid) + 'A' - 1;
343 name[1] = EDID1_INFO_MANUFACTURER_NAME_CHAR2(*edid) + 'A' - 1;
344 name[2] = EDID1_INFO_MANUFACTURER_NAME_CHAR3(*edid) + 'A' - 1;
345 name[3] = '\0';
346}
347
348void edid_print_info(struct edid1_info *edid_info)
349{
350 int i;
351 char manufacturer[4];
352 unsigned int have_timing = 0;
353 uint32_t serial_number;
354
355 if (edid_check_info(edid_info)) {
356 printf("Not a valid EDID\n");
357 return;
358 }
359
360 printf("EDID version: %d.%d\n",
361 edid_info->version, edid_info->revision);
362
363 printf("Product ID code: %04x\n", EDID1_INFO_PRODUCT_CODE(*edid_info));
364
365 edid_get_manufacturer_name(edid_info, manufacturer);
366 printf("Manufacturer: %s\n", manufacturer);
367
368 serial_number = EDID1_INFO_SERIAL_NUMBER(*edid_info);
369 if (serial_number != 0xffffffff) {
370 if (strcmp(manufacturer, "MAG") == 0)
371 serial_number -= 0x7000000;
372 if (strcmp(manufacturer, "OQI") == 0)
373 serial_number -= 456150000;
374 if (strcmp(manufacturer, "VSC") == 0)
375 serial_number -= 640000000;
376 }
377 printf("Serial number: %08x\n", serial_number);
378 printf("Manufactured in week: %d year: %d\n",
379 edid_info->week, edid_info->year + 1990);
380
381 printf("Video input definition: %svoltage level %d%s%s%s%s%s\n",
382 EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid_info) ?
383 "digital signal, " : "analog signal, ",
384 EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(*edid_info),
385 EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(*edid_info) ?
386 ", blank to black" : "",
387 EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(*edid_info) ?
388 ", separate sync" : "",
389 EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(*edid_info) ?
390 ", composite sync" : "",
391 EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(*edid_info) ?
392 ", sync on green" : "",
393 EDID1_INFO_VIDEO_INPUT_SERRATION_V(*edid_info) ?
394 ", serration v" : "");
395
396 printf("Monitor is %s\n",
397 EDID1_INFO_FEATURE_RGB(*edid_info) ? "RGB" : "non-RGB");
398
399 printf("Maximum visible display size: %d cm x %d cm\n",
400 edid_info->max_size_horizontal,
401 edid_info->max_size_vertical);
402
403 printf("Power management features: %s%s, %s%s, %s%s\n",
404 EDID1_INFO_FEATURE_ACTIVE_OFF(*edid_info) ?
405 "" : "no ", "active off",
406 EDID1_INFO_FEATURE_SUSPEND(*edid_info) ? "" : "no ", "suspend",
407 EDID1_INFO_FEATURE_STANDBY(*edid_info) ? "" : "no ", "standby");
408
409 printf("Estabilished timings:\n");
410 if (EDID1_INFO_ESTABLISHED_TIMING_720X400_70(*edid_info))
411 printf("\t720x400\t\t70 Hz (VGA 640x400, IBM)\n");
412 if (EDID1_INFO_ESTABLISHED_TIMING_720X400_88(*edid_info))
413 printf("\t720x400\t\t88 Hz (XGA2)\n");
414 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_60(*edid_info))
415 printf("\t640x480\t\t60 Hz (VGA)\n");
416 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_67(*edid_info))
417 printf("\t640x480\t\t67 Hz (Mac II, Apple)\n");
418 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_72(*edid_info))
419 printf("\t640x480\t\t72 Hz (VESA)\n");
420 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_75(*edid_info))
421 printf("\t640x480\t\t75 Hz (VESA)\n");
422 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_56(*edid_info))
423 printf("\t800x600\t\t56 Hz (VESA)\n");
424 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_60(*edid_info))
425 printf("\t800x600\t\t60 Hz (VESA)\n");
426 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_72(*edid_info))
427 printf("\t800x600\t\t72 Hz (VESA)\n");
428 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_75(*edid_info))
429 printf("\t800x600\t\t75 Hz (VESA)\n");
430 if (EDID1_INFO_ESTABLISHED_TIMING_832X624_75(*edid_info))
431 printf("\t832x624\t\t75 Hz (Mac II)\n");
432 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(*edid_info))
433 printf("\t1024x768\t87 Hz Interlaced (8514A)\n");
434 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(*edid_info))
435 printf("\t1024x768\t60 Hz (VESA)\n");
436 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(*edid_info))
437 printf("\t1024x768\t70 Hz (VESA)\n");
438 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(*edid_info))
439 printf("\t1024x768\t75 Hz (VESA)\n");
440 if (EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(*edid_info))
441 printf("\t1280x1024\t75 (VESA)\n");
442 if (EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(*edid_info))
443 printf("\t1152x870\t75 (Mac II)\n");
444
445 /* Standard timings. */
446 printf("Standard timings:\n");
447 for (i = 0; i < ARRAY_SIZE(edid_info->standard_timings); i++) {
448 unsigned int aspect = 10000;
449 unsigned int x, y;
450 unsigned char xres, vfreq;
451
452 xres = EDID1_INFO_STANDARD_TIMING_XRESOLUTION(*edid_info, i);
453 vfreq = EDID1_INFO_STANDARD_TIMING_VFREQ(*edid_info, i);
454 if ((xres != vfreq) ||
455 ((xres != 0) && (xres != 1)) ||
456 ((vfreq != 0) && (vfreq != 1))) {
457 switch (EDID1_INFO_STANDARD_TIMING_ASPECT(*edid_info,
458 i)) {
459 case ASPECT_625:
460 aspect = 6250;
461 break;
462 case ASPECT_75:
463 aspect = 7500;
464 break;
465 case ASPECT_8:
466 aspect = 8000;
467 break;
468 case ASPECT_5625:
469 aspect = 5625;
470 break;
471 }
472 x = (xres + 31) * 8;
473 y = x * aspect / 10000;
474 printf("\t%dx%d%c\t%d Hz\n", x, y,
475 x > 1000 ? ' ' : '\t', (vfreq & 0x3f) + 60);
476 have_timing = 1;
477 }
478 }
479
480 /* Detailed timing information. */
481 for (i = 0; i < ARRAY_SIZE(edid_info->monitor_details.descriptor);
482 i++) {
483 edid_print_dtd(&edid_info->monitor_details.descriptor[i],
484 &have_timing);
485 }
486
487 if (!have_timing)
488 printf("\tNone\n");
489}