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/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/libkern.h>
39 #include <dev/videomode/videomode.h>
40 #include <dev/videomode/ediddevs.h>
41 #include <dev/videomode/edidreg.h>
42 #include <dev/videomode/edidvar.h>
43 #include <dev/videomode/vesagtf.h>
46 #define DIVIDE(x,y) (((x) + ((y) / 2)) / (y))
48 /* These are reversed established timing order */
49 static const char *_edid_modes[] = {
55 "832x624x74", /* rounding error, 74.55 Hz aka "832x624x75" */
64 "720x400x87", /* rounding error, 87.85 Hz aka "720x400x88" */
80 #include <dev/videomode/ediddevs_data.h>
81 #endif /* EDIDVERBOSE */
84 edid_findvendor(const char *vendor)
89 for (n = 0; n < edid_nvendors; n++)
90 if (memcmp(edid_vendors[n].vendor, vendor, 3) == 0)
91 return edid_vendors[n].name;
97 edid_findproduct(const char *vendor, uint16_t product)
102 for (n = 0; n < edid_nproducts; n++)
103 if (edid_products[n].product == product &&
104 memcmp(edid_products[n].vendor, vendor, 3) == 0)
105 return edid_products[n].name;
106 #endif /* EDIDVERBOSE */
112 edid_strchomp(char *ptr)
128 edid_is_valid(uint8_t *d)
131 uint8_t sig[8] = EDID_SIGNATURE;
133 if (memcmp(d, sig, 8) != 0)
136 for (i = 0; i < 128; i++)
138 if ((sum & 0xff) != 0)
145 edid_print(struct edid_info *edid)
151 printf("Vendor: [%s] %s\n", edid->edid_vendor, edid->edid_vendorname);
152 printf("Product: [%04X] %s\n", edid->edid_product,
153 edid->edid_productname);
154 printf("Serial number: %s\n", edid->edid_serial);
155 printf("Manufactured %d Week %d\n",
156 edid->edid_year, edid->edid_week);
157 printf("EDID Version %d.%d\n", edid->edid_version,
158 edid->edid_revision);
159 printf("EDID Comment: %s\n", edid->edid_comment);
161 printf("Video Input: %x\n", edid->edid_video_input);
162 if (edid->edid_video_input & EDID_VIDEO_INPUT_DIGITAL) {
164 if (edid->edid_video_input & EDID_VIDEO_INPUT_DFP1_COMPAT)
165 printf(" (DFP 1.x compatible)");
168 printf("\tAnalog\n");
169 switch (EDID_VIDEO_INPUT_LEVEL(edid->edid_video_input)) {
171 printf("\t-0.7, 0.3V\n");
174 printf("\t-0.714, 0.286V\n");
177 printf("\t-1.0, 0.4V\n");
180 printf("\t-0.7, 0.0V\n");
183 if (edid->edid_video_input & EDID_VIDEO_INPUT_BLANK_TO_BLACK)
184 printf("\tBlank-to-black setup\n");
185 if (edid->edid_video_input & EDID_VIDEO_INPUT_SEPARATE_SYNCS)
186 printf("\tSeparate syncs\n");
187 if (edid->edid_video_input & EDID_VIDEO_INPUT_COMPOSITE_SYNC)
188 printf("\tComposite sync\n");
189 if (edid->edid_video_input & EDID_VIDEO_INPUT_SYNC_ON_GRN)
190 printf("\tSync on green\n");
191 if (edid->edid_video_input & EDID_VIDEO_INPUT_SERRATION)
192 printf("\tSerration vsync\n");
195 printf("Gamma: %d.%02d\n",
196 edid->edid_gamma / 100, edid->edid_gamma % 100);
198 printf("Max Size: %d cm x %d cm\n",
199 edid->edid_max_hsize, edid->edid_max_vsize);
201 printf("Features: %x\n", edid->edid_features);
202 if (edid->edid_features & EDID_FEATURES_STANDBY)
203 printf("\tDPMS standby\n");
204 if (edid->edid_features & EDID_FEATURES_SUSPEND)
205 printf("\tDPMS suspend\n");
206 if (edid->edid_features & EDID_FEATURES_ACTIVE_OFF)
207 printf("\tDPMS active-off\n");
208 switch (EDID_FEATURES_DISP_TYPE(edid->edid_features)) {
209 case EDID_FEATURES_DISP_TYPE_MONO:
210 printf("\tMonochrome\n");
212 case EDID_FEATURES_DISP_TYPE_RGB:
215 case EDID_FEATURES_DISP_TYPE_NON_RGB:
216 printf("\tMulticolor\n");
218 case EDID_FEATURES_DISP_TYPE_UNDEFINED:
219 printf("\tUndefined monitor type\n");
222 if (edid->edid_features & EDID_FEATURES_STD_COLOR)
223 printf("\tStandard color space\n");
224 if (edid->edid_features & EDID_FEATURES_PREFERRED_TIMING)
225 printf("\tPreferred timing\n");
226 if (edid->edid_features & EDID_FEATURES_DEFAULT_GTF)
227 printf("\tDefault GTF supported\n");
229 printf("Chroma Info:\n");
230 printf("\tRed X: 0.%03d\n", edid->edid_chroma.ec_redx);
231 printf("\tRed Y: 0.%03d\n", edid->edid_chroma.ec_redy);
232 printf("\tGrn X: 0.%03d\n", edid->edid_chroma.ec_greenx);
233 printf("\tGrn Y: 0.%03d\n", edid->edid_chroma.ec_greeny);
234 printf("\tBlu X: 0.%03d\n", edid->edid_chroma.ec_bluex);
235 printf("\tBlu Y: 0.%03d\n", edid->edid_chroma.ec_bluey);
236 printf("\tWht X: 0.%03d\n", edid->edid_chroma.ec_whitex);
237 printf("\tWht Y: 0.%03d\n", edid->edid_chroma.ec_whitey);
239 if (edid->edid_have_range) {
241 printf("\tHorizontal: %d - %d kHz\n",
242 edid->edid_range.er_min_hfreq,
243 edid->edid_range.er_max_hfreq);
244 printf("\tVertical: %d - %d Hz\n",
245 edid->edid_range.er_min_vfreq,
246 edid->edid_range.er_max_vfreq);
247 printf("\tMax Dot Clock: %d MHz\n",
248 edid->edid_range.er_max_clock);
249 if (edid->edid_range.er_have_gtf2) {
250 printf("\tGTF2 hfreq: %d\n",
251 edid->edid_range.er_gtf2_hfreq);
252 printf("\tGTF2 C: %d\n", edid->edid_range.er_gtf2_c);
253 printf("\tGTF2 M: %d\n", edid->edid_range.er_gtf2_m);
254 printf("\tGTF2 J: %d\n", edid->edid_range.er_gtf2_j);
255 printf("\tGTF2 K: %d\n", edid->edid_range.er_gtf2_k);
258 printf("Video modes:\n");
259 for (i = 0; i < edid->edid_nmodes; i++) {
260 printf("\t%dx%d @ %dHz",
261 edid->edid_modes[i].hdisplay,
262 edid->edid_modes[i].vdisplay,
263 DIVIDE(DIVIDE(edid->edid_modes[i].dot_clock * 1000,
264 edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal));
265 printf(" (%d %d %d %d %d %d %d",
266 edid->edid_modes[i].dot_clock,
267 edid->edid_modes[i].hsync_start,
268 edid->edid_modes[i].hsync_end,
269 edid->edid_modes[i].htotal,
270 edid->edid_modes[i].vsync_start,
271 edid->edid_modes[i].vsync_end,
272 edid->edid_modes[i].vtotal);
273 printf(" %s%sH %s%sV)\n",
274 edid->edid_modes[i].flags & VID_PHSYNC ? "+" : "",
275 edid->edid_modes[i].flags & VID_NHSYNC ? "-" : "",
276 edid->edid_modes[i].flags & VID_PVSYNC ? "+" : "",
277 edid->edid_modes[i].flags & VID_NVSYNC ? "-" : "");
279 if (edid->edid_preferred_mode)
280 printf("Preferred mode: %dx%d @ %dHz\n",
281 edid->edid_preferred_mode->hdisplay,
282 edid->edid_preferred_mode->vdisplay,
283 DIVIDE(DIVIDE(edid->edid_preferred_mode->dot_clock * 1000,
284 edid->edid_preferred_mode->htotal),
285 edid->edid_preferred_mode->vtotal));
287 printf("Number of extension blocks: %d\n", edid->edid_ext_block_count);
290 static const struct videomode *
291 edid_mode_lookup_list(const char *name)
295 for (i = 0; i < videomode_count; i++)
296 if (strcmp(name, videomode_list[i].name) == 0)
297 return &videomode_list[i];
301 static struct videomode *
302 edid_search_mode(struct edid_info *edid, const struct videomode *mode)
306 refresh = DIVIDE(DIVIDE(mode->dot_clock * 1000,
307 mode->htotal), mode->vtotal);
308 for (i = 0; i < edid->edid_nmodes; i++) {
309 if (mode->hdisplay == edid->edid_modes[i].hdisplay &&
310 mode->vdisplay == edid->edid_modes[i].vdisplay &&
311 refresh == DIVIDE(DIVIDE(
312 edid->edid_modes[i].dot_clock * 1000,
313 edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal)) {
314 return &edid->edid_modes[i];
321 edid_std_timing(uint8_t *data, struct videomode *vmp)
324 const struct videomode *lookup;
327 if ((data[0] == 1 && data[1] == 1) ||
328 (data[0] == 0 && data[1] == 0) ||
329 (data[0] == 0x20 && data[1] == 0x20))
332 x = EDID_STD_TIMING_HRES(data);
333 switch (EDID_STD_TIMING_RATIO(data)) {
334 case EDID_STD_TIMING_RATIO_16_10:
337 case EDID_STD_TIMING_RATIO_4_3:
340 case EDID_STD_TIMING_RATIO_5_4:
343 case EDID_STD_TIMING_RATIO_16_9:
348 f = EDID_STD_TIMING_VFREQ(data);
350 /* first try to lookup the mode as a DMT timing */
351 snprintf(name, sizeof(name), "%dx%dx%d", x, y, f);
352 if ((lookup = edid_mode_lookup_list(name)) != NULL) {
355 /* failing that, calculate it using gtf */
357 * Hmm. I'm not using alternate GTF timings, which
358 * could, in theory, be present.
360 vesagtf_mode(x, y, f, vmp);
366 edid_det_timing(uint8_t *data, struct videomode *vmp)
368 unsigned hactive, hblank, hsyncwid, hsyncoff;
369 unsigned vactive, vblank, vsyncwid, vsyncoff;
372 flags = EDID_DET_TIMING_FLAGS(data);
374 /* we don't support stereo modes (for now) */
375 if (flags & (EDID_DET_TIMING_FLAG_STEREO |
376 EDID_DET_TIMING_FLAG_STEREO_MODE))
379 vmp->dot_clock = EDID_DET_TIMING_DOT_CLOCK(data) / 1000;
381 hactive = EDID_DET_TIMING_HACTIVE(data);
382 hblank = EDID_DET_TIMING_HBLANK(data);
383 hsyncwid = EDID_DET_TIMING_HSYNC_WIDTH(data);
384 hsyncoff = EDID_DET_TIMING_HSYNC_OFFSET(data);
386 vactive = EDID_DET_TIMING_VACTIVE(data);
387 vblank = EDID_DET_TIMING_VBLANK(data);
388 vsyncwid = EDID_DET_TIMING_VSYNC_WIDTH(data);
389 vsyncoff = EDID_DET_TIMING_VSYNC_OFFSET(data);
391 /* Borders are contained within the blank areas. */
393 vmp->hdisplay = hactive;
394 vmp->htotal = hactive + hblank;
395 vmp->hsync_start = hactive + hsyncoff;
396 vmp->hsync_end = vmp->hsync_start + hsyncwid;
398 vmp->vdisplay = vactive;
399 vmp->vtotal = vactive + vblank;
400 vmp->vsync_start = vactive + vsyncoff;
401 vmp->vsync_end = vmp->vsync_start + vsyncwid;
405 if (flags & EDID_DET_TIMING_FLAG_INTERLACE)
406 vmp->flags |= VID_INTERLACE;
407 if (flags & EDID_DET_TIMING_FLAG_HSYNC_POSITIVE)
408 vmp->flags |= VID_PHSYNC;
410 vmp->flags |= VID_NHSYNC;
412 if (flags & EDID_DET_TIMING_FLAG_VSYNC_POSITIVE)
413 vmp->flags |= VID_PVSYNC;
415 vmp->flags |= VID_NVSYNC;
421 edid_block(struct edid_info *edid, uint8_t *data)
424 struct videomode mode, *exist_mode;
426 if (EDID_BLOCK_IS_DET_TIMING(data)) {
427 if (!edid_det_timing(data, &mode))
429 /* Does this mode already exist? */
430 exist_mode = edid_search_mode(edid, &mode);
431 if (exist_mode != NULL) {
433 if (edid->edid_preferred_mode == NULL)
434 edid->edid_preferred_mode = exist_mode;
436 edid->edid_modes[edid->edid_nmodes] = mode;
437 if (edid->edid_preferred_mode == NULL)
438 edid->edid_preferred_mode =
439 &edid->edid_modes[edid->edid_nmodes];
445 switch (EDID_BLOCK_TYPE(data)) {
446 case EDID_DESC_BLOCK_TYPE_SERIAL:
447 memcpy(edid->edid_serial, data + EDID_DESC_ASCII_DATA_OFFSET,
448 EDID_DESC_ASCII_DATA_LEN);
449 edid->edid_serial[sizeof(edid->edid_serial) - 1] = 0;
452 case EDID_DESC_BLOCK_TYPE_ASCII:
453 memcpy(edid->edid_comment, data + EDID_DESC_ASCII_DATA_OFFSET,
454 EDID_DESC_ASCII_DATA_LEN);
455 edid->edid_comment[sizeof(edid->edid_comment) - 1] = 0;
458 case EDID_DESC_BLOCK_TYPE_RANGE:
459 edid->edid_have_range = 1;
460 edid->edid_range.er_min_vfreq = EDID_DESC_RANGE_MIN_VFREQ(data);
461 edid->edid_range.er_max_vfreq = EDID_DESC_RANGE_MAX_VFREQ(data);
462 edid->edid_range.er_min_hfreq = EDID_DESC_RANGE_MIN_HFREQ(data);
463 edid->edid_range.er_max_hfreq = EDID_DESC_RANGE_MAX_HFREQ(data);
464 edid->edid_range.er_max_clock = EDID_DESC_RANGE_MAX_CLOCK(data);
465 if (!EDID_DESC_RANGE_HAVE_GTF2(data))
467 edid->edid_range.er_have_gtf2 = 1;
468 edid->edid_range.er_gtf2_hfreq =
469 EDID_DESC_RANGE_GTF2_HFREQ(data);
470 edid->edid_range.er_gtf2_c = EDID_DESC_RANGE_GTF2_C(data);
471 edid->edid_range.er_gtf2_m = EDID_DESC_RANGE_GTF2_M(data);
472 edid->edid_range.er_gtf2_j = EDID_DESC_RANGE_GTF2_J(data);
473 edid->edid_range.er_gtf2_k = EDID_DESC_RANGE_GTF2_K(data);
476 case EDID_DESC_BLOCK_TYPE_NAME:
477 /* copy the product name into place */
478 memcpy(edid->edid_productname,
479 data + EDID_DESC_ASCII_DATA_OFFSET,
480 EDID_DESC_ASCII_DATA_LEN);
483 case EDID_DESC_BLOCK_TYPE_STD_TIMING:
484 data += EDID_DESC_STD_TIMING_START;
485 for (i = 0; i < EDID_DESC_STD_TIMING_COUNT; i++) {
486 if (edid_std_timing(data, &mode)) {
487 /* Does this mode already exist? */
488 exist_mode = edid_search_mode(edid, &mode);
489 if (exist_mode == NULL) {
490 edid->edid_modes[edid->edid_nmodes] =
499 case EDID_DESC_BLOCK_TYPE_COLOR_POINT:
500 /* XXX: not implemented yet */
506 * Gets EDID version in BCD, e.g. EDID v1.3 returned as 0x0103
509 edid_parse(uint8_t *data, struct edid_info *edid)
511 uint16_t manfid, estmodes;
512 const struct videomode *vmp;
515 int max_dotclock = 0;
518 if (edid_is_valid(data) != 0)
521 /* get product identification */
522 manfid = EDID_VENDOR_ID(data);
523 edid->edid_vendor[0] = EDID_MANFID_0(manfid);
524 edid->edid_vendor[1] = EDID_MANFID_1(manfid);
525 edid->edid_vendor[2] = EDID_MANFID_2(manfid);
526 edid->edid_vendor[3] = 0; /* null terminate for convenience */
528 edid->edid_product = data[EDID_OFFSET_PRODUCT_ID] +
529 (data[EDID_OFFSET_PRODUCT_ID + 1] << 8);
531 name = edid_findvendor(edid->edid_vendor);
533 strlcpy(edid->edid_vendorname, name,
534 sizeof(edid->edid_vendorname));
536 edid->edid_vendorname[0] = '\0';
538 name = edid_findproduct(edid->edid_vendor, edid->edid_product);
540 strlcpy(edid->edid_productname, name,
541 sizeof(edid->edid_productname));
543 edid->edid_productname[0] = '\0';
545 snprintf(edid->edid_serial, sizeof(edid->edid_serial), "%08x",
546 EDID_SERIAL_NUMBER(data));
548 edid->edid_week = EDID_WEEK(data);
549 edid->edid_year = EDID_YEAR(data);
551 /* get edid revision */
552 edid->edid_version = EDID_VERSION(data);
553 edid->edid_revision = EDID_REVISION(data);
555 edid->edid_video_input = EDID_VIDEO_INPUT(data);
556 edid->edid_max_hsize = EDID_MAX_HSIZE(data);
557 edid->edid_max_vsize = EDID_MAX_VSIZE(data);
559 edid->edid_gamma = EDID_GAMMA(data);
560 edid->edid_features = EDID_FEATURES(data);
562 edid->edid_chroma.ec_redx = EDID_CHROMA_REDX(data);
563 edid->edid_chroma.ec_redy = EDID_CHROMA_REDX(data);
564 edid->edid_chroma.ec_greenx = EDID_CHROMA_GREENX(data);
565 edid->edid_chroma.ec_greeny = EDID_CHROMA_GREENY(data);
566 edid->edid_chroma.ec_bluex = EDID_CHROMA_BLUEX(data);
567 edid->edid_chroma.ec_bluey = EDID_CHROMA_BLUEY(data);
568 edid->edid_chroma.ec_whitex = EDID_CHROMA_WHITEX(data);
569 edid->edid_chroma.ec_whitey = EDID_CHROMA_WHITEY(data);
571 edid->edid_ext_block_count = EDID_EXT_BLOCK_COUNT(data);
573 /* lookup established modes */
574 edid->edid_nmodes = 0;
575 edid->edid_preferred_mode = NULL;
576 estmodes = EDID_EST_TIMING(data);
577 /* Iterate in esztablished timing order */
578 for (i = 15; i >= 0; i--) {
579 if (estmodes & (1 << i)) {
580 vmp = edid_mode_lookup_list(_edid_modes[i]);
582 edid->edid_modes[edid->edid_nmodes] = *vmp;
587 printf("no data for est. mode %s\n",
593 /* do standard timing section */
594 for (i = 0; i < EDID_STD_TIMING_COUNT; i++) {
595 struct videomode mode, *exist_mode;
596 if (edid_std_timing(data + EDID_OFFSET_STD_TIMING + i * 2,
598 /* Does this mode already exist? */
599 exist_mode = edid_search_mode(edid, &mode);
600 if (exist_mode == NULL) {
601 edid->edid_modes[edid->edid_nmodes] = mode;
607 /* do detailed timings and descriptors */
608 for (i = 0; i < EDID_BLOCK_COUNT; i++) {
609 edid_block(edid, data + EDID_OFFSET_DESC_BLOCK +
610 i * EDID_BLOCK_SIZE);
613 edid_strchomp(edid->edid_vendorname);
614 edid_strchomp(edid->edid_productname);
615 edid_strchomp(edid->edid_serial);
616 edid_strchomp(edid->edid_comment);
620 * some monitors lie about their maximum supported dot clock
621 * by claiming to support modes which need a higher dot clock
622 * than the stated maximum.
623 * For sanity's sake we bump it to the highest dot clock we find
624 * in the list of supported modes
626 for (i = 0; i < edid->edid_nmodes; i++)
627 if (edid->edid_modes[i].dot_clock > max_dotclock)
628 max_dotclock = edid->edid_modes[i].dot_clock;
630 printf("edid: max_dotclock according to supported modes: %d\n",
633 mhz = (max_dotclock + 999) / 1000;
635 if (edid->edid_have_range) {
636 if (mhz > edid->edid_range.er_max_clock)
637 edid->edid_range.er_max_clock = mhz;
639 edid->edid_range.er_max_clock = mhz;