drm_edid.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*
  2. * Copyright (c) 2006 Luc Verhaegen (quirks list)
  3. * Copyright (c) 2007-2008 Intel Corporation
  4. * Jesse Barnes <jesse.barnes@intel.com>
  5. * Copyright 2010 Red Hat, Inc.
  6. *
  7. * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
  8. * FB layer.
  9. * Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice (including the
  19. * next paragraph) shall be included in all copies or substantial portions
  20. * of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/i2c.h>
  32. #include <linux/i2c-algo-bit.h>
  33. #include "drmP.h"
  34. #include "drm_edid.h"
  35. #define EDID_EST_TIMINGS 16
  36. #define EDID_STD_TIMINGS 8
  37. #define EDID_DETAILED_TIMINGS 4
  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. #define LEVEL_DMT 0
  61. #define LEVEL_GTF 1
  62. #define LEVEL_CVT 2
  63. static struct edid_quirk {
  64. char *vendor;
  65. int product_id;
  66. u32 quirks;
  67. } edid_quirk_list[] = {
  68. /* Acer AL1706 */
  69. { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
  70. /* Acer F51 */
  71. { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
  72. /* Unknown Acer */
  73. { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  74. /* Belinea 10 15 55 */
  75. { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
  76. { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
  77. /* Envision Peripherals, Inc. EN-7100e */
  78. { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
  79. /* Funai Electronics PM36B */
  80. { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
  81. EDID_QUIRK_DETAILED_IN_CM },
  82. /* LG Philips LCD LP154W01-A5 */
  83. { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
  84. { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
  85. /* Philips 107p5 CRT */
  86. { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  87. /* Proview AY765C */
  88. { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
  89. /* Samsung SyncMaster 205BW. Note: irony */
  90. { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
  91. /* Samsung SyncMaster 22[5-6]BW */
  92. { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
  93. { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
  94. };
  95. /*** DDC fetch and block validation ***/
  96. static const u8 edid_header[] = {
  97. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
  98. };
  99. /*
  100. * Sanity check the EDID block (base or extension). Return 0 if the block
  101. * doesn't check out, or 1 if it's valid.
  102. */
  103. static bool
  104. drm_edid_block_valid(u8 *raw_edid)
  105. {
  106. int i;
  107. u8 csum = 0;
  108. struct edid *edid = (struct edid *)raw_edid;
  109. if (raw_edid[0] == 0x00) {
  110. int score = 0;
  111. for (i = 0; i < sizeof(edid_header); i++)
  112. if (raw_edid[i] == edid_header[i])
  113. score++;
  114. if (score == 8) ;
  115. else if (score >= 6) {
  116. DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
  117. memcpy(raw_edid, edid_header, sizeof(edid_header));
  118. } else {
  119. goto bad;
  120. }
  121. }
  122. for (i = 0; i < EDID_LENGTH; i++)
  123. csum += raw_edid[i];
  124. if (csum) {
  125. DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
  126. goto bad;
  127. }
  128. /* per-block-type checks */
  129. switch (raw_edid[0]) {
  130. case 0: /* base */
  131. if (edid->version != 1) {
  132. DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
  133. goto bad;
  134. }
  135. if (edid->revision > 4)
  136. DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
  137. break;
  138. default:
  139. break;
  140. }
  141. return 1;
  142. bad:
  143. if (raw_edid) {
  144. DRM_ERROR("Raw EDID:\n");
  145. print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
  146. printk("\n");
  147. }
  148. return 0;
  149. }
  150. /**
  151. * drm_edid_is_valid - sanity check EDID data
  152. * @edid: EDID data
  153. *
  154. * Sanity-check an entire EDID record (including extensions)
  155. */
  156. bool drm_edid_is_valid(struct edid *edid)
  157. {
  158. int i;
  159. u8 *raw = (u8 *)edid;
  160. if (!edid)
  161. return false;
  162. for (i = 0; i <= edid->extensions; i++)
  163. if (!drm_edid_block_valid(raw + i * EDID_LENGTH))
  164. return false;
  165. return true;
  166. }
  167. EXPORT_SYMBOL(drm_edid_is_valid);
  168. #define DDC_ADDR 0x50
  169. #define DDC_SEGMENT_ADDR 0x30
  170. /**
  171. * Get EDID information via I2C.
  172. *
  173. * \param adapter : i2c device adaptor
  174. * \param buf : EDID data buffer to be filled
  175. * \param len : EDID data buffer length
  176. * \return 0 on success or -1 on failure.
  177. *
  178. * Try to fetch EDID information by calling i2c driver function.
  179. */
  180. static int
  181. drm_do_probe_ddc_edid(struct i2c_adapter *adapter, unsigned char *buf,
  182. int block, int len)
  183. {
  184. unsigned char start = block * EDID_LENGTH;
  185. struct i2c_msg msgs[] = {
  186. {
  187. .addr = DDC_ADDR,
  188. .flags = 0,
  189. .len = 1,
  190. .buf = &start,
  191. }, {
  192. .addr = DDC_ADDR,
  193. .flags = I2C_M_RD,
  194. .len = len,
  195. .buf = buf + start,
  196. }
  197. };
  198. if (i2c_transfer(adapter, msgs, 2) == 2)
  199. return 0;
  200. return -1;
  201. }
  202. static u8 *
  203. drm_do_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
  204. {
  205. int i, j = 0;
  206. u8 *block, *new;
  207. if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
  208. return NULL;
  209. /* base block fetch */
  210. for (i = 0; i < 4; i++) {
  211. if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
  212. goto out;
  213. if (drm_edid_block_valid(block))
  214. break;
  215. }
  216. if (i == 4)
  217. goto carp;
  218. /* if there's no extensions, we're done */
  219. if (block[0x7e] == 0)
  220. return block;
  221. new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, GFP_KERNEL);
  222. if (!new)
  223. goto out;
  224. block = new;
  225. for (j = 1; j <= block[0x7e]; j++) {
  226. for (i = 0; i < 4; i++) {
  227. if (drm_do_probe_ddc_edid(adapter, block, j,
  228. EDID_LENGTH))
  229. goto out;
  230. if (drm_edid_block_valid(block + j * EDID_LENGTH))
  231. break;
  232. }
  233. if (i == 4)
  234. goto carp;
  235. }
  236. return block;
  237. carp:
  238. dev_warn(&connector->dev->pdev->dev, "%s: EDID block %d invalid.\n",
  239. drm_get_connector_name(connector), j);
  240. out:
  241. kfree(block);
  242. return NULL;
  243. }
  244. /**
  245. * Probe DDC presence.
  246. *
  247. * \param adapter : i2c device adaptor
  248. * \return 1 on success
  249. */
  250. static bool
  251. drm_probe_ddc(struct i2c_adapter *adapter)
  252. {
  253. unsigned char out;
  254. return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
  255. }
  256. /**
  257. * drm_get_edid - get EDID data, if available
  258. * @connector: connector we're probing
  259. * @adapter: i2c adapter to use for DDC
  260. *
  261. * Poke the given i2c channel to grab EDID data if possible. If found,
  262. * attach it to the connector.
  263. *
  264. * Return edid data or NULL if we couldn't find any.
  265. */
  266. struct edid *drm_get_edid(struct drm_connector *connector,
  267. struct i2c_adapter *adapter)
  268. {
  269. struct edid *edid = NULL;
  270. if (drm_probe_ddc(adapter))
  271. edid = (struct edid *)drm_do_get_edid(connector, adapter);
  272. connector->display_info.raw_edid = (char *)edid;
  273. return edid;
  274. }
  275. EXPORT_SYMBOL(drm_get_edid);
  276. /*** EDID parsing ***/
  277. /**
  278. * edid_vendor - match a string against EDID's obfuscated vendor field
  279. * @edid: EDID to match
  280. * @vendor: vendor string
  281. *
  282. * Returns true if @vendor is in @edid, false otherwise
  283. */
  284. static bool edid_vendor(struct edid *edid, char *vendor)
  285. {
  286. char edid_vendor[3];
  287. edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
  288. edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
  289. ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
  290. edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
  291. return !strncmp(edid_vendor, vendor, 3);
  292. }
  293. /**
  294. * edid_get_quirks - return quirk flags for a given EDID
  295. * @edid: EDID to process
  296. *
  297. * This tells subsequent routines what fixes they need to apply.
  298. */
  299. static u32 edid_get_quirks(struct edid *edid)
  300. {
  301. struct edid_quirk *quirk;
  302. int i;
  303. for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
  304. quirk = &edid_quirk_list[i];
  305. if (edid_vendor(edid, quirk->vendor) &&
  306. (EDID_PRODUCT_ID(edid) == quirk->product_id))
  307. return quirk->quirks;
  308. }
  309. return 0;
  310. }
  311. #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
  312. #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
  313. /**
  314. * edid_fixup_preferred - set preferred modes based on quirk list
  315. * @connector: has mode list to fix up
  316. * @quirks: quirks list
  317. *
  318. * Walk the mode list for @connector, clearing the preferred status
  319. * on existing modes and setting it anew for the right mode ala @quirks.
  320. */
  321. static void edid_fixup_preferred(struct drm_connector *connector,
  322. u32 quirks)
  323. {
  324. struct drm_display_mode *t, *cur_mode, *preferred_mode;
  325. int target_refresh = 0;
  326. if (list_empty(&connector->probed_modes))
  327. return;
  328. if (quirks & EDID_QUIRK_PREFER_LARGE_60)
  329. target_refresh = 60;
  330. if (quirks & EDID_QUIRK_PREFER_LARGE_75)
  331. target_refresh = 75;
  332. preferred_mode = list_first_entry(&connector->probed_modes,
  333. struct drm_display_mode, head);
  334. list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
  335. cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  336. if (cur_mode == preferred_mode)
  337. continue;
  338. /* Largest mode is preferred */
  339. if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
  340. preferred_mode = cur_mode;
  341. /* At a given size, try to get closest to target refresh */
  342. if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
  343. MODE_REFRESH_DIFF(cur_mode, target_refresh) <
  344. MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
  345. preferred_mode = cur_mode;
  346. }
  347. }
  348. preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
  349. }
  350. /*
  351. * Add the Autogenerated from the DMT spec.
  352. * This table is copied from xfree86/modes/xf86EdidModes.c.
  353. * But the mode with Reduced blank feature is deleted.
  354. */
  355. static struct drm_display_mode drm_dmt_modes[] = {
  356. /* 640x350@85Hz */
  357. { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 31500, 640, 672,
  358. 736, 832, 0, 350, 382, 385, 445, 0,
  359. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  360. /* 640x400@85Hz */
  361. { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 31500, 640, 672,
  362. 736, 832, 0, 400, 401, 404, 445, 0,
  363. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  364. /* 720x400@85Hz */
  365. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 756,
  366. 828, 936, 0, 400, 401, 404, 446, 0,
  367. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  368. /* 640x480@60Hz */
  369. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  370. 752, 800, 0, 480, 489, 492, 525, 0,
  371. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  372. /* 640x480@72Hz */
  373. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
  374. 704, 832, 0, 480, 489, 492, 520, 0,
  375. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  376. /* 640x480@75Hz */
  377. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
  378. 720, 840, 0, 480, 481, 484, 500, 0,
  379. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  380. /* 640x480@85Hz */
  381. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 36000, 640, 696,
  382. 752, 832, 0, 480, 481, 484, 509, 0,
  383. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  384. /* 800x600@56Hz */
  385. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
  386. 896, 1024, 0, 600, 601, 603, 625, 0,
  387. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  388. /* 800x600@60Hz */
  389. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  390. 968, 1056, 0, 600, 601, 605, 628, 0,
  391. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  392. /* 800x600@72Hz */
  393. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
  394. 976, 1040, 0, 600, 637, 643, 666, 0,
  395. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  396. /* 800x600@75Hz */
  397. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
  398. 896, 1056, 0, 600, 601, 604, 625, 0,
  399. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  400. /* 800x600@85Hz */
  401. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 56250, 800, 832,
  402. 896, 1048, 0, 600, 601, 604, 631, 0,
  403. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  404. /* 848x480@60Hz */
  405. { DRM_MODE("848x480", DRM_MODE_TYPE_DRIVER, 33750, 848, 864,
  406. 976, 1088, 0, 480, 486, 494, 517, 0,
  407. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  408. /* 1024x768@43Hz, interlace */
  409. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 44900, 1024, 1032,
  410. 1208, 1264, 0, 768, 768, 772, 817, 0,
  411. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
  412. DRM_MODE_FLAG_INTERLACE) },
  413. /* 1024x768@60Hz */
  414. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  415. 1184, 1344, 0, 768, 771, 777, 806, 0,
  416. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  417. /* 1024x768@70Hz */
  418. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
  419. 1184, 1328, 0, 768, 771, 777, 806, 0,
  420. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  421. /* 1024x768@75Hz */
  422. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78750, 1024, 1040,
  423. 1136, 1312, 0, 768, 769, 772, 800, 0,
  424. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  425. /* 1024x768@85Hz */
  426. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 94500, 1024, 1072,
  427. 1072, 1376, 0, 768, 769, 772, 808, 0,
  428. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  429. /* 1152x864@75Hz */
  430. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  431. 1344, 1600, 0, 864, 865, 868, 900, 0,
  432. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  433. /* 1280x768@60Hz */
  434. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  435. 1472, 1664, 0, 768, 771, 778, 798, 0,
  436. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  437. /* 1280x768@75Hz */
  438. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 102250, 1280, 1360,
  439. 1488, 1696, 0, 768, 771, 778, 805, 0,
  440. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  441. /* 1280x768@85Hz */
  442. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 117500, 1280, 1360,
  443. 1496, 1712, 0, 768, 771, 778, 809, 0,
  444. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  445. /* 1280x800@60Hz */
  446. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  447. 1480, 1680, 0, 800, 803, 809, 831, 0,
  448. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  449. /* 1280x800@75Hz */
  450. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 106500, 1280, 1360,
  451. 1488, 1696, 0, 800, 803, 809, 838, 0,
  452. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  453. /* 1280x800@85Hz */
  454. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 122500, 1280, 1360,
  455. 1496, 1712, 0, 800, 803, 809, 843, 0,
  456. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  457. /* 1280x960@60Hz */
  458. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  459. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  460. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  461. /* 1280x960@85Hz */
  462. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1344,
  463. 1504, 1728, 0, 960, 961, 964, 1011, 0,
  464. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  465. /* 1280x1024@60Hz */
  466. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  467. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  468. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  469. /* 1280x1024@75Hz */
  470. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
  471. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  472. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  473. /* 1280x1024@85Hz */
  474. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 157500, 1280, 1344,
  475. 1504, 1728, 0, 1024, 1025, 1028, 1072, 0,
  476. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  477. /* 1360x768@60Hz */
  478. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  479. 1536, 1792, 0, 768, 771, 777, 795, 0,
  480. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  481. /* 1440x1050@60Hz */
  482. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  483. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  484. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  485. /* 1440x1050@75Hz */
  486. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 156000, 1400, 1504,
  487. 1648, 1896, 0, 1050, 1053, 1057, 1099, 0,
  488. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  489. /* 1440x1050@85Hz */
  490. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 179500, 1400, 1504,
  491. 1656, 1912, 0, 1050, 1053, 1057, 1105, 0,
  492. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  493. /* 1440x900@60Hz */
  494. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  495. 1672, 1904, 0, 900, 903, 909, 934, 0,
  496. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  497. /* 1440x900@75Hz */
  498. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 136750, 1440, 1536,
  499. 1688, 1936, 0, 900, 903, 909, 942, 0,
  500. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  501. /* 1440x900@85Hz */
  502. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 157000, 1440, 1544,
  503. 1696, 1952, 0, 900, 903, 909, 948, 0,
  504. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  505. /* 1600x1200@60Hz */
  506. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  507. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  508. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  509. /* 1600x1200@65Hz */
  510. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 175500, 1600, 1664,
  511. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  512. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  513. /* 1600x1200@70Hz */
  514. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 189000, 1600, 1664,
  515. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  516. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  517. /* 1600x1200@75Hz */
  518. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 2025000, 1600, 1664,
  519. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  520. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  521. /* 1600x1200@85Hz */
  522. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 229500, 1600, 1664,
  523. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  524. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  525. /* 1680x1050@60Hz */
  526. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  527. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  528. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  529. /* 1680x1050@75Hz */
  530. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 187000, 1680, 1800,
  531. 1976, 2272, 0, 1050, 1053, 1059, 1099, 0,
  532. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  533. /* 1680x1050@85Hz */
  534. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 214750, 1680, 1808,
  535. 1984, 2288, 0, 1050, 1053, 1059, 1105, 0,
  536. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  537. /* 1792x1344@60Hz */
  538. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  539. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  540. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  541. /* 1729x1344@75Hz */
  542. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 261000, 1792, 1888,
  543. 2104, 2456, 0, 1344, 1345, 1348, 1417, 0,
  544. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  545. /* 1853x1392@60Hz */
  546. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  547. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  548. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  549. /* 1856x1392@75Hz */
  550. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 288000, 1856, 1984,
  551. 2208, 2560, 0, 1392, 1395, 1399, 1500, 0,
  552. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  553. /* 1920x1200@60Hz */
  554. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  555. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  556. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  557. /* 1920x1200@75Hz */
  558. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 245250, 1920, 2056,
  559. 2264, 2608, 0, 1200, 1203, 1209, 1255, 0,
  560. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  561. /* 1920x1200@85Hz */
  562. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 281250, 1920, 2064,
  563. 2272, 2624, 0, 1200, 1203, 1209, 1262, 0,
  564. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  565. /* 1920x1440@60Hz */
  566. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  567. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  568. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  569. /* 1920x1440@75Hz */
  570. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2064,
  571. 2288, 2640, 0, 1440, 1441, 1444, 1500, 0,
  572. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  573. /* 2560x1600@60Hz */
  574. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  575. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  576. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  577. /* 2560x1600@75HZ */
  578. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 443250, 2560, 2768,
  579. 3048, 3536, 0, 1600, 1603, 1609, 1672, 0,
  580. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  581. /* 2560x1600@85HZ */
  582. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 505250, 2560, 2768,
  583. 3048, 3536, 0, 1600, 1603, 1609, 1682, 0,
  584. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  585. };
  586. static const int drm_num_dmt_modes =
  587. sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
  588. static struct drm_display_mode *drm_find_dmt(struct drm_device *dev,
  589. int hsize, int vsize, int fresh)
  590. {
  591. int i;
  592. struct drm_display_mode *ptr, *mode;
  593. mode = NULL;
  594. for (i = 0; i < drm_num_dmt_modes; i++) {
  595. ptr = &drm_dmt_modes[i];
  596. if (hsize == ptr->hdisplay &&
  597. vsize == ptr->vdisplay &&
  598. fresh == drm_mode_vrefresh(ptr)) {
  599. /* get the expected default mode */
  600. mode = drm_mode_duplicate(dev, ptr);
  601. break;
  602. }
  603. }
  604. return mode;
  605. }
  606. typedef void detailed_cb(struct detailed_timing *timing, void *closure);
  607. static void
  608. drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
  609. {
  610. int i;
  611. struct edid *edid = (struct edid *)raw_edid;
  612. if (edid == NULL)
  613. return;
  614. for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
  615. cb(&(edid->detailed_timings[i]), closure);
  616. /* XXX extension block walk */
  617. }
  618. static void
  619. is_rb(struct detailed_timing *t, void *data)
  620. {
  621. u8 *r = (u8 *)t;
  622. if (r[3] == EDID_DETAIL_MONITOR_RANGE)
  623. if (r[15] & 0x10)
  624. *(bool *)data = true;
  625. }
  626. /* EDID 1.4 defines this explicitly. For EDID 1.3, we guess, badly. */
  627. static bool
  628. drm_monitor_supports_rb(struct edid *edid)
  629. {
  630. if (edid->revision >= 4) {
  631. bool ret;
  632. drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
  633. return ret;
  634. }
  635. return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
  636. }
  637. /*
  638. * 0 is reserved. The spec says 0x01 fill for unused timings. Some old
  639. * monitors fill with ascii space (0x20) instead.
  640. */
  641. static int
  642. bad_std_timing(u8 a, u8 b)
  643. {
  644. return (a == 0x00 && b == 0x00) ||
  645. (a == 0x01 && b == 0x01) ||
  646. (a == 0x20 && b == 0x20);
  647. }
  648. /**
  649. * drm_mode_std - convert standard mode info (width, height, refresh) into mode
  650. * @t: standard timing params
  651. * @timing_level: standard timing level
  652. *
  653. * Take the standard timing params (in this case width, aspect, and refresh)
  654. * and convert them into a real mode using CVT/GTF/DMT.
  655. */
  656. static struct drm_display_mode *
  657. drm_mode_std(struct drm_connector *connector, struct std_timing *t,
  658. int revision, int timing_level)
  659. {
  660. struct drm_device *dev = connector->dev;
  661. struct drm_display_mode *m, *mode = NULL;
  662. int hsize, vsize;
  663. int vrefresh_rate;
  664. unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
  665. >> EDID_TIMING_ASPECT_SHIFT;
  666. unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
  667. >> EDID_TIMING_VFREQ_SHIFT;
  668. if (bad_std_timing(t->hsize, t->vfreq_aspect))
  669. return NULL;
  670. /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
  671. hsize = t->hsize * 8 + 248;
  672. /* vrefresh_rate = vfreq + 60 */
  673. vrefresh_rate = vfreq + 60;
  674. /* the vdisplay is calculated based on the aspect ratio */
  675. if (aspect_ratio == 0) {
  676. if (revision < 3)
  677. vsize = hsize;
  678. else
  679. vsize = (hsize * 10) / 16;
  680. } else if (aspect_ratio == 1)
  681. vsize = (hsize * 3) / 4;
  682. else if (aspect_ratio == 2)
  683. vsize = (hsize * 4) / 5;
  684. else
  685. vsize = (hsize * 9) / 16;
  686. /* HDTV hack, part 1 */
  687. if (vrefresh_rate == 60 &&
  688. ((hsize == 1360 && vsize == 765) ||
  689. (hsize == 1368 && vsize == 769))) {
  690. hsize = 1366;
  691. vsize = 768;
  692. }
  693. /*
  694. * If this connector already has a mode for this size and refresh
  695. * rate (because it came from detailed or CVT info), use that
  696. * instead. This way we don't have to guess at interlace or
  697. * reduced blanking.
  698. */
  699. list_for_each_entry(m, &connector->modes, head)
  700. if (m->hdisplay == hsize && m->vdisplay == vsize &&
  701. drm_mode_vrefresh(m) == vrefresh_rate)
  702. return NULL;
  703. /* HDTV hack, part 2 */
  704. if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
  705. mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
  706. false);
  707. mode->hdisplay = 1366;
  708. mode->vsync_start = mode->vsync_start - 1;
  709. mode->vsync_end = mode->vsync_end - 1;
  710. return mode;
  711. }
  712. /* check whether it can be found in default mode table */
  713. mode = drm_find_dmt(dev, hsize, vsize, vrefresh_rate);
  714. if (mode)
  715. return mode;
  716. switch (timing_level) {
  717. case LEVEL_DMT:
  718. break;
  719. case LEVEL_GTF:
  720. mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
  721. break;
  722. case LEVEL_CVT:
  723. mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
  724. false);
  725. break;
  726. }
  727. return mode;
  728. }
  729. /*
  730. * EDID is delightfully ambiguous about how interlaced modes are to be
  731. * encoded. Our internal representation is of frame height, but some
  732. * HDTV detailed timings are encoded as field height.
  733. *
  734. * The format list here is from CEA, in frame size. Technically we
  735. * should be checking refresh rate too. Whatever.
  736. */
  737. static void
  738. drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
  739. struct detailed_pixel_timing *pt)
  740. {
  741. int i;
  742. static const struct {
  743. int w, h;
  744. } cea_interlaced[] = {
  745. { 1920, 1080 },
  746. { 720, 480 },
  747. { 1440, 480 },
  748. { 2880, 480 },
  749. { 720, 576 },
  750. { 1440, 576 },
  751. { 2880, 576 },
  752. };
  753. static const int n_sizes =
  754. sizeof(cea_interlaced)/sizeof(cea_interlaced[0]);
  755. if (!(pt->misc & DRM_EDID_PT_INTERLACED))
  756. return;
  757. for (i = 0; i < n_sizes; i++) {
  758. if ((mode->hdisplay == cea_interlaced[i].w) &&
  759. (mode->vdisplay == cea_interlaced[i].h / 2)) {
  760. mode->vdisplay *= 2;
  761. mode->vsync_start *= 2;
  762. mode->vsync_end *= 2;
  763. mode->vtotal *= 2;
  764. mode->vtotal |= 1;
  765. }
  766. }
  767. mode->flags |= DRM_MODE_FLAG_INTERLACE;
  768. }
  769. /**
  770. * drm_mode_detailed - create a new mode from an EDID detailed timing section
  771. * @dev: DRM device (needed to create new mode)
  772. * @edid: EDID block
  773. * @timing: EDID detailed timing info
  774. * @quirks: quirks to apply
  775. *
  776. * An EDID detailed timing block contains enough info for us to create and
  777. * return a new struct drm_display_mode.
  778. */
  779. static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
  780. struct edid *edid,
  781. struct detailed_timing *timing,
  782. u32 quirks)
  783. {
  784. struct drm_display_mode *mode;
  785. struct detailed_pixel_timing *pt = &timing->data.pixel_data;
  786. unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
  787. unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
  788. unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
  789. unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
  790. unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
  791. unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
  792. unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
  793. unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
  794. /* ignore tiny modes */
  795. if (hactive < 64 || vactive < 64)
  796. return NULL;
  797. if (pt->misc & DRM_EDID_PT_STEREO) {
  798. printk(KERN_WARNING "stereo mode not supported\n");
  799. return NULL;
  800. }
  801. if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
  802. printk(KERN_WARNING "composite sync not supported\n");
  803. }
  804. /* it is incorrect if hsync/vsync width is zero */
  805. if (!hsync_pulse_width || !vsync_pulse_width) {
  806. DRM_DEBUG_KMS("Incorrect Detailed timing. "
  807. "Wrong Hsync/Vsync pulse width\n");
  808. return NULL;
  809. }
  810. mode = drm_mode_create(dev);
  811. if (!mode)
  812. return NULL;
  813. mode->type = DRM_MODE_TYPE_DRIVER;
  814. if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
  815. timing->pixel_clock = cpu_to_le16(1088);
  816. mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
  817. mode->hdisplay = hactive;
  818. mode->hsync_start = mode->hdisplay + hsync_offset;
  819. mode->hsync_end = mode->hsync_start + hsync_pulse_width;
  820. mode->htotal = mode->hdisplay + hblank;
  821. mode->vdisplay = vactive;
  822. mode->vsync_start = mode->vdisplay + vsync_offset;
  823. mode->vsync_end = mode->vsync_start + vsync_pulse_width;
  824. mode->vtotal = mode->vdisplay + vblank;
  825. /* Some EDIDs have bogus h/vtotal values */
  826. if (mode->hsync_end > mode->htotal)
  827. mode->htotal = mode->hsync_end + 1;
  828. if (mode->vsync_end > mode->vtotal)
  829. mode->vtotal = mode->vsync_end + 1;
  830. drm_mode_set_name(mode);
  831. drm_mode_do_interlace_quirk(mode, pt);
  832. if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
  833. pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
  834. }
  835. mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
  836. DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  837. mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
  838. DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  839. mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
  840. mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
  841. if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
  842. mode->width_mm *= 10;
  843. mode->height_mm *= 10;
  844. }
  845. if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
  846. mode->width_mm = edid->width_cm * 10;
  847. mode->height_mm = edid->height_cm * 10;
  848. }
  849. return mode;
  850. }
  851. /*
  852. * Detailed mode info for the EDID "established modes" data to use.
  853. */
  854. static struct drm_display_mode edid_est_modes[] = {
  855. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
  856. 968, 1056, 0, 600, 601, 605, 628, 0,
  857. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
  858. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
  859. 896, 1024, 0, 600, 601, 603, 625, 0,
  860. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
  861. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
  862. 720, 840, 0, 480, 481, 484, 500, 0,
  863. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
  864. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
  865. 704, 832, 0, 480, 489, 491, 520, 0,
  866. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
  867. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
  868. 768, 864, 0, 480, 483, 486, 525, 0,
  869. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
  870. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
  871. 752, 800, 0, 480, 490, 492, 525, 0,
  872. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
  873. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
  874. 846, 900, 0, 400, 421, 423, 449, 0,
  875. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
  876. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
  877. 846, 900, 0, 400, 412, 414, 449, 0,
  878. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
  879. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
  880. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  881. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
  882. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
  883. 1136, 1312, 0, 768, 769, 772, 800, 0,
  884. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
  885. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
  886. 1184, 1328, 0, 768, 771, 777, 806, 0,
  887. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
  888. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  889. 1184, 1344, 0, 768, 771, 777, 806, 0,
  890. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
  891. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
  892. 1208, 1264, 0, 768, 768, 776, 817, 0,
  893. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
  894. { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
  895. 928, 1152, 0, 624, 625, 628, 667, 0,
  896. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
  897. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
  898. 896, 1056, 0, 600, 601, 604, 625, 0,
  899. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
  900. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
  901. 976, 1040, 0, 600, 637, 643, 666, 0,
  902. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
  903. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  904. 1344, 1600, 0, 864, 865, 868, 900, 0,
  905. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
  906. };
  907. /**
  908. * add_established_modes - get est. modes from EDID and add them
  909. * @edid: EDID block to scan
  910. *
  911. * Each EDID block contains a bitmap of the supported "established modes" list
  912. * (defined above). Tease them out and add them to the global modes list.
  913. */
  914. static int add_established_modes(struct drm_connector *connector, struct edid *edid)
  915. {
  916. struct drm_device *dev = connector->dev;
  917. unsigned long est_bits = edid->established_timings.t1 |
  918. (edid->established_timings.t2 << 8) |
  919. ((edid->established_timings.mfg_rsvd & 0x80) << 9);
  920. int i, modes = 0;
  921. for (i = 0; i <= EDID_EST_TIMINGS; i++)
  922. if (est_bits & (1<<i)) {
  923. struct drm_display_mode *newmode;
  924. newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
  925. if (newmode) {
  926. drm_mode_probed_add(connector, newmode);
  927. modes++;
  928. }
  929. }
  930. return modes;
  931. }
  932. /**
  933. * stanard_timing_level - get std. timing level(CVT/GTF/DMT)
  934. * @edid: EDID block to scan
  935. */
  936. static int standard_timing_level(struct edid *edid)
  937. {
  938. if (edid->revision >= 2) {
  939. if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
  940. return LEVEL_CVT;
  941. return LEVEL_GTF;
  942. }
  943. return LEVEL_DMT;
  944. }
  945. /**
  946. * add_standard_modes - get std. modes from EDID and add them
  947. * @edid: EDID block to scan
  948. *
  949. * Standard modes can be calculated using the CVT standard. Grab them from
  950. * @edid, calculate them, and add them to the list.
  951. */
  952. static int add_standard_modes(struct drm_connector *connector, struct edid *edid)
  953. {
  954. int i, modes = 0;
  955. int timing_level;
  956. timing_level = standard_timing_level(edid);
  957. for (i = 0; i < EDID_STD_TIMINGS; i++) {
  958. struct drm_display_mode *newmode;
  959. newmode = drm_mode_std(connector, &edid->standard_timings[i],
  960. edid->revision, timing_level);
  961. if (newmode) {
  962. drm_mode_probed_add(connector, newmode);
  963. modes++;
  964. }
  965. }
  966. return modes;
  967. }
  968. static bool
  969. mode_is_rb(struct drm_display_mode *mode)
  970. {
  971. return (mode->htotal - mode->hdisplay == 160) &&
  972. (mode->hsync_end - mode->hdisplay == 80) &&
  973. (mode->hsync_end - mode->hsync_start == 32) &&
  974. (mode->vsync_start - mode->vdisplay == 3);
  975. }
  976. static bool
  977. mode_in_hsync_range(struct drm_display_mode *mode, struct edid *edid, u8 *t)
  978. {
  979. int hsync, hmin, hmax;
  980. hmin = t[7];
  981. if (edid->revision >= 4)
  982. hmin += ((t[4] & 0x04) ? 255 : 0);
  983. hmax = t[8];
  984. if (edid->revision >= 4)
  985. hmax += ((t[4] & 0x08) ? 255 : 0);
  986. hsync = drm_mode_hsync(mode);
  987. return (hsync <= hmax && hsync >= hmin);
  988. }
  989. static bool
  990. mode_in_vsync_range(struct drm_display_mode *mode, struct edid *edid, u8 *t)
  991. {
  992. int vsync, vmin, vmax;
  993. vmin = t[5];
  994. if (edid->revision >= 4)
  995. vmin += ((t[4] & 0x01) ? 255 : 0);
  996. vmax = t[6];
  997. if (edid->revision >= 4)
  998. vmax += ((t[4] & 0x02) ? 255 : 0);
  999. vsync = drm_mode_vrefresh(mode);
  1000. return (vsync <= vmax && vsync >= vmin);
  1001. }
  1002. static u32
  1003. range_pixel_clock(struct edid *edid, u8 *t)
  1004. {
  1005. /* unspecified */
  1006. if (t[9] == 0 || t[9] == 255)
  1007. return 0;
  1008. /* 1.4 with CVT support gives us real precision, yay */
  1009. if (edid->revision >= 4 && t[10] == 0x04)
  1010. return (t[9] * 10000) - ((t[12] >> 2) * 250);
  1011. /* 1.3 is pathetic, so fuzz up a bit */
  1012. return t[9] * 10000 + 5001;
  1013. }
  1014. /*
  1015. * XXX fix this for GTF secondary curve formula
  1016. */
  1017. static bool
  1018. mode_in_range(struct drm_display_mode *mode, struct edid *edid,
  1019. struct detailed_timing *timing)
  1020. {
  1021. u32 max_clock;
  1022. u8 *t = (u8 *)timing;
  1023. if (!mode_in_hsync_range(mode, edid, t))
  1024. return false;
  1025. if (!mode_in_vsync_range(mode, edid, t))
  1026. return false;
  1027. if ((max_clock = range_pixel_clock(edid, t)))
  1028. if (mode->clock > max_clock)
  1029. return false;
  1030. /* 1.4 max horizontal check */
  1031. if (edid->revision >= 4 && t[10] == 0x04)
  1032. if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
  1033. return false;
  1034. if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
  1035. return false;
  1036. return true;
  1037. }
  1038. /*
  1039. * XXX If drm_dmt_modes ever regrows the CVT-R modes (and it will) this will
  1040. * need to account for them.
  1041. */
  1042. static int
  1043. drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
  1044. struct detailed_timing *timing)
  1045. {
  1046. int i, modes = 0;
  1047. struct drm_display_mode *newmode;
  1048. struct drm_device *dev = connector->dev;
  1049. for (i = 0; i < drm_num_dmt_modes; i++) {
  1050. if (mode_in_range(drm_dmt_modes + i, edid, timing)) {
  1051. newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
  1052. if (newmode) {
  1053. drm_mode_probed_add(connector, newmode);
  1054. modes++;
  1055. }
  1056. }
  1057. }
  1058. return modes;
  1059. }
  1060. static int drm_cvt_modes(struct drm_connector *connector,
  1061. struct detailed_timing *timing)
  1062. {
  1063. int i, j, modes = 0;
  1064. struct drm_display_mode *newmode;
  1065. struct drm_device *dev = connector->dev;
  1066. struct cvt_timing *cvt;
  1067. const int rates[] = { 60, 85, 75, 60, 50 };
  1068. const u8 empty[3] = { 0, 0, 0 };
  1069. for (i = 0; i < 4; i++) {
  1070. int uninitialized_var(width), height;
  1071. cvt = &(timing->data.other_data.data.cvt[i]);
  1072. if (!memcmp(cvt->code, empty, 3))
  1073. continue;
  1074. height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
  1075. switch (cvt->code[1] & 0x0c) {
  1076. case 0x00:
  1077. width = height * 4 / 3;
  1078. break;
  1079. case 0x04:
  1080. width = height * 16 / 9;
  1081. break;
  1082. case 0x08:
  1083. width = height * 16 / 10;
  1084. break;
  1085. case 0x0c:
  1086. width = height * 15 / 9;
  1087. break;
  1088. }
  1089. for (j = 1; j < 5; j++) {
  1090. if (cvt->code[2] & (1 << j)) {
  1091. newmode = drm_cvt_mode(dev, width, height,
  1092. rates[j], j == 0,
  1093. false, false);
  1094. if (newmode) {
  1095. drm_mode_probed_add(connector, newmode);
  1096. modes++;
  1097. }
  1098. }
  1099. }
  1100. }
  1101. return modes;
  1102. }
  1103. static const struct {
  1104. short w;
  1105. short h;
  1106. short r;
  1107. short rb;
  1108. } est3_modes[] = {
  1109. /* byte 6 */
  1110. { 640, 350, 85, 0 },
  1111. { 640, 400, 85, 0 },
  1112. { 720, 400, 85, 0 },
  1113. { 640, 480, 85, 0 },
  1114. { 848, 480, 60, 0 },
  1115. { 800, 600, 85, 0 },
  1116. { 1024, 768, 85, 0 },
  1117. { 1152, 864, 75, 0 },
  1118. /* byte 7 */
  1119. { 1280, 768, 60, 1 },
  1120. { 1280, 768, 60, 0 },
  1121. { 1280, 768, 75, 0 },
  1122. { 1280, 768, 85, 0 },
  1123. { 1280, 960, 60, 0 },
  1124. { 1280, 960, 85, 0 },
  1125. { 1280, 1024, 60, 0 },
  1126. { 1280, 1024, 85, 0 },
  1127. /* byte 8 */
  1128. { 1360, 768, 60, 0 },
  1129. { 1440, 900, 60, 1 },
  1130. { 1440, 900, 60, 0 },
  1131. { 1440, 900, 75, 0 },
  1132. { 1440, 900, 85, 0 },
  1133. { 1400, 1050, 60, 1 },
  1134. { 1400, 1050, 60, 0 },
  1135. { 1400, 1050, 75, 0 },
  1136. /* byte 9 */
  1137. { 1400, 1050, 85, 0 },
  1138. { 1680, 1050, 60, 1 },
  1139. { 1680, 1050, 60, 0 },
  1140. { 1680, 1050, 75, 0 },
  1141. { 1680, 1050, 85, 0 },
  1142. { 1600, 1200, 60, 0 },
  1143. { 1600, 1200, 65, 0 },
  1144. { 1600, 1200, 70, 0 },
  1145. /* byte 10 */
  1146. { 1600, 1200, 75, 0 },
  1147. { 1600, 1200, 85, 0 },
  1148. { 1792, 1344, 60, 0 },
  1149. { 1792, 1344, 85, 0 },
  1150. { 1856, 1392, 60, 0 },
  1151. { 1856, 1392, 75, 0 },
  1152. { 1920, 1200, 60, 1 },
  1153. { 1920, 1200, 60, 0 },
  1154. /* byte 11 */
  1155. { 1920, 1200, 75, 0 },
  1156. { 1920, 1200, 85, 0 },
  1157. { 1920, 1440, 60, 0 },
  1158. { 1920, 1440, 75, 0 },
  1159. };
  1160. static const int num_est3_modes = sizeof(est3_modes) / sizeof(est3_modes[0]);
  1161. static int
  1162. drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
  1163. {
  1164. int i, j, m, modes = 0;
  1165. struct drm_display_mode *mode;
  1166. u8 *est = ((u8 *)timing) + 5;
  1167. for (i = 0; i < 6; i++) {
  1168. for (j = 7; j > 0; j--) {
  1169. m = (i * 8) + (7 - j);
  1170. if (m > num_est3_modes)
  1171. break;
  1172. if (est[i] & (1 << j)) {
  1173. mode = drm_find_dmt(connector->dev,
  1174. est3_modes[m].w,
  1175. est3_modes[m].h,
  1176. est3_modes[m].r
  1177. /*, est3_modes[m].rb */);
  1178. if (mode) {
  1179. drm_mode_probed_add(connector, mode);
  1180. modes++;
  1181. }
  1182. }
  1183. }
  1184. }
  1185. return modes;
  1186. }
  1187. static int add_detailed_modes(struct drm_connector *connector,
  1188. struct detailed_timing *timing,
  1189. struct edid *edid, u32 quirks, int preferred)
  1190. {
  1191. int i, modes = 0;
  1192. struct detailed_non_pixel *data = &timing->data.other_data;
  1193. int timing_level = standard_timing_level(edid);
  1194. int gtf = (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF);
  1195. struct drm_display_mode *newmode;
  1196. struct drm_device *dev = connector->dev;
  1197. if (timing->pixel_clock) {
  1198. newmode = drm_mode_detailed(dev, edid, timing, quirks);
  1199. if (!newmode)
  1200. return 0;
  1201. if (preferred)
  1202. newmode->type |= DRM_MODE_TYPE_PREFERRED;
  1203. drm_mode_probed_add(connector, newmode);
  1204. return 1;
  1205. }
  1206. /* other timing types */
  1207. switch (data->type) {
  1208. case EDID_DETAIL_MONITOR_RANGE:
  1209. if (gtf)
  1210. modes += drm_gtf_modes_for_range(connector, edid,
  1211. timing);
  1212. break;
  1213. case EDID_DETAIL_STD_MODES:
  1214. /* Six modes per detailed section */
  1215. for (i = 0; i < 6; i++) {
  1216. struct std_timing *std;
  1217. struct drm_display_mode *newmode;
  1218. std = &data->data.timings[i];
  1219. newmode = drm_mode_std(connector, std, edid->revision,
  1220. timing_level);
  1221. if (newmode) {
  1222. drm_mode_probed_add(connector, newmode);
  1223. modes++;
  1224. }
  1225. }
  1226. break;
  1227. case EDID_DETAIL_CVT_3BYTE:
  1228. modes += drm_cvt_modes(connector, timing);
  1229. break;
  1230. case EDID_DETAIL_EST_TIMINGS:
  1231. modes += drm_est3_modes(connector, timing);
  1232. break;
  1233. default:
  1234. break;
  1235. }
  1236. return modes;
  1237. }
  1238. /**
  1239. * add_detailed_info - get detailed mode info from EDID data
  1240. * @connector: attached connector
  1241. * @edid: EDID block to scan
  1242. * @quirks: quirks to apply
  1243. *
  1244. * Some of the detailed timing sections may contain mode information. Grab
  1245. * it and add it to the list.
  1246. */
  1247. static int add_detailed_info(struct drm_connector *connector,
  1248. struct edid *edid, u32 quirks)
  1249. {
  1250. int i, modes = 0;
  1251. for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
  1252. struct detailed_timing *timing = &edid->detailed_timings[i];
  1253. int preferred = (i == 0);
  1254. if (preferred && edid->version == 1 && edid->revision < 4)
  1255. preferred = (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
  1256. /* In 1.0, only timings are allowed */
  1257. if (!timing->pixel_clock && edid->version == 1 &&
  1258. edid->revision == 0)
  1259. continue;
  1260. modes += add_detailed_modes(connector, timing, edid, quirks,
  1261. preferred);
  1262. }
  1263. return modes;
  1264. }
  1265. /**
  1266. * add_detailed_mode_eedid - get detailed mode info from addtional timing
  1267. * EDID block
  1268. * @connector: attached connector
  1269. * @edid: EDID block to scan(It is only to get addtional timing EDID block)
  1270. * @quirks: quirks to apply
  1271. *
  1272. * Some of the detailed timing sections may contain mode information. Grab
  1273. * it and add it to the list.
  1274. */
  1275. static int add_detailed_info_eedid(struct drm_connector *connector,
  1276. struct edid *edid, u32 quirks)
  1277. {
  1278. int i, modes = 0;
  1279. char *edid_ext = NULL;
  1280. struct detailed_timing *timing;
  1281. int start_offset, end_offset;
  1282. int timing_level;
  1283. if (edid->version == 1 && edid->revision < 3)
  1284. return 0;
  1285. if (!edid->extensions)
  1286. return 0;
  1287. /* Find CEA extension */
  1288. for (i = 0; i < edid->extensions; i++) {
  1289. edid_ext = (char *)edid + EDID_LENGTH * (i + 1);
  1290. if (edid_ext[0] == 0x02)
  1291. break;
  1292. }
  1293. if (i == edid->extensions)
  1294. return 0;
  1295. /* Get the start offset of detailed timing block */
  1296. start_offset = edid_ext[2];
  1297. if (start_offset == 0) {
  1298. /* If the start_offset is zero, it means that neither detailed
  1299. * info nor data block exist. In such case it is also
  1300. * unnecessary to parse the detailed timing info.
  1301. */
  1302. return 0;
  1303. }
  1304. timing_level = standard_timing_level(edid);
  1305. end_offset = EDID_LENGTH;
  1306. end_offset -= sizeof(struct detailed_timing);
  1307. for (i = start_offset; i < end_offset;
  1308. i += sizeof(struct detailed_timing)) {
  1309. timing = (struct detailed_timing *)(edid_ext + i);
  1310. modes += add_detailed_modes(connector, timing, edid, quirks, 0);
  1311. }
  1312. return modes;
  1313. }
  1314. #define HDMI_IDENTIFIER 0x000C03
  1315. #define VENDOR_BLOCK 0x03
  1316. /**
  1317. * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
  1318. * @edid: monitor EDID information
  1319. *
  1320. * Parse the CEA extension according to CEA-861-B.
  1321. * Return true if HDMI, false if not or unknown.
  1322. */
  1323. bool drm_detect_hdmi_monitor(struct edid *edid)
  1324. {
  1325. char *edid_ext = NULL;
  1326. int i, hdmi_id;
  1327. int start_offset, end_offset;
  1328. bool is_hdmi = false;
  1329. /* No EDID or EDID extensions */
  1330. if (edid == NULL || edid->extensions == 0)
  1331. goto end;
  1332. /* Find CEA extension */
  1333. for (i = 0; i < edid->extensions; i++) {
  1334. edid_ext = (char *)edid + EDID_LENGTH * (i + 1);
  1335. /* This block is CEA extension */
  1336. if (edid_ext[0] == 0x02)
  1337. break;
  1338. }
  1339. if (i == edid->extensions)
  1340. goto end;
  1341. /* Data block offset in CEA extension block */
  1342. start_offset = 4;
  1343. end_offset = edid_ext[2];
  1344. /*
  1345. * Because HDMI identifier is in Vendor Specific Block,
  1346. * search it from all data blocks of CEA extension.
  1347. */
  1348. for (i = start_offset; i < end_offset;
  1349. /* Increased by data block len */
  1350. i += ((edid_ext[i] & 0x1f) + 1)) {
  1351. /* Find vendor specific block */
  1352. if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
  1353. hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
  1354. edid_ext[i + 3] << 16;
  1355. /* Find HDMI identifier */
  1356. if (hdmi_id == HDMI_IDENTIFIER)
  1357. is_hdmi = true;
  1358. break;
  1359. }
  1360. }
  1361. end:
  1362. return is_hdmi;
  1363. }
  1364. EXPORT_SYMBOL(drm_detect_hdmi_monitor);
  1365. /**
  1366. * drm_add_edid_modes - add modes from EDID data, if available
  1367. * @connector: connector we're probing
  1368. * @edid: edid data
  1369. *
  1370. * Add the specified modes to the connector's mode list.
  1371. *
  1372. * Return number of modes added or 0 if we couldn't find any.
  1373. */
  1374. int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
  1375. {
  1376. int num_modes = 0;
  1377. u32 quirks;
  1378. if (edid == NULL) {
  1379. return 0;
  1380. }
  1381. if (!drm_edid_is_valid(edid)) {
  1382. dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
  1383. drm_get_connector_name(connector));
  1384. return 0;
  1385. }
  1386. quirks = edid_get_quirks(edid);
  1387. /*
  1388. * EDID spec says modes should be preferred in this order:
  1389. * - preferred detailed mode
  1390. * - other detailed modes from base block
  1391. * - detailed modes from extension blocks
  1392. * - CVT 3-byte code modes
  1393. * - standard timing codes
  1394. * - established timing codes
  1395. * - modes inferred from GTF or CVT range information
  1396. *
  1397. * We don't quite implement this yet, but we're close.
  1398. *
  1399. * XXX order for additional mode types in extension blocks?
  1400. */
  1401. num_modes += add_detailed_info(connector, edid, quirks);
  1402. num_modes += add_detailed_info_eedid(connector, edid, quirks);
  1403. num_modes += add_standard_modes(connector, edid);
  1404. num_modes += add_established_modes(connector, edid);
  1405. if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
  1406. edid_fixup_preferred(connector, quirks);
  1407. connector->display_info.serration_vsync = (edid->input & DRM_EDID_INPUT_SERRATION_VSYNC) ? 1 : 0;
  1408. connector->display_info.sync_on_green = (edid->input & DRM_EDID_INPUT_SYNC_ON_GREEN) ? 1 : 0;
  1409. connector->display_info.composite_sync = (edid->input & DRM_EDID_INPUT_COMPOSITE_SYNC) ? 1 : 0;
  1410. connector->display_info.separate_syncs = (edid->input & DRM_EDID_INPUT_SEPARATE_SYNCS) ? 1 : 0;
  1411. connector->display_info.blank_to_black = (edid->input & DRM_EDID_INPUT_BLANK_TO_BLACK) ? 1 : 0;
  1412. connector->display_info.video_level = (edid->input & DRM_EDID_INPUT_VIDEO_LEVEL) >> 5;
  1413. connector->display_info.digital = (edid->input & DRM_EDID_INPUT_DIGITAL) ? 1 : 0;
  1414. connector->display_info.width_mm = edid->width_cm * 10;
  1415. connector->display_info.height_mm = edid->height_cm * 10;
  1416. connector->display_info.gamma = edid->gamma;
  1417. connector->display_info.gtf_supported = (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF) ? 1 : 0;
  1418. connector->display_info.standard_color = (edid->features & DRM_EDID_FEATURE_STANDARD_COLOR) ? 1 : 0;
  1419. connector->display_info.display_type = (edid->features & DRM_EDID_FEATURE_DISPLAY_TYPE) >> 3;
  1420. connector->display_info.active_off_supported = (edid->features & DRM_EDID_FEATURE_PM_ACTIVE_OFF) ? 1 : 0;
  1421. connector->display_info.suspend_supported = (edid->features & DRM_EDID_FEATURE_PM_SUSPEND) ? 1 : 0;
  1422. connector->display_info.standby_supported = (edid->features & DRM_EDID_FEATURE_PM_STANDBY) ? 1 : 0;
  1423. connector->display_info.gamma = edid->gamma;
  1424. return num_modes;
  1425. }
  1426. EXPORT_SYMBOL(drm_add_edid_modes);
  1427. /**
  1428. * drm_add_modes_noedid - add modes for the connectors without EDID
  1429. * @connector: connector we're probing
  1430. * @hdisplay: the horizontal display limit
  1431. * @vdisplay: the vertical display limit
  1432. *
  1433. * Add the specified modes to the connector's mode list. Only when the
  1434. * hdisplay/vdisplay is not beyond the given limit, it will be added.
  1435. *
  1436. * Return number of modes added or 0 if we couldn't find any.
  1437. */
  1438. int drm_add_modes_noedid(struct drm_connector *connector,
  1439. int hdisplay, int vdisplay)
  1440. {
  1441. int i, count, num_modes = 0;
  1442. struct drm_display_mode *mode, *ptr;
  1443. struct drm_device *dev = connector->dev;
  1444. count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
  1445. if (hdisplay < 0)
  1446. hdisplay = 0;
  1447. if (vdisplay < 0)
  1448. vdisplay = 0;
  1449. for (i = 0; i < count; i++) {
  1450. ptr = &drm_dmt_modes[i];
  1451. if (hdisplay && vdisplay) {
  1452. /*
  1453. * Only when two are valid, they will be used to check
  1454. * whether the mode should be added to the mode list of
  1455. * the connector.
  1456. */
  1457. if (ptr->hdisplay > hdisplay ||
  1458. ptr->vdisplay > vdisplay)
  1459. continue;
  1460. }
  1461. if (drm_mode_vrefresh(ptr) > 61)
  1462. continue;
  1463. mode = drm_mode_duplicate(dev, ptr);
  1464. if (mode) {
  1465. drm_mode_probed_add(connector, mode);
  1466. num_modes++;
  1467. }
  1468. }
  1469. return num_modes;
  1470. }
  1471. EXPORT_SYMBOL(drm_add_modes_noedid);