edid.c 12 KB

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