drm_edid.c 45 KB

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