drm_edid.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Copyright (c) 2006 Luc Verhaegen (quirks list)
  3. * Copyright (c) 2007-2008 Intel Corporation
  4. * Jesse Barnes <jesse.barnes@intel.com>
  5. *
  6. * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
  7. * FB layer.
  8. * Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  14. * and/or sell copies of the Software, and to permit persons to whom the
  15. * Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice (including the
  18. * next paragraph) shall be included in all copies or substantial portions
  19. * of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  24. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  27. * DEALINGS IN THE SOFTWARE.
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/i2c.h>
  31. #include <linux/i2c-algo-bit.h>
  32. #include "drmP.h"
  33. #include "drm_edid.h"
  34. /*
  35. * TODO:
  36. * - support EDID 1.4 (incl. CE blocks)
  37. */
  38. /*
  39. * EDID blocks out in the wild have a variety of bugs, try to collect
  40. * them here (note that userspace may work around broken monitors first,
  41. * but fixes should make their way here so that the kernel "just works"
  42. * on as many displays as possible).
  43. */
  44. /* First detailed mode wrong, use largest 60Hz mode */
  45. #define EDID_QUIRK_PREFER_LARGE_60 (1 << 0)
  46. /* Reported 135MHz pixel clock is too high, needs adjustment */
  47. #define EDID_QUIRK_135_CLOCK_TOO_HIGH (1 << 1)
  48. /* Prefer the largest mode at 75 Hz */
  49. #define EDID_QUIRK_PREFER_LARGE_75 (1 << 2)
  50. /* Detail timing is in cm not mm */
  51. #define EDID_QUIRK_DETAILED_IN_CM (1 << 3)
  52. /* Detailed timing descriptors have bogus size values, so just take the
  53. * maximum size and use that.
  54. */
  55. #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE (1 << 4)
  56. /* Monitor forgot to set the first detailed is preferred bit. */
  57. #define EDID_QUIRK_FIRST_DETAILED_PREFERRED (1 << 5)
  58. /* use +hsync +vsync for detailed mode */
  59. #define EDID_QUIRK_DETAILED_SYNC_PP (1 << 6)
  60. static struct edid_quirk {
  61. char *vendor;
  62. int product_id;
  63. u32 quirks;
  64. } edid_quirk_list[] = {
  65. /* Acer AL1706 */
  66. { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
  67. /* Acer F51 */
  68. { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
  69. /* Unknown Acer */
  70. { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  71. /* Belinea 10 15 55 */
  72. { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
  73. { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
  74. /* Envision Peripherals, Inc. EN-7100e */
  75. { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
  76. /* Funai Electronics PM36B */
  77. { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
  78. EDID_QUIRK_DETAILED_IN_CM },
  79. /* LG Philips LCD LP154W01-A5 */
  80. { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
  81. { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
  82. /* Philips 107p5 CRT */
  83. { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  84. /* Proview AY765C */
  85. { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  86. /* Samsung SyncMaster 205BW. Note: irony */
  87. { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
  88. /* Samsung SyncMaster 22[5-6]BW */
  89. { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
  90. { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
  91. };
  92. /* Valid EDID header has these bytes */
  93. static u8 edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
  94. /**
  95. * edid_is_valid - sanity check EDID data
  96. * @edid: EDID data
  97. *
  98. * Sanity check the EDID block by looking at the header, the version number
  99. * and the checksum. Return 0 if the EDID doesn't check out, or 1 if it's
  100. * valid.
  101. */
  102. static bool edid_is_valid(struct edid *edid)
  103. {
  104. int i;
  105. u8 csum = 0;
  106. u8 *raw_edid = (u8 *)edid;
  107. if (memcmp(edid->header, edid_header, sizeof(edid_header)))
  108. goto bad;
  109. if (edid->version != 1) {
  110. DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
  111. goto bad;
  112. }
  113. if (edid->revision > 4)
  114. DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
  115. for (i = 0; i < EDID_LENGTH; i++)
  116. csum += raw_edid[i];
  117. if (csum) {
  118. DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
  119. goto bad;
  120. }
  121. return 1;
  122. bad:
  123. if (raw_edid) {
  124. DRM_ERROR("Raw EDID:\n");
  125. print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
  126. printk("\n");
  127. }
  128. return 0;
  129. }
  130. /**
  131. * edid_vendor - match a string against EDID's obfuscated vendor field
  132. * @edid: EDID to match
  133. * @vendor: vendor string
  134. *
  135. * Returns true if @vendor is in @edid, false otherwise
  136. */
  137. static bool edid_vendor(struct edid *edid, char *vendor)
  138. {
  139. char edid_vendor[3];
  140. edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
  141. edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
  142. ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
  143. edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
  144. return !strncmp(edid_vendor, vendor, 3);
  145. }
  146. /**
  147. * edid_get_quirks - return quirk flags for a given EDID
  148. * @edid: EDID to process
  149. *
  150. * This tells subsequent routines what fixes they need to apply.
  151. */
  152. static u32 edid_get_quirks(struct edid *edid)
  153. {
  154. struct edid_quirk *quirk;
  155. int i;
  156. for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
  157. quirk = &edid_quirk_list[i];
  158. if (edid_vendor(edid, quirk->vendor) &&
  159. (EDID_PRODUCT_ID(edid) == quirk->product_id))
  160. return quirk->quirks;
  161. }
  162. return 0;
  163. }
  164. #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
  165. #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
  166. /**
  167. * edid_fixup_preferred - set preferred modes based on quirk list
  168. * @connector: has mode list to fix up
  169. * @quirks: quirks list
  170. *
  171. * Walk the mode list for @connector, clearing the preferred status
  172. * on existing modes and setting it anew for the right mode ala @quirks.
  173. */
  174. static void edid_fixup_preferred(struct drm_connector *connector,
  175. u32 quirks)
  176. {
  177. struct drm_display_mode *t, *cur_mode, *preferred_mode;
  178. int target_refresh = 0;
  179. if (list_empty(&connector->probed_modes))
  180. return;
  181. if (quirks & EDID_QUIRK_PREFER_LARGE_60)
  182. target_refresh = 60;
  183. if (quirks & EDID_QUIRK_PREFER_LARGE_75)
  184. target_refresh = 75;
  185. preferred_mode = list_first_entry(&connector->probed_modes,
  186. struct drm_display_mode, head);
  187. list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
  188. cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  189. if (cur_mode == preferred_mode)
  190. continue;
  191. /* Largest mode is preferred */
  192. if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
  193. preferred_mode = cur_mode;
  194. /* At a given size, try to get closest to target refresh */
  195. if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
  196. MODE_REFRESH_DIFF(cur_mode, target_refresh) <
  197. MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
  198. preferred_mode = cur_mode;
  199. }
  200. }
  201. preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
  202. }
  203. /**
  204. * drm_mode_std - convert standard mode info (width, height, refresh) into mode
  205. * @t: standard timing params
  206. *
  207. * Take the standard timing params (in this case width, aspect, and refresh)
  208. * and convert them into a real mode using CVT.
  209. *
  210. * Punts for now, but should eventually use the FB layer's CVT based mode
  211. * generation code.
  212. */
  213. struct drm_display_mode *drm_mode_std(struct drm_device *dev,
  214. struct std_timing *t)
  215. {
  216. struct drm_display_mode *mode;
  217. int hsize = t->hsize * 8 + 248, vsize;
  218. mode = drm_mode_create(dev);
  219. if (!mode)
  220. return NULL;
  221. if (t->aspect_ratio == 0)
  222. vsize = (hsize * 10) / 16;
  223. else if (t->aspect_ratio == 1)
  224. vsize = (hsize * 3) / 4;
  225. else if (t->aspect_ratio == 2)
  226. vsize = (hsize * 4) / 5;
  227. else
  228. vsize = (hsize * 9) / 16;
  229. drm_mode_set_name(mode);
  230. return mode;
  231. }
  232. /**
  233. * drm_mode_detailed - create a new mode from an EDID detailed timing section
  234. * @dev: DRM device (needed to create new mode)
  235. * @edid: EDID block
  236. * @timing: EDID detailed timing info
  237. * @quirks: quirks to apply
  238. *
  239. * An EDID detailed timing block contains enough info for us to create and
  240. * return a new struct drm_display_mode.
  241. */
  242. static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
  243. struct edid *edid,
  244. struct detailed_timing *timing,
  245. u32 quirks)
  246. {
  247. struct drm_display_mode *mode;
  248. struct detailed_pixel_timing *pt = &timing->data.pixel_data;
  249. if (pt->stereo) {
  250. printk(KERN_WARNING "stereo mode not supported\n");
  251. return NULL;
  252. }
  253. if (!pt->separate_sync) {
  254. printk(KERN_WARNING "integrated sync not supported\n");
  255. return NULL;
  256. }
  257. mode = drm_mode_create(dev);
  258. if (!mode)
  259. return NULL;
  260. mode->type = DRM_MODE_TYPE_DRIVER;
  261. if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
  262. timing->pixel_clock = 1088;
  263. mode->clock = timing->pixel_clock * 10;
  264. mode->hdisplay = (pt->hactive_hi << 8) | pt->hactive_lo;
  265. mode->hsync_start = mode->hdisplay + ((pt->hsync_offset_hi << 8) |
  266. pt->hsync_offset_lo);
  267. mode->hsync_end = mode->hsync_start +
  268. ((pt->hsync_pulse_width_hi << 8) |
  269. pt->hsync_pulse_width_lo);
  270. mode->htotal = mode->hdisplay + ((pt->hblank_hi << 8) | pt->hblank_lo);
  271. mode->vdisplay = (pt->vactive_hi << 8) | pt->vactive_lo;
  272. mode->vsync_start = mode->vdisplay + ((pt->vsync_offset_hi << 4) |
  273. pt->vsync_offset_lo);
  274. mode->vsync_end = mode->vsync_start +
  275. ((pt->vsync_pulse_width_hi << 4) |
  276. pt->vsync_pulse_width_lo);
  277. mode->vtotal = mode->vdisplay + ((pt->vblank_hi << 8) | pt->vblank_lo);
  278. drm_mode_set_name(mode);
  279. if (pt->interlaced)
  280. mode->flags |= DRM_MODE_FLAG_INTERLACE;
  281. if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
  282. pt->hsync_positive = 1;
  283. pt->vsync_positive = 1;
  284. }
  285. mode->flags |= pt->hsync_positive ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  286. mode->flags |= pt->vsync_positive ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  287. mode->width_mm = pt->width_mm_lo | (pt->width_mm_hi << 8);
  288. mode->height_mm = pt->height_mm_lo | (pt->height_mm_hi << 8);
  289. if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
  290. mode->width_mm *= 10;
  291. mode->height_mm *= 10;
  292. }
  293. if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
  294. mode->width_mm = edid->width_cm * 10;
  295. mode->height_mm = edid->height_cm * 10;
  296. }
  297. return mode;
  298. }
  299. /*
  300. * Detailed mode info for the EDID "established modes" data to use.
  301. */
  302. static struct drm_display_mode edid_est_modes[] = {
  303. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  304. 968, 1056, 0, 600, 601, 605, 628, 0,
  305. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
  306. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
  307. 896, 1024, 0, 600, 601, 603, 625, 0,
  308. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
  309. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
  310. 720, 840, 0, 480, 481, 484, 500, 0,
  311. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
  312. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
  313. 704, 832, 0, 480, 489, 491, 520, 0,
  314. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
  315. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
  316. 768, 864, 0, 480, 483, 486, 525, 0,
  317. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
  318. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
  319. 752, 800, 0, 480, 490, 492, 525, 0,
  320. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
  321. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
  322. 846, 900, 0, 400, 421, 423, 449, 0,
  323. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
  324. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
  325. 846, 900, 0, 400, 412, 414, 449, 0,
  326. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
  327. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
  328. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  329. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
  330. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
  331. 1136, 1312, 0, 768, 769, 772, 800, 0,
  332. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
  333. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
  334. 1184, 1328, 0, 768, 771, 777, 806, 0,
  335. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
  336. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  337. 1184, 1344, 0, 768, 771, 777, 806, 0,
  338. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
  339. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
  340. 1208, 1264, 0, 768, 768, 776, 817, 0,
  341. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
  342. { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
  343. 928, 1152, 0, 624, 625, 628, 667, 0,
  344. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
  345. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
  346. 896, 1056, 0, 600, 601, 604, 625, 0,
  347. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
  348. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
  349. 976, 1040, 0, 600, 637, 643, 666, 0,
  350. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
  351. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  352. 1344, 1600, 0, 864, 865, 868, 900, 0,
  353. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
  354. };
  355. #define EDID_EST_TIMINGS 16
  356. #define EDID_STD_TIMINGS 8
  357. #define EDID_DETAILED_TIMINGS 4
  358. /**
  359. * add_established_modes - get est. modes from EDID and add them
  360. * @edid: EDID block to scan
  361. *
  362. * Each EDID block contains a bitmap of the supported "established modes" list
  363. * (defined above). Tease them out and add them to the global modes list.
  364. */
  365. static int add_established_modes(struct drm_connector *connector, struct edid *edid)
  366. {
  367. struct drm_device *dev = connector->dev;
  368. unsigned long est_bits = edid->established_timings.t1 |
  369. (edid->established_timings.t2 << 8) |
  370. ((edid->established_timings.mfg_rsvd & 0x80) << 9);
  371. int i, modes = 0;
  372. for (i = 0; i <= EDID_EST_TIMINGS; i++)
  373. if (est_bits & (1<<i)) {
  374. struct drm_display_mode *newmode;
  375. newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
  376. if (newmode) {
  377. drm_mode_probed_add(connector, newmode);
  378. modes++;
  379. }
  380. }
  381. return modes;
  382. }
  383. /**
  384. * add_standard_modes - get std. modes from EDID and add them
  385. * @edid: EDID block to scan
  386. *
  387. * Standard modes can be calculated using the CVT standard. Grab them from
  388. * @edid, calculate them, and add them to the list.
  389. */
  390. static int add_standard_modes(struct drm_connector *connector, struct edid *edid)
  391. {
  392. struct drm_device *dev = connector->dev;
  393. int i, modes = 0;
  394. for (i = 0; i < EDID_STD_TIMINGS; i++) {
  395. struct std_timing *t = &edid->standard_timings[i];
  396. struct drm_display_mode *newmode;
  397. /* If std timings bytes are 1, 1 it's empty */
  398. if (t->hsize == 1 && (t->aspect_ratio | t->vfreq) == 1)
  399. continue;
  400. newmode = drm_mode_std(dev, &edid->standard_timings[i]);
  401. if (newmode) {
  402. drm_mode_probed_add(connector, newmode);
  403. modes++;
  404. }
  405. }
  406. return modes;
  407. }
  408. /**
  409. * add_detailed_modes - get detailed mode info from EDID data
  410. * @connector: attached connector
  411. * @edid: EDID block to scan
  412. * @quirks: quirks to apply
  413. *
  414. * Some of the detailed timing sections may contain mode information. Grab
  415. * it and add it to the list.
  416. */
  417. static int add_detailed_info(struct drm_connector *connector,
  418. struct edid *edid, u32 quirks)
  419. {
  420. struct drm_device *dev = connector->dev;
  421. int i, j, modes = 0;
  422. for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
  423. struct detailed_timing *timing = &edid->detailed_timings[i];
  424. struct detailed_non_pixel *data = &timing->data.other_data;
  425. struct drm_display_mode *newmode;
  426. /* EDID up to and including 1.2 may put monitor info here */
  427. if (edid->version == 1 && edid->revision < 3)
  428. continue;
  429. /* Detailed mode timing */
  430. if (timing->pixel_clock) {
  431. newmode = drm_mode_detailed(dev, edid, timing, quirks);
  432. if (!newmode)
  433. continue;
  434. /* First detailed mode is preferred */
  435. if (i == 0 && edid->preferred_timing)
  436. newmode->type |= DRM_MODE_TYPE_PREFERRED;
  437. drm_mode_probed_add(connector, newmode);
  438. modes++;
  439. continue;
  440. }
  441. /* Other timing or info */
  442. switch (data->type) {
  443. case EDID_DETAIL_MONITOR_SERIAL:
  444. break;
  445. case EDID_DETAIL_MONITOR_STRING:
  446. break;
  447. case EDID_DETAIL_MONITOR_RANGE:
  448. /* Get monitor range data */
  449. break;
  450. case EDID_DETAIL_MONITOR_NAME:
  451. break;
  452. case EDID_DETAIL_MONITOR_CPDATA:
  453. break;
  454. case EDID_DETAIL_STD_MODES:
  455. /* Five modes per detailed section */
  456. for (j = 0; j < 5; i++) {
  457. struct std_timing *std;
  458. struct drm_display_mode *newmode;
  459. std = &data->data.timings[j];
  460. newmode = drm_mode_std(dev, std);
  461. if (newmode) {
  462. drm_mode_probed_add(connector, newmode);
  463. modes++;
  464. }
  465. }
  466. break;
  467. default:
  468. break;
  469. }
  470. }
  471. return modes;
  472. }
  473. #define DDC_ADDR 0x50
  474. /**
  475. * Get EDID information via I2C.
  476. *
  477. * \param adapter : i2c device adaptor
  478. * \param buf : EDID data buffer to be filled
  479. * \param len : EDID data buffer length
  480. * \return 0 on success or -1 on failure.
  481. *
  482. * Try to fetch EDID information by calling i2c driver function.
  483. */
  484. int drm_do_probe_ddc_edid(struct i2c_adapter *adapter,
  485. unsigned char *buf, int len)
  486. {
  487. unsigned char start = 0x0;
  488. struct i2c_msg msgs[] = {
  489. {
  490. .addr = DDC_ADDR,
  491. .flags = 0,
  492. .len = 1,
  493. .buf = &start,
  494. }, {
  495. .addr = DDC_ADDR,
  496. .flags = I2C_M_RD,
  497. .len = len,
  498. .buf = buf,
  499. }
  500. };
  501. if (i2c_transfer(adapter, msgs, 2) == 2)
  502. return 0;
  503. dev_info(&adapter->dev, "unable to read EDID block.\n");
  504. return -1;
  505. }
  506. EXPORT_SYMBOL(drm_do_probe_ddc_edid);
  507. /**
  508. * Get EDID information.
  509. *
  510. * \param adapter : i2c device adaptor.
  511. * \param buf : EDID data buffer to be filled
  512. * \param len : EDID data buffer length
  513. * \return 0 on success or -1 on failure.
  514. *
  515. * Initialize DDC, then fetch EDID information
  516. * by calling drm_do_probe_ddc_edid function.
  517. */
  518. static int drm_ddc_read(struct i2c_adapter *adapter,
  519. unsigned char *buf, int len)
  520. {
  521. struct i2c_algo_bit_data *algo_data = adapter->algo_data;
  522. int i, j;
  523. int ret = -1;
  524. algo_data->setscl(algo_data->data, 1);
  525. for (i = 0; i < 1; i++) {
  526. /* For some old monitors we need the
  527. * following process to initialize/stop DDC
  528. */
  529. algo_data->setsda(algo_data->data, 1);
  530. msleep(13);
  531. algo_data->setscl(algo_data->data, 1);
  532. for (j = 0; j < 5; j++) {
  533. msleep(10);
  534. if (algo_data->getscl(algo_data->data))
  535. break;
  536. }
  537. if (j == 5)
  538. continue;
  539. algo_data->setsda(algo_data->data, 0);
  540. msleep(15);
  541. algo_data->setscl(algo_data->data, 0);
  542. msleep(15);
  543. algo_data->setsda(algo_data->data, 1);
  544. msleep(15);
  545. /* Do the real work */
  546. ret = drm_do_probe_ddc_edid(adapter, buf, len);
  547. algo_data->setsda(algo_data->data, 0);
  548. algo_data->setscl(algo_data->data, 0);
  549. msleep(15);
  550. algo_data->setscl(algo_data->data, 1);
  551. for (j = 0; j < 10; j++) {
  552. msleep(10);
  553. if (algo_data->getscl(algo_data->data))
  554. break;
  555. }
  556. algo_data->setsda(algo_data->data, 1);
  557. msleep(15);
  558. algo_data->setscl(algo_data->data, 0);
  559. algo_data->setsda(algo_data->data, 0);
  560. if (ret == 0)
  561. break;
  562. }
  563. /* Release the DDC lines when done or the Apple Cinema HD display
  564. * will switch off
  565. */
  566. algo_data->setsda(algo_data->data, 1);
  567. algo_data->setscl(algo_data->data, 1);
  568. return ret;
  569. }
  570. static int drm_ddc_read_edid(struct drm_connector *connector,
  571. struct i2c_adapter *adapter,
  572. char *buf, int len)
  573. {
  574. int ret;
  575. ret = drm_ddc_read(adapter, buf, len);
  576. if (ret != 0) {
  577. dev_info(&connector->dev->pdev->dev, "%s: no EDID data\n",
  578. drm_get_connector_name(connector));
  579. goto end;
  580. }
  581. if (!edid_is_valid((struct edid *)buf)) {
  582. dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
  583. drm_get_connector_name(connector));
  584. ret = -1;
  585. }
  586. end:
  587. return ret;
  588. }
  589. #define MAX_EDID_EXT_NUM 4
  590. /**
  591. * drm_get_edid - get EDID data, if available
  592. * @connector: connector we're probing
  593. * @adapter: i2c adapter to use for DDC
  594. *
  595. * Poke the given connector's i2c channel to grab EDID data if possible.
  596. *
  597. * Return edid data or NULL if we couldn't find any.
  598. */
  599. struct edid *drm_get_edid(struct drm_connector *connector,
  600. struct i2c_adapter *adapter)
  601. {
  602. int ret;
  603. struct edid *edid;
  604. edid = kmalloc(EDID_LENGTH * (MAX_EDID_EXT_NUM + 1),
  605. GFP_KERNEL);
  606. if (edid == NULL) {
  607. dev_warn(&connector->dev->pdev->dev,
  608. "Failed to allocate EDID\n");
  609. goto end;
  610. }
  611. /* Read first EDID block */
  612. ret = drm_ddc_read_edid(connector, adapter,
  613. (unsigned char *)edid, EDID_LENGTH);
  614. if (ret != 0)
  615. goto clean_up;
  616. /* There are EDID extensions to be read */
  617. if (edid->extensions != 0) {
  618. int edid_ext_num = edid->extensions;
  619. if (edid_ext_num > MAX_EDID_EXT_NUM) {
  620. dev_warn(&connector->dev->pdev->dev,
  621. "The number of extension(%d) is "
  622. "over max (%d), actually read number (%d)\n",
  623. edid_ext_num, MAX_EDID_EXT_NUM,
  624. MAX_EDID_EXT_NUM);
  625. /* Reset EDID extension number to be read */
  626. edid_ext_num = MAX_EDID_EXT_NUM;
  627. }
  628. /* Read EDID including extensions too */
  629. ret = drm_ddc_read_edid(connector, adapter, (char *)edid,
  630. EDID_LENGTH * (edid_ext_num + 1));
  631. if (ret != 0)
  632. goto clean_up;
  633. }
  634. connector->display_info.raw_edid = (char *)edid;
  635. goto end;
  636. clean_up:
  637. kfree(edid);
  638. edid = NULL;
  639. end:
  640. return edid;
  641. }
  642. EXPORT_SYMBOL(drm_get_edid);
  643. #define HDMI_IDENTIFIER 0x000C03
  644. #define VENDOR_BLOCK 0x03
  645. /**
  646. * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
  647. * @edid: monitor EDID information
  648. *
  649. * Parse the CEA extension according to CEA-861-B.
  650. * Return true if HDMI, false if not or unknown.
  651. */
  652. bool drm_detect_hdmi_monitor(struct edid *edid)
  653. {
  654. char *edid_ext = NULL;
  655. int i, hdmi_id, edid_ext_num;
  656. int start_offset, end_offset;
  657. bool is_hdmi = false;
  658. /* No EDID or EDID extensions */
  659. if (edid == NULL || edid->extensions == 0)
  660. goto end;
  661. /* Chose real EDID extension number */
  662. edid_ext_num = edid->extensions > MAX_EDID_EXT_NUM ?
  663. MAX_EDID_EXT_NUM : edid->extensions;
  664. /* Find CEA extension */
  665. for (i = 0; i < edid_ext_num; i++) {
  666. edid_ext = (char *)edid + EDID_LENGTH * (i + 1);
  667. /* This block is CEA extension */
  668. if (edid_ext[0] == 0x02)
  669. break;
  670. }
  671. if (i == edid_ext_num)
  672. goto end;
  673. /* Data block offset in CEA extension block */
  674. start_offset = 4;
  675. end_offset = edid_ext[2];
  676. /*
  677. * Because HDMI identifier is in Vendor Specific Block,
  678. * search it from all data blocks of CEA extension.
  679. */
  680. for (i = start_offset; i < end_offset;
  681. /* Increased by data block len */
  682. i += ((edid_ext[i] & 0x1f) + 1)) {
  683. /* Find vendor specific block */
  684. if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
  685. hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
  686. edid_ext[i + 3] << 16;
  687. /* Find HDMI identifier */
  688. if (hdmi_id == HDMI_IDENTIFIER)
  689. is_hdmi = true;
  690. break;
  691. }
  692. }
  693. end:
  694. return is_hdmi;
  695. }
  696. EXPORT_SYMBOL(drm_detect_hdmi_monitor);
  697. /**
  698. * drm_add_edid_modes - add modes from EDID data, if available
  699. * @connector: connector we're probing
  700. * @edid: edid data
  701. *
  702. * Add the specified modes to the connector's mode list.
  703. *
  704. * Return number of modes added or 0 if we couldn't find any.
  705. */
  706. int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
  707. {
  708. int num_modes = 0;
  709. u32 quirks;
  710. if (edid == NULL) {
  711. return 0;
  712. }
  713. if (!edid_is_valid(edid)) {
  714. dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
  715. drm_get_connector_name(connector));
  716. return 0;
  717. }
  718. quirks = edid_get_quirks(edid);
  719. num_modes += add_established_modes(connector, edid);
  720. num_modes += add_standard_modes(connector, edid);
  721. num_modes += add_detailed_info(connector, edid, quirks);
  722. if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
  723. edid_fixup_preferred(connector, quirks);
  724. connector->display_info.serration_vsync = edid->serration_vsync;
  725. connector->display_info.sync_on_green = edid->sync_on_green;
  726. connector->display_info.composite_sync = edid->composite_sync;
  727. connector->display_info.separate_syncs = edid->separate_syncs;
  728. connector->display_info.blank_to_black = edid->blank_to_black;
  729. connector->display_info.video_level = edid->video_level;
  730. connector->display_info.digital = edid->digital;
  731. connector->display_info.width_mm = edid->width_cm * 10;
  732. connector->display_info.height_mm = edid->height_cm * 10;
  733. connector->display_info.gamma = edid->gamma;
  734. connector->display_info.gtf_supported = edid->default_gtf;
  735. connector->display_info.standard_color = edid->standard_color;
  736. connector->display_info.display_type = edid->display_type;
  737. connector->display_info.active_off_supported = edid->pm_active_off;
  738. connector->display_info.suspend_supported = edid->pm_suspend;
  739. connector->display_info.standby_supported = edid->pm_standby;
  740. connector->display_info.gamma = edid->gamma;
  741. return num_modes;
  742. }
  743. EXPORT_SYMBOL(drm_add_edid_modes);