drm_edid.c 40 KB

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