drm_edid.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 <= 0 || edid->revision > 3) {
  114. DRM_ERROR("EDID has minor version %d, which is not between 0-3\n", edid->revision);
  115. goto bad;
  116. }
  117. for (i = 0; i < EDID_LENGTH; i++)
  118. csum += raw_edid[i];
  119. if (csum) {
  120. DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
  121. goto bad;
  122. }
  123. return 1;
  124. bad:
  125. if (raw_edid) {
  126. DRM_ERROR("Raw EDID:\n");
  127. print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
  128. printk("\n");
  129. }
  130. return 0;
  131. }
  132. /**
  133. * edid_vendor - match a string against EDID's obfuscated vendor field
  134. * @edid: EDID to match
  135. * @vendor: vendor string
  136. *
  137. * Returns true if @vendor is in @edid, false otherwise
  138. */
  139. static bool edid_vendor(struct edid *edid, char *vendor)
  140. {
  141. char edid_vendor[3];
  142. edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
  143. edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
  144. ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
  145. edid_vendor[2] = (edid->mfg_id[2] & 0x1f) + '@';
  146. return !strncmp(edid_vendor, vendor, 3);
  147. }
  148. /**
  149. * edid_get_quirks - return quirk flags for a given EDID
  150. * @edid: EDID to process
  151. *
  152. * This tells subsequent routines what fixes they need to apply.
  153. */
  154. static u32 edid_get_quirks(struct edid *edid)
  155. {
  156. struct edid_quirk *quirk;
  157. int i;
  158. for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
  159. quirk = &edid_quirk_list[i];
  160. if (edid_vendor(edid, quirk->vendor) &&
  161. (EDID_PRODUCT_ID(edid) == quirk->product_id))
  162. return quirk->quirks;
  163. }
  164. return 0;
  165. }
  166. #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
  167. #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
  168. /**
  169. * edid_fixup_preferred - set preferred modes based on quirk list
  170. * @connector: has mode list to fix up
  171. * @quirks: quirks list
  172. *
  173. * Walk the mode list for @connector, clearing the preferred status
  174. * on existing modes and setting it anew for the right mode ala @quirks.
  175. */
  176. static void edid_fixup_preferred(struct drm_connector *connector,
  177. u32 quirks)
  178. {
  179. struct drm_display_mode *t, *cur_mode, *preferred_mode;
  180. int target_refresh = 0;
  181. if (list_empty(&connector->probed_modes))
  182. return;
  183. if (quirks & EDID_QUIRK_PREFER_LARGE_60)
  184. target_refresh = 60;
  185. if (quirks & EDID_QUIRK_PREFER_LARGE_75)
  186. target_refresh = 75;
  187. preferred_mode = list_first_entry(&connector->probed_modes,
  188. struct drm_display_mode, head);
  189. list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
  190. cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  191. if (cur_mode == preferred_mode)
  192. continue;
  193. /* Largest mode is preferred */
  194. if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
  195. preferred_mode = cur_mode;
  196. /* At a given size, try to get closest to target refresh */
  197. if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
  198. MODE_REFRESH_DIFF(cur_mode, target_refresh) <
  199. MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
  200. preferred_mode = cur_mode;
  201. }
  202. }
  203. preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
  204. }
  205. /**
  206. * drm_mode_std - convert standard mode info (width, height, refresh) into mode
  207. * @t: standard timing params
  208. *
  209. * Take the standard timing params (in this case width, aspect, and refresh)
  210. * and convert them into a real mode using CVT.
  211. *
  212. * Punts for now, but should eventually use the FB layer's CVT based mode
  213. * generation code.
  214. */
  215. struct drm_display_mode *drm_mode_std(struct drm_device *dev,
  216. struct std_timing *t)
  217. {
  218. struct drm_display_mode *mode;
  219. int hsize = t->hsize * 8 + 248, vsize;
  220. mode = drm_mode_create(dev);
  221. if (!mode)
  222. return NULL;
  223. if (t->aspect_ratio == 0)
  224. vsize = (hsize * 10) / 16;
  225. else if (t->aspect_ratio == 1)
  226. vsize = (hsize * 3) / 4;
  227. else if (t->aspect_ratio == 2)
  228. vsize = (hsize * 4) / 5;
  229. else
  230. vsize = (hsize * 9) / 16;
  231. drm_mode_set_name(mode);
  232. return mode;
  233. }
  234. /**
  235. * drm_mode_detailed - create a new mode from an EDID detailed timing section
  236. * @dev: DRM device (needed to create new mode)
  237. * @edid: EDID block
  238. * @timing: EDID detailed timing info
  239. * @quirks: quirks to apply
  240. *
  241. * An EDID detailed timing block contains enough info for us to create and
  242. * return a new struct drm_display_mode.
  243. */
  244. static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
  245. struct edid *edid,
  246. struct detailed_timing *timing,
  247. u32 quirks)
  248. {
  249. struct drm_display_mode *mode;
  250. struct detailed_pixel_timing *pt = &timing->data.pixel_data;
  251. if (pt->stereo) {
  252. printk(KERN_WARNING "stereo mode not supported\n");
  253. return NULL;
  254. }
  255. if (!pt->separate_sync) {
  256. printk(KERN_WARNING "integrated sync not supported\n");
  257. return NULL;
  258. }
  259. mode = drm_mode_create(dev);
  260. if (!mode)
  261. return NULL;
  262. mode->type = DRM_MODE_TYPE_DRIVER;
  263. if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
  264. timing->pixel_clock = 1088;
  265. mode->clock = timing->pixel_clock * 10;
  266. mode->hdisplay = (pt->hactive_hi << 8) | pt->hactive_lo;
  267. mode->hsync_start = mode->hdisplay + ((pt->hsync_offset_hi << 8) |
  268. pt->hsync_offset_lo);
  269. mode->hsync_end = mode->hsync_start +
  270. ((pt->hsync_pulse_width_hi << 8) |
  271. pt->hsync_pulse_width_lo);
  272. mode->htotal = mode->hdisplay + ((pt->hblank_hi << 8) | pt->hblank_lo);
  273. mode->vdisplay = (pt->vactive_hi << 8) | pt->vactive_lo;
  274. mode->vsync_start = mode->vdisplay + ((pt->vsync_offset_hi << 8) |
  275. pt->vsync_offset_lo);
  276. mode->vsync_end = mode->vsync_start +
  277. ((pt->vsync_pulse_width_hi << 8) |
  278. pt->vsync_pulse_width_lo);
  279. mode->vtotal = mode->vdisplay + ((pt->vblank_hi << 8) | pt->vblank_lo);
  280. drm_mode_set_name(mode);
  281. if (pt->interlaced)
  282. mode->flags |= DRM_MODE_FLAG_INTERLACE;
  283. if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
  284. pt->hsync_positive = 1;
  285. pt->vsync_positive = 1;
  286. }
  287. mode->flags |= pt->hsync_positive ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  288. mode->flags |= pt->vsync_positive ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  289. mode->width_mm = pt->width_mm_lo | (pt->width_mm_hi << 8);
  290. mode->height_mm = pt->height_mm_lo | (pt->height_mm_hi << 8);
  291. if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
  292. mode->width_mm *= 10;
  293. mode->height_mm *= 10;
  294. }
  295. if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
  296. mode->width_mm = edid->width_cm * 10;
  297. mode->height_mm = edid->height_cm * 10;
  298. }
  299. return mode;
  300. }
  301. /*
  302. * Detailed mode info for the EDID "established modes" data to use.
  303. */
  304. static struct drm_display_mode edid_est_modes[] = {
  305. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  306. 968, 1056, 0, 600, 601, 605, 628, 0,
  307. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
  308. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
  309. 896, 1024, 0, 600, 601, 603, 625, 0,
  310. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
  311. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
  312. 720, 840, 0, 480, 481, 484, 500, 0,
  313. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
  314. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
  315. 704, 832, 0, 480, 489, 491, 520, 0,
  316. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
  317. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
  318. 768, 864, 0, 480, 483, 486, 525, 0,
  319. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
  320. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
  321. 752, 800, 0, 480, 490, 492, 525, 0,
  322. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
  323. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
  324. 846, 900, 0, 400, 421, 423, 449, 0,
  325. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
  326. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
  327. 846, 900, 0, 400, 412, 414, 449, 0,
  328. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
  329. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
  330. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  331. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
  332. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
  333. 1136, 1312, 0, 768, 769, 772, 800, 0,
  334. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
  335. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
  336. 1184, 1328, 0, 768, 771, 777, 806, 0,
  337. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
  338. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  339. 1184, 1344, 0, 768, 771, 777, 806, 0,
  340. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
  341. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
  342. 1208, 1264, 0, 768, 768, 776, 817, 0,
  343. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
  344. { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
  345. 928, 1152, 0, 624, 625, 628, 667, 0,
  346. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
  347. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
  348. 896, 1056, 0, 600, 601, 604, 625, 0,
  349. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
  350. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
  351. 976, 1040, 0, 600, 637, 643, 666, 0,
  352. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
  353. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  354. 1344, 1600, 0, 864, 865, 868, 900, 0,
  355. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
  356. };
  357. #define EDID_EST_TIMINGS 16
  358. #define EDID_STD_TIMINGS 8
  359. #define EDID_DETAILED_TIMINGS 4
  360. /**
  361. * add_established_modes - get est. modes from EDID and add them
  362. * @edid: EDID block to scan
  363. *
  364. * Each EDID block contains a bitmap of the supported "established modes" list
  365. * (defined above). Tease them out and add them to the global modes list.
  366. */
  367. static int add_established_modes(struct drm_connector *connector, struct edid *edid)
  368. {
  369. struct drm_device *dev = connector->dev;
  370. unsigned long est_bits = edid->established_timings.t1 |
  371. (edid->established_timings.t2 << 8) |
  372. ((edid->established_timings.mfg_rsvd & 0x80) << 9);
  373. int i, modes = 0;
  374. for (i = 0; i <= EDID_EST_TIMINGS; i++)
  375. if (est_bits & (1<<i)) {
  376. struct drm_display_mode *newmode;
  377. newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
  378. if (newmode) {
  379. drm_mode_probed_add(connector, newmode);
  380. modes++;
  381. }
  382. }
  383. return modes;
  384. }
  385. /**
  386. * add_standard_modes - get std. modes from EDID and add them
  387. * @edid: EDID block to scan
  388. *
  389. * Standard modes can be calculated using the CVT standard. Grab them from
  390. * @edid, calculate them, and add them to the list.
  391. */
  392. static int add_standard_modes(struct drm_connector *connector, struct edid *edid)
  393. {
  394. struct drm_device *dev = connector->dev;
  395. int i, modes = 0;
  396. for (i = 0; i < EDID_STD_TIMINGS; i++) {
  397. struct std_timing *t = &edid->standard_timings[i];
  398. struct drm_display_mode *newmode;
  399. /* If std timings bytes are 1, 1 it's empty */
  400. if (t->hsize == 1 && (t->aspect_ratio | t->vfreq) == 1)
  401. continue;
  402. newmode = drm_mode_std(dev, &edid->standard_timings[i]);
  403. if (newmode) {
  404. drm_mode_probed_add(connector, newmode);
  405. modes++;
  406. }
  407. }
  408. return modes;
  409. }
  410. /**
  411. * add_detailed_modes - get detailed mode info from EDID data
  412. * @connector: attached connector
  413. * @edid: EDID block to scan
  414. * @quirks: quirks to apply
  415. *
  416. * Some of the detailed timing sections may contain mode information. Grab
  417. * it and add it to the list.
  418. */
  419. static int add_detailed_info(struct drm_connector *connector,
  420. struct edid *edid, u32 quirks)
  421. {
  422. struct drm_device *dev = connector->dev;
  423. int i, j, modes = 0;
  424. for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
  425. struct detailed_timing *timing = &edid->detailed_timings[i];
  426. struct detailed_non_pixel *data = &timing->data.other_data;
  427. struct drm_display_mode *newmode;
  428. /* EDID up to and including 1.2 may put monitor info here */
  429. if (edid->version == 1 && edid->revision < 3)
  430. continue;
  431. /* Detailed mode timing */
  432. if (timing->pixel_clock) {
  433. newmode = drm_mode_detailed(dev, edid, timing, quirks);
  434. if (!newmode)
  435. continue;
  436. /* First detailed mode is preferred */
  437. if (i == 0 && edid->preferred_timing)
  438. newmode->type |= DRM_MODE_TYPE_PREFERRED;
  439. drm_mode_probed_add(connector, newmode);
  440. modes++;
  441. continue;
  442. }
  443. /* Other timing or info */
  444. switch (data->type) {
  445. case EDID_DETAIL_MONITOR_SERIAL:
  446. break;
  447. case EDID_DETAIL_MONITOR_STRING:
  448. break;
  449. case EDID_DETAIL_MONITOR_RANGE:
  450. /* Get monitor range data */
  451. break;
  452. case EDID_DETAIL_MONITOR_NAME:
  453. break;
  454. case EDID_DETAIL_MONITOR_CPDATA:
  455. break;
  456. case EDID_DETAIL_STD_MODES:
  457. /* Five modes per detailed section */
  458. for (j = 0; j < 5; i++) {
  459. struct std_timing *std;
  460. struct drm_display_mode *newmode;
  461. std = &data->data.timings[j];
  462. newmode = drm_mode_std(dev, std);
  463. if (newmode) {
  464. drm_mode_probed_add(connector, newmode);
  465. modes++;
  466. }
  467. }
  468. break;
  469. default:
  470. break;
  471. }
  472. }
  473. return modes;
  474. }
  475. #define DDC_ADDR 0x50
  476. unsigned char *drm_do_probe_ddc_edid(struct i2c_adapter *adapter)
  477. {
  478. unsigned char start = 0x0;
  479. unsigned char *buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
  480. struct i2c_msg msgs[] = {
  481. {
  482. .addr = DDC_ADDR,
  483. .flags = 0,
  484. .len = 1,
  485. .buf = &start,
  486. }, {
  487. .addr = DDC_ADDR,
  488. .flags = I2C_M_RD,
  489. .len = EDID_LENGTH,
  490. .buf = buf,
  491. }
  492. };
  493. if (!buf) {
  494. dev_warn(&adapter->dev, "unable to allocate memory for EDID "
  495. "block.\n");
  496. return NULL;
  497. }
  498. if (i2c_transfer(adapter, msgs, 2) == 2)
  499. return buf;
  500. dev_info(&adapter->dev, "unable to read EDID block.\n");
  501. kfree(buf);
  502. return NULL;
  503. }
  504. EXPORT_SYMBOL(drm_do_probe_ddc_edid);
  505. static unsigned char *drm_ddc_read(struct i2c_adapter *adapter)
  506. {
  507. struct i2c_algo_bit_data *algo_data = adapter->algo_data;
  508. unsigned char *edid = NULL;
  509. int i, j;
  510. algo_data->setscl(algo_data->data, 1);
  511. for (i = 0; i < 1; i++) {
  512. /* For some old monitors we need the
  513. * following process to initialize/stop DDC
  514. */
  515. algo_data->setsda(algo_data->data, 1);
  516. msleep(13);
  517. algo_data->setscl(algo_data->data, 1);
  518. for (j = 0; j < 5; j++) {
  519. msleep(10);
  520. if (algo_data->getscl(algo_data->data))
  521. break;
  522. }
  523. if (j == 5)
  524. continue;
  525. algo_data->setsda(algo_data->data, 0);
  526. msleep(15);
  527. algo_data->setscl(algo_data->data, 0);
  528. msleep(15);
  529. algo_data->setsda(algo_data->data, 1);
  530. msleep(15);
  531. /* Do the real work */
  532. edid = drm_do_probe_ddc_edid(adapter);
  533. algo_data->setsda(algo_data->data, 0);
  534. algo_data->setscl(algo_data->data, 0);
  535. msleep(15);
  536. algo_data->setscl(algo_data->data, 1);
  537. for (j = 0; j < 10; j++) {
  538. msleep(10);
  539. if (algo_data->getscl(algo_data->data))
  540. break;
  541. }
  542. algo_data->setsda(algo_data->data, 1);
  543. msleep(15);
  544. algo_data->setscl(algo_data->data, 0);
  545. algo_data->setsda(algo_data->data, 0);
  546. if (edid)
  547. break;
  548. }
  549. /* Release the DDC lines when done or the Apple Cinema HD display
  550. * will switch off
  551. */
  552. algo_data->setsda(algo_data->data, 1);
  553. algo_data->setscl(algo_data->data, 1);
  554. return edid;
  555. }
  556. /**
  557. * drm_get_edid - get EDID data, if available
  558. * @connector: connector we're probing
  559. * @adapter: i2c adapter to use for DDC
  560. *
  561. * Poke the given connector's i2c channel to grab EDID data if possible.
  562. *
  563. * Return edid data or NULL if we couldn't find any.
  564. */
  565. struct edid *drm_get_edid(struct drm_connector *connector,
  566. struct i2c_adapter *adapter)
  567. {
  568. struct edid *edid;
  569. edid = (struct edid *)drm_ddc_read(adapter);
  570. if (!edid) {
  571. dev_warn(&connector->dev->pdev->dev, "%s: no EDID data\n",
  572. drm_get_connector_name(connector));
  573. return NULL;
  574. }
  575. if (!edid_is_valid(edid)) {
  576. dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
  577. drm_get_connector_name(connector));
  578. kfree(edid);
  579. return NULL;
  580. }
  581. connector->display_info.raw_edid = (char *)edid;
  582. return edid;
  583. }
  584. EXPORT_SYMBOL(drm_get_edid);
  585. /**
  586. * drm_add_edid_modes - add modes from EDID data, if available
  587. * @connector: connector we're probing
  588. * @edid: edid data
  589. *
  590. * Add the specified modes to the connector's mode list.
  591. *
  592. * Return number of modes added or 0 if we couldn't find any.
  593. */
  594. int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
  595. {
  596. int num_modes = 0;
  597. u32 quirks;
  598. if (edid == NULL) {
  599. return 0;
  600. }
  601. if (!edid_is_valid(edid)) {
  602. dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
  603. drm_get_connector_name(connector));
  604. return 0;
  605. }
  606. quirks = edid_get_quirks(edid);
  607. num_modes += add_established_modes(connector, edid);
  608. num_modes += add_standard_modes(connector, edid);
  609. num_modes += add_detailed_info(connector, edid, quirks);
  610. if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
  611. edid_fixup_preferred(connector, quirks);
  612. connector->display_info.serration_vsync = edid->serration_vsync;
  613. connector->display_info.sync_on_green = edid->sync_on_green;
  614. connector->display_info.composite_sync = edid->composite_sync;
  615. connector->display_info.separate_syncs = edid->separate_syncs;
  616. connector->display_info.blank_to_black = edid->blank_to_black;
  617. connector->display_info.video_level = edid->video_level;
  618. connector->display_info.digital = edid->digital;
  619. connector->display_info.width_mm = edid->width_cm * 10;
  620. connector->display_info.height_mm = edid->height_cm * 10;
  621. connector->display_info.gamma = edid->gamma;
  622. connector->display_info.gtf_supported = edid->default_gtf;
  623. connector->display_info.standard_color = edid->standard_color;
  624. connector->display_info.display_type = edid->display_type;
  625. connector->display_info.active_off_supported = edid->pm_active_off;
  626. connector->display_info.suspend_supported = edid->pm_suspend;
  627. connector->display_info.standby_supported = edid->pm_standby;
  628. connector->display_info.gamma = edid->gamma;
  629. return num_modes;
  630. }
  631. EXPORT_SYMBOL(drm_add_edid_modes);