drm_edid.c 50 KB

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