1*0a6a1f1dSLionel Sambuc /* $NetBSD: edid.c,v 1.13 2014/11/17 00:46:04 jmcneill Exp $ */
2bdf33c70SThomas Cort
3bdf33c70SThomas Cort /*-
4bdf33c70SThomas Cort * Copyright (c) 2006 Itronix Inc.
5bdf33c70SThomas Cort * All rights reserved.
6bdf33c70SThomas Cort *
7bdf33c70SThomas Cort * Written by Garrett D'Amore for Itronix Inc.
8bdf33c70SThomas Cort *
9bdf33c70SThomas Cort * Redistribution and use in source and binary forms, with or without
10bdf33c70SThomas Cort * modification, are permitted provided that the following conditions
11bdf33c70SThomas Cort * are met:
12bdf33c70SThomas Cort * 1. Redistributions of source code must retain the above copyright
13bdf33c70SThomas Cort * notice, this list of conditions and the following disclaimer.
14bdf33c70SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
15bdf33c70SThomas Cort * notice, this list of conditions and the following disclaimer in the
16bdf33c70SThomas Cort * documentation and/or other materials provided with the distribution.
17bdf33c70SThomas Cort * 3. The name of Itronix Inc. may not be used to endorse
18bdf33c70SThomas Cort * or promote products derived from this software without specific
19bdf33c70SThomas Cort * prior written permission.
20bdf33c70SThomas Cort *
21bdf33c70SThomas Cort * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
22bdf33c70SThomas Cort * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23bdf33c70SThomas Cort * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24bdf33c70SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25bdf33c70SThomas Cort * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26bdf33c70SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27bdf33c70SThomas Cort * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28bdf33c70SThomas Cort * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29bdf33c70SThomas Cort * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30bdf33c70SThomas Cort * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31bdf33c70SThomas Cort * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32bdf33c70SThomas Cort */
33bdf33c70SThomas Cort
34bdf33c70SThomas Cort #include <sys/cdefs.h>
35*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: edid.c,v 1.13 2014/11/17 00:46:04 jmcneill Exp $");
36bdf33c70SThomas Cort
37bdf33c70SThomas Cort #include <sys/param.h>
3884d9c625SLionel Sambuc #if defined(__minix)
39bdf33c70SThomas Cort #include <errno.h>
40bdf33c70SThomas Cort #include <stdio.h>
41bdf33c70SThomas Cort #include <string.h>
42bdf33c70SThomas Cort #ifndef aprint_debug
43bdf33c70SThomas Cort #define aprint_debug if (0) printf
44bdf33c70SThomas Cort #endif /* !aprint_debug */
4584d9c625SLionel Sambuc #else
46bdf33c70SThomas Cort #include <sys/systm.h>
47bdf33c70SThomas Cort #include <sys/device.h>
48bdf33c70SThomas Cort #include <sys/kernel.h>
49bdf33c70SThomas Cort #include <sys/malloc.h>
5084d9c625SLionel Sambuc #endif /* !defined(__minix) */
51bdf33c70SThomas Cort #include <dev/videomode/videomode.h>
52bdf33c70SThomas Cort #include <dev/videomode/ediddevs.h>
53bdf33c70SThomas Cort #include <dev/videomode/edidreg.h>
54bdf33c70SThomas Cort #include <dev/videomode/edidvar.h>
55bdf33c70SThomas Cort #include <dev/videomode/vesagtf.h>
56bdf33c70SThomas Cort
57bdf33c70SThomas Cort #define EDIDVERBOSE 1
58bdf33c70SThomas Cort #define DIVIDE(x,y) (((x) + ((y) / 2)) / (y))
59bdf33c70SThomas Cort
60bdf33c70SThomas Cort /* These are reversed established timing order */
61bdf33c70SThomas Cort static const char *_edid_modes[] = {
62bdf33c70SThomas Cort "1280x1024x75",
63bdf33c70SThomas Cort "1024x768x75",
64bdf33c70SThomas Cort "1024x768x70",
65bdf33c70SThomas Cort "1024x768x60",
66bdf33c70SThomas Cort "1024x768x87i",
67bdf33c70SThomas Cort "832x624x74", /* rounding error, 74.55 Hz aka "832x624x75" */
68bdf33c70SThomas Cort "800x600x75",
69bdf33c70SThomas Cort "800x600x72",
70bdf33c70SThomas Cort "800x600x60",
71bdf33c70SThomas Cort "800x600x56",
72bdf33c70SThomas Cort "640x480x75",
73bdf33c70SThomas Cort "640x480x72",
74bdf33c70SThomas Cort "640x480x67",
75bdf33c70SThomas Cort "640x480x60",
76bdf33c70SThomas Cort "720x400x87", /* rounding error, 87.85 Hz aka "720x400x88" */
77bdf33c70SThomas Cort "720x400x70",
78bdf33c70SThomas Cort };
79bdf33c70SThomas Cort
80bdf33c70SThomas Cort #ifdef EDIDVERBOSE
81bdf33c70SThomas Cort struct edid_vendor {
82bdf33c70SThomas Cort const char *vendor;
83bdf33c70SThomas Cort const char *name;
84bdf33c70SThomas Cort };
85bdf33c70SThomas Cort
86bdf33c70SThomas Cort struct edid_product {
87bdf33c70SThomas Cort const char *vendor;
88bdf33c70SThomas Cort uint16_t product;
89bdf33c70SThomas Cort const char *name;
90bdf33c70SThomas Cort };
91bdf33c70SThomas Cort
92bdf33c70SThomas Cort #include <dev/videomode/ediddevs_data.h>
93bdf33c70SThomas Cort #endif /* EDIDVERBOSE */
94bdf33c70SThomas Cort
95bdf33c70SThomas Cort static const char *
edid_findvendor(const char * vendor)96bdf33c70SThomas Cort edid_findvendor(const char *vendor)
97bdf33c70SThomas Cort {
98bdf33c70SThomas Cort #ifdef EDIDVERBOSE
99bdf33c70SThomas Cort int n;
100bdf33c70SThomas Cort
101bdf33c70SThomas Cort for (n = 0; n < edid_nvendors; n++)
102bdf33c70SThomas Cort if (memcmp(edid_vendors[n].vendor, vendor, 3) == 0)
103bdf33c70SThomas Cort return edid_vendors[n].name;
104bdf33c70SThomas Cort #endif
105bdf33c70SThomas Cort return NULL;
106bdf33c70SThomas Cort }
107bdf33c70SThomas Cort
108bdf33c70SThomas Cort static const char *
edid_findproduct(const char * vendor,uint16_t product)109bdf33c70SThomas Cort edid_findproduct(const char *vendor, uint16_t product)
110bdf33c70SThomas Cort {
111bdf33c70SThomas Cort #ifdef EDIDVERBOSE
112bdf33c70SThomas Cort int n;
113bdf33c70SThomas Cort
114bdf33c70SThomas Cort for (n = 0; n < edid_nproducts; n++)
115bdf33c70SThomas Cort if (edid_products[n].product == product &&
116bdf33c70SThomas Cort memcmp(edid_products[n].vendor, vendor, 3) == 0)
117bdf33c70SThomas Cort return edid_products[n].name;
118bdf33c70SThomas Cort #endif /* EDIDVERBOSE */
119bdf33c70SThomas Cort return NULL;
120bdf33c70SThomas Cort
121bdf33c70SThomas Cort }
122bdf33c70SThomas Cort
123bdf33c70SThomas Cort static void
edid_strchomp(char * ptr)124bdf33c70SThomas Cort edid_strchomp(char *ptr)
125bdf33c70SThomas Cort {
126bdf33c70SThomas Cort for (;;) {
127bdf33c70SThomas Cort switch (*ptr) {
128bdf33c70SThomas Cort case '\0':
129bdf33c70SThomas Cort return;
130bdf33c70SThomas Cort case '\r':
131bdf33c70SThomas Cort case '\n':
132bdf33c70SThomas Cort *ptr = '\0';
133bdf33c70SThomas Cort return;
134bdf33c70SThomas Cort }
135bdf33c70SThomas Cort ptr++;
136bdf33c70SThomas Cort }
137bdf33c70SThomas Cort }
138bdf33c70SThomas Cort
139bdf33c70SThomas Cort int
edid_is_valid(uint8_t * d)140bdf33c70SThomas Cort edid_is_valid(uint8_t *d)
141bdf33c70SThomas Cort {
142bdf33c70SThomas Cort int sum = 0, i;
143bdf33c70SThomas Cort uint8_t sig[8] = EDID_SIGNATURE;
144bdf33c70SThomas Cort
145bdf33c70SThomas Cort if (memcmp(d, sig, 8) != 0)
146bdf33c70SThomas Cort return EINVAL;
147bdf33c70SThomas Cort
148bdf33c70SThomas Cort for (i = 0; i < 128; i++)
149bdf33c70SThomas Cort sum += d[i];
150bdf33c70SThomas Cort if ((sum & 0xff) != 0)
151bdf33c70SThomas Cort return EINVAL;
152bdf33c70SThomas Cort
153bdf33c70SThomas Cort return 0;
154bdf33c70SThomas Cort }
155bdf33c70SThomas Cort
156bdf33c70SThomas Cort void
edid_print(struct edid_info * edid)157bdf33c70SThomas Cort edid_print(struct edid_info *edid)
158bdf33c70SThomas Cort {
159bdf33c70SThomas Cort int i;
160bdf33c70SThomas Cort
161bdf33c70SThomas Cort if (edid == NULL)
162bdf33c70SThomas Cort return;
163bdf33c70SThomas Cort printf("Vendor: [%s] %s\n", edid->edid_vendor, edid->edid_vendorname);
164bdf33c70SThomas Cort printf("Product: [%04X] %s\n", edid->edid_product,
165bdf33c70SThomas Cort edid->edid_productname);
166bdf33c70SThomas Cort printf("Serial number: %s\n", edid->edid_serial);
167bdf33c70SThomas Cort printf("Manufactured %d Week %d\n",
168bdf33c70SThomas Cort edid->edid_year, edid->edid_week);
169bdf33c70SThomas Cort printf("EDID Version %d.%d\n", edid->edid_version,
170bdf33c70SThomas Cort edid->edid_revision);
171bdf33c70SThomas Cort printf("EDID Comment: %s\n", edid->edid_comment);
172bdf33c70SThomas Cort
173bdf33c70SThomas Cort printf("Video Input: %x\n", edid->edid_video_input);
174bdf33c70SThomas Cort if (edid->edid_video_input & EDID_VIDEO_INPUT_DIGITAL) {
175bdf33c70SThomas Cort printf("\tDigital");
176bdf33c70SThomas Cort if (edid->edid_video_input & EDID_VIDEO_INPUT_DFP1_COMPAT)
177bdf33c70SThomas Cort printf(" (DFP 1.x compatible)");
178bdf33c70SThomas Cort printf("\n");
179bdf33c70SThomas Cort } else {
180bdf33c70SThomas Cort printf("\tAnalog\n");
181bdf33c70SThomas Cort switch (EDID_VIDEO_INPUT_LEVEL(edid->edid_video_input)) {
182bdf33c70SThomas Cort case 0:
183bdf33c70SThomas Cort printf("\t-0.7, 0.3V\n");
184bdf33c70SThomas Cort break;
185bdf33c70SThomas Cort case 1:
186bdf33c70SThomas Cort printf("\t-0.714, 0.286V\n");
187bdf33c70SThomas Cort break;
188bdf33c70SThomas Cort case 2:
189bdf33c70SThomas Cort printf("\t-1.0, 0.4V\n");
190bdf33c70SThomas Cort break;
191bdf33c70SThomas Cort case 3:
192bdf33c70SThomas Cort printf("\t-0.7, 0.0V\n");
193bdf33c70SThomas Cort break;
194bdf33c70SThomas Cort }
195bdf33c70SThomas Cort if (edid->edid_video_input & EDID_VIDEO_INPUT_BLANK_TO_BLACK)
196bdf33c70SThomas Cort printf("\tBlank-to-black setup\n");
197bdf33c70SThomas Cort if (edid->edid_video_input & EDID_VIDEO_INPUT_SEPARATE_SYNCS)
198bdf33c70SThomas Cort printf("\tSeperate syncs\n");
199bdf33c70SThomas Cort if (edid->edid_video_input & EDID_VIDEO_INPUT_COMPOSITE_SYNC)
200bdf33c70SThomas Cort printf("\tComposite sync\n");
201bdf33c70SThomas Cort if (edid->edid_video_input & EDID_VIDEO_INPUT_SYNC_ON_GRN)
202bdf33c70SThomas Cort printf("\tSync on green\n");
203bdf33c70SThomas Cort if (edid->edid_video_input & EDID_VIDEO_INPUT_SERRATION)
204bdf33c70SThomas Cort printf("\tSerration vsync\n");
205bdf33c70SThomas Cort }
206bdf33c70SThomas Cort
207bdf33c70SThomas Cort printf("Gamma: %d.%02d\n",
208bdf33c70SThomas Cort edid->edid_gamma / 100, edid->edid_gamma % 100);
209bdf33c70SThomas Cort
210bdf33c70SThomas Cort printf("Max Size: %d cm x %d cm\n",
211bdf33c70SThomas Cort edid->edid_max_hsize, edid->edid_max_vsize);
212bdf33c70SThomas Cort
213bdf33c70SThomas Cort printf("Features: %x\n", edid->edid_features);
214bdf33c70SThomas Cort if (edid->edid_features & EDID_FEATURES_STANDBY)
215bdf33c70SThomas Cort printf("\tDPMS standby\n");
216bdf33c70SThomas Cort if (edid->edid_features & EDID_FEATURES_SUSPEND)
217bdf33c70SThomas Cort printf("\tDPMS suspend\n");
218bdf33c70SThomas Cort if (edid->edid_features & EDID_FEATURES_ACTIVE_OFF)
219bdf33c70SThomas Cort printf("\tDPMS active-off\n");
220bdf33c70SThomas Cort switch (EDID_FEATURES_DISP_TYPE(edid->edid_features)) {
221bdf33c70SThomas Cort case EDID_FEATURES_DISP_TYPE_MONO:
222bdf33c70SThomas Cort printf("\tMonochrome\n");
223bdf33c70SThomas Cort break;
224bdf33c70SThomas Cort case EDID_FEATURES_DISP_TYPE_RGB:
225bdf33c70SThomas Cort printf("\tRGB\n");
226bdf33c70SThomas Cort break;
227bdf33c70SThomas Cort case EDID_FEATURES_DISP_TYPE_NON_RGB:
228bdf33c70SThomas Cort printf("\tMulticolor\n");
229bdf33c70SThomas Cort break;
230bdf33c70SThomas Cort case EDID_FEATURES_DISP_TYPE_UNDEFINED:
231bdf33c70SThomas Cort printf("\tUndefined monitor type\n");
232bdf33c70SThomas Cort break;
233bdf33c70SThomas Cort }
234bdf33c70SThomas Cort if (edid->edid_features & EDID_FEATURES_STD_COLOR)
235bdf33c70SThomas Cort printf("\tStandard color space\n");
236bdf33c70SThomas Cort if (edid->edid_features & EDID_FEATURES_PREFERRED_TIMING)
237bdf33c70SThomas Cort printf("\tPreferred timing\n");
238bdf33c70SThomas Cort if (edid->edid_features & EDID_FEATURES_DEFAULT_GTF)
239bdf33c70SThomas Cort printf("\tDefault GTF supported\n");
240bdf33c70SThomas Cort
241bdf33c70SThomas Cort printf("Chroma Info:\n");
242bdf33c70SThomas Cort printf("\tRed X: 0.%03d\n", edid->edid_chroma.ec_redx);
243bdf33c70SThomas Cort printf("\tRed Y: 0.%03d\n", edid->edid_chroma.ec_redy);
244bdf33c70SThomas Cort printf("\tGrn X: 0.%03d\n", edid->edid_chroma.ec_greenx);
245bdf33c70SThomas Cort printf("\tGrn Y: 0.%03d\n", edid->edid_chroma.ec_greeny);
246bdf33c70SThomas Cort printf("\tBlu X: 0.%03d\n", edid->edid_chroma.ec_bluex);
247bdf33c70SThomas Cort printf("\tBlu Y: 0.%03d\n", edid->edid_chroma.ec_bluey);
248bdf33c70SThomas Cort printf("\tWht X: 0.%03d\n", edid->edid_chroma.ec_whitex);
249bdf33c70SThomas Cort printf("\tWht Y: 0.%03d\n", edid->edid_chroma.ec_whitey);
250bdf33c70SThomas Cort
251bdf33c70SThomas Cort if (edid->edid_have_range) {
252bdf33c70SThomas Cort printf("Range:\n");
253bdf33c70SThomas Cort printf("\tHorizontal: %d - %d kHz\n",
254bdf33c70SThomas Cort edid->edid_range.er_min_hfreq,
255bdf33c70SThomas Cort edid->edid_range.er_max_hfreq);
256bdf33c70SThomas Cort printf("\tVertical: %d - %d Hz\n",
257bdf33c70SThomas Cort edid->edid_range.er_min_vfreq,
258bdf33c70SThomas Cort edid->edid_range.er_max_vfreq);
259bdf33c70SThomas Cort printf("\tMax Dot Clock: %d MHz\n",
260bdf33c70SThomas Cort edid->edid_range.er_max_clock);
261bdf33c70SThomas Cort if (edid->edid_range.er_have_gtf2) {
262bdf33c70SThomas Cort printf("\tGTF2 hfreq: %d\n",
263bdf33c70SThomas Cort edid->edid_range.er_gtf2_hfreq);
264bdf33c70SThomas Cort printf("\tGTF2 C: %d\n", edid->edid_range.er_gtf2_c);
265bdf33c70SThomas Cort printf("\tGTF2 M: %d\n", edid->edid_range.er_gtf2_m);
266bdf33c70SThomas Cort printf("\tGTF2 J: %d\n", edid->edid_range.er_gtf2_j);
267bdf33c70SThomas Cort printf("\tGTF2 K: %d\n", edid->edid_range.er_gtf2_k);
268bdf33c70SThomas Cort }
269bdf33c70SThomas Cort }
270bdf33c70SThomas Cort printf("Video modes:\n");
271bdf33c70SThomas Cort for (i = 0; i < edid->edid_nmodes; i++) {
272bdf33c70SThomas Cort printf("\t%dx%d @ %dHz",
273bdf33c70SThomas Cort edid->edid_modes[i].hdisplay,
274bdf33c70SThomas Cort edid->edid_modes[i].vdisplay,
275bdf33c70SThomas Cort DIVIDE(DIVIDE(edid->edid_modes[i].dot_clock * 1000,
276bdf33c70SThomas Cort edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal));
277bdf33c70SThomas Cort printf(" (%d %d %d %d %d %d %d",
278bdf33c70SThomas Cort edid->edid_modes[i].dot_clock,
279bdf33c70SThomas Cort edid->edid_modes[i].hsync_start,
280bdf33c70SThomas Cort edid->edid_modes[i].hsync_end,
281bdf33c70SThomas Cort edid->edid_modes[i].htotal,
282bdf33c70SThomas Cort edid->edid_modes[i].vsync_start,
283bdf33c70SThomas Cort edid->edid_modes[i].vsync_end,
284bdf33c70SThomas Cort edid->edid_modes[i].vtotal);
285bdf33c70SThomas Cort printf(" %s%sH %s%sV)\n",
286bdf33c70SThomas Cort edid->edid_modes[i].flags & VID_PHSYNC ? "+" : "",
287bdf33c70SThomas Cort edid->edid_modes[i].flags & VID_NHSYNC ? "-" : "",
288bdf33c70SThomas Cort edid->edid_modes[i].flags & VID_PVSYNC ? "+" : "",
289bdf33c70SThomas Cort edid->edid_modes[i].flags & VID_NVSYNC ? "-" : "");
290bdf33c70SThomas Cort }
291bdf33c70SThomas Cort if (edid->edid_preferred_mode)
292bdf33c70SThomas Cort printf("Preferred mode: %dx%d @ %dHz\n",
293bdf33c70SThomas Cort edid->edid_preferred_mode->hdisplay,
294bdf33c70SThomas Cort edid->edid_preferred_mode->vdisplay,
295bdf33c70SThomas Cort DIVIDE(DIVIDE(edid->edid_preferred_mode->dot_clock * 1000,
296bdf33c70SThomas Cort edid->edid_preferred_mode->htotal),
297bdf33c70SThomas Cort edid->edid_preferred_mode->vtotal));
298*0a6a1f1dSLionel Sambuc
299*0a6a1f1dSLionel Sambuc printf("Number of extension blocks: %d\n", edid->edid_ext_block_count);
300bdf33c70SThomas Cort }
301bdf33c70SThomas Cort
302bdf33c70SThomas Cort static const struct videomode *
edid_mode_lookup_list(const char * name)303bdf33c70SThomas Cort edid_mode_lookup_list(const char *name)
304bdf33c70SThomas Cort {
305bdf33c70SThomas Cort int i;
306bdf33c70SThomas Cort
307bdf33c70SThomas Cort for (i = 0; i < videomode_count; i++)
308bdf33c70SThomas Cort if (strcmp(name, videomode_list[i].name) == 0)
309bdf33c70SThomas Cort return &videomode_list[i];
310bdf33c70SThomas Cort return NULL;
311bdf33c70SThomas Cort }
312bdf33c70SThomas Cort
313bdf33c70SThomas Cort static struct videomode *
edid_search_mode(struct edid_info * edid,const struct videomode * mode)314bdf33c70SThomas Cort edid_search_mode(struct edid_info *edid, const struct videomode *mode)
315bdf33c70SThomas Cort {
316bdf33c70SThomas Cort int refresh, i;
317bdf33c70SThomas Cort
318bdf33c70SThomas Cort refresh = DIVIDE(DIVIDE(mode->dot_clock * 1000,
319bdf33c70SThomas Cort mode->htotal), mode->vtotal);
320bdf33c70SThomas Cort for (i = 0; i < edid->edid_nmodes; i++) {
321bdf33c70SThomas Cort if (mode->hdisplay == edid->edid_modes[i].hdisplay &&
322bdf33c70SThomas Cort mode->vdisplay == edid->edid_modes[i].vdisplay &&
323bdf33c70SThomas Cort refresh == DIVIDE(DIVIDE(
324bdf33c70SThomas Cort edid->edid_modes[i].dot_clock * 1000,
325bdf33c70SThomas Cort edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal)) {
326bdf33c70SThomas Cort return &edid->edid_modes[i];
327bdf33c70SThomas Cort }
328bdf33c70SThomas Cort }
329bdf33c70SThomas Cort return NULL;
330bdf33c70SThomas Cort }
331bdf33c70SThomas Cort
332bdf33c70SThomas Cort static int
edid_std_timing(uint8_t * data,struct videomode * vmp)333bdf33c70SThomas Cort edid_std_timing(uint8_t *data, struct videomode *vmp)
334bdf33c70SThomas Cort {
335bdf33c70SThomas Cort unsigned x, y, f;
336bdf33c70SThomas Cort const struct videomode *lookup;
337bdf33c70SThomas Cort char name[80];
338bdf33c70SThomas Cort
339bdf33c70SThomas Cort if ((data[0] == 1 && data[1] == 1) ||
340bdf33c70SThomas Cort (data[0] == 0 && data[1] == 0) ||
341bdf33c70SThomas Cort (data[0] == 0x20 && data[1] == 0x20))
342bdf33c70SThomas Cort return 0;
343bdf33c70SThomas Cort
344bdf33c70SThomas Cort x = EDID_STD_TIMING_HRES(data);
345bdf33c70SThomas Cort switch (EDID_STD_TIMING_RATIO(data)) {
346bdf33c70SThomas Cort case EDID_STD_TIMING_RATIO_16_10:
347bdf33c70SThomas Cort y = x * 10 / 16;
348bdf33c70SThomas Cort break;
349bdf33c70SThomas Cort case EDID_STD_TIMING_RATIO_4_3:
350bdf33c70SThomas Cort y = x * 3 / 4;
351bdf33c70SThomas Cort break;
352bdf33c70SThomas Cort case EDID_STD_TIMING_RATIO_5_4:
353bdf33c70SThomas Cort y = x * 4 / 5;
354bdf33c70SThomas Cort break;
355bdf33c70SThomas Cort case EDID_STD_TIMING_RATIO_16_9:
356bdf33c70SThomas Cort default:
357bdf33c70SThomas Cort y = x * 9 / 16;
358bdf33c70SThomas Cort break;
359bdf33c70SThomas Cort }
360bdf33c70SThomas Cort f = EDID_STD_TIMING_VFREQ(data);
361bdf33c70SThomas Cort
362bdf33c70SThomas Cort /* first try to lookup the mode as a DMT timing */
363bdf33c70SThomas Cort snprintf(name, sizeof(name), "%dx%dx%d", x, y, f);
364bdf33c70SThomas Cort if ((lookup = edid_mode_lookup_list(name)) != NULL) {
365bdf33c70SThomas Cort *vmp = *lookup;
366bdf33c70SThomas Cort } else {
367bdf33c70SThomas Cort /* failing that, calculate it using gtf */
368bdf33c70SThomas Cort /*
369bdf33c70SThomas Cort * Hmm. I'm not using alternate GTF timings, which
370bdf33c70SThomas Cort * could, in theory, be present.
371bdf33c70SThomas Cort */
372bdf33c70SThomas Cort vesagtf_mode(x, y, f, vmp);
373bdf33c70SThomas Cort }
374bdf33c70SThomas Cort return 1;
375bdf33c70SThomas Cort }
376bdf33c70SThomas Cort
377bdf33c70SThomas Cort static int
edid_det_timing(uint8_t * data,struct videomode * vmp)378bdf33c70SThomas Cort edid_det_timing(uint8_t *data, struct videomode *vmp)
379bdf33c70SThomas Cort {
380bdf33c70SThomas Cort unsigned hactive, hblank, hsyncwid, hsyncoff;
381bdf33c70SThomas Cort unsigned vactive, vblank, vsyncwid, vsyncoff;
382bdf33c70SThomas Cort uint8_t flags;
383bdf33c70SThomas Cort
384bdf33c70SThomas Cort flags = EDID_DET_TIMING_FLAGS(data);
385bdf33c70SThomas Cort
386bdf33c70SThomas Cort /* we don't support stereo modes (for now) */
387bdf33c70SThomas Cort if (flags & (EDID_DET_TIMING_FLAG_STEREO |
388bdf33c70SThomas Cort EDID_DET_TIMING_FLAG_STEREO_MODE))
389bdf33c70SThomas Cort return 0;
390bdf33c70SThomas Cort
391bdf33c70SThomas Cort vmp->dot_clock = EDID_DET_TIMING_DOT_CLOCK(data) / 1000;
392bdf33c70SThomas Cort
393bdf33c70SThomas Cort hactive = EDID_DET_TIMING_HACTIVE(data);
394bdf33c70SThomas Cort hblank = EDID_DET_TIMING_HBLANK(data);
395bdf33c70SThomas Cort hsyncwid = EDID_DET_TIMING_HSYNC_WIDTH(data);
396bdf33c70SThomas Cort hsyncoff = EDID_DET_TIMING_HSYNC_OFFSET(data);
397bdf33c70SThomas Cort
398bdf33c70SThomas Cort vactive = EDID_DET_TIMING_VACTIVE(data);
399bdf33c70SThomas Cort vblank = EDID_DET_TIMING_VBLANK(data);
400bdf33c70SThomas Cort vsyncwid = EDID_DET_TIMING_VSYNC_WIDTH(data);
401bdf33c70SThomas Cort vsyncoff = EDID_DET_TIMING_VSYNC_OFFSET(data);
402bdf33c70SThomas Cort
403bdf33c70SThomas Cort /* Borders are contained within the blank areas. */
404bdf33c70SThomas Cort
405bdf33c70SThomas Cort vmp->hdisplay = hactive;
406bdf33c70SThomas Cort vmp->htotal = hactive + hblank;
407bdf33c70SThomas Cort vmp->hsync_start = hactive + hsyncoff;
408bdf33c70SThomas Cort vmp->hsync_end = vmp->hsync_start + hsyncwid;
409bdf33c70SThomas Cort
410bdf33c70SThomas Cort vmp->vdisplay = vactive;
411bdf33c70SThomas Cort vmp->vtotal = vactive + vblank;
412bdf33c70SThomas Cort vmp->vsync_start = vactive + vsyncoff;
413bdf33c70SThomas Cort vmp->vsync_end = vmp->vsync_start + vsyncwid;
414bdf33c70SThomas Cort
415bdf33c70SThomas Cort vmp->flags = 0;
416bdf33c70SThomas Cort
417bdf33c70SThomas Cort if (flags & EDID_DET_TIMING_FLAG_INTERLACE)
418bdf33c70SThomas Cort vmp->flags |= VID_INTERLACE;
419bdf33c70SThomas Cort if (flags & EDID_DET_TIMING_FLAG_HSYNC_POSITIVE)
420bdf33c70SThomas Cort vmp->flags |= VID_PHSYNC;
421bdf33c70SThomas Cort else
422bdf33c70SThomas Cort vmp->flags |= VID_NHSYNC;
423bdf33c70SThomas Cort
424bdf33c70SThomas Cort if (flags & EDID_DET_TIMING_FLAG_VSYNC_POSITIVE)
425bdf33c70SThomas Cort vmp->flags |= VID_PVSYNC;
426bdf33c70SThomas Cort else
427bdf33c70SThomas Cort vmp->flags |= VID_NVSYNC;
428bdf33c70SThomas Cort
429bdf33c70SThomas Cort return 1;
430bdf33c70SThomas Cort }
431bdf33c70SThomas Cort
432bdf33c70SThomas Cort static void
edid_block(struct edid_info * edid,uint8_t * data)433bdf33c70SThomas Cort edid_block(struct edid_info *edid, uint8_t *data)
434bdf33c70SThomas Cort {
435bdf33c70SThomas Cort int i;
436bdf33c70SThomas Cort struct videomode mode, *exist_mode;
437bdf33c70SThomas Cort
438bdf33c70SThomas Cort if (EDID_BLOCK_IS_DET_TIMING(data)) {
439bdf33c70SThomas Cort if (!edid_det_timing(data, &mode))
440bdf33c70SThomas Cort return;
441bdf33c70SThomas Cort /* Does this mode already exist? */
442bdf33c70SThomas Cort exist_mode = edid_search_mode(edid, &mode);
443bdf33c70SThomas Cort if (exist_mode != NULL) {
444bdf33c70SThomas Cort *exist_mode = mode;
445bdf33c70SThomas Cort if (edid->edid_preferred_mode == NULL)
446bdf33c70SThomas Cort edid->edid_preferred_mode = exist_mode;
447bdf33c70SThomas Cort } else {
448bdf33c70SThomas Cort edid->edid_modes[edid->edid_nmodes] = mode;
449bdf33c70SThomas Cort if (edid->edid_preferred_mode == NULL)
450bdf33c70SThomas Cort edid->edid_preferred_mode =
451bdf33c70SThomas Cort &edid->edid_modes[edid->edid_nmodes];
452bdf33c70SThomas Cort edid->edid_nmodes++;
453bdf33c70SThomas Cort }
454bdf33c70SThomas Cort return;
455bdf33c70SThomas Cort }
456bdf33c70SThomas Cort
457bdf33c70SThomas Cort switch (EDID_BLOCK_TYPE(data)) {
458bdf33c70SThomas Cort case EDID_DESC_BLOCK_TYPE_SERIAL:
459bdf33c70SThomas Cort memcpy(edid->edid_serial, data + EDID_DESC_ASCII_DATA_OFFSET,
460bdf33c70SThomas Cort EDID_DESC_ASCII_DATA_LEN);
461bdf33c70SThomas Cort edid->edid_serial[sizeof(edid->edid_serial) - 1] = 0;
462bdf33c70SThomas Cort break;
463bdf33c70SThomas Cort
464bdf33c70SThomas Cort case EDID_DESC_BLOCK_TYPE_ASCII:
465bdf33c70SThomas Cort memcpy(edid->edid_comment, data + EDID_DESC_ASCII_DATA_OFFSET,
466bdf33c70SThomas Cort EDID_DESC_ASCII_DATA_LEN);
467bdf33c70SThomas Cort edid->edid_comment[sizeof(edid->edid_comment) - 1] = 0;
468bdf33c70SThomas Cort break;
469bdf33c70SThomas Cort
470bdf33c70SThomas Cort case EDID_DESC_BLOCK_TYPE_RANGE:
471bdf33c70SThomas Cort edid->edid_have_range = 1;
472bdf33c70SThomas Cort edid->edid_range.er_min_vfreq = EDID_DESC_RANGE_MIN_VFREQ(data);
473bdf33c70SThomas Cort edid->edid_range.er_max_vfreq = EDID_DESC_RANGE_MAX_VFREQ(data);
474bdf33c70SThomas Cort edid->edid_range.er_min_hfreq = EDID_DESC_RANGE_MIN_HFREQ(data);
475bdf33c70SThomas Cort edid->edid_range.er_max_hfreq = EDID_DESC_RANGE_MAX_HFREQ(data);
476bdf33c70SThomas Cort edid->edid_range.er_max_clock = EDID_DESC_RANGE_MAX_CLOCK(data);
477bdf33c70SThomas Cort if (!EDID_DESC_RANGE_HAVE_GTF2(data))
478bdf33c70SThomas Cort break;
479bdf33c70SThomas Cort edid->edid_range.er_have_gtf2 = 1;
480bdf33c70SThomas Cort edid->edid_range.er_gtf2_hfreq =
481bdf33c70SThomas Cort EDID_DESC_RANGE_GTF2_HFREQ(data);
482bdf33c70SThomas Cort edid->edid_range.er_gtf2_c = EDID_DESC_RANGE_GTF2_C(data);
483bdf33c70SThomas Cort edid->edid_range.er_gtf2_m = EDID_DESC_RANGE_GTF2_M(data);
484bdf33c70SThomas Cort edid->edid_range.er_gtf2_j = EDID_DESC_RANGE_GTF2_J(data);
485bdf33c70SThomas Cort edid->edid_range.er_gtf2_k = EDID_DESC_RANGE_GTF2_K(data);
486bdf33c70SThomas Cort break;
487bdf33c70SThomas Cort
488bdf33c70SThomas Cort case EDID_DESC_BLOCK_TYPE_NAME:
489bdf33c70SThomas Cort /* copy the product name into place */
490bdf33c70SThomas Cort memcpy(edid->edid_productname,
491bdf33c70SThomas Cort data + EDID_DESC_ASCII_DATA_OFFSET,
492bdf33c70SThomas Cort EDID_DESC_ASCII_DATA_LEN);
493bdf33c70SThomas Cort break;
494bdf33c70SThomas Cort
495bdf33c70SThomas Cort case EDID_DESC_BLOCK_TYPE_STD_TIMING:
496bdf33c70SThomas Cort data += EDID_DESC_STD_TIMING_START;
497bdf33c70SThomas Cort for (i = 0; i < EDID_DESC_STD_TIMING_COUNT; i++) {
498bdf33c70SThomas Cort if (edid_std_timing(data, &mode)) {
499bdf33c70SThomas Cort /* Does this mode already exist? */
500bdf33c70SThomas Cort exist_mode = edid_search_mode(edid, &mode);
501bdf33c70SThomas Cort if (exist_mode == NULL) {
502bdf33c70SThomas Cort edid->edid_modes[edid->edid_nmodes] =
503bdf33c70SThomas Cort mode;
504bdf33c70SThomas Cort edid->edid_nmodes++;
505bdf33c70SThomas Cort }
506bdf33c70SThomas Cort }
507bdf33c70SThomas Cort data += 2;
508bdf33c70SThomas Cort }
509bdf33c70SThomas Cort break;
510bdf33c70SThomas Cort
511bdf33c70SThomas Cort case EDID_DESC_BLOCK_TYPE_COLOR_POINT:
512bdf33c70SThomas Cort /* XXX: not implemented yet */
513bdf33c70SThomas Cort break;
514bdf33c70SThomas Cort }
515bdf33c70SThomas Cort }
516bdf33c70SThomas Cort
517bdf33c70SThomas Cort /*
518bdf33c70SThomas Cort * Gets EDID version in BCD, e.g. EDID v1.3 returned as 0x0103
519bdf33c70SThomas Cort */
520bdf33c70SThomas Cort int
edid_parse(uint8_t * data,struct edid_info * edid)521bdf33c70SThomas Cort edid_parse(uint8_t *data, struct edid_info *edid)
522bdf33c70SThomas Cort {
523bdf33c70SThomas Cort uint16_t manfid, estmodes;
524bdf33c70SThomas Cort const struct videomode *vmp;
525bdf33c70SThomas Cort int i;
526bdf33c70SThomas Cort const char *name;
527bdf33c70SThomas Cort int max_dotclock = 0;
528bdf33c70SThomas Cort int mhz;
529bdf33c70SThomas Cort
530bdf33c70SThomas Cort if (edid_is_valid(data) != 0)
531bdf33c70SThomas Cort return -1;
532bdf33c70SThomas Cort
533bdf33c70SThomas Cort /* get product identification */
534bdf33c70SThomas Cort manfid = EDID_VENDOR_ID(data);
535bdf33c70SThomas Cort edid->edid_vendor[0] = EDID_MANFID_0(manfid);
536bdf33c70SThomas Cort edid->edid_vendor[1] = EDID_MANFID_1(manfid);
537bdf33c70SThomas Cort edid->edid_vendor[2] = EDID_MANFID_2(manfid);
538bdf33c70SThomas Cort edid->edid_vendor[3] = 0; /* null terminate for convenience */
539bdf33c70SThomas Cort
540bdf33c70SThomas Cort edid->edid_product = data[EDID_OFFSET_PRODUCT_ID] +
541bdf33c70SThomas Cort (data[EDID_OFFSET_PRODUCT_ID + 1] << 8);
542bdf33c70SThomas Cort
543bdf33c70SThomas Cort name = edid_findvendor(edid->edid_vendor);
544bdf33c70SThomas Cort if (name != NULL)
545bdf33c70SThomas Cort strlcpy(edid->edid_vendorname, name,
546bdf33c70SThomas Cort sizeof(edid->edid_vendorname));
547bdf33c70SThomas Cort else
548bdf33c70SThomas Cort edid->edid_vendorname[0] = '\0';
549bdf33c70SThomas Cort
550bdf33c70SThomas Cort name = edid_findproduct(edid->edid_vendor, edid->edid_product);
551bdf33c70SThomas Cort if (name != NULL)
552bdf33c70SThomas Cort strlcpy(edid->edid_productname, name,
553bdf33c70SThomas Cort sizeof(edid->edid_productname));
554bdf33c70SThomas Cort else
555bdf33c70SThomas Cort edid->edid_productname[0] = '\0';
556bdf33c70SThomas Cort
557bdf33c70SThomas Cort snprintf(edid->edid_serial, sizeof(edid->edid_serial), "%08x",
558bdf33c70SThomas Cort EDID_SERIAL_NUMBER(data));
559bdf33c70SThomas Cort
560bdf33c70SThomas Cort edid->edid_week = EDID_WEEK(data);
561bdf33c70SThomas Cort edid->edid_year = EDID_YEAR(data);
562bdf33c70SThomas Cort
563bdf33c70SThomas Cort /* get edid revision */
564bdf33c70SThomas Cort edid->edid_version = EDID_VERSION(data);
565bdf33c70SThomas Cort edid->edid_revision = EDID_REVISION(data);
566bdf33c70SThomas Cort
567bdf33c70SThomas Cort edid->edid_video_input = EDID_VIDEO_INPUT(data);
568bdf33c70SThomas Cort edid->edid_max_hsize = EDID_MAX_HSIZE(data);
569bdf33c70SThomas Cort edid->edid_max_vsize = EDID_MAX_VSIZE(data);
570bdf33c70SThomas Cort
571bdf33c70SThomas Cort edid->edid_gamma = EDID_GAMMA(data);
572bdf33c70SThomas Cort edid->edid_features = EDID_FEATURES(data);
573bdf33c70SThomas Cort
574bdf33c70SThomas Cort edid->edid_chroma.ec_redx = EDID_CHROMA_REDX(data);
575bdf33c70SThomas Cort edid->edid_chroma.ec_redy = EDID_CHROMA_REDX(data);
576bdf33c70SThomas Cort edid->edid_chroma.ec_greenx = EDID_CHROMA_GREENX(data);
577bdf33c70SThomas Cort edid->edid_chroma.ec_greeny = EDID_CHROMA_GREENY(data);
578bdf33c70SThomas Cort edid->edid_chroma.ec_bluex = EDID_CHROMA_BLUEX(data);
579bdf33c70SThomas Cort edid->edid_chroma.ec_bluey = EDID_CHROMA_BLUEY(data);
580bdf33c70SThomas Cort edid->edid_chroma.ec_whitex = EDID_CHROMA_WHITEX(data);
581bdf33c70SThomas Cort edid->edid_chroma.ec_whitey = EDID_CHROMA_WHITEY(data);
582bdf33c70SThomas Cort
583*0a6a1f1dSLionel Sambuc edid->edid_ext_block_count = EDID_EXT_BLOCK_COUNT(data);
584*0a6a1f1dSLionel Sambuc
585bdf33c70SThomas Cort /* lookup established modes */
586bdf33c70SThomas Cort edid->edid_nmodes = 0;
587bdf33c70SThomas Cort edid->edid_preferred_mode = NULL;
588bdf33c70SThomas Cort estmodes = EDID_EST_TIMING(data);
589bdf33c70SThomas Cort /* Iterate in esztablished timing order */
590bdf33c70SThomas Cort for (i = 15; i >= 0; i--) {
591bdf33c70SThomas Cort if (estmodes & (1 << i)) {
592bdf33c70SThomas Cort vmp = edid_mode_lookup_list(_edid_modes[i]);
593bdf33c70SThomas Cort if (vmp != NULL) {
594bdf33c70SThomas Cort edid->edid_modes[edid->edid_nmodes] = *vmp;
595bdf33c70SThomas Cort edid->edid_nmodes++;
596bdf33c70SThomas Cort }
597bdf33c70SThomas Cort #ifdef DIAGNOSTIC
598bdf33c70SThomas Cort else
599bdf33c70SThomas Cort printf("no data for est. mode %s\n",
600bdf33c70SThomas Cort _edid_modes[i]);
601bdf33c70SThomas Cort #endif
602bdf33c70SThomas Cort }
603bdf33c70SThomas Cort }
604bdf33c70SThomas Cort
605bdf33c70SThomas Cort /* do standard timing section */
606bdf33c70SThomas Cort for (i = 0; i < EDID_STD_TIMING_COUNT; i++) {
607bdf33c70SThomas Cort struct videomode mode, *exist_mode;
608bdf33c70SThomas Cort if (edid_std_timing(data + EDID_OFFSET_STD_TIMING + i * 2,
609bdf33c70SThomas Cort &mode)) {
610bdf33c70SThomas Cort /* Does this mode already exist? */
611bdf33c70SThomas Cort exist_mode = edid_search_mode(edid, &mode);
612bdf33c70SThomas Cort if (exist_mode == NULL) {
613bdf33c70SThomas Cort edid->edid_modes[edid->edid_nmodes] = mode;
614bdf33c70SThomas Cort edid->edid_nmodes++;
615bdf33c70SThomas Cort }
616bdf33c70SThomas Cort }
617bdf33c70SThomas Cort }
618bdf33c70SThomas Cort
619bdf33c70SThomas Cort /* do detailed timings and descriptors */
620bdf33c70SThomas Cort for (i = 0; i < EDID_BLOCK_COUNT; i++) {
621bdf33c70SThomas Cort edid_block(edid, data + EDID_OFFSET_DESC_BLOCK +
622bdf33c70SThomas Cort i * EDID_BLOCK_SIZE);
623bdf33c70SThomas Cort }
624bdf33c70SThomas Cort
625bdf33c70SThomas Cort edid_strchomp(edid->edid_vendorname);
626bdf33c70SThomas Cort edid_strchomp(edid->edid_productname);
627bdf33c70SThomas Cort edid_strchomp(edid->edid_serial);
628bdf33c70SThomas Cort edid_strchomp(edid->edid_comment);
629bdf33c70SThomas Cort
630bdf33c70SThomas Cort /*
631bdf33c70SThomas Cort * XXX
632bdf33c70SThomas Cort * some monitors lie about their maximum supported dot clock
633bdf33c70SThomas Cort * by claiming to support modes which need a higher dot clock
634bdf33c70SThomas Cort * than the stated maximum.
635bdf33c70SThomas Cort * For sanity's sake we bump it to the highest dot clock we find
636bdf33c70SThomas Cort * in the list of supported modes
637bdf33c70SThomas Cort */
638bdf33c70SThomas Cort for (i = 0; i < edid->edid_nmodes; i++)
639bdf33c70SThomas Cort if (edid->edid_modes[i].dot_clock > max_dotclock)
640bdf33c70SThomas Cort max_dotclock = edid->edid_modes[i].dot_clock;
641bdf33c70SThomas Cort
642bdf33c70SThomas Cort aprint_debug("max_dotclock according to supported modes: %d\n",
643bdf33c70SThomas Cort max_dotclock);
644bdf33c70SThomas Cort
645bdf33c70SThomas Cort mhz = (max_dotclock + 999) / 1000;
646bdf33c70SThomas Cort
647bdf33c70SThomas Cort if (edid->edid_have_range) {
648bdf33c70SThomas Cort if (mhz > edid->edid_range.er_max_clock)
649bdf33c70SThomas Cort edid->edid_range.er_max_clock = mhz;
650bdf33c70SThomas Cort } else
651bdf33c70SThomas Cort edid->edid_range.er_max_clock = mhz;
652bdf33c70SThomas Cort
653bdf33c70SThomas Cort return 0;
654bdf33c70SThomas Cort }
655bdf33c70SThomas Cort
656