1 /* $NetBSD: edid.c,v 1.12 2013/02/08 16:35:10 skrll Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Written by Garrett D'Amore for Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/libkern.h>
43 #include <dev/videomode/videomode.h>
44 #include <dev/videomode/ediddevs.h>
45 #include <dev/videomode/edidreg.h>
46 #include <dev/videomode/edidvar.h>
47 #include <dev/videomode/vesagtf.h>
50 #define DIVIDE(x,y) (((x) + ((y) / 2)) / (y))
52 /* These are reversed established timing order */
53 static const char *_edid_modes[] = {
59 "832x624x74", /* rounding error, 74.55 Hz aka "832x624x75" */
68 "720x400x87", /* rounding error, 87.85 Hz aka "720x400x88" */
84 #include <dev/videomode/ediddevs_data.h>
85 #endif /* EDIDVERBOSE */
88 edid_findvendor(const char *vendor)
93 for (n = 0; n < edid_nvendors; n++)
94 if (memcmp(edid_vendors[n].vendor, vendor, 3) == 0)
95 return edid_vendors[n].name;
101 edid_findproduct(const char *vendor, uint16_t product)
106 for (n = 0; n < edid_nproducts; n++)
107 if (edid_products[n].product == product &&
108 memcmp(edid_products[n].vendor, vendor, 3) == 0)
109 return edid_products[n].name;
110 #endif /* EDIDVERBOSE */
116 edid_strchomp(char *ptr)
132 edid_is_valid(uint8_t *d)
135 uint8_t sig[8] = EDID_SIGNATURE;
137 if (memcmp(d, sig, 8) != 0)
140 for (i = 0; i < 128; i++)
142 if ((sum & 0xff) != 0)
149 edid_print(struct edid_info *edid)
155 printf("Vendor: [%s] %s\n", edid->edid_vendor, edid->edid_vendorname);
156 printf("Product: [%04X] %s\n", edid->edid_product,
157 edid->edid_productname);
158 printf("Serial number: %s\n", edid->edid_serial);
159 printf("Manufactured %d Week %d\n",
160 edid->edid_year, edid->edid_week);
161 printf("EDID Version %d.%d\n", edid->edid_version,
162 edid->edid_revision);
163 printf("EDID Comment: %s\n", edid->edid_comment);
165 printf("Video Input: %x\n", edid->edid_video_input);
166 if (edid->edid_video_input & EDID_VIDEO_INPUT_DIGITAL) {
168 if (edid->edid_video_input & EDID_VIDEO_INPUT_DFP1_COMPAT)
169 printf(" (DFP 1.x compatible)");
172 printf("\tAnalog\n");
173 switch (EDID_VIDEO_INPUT_LEVEL(edid->edid_video_input)) {
175 printf("\t-0.7, 0.3V\n");
178 printf("\t-0.714, 0.286V\n");
181 printf("\t-1.0, 0.4V\n");
184 printf("\t-0.7, 0.0V\n");
187 if (edid->edid_video_input & EDID_VIDEO_INPUT_BLANK_TO_BLACK)
188 printf("\tBlank-to-black setup\n");
189 if (edid->edid_video_input & EDID_VIDEO_INPUT_SEPARATE_SYNCS)
190 printf("\tSeparate syncs\n");
191 if (edid->edid_video_input & EDID_VIDEO_INPUT_COMPOSITE_SYNC)
192 printf("\tComposite sync\n");
193 if (edid->edid_video_input & EDID_VIDEO_INPUT_SYNC_ON_GRN)
194 printf("\tSync on green\n");
195 if (edid->edid_video_input & EDID_VIDEO_INPUT_SERRATION)
196 printf("\tSerration vsync\n");
199 printf("Gamma: %d.%02d\n",
200 edid->edid_gamma / 100, edid->edid_gamma % 100);
202 printf("Max Size: %d cm x %d cm\n",
203 edid->edid_max_hsize, edid->edid_max_vsize);
205 printf("Features: %x\n", edid->edid_features);
206 if (edid->edid_features & EDID_FEATURES_STANDBY)
207 printf("\tDPMS standby\n");
208 if (edid->edid_features & EDID_FEATURES_SUSPEND)
209 printf("\tDPMS suspend\n");
210 if (edid->edid_features & EDID_FEATURES_ACTIVE_OFF)
211 printf("\tDPMS active-off\n");
212 switch (EDID_FEATURES_DISP_TYPE(edid->edid_features)) {
213 case EDID_FEATURES_DISP_TYPE_MONO:
214 printf("\tMonochrome\n");
216 case EDID_FEATURES_DISP_TYPE_RGB:
219 case EDID_FEATURES_DISP_TYPE_NON_RGB:
220 printf("\tMulticolor\n");
222 case EDID_FEATURES_DISP_TYPE_UNDEFINED:
223 printf("\tUndefined monitor type\n");
226 if (edid->edid_features & EDID_FEATURES_STD_COLOR)
227 printf("\tStandard color space\n");
228 if (edid->edid_features & EDID_FEATURES_PREFERRED_TIMING)
229 printf("\tPreferred timing\n");
230 if (edid->edid_features & EDID_FEATURES_DEFAULT_GTF)
231 printf("\tDefault GTF supported\n");
233 printf("Chroma Info:\n");
234 printf("\tRed X: 0.%03d\n", edid->edid_chroma.ec_redx);
235 printf("\tRed Y: 0.%03d\n", edid->edid_chroma.ec_redy);
236 printf("\tGrn X: 0.%03d\n", edid->edid_chroma.ec_greenx);
237 printf("\tGrn Y: 0.%03d\n", edid->edid_chroma.ec_greeny);
238 printf("\tBlu X: 0.%03d\n", edid->edid_chroma.ec_bluex);
239 printf("\tBlu Y: 0.%03d\n", edid->edid_chroma.ec_bluey);
240 printf("\tWht X: 0.%03d\n", edid->edid_chroma.ec_whitex);
241 printf("\tWht Y: 0.%03d\n", edid->edid_chroma.ec_whitey);
243 if (edid->edid_have_range) {
245 printf("\tHorizontal: %d - %d kHz\n",
246 edid->edid_range.er_min_hfreq,
247 edid->edid_range.er_max_hfreq);
248 printf("\tVertical: %d - %d Hz\n",
249 edid->edid_range.er_min_vfreq,
250 edid->edid_range.er_max_vfreq);
251 printf("\tMax Dot Clock: %d MHz\n",
252 edid->edid_range.er_max_clock);
253 if (edid->edid_range.er_have_gtf2) {
254 printf("\tGTF2 hfreq: %d\n",
255 edid->edid_range.er_gtf2_hfreq);
256 printf("\tGTF2 C: %d\n", edid->edid_range.er_gtf2_c);
257 printf("\tGTF2 M: %d\n", edid->edid_range.er_gtf2_m);
258 printf("\tGTF2 J: %d\n", edid->edid_range.er_gtf2_j);
259 printf("\tGTF2 K: %d\n", edid->edid_range.er_gtf2_k);
262 printf("Video modes:\n");
263 for (i = 0; i < edid->edid_nmodes; i++) {
264 printf("\t%dx%d @ %dHz",
265 edid->edid_modes[i].hdisplay,
266 edid->edid_modes[i].vdisplay,
267 DIVIDE(DIVIDE(edid->edid_modes[i].dot_clock * 1000,
268 edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal));
269 printf(" (%d %d %d %d %d %d %d",
270 edid->edid_modes[i].dot_clock,
271 edid->edid_modes[i].hsync_start,
272 edid->edid_modes[i].hsync_end,
273 edid->edid_modes[i].htotal,
274 edid->edid_modes[i].vsync_start,
275 edid->edid_modes[i].vsync_end,
276 edid->edid_modes[i].vtotal);
277 printf(" %s%sH %s%sV)\n",
278 edid->edid_modes[i].flags & VID_PHSYNC ? "+" : "",
279 edid->edid_modes[i].flags & VID_NHSYNC ? "-" : "",
280 edid->edid_modes[i].flags & VID_PVSYNC ? "+" : "",
281 edid->edid_modes[i].flags & VID_NVSYNC ? "-" : "");
283 if (edid->edid_preferred_mode)
284 printf("Preferred mode: %dx%d @ %dHz\n",
285 edid->edid_preferred_mode->hdisplay,
286 edid->edid_preferred_mode->vdisplay,
287 DIVIDE(DIVIDE(edid->edid_preferred_mode->dot_clock * 1000,
288 edid->edid_preferred_mode->htotal),
289 edid->edid_preferred_mode->vtotal));
291 printf("Number of extension blocks: %d\n", edid->edid_ext_block_count);
294 static const struct videomode *
295 edid_mode_lookup_list(const char *name)
299 for (i = 0; i < videomode_count; i++)
300 if (strcmp(name, videomode_list[i].name) == 0)
301 return &videomode_list[i];
305 static struct videomode *
306 edid_search_mode(struct edid_info *edid, const struct videomode *mode)
310 refresh = DIVIDE(DIVIDE(mode->dot_clock * 1000,
311 mode->htotal), mode->vtotal);
312 for (i = 0; i < edid->edid_nmodes; i++) {
313 if (mode->hdisplay == edid->edid_modes[i].hdisplay &&
314 mode->vdisplay == edid->edid_modes[i].vdisplay &&
315 refresh == DIVIDE(DIVIDE(
316 edid->edid_modes[i].dot_clock * 1000,
317 edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal)) {
318 return &edid->edid_modes[i];
325 edid_std_timing(uint8_t *data, struct videomode *vmp)
328 const struct videomode *lookup;
331 if ((data[0] == 1 && data[1] == 1) ||
332 (data[0] == 0 && data[1] == 0) ||
333 (data[0] == 0x20 && data[1] == 0x20))
336 x = EDID_STD_TIMING_HRES(data);
337 switch (EDID_STD_TIMING_RATIO(data)) {
338 case EDID_STD_TIMING_RATIO_16_10:
341 case EDID_STD_TIMING_RATIO_4_3:
344 case EDID_STD_TIMING_RATIO_5_4:
347 case EDID_STD_TIMING_RATIO_16_9:
352 f = EDID_STD_TIMING_VFREQ(data);
354 /* first try to lookup the mode as a DMT timing */
355 snprintf(name, sizeof(name), "%dx%dx%d", x, y, f);
356 if ((lookup = edid_mode_lookup_list(name)) != NULL) {
359 /* failing that, calculate it using gtf */
361 * Hmm. I'm not using alternate GTF timings, which
362 * could, in theory, be present.
364 vesagtf_mode(x, y, f, vmp);
370 edid_det_timing(uint8_t *data, struct videomode *vmp)
372 unsigned hactive, hblank, hsyncwid, hsyncoff;
373 unsigned vactive, vblank, vsyncwid, vsyncoff;
376 flags = EDID_DET_TIMING_FLAGS(data);
378 /* we don't support stereo modes (for now) */
379 if (flags & (EDID_DET_TIMING_FLAG_STEREO |
380 EDID_DET_TIMING_FLAG_STEREO_MODE))
383 vmp->dot_clock = EDID_DET_TIMING_DOT_CLOCK(data) / 1000;
385 hactive = EDID_DET_TIMING_HACTIVE(data);
386 hblank = EDID_DET_TIMING_HBLANK(data);
387 hsyncwid = EDID_DET_TIMING_HSYNC_WIDTH(data);
388 hsyncoff = EDID_DET_TIMING_HSYNC_OFFSET(data);
390 vactive = EDID_DET_TIMING_VACTIVE(data);
391 vblank = EDID_DET_TIMING_VBLANK(data);
392 vsyncwid = EDID_DET_TIMING_VSYNC_WIDTH(data);
393 vsyncoff = EDID_DET_TIMING_VSYNC_OFFSET(data);
395 /* Borders are contained within the blank areas. */
397 vmp->hdisplay = hactive;
398 vmp->htotal = hactive + hblank;
399 vmp->hsync_start = hactive + hsyncoff;
400 vmp->hsync_end = vmp->hsync_start + hsyncwid;
402 vmp->vdisplay = vactive;
403 vmp->vtotal = vactive + vblank;
404 vmp->vsync_start = vactive + vsyncoff;
405 vmp->vsync_end = vmp->vsync_start + vsyncwid;
409 if (flags & EDID_DET_TIMING_FLAG_INTERLACE)
410 vmp->flags |= VID_INTERLACE;
411 if (flags & EDID_DET_TIMING_FLAG_HSYNC_POSITIVE)
412 vmp->flags |= VID_PHSYNC;
414 vmp->flags |= VID_NHSYNC;
416 if (flags & EDID_DET_TIMING_FLAG_VSYNC_POSITIVE)
417 vmp->flags |= VID_PVSYNC;
419 vmp->flags |= VID_NVSYNC;
425 edid_block(struct edid_info *edid, uint8_t *data)
428 struct videomode mode, *exist_mode;
430 if (EDID_BLOCK_IS_DET_TIMING(data)) {
431 if (!edid_det_timing(data, &mode))
433 /* Does this mode already exist? */
434 exist_mode = edid_search_mode(edid, &mode);
435 if (exist_mode != NULL) {
437 if (edid->edid_preferred_mode == NULL)
438 edid->edid_preferred_mode = exist_mode;
440 edid->edid_modes[edid->edid_nmodes] = mode;
441 if (edid->edid_preferred_mode == NULL)
442 edid->edid_preferred_mode =
443 &edid->edid_modes[edid->edid_nmodes];
449 switch (EDID_BLOCK_TYPE(data)) {
450 case EDID_DESC_BLOCK_TYPE_SERIAL:
451 memcpy(edid->edid_serial, data + EDID_DESC_ASCII_DATA_OFFSET,
452 EDID_DESC_ASCII_DATA_LEN);
453 edid->edid_serial[sizeof(edid->edid_serial) - 1] = 0;
456 case EDID_DESC_BLOCK_TYPE_ASCII:
457 memcpy(edid->edid_comment, data + EDID_DESC_ASCII_DATA_OFFSET,
458 EDID_DESC_ASCII_DATA_LEN);
459 edid->edid_comment[sizeof(edid->edid_comment) - 1] = 0;
462 case EDID_DESC_BLOCK_TYPE_RANGE:
463 edid->edid_have_range = 1;
464 edid->edid_range.er_min_vfreq = EDID_DESC_RANGE_MIN_VFREQ(data);
465 edid->edid_range.er_max_vfreq = EDID_DESC_RANGE_MAX_VFREQ(data);
466 edid->edid_range.er_min_hfreq = EDID_DESC_RANGE_MIN_HFREQ(data);
467 edid->edid_range.er_max_hfreq = EDID_DESC_RANGE_MAX_HFREQ(data);
468 edid->edid_range.er_max_clock = EDID_DESC_RANGE_MAX_CLOCK(data);
469 if (!EDID_DESC_RANGE_HAVE_GTF2(data))
471 edid->edid_range.er_have_gtf2 = 1;
472 edid->edid_range.er_gtf2_hfreq =
473 EDID_DESC_RANGE_GTF2_HFREQ(data);
474 edid->edid_range.er_gtf2_c = EDID_DESC_RANGE_GTF2_C(data);
475 edid->edid_range.er_gtf2_m = EDID_DESC_RANGE_GTF2_M(data);
476 edid->edid_range.er_gtf2_j = EDID_DESC_RANGE_GTF2_J(data);
477 edid->edid_range.er_gtf2_k = EDID_DESC_RANGE_GTF2_K(data);
480 case EDID_DESC_BLOCK_TYPE_NAME:
481 /* copy the product name into place */
482 memcpy(edid->edid_productname,
483 data + EDID_DESC_ASCII_DATA_OFFSET,
484 EDID_DESC_ASCII_DATA_LEN);
487 case EDID_DESC_BLOCK_TYPE_STD_TIMING:
488 data += EDID_DESC_STD_TIMING_START;
489 for (i = 0; i < EDID_DESC_STD_TIMING_COUNT; i++) {
490 if (edid_std_timing(data, &mode)) {
491 /* Does this mode already exist? */
492 exist_mode = edid_search_mode(edid, &mode);
493 if (exist_mode == NULL) {
494 edid->edid_modes[edid->edid_nmodes] =
503 case EDID_DESC_BLOCK_TYPE_COLOR_POINT:
504 /* XXX: not implemented yet */
510 * Gets EDID version in BCD, e.g. EDID v1.3 returned as 0x0103
513 edid_parse(uint8_t *data, struct edid_info *edid)
515 uint16_t manfid, estmodes;
516 const struct videomode *vmp;
519 int max_dotclock = 0;
522 if (edid_is_valid(data) != 0)
525 /* get product identification */
526 manfid = EDID_VENDOR_ID(data);
527 edid->edid_vendor[0] = EDID_MANFID_0(manfid);
528 edid->edid_vendor[1] = EDID_MANFID_1(manfid);
529 edid->edid_vendor[2] = EDID_MANFID_2(manfid);
530 edid->edid_vendor[3] = 0; /* null terminate for convenience */
532 edid->edid_product = data[EDID_OFFSET_PRODUCT_ID] +
533 (data[EDID_OFFSET_PRODUCT_ID + 1] << 8);
535 name = edid_findvendor(edid->edid_vendor);
537 strlcpy(edid->edid_vendorname, name,
538 sizeof(edid->edid_vendorname));
540 edid->edid_vendorname[0] = '\0';
542 name = edid_findproduct(edid->edid_vendor, edid->edid_product);
544 strlcpy(edid->edid_productname, name,
545 sizeof(edid->edid_productname));
547 edid->edid_productname[0] = '\0';
549 snprintf(edid->edid_serial, sizeof(edid->edid_serial), "%08x",
550 EDID_SERIAL_NUMBER(data));
552 edid->edid_week = EDID_WEEK(data);
553 edid->edid_year = EDID_YEAR(data);
555 /* get edid revision */
556 edid->edid_version = EDID_VERSION(data);
557 edid->edid_revision = EDID_REVISION(data);
559 edid->edid_video_input = EDID_VIDEO_INPUT(data);
560 edid->edid_max_hsize = EDID_MAX_HSIZE(data);
561 edid->edid_max_vsize = EDID_MAX_VSIZE(data);
563 edid->edid_gamma = EDID_GAMMA(data);
564 edid->edid_features = EDID_FEATURES(data);
566 edid->edid_chroma.ec_redx = EDID_CHROMA_REDX(data);
567 edid->edid_chroma.ec_redy = EDID_CHROMA_REDX(data);
568 edid->edid_chroma.ec_greenx = EDID_CHROMA_GREENX(data);
569 edid->edid_chroma.ec_greeny = EDID_CHROMA_GREENY(data);
570 edid->edid_chroma.ec_bluex = EDID_CHROMA_BLUEX(data);
571 edid->edid_chroma.ec_bluey = EDID_CHROMA_BLUEY(data);
572 edid->edid_chroma.ec_whitex = EDID_CHROMA_WHITEX(data);
573 edid->edid_chroma.ec_whitey = EDID_CHROMA_WHITEY(data);
575 edid->edid_ext_block_count = EDID_EXT_BLOCK_COUNT(data);
577 /* lookup established modes */
578 edid->edid_nmodes = 0;
579 edid->edid_preferred_mode = NULL;
580 estmodes = EDID_EST_TIMING(data);
581 /* Iterate in esztablished timing order */
582 for (i = 15; i >= 0; i--) {
583 if (estmodes & (1 << i)) {
584 vmp = edid_mode_lookup_list(_edid_modes[i]);
586 edid->edid_modes[edid->edid_nmodes] = *vmp;
591 printf("no data for est. mode %s\n",
597 /* do standard timing section */
598 for (i = 0; i < EDID_STD_TIMING_COUNT; i++) {
599 struct videomode mode, *exist_mode;
600 if (edid_std_timing(data + EDID_OFFSET_STD_TIMING + i * 2,
602 /* Does this mode already exist? */
603 exist_mode = edid_search_mode(edid, &mode);
604 if (exist_mode == NULL) {
605 edid->edid_modes[edid->edid_nmodes] = mode;
611 /* do detailed timings and descriptors */
612 for (i = 0; i < EDID_BLOCK_COUNT; i++) {
613 edid_block(edid, data + EDID_OFFSET_DESC_BLOCK +
614 i * EDID_BLOCK_SIZE);
617 edid_strchomp(edid->edid_vendorname);
618 edid_strchomp(edid->edid_productname);
619 edid_strchomp(edid->edid_serial);
620 edid_strchomp(edid->edid_comment);
624 * some monitors lie about their maximum supported dot clock
625 * by claiming to support modes which need a higher dot clock
626 * than the stated maximum.
627 * For sanity's sake we bump it to the highest dot clock we find
628 * in the list of supported modes
630 for (i = 0; i < edid->edid_nmodes; i++)
631 if (edid->edid_modes[i].dot_clock > max_dotclock)
632 max_dotclock = edid->edid_modes[i].dot_clock;
634 printf("edid: max_dotclock according to supported modes: %d\n",
637 mhz = (max_dotclock + 999) / 1000;
639 if (edid->edid_have_range) {
640 if (mhz > edid->edid_range.er_max_clock)
641 edid->edid_range.er_max_clock = mhz;
643 edid->edid_range.er_max_clock = mhz;