Merge branch 'master' of git://git.denx.de/u-boot-mmc
[platform/kernel/u-boot.git] / common / edid.c
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 <fdtdec.h>
17 #include <linux/ctype.h>
18 #include <linux/string.h>
19
20 int 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
34 int 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
45 int 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
69 /* Set all parts of a timing entry to the same value */
70 static 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  */
83 static 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;
88         struct edid_detailed_timing *t = (struct edid_detailed_timing *)buf;
89
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
115         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
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,
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
139 /**
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  */
144 static 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)
151                 end = 127;
152         if (end < 4 || end > 127)
153                 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
172 int edid_get_timing(u8 *buf, int buf_size, struct display_timing *timing,
173                     int *panel_bits_per_colourp)
174 {
175         struct edid1_info *edid = (struct edid1_info *)buf;
176         bool timing_done;
177         int i;
178
179         if (buf_size < sizeof(*edid) || edid_check_info(edid)) {
180                 debug("%s: Invalid buffer\n", __func__);
181                 return -EINVAL;
182         }
183
184         if (!EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(*edid)) {
185                 debug("%s: No preferred timing\n", __func__);
186                 return -ENOENT;
187         }
188
189         /* Look for detailed timing */
190         timing_done = false;
191         for (i = 0; i < 4; i++) {
192                 struct edid_monitor_descriptor *desc;
193
194                 desc = &edid->monitor_details.descriptor[i];
195                 if (desc->zero_flag_1 != 0) {
196                         decode_timing((u8 *)desc, timing);
197                         timing_done = true;
198                         break;
199                 }
200         }
201         if (!timing_done)
202                 return -EINVAL;
203
204         if (!EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid)) {
205                 debug("%s: Not a digital display\n", __func__);
206                 return -ENOSYS;
207         }
208         if (edid->version != 1 || edid->revision < 4) {
209                 debug("%s: EDID version %d.%d does not have required info\n",
210                       __func__, edid->version, edid->revision);
211                 *panel_bits_per_colourp = -1;
212         } else  {
213                 *panel_bits_per_colourp =
214                         ((edid->video_input_definition & 0x70) >> 3) + 4;
215         }
216
217         timing->hdmi_monitor = false;
218         if (edid->extension_flag && (buf_size >= EDID_EXT_SIZE)) {
219                 struct edid_cea861_info *info =
220                         (struct edid_cea861_info *)(buf + sizeof(*edid));
221
222                 if (info->extension_tag == EDID_CEA861_EXTENSION_TAG)
223                         timing->hdmi_monitor = cea_is_hdmi_vsdb_present(info);
224         }
225
226         return 0;
227 }
228
229 /**
230  * Snip the tailing whitespace/return of a string.
231  *
232  * @param string        The string to be snipped
233  * @return the snipped string
234  */
235 static char *snip(char *string)
236 {
237         char *s;
238
239         /*
240          * This is always a 13 character buffer
241          * and it's not always terminated.
242          */
243         string[12] = '\0';
244         s = &string[strlen(string) - 1];
245
246         while (s >= string && (isspace(*s) || *s == '\n' || *s == '\r' ||
247                         *s == '\0'))
248                 *(s--) = '\0';
249
250         return string;
251 }
252
253 /**
254  * Print an EDID monitor descriptor block
255  *
256  * @param monitor       The EDID monitor descriptor block
257  * @have_timing         Modifies to 1 if the desciptor contains timing info
258  */
259 static void edid_print_dtd(struct edid_monitor_descriptor *monitor,
260                            unsigned int *have_timing)
261 {
262         unsigned char *bytes = (unsigned char *)monitor;
263         struct edid_detailed_timing *timing =
264                         (struct edid_detailed_timing *)monitor;
265
266         if (bytes[0] == 0 && bytes[1] == 0) {
267                 if (monitor->type == EDID_MONITOR_DESCRIPTOR_SERIAL)
268                         printf("Monitor serial number: %s\n",
269                                snip(monitor->data.string));
270                 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_ASCII)
271                         printf("Monitor ID: %s\n",
272                                snip(monitor->data.string));
273                 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_NAME)
274                         printf("Monitor name: %s\n",
275                                snip(monitor->data.string));
276                 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE)
277                         printf("Monitor range limits, horizontal sync: "
278                                "%d-%d kHz, vertical refresh: "
279                                "%d-%d Hz, max pixel clock: "
280                                "%d MHz\n",
281                                monitor->data.range_data.horizontal_min,
282                                monitor->data.range_data.horizontal_max,
283                                monitor->data.range_data.vertical_min,
284                                monitor->data.range_data.vertical_max,
285                                monitor->data.range_data.pixel_clock_max * 10);
286         } else {
287                 uint32_t pixclock, h_active, h_blanking, v_active, v_blanking;
288                 uint32_t h_total, v_total, vfreq;
289
290                 pixclock = EDID_DETAILED_TIMING_PIXEL_CLOCK(*timing);
291                 h_active = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*timing);
292                 h_blanking = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*timing);
293                 v_active = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*timing);
294                 v_blanking = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*timing);
295
296                 h_total = h_active + h_blanking;
297                 v_total = v_active + v_blanking;
298                 if (v_total * h_total)
299                         vfreq = pixclock / (v_total * h_total);
300                 else
301                         vfreq = 1; /* Error case */
302                 printf("\t%dx%d\%c\t%d Hz (detailed)\n", h_active,
303                        v_active, h_active > 1000 ? ' ' : '\t', vfreq);
304                 *have_timing = 1;
305         }
306 }
307
308 /**
309  * Get the manufacturer name from an EDID info.
310  *
311  * @param edid_info     The EDID info to be printed
312  * @param name          Returns the string of the manufacturer name
313  */
314 static void edid_get_manufacturer_name(struct edid1_info *edid, char *name)
315 {
316         name[0] = EDID1_INFO_MANUFACTURER_NAME_CHAR1(*edid) + 'A' - 1;
317         name[1] = EDID1_INFO_MANUFACTURER_NAME_CHAR2(*edid) + 'A' - 1;
318         name[2] = EDID1_INFO_MANUFACTURER_NAME_CHAR3(*edid) + 'A' - 1;
319         name[3] = '\0';
320 }
321
322 void edid_print_info(struct edid1_info *edid_info)
323 {
324         int i;
325         char manufacturer[4];
326         unsigned int have_timing = 0;
327         uint32_t serial_number;
328
329         if (edid_check_info(edid_info)) {
330                 printf("Not a valid EDID\n");
331                 return;
332         }
333
334         printf("EDID version: %d.%d\n",
335                edid_info->version, edid_info->revision);
336
337         printf("Product ID code: %04x\n", EDID1_INFO_PRODUCT_CODE(*edid_info));
338
339         edid_get_manufacturer_name(edid_info, manufacturer);
340         printf("Manufacturer: %s\n", manufacturer);
341
342         serial_number = EDID1_INFO_SERIAL_NUMBER(*edid_info);
343         if (serial_number != 0xffffffff) {
344                 if (strcmp(manufacturer, "MAG") == 0)
345                         serial_number -= 0x7000000;
346                 if (strcmp(manufacturer, "OQI") == 0)
347                         serial_number -= 456150000;
348                 if (strcmp(manufacturer, "VSC") == 0)
349                         serial_number -= 640000000;
350         }
351         printf("Serial number: %08x\n", serial_number);
352         printf("Manufactured in week: %d year: %d\n",
353                edid_info->week, edid_info->year + 1990);
354
355         printf("Video input definition: %svoltage level %d%s%s%s%s%s\n",
356                EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid_info) ?
357                "digital signal, " : "analog signal, ",
358                EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(*edid_info),
359                EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(*edid_info) ?
360                ", blank to black" : "",
361                EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(*edid_info) ?
362                ", separate sync" : "",
363                EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(*edid_info) ?
364                ", composite sync" : "",
365                EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(*edid_info) ?
366                ", sync on green" : "",
367                EDID1_INFO_VIDEO_INPUT_SERRATION_V(*edid_info) ?
368                ", serration v" : "");
369
370         printf("Monitor is %s\n",
371                EDID1_INFO_FEATURE_RGB(*edid_info) ? "RGB" : "non-RGB");
372
373         printf("Maximum visible display size: %d cm x %d cm\n",
374                edid_info->max_size_horizontal,
375                edid_info->max_size_vertical);
376
377         printf("Power management features: %s%s, %s%s, %s%s\n",
378                EDID1_INFO_FEATURE_ACTIVE_OFF(*edid_info) ?
379                "" : "no ", "active off",
380                EDID1_INFO_FEATURE_SUSPEND(*edid_info) ? "" : "no ", "suspend",
381                EDID1_INFO_FEATURE_STANDBY(*edid_info) ? "" : "no ", "standby");
382
383         printf("Estabilished timings:\n");
384         if (EDID1_INFO_ESTABLISHED_TIMING_720X400_70(*edid_info))
385                 printf("\t720x400\t\t70 Hz (VGA 640x400, IBM)\n");
386         if (EDID1_INFO_ESTABLISHED_TIMING_720X400_88(*edid_info))
387                 printf("\t720x400\t\t88 Hz (XGA2)\n");
388         if (EDID1_INFO_ESTABLISHED_TIMING_640X480_60(*edid_info))
389                 printf("\t640x480\t\t60 Hz (VGA)\n");
390         if (EDID1_INFO_ESTABLISHED_TIMING_640X480_67(*edid_info))
391                 printf("\t640x480\t\t67 Hz (Mac II, Apple)\n");
392         if (EDID1_INFO_ESTABLISHED_TIMING_640X480_72(*edid_info))
393                 printf("\t640x480\t\t72 Hz (VESA)\n");
394         if (EDID1_INFO_ESTABLISHED_TIMING_640X480_75(*edid_info))
395                 printf("\t640x480\t\t75 Hz (VESA)\n");
396         if (EDID1_INFO_ESTABLISHED_TIMING_800X600_56(*edid_info))
397                 printf("\t800x600\t\t56 Hz (VESA)\n");
398         if (EDID1_INFO_ESTABLISHED_TIMING_800X600_60(*edid_info))
399                 printf("\t800x600\t\t60 Hz (VESA)\n");
400         if (EDID1_INFO_ESTABLISHED_TIMING_800X600_72(*edid_info))
401                 printf("\t800x600\t\t72 Hz (VESA)\n");
402         if (EDID1_INFO_ESTABLISHED_TIMING_800X600_75(*edid_info))
403                 printf("\t800x600\t\t75 Hz (VESA)\n");
404         if (EDID1_INFO_ESTABLISHED_TIMING_832X624_75(*edid_info))
405                 printf("\t832x624\t\t75 Hz (Mac II)\n");
406         if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(*edid_info))
407                 printf("\t1024x768\t87 Hz Interlaced (8514A)\n");
408         if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(*edid_info))
409                 printf("\t1024x768\t60 Hz (VESA)\n");
410         if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(*edid_info))
411                 printf("\t1024x768\t70 Hz (VESA)\n");
412         if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(*edid_info))
413                 printf("\t1024x768\t75 Hz (VESA)\n");
414         if (EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(*edid_info))
415                 printf("\t1280x1024\t75 (VESA)\n");
416         if (EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(*edid_info))
417                 printf("\t1152x870\t75 (Mac II)\n");
418
419         /* Standard timings. */
420         printf("Standard timings:\n");
421         for (i = 0; i < ARRAY_SIZE(edid_info->standard_timings); i++) {
422                 unsigned int aspect = 10000;
423                 unsigned int x, y;
424                 unsigned char xres, vfreq;
425
426                 xres = EDID1_INFO_STANDARD_TIMING_XRESOLUTION(*edid_info, i);
427                 vfreq = EDID1_INFO_STANDARD_TIMING_VFREQ(*edid_info, i);
428                 if ((xres != vfreq) ||
429                     ((xres != 0) && (xres != 1)) ||
430                     ((vfreq != 0) && (vfreq != 1))) {
431                         switch (EDID1_INFO_STANDARD_TIMING_ASPECT(*edid_info,
432                                         i)) {
433                         case ASPECT_625:
434                                 aspect = 6250;
435                                 break;
436                         case ASPECT_75:
437                                 aspect = 7500;
438                                 break;
439                         case ASPECT_8:
440                                 aspect = 8000;
441                                 break;
442                         case ASPECT_5625:
443                                 aspect = 5625;
444                                 break;
445                         }
446                         x = (xres + 31) * 8;
447                         y = x * aspect / 10000;
448                         printf("\t%dx%d%c\t%d Hz\n", x, y,
449                                x > 1000 ? ' ' : '\t', (vfreq & 0x3f) + 60);
450                         have_timing = 1;
451                 }
452         }
453
454         /* Detailed timing information. */
455         for (i = 0; i < ARRAY_SIZE(edid_info->monitor_details.descriptor);
456                         i++) {
457                 edid_print_dtd(&edid_info->monitor_details.descriptor[i],
458                                &have_timing);
459         }
460
461         if (!have_timing)
462                 printf("\tNone\n");
463 }