xref: /netbsd-src/sys/dev/videomode/edid.c (revision 882ce77c8259f2ea91d3a28ccd730038dd0e6f6f)
1*882ce77cSthorpej /* $NetBSD: edid.c,v 1.18 2022/09/25 21:27:39 thorpej Exp $ */
26594783dSgdamore 
36594783dSgdamore /*-
46594783dSgdamore  * Copyright (c) 2006 Itronix Inc.
56594783dSgdamore  * All rights reserved.
66594783dSgdamore  *
76594783dSgdamore  * Written by Garrett D'Amore for Itronix Inc.
86594783dSgdamore  *
96594783dSgdamore  * Redistribution and use in source and binary forms, with or without
106594783dSgdamore  * modification, are permitted provided that the following conditions
116594783dSgdamore  * are met:
126594783dSgdamore  * 1. Redistributions of source code must retain the above copyright
136594783dSgdamore  *    notice, this list of conditions and the following disclaimer.
146594783dSgdamore  * 2. Redistributions in binary form must reproduce the above copyright
156594783dSgdamore  *    notice, this list of conditions and the following disclaimer in the
166594783dSgdamore  *    documentation and/or other materials provided with the distribution.
176594783dSgdamore  * 3. The name of Itronix Inc. may not be used to endorse
186594783dSgdamore  *    or promote products derived from this software without specific
196594783dSgdamore  *    prior written permission.
206594783dSgdamore  *
216594783dSgdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
226594783dSgdamore  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
236594783dSgdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246594783dSgdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
256594783dSgdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266594783dSgdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
276594783dSgdamore  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
286594783dSgdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
296594783dSgdamore  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
306594783dSgdamore  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
316594783dSgdamore  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
326594783dSgdamore  */
336594783dSgdamore 
346594783dSgdamore #include <sys/cdefs.h>
35*882ce77cSthorpej __KERNEL_RCSID(0, "$NetBSD: edid.c,v 1.18 2022/09/25 21:27:39 thorpej Exp $");
366594783dSgdamore 
3708b20cdaSmlelstv #ifdef _KERNEL
386594783dSgdamore #include <sys/param.h>
396594783dSgdamore #include <sys/systm.h>
406594783dSgdamore #include <sys/device.h>
416594783dSgdamore #include <sys/kernel.h>
4208b20cdaSmlelstv #else
4308b20cdaSmlelstv #include <stdlib.h>
4408b20cdaSmlelstv #include <inttypes.h>
4508b20cdaSmlelstv #include <string.h>
4608b20cdaSmlelstv #include <errno.h>
4708b20cdaSmlelstv #include <stdio.h>
4808b20cdaSmlelstv #endif
4908b20cdaSmlelstv 
506594783dSgdamore #include <dev/videomode/videomode.h>
516594783dSgdamore #include <dev/videomode/ediddevs.h>
526594783dSgdamore #include <dev/videomode/edidreg.h>
536594783dSgdamore #include <dev/videomode/edidvar.h>
546594783dSgdamore #include <dev/videomode/vesagtf.h>
556594783dSgdamore 
566594783dSgdamore #define	EDIDVERBOSE	1
576594783dSgdamore #define	DIVIDE(x,y)	(((x) + ((y) / 2)) / (y))
586594783dSgdamore 
598856d368Sjdc /* These are reversed established timing order */
606594783dSgdamore static const char *_edid_modes[] =  {
616594783dSgdamore 	"1280x1024x75",
62d92bd53fSgdamore 	"1024x768x75",
63d92bd53fSgdamore 	"1024x768x70",
64d92bd53fSgdamore 	"1024x768x60",
65d92bd53fSgdamore 	"1024x768x87i",
668856d368Sjdc 	"832x624x74",	/* rounding error, 74.55 Hz aka "832x624x75" */
67d92bd53fSgdamore 	"800x600x75",
68d92bd53fSgdamore 	"800x600x72",
69d92bd53fSgdamore 	"800x600x60",
70d92bd53fSgdamore 	"800x600x56",
71d92bd53fSgdamore 	"640x480x75",
72d92bd53fSgdamore 	"640x480x72",
73d92bd53fSgdamore 	"640x480x67",
74d92bd53fSgdamore 	"640x480x60",
758856d368Sjdc 	"720x400x87",	/* rounding error, 87.85 Hz aka "720x400x88" */
768856d368Sjdc 	"720x400x70",
776594783dSgdamore };
786594783dSgdamore 
796594783dSgdamore #ifdef	EDIDVERBOSE
806594783dSgdamore struct edid_vendor {
816594783dSgdamore 	const char	*vendor;
826594783dSgdamore 	const char	*name;
836594783dSgdamore };
846594783dSgdamore 
856594783dSgdamore struct edid_product {
866594783dSgdamore 	const char	*vendor;
876594783dSgdamore 	uint16_t	product;
886594783dSgdamore 	const char	*name;
896594783dSgdamore };
906594783dSgdamore 
916594783dSgdamore #include <dev/videomode/ediddevs_data.h>
926594783dSgdamore #endif	/* EDIDVERBOSE */
936594783dSgdamore 
946594783dSgdamore static const char *
edid_findvendor(const char * vendor)956594783dSgdamore edid_findvendor(const char *vendor)
966594783dSgdamore {
976594783dSgdamore #ifdef	EDIDVERBOSE
986594783dSgdamore 	int	n;
996594783dSgdamore 
1006594783dSgdamore 	for (n = 0; n < edid_nvendors; n++)
1016594783dSgdamore 		if (memcmp(edid_vendors[n].vendor, vendor, 3) == 0)
102cd9ad85fSchristos 			return edid_vendors[n].name;
1036594783dSgdamore #endif
1046594783dSgdamore 	return NULL;
1056594783dSgdamore }
1066594783dSgdamore 
1076594783dSgdamore static const char *
edid_findproduct(const char * vendor,uint16_t product)1086594783dSgdamore edid_findproduct(const char *vendor, uint16_t product)
1096594783dSgdamore {
1106594783dSgdamore #ifdef	EDIDVERBOSE
1116594783dSgdamore 	int	n;
1126594783dSgdamore 
113c03477f1Stsutsui 	for (n = 0; n < edid_nproducts; n++)
114cd9ad85fSchristos 		if (edid_products[n].product == product &&
115cd9ad85fSchristos 		    memcmp(edid_products[n].vendor, vendor, 3) == 0)
1168dffb314Schristos 			return edid_products[n].name;
1176594783dSgdamore #endif	/* EDIDVERBOSE */
1186594783dSgdamore 	return NULL;
1196594783dSgdamore 
1206594783dSgdamore }
1216594783dSgdamore 
1226594783dSgdamore static void
edid_strchomp(char * ptr)1236594783dSgdamore edid_strchomp(char *ptr)
1246594783dSgdamore {
1256594783dSgdamore 	for (;;) {
1266594783dSgdamore 		switch (*ptr) {
127cd9ad85fSchristos 		case '\0':
1286594783dSgdamore 			return;
1296594783dSgdamore 		case '\r':
1306594783dSgdamore 		case '\n':
131cd9ad85fSchristos 			*ptr = '\0';
1326594783dSgdamore 			return;
1336594783dSgdamore 		}
1346594783dSgdamore 		ptr++;
1356594783dSgdamore 	}
1366594783dSgdamore }
1376594783dSgdamore 
1386594783dSgdamore int
edid_is_valid(uint8_t * d)1396594783dSgdamore edid_is_valid(uint8_t *d)
1406594783dSgdamore {
1416594783dSgdamore 	int sum = 0, i;
1426594783dSgdamore 	uint8_t sig[8] = EDID_SIGNATURE;
1436594783dSgdamore 
1446594783dSgdamore 	if (memcmp(d, sig, 8) != 0)
1456594783dSgdamore 		return EINVAL;
1466594783dSgdamore 
1476594783dSgdamore 	for (i = 0; i < 128; i++)
1486594783dSgdamore 		sum += d[i];
1496594783dSgdamore 	if ((sum & 0xff) != 0)
1506594783dSgdamore 		return EINVAL;
1516594783dSgdamore 
1526594783dSgdamore 	return 0;
1536594783dSgdamore }
1546594783dSgdamore 
1556594783dSgdamore void
edid_print(struct edid_info * edid)1566594783dSgdamore edid_print(struct edid_info *edid)
1576594783dSgdamore {
1586594783dSgdamore 	int	i;
1596594783dSgdamore 
1606594783dSgdamore 	if (edid == NULL)
1616594783dSgdamore 		return;
1626594783dSgdamore 	printf("Vendor: [%s] %s\n", edid->edid_vendor, edid->edid_vendorname);
1636594783dSgdamore 	printf("Product: [%04X] %s\n", edid->edid_product,
1646594783dSgdamore 	    edid->edid_productname);
1656594783dSgdamore 	printf("Serial number: %s\n", edid->edid_serial);
1666594783dSgdamore 	printf("Manufactured %d Week %d\n",
1676594783dSgdamore 	    edid->edid_year, edid->edid_week);
1686594783dSgdamore 	printf("EDID Version %d.%d\n", edid->edid_version,
1696594783dSgdamore 	    edid->edid_revision);
1706594783dSgdamore 	printf("EDID Comment: %s\n", edid->edid_comment);
1716594783dSgdamore 
1726594783dSgdamore 	printf("Video Input: %x\n", edid->edid_video_input);
1736594783dSgdamore 	if (edid->edid_video_input & EDID_VIDEO_INPUT_DIGITAL) {
1746594783dSgdamore 		printf("\tDigital");
1756594783dSgdamore 		if (edid->edid_video_input & EDID_VIDEO_INPUT_DFP1_COMPAT)
1766594783dSgdamore 			printf(" (DFP 1.x compatible)");
1776594783dSgdamore 		printf("\n");
1786594783dSgdamore 	} else {
1796594783dSgdamore 		printf("\tAnalog\n");
1806594783dSgdamore 		switch (EDID_VIDEO_INPUT_LEVEL(edid->edid_video_input)) {
1816594783dSgdamore 		case 0:
1826594783dSgdamore 			printf("\t-0.7, 0.3V\n");
1836594783dSgdamore 			break;
1846594783dSgdamore 		case 1:
1856594783dSgdamore 			printf("\t-0.714, 0.286V\n");
1866594783dSgdamore 			break;
1876594783dSgdamore 		case 2:
1886594783dSgdamore 			printf("\t-1.0, 0.4V\n");
1896594783dSgdamore 			break;
1906594783dSgdamore 		case 3:
1916594783dSgdamore 			printf("\t-0.7, 0.0V\n");
1926594783dSgdamore 			break;
1936594783dSgdamore 		}
1946594783dSgdamore 		if (edid->edid_video_input & EDID_VIDEO_INPUT_BLANK_TO_BLACK)
1956594783dSgdamore 			printf("\tBlank-to-black setup\n");
1966594783dSgdamore 		if (edid->edid_video_input & EDID_VIDEO_INPUT_SEPARATE_SYNCS)
197f84252b4Sandvar 			printf("\tSeparate syncs\n");
1986594783dSgdamore 		if (edid->edid_video_input & EDID_VIDEO_INPUT_COMPOSITE_SYNC)
1996594783dSgdamore 			printf("\tComposite sync\n");
2006594783dSgdamore 		if (edid->edid_video_input & EDID_VIDEO_INPUT_SYNC_ON_GRN)
2016594783dSgdamore 			printf("\tSync on green\n");
2026594783dSgdamore 		if (edid->edid_video_input & EDID_VIDEO_INPUT_SERRATION)
2036594783dSgdamore 			printf("\tSerration vsync\n");
2046594783dSgdamore 	}
2056594783dSgdamore 
2066594783dSgdamore 	printf("Gamma: %d.%02d\n",
2076594783dSgdamore 	    edid->edid_gamma / 100, edid->edid_gamma % 100);
2086594783dSgdamore 
2096594783dSgdamore 	printf("Max Size: %d cm x %d cm\n",
2106594783dSgdamore 	    edid->edid_max_hsize, edid->edid_max_vsize);
2116594783dSgdamore 
2126594783dSgdamore 	printf("Features: %x\n", edid->edid_features);
2136594783dSgdamore 	if (edid->edid_features & EDID_FEATURES_STANDBY)
2146594783dSgdamore 		printf("\tDPMS standby\n");
2156594783dSgdamore 	if (edid->edid_features & EDID_FEATURES_SUSPEND)
2166594783dSgdamore 		printf("\tDPMS suspend\n");
2176594783dSgdamore 	if (edid->edid_features & EDID_FEATURES_ACTIVE_OFF)
2186594783dSgdamore 		printf("\tDPMS active-off\n");
2196594783dSgdamore 	switch (EDID_FEATURES_DISP_TYPE(edid->edid_features)) {
2206594783dSgdamore 	case EDID_FEATURES_DISP_TYPE_MONO:
2216594783dSgdamore 		printf("\tMonochrome\n");
2226594783dSgdamore 		break;
2236594783dSgdamore 	case EDID_FEATURES_DISP_TYPE_RGB:
2246594783dSgdamore 		printf("\tRGB\n");
2256594783dSgdamore 		break;
2266594783dSgdamore 	case EDID_FEATURES_DISP_TYPE_NON_RGB:
2276594783dSgdamore 		printf("\tMulticolor\n");
2286594783dSgdamore 		break;
2296594783dSgdamore 	case EDID_FEATURES_DISP_TYPE_UNDEFINED:
2306594783dSgdamore 		printf("\tUndefined monitor type\n");
2316594783dSgdamore 		break;
2326594783dSgdamore 	}
2336594783dSgdamore 	if (edid->edid_features & EDID_FEATURES_STD_COLOR)
2346594783dSgdamore 		printf("\tStandard color space\n");
2356594783dSgdamore 	if (edid->edid_features & EDID_FEATURES_PREFERRED_TIMING)
2366594783dSgdamore 		printf("\tPreferred timing\n");
2376594783dSgdamore 	if (edid->edid_features & EDID_FEATURES_DEFAULT_GTF)
2386594783dSgdamore 		printf("\tDefault GTF supported\n");
2396594783dSgdamore 
2406594783dSgdamore 	printf("Chroma Info:\n");
2416594783dSgdamore 	printf("\tRed X: 0.%03d\n", edid->edid_chroma.ec_redx);
2426594783dSgdamore 	printf("\tRed Y: 0.%03d\n", edid->edid_chroma.ec_redy);
2436594783dSgdamore 	printf("\tGrn X: 0.%03d\n", edid->edid_chroma.ec_greenx);
2446594783dSgdamore 	printf("\tGrn Y: 0.%03d\n", edid->edid_chroma.ec_greeny);
2456594783dSgdamore 	printf("\tBlu X: 0.%03d\n", edid->edid_chroma.ec_bluex);
2466594783dSgdamore 	printf("\tBlu Y: 0.%03d\n", edid->edid_chroma.ec_bluey);
2476594783dSgdamore 	printf("\tWht X: 0.%03d\n", edid->edid_chroma.ec_whitex);
2486594783dSgdamore 	printf("\tWht Y: 0.%03d\n", edid->edid_chroma.ec_whitey);
2496594783dSgdamore 
2506594783dSgdamore 	if (edid->edid_have_range) {
2516594783dSgdamore 		printf("Range:\n");
2526594783dSgdamore 		printf("\tHorizontal: %d - %d kHz\n",
2536594783dSgdamore 		    edid->edid_range.er_min_hfreq,
2546594783dSgdamore 		    edid->edid_range.er_max_hfreq);
2556594783dSgdamore 		printf("\tVertical: %d - %d Hz\n",
2566594783dSgdamore 		    edid->edid_range.er_min_vfreq,
2576594783dSgdamore 		    edid->edid_range.er_max_vfreq);
2586594783dSgdamore 		printf("\tMax Dot Clock: %d MHz\n",
2596594783dSgdamore 		    edid->edid_range.er_max_clock);
2606594783dSgdamore 		if (edid->edid_range.er_have_gtf2) {
2616594783dSgdamore 			printf("\tGTF2 hfreq: %d\n",
2626594783dSgdamore 			    edid->edid_range.er_gtf2_hfreq);
2636594783dSgdamore 			printf("\tGTF2 C: %d\n", edid->edid_range.er_gtf2_c);
2646594783dSgdamore 			printf("\tGTF2 M: %d\n", edid->edid_range.er_gtf2_m);
2656594783dSgdamore 			printf("\tGTF2 J: %d\n", edid->edid_range.er_gtf2_j);
2666594783dSgdamore 			printf("\tGTF2 K: %d\n", edid->edid_range.er_gtf2_k);
2676594783dSgdamore 		}
2686594783dSgdamore 	}
2696594783dSgdamore 	printf("Video modes:\n");
2706594783dSgdamore 	for (i = 0; i < edid->edid_nmodes; i++) {
27146869594Sjdc 		printf("\t%dx%d @ %dHz",
2726594783dSgdamore 		    edid->edid_modes[i].hdisplay,
2736594783dSgdamore 		    edid->edid_modes[i].vdisplay,
2746594783dSgdamore 		    DIVIDE(DIVIDE(edid->edid_modes[i].dot_clock * 1000,
275cd9ad85fSchristos 		    edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal));
27646869594Sjdc 		printf(" (%d %d %d %d %d %d %d",
27746869594Sjdc 		    edid->edid_modes[i].dot_clock,
27846869594Sjdc 		    edid->edid_modes[i].hsync_start,
27946869594Sjdc 		    edid->edid_modes[i].hsync_end,
28046869594Sjdc 		    edid->edid_modes[i].htotal,
28146869594Sjdc 		    edid->edid_modes[i].vsync_start,
28246869594Sjdc 		    edid->edid_modes[i].vsync_end,
28346869594Sjdc 		    edid->edid_modes[i].vtotal);
28446869594Sjdc 		printf(" %s%sH %s%sV)\n",
28546869594Sjdc 		    edid->edid_modes[i].flags & VID_PHSYNC ? "+" : "",
28646869594Sjdc 		    edid->edid_modes[i].flags & VID_NHSYNC ? "-" : "",
28746869594Sjdc 		    edid->edid_modes[i].flags & VID_PVSYNC ? "+" : "",
28846869594Sjdc 		    edid->edid_modes[i].flags & VID_NVSYNC ? "-" : "");
2896594783dSgdamore 	}
2906594783dSgdamore 	if (edid->edid_preferred_mode)
2916594783dSgdamore 		printf("Preferred mode: %dx%d @ %dHz\n",
2926594783dSgdamore 		    edid->edid_preferred_mode->hdisplay,
2936594783dSgdamore 		    edid->edid_preferred_mode->vdisplay,
2946594783dSgdamore 		    DIVIDE(DIVIDE(edid->edid_preferred_mode->dot_clock * 1000,
2956594783dSgdamore 		    edid->edid_preferred_mode->htotal),
2966594783dSgdamore 		    edid->edid_preferred_mode->vtotal));
2973f50a23cSjmcneill 
2983f50a23cSjmcneill 	printf("Number of extension blocks: %d\n", edid->edid_ext_block_count);
2996594783dSgdamore }
3006594783dSgdamore 
3016594783dSgdamore static const struct videomode *
edid_mode_lookup_list(const char * name)3026594783dSgdamore edid_mode_lookup_list(const char *name)
3036594783dSgdamore {
3046594783dSgdamore 	int	i;
3056594783dSgdamore 
3066594783dSgdamore 	for (i = 0; i < videomode_count; i++)
3076594783dSgdamore 		if (strcmp(name, videomode_list[i].name) == 0)
3086594783dSgdamore 			return &videomode_list[i];
3096594783dSgdamore 	return NULL;
3106594783dSgdamore }
3116594783dSgdamore 
31225950f6eSjdc static struct videomode *
edid_search_mode(struct edid_info * edid,const struct videomode * mode)31325950f6eSjdc edid_search_mode(struct edid_info *edid, const struct videomode *mode)
31425950f6eSjdc {
31525950f6eSjdc 	int	refresh, i;
31625950f6eSjdc 
31725950f6eSjdc 	refresh = DIVIDE(DIVIDE(mode->dot_clock * 1000,
31825950f6eSjdc 	    mode->htotal), mode->vtotal);
31925950f6eSjdc 	for (i = 0; i < edid->edid_nmodes; i++) {
32025950f6eSjdc 		if (mode->hdisplay == edid->edid_modes[i].hdisplay &&
32125950f6eSjdc 		    mode->vdisplay == edid->edid_modes[i].vdisplay &&
32225950f6eSjdc 		    refresh == DIVIDE(DIVIDE(
32325950f6eSjdc 		    edid->edid_modes[i].dot_clock * 1000,
324cd9ad85fSchristos 		    edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal)) {
325cd9ad85fSchristos 			return &edid->edid_modes[i];
32625950f6eSjdc 		}
32725950f6eSjdc 	}
32825950f6eSjdc 	return NULL;
32925950f6eSjdc }
33025950f6eSjdc 
3316594783dSgdamore static int
edid_std_timing(uint8_t * data,struct videomode * vmp)3326594783dSgdamore edid_std_timing(uint8_t *data, struct videomode *vmp)
3336594783dSgdamore {
3346594783dSgdamore 	unsigned			x, y, f;
3356594783dSgdamore 	const struct videomode		*lookup;
3366594783dSgdamore 	char				name[80];
3376594783dSgdamore 
3386594783dSgdamore 	if ((data[0] == 1 && data[1] == 1) ||
3396594783dSgdamore 	    (data[0] == 0 && data[1] == 0) ||
3406594783dSgdamore 	    (data[0] == 0x20 && data[1] == 0x20))
3416594783dSgdamore 		return 0;
3426594783dSgdamore 
3436594783dSgdamore 	x = EDID_STD_TIMING_HRES(data);
3446594783dSgdamore 	switch (EDID_STD_TIMING_RATIO(data)) {
3456594783dSgdamore 	case EDID_STD_TIMING_RATIO_16_10:
3466594783dSgdamore 		y = x * 10 / 16;
3476594783dSgdamore 		break;
3486594783dSgdamore 	case EDID_STD_TIMING_RATIO_4_3:
3496594783dSgdamore 		y = x * 3 / 4;
3506594783dSgdamore 		break;
3516594783dSgdamore 	case EDID_STD_TIMING_RATIO_5_4:
3526594783dSgdamore 		y = x * 4 / 5;
3536594783dSgdamore 		break;
3546594783dSgdamore 	case EDID_STD_TIMING_RATIO_16_9:
3556594783dSgdamore 	default:
3566594783dSgdamore 		y = x * 9 / 16;
3576594783dSgdamore 		break;
3586594783dSgdamore 	}
3596594783dSgdamore 	f = EDID_STD_TIMING_VFREQ(data);
3606594783dSgdamore 
3616594783dSgdamore 	/* first try to lookup the mode as a DMT timing */
3626594783dSgdamore 	snprintf(name, sizeof(name), "%dx%dx%d", x, y, f);
3636594783dSgdamore 	if ((lookup = edid_mode_lookup_list(name)) != NULL) {
3646594783dSgdamore 		*vmp = *lookup;
365cd9ad85fSchristos 	} else {
3666594783dSgdamore 		/* failing that, calculate it using gtf */
3676594783dSgdamore 		/*
3686594783dSgdamore 		 * Hmm. I'm not using alternate GTF timings, which
3696594783dSgdamore 		 * could, in theory, be present.
3706594783dSgdamore 		 */
3716594783dSgdamore 		vesagtf_mode(x, y, f, vmp);
3726594783dSgdamore 	}
3736594783dSgdamore 	return 1;
3746594783dSgdamore }
3756594783dSgdamore 
3766594783dSgdamore static int
edid_det_timing(uint8_t * data,struct videomode * vmp)3776594783dSgdamore edid_det_timing(uint8_t *data, struct videomode *vmp)
3786594783dSgdamore {
3796594783dSgdamore 	unsigned	hactive, hblank, hsyncwid, hsyncoff;
3806594783dSgdamore 	unsigned	vactive, vblank, vsyncwid, vsyncoff;
3816594783dSgdamore 	uint8_t		flags;
3826594783dSgdamore 
3836594783dSgdamore 	flags = EDID_DET_TIMING_FLAGS(data);
3846594783dSgdamore 
3856594783dSgdamore 	/* we don't support stereo modes (for now) */
3866594783dSgdamore 	if (flags & (EDID_DET_TIMING_FLAG_STEREO |
38746869594Sjdc 		EDID_DET_TIMING_FLAG_STEREO_MODE))
3886594783dSgdamore 		return 0;
3896594783dSgdamore 
3906594783dSgdamore 	vmp->dot_clock = EDID_DET_TIMING_DOT_CLOCK(data) / 1000;
3916594783dSgdamore 
3926594783dSgdamore 	hactive = EDID_DET_TIMING_HACTIVE(data);
3936594783dSgdamore 	hblank = EDID_DET_TIMING_HBLANK(data);
3946594783dSgdamore 	hsyncwid = EDID_DET_TIMING_HSYNC_WIDTH(data);
3956594783dSgdamore 	hsyncoff = EDID_DET_TIMING_HSYNC_OFFSET(data);
3966594783dSgdamore 
3976594783dSgdamore 	vactive = EDID_DET_TIMING_VACTIVE(data);
3986594783dSgdamore 	vblank = EDID_DET_TIMING_VBLANK(data);
3996594783dSgdamore 	vsyncwid = EDID_DET_TIMING_VSYNC_WIDTH(data);
4006594783dSgdamore 	vsyncoff = EDID_DET_TIMING_VSYNC_OFFSET(data);
4016594783dSgdamore 
40225950f6eSjdc 	/* Borders are contained within the blank areas. */
4036594783dSgdamore 
4046594783dSgdamore 	vmp->hdisplay = hactive;
4056594783dSgdamore 	vmp->htotal = hactive + hblank;
4066594783dSgdamore 	vmp->hsync_start = hactive + hsyncoff;
4076594783dSgdamore 	vmp->hsync_end = vmp->hsync_start + hsyncwid;
4086594783dSgdamore 
4096594783dSgdamore 	vmp->vdisplay = vactive;
4106594783dSgdamore 	vmp->vtotal = vactive + vblank;
4116594783dSgdamore 	vmp->vsync_start = vactive + vsyncoff;
4126594783dSgdamore 	vmp->vsync_end = vmp->vsync_start + vsyncwid;
4136594783dSgdamore 
4146594783dSgdamore 	vmp->flags = 0;
4156594783dSgdamore 
4166594783dSgdamore 	if (flags & EDID_DET_TIMING_FLAG_INTERLACE)
4176594783dSgdamore 		vmp->flags |= VID_INTERLACE;
4186594783dSgdamore 	if (flags & EDID_DET_TIMING_FLAG_HSYNC_POSITIVE)
4196594783dSgdamore 		vmp->flags |= VID_PHSYNC;
4206594783dSgdamore 	else
4216594783dSgdamore 		vmp->flags |= VID_NHSYNC;
4226594783dSgdamore 
4236594783dSgdamore 	if (flags & EDID_DET_TIMING_FLAG_VSYNC_POSITIVE)
4246594783dSgdamore 		vmp->flags |= VID_PVSYNC;
4256594783dSgdamore 	else
4266594783dSgdamore 		vmp->flags |= VID_NVSYNC;
4276594783dSgdamore 
4286594783dSgdamore 	return 1;
4296594783dSgdamore }
4306594783dSgdamore 
bump_preferred_mode(struct edid_info * edid,struct videomode * m)431dbccb460Smacallan static void bump_preferred_mode(struct edid_info *edid, struct videomode *m)
432dbccb460Smacallan {
433dbccb460Smacallan 	/*
434dbccb460Smacallan 	 * XXX
435dbccb460Smacallan 	 * Iiyama 4800 series monitors may have their native resolution in the
436dbccb460Smacallan 	 * 2nd detailed timing descriptor instead of the 1st. Try to detect
437dbccb460Smacallan 	 * that here and pick the native mode anyway.
438dbccb460Smacallan 	 */
439dbccb460Smacallan 	if (edid->edid_preferred_mode == NULL) {
440dbccb460Smacallan 		edid->edid_preferred_mode = m;
44108b20cdaSmlelstv 	} else if ((strncmp((char*)edid->edid_vendor, "IVM", 3) == 0) &&
442dbccb460Smacallan 	           (edid->edid_product == 0x4800) &&
443dbccb460Smacallan 	           (edid->edid_preferred_mode->dot_clock < m->dot_clock))
444dbccb460Smacallan 		edid->edid_preferred_mode = m;
445dbccb460Smacallan }
446dbccb460Smacallan 
4476594783dSgdamore static void
edid_block(struct edid_info * edid,uint8_t * data)4486594783dSgdamore edid_block(struct edid_info *edid, uint8_t *data)
4496594783dSgdamore {
4506594783dSgdamore 	int			i;
45125950f6eSjdc 	struct videomode	mode, *exist_mode;
4526594783dSgdamore 
4536594783dSgdamore 	if (EDID_BLOCK_IS_DET_TIMING(data)) {
4548dffb314Schristos 		if (!edid_det_timing(data, &mode))
455cd9ad85fSchristos 			return;
45625950f6eSjdc 		/* Does this mode already exist? */
45725950f6eSjdc 		exist_mode = edid_search_mode(edid, &mode);
45825950f6eSjdc 		if (exist_mode != NULL) {
459cd9ad85fSchristos 			*exist_mode = mode;
460dbccb460Smacallan 			bump_preferred_mode(edid, exist_mode);
46125950f6eSjdc 		} else {
4626594783dSgdamore 			edid->edid_modes[edid->edid_nmodes] = mode;
463dbccb460Smacallan 			bump_preferred_mode(edid,
464dbccb460Smacallan 			    &edid->edid_modes[edid->edid_nmodes]);
4656594783dSgdamore 			edid->edid_nmodes++;
4666594783dSgdamore 		}
4676594783dSgdamore 		return;
4686594783dSgdamore 	}
4696594783dSgdamore 
4706594783dSgdamore 	switch (EDID_BLOCK_TYPE(data)) {
4716594783dSgdamore 	case EDID_DESC_BLOCK_TYPE_SERIAL:
472cd9ad85fSchristos 		memcpy(edid->edid_serial, data + EDID_DESC_ASCII_DATA_OFFSET,
4736594783dSgdamore 		    EDID_DESC_ASCII_DATA_LEN);
4746594783dSgdamore 		edid->edid_serial[sizeof(edid->edid_serial) - 1] = 0;
4756594783dSgdamore 		break;
4766594783dSgdamore 
4776594783dSgdamore 	case EDID_DESC_BLOCK_TYPE_ASCII:
47887841982Smaxv 		memset(edid->edid_comment, 0, sizeof(edid->edid_comment));
479cd9ad85fSchristos 		memcpy(edid->edid_comment, data + EDID_DESC_ASCII_DATA_OFFSET,
4806594783dSgdamore 		    EDID_DESC_ASCII_DATA_LEN);
4816594783dSgdamore 		edid->edid_comment[sizeof(edid->edid_comment) - 1] = 0;
4826594783dSgdamore 		break;
4836594783dSgdamore 
4846594783dSgdamore 	case EDID_DESC_BLOCK_TYPE_RANGE:
4856594783dSgdamore 		edid->edid_have_range = 1;
486cd9ad85fSchristos 		edid->edid_range.er_min_vfreq =	EDID_DESC_RANGE_MIN_VFREQ(data);
487cd9ad85fSchristos 		edid->edid_range.er_max_vfreq =	EDID_DESC_RANGE_MAX_VFREQ(data);
488cd9ad85fSchristos 		edid->edid_range.er_min_hfreq =	EDID_DESC_RANGE_MIN_HFREQ(data);
489cd9ad85fSchristos 		edid->edid_range.er_max_hfreq =	EDID_DESC_RANGE_MAX_HFREQ(data);
490cd9ad85fSchristos 		edid->edid_range.er_max_clock = EDID_DESC_RANGE_MAX_CLOCK(data);
491cd9ad85fSchristos 		if (!EDID_DESC_RANGE_HAVE_GTF2(data))
492cd9ad85fSchristos 			break;
4936594783dSgdamore 		edid->edid_range.er_have_gtf2 = 1;
4946594783dSgdamore 		edid->edid_range.er_gtf2_hfreq =
4956594783dSgdamore 		    EDID_DESC_RANGE_GTF2_HFREQ(data);
496cd9ad85fSchristos 		edid->edid_range.er_gtf2_c = EDID_DESC_RANGE_GTF2_C(data);
497cd9ad85fSchristos 		edid->edid_range.er_gtf2_m = EDID_DESC_RANGE_GTF2_M(data);
498cd9ad85fSchristos 		edid->edid_range.er_gtf2_j = EDID_DESC_RANGE_GTF2_J(data);
499cd9ad85fSchristos 		edid->edid_range.er_gtf2_k = EDID_DESC_RANGE_GTF2_K(data);
5006594783dSgdamore 		break;
5016594783dSgdamore 
5026594783dSgdamore 	case EDID_DESC_BLOCK_TYPE_NAME:
5036594783dSgdamore 		/* copy the product name into place */
5046594783dSgdamore 		memcpy(edid->edid_productname,
5056594783dSgdamore 		    data + EDID_DESC_ASCII_DATA_OFFSET,
5066594783dSgdamore 		    EDID_DESC_ASCII_DATA_LEN);
5076594783dSgdamore 		break;
5086594783dSgdamore 
5096594783dSgdamore 	case EDID_DESC_BLOCK_TYPE_STD_TIMING:
5106594783dSgdamore 		data += EDID_DESC_STD_TIMING_START;
5116594783dSgdamore 		for (i = 0; i < EDID_DESC_STD_TIMING_COUNT; i++) {
5126594783dSgdamore 			if (edid_std_timing(data, &mode)) {
51325950f6eSjdc 				/* Does this mode already exist? */
51425950f6eSjdc 				exist_mode = edid_search_mode(edid, &mode);
51525950f6eSjdc 				if (exist_mode == NULL) {
51625950f6eSjdc 					edid->edid_modes[edid->edid_nmodes] =
51725950f6eSjdc 					    mode;
5186594783dSgdamore 					edid->edid_nmodes++;
5196594783dSgdamore 				}
52025950f6eSjdc 			}
5216594783dSgdamore 			data += 2;
5226594783dSgdamore 		}
5236594783dSgdamore 		break;
5246594783dSgdamore 
5256594783dSgdamore 	case EDID_DESC_BLOCK_TYPE_COLOR_POINT:
5266594783dSgdamore 		/* XXX: not implemented yet */
5276594783dSgdamore 		break;
5286594783dSgdamore 	}
5296594783dSgdamore }
5306594783dSgdamore 
5316594783dSgdamore /*
5326594783dSgdamore  * Gets EDID version in BCD, e.g. EDID v1.3  returned as 0x0103
5336594783dSgdamore  */
53480044be1Sgdamore int
edid_parse(uint8_t * data,struct edid_info * edid)53580044be1Sgdamore edid_parse(uint8_t *data, struct edid_info *edid)
5366594783dSgdamore {
5376594783dSgdamore 	uint16_t		manfid, estmodes;
5386594783dSgdamore 	const struct videomode	*vmp;
5396594783dSgdamore 	int			i;
5406594783dSgdamore 	const char		*name;
541f658e972Smacallan 	int max_dotclock = 0;
542f658e972Smacallan 	int mhz;
5436594783dSgdamore 
5446594783dSgdamore 	if (edid_is_valid(data) != 0)
54580044be1Sgdamore 		return -1;
5466594783dSgdamore 
5476594783dSgdamore 	/* get product identification */
5486594783dSgdamore 	manfid = EDID_VENDOR_ID(data);
5496594783dSgdamore 	edid->edid_vendor[0] = EDID_MANFID_0(manfid);
5506594783dSgdamore 	edid->edid_vendor[1] = EDID_MANFID_1(manfid);
5516594783dSgdamore 	edid->edid_vendor[2] = EDID_MANFID_2(manfid);
5526594783dSgdamore 	edid->edid_vendor[3] = 0;	/* null terminate for convenience */
5536594783dSgdamore 
5546594783dSgdamore 	edid->edid_product = data[EDID_OFFSET_PRODUCT_ID] +
5556594783dSgdamore 	    (data[EDID_OFFSET_PRODUCT_ID + 1] << 8);
5566594783dSgdamore 
55708b20cdaSmlelstv 	name = edid_findvendor((char *)edid->edid_vendor);
558cd9ad85fSchristos 	if (name != NULL)
559cd9ad85fSchristos 		strlcpy(edid->edid_vendorname, name,
560cd9ad85fSchristos 		    sizeof(edid->edid_vendorname));
561cd9ad85fSchristos 	else
562cd9ad85fSchristos 		edid->edid_vendorname[0] = '\0';
5636594783dSgdamore 
56408b20cdaSmlelstv 	name = edid_findproduct((char *)edid->edid_vendor, edid->edid_product);
565cd9ad85fSchristos 	if (name != NULL)
566cd9ad85fSchristos 		strlcpy(edid->edid_productname, name,
567cd9ad85fSchristos 		    sizeof(edid->edid_productname));
568cd9ad85fSchristos 	else
569cd9ad85fSchristos 	    edid->edid_productname[0] = '\0';
5706594783dSgdamore 
5716594783dSgdamore 	snprintf(edid->edid_serial, sizeof(edid->edid_serial), "%08x",
5726594783dSgdamore 	    EDID_SERIAL_NUMBER(data));
5736594783dSgdamore 
57487841982Smaxv 	edid->edid_comment[0] = '\0';
57587841982Smaxv 
5766594783dSgdamore 	edid->edid_week = EDID_WEEK(data);
5776594783dSgdamore 	edid->edid_year = EDID_YEAR(data);
5786594783dSgdamore 
5796594783dSgdamore 	/* get edid revision */
5806594783dSgdamore 	edid->edid_version = EDID_VERSION(data);
5816594783dSgdamore 	edid->edid_revision = EDID_REVISION(data);
5826594783dSgdamore 
5836594783dSgdamore 	edid->edid_video_input = EDID_VIDEO_INPUT(data);
5846594783dSgdamore 	edid->edid_max_hsize = EDID_MAX_HSIZE(data);
5856594783dSgdamore 	edid->edid_max_vsize = EDID_MAX_VSIZE(data);
5866594783dSgdamore 
5876594783dSgdamore 	edid->edid_gamma = EDID_GAMMA(data);
5886594783dSgdamore 	edid->edid_features = EDID_FEATURES(data);
5896594783dSgdamore 
5906594783dSgdamore 	edid->edid_chroma.ec_redx = EDID_CHROMA_REDX(data);
5916594783dSgdamore 	edid->edid_chroma.ec_redy = EDID_CHROMA_REDX(data);
5926594783dSgdamore 	edid->edid_chroma.ec_greenx = EDID_CHROMA_GREENX(data);
5936594783dSgdamore 	edid->edid_chroma.ec_greeny = EDID_CHROMA_GREENY(data);
5946594783dSgdamore 	edid->edid_chroma.ec_bluex = EDID_CHROMA_BLUEX(data);
5956594783dSgdamore 	edid->edid_chroma.ec_bluey = EDID_CHROMA_BLUEY(data);
5966594783dSgdamore 	edid->edid_chroma.ec_whitex = EDID_CHROMA_WHITEX(data);
5976594783dSgdamore 	edid->edid_chroma.ec_whitey = EDID_CHROMA_WHITEY(data);
5986594783dSgdamore 
5993f50a23cSjmcneill 	edid->edid_ext_block_count = EDID_EXT_BLOCK_COUNT(data);
6003f50a23cSjmcneill 
6016594783dSgdamore 	/* lookup established modes */
6029975dc8fSmacallan 	edid->edid_nmodes = 0;
603f658e972Smacallan 	edid->edid_preferred_mode = NULL;
6046594783dSgdamore 	estmodes = EDID_EST_TIMING(data);
6058856d368Sjdc 	/* Iterate in esztablished timing order */
6068856d368Sjdc 	for (i = 15; i >= 0; i--) {
6076594783dSgdamore 		if (estmodes & (1 << i)) {
6086594783dSgdamore 			vmp = edid_mode_lookup_list(_edid_modes[i]);
6096594783dSgdamore 			if (vmp != NULL) {
6106594783dSgdamore 				edid->edid_modes[edid->edid_nmodes] = *vmp;
6116594783dSgdamore 				edid->edid_nmodes++;
6126594783dSgdamore 			}
613f658e972Smacallan #ifdef DIAGNOSTIC
614f658e972Smacallan 			  else
615f658e972Smacallan 				printf("no data for est. mode %s\n",
616f658e972Smacallan 				    _edid_modes[i]);
617f658e972Smacallan #endif
6186594783dSgdamore 		}
6196594783dSgdamore 	}
6206594783dSgdamore 
6216594783dSgdamore 	/* do standard timing section */
6226594783dSgdamore 	for (i = 0; i < EDID_STD_TIMING_COUNT; i++) {
62325950f6eSjdc 		struct videomode	mode, *exist_mode;
6246594783dSgdamore 		if (edid_std_timing(data + EDID_OFFSET_STD_TIMING + i * 2,
6256594783dSgdamore 			&mode)) {
62625950f6eSjdc 			/* Does this mode already exist? */
62725950f6eSjdc 			exist_mode = edid_search_mode(edid, &mode);
62825950f6eSjdc 			if (exist_mode == NULL) {
6296594783dSgdamore 				edid->edid_modes[edid->edid_nmodes] = mode;
6306594783dSgdamore 				edid->edid_nmodes++;
6316594783dSgdamore 			}
6326594783dSgdamore 		}
63325950f6eSjdc 	}
6346594783dSgdamore 
6356594783dSgdamore 	/* do detailed timings and descriptors */
6366594783dSgdamore 	for (i = 0; i < EDID_BLOCK_COUNT; i++) {
6376594783dSgdamore 		edid_block(edid, data + EDID_OFFSET_DESC_BLOCK +
6386594783dSgdamore 		    i * EDID_BLOCK_SIZE);
6396594783dSgdamore 	}
6406594783dSgdamore 
6416594783dSgdamore 	edid_strchomp(edid->edid_vendorname);
6426594783dSgdamore 	edid_strchomp(edid->edid_productname);
6436594783dSgdamore 	edid_strchomp(edid->edid_serial);
6446594783dSgdamore 	edid_strchomp(edid->edid_comment);
6456594783dSgdamore 
646f658e972Smacallan 	/*
647f658e972Smacallan 	 * XXX
648f658e972Smacallan 	 * some monitors lie about their maximum supported dot clock
649f658e972Smacallan 	 * by claiming to support modes which need a higher dot clock
650f658e972Smacallan 	 * than the stated maximum.
651f658e972Smacallan 	 * For sanity's sake we bump it to the highest dot clock we find
652f658e972Smacallan 	 * in the list of supported modes
653f658e972Smacallan 	 */
654f658e972Smacallan 	for (i = 0; i < edid->edid_nmodes; i++)
655f658e972Smacallan 		if (edid->edid_modes[i].dot_clock > max_dotclock)
656f658e972Smacallan 			max_dotclock = edid->edid_modes[i].dot_clock;
657f658e972Smacallan 
65808b20cdaSmlelstv #ifdef _KERNEL
65909cb8836Sskrll 	aprint_debug("max_dotclock according to supported modes: %d\n",
660f658e972Smacallan 	    max_dotclock);
66108b20cdaSmlelstv #endif
662f658e972Smacallan 
663f658e972Smacallan 	mhz = (max_dotclock + 999) / 1000;
664f658e972Smacallan 
665f658e972Smacallan 	if (edid->edid_have_range) {
666f658e972Smacallan 		if (mhz > edid->edid_range.er_max_clock)
667f658e972Smacallan 			edid->edid_range.er_max_clock = mhz;
668f658e972Smacallan 	} else
669f658e972Smacallan 		edid->edid_range.er_max_clock = mhz;
670f658e972Smacallan 
67180044be1Sgdamore 	return 0;
6726594783dSgdamore }
6736594783dSgdamore 
674