drm_edid.c 49 KB

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