edid.c 15 KB

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