powerpc/mpc85xx:Disable Debug TLB entry before init_tlbs
[platform/kernel/u-boot.git] / common / edid.c
1 /*
2  * Copyright (c) 2012 The Chromium OS Authors.
3  *
4  * (C) Copyright 2010
5  * Petr Stetiar <ynezz@true.cz>
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  *
25  * Contains stolen code from ddcprobe project which is:
26  * Copyright (C) Nalin Dahyabhai <bigfun@pobox.com>
27  *
28  */
29
30 #include <common.h>
31 #include <edid.h>
32 #include <linux/ctype.h>
33 #include <linux/string.h>
34
35 int edid_check_info(struct edid1_info *edid_info)
36 {
37         if ((edid_info == NULL) || (edid_info->version == 0))
38                 return -1;
39
40         if (memcmp(edid_info->header, "\x0\xff\xff\xff\xff\xff\xff\x0", 8))
41                 return -1;
42
43         if (edid_info->version == 0xff && edid_info->revision == 0xff)
44                 return -1;
45
46         return 0;
47 }
48
49 int edid_get_ranges(struct edid1_info *edid, unsigned int *hmin,
50                     unsigned int *hmax, unsigned int *vmin,
51                     unsigned int *vmax)
52 {
53         int i;
54         struct edid_monitor_descriptor *monitor;
55
56         *hmin = *hmax = *vmin = *vmax = 0;
57         if (edid_check_info(edid))
58                 return -1;
59
60         for (i = 0; i < ARRAY_SIZE(edid->monitor_details.descriptor); i++) {
61                 monitor = &edid->monitor_details.descriptor[i];
62                 if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE) {
63                         *hmin = monitor->data.range_data.horizontal_min;
64                         *hmax = monitor->data.range_data.horizontal_max;
65                         *vmin = monitor->data.range_data.vertical_min;
66                         *vmax = monitor->data.range_data.vertical_max;
67                         return 0;
68                 }
69         }
70         return -1;
71 }
72
73 /**
74  * Snip the tailing whitespace/return of a string.
75  *
76  * @param string        The string to be snipped
77  * @return the snipped string
78  */
79 static char *snip(char *string)
80 {
81         char *s;
82
83         /*
84          * This is always a 13 character buffer
85          * and it's not always terminated.
86          */
87         string[12] = '\0';
88         s = &string[strlen(string) - 1];
89
90         while (s >= string && (isspace(*s) || *s == '\n' || *s == '\r' ||
91                         *s == '\0'))
92                 *(s--) = '\0';
93
94         return string;
95 }
96
97 /**
98  * Print an EDID monitor descriptor block
99  *
100  * @param monitor       The EDID monitor descriptor block
101  * @have_timing         Modifies to 1 if the desciptor contains timing info
102  */
103 static void edid_print_dtd(struct edid_monitor_descriptor *monitor,
104                            unsigned int *have_timing)
105 {
106         unsigned char *bytes = (unsigned char *)monitor;
107         struct edid_detailed_timing *timing =
108                         (struct edid_detailed_timing *)monitor;
109
110         if (bytes[0] == 0 && bytes[1] == 0) {
111                 if (monitor->type == EDID_MONITOR_DESCRIPTOR_SERIAL)
112                         printf("Monitor serial number: %s\n",
113                                snip(monitor->data.string));
114                 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_ASCII)
115                         printf("Monitor ID: %s\n",
116                                snip(monitor->data.string));
117                 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_NAME)
118                         printf("Monitor name: %s\n",
119                                snip(monitor->data.string));
120                 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE)
121                         printf("Monitor range limits, horizontal sync: "
122                                "%d-%d kHz, vertical refresh: "
123                                "%d-%d Hz, max pixel clock: "
124                                "%d MHz\n",
125                                monitor->data.range_data.horizontal_min,
126                                monitor->data.range_data.horizontal_max,
127                                monitor->data.range_data.vertical_min,
128                                monitor->data.range_data.vertical_max,
129                                monitor->data.range_data.pixel_clock_max * 10);
130         } else {
131                 uint32_t pixclock, h_active, h_blanking, v_active, v_blanking;
132                 uint32_t h_total, v_total, vfreq;
133
134                 pixclock = EDID_DETAILED_TIMING_PIXEL_CLOCK(*timing);
135                 h_active = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*timing);
136                 h_blanking = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*timing);
137                 v_active = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*timing);
138                 v_blanking = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*timing);
139
140                 h_total = h_active + h_blanking;
141                 v_total = v_active + v_blanking;
142                 if (v_total * h_total)
143                         vfreq = pixclock / (v_total * h_total);
144                 else
145                         vfreq = 1; /* Error case */
146                 printf("\t%dx%d\%c\t%d Hz (detailed)\n", h_active,
147                        v_active, h_active > 1000 ? ' ' : '\t', vfreq);
148                 *have_timing = 1;
149         }
150 }
151
152 /**
153  * Get the manufacturer name from an EDID info.
154  *
155  * @param edid_info     The EDID info to be printed
156  * @param name          Returns the string of the manufacturer name
157  */
158 static void edid_get_manufacturer_name(struct edid1_info *edid, char *name)
159 {
160         name[0] = EDID1_INFO_MANUFACTURER_NAME_CHAR1(*edid) + 'A' - 1;
161         name[1] = EDID1_INFO_MANUFACTURER_NAME_CHAR2(*edid) + 'A' - 1;
162         name[2] = EDID1_INFO_MANUFACTURER_NAME_CHAR3(*edid) + 'A' - 1;
163         name[3] = '\0';
164 }
165
166 void edid_print_info(struct edid1_info *edid_info)
167 {
168         int i;
169         char manufacturer[4];
170         unsigned int have_timing = 0;
171         uint32_t serial_number;
172
173         if (edid_check_info(edid_info)) {
174                 printf("Not a valid EDID\n");
175                 return;
176         }
177
178         printf("EDID version: %d.%d\n",
179                edid_info->version, edid_info->revision);
180
181         printf("Product ID code: %04x\n", EDID1_INFO_PRODUCT_CODE(*edid_info));
182
183         edid_get_manufacturer_name(edid_info, manufacturer);
184         printf("Manufacturer: %s\n", manufacturer);
185
186         serial_number = EDID1_INFO_SERIAL_NUMBER(*edid_info);
187         if (serial_number != 0xffffffff) {
188                 if (strcmp(manufacturer, "MAG") == 0)
189                         serial_number -= 0x7000000;
190                 if (strcmp(manufacturer, "OQI") == 0)
191                         serial_number -= 456150000;
192                 if (strcmp(manufacturer, "VSC") == 0)
193                         serial_number -= 640000000;
194         }
195         printf("Serial number: %08x\n", serial_number);
196         printf("Manufactured in week: %d year: %d\n",
197                edid_info->week, edid_info->year + 1990);
198
199         printf("Video input definition: %svoltage level %d%s%s%s%s%s\n",
200                EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid_info) ?
201                "digital signal, " : "analog signal, ",
202                EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(*edid_info),
203                EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(*edid_info) ?
204                ", blank to black" : "",
205                EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(*edid_info) ?
206                ", separate sync" : "",
207                EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(*edid_info) ?
208                ", composite sync" : "",
209                EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(*edid_info) ?
210                ", sync on green" : "",
211                EDID1_INFO_VIDEO_INPUT_SERRATION_V(*edid_info) ?
212                ", serration v" : "");
213
214         printf("Monitor is %s\n",
215                EDID1_INFO_FEATURE_RGB(*edid_info) ? "RGB" : "non-RGB");
216
217         printf("Maximum visible display size: %d cm x %d cm\n",
218                edid_info->max_size_horizontal,
219                edid_info->max_size_vertical);
220
221         printf("Power management features: %s%s, %s%s, %s%s\n",
222                EDID1_INFO_FEATURE_ACTIVE_OFF(*edid_info) ?
223                "" : "no ", "active off",
224                EDID1_INFO_FEATURE_SUSPEND(*edid_info) ? "" : "no ", "suspend",
225                EDID1_INFO_FEATURE_STANDBY(*edid_info) ? "" : "no ", "standby");
226
227         printf("Estabilished timings:\n");
228         if (EDID1_INFO_ESTABLISHED_TIMING_720X400_70(*edid_info))
229                 printf("\t720x400\t\t70 Hz (VGA 640x400, IBM)\n");
230         if (EDID1_INFO_ESTABLISHED_TIMING_720X400_88(*edid_info))
231                 printf("\t720x400\t\t88 Hz (XGA2)\n");
232         if (EDID1_INFO_ESTABLISHED_TIMING_640X480_60(*edid_info))
233                 printf("\t640x480\t\t60 Hz (VGA)\n");
234         if (EDID1_INFO_ESTABLISHED_TIMING_640X480_67(*edid_info))
235                 printf("\t640x480\t\t67 Hz (Mac II, Apple)\n");
236         if (EDID1_INFO_ESTABLISHED_TIMING_640X480_72(*edid_info))
237                 printf("\t640x480\t\t72 Hz (VESA)\n");
238         if (EDID1_INFO_ESTABLISHED_TIMING_640X480_75(*edid_info))
239                 printf("\t640x480\t\t75 Hz (VESA)\n");
240         if (EDID1_INFO_ESTABLISHED_TIMING_800X600_56(*edid_info))
241                 printf("\t800x600\t\t56 Hz (VESA)\n");
242         if (EDID1_INFO_ESTABLISHED_TIMING_800X600_60(*edid_info))
243                 printf("\t800x600\t\t60 Hz (VESA)\n");
244         if (EDID1_INFO_ESTABLISHED_TIMING_800X600_72(*edid_info))
245                 printf("\t800x600\t\t72 Hz (VESA)\n");
246         if (EDID1_INFO_ESTABLISHED_TIMING_800X600_75(*edid_info))
247                 printf("\t800x600\t\t75 Hz (VESA)\n");
248         if (EDID1_INFO_ESTABLISHED_TIMING_832X624_75(*edid_info))
249                 printf("\t832x624\t\t75 Hz (Mac II)\n");
250         if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(*edid_info))
251                 printf("\t1024x768\t87 Hz Interlaced (8514A)\n");
252         if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(*edid_info))
253                 printf("\t1024x768\t60 Hz (VESA)\n");
254         if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(*edid_info))
255                 printf("\t1024x768\t70 Hz (VESA)\n");
256         if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(*edid_info))
257                 printf("\t1024x768\t75 Hz (VESA)\n");
258         if (EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(*edid_info))
259                 printf("\t1280x1024\t75 (VESA)\n");
260         if (EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(*edid_info))
261                 printf("\t1152x870\t75 (Mac II)\n");
262
263         /* Standard timings. */
264         printf("Standard timings:\n");
265         for (i = 0; i < ARRAY_SIZE(edid_info->standard_timings); i++) {
266                 unsigned int aspect = 10000;
267                 unsigned int x, y;
268                 unsigned char xres, vfreq;
269
270                 xres = EDID1_INFO_STANDARD_TIMING_XRESOLUTION(*edid_info, i);
271                 vfreq = EDID1_INFO_STANDARD_TIMING_VFREQ(*edid_info, i);
272                 if ((xres != vfreq) ||
273                     ((xres != 0) && (xres != 1)) ||
274                     ((vfreq != 0) && (vfreq != 1))) {
275                         switch (EDID1_INFO_STANDARD_TIMING_ASPECT(*edid_info,
276                                         i)) {
277                         case ASPECT_625:
278                                 aspect = 6250;
279                                 break;
280                         case ASPECT_75:
281                                 aspect = 7500;
282                                 break;
283                         case ASPECT_8:
284                                 aspect = 8000;
285                                 break;
286                         case ASPECT_5625:
287                                 aspect = 5625;
288                                 break;
289                         }
290                         x = (xres + 31) * 8;
291                         y = x * aspect / 10000;
292                         printf("\t%dx%d%c\t%d Hz\n", x, y,
293                                x > 1000 ? ' ' : '\t', (vfreq & 0x3f) + 60);
294                         have_timing = 1;
295                 }
296         }
297
298         /* Detailed timing information. */
299         for (i = 0; i < ARRAY_SIZE(edid_info->monitor_details.descriptor);
300                         i++) {
301                 edid_print_dtd(&edid_info->monitor_details.descriptor[i],
302                                &have_timing);
303         }
304
305         if (!have_timing)
306                 printf("\tNone\n");
307 }