1 /* $NetBSD: edid.c,v 1.12 2013/02/08 16:35:10 skrll Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
7 * Written by Garrett D'Amore for Itronix Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/libkern.h>
40 #include <dev/videomode/videomode.h>
41 #include <dev/videomode/ediddevs.h>
42 #include <dev/videomode/edidreg.h>
43 #include <dev/videomode/edidvar.h>
44 #include <dev/videomode/vesagtf.h>
47 #define DIVIDE(x,y) (((x) + ((y) / 2)) / (y))
49 /* These are reversed established timing order */
50 static const char *_edid_modes[] = {
56 "832x624x74", /* rounding error, 74.55 Hz aka "832x624x75" */
65 "720x400x87", /* rounding error, 87.85 Hz aka "720x400x88" */
81 #include <dev/videomode/ediddevs_data.h>
82 #endif /* EDIDVERBOSE */
85 edid_findvendor(const char *vendor)
90 for (n = 0; n < edid_nvendors; n++)
91 if (memcmp(edid_vendors[n].vendor, vendor, 3) == 0)
92 return edid_vendors[n].name;
98 edid_findproduct(const char *vendor, uint16_t product)
103 for (n = 0; n < edid_nproducts; n++)
104 if (edid_products[n].product == product &&
105 memcmp(edid_products[n].vendor, vendor, 3) == 0)
106 return edid_products[n].name;
107 #endif /* EDIDVERBOSE */
113 edid_strchomp(char *ptr)
129 edid_is_valid(uint8_t *d)
132 uint8_t sig[8] = EDID_SIGNATURE;
134 if (memcmp(d, sig, 8) != 0)
137 for (i = 0; i < 128; i++)
139 if ((sum & 0xff) != 0)
146 edid_print(struct edid_info *edid)
152 printf("Vendor: [%s] %s\n", edid->edid_vendor, edid->edid_vendorname);
153 printf("Product: [%04X] %s\n", edid->edid_product,
154 edid->edid_productname);
155 printf("Serial number: %s\n", edid->edid_serial);
156 printf("Manufactured %d Week %d\n",
157 edid->edid_year, edid->edid_week);
158 printf("EDID Version %d.%d\n", edid->edid_version,
159 edid->edid_revision);
160 printf("EDID Comment: %s\n", edid->edid_comment);
162 printf("Video Input: %x\n", edid->edid_video_input);
163 if (edid->edid_video_input & EDID_VIDEO_INPUT_DIGITAL) {
165 if (edid->edid_video_input & EDID_VIDEO_INPUT_DFP1_COMPAT)
166 printf(" (DFP 1.x compatible)");
169 printf("\tAnalog\n");
170 switch (EDID_VIDEO_INPUT_LEVEL(edid->edid_video_input)) {
172 printf("\t-0.7, 0.3V\n");
175 printf("\t-0.714, 0.286V\n");
178 printf("\t-1.0, 0.4V\n");
181 printf("\t-0.7, 0.0V\n");
184 if (edid->edid_video_input & EDID_VIDEO_INPUT_BLANK_TO_BLACK)
185 printf("\tBlank-to-black setup\n");
186 if (edid->edid_video_input & EDID_VIDEO_INPUT_SEPARATE_SYNCS)
187 printf("\tSeparate syncs\n");
188 if (edid->edid_video_input & EDID_VIDEO_INPUT_COMPOSITE_SYNC)
189 printf("\tComposite sync\n");
190 if (edid->edid_video_input & EDID_VIDEO_INPUT_SYNC_ON_GRN)
191 printf("\tSync on green\n");
192 if (edid->edid_video_input & EDID_VIDEO_INPUT_SERRATION)
193 printf("\tSerration vsync\n");
196 printf("Gamma: %d.%02d\n",
197 edid->edid_gamma / 100, edid->edid_gamma % 100);
199 printf("Max Size: %d cm x %d cm\n",
200 edid->edid_max_hsize, edid->edid_max_vsize);
202 printf("Features: %x\n", edid->edid_features);
203 if (edid->edid_features & EDID_FEATURES_STANDBY)
204 printf("\tDPMS standby\n");
205 if (edid->edid_features & EDID_FEATURES_SUSPEND)
206 printf("\tDPMS suspend\n");
207 if (edid->edid_features & EDID_FEATURES_ACTIVE_OFF)
208 printf("\tDPMS active-off\n");
209 switch (EDID_FEATURES_DISP_TYPE(edid->edid_features)) {
210 case EDID_FEATURES_DISP_TYPE_MONO:
211 printf("\tMonochrome\n");
213 case EDID_FEATURES_DISP_TYPE_RGB:
216 case EDID_FEATURES_DISP_TYPE_NON_RGB:
217 printf("\tMulticolor\n");
219 case EDID_FEATURES_DISP_TYPE_UNDEFINED:
220 printf("\tUndefined monitor type\n");
223 if (edid->edid_features & EDID_FEATURES_STD_COLOR)
224 printf("\tStandard color space\n");
225 if (edid->edid_features & EDID_FEATURES_PREFERRED_TIMING)
226 printf("\tPreferred timing\n");
227 if (edid->edid_features & EDID_FEATURES_DEFAULT_GTF)
228 printf("\tDefault GTF supported\n");
230 printf("Chroma Info:\n");
231 printf("\tRed X: 0.%03d\n", edid->edid_chroma.ec_redx);
232 printf("\tRed Y: 0.%03d\n", edid->edid_chroma.ec_redy);
233 printf("\tGrn X: 0.%03d\n", edid->edid_chroma.ec_greenx);
234 printf("\tGrn Y: 0.%03d\n", edid->edid_chroma.ec_greeny);
235 printf("\tBlu X: 0.%03d\n", edid->edid_chroma.ec_bluex);
236 printf("\tBlu Y: 0.%03d\n", edid->edid_chroma.ec_bluey);
237 printf("\tWht X: 0.%03d\n", edid->edid_chroma.ec_whitex);
238 printf("\tWht Y: 0.%03d\n", edid->edid_chroma.ec_whitey);
240 if (edid->edid_have_range) {
242 printf("\tHorizontal: %d - %d kHz\n",
243 edid->edid_range.er_min_hfreq,
244 edid->edid_range.er_max_hfreq);
245 printf("\tVertical: %d - %d Hz\n",
246 edid->edid_range.er_min_vfreq,
247 edid->edid_range.er_max_vfreq);
248 printf("\tMax Dot Clock: %d MHz\n",
249 edid->edid_range.er_max_clock);
250 if (edid->edid_range.er_have_gtf2) {
251 printf("\tGTF2 hfreq: %d\n",
252 edid->edid_range.er_gtf2_hfreq);
253 printf("\tGTF2 C: %d\n", edid->edid_range.er_gtf2_c);
254 printf("\tGTF2 M: %d\n", edid->edid_range.er_gtf2_m);
255 printf("\tGTF2 J: %d\n", edid->edid_range.er_gtf2_j);
256 printf("\tGTF2 K: %d\n", edid->edid_range.er_gtf2_k);
259 printf("Video modes:\n");
260 for (i = 0; i < edid->edid_nmodes; i++) {
261 printf("\t%dx%d @ %dHz",
262 edid->edid_modes[i].hdisplay,
263 edid->edid_modes[i].vdisplay,
264 DIVIDE(DIVIDE(edid->edid_modes[i].dot_clock * 1000,
265 edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal));
266 printf(" (%d %d %d %d %d %d %d",
267 edid->edid_modes[i].dot_clock,
268 edid->edid_modes[i].hsync_start,
269 edid->edid_modes[i].hsync_end,
270 edid->edid_modes[i].htotal,
271 edid->edid_modes[i].vsync_start,
272 edid->edid_modes[i].vsync_end,
273 edid->edid_modes[i].vtotal);
274 printf(" %s%sH %s%sV)\n",
275 edid->edid_modes[i].flags & VID_PHSYNC ? "+" : "",
276 edid->edid_modes[i].flags & VID_NHSYNC ? "-" : "",
277 edid->edid_modes[i].flags & VID_PVSYNC ? "+" : "",
278 edid->edid_modes[i].flags & VID_NVSYNC ? "-" : "");
280 if (edid->edid_preferred_mode)
281 printf("Preferred mode: %dx%d @ %dHz\n",
282 edid->edid_preferred_mode->hdisplay,
283 edid->edid_preferred_mode->vdisplay,
284 DIVIDE(DIVIDE(edid->edid_preferred_mode->dot_clock * 1000,
285 edid->edid_preferred_mode->htotal),
286 edid->edid_preferred_mode->vtotal));
288 printf("Number of extension blocks: %d\n", edid->edid_ext_block_count);
291 static const struct videomode *
292 edid_mode_lookup_list(const char *name)
296 for (i = 0; i < videomode_count; i++)
297 if (strcmp(name, videomode_list[i].name) == 0)
298 return &videomode_list[i];
302 static struct videomode *
303 edid_search_mode(struct edid_info *edid, const struct videomode *mode)
307 refresh = DIVIDE(DIVIDE(mode->dot_clock * 1000,
308 mode->htotal), mode->vtotal);
309 for (i = 0; i < edid->edid_nmodes; i++) {
310 if (mode->hdisplay == edid->edid_modes[i].hdisplay &&
311 mode->vdisplay == edid->edid_modes[i].vdisplay &&
312 refresh == DIVIDE(DIVIDE(
313 edid->edid_modes[i].dot_clock * 1000,
314 edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal)) {
315 return &edid->edid_modes[i];
322 edid_std_timing(uint8_t *data, struct videomode *vmp)
325 const struct videomode *lookup;
328 if ((data[0] == 1 && data[1] == 1) ||
329 (data[0] == 0 && data[1] == 0) ||
330 (data[0] == 0x20 && data[1] == 0x20))
333 x = EDID_STD_TIMING_HRES(data);
334 switch (EDID_STD_TIMING_RATIO(data)) {
335 case EDID_STD_TIMING_RATIO_16_10:
338 case EDID_STD_TIMING_RATIO_4_3:
341 case EDID_STD_TIMING_RATIO_5_4:
344 case EDID_STD_TIMING_RATIO_16_9:
349 f = EDID_STD_TIMING_VFREQ(data);
351 /* first try to lookup the mode as a DMT timing */
352 snprintf(name, sizeof(name), "%dx%dx%d", x, y, f);
353 if ((lookup = edid_mode_lookup_list(name)) != NULL) {
356 /* failing that, calculate it using gtf */
358 * Hmm. I'm not using alternate GTF timings, which
359 * could, in theory, be present.
361 vesagtf_mode(x, y, f, vmp);
367 edid_det_timing(uint8_t *data, struct videomode *vmp)
369 unsigned hactive, hblank, hsyncwid, hsyncoff;
370 unsigned vactive, vblank, vsyncwid, vsyncoff;
373 flags = EDID_DET_TIMING_FLAGS(data);
375 /* we don't support stereo modes (for now) */
376 if (flags & (EDID_DET_TIMING_FLAG_STEREO |
377 EDID_DET_TIMING_FLAG_STEREO_MODE))
380 vmp->dot_clock = EDID_DET_TIMING_DOT_CLOCK(data) / 1000;
382 hactive = EDID_DET_TIMING_HACTIVE(data);
383 hblank = EDID_DET_TIMING_HBLANK(data);
384 hsyncwid = EDID_DET_TIMING_HSYNC_WIDTH(data);
385 hsyncoff = EDID_DET_TIMING_HSYNC_OFFSET(data);
387 vactive = EDID_DET_TIMING_VACTIVE(data);
388 vblank = EDID_DET_TIMING_VBLANK(data);
389 vsyncwid = EDID_DET_TIMING_VSYNC_WIDTH(data);
390 vsyncoff = EDID_DET_TIMING_VSYNC_OFFSET(data);
392 /* Borders are contained within the blank areas. */
394 vmp->hdisplay = hactive;
395 vmp->htotal = hactive + hblank;
396 vmp->hsync_start = hactive + hsyncoff;
397 vmp->hsync_end = vmp->hsync_start + hsyncwid;
399 vmp->vdisplay = vactive;
400 vmp->vtotal = vactive + vblank;
401 vmp->vsync_start = vactive + vsyncoff;
402 vmp->vsync_end = vmp->vsync_start + vsyncwid;
406 if (flags & EDID_DET_TIMING_FLAG_INTERLACE)
407 vmp->flags |= VID_INTERLACE;
408 if (flags & EDID_DET_TIMING_FLAG_HSYNC_POSITIVE)
409 vmp->flags |= VID_PHSYNC;
411 vmp->flags |= VID_NHSYNC;
413 if (flags & EDID_DET_TIMING_FLAG_VSYNC_POSITIVE)
414 vmp->flags |= VID_PVSYNC;
416 vmp->flags |= VID_NVSYNC;
422 edid_block(struct edid_info *edid, uint8_t *data)
425 struct videomode mode, *exist_mode;
427 if (EDID_BLOCK_IS_DET_TIMING(data)) {
428 if (!edid_det_timing(data, &mode))
430 /* Does this mode already exist? */
431 exist_mode = edid_search_mode(edid, &mode);
432 if (exist_mode != NULL) {
434 if (edid->edid_preferred_mode == NULL)
435 edid->edid_preferred_mode = exist_mode;
437 edid->edid_modes[edid->edid_nmodes] = mode;
438 if (edid->edid_preferred_mode == NULL)
439 edid->edid_preferred_mode =
440 &edid->edid_modes[edid->edid_nmodes];
446 switch (EDID_BLOCK_TYPE(data)) {
447 case EDID_DESC_BLOCK_TYPE_SERIAL:
448 memcpy(edid->edid_serial, data + EDID_DESC_ASCII_DATA_OFFSET,
449 EDID_DESC_ASCII_DATA_LEN);
450 edid->edid_serial[sizeof(edid->edid_serial) - 1] = 0;
453 case EDID_DESC_BLOCK_TYPE_ASCII:
454 memcpy(edid->edid_comment, data + EDID_DESC_ASCII_DATA_OFFSET,
455 EDID_DESC_ASCII_DATA_LEN);
456 edid->edid_comment[sizeof(edid->edid_comment) - 1] = 0;
459 case EDID_DESC_BLOCK_TYPE_RANGE:
460 edid->edid_have_range = 1;
461 edid->edid_range.er_min_vfreq = EDID_DESC_RANGE_MIN_VFREQ(data);
462 edid->edid_range.er_max_vfreq = EDID_DESC_RANGE_MAX_VFREQ(data);
463 edid->edid_range.er_min_hfreq = EDID_DESC_RANGE_MIN_HFREQ(data);
464 edid->edid_range.er_max_hfreq = EDID_DESC_RANGE_MAX_HFREQ(data);
465 edid->edid_range.er_max_clock = EDID_DESC_RANGE_MAX_CLOCK(data);
466 if (!EDID_DESC_RANGE_HAVE_GTF2(data))
468 edid->edid_range.er_have_gtf2 = 1;
469 edid->edid_range.er_gtf2_hfreq =
470 EDID_DESC_RANGE_GTF2_HFREQ(data);
471 edid->edid_range.er_gtf2_c = EDID_DESC_RANGE_GTF2_C(data);
472 edid->edid_range.er_gtf2_m = EDID_DESC_RANGE_GTF2_M(data);
473 edid->edid_range.er_gtf2_j = EDID_DESC_RANGE_GTF2_J(data);
474 edid->edid_range.er_gtf2_k = EDID_DESC_RANGE_GTF2_K(data);
477 case EDID_DESC_BLOCK_TYPE_NAME:
478 /* copy the product name into place */
479 memcpy(edid->edid_productname,
480 data + EDID_DESC_ASCII_DATA_OFFSET,
481 EDID_DESC_ASCII_DATA_LEN);
484 case EDID_DESC_BLOCK_TYPE_STD_TIMING:
485 data += EDID_DESC_STD_TIMING_START;
486 for (i = 0; i < EDID_DESC_STD_TIMING_COUNT; i++) {
487 if (edid_std_timing(data, &mode)) {
488 /* Does this mode already exist? */
489 exist_mode = edid_search_mode(edid, &mode);
490 if (exist_mode == NULL) {
491 edid->edid_modes[edid->edid_nmodes] =
500 case EDID_DESC_BLOCK_TYPE_COLOR_POINT:
501 /* XXX: not implemented yet */
507 * Gets EDID version in BCD, e.g. EDID v1.3 returned as 0x0103
510 edid_parse(uint8_t *data, struct edid_info *edid)
512 uint16_t manfid, estmodes;
513 const struct videomode *vmp;
516 int max_dotclock = 0;
519 if (edid_is_valid(data) != 0)
522 /* get product identification */
523 manfid = EDID_VENDOR_ID(data);
524 edid->edid_vendor[0] = EDID_MANFID_0(manfid);
525 edid->edid_vendor[1] = EDID_MANFID_1(manfid);
526 edid->edid_vendor[2] = EDID_MANFID_2(manfid);
527 edid->edid_vendor[3] = 0; /* null terminate for convenience */
529 edid->edid_product = data[EDID_OFFSET_PRODUCT_ID] +
530 (data[EDID_OFFSET_PRODUCT_ID + 1] << 8);
532 name = edid_findvendor(edid->edid_vendor);
534 strlcpy(edid->edid_vendorname, name,
535 sizeof(edid->edid_vendorname));
537 edid->edid_vendorname[0] = '\0';
539 name = edid_findproduct(edid->edid_vendor, edid->edid_product);
541 strlcpy(edid->edid_productname, name,
542 sizeof(edid->edid_productname));
544 edid->edid_productname[0] = '\0';
546 snprintf(edid->edid_serial, sizeof(edid->edid_serial), "%08x",
547 EDID_SERIAL_NUMBER(data));
549 edid->edid_week = EDID_WEEK(data);
550 edid->edid_year = EDID_YEAR(data);
552 /* get edid revision */
553 edid->edid_version = EDID_VERSION(data);
554 edid->edid_revision = EDID_REVISION(data);
556 edid->edid_video_input = EDID_VIDEO_INPUT(data);
557 edid->edid_max_hsize = EDID_MAX_HSIZE(data);
558 edid->edid_max_vsize = EDID_MAX_VSIZE(data);
560 edid->edid_gamma = EDID_GAMMA(data);
561 edid->edid_features = EDID_FEATURES(data);
563 edid->edid_chroma.ec_redx = EDID_CHROMA_REDX(data);
564 edid->edid_chroma.ec_redy = EDID_CHROMA_REDX(data);
565 edid->edid_chroma.ec_greenx = EDID_CHROMA_GREENX(data);
566 edid->edid_chroma.ec_greeny = EDID_CHROMA_GREENY(data);
567 edid->edid_chroma.ec_bluex = EDID_CHROMA_BLUEX(data);
568 edid->edid_chroma.ec_bluey = EDID_CHROMA_BLUEY(data);
569 edid->edid_chroma.ec_whitex = EDID_CHROMA_WHITEX(data);
570 edid->edid_chroma.ec_whitey = EDID_CHROMA_WHITEY(data);
572 edid->edid_ext_block_count = EDID_EXT_BLOCK_COUNT(data);
574 /* lookup established modes */
575 edid->edid_nmodes = 0;
576 edid->edid_preferred_mode = NULL;
577 estmodes = EDID_EST_TIMING(data);
578 /* Iterate in esztablished timing order */
579 for (i = 15; i >= 0; i--) {
580 if (estmodes & (1 << i)) {
581 vmp = edid_mode_lookup_list(_edid_modes[i]);
583 edid->edid_modes[edid->edid_nmodes] = *vmp;
588 printf("no data for est. mode %s\n",
594 /* do standard timing section */
595 for (i = 0; i < EDID_STD_TIMING_COUNT; i++) {
596 struct videomode mode, *exist_mode;
597 if (edid_std_timing(data + EDID_OFFSET_STD_TIMING + i * 2,
599 /* Does this mode already exist? */
600 exist_mode = edid_search_mode(edid, &mode);
601 if (exist_mode == NULL) {
602 edid->edid_modes[edid->edid_nmodes] = mode;
608 /* do detailed timings and descriptors */
609 for (i = 0; i < EDID_BLOCK_COUNT; i++) {
610 edid_block(edid, data + EDID_OFFSET_DESC_BLOCK +
611 i * EDID_BLOCK_SIZE);
614 edid_strchomp(edid->edid_vendorname);
615 edid_strchomp(edid->edid_productname);
616 edid_strchomp(edid->edid_serial);
617 edid_strchomp(edid->edid_comment);
621 * some monitors lie about their maximum supported dot clock
622 * by claiming to support modes which need a higher dot clock
623 * than the stated maximum.
624 * For sanity's sake we bump it to the highest dot clock we find
625 * in the list of supported modes
627 for (i = 0; i < edid->edid_nmodes; i++)
628 if (edid->edid_modes[i].dot_clock > max_dotclock)
629 max_dotclock = edid->edid_modes[i].dot_clock;
631 printf("edid: max_dotclock according to supported modes: %d\n",
634 mhz = (max_dotclock + 999) / 1000;
636 if (edid->edid_have_range) {
637 if (mhz > edid->edid_range.er_max_clock)
638 edid->edid_range.er_max_clock = mhz;
640 edid->edid_range.er_max_clock = mhz;