1 /* 2 * Copyright (c) 2012 The Chromium OS Authors. 3 * 4 * (C) Copyright 2010 5 * Petr Stetiar <ynezz@true.cz> 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 * 9 * Contains stolen code from ddcprobe project which is: 10 * Copyright (C) Nalin Dahyabhai <bigfun@pobox.com> 11 */ 12 13 #include <common.h> 14 #include <edid.h> 15 #include <errno.h> 16 #include <fdtdec.h> 17 #include <linux/ctype.h> 18 #include <linux/string.h> 19 20 int edid_check_info(struct edid1_info *edid_info) 21 { 22 if ((edid_info == NULL) || (edid_info->version == 0)) 23 return -1; 24 25 if (memcmp(edid_info->header, "\x0\xff\xff\xff\xff\xff\xff\x0", 8)) 26 return -1; 27 28 if (edid_info->version == 0xff && edid_info->revision == 0xff) 29 return -1; 30 31 return 0; 32 } 33 34 int edid_check_checksum(u8 *edid_block) 35 { 36 u8 checksum = 0; 37 int i; 38 39 for (i = 0; i < 128; i++) 40 checksum += edid_block[i]; 41 42 return (checksum == 0) ? 0 : -EINVAL; 43 } 44 45 int edid_get_ranges(struct edid1_info *edid, unsigned int *hmin, 46 unsigned int *hmax, unsigned int *vmin, 47 unsigned int *vmax) 48 { 49 int i; 50 struct edid_monitor_descriptor *monitor; 51 52 *hmin = *hmax = *vmin = *vmax = 0; 53 if (edid_check_info(edid)) 54 return -1; 55 56 for (i = 0; i < ARRAY_SIZE(edid->monitor_details.descriptor); i++) { 57 monitor = &edid->monitor_details.descriptor[i]; 58 if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE) { 59 *hmin = monitor->data.range_data.horizontal_min; 60 *hmax = monitor->data.range_data.horizontal_max; 61 *vmin = monitor->data.range_data.vertical_min; 62 *vmax = monitor->data.range_data.vertical_max; 63 return 0; 64 } 65 } 66 return -1; 67 } 68 69 /* Set all parts of a timing entry to the same value */ 70 static void set_entry(struct timing_entry *entry, u32 value) 71 { 72 entry->min = value; 73 entry->typ = value; 74 entry->max = value; 75 } 76 77 /** 78 * decode_timing() - Decoding an 18-byte detailed timing record 79 * 80 * @buf: Pointer to EDID detailed timing record 81 * @timing: Place to put timing 82 */ 83 static void decode_timing(u8 *buf, struct display_timing *timing) 84 { 85 uint x_mm, y_mm; 86 unsigned int ha, hbl, hso, hspw, hborder; 87 unsigned int va, vbl, vso, vspw, vborder; 88 struct edid_detailed_timing *t = (struct edid_detailed_timing *)buf; 89 90 /* Edid contains pixel clock in terms of 10KHz */ 91 set_entry(&timing->pixelclock, (buf[0] + (buf[1] << 8)) * 10000); 92 x_mm = (buf[12] + ((buf[14] & 0xf0) << 4)); 93 y_mm = (buf[13] + ((buf[14] & 0x0f) << 8)); 94 ha = (buf[2] + ((buf[4] & 0xf0) << 4)); 95 hbl = (buf[3] + ((buf[4] & 0x0f) << 8)); 96 hso = (buf[8] + ((buf[11] & 0xc0) << 2)); 97 hspw = (buf[9] + ((buf[11] & 0x30) << 4)); 98 hborder = buf[15]; 99 va = (buf[5] + ((buf[7] & 0xf0) << 4)); 100 vbl = (buf[6] + ((buf[7] & 0x0f) << 8)); 101 vso = ((buf[10] >> 4) + ((buf[11] & 0x0c) << 2)); 102 vspw = ((buf[10] & 0x0f) + ((buf[11] & 0x03) << 4)); 103 vborder = buf[16]; 104 105 set_entry(&timing->hactive, ha); 106 set_entry(&timing->hfront_porch, hso); 107 set_entry(&timing->hback_porch, hbl - hso - hspw); 108 set_entry(&timing->hsync_len, hspw); 109 110 set_entry(&timing->vactive, va); 111 set_entry(&timing->vfront_porch, vso); 112 set_entry(&timing->vback_porch, vbl - vso - vspw); 113 set_entry(&timing->vsync_len, vspw); 114 115 timing->flags = 0; 116 if (EDID_DETAILED_TIMING_FLAG_HSYNC_POLARITY(*t)) 117 timing->flags |= DISPLAY_FLAGS_HSYNC_HIGH; 118 else 119 timing->flags |= DISPLAY_FLAGS_HSYNC_LOW; 120 if (EDID_DETAILED_TIMING_FLAG_VSYNC_POLARITY(*t)) 121 timing->flags |= DISPLAY_FLAGS_VSYNC_HIGH; 122 else 123 timing->flags |= DISPLAY_FLAGS_VSYNC_LOW; 124 125 if (EDID_DETAILED_TIMING_FLAG_INTERLACED(*t)) 126 timing->flags = DISPLAY_FLAGS_INTERLACED; 127 128 debug("Detailed mode clock %u Hz, %d mm x %d mm\n" 129 " %04x %04x %04x %04x hborder %x\n" 130 " %04x %04x %04x %04x vborder %x\n", 131 timing->pixelclock.typ, 132 x_mm, y_mm, 133 ha, ha + hso, ha + hso + hspw, 134 ha + hbl, hborder, 135 va, va + vso, va + vso + vspw, 136 va + vbl, vborder); 137 } 138 139 int edid_get_timing(u8 *buf, int buf_size, struct display_timing *timing, 140 int *panel_bits_per_colourp) 141 { 142 struct edid1_info *edid = (struct edid1_info *)buf; 143 bool timing_done; 144 int i; 145 146 if (buf_size < sizeof(*edid) || edid_check_info(edid)) { 147 debug("%s: Invalid buffer\n", __func__); 148 return -EINVAL; 149 } 150 151 if (!EDID1_INFO_FEATURE_PREFERRED_TIMING_MODE(*edid)) { 152 debug("%s: No preferred timing\n", __func__); 153 return -ENOENT; 154 } 155 156 /* Look for detailed timing */ 157 timing_done = false; 158 for (i = 0; i < 4; i++) { 159 struct edid_monitor_descriptor *desc; 160 161 desc = &edid->monitor_details.descriptor[i]; 162 if (desc->zero_flag_1 != 0) { 163 decode_timing((u8 *)desc, timing); 164 timing_done = true; 165 break; 166 } 167 } 168 if (!timing_done) 169 return -EINVAL; 170 171 if (!EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid)) { 172 debug("%s: Not a digital display\n", __func__); 173 return -ENOSYS; 174 } 175 if (edid->version != 1 || edid->revision < 4) { 176 debug("%s: EDID version %d.%d does not have required info\n", 177 __func__, edid->version, edid->revision); 178 *panel_bits_per_colourp = -1; 179 } else { 180 *panel_bits_per_colourp = 181 ((edid->video_input_definition & 0x70) >> 3) + 4; 182 } 183 184 return 0; 185 } 186 187 /** 188 * Snip the tailing whitespace/return of a string. 189 * 190 * @param string The string to be snipped 191 * @return the snipped string 192 */ 193 static char *snip(char *string) 194 { 195 char *s; 196 197 /* 198 * This is always a 13 character buffer 199 * and it's not always terminated. 200 */ 201 string[12] = '\0'; 202 s = &string[strlen(string) - 1]; 203 204 while (s >= string && (isspace(*s) || *s == '\n' || *s == '\r' || 205 *s == '\0')) 206 *(s--) = '\0'; 207 208 return string; 209 } 210 211 /** 212 * Print an EDID monitor descriptor block 213 * 214 * @param monitor The EDID monitor descriptor block 215 * @have_timing Modifies to 1 if the desciptor contains timing info 216 */ 217 static void edid_print_dtd(struct edid_monitor_descriptor *monitor, 218 unsigned int *have_timing) 219 { 220 unsigned char *bytes = (unsigned char *)monitor; 221 struct edid_detailed_timing *timing = 222 (struct edid_detailed_timing *)monitor; 223 224 if (bytes[0] == 0 && bytes[1] == 0) { 225 if (monitor->type == EDID_MONITOR_DESCRIPTOR_SERIAL) 226 printf("Monitor serial number: %s\n", 227 snip(monitor->data.string)); 228 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_ASCII) 229 printf("Monitor ID: %s\n", 230 snip(monitor->data.string)); 231 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_NAME) 232 printf("Monitor name: %s\n", 233 snip(monitor->data.string)); 234 else if (monitor->type == EDID_MONITOR_DESCRIPTOR_RANGE) 235 printf("Monitor range limits, horizontal sync: " 236 "%d-%d kHz, vertical refresh: " 237 "%d-%d Hz, max pixel clock: " 238 "%d MHz\n", 239 monitor->data.range_data.horizontal_min, 240 monitor->data.range_data.horizontal_max, 241 monitor->data.range_data.vertical_min, 242 monitor->data.range_data.vertical_max, 243 monitor->data.range_data.pixel_clock_max * 10); 244 } else { 245 uint32_t pixclock, h_active, h_blanking, v_active, v_blanking; 246 uint32_t h_total, v_total, vfreq; 247 248 pixclock = EDID_DETAILED_TIMING_PIXEL_CLOCK(*timing); 249 h_active = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*timing); 250 h_blanking = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*timing); 251 v_active = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*timing); 252 v_blanking = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*timing); 253 254 h_total = h_active + h_blanking; 255 v_total = v_active + v_blanking; 256 if (v_total * h_total) 257 vfreq = pixclock / (v_total * h_total); 258 else 259 vfreq = 1; /* Error case */ 260 printf("\t%dx%d\%c\t%d Hz (detailed)\n", h_active, 261 v_active, h_active > 1000 ? ' ' : '\t', vfreq); 262 *have_timing = 1; 263 } 264 } 265 266 /** 267 * Get the manufacturer name from an EDID info. 268 * 269 * @param edid_info The EDID info to be printed 270 * @param name Returns the string of the manufacturer name 271 */ 272 static void edid_get_manufacturer_name(struct edid1_info *edid, char *name) 273 { 274 name[0] = EDID1_INFO_MANUFACTURER_NAME_CHAR1(*edid) + 'A' - 1; 275 name[1] = EDID1_INFO_MANUFACTURER_NAME_CHAR2(*edid) + 'A' - 1; 276 name[2] = EDID1_INFO_MANUFACTURER_NAME_CHAR3(*edid) + 'A' - 1; 277 name[3] = '\0'; 278 } 279 280 void edid_print_info(struct edid1_info *edid_info) 281 { 282 int i; 283 char manufacturer[4]; 284 unsigned int have_timing = 0; 285 uint32_t serial_number; 286 287 if (edid_check_info(edid_info)) { 288 printf("Not a valid EDID\n"); 289 return; 290 } 291 292 printf("EDID version: %d.%d\n", 293 edid_info->version, edid_info->revision); 294 295 printf("Product ID code: %04x\n", EDID1_INFO_PRODUCT_CODE(*edid_info)); 296 297 edid_get_manufacturer_name(edid_info, manufacturer); 298 printf("Manufacturer: %s\n", manufacturer); 299 300 serial_number = EDID1_INFO_SERIAL_NUMBER(*edid_info); 301 if (serial_number != 0xffffffff) { 302 if (strcmp(manufacturer, "MAG") == 0) 303 serial_number -= 0x7000000; 304 if (strcmp(manufacturer, "OQI") == 0) 305 serial_number -= 456150000; 306 if (strcmp(manufacturer, "VSC") == 0) 307 serial_number -= 640000000; 308 } 309 printf("Serial number: %08x\n", serial_number); 310 printf("Manufactured in week: %d year: %d\n", 311 edid_info->week, edid_info->year + 1990); 312 313 printf("Video input definition: %svoltage level %d%s%s%s%s%s\n", 314 EDID1_INFO_VIDEO_INPUT_DIGITAL(*edid_info) ? 315 "digital signal, " : "analog signal, ", 316 EDID1_INFO_VIDEO_INPUT_VOLTAGE_LEVEL(*edid_info), 317 EDID1_INFO_VIDEO_INPUT_BLANK_TO_BLACK(*edid_info) ? 318 ", blank to black" : "", 319 EDID1_INFO_VIDEO_INPUT_SEPARATE_SYNC(*edid_info) ? 320 ", separate sync" : "", 321 EDID1_INFO_VIDEO_INPUT_COMPOSITE_SYNC(*edid_info) ? 322 ", composite sync" : "", 323 EDID1_INFO_VIDEO_INPUT_SYNC_ON_GREEN(*edid_info) ? 324 ", sync on green" : "", 325 EDID1_INFO_VIDEO_INPUT_SERRATION_V(*edid_info) ? 326 ", serration v" : ""); 327 328 printf("Monitor is %s\n", 329 EDID1_INFO_FEATURE_RGB(*edid_info) ? "RGB" : "non-RGB"); 330 331 printf("Maximum visible display size: %d cm x %d cm\n", 332 edid_info->max_size_horizontal, 333 edid_info->max_size_vertical); 334 335 printf("Power management features: %s%s, %s%s, %s%s\n", 336 EDID1_INFO_FEATURE_ACTIVE_OFF(*edid_info) ? 337 "" : "no ", "active off", 338 EDID1_INFO_FEATURE_SUSPEND(*edid_info) ? "" : "no ", "suspend", 339 EDID1_INFO_FEATURE_STANDBY(*edid_info) ? "" : "no ", "standby"); 340 341 printf("Estabilished timings:\n"); 342 if (EDID1_INFO_ESTABLISHED_TIMING_720X400_70(*edid_info)) 343 printf("\t720x400\t\t70 Hz (VGA 640x400, IBM)\n"); 344 if (EDID1_INFO_ESTABLISHED_TIMING_720X400_88(*edid_info)) 345 printf("\t720x400\t\t88 Hz (XGA2)\n"); 346 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_60(*edid_info)) 347 printf("\t640x480\t\t60 Hz (VGA)\n"); 348 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_67(*edid_info)) 349 printf("\t640x480\t\t67 Hz (Mac II, Apple)\n"); 350 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_72(*edid_info)) 351 printf("\t640x480\t\t72 Hz (VESA)\n"); 352 if (EDID1_INFO_ESTABLISHED_TIMING_640X480_75(*edid_info)) 353 printf("\t640x480\t\t75 Hz (VESA)\n"); 354 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_56(*edid_info)) 355 printf("\t800x600\t\t56 Hz (VESA)\n"); 356 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_60(*edid_info)) 357 printf("\t800x600\t\t60 Hz (VESA)\n"); 358 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_72(*edid_info)) 359 printf("\t800x600\t\t72 Hz (VESA)\n"); 360 if (EDID1_INFO_ESTABLISHED_TIMING_800X600_75(*edid_info)) 361 printf("\t800x600\t\t75 Hz (VESA)\n"); 362 if (EDID1_INFO_ESTABLISHED_TIMING_832X624_75(*edid_info)) 363 printf("\t832x624\t\t75 Hz (Mac II)\n"); 364 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_87I(*edid_info)) 365 printf("\t1024x768\t87 Hz Interlaced (8514A)\n"); 366 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_60(*edid_info)) 367 printf("\t1024x768\t60 Hz (VESA)\n"); 368 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_70(*edid_info)) 369 printf("\t1024x768\t70 Hz (VESA)\n"); 370 if (EDID1_INFO_ESTABLISHED_TIMING_1024X768_75(*edid_info)) 371 printf("\t1024x768\t75 Hz (VESA)\n"); 372 if (EDID1_INFO_ESTABLISHED_TIMING_1280X1024_75(*edid_info)) 373 printf("\t1280x1024\t75 (VESA)\n"); 374 if (EDID1_INFO_ESTABLISHED_TIMING_1152X870_75(*edid_info)) 375 printf("\t1152x870\t75 (Mac II)\n"); 376 377 /* Standard timings. */ 378 printf("Standard timings:\n"); 379 for (i = 0; i < ARRAY_SIZE(edid_info->standard_timings); i++) { 380 unsigned int aspect = 10000; 381 unsigned int x, y; 382 unsigned char xres, vfreq; 383 384 xres = EDID1_INFO_STANDARD_TIMING_XRESOLUTION(*edid_info, i); 385 vfreq = EDID1_INFO_STANDARD_TIMING_VFREQ(*edid_info, i); 386 if ((xres != vfreq) || 387 ((xres != 0) && (xres != 1)) || 388 ((vfreq != 0) && (vfreq != 1))) { 389 switch (EDID1_INFO_STANDARD_TIMING_ASPECT(*edid_info, 390 i)) { 391 case ASPECT_625: 392 aspect = 6250; 393 break; 394 case ASPECT_75: 395 aspect = 7500; 396 break; 397 case ASPECT_8: 398 aspect = 8000; 399 break; 400 case ASPECT_5625: 401 aspect = 5625; 402 break; 403 } 404 x = (xres + 31) * 8; 405 y = x * aspect / 10000; 406 printf("\t%dx%d%c\t%d Hz\n", x, y, 407 x > 1000 ? ' ' : '\t', (vfreq & 0x3f) + 60); 408 have_timing = 1; 409 } 410 } 411 412 /* Detailed timing information. */ 413 for (i = 0; i < ARRAY_SIZE(edid_info->monitor_details.descriptor); 414 i++) { 415 edid_print_dtd(&edid_info->monitor_details.descriptor[i], 416 &have_timing); 417 } 418 419 if (!have_timing) 420 printf("\tNone\n"); 421 } 422