drm_edid.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  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. static bool
  416. mode_is_rb(const struct drm_display_mode *mode)
  417. {
  418. return (mode->htotal - mode->hdisplay == 160) &&
  419. (mode->hsync_end - mode->hdisplay == 80) &&
  420. (mode->hsync_end - mode->hsync_start == 32) &&
  421. (mode->vsync_start - mode->vdisplay == 3);
  422. }
  423. /*
  424. * drm_mode_find_dmt - Create a copy of a mode if present in DMT
  425. * @dev: Device to duplicate against
  426. * @hsize: Mode width
  427. * @vsize: Mode height
  428. * @fresh: Mode refresh rate
  429. * @rb: Mode reduced-blanking-ness
  430. *
  431. * Walk the DMT mode list looking for a match for the given parameters.
  432. * Return a newly allocated copy of the mode, or NULL if not found.
  433. */
  434. struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
  435. int hsize, int vsize, int fresh,
  436. bool rb)
  437. {
  438. int i;
  439. for (i = 0; i < drm_num_dmt_modes; i++) {
  440. const struct drm_display_mode *ptr = &drm_dmt_modes[i];
  441. if (hsize != ptr->hdisplay)
  442. continue;
  443. if (vsize != ptr->vdisplay)
  444. continue;
  445. if (fresh != drm_mode_vrefresh(ptr))
  446. continue;
  447. if (rb != mode_is_rb(ptr))
  448. continue;
  449. return drm_mode_duplicate(dev, ptr);
  450. }
  451. return NULL;
  452. }
  453. EXPORT_SYMBOL(drm_mode_find_dmt);
  454. typedef void detailed_cb(struct detailed_timing *timing, void *closure);
  455. static void
  456. cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
  457. {
  458. int i, n = 0;
  459. u8 d = ext[0x02];
  460. u8 *det_base = ext + d;
  461. n = (127 - d) / 18;
  462. for (i = 0; i < n; i++)
  463. cb((struct detailed_timing *)(det_base + 18 * i), closure);
  464. }
  465. static void
  466. vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
  467. {
  468. unsigned int i, n = min((int)ext[0x02], 6);
  469. u8 *det_base = ext + 5;
  470. if (ext[0x01] != 1)
  471. return; /* unknown version */
  472. for (i = 0; i < n; i++)
  473. cb((struct detailed_timing *)(det_base + 18 * i), closure);
  474. }
  475. static void
  476. drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
  477. {
  478. int i;
  479. struct edid *edid = (struct edid *)raw_edid;
  480. if (edid == NULL)
  481. return;
  482. for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
  483. cb(&(edid->detailed_timings[i]), closure);
  484. for (i = 1; i <= raw_edid[0x7e]; i++) {
  485. u8 *ext = raw_edid + (i * EDID_LENGTH);
  486. switch (*ext) {
  487. case CEA_EXT:
  488. cea_for_each_detailed_block(ext, cb, closure);
  489. break;
  490. case VTB_EXT:
  491. vtb_for_each_detailed_block(ext, cb, closure);
  492. break;
  493. default:
  494. break;
  495. }
  496. }
  497. }
  498. static void
  499. is_rb(struct detailed_timing *t, void *data)
  500. {
  501. u8 *r = (u8 *)t;
  502. if (r[3] == EDID_DETAIL_MONITOR_RANGE)
  503. if (r[15] & 0x10)
  504. *(bool *)data = true;
  505. }
  506. /* EDID 1.4 defines this explicitly. For EDID 1.3, we guess, badly. */
  507. static bool
  508. drm_monitor_supports_rb(struct edid *edid)
  509. {
  510. if (edid->revision >= 4) {
  511. bool ret;
  512. drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
  513. return ret;
  514. }
  515. return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
  516. }
  517. static void
  518. find_gtf2(struct detailed_timing *t, void *data)
  519. {
  520. u8 *r = (u8 *)t;
  521. if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
  522. *(u8 **)data = r;
  523. }
  524. /* Secondary GTF curve kicks in above some break frequency */
  525. static int
  526. drm_gtf2_hbreak(struct edid *edid)
  527. {
  528. u8 *r = NULL;
  529. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  530. return r ? (r[12] * 2) : 0;
  531. }
  532. static int
  533. drm_gtf2_2c(struct edid *edid)
  534. {
  535. u8 *r = NULL;
  536. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  537. return r ? r[13] : 0;
  538. }
  539. static int
  540. drm_gtf2_m(struct edid *edid)
  541. {
  542. u8 *r = NULL;
  543. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  544. return r ? (r[15] << 8) + r[14] : 0;
  545. }
  546. static int
  547. drm_gtf2_k(struct edid *edid)
  548. {
  549. u8 *r = NULL;
  550. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  551. return r ? r[16] : 0;
  552. }
  553. static int
  554. drm_gtf2_2j(struct edid *edid)
  555. {
  556. u8 *r = NULL;
  557. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  558. return r ? r[17] : 0;
  559. }
  560. /**
  561. * standard_timing_level - get std. timing level(CVT/GTF/DMT)
  562. * @edid: EDID block to scan
  563. */
  564. static int standard_timing_level(struct edid *edid)
  565. {
  566. if (edid->revision >= 2) {
  567. if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
  568. return LEVEL_CVT;
  569. if (drm_gtf2_hbreak(edid))
  570. return LEVEL_GTF2;
  571. return LEVEL_GTF;
  572. }
  573. return LEVEL_DMT;
  574. }
  575. /*
  576. * 0 is reserved. The spec says 0x01 fill for unused timings. Some old
  577. * monitors fill with ascii space (0x20) instead.
  578. */
  579. static int
  580. bad_std_timing(u8 a, u8 b)
  581. {
  582. return (a == 0x00 && b == 0x00) ||
  583. (a == 0x01 && b == 0x01) ||
  584. (a == 0x20 && b == 0x20);
  585. }
  586. /**
  587. * drm_mode_std - convert standard mode info (width, height, refresh) into mode
  588. * @t: standard timing params
  589. * @timing_level: standard timing level
  590. *
  591. * Take the standard timing params (in this case width, aspect, and refresh)
  592. * and convert them into a real mode using CVT/GTF/DMT.
  593. */
  594. static struct drm_display_mode *
  595. drm_mode_std(struct drm_connector *connector, struct edid *edid,
  596. struct std_timing *t, int revision)
  597. {
  598. struct drm_device *dev = connector->dev;
  599. struct drm_display_mode *m, *mode = NULL;
  600. int hsize, vsize;
  601. int vrefresh_rate;
  602. unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
  603. >> EDID_TIMING_ASPECT_SHIFT;
  604. unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
  605. >> EDID_TIMING_VFREQ_SHIFT;
  606. int timing_level = standard_timing_level(edid);
  607. if (bad_std_timing(t->hsize, t->vfreq_aspect))
  608. return NULL;
  609. /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
  610. hsize = t->hsize * 8 + 248;
  611. /* vrefresh_rate = vfreq + 60 */
  612. vrefresh_rate = vfreq + 60;
  613. /* the vdisplay is calculated based on the aspect ratio */
  614. if (aspect_ratio == 0) {
  615. if (revision < 3)
  616. vsize = hsize;
  617. else
  618. vsize = (hsize * 10) / 16;
  619. } else if (aspect_ratio == 1)
  620. vsize = (hsize * 3) / 4;
  621. else if (aspect_ratio == 2)
  622. vsize = (hsize * 4) / 5;
  623. else
  624. vsize = (hsize * 9) / 16;
  625. /* HDTV hack, part 1 */
  626. if (vrefresh_rate == 60 &&
  627. ((hsize == 1360 && vsize == 765) ||
  628. (hsize == 1368 && vsize == 769))) {
  629. hsize = 1366;
  630. vsize = 768;
  631. }
  632. /*
  633. * If this connector already has a mode for this size and refresh
  634. * rate (because it came from detailed or CVT info), use that
  635. * instead. This way we don't have to guess at interlace or
  636. * reduced blanking.
  637. */
  638. list_for_each_entry(m, &connector->probed_modes, head)
  639. if (m->hdisplay == hsize && m->vdisplay == vsize &&
  640. drm_mode_vrefresh(m) == vrefresh_rate)
  641. return NULL;
  642. /* HDTV hack, part 2 */
  643. if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
  644. mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
  645. false);
  646. mode->hdisplay = 1366;
  647. mode->hsync_start = mode->hsync_start - 1;
  648. mode->hsync_end = mode->hsync_end - 1;
  649. return mode;
  650. }
  651. /* check whether it can be found in default mode table */
  652. if (drm_monitor_supports_rb(edid)) {
  653. mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
  654. true);
  655. if (mode)
  656. return mode;
  657. }
  658. mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
  659. if (mode)
  660. return mode;
  661. /* okay, generate it */
  662. switch (timing_level) {
  663. case LEVEL_DMT:
  664. break;
  665. case LEVEL_GTF:
  666. mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
  667. break;
  668. case LEVEL_GTF2:
  669. /*
  670. * This is potentially wrong if there's ever a monitor with
  671. * more than one ranges section, each claiming a different
  672. * secondary GTF curve. Please don't do that.
  673. */
  674. mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
  675. if (!mode)
  676. return NULL;
  677. if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
  678. drm_mode_destroy(dev, mode);
  679. mode = drm_gtf_mode_complex(dev, hsize, vsize,
  680. vrefresh_rate, 0, 0,
  681. drm_gtf2_m(edid),
  682. drm_gtf2_2c(edid),
  683. drm_gtf2_k(edid),
  684. drm_gtf2_2j(edid));
  685. }
  686. break;
  687. case LEVEL_CVT:
  688. mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
  689. false);
  690. break;
  691. }
  692. return mode;
  693. }
  694. /*
  695. * EDID is delightfully ambiguous about how interlaced modes are to be
  696. * encoded. Our internal representation is of frame height, but some
  697. * HDTV detailed timings are encoded as field height.
  698. *
  699. * The format list here is from CEA, in frame size. Technically we
  700. * should be checking refresh rate too. Whatever.
  701. */
  702. static void
  703. drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
  704. struct detailed_pixel_timing *pt)
  705. {
  706. int i;
  707. static const struct {
  708. int w, h;
  709. } cea_interlaced[] = {
  710. { 1920, 1080 },
  711. { 720, 480 },
  712. { 1440, 480 },
  713. { 2880, 480 },
  714. { 720, 576 },
  715. { 1440, 576 },
  716. { 2880, 576 },
  717. };
  718. if (!(pt->misc & DRM_EDID_PT_INTERLACED))
  719. return;
  720. for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
  721. if ((mode->hdisplay == cea_interlaced[i].w) &&
  722. (mode->vdisplay == cea_interlaced[i].h / 2)) {
  723. mode->vdisplay *= 2;
  724. mode->vsync_start *= 2;
  725. mode->vsync_end *= 2;
  726. mode->vtotal *= 2;
  727. mode->vtotal |= 1;
  728. }
  729. }
  730. mode->flags |= DRM_MODE_FLAG_INTERLACE;
  731. }
  732. /**
  733. * drm_mode_detailed - create a new mode from an EDID detailed timing section
  734. * @dev: DRM device (needed to create new mode)
  735. * @edid: EDID block
  736. * @timing: EDID detailed timing info
  737. * @quirks: quirks to apply
  738. *
  739. * An EDID detailed timing block contains enough info for us to create and
  740. * return a new struct drm_display_mode.
  741. */
  742. static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
  743. struct edid *edid,
  744. struct detailed_timing *timing,
  745. u32 quirks)
  746. {
  747. struct drm_display_mode *mode;
  748. struct detailed_pixel_timing *pt = &timing->data.pixel_data;
  749. unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
  750. unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
  751. unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
  752. unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
  753. unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
  754. unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
  755. unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
  756. unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
  757. /* ignore tiny modes */
  758. if (hactive < 64 || vactive < 64)
  759. return NULL;
  760. if (pt->misc & DRM_EDID_PT_STEREO) {
  761. printk(KERN_WARNING "stereo mode not supported\n");
  762. return NULL;
  763. }
  764. if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
  765. printk(KERN_WARNING "composite sync not supported\n");
  766. }
  767. /* it is incorrect if hsync/vsync width is zero */
  768. if (!hsync_pulse_width || !vsync_pulse_width) {
  769. DRM_DEBUG_KMS("Incorrect Detailed timing. "
  770. "Wrong Hsync/Vsync pulse width\n");
  771. return NULL;
  772. }
  773. mode = drm_mode_create(dev);
  774. if (!mode)
  775. return NULL;
  776. mode->type = DRM_MODE_TYPE_DRIVER;
  777. if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
  778. timing->pixel_clock = cpu_to_le16(1088);
  779. mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
  780. mode->hdisplay = hactive;
  781. mode->hsync_start = mode->hdisplay + hsync_offset;
  782. mode->hsync_end = mode->hsync_start + hsync_pulse_width;
  783. mode->htotal = mode->hdisplay + hblank;
  784. mode->vdisplay = vactive;
  785. mode->vsync_start = mode->vdisplay + vsync_offset;
  786. mode->vsync_end = mode->vsync_start + vsync_pulse_width;
  787. mode->vtotal = mode->vdisplay + vblank;
  788. /* Some EDIDs have bogus h/vtotal values */
  789. if (mode->hsync_end > mode->htotal)
  790. mode->htotal = mode->hsync_end + 1;
  791. if (mode->vsync_end > mode->vtotal)
  792. mode->vtotal = mode->vsync_end + 1;
  793. drm_mode_do_interlace_quirk(mode, pt);
  794. drm_mode_set_name(mode);
  795. if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
  796. pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
  797. }
  798. mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
  799. DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  800. mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
  801. DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  802. mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
  803. mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
  804. if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
  805. mode->width_mm *= 10;
  806. mode->height_mm *= 10;
  807. }
  808. if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
  809. mode->width_mm = edid->width_cm * 10;
  810. mode->height_mm = edid->height_cm * 10;
  811. }
  812. return mode;
  813. }
  814. static bool
  815. mode_in_hsync_range(const struct drm_display_mode *mode,
  816. struct edid *edid, u8 *t)
  817. {
  818. int hsync, hmin, hmax;
  819. hmin = t[7];
  820. if (edid->revision >= 4)
  821. hmin += ((t[4] & 0x04) ? 255 : 0);
  822. hmax = t[8];
  823. if (edid->revision >= 4)
  824. hmax += ((t[4] & 0x08) ? 255 : 0);
  825. hsync = drm_mode_hsync(mode);
  826. return (hsync <= hmax && hsync >= hmin);
  827. }
  828. static bool
  829. mode_in_vsync_range(const struct drm_display_mode *mode,
  830. struct edid *edid, u8 *t)
  831. {
  832. int vsync, vmin, vmax;
  833. vmin = t[5];
  834. if (edid->revision >= 4)
  835. vmin += ((t[4] & 0x01) ? 255 : 0);
  836. vmax = t[6];
  837. if (edid->revision >= 4)
  838. vmax += ((t[4] & 0x02) ? 255 : 0);
  839. vsync = drm_mode_vrefresh(mode);
  840. return (vsync <= vmax && vsync >= vmin);
  841. }
  842. static u32
  843. range_pixel_clock(struct edid *edid, u8 *t)
  844. {
  845. /* unspecified */
  846. if (t[9] == 0 || t[9] == 255)
  847. return 0;
  848. /* 1.4 with CVT support gives us real precision, yay */
  849. if (edid->revision >= 4 && t[10] == 0x04)
  850. return (t[9] * 10000) - ((t[12] >> 2) * 250);
  851. /* 1.3 is pathetic, so fuzz up a bit */
  852. return t[9] * 10000 + 5001;
  853. }
  854. static bool
  855. mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
  856. struct detailed_timing *timing)
  857. {
  858. u32 max_clock;
  859. u8 *t = (u8 *)timing;
  860. if (!mode_in_hsync_range(mode, edid, t))
  861. return false;
  862. if (!mode_in_vsync_range(mode, edid, t))
  863. return false;
  864. if ((max_clock = range_pixel_clock(edid, t)))
  865. if (mode->clock > max_clock)
  866. return false;
  867. /* 1.4 max horizontal check */
  868. if (edid->revision >= 4 && t[10] == 0x04)
  869. if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
  870. return false;
  871. if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
  872. return false;
  873. return true;
  874. }
  875. static int
  876. drm_dmt_modes_for_range(struct drm_connector *connector, struct edid *edid,
  877. struct detailed_timing *timing)
  878. {
  879. int i, modes = 0;
  880. struct drm_display_mode *newmode;
  881. struct drm_device *dev = connector->dev;
  882. for (i = 0; i < drm_num_dmt_modes; i++) {
  883. if (mode_in_range(drm_dmt_modes + i, edid, timing)) {
  884. newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
  885. if (newmode) {
  886. drm_mode_probed_add(connector, newmode);
  887. modes++;
  888. }
  889. }
  890. }
  891. return modes;
  892. }
  893. /* fix up 1366x768 mode from 1368x768;
  894. * GFT/CVT can't express 1366 width which isn't dividable by 8
  895. */
  896. static void fixup_mode_1366x768(struct drm_display_mode *mode)
  897. {
  898. if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
  899. mode->hdisplay = 1366;
  900. mode->hsync_start--;
  901. mode->hsync_end--;
  902. drm_mode_set_name(mode);
  903. }
  904. }
  905. static int
  906. drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
  907. struct detailed_timing *timing)
  908. {
  909. int i, modes = 0;
  910. struct drm_display_mode *newmode;
  911. struct drm_device *dev = connector->dev;
  912. for (i = 0; i < num_extra_modes; i++) {
  913. const struct minimode *m = &extra_modes[i];
  914. newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
  915. if (!newmode)
  916. return modes;
  917. fixup_mode_1366x768(newmode);
  918. if (!mode_in_range(newmode, edid, timing)) {
  919. drm_mode_destroy(dev, newmode);
  920. continue;
  921. }
  922. drm_mode_probed_add(connector, newmode);
  923. modes++;
  924. }
  925. return modes;
  926. }
  927. static int
  928. drm_cvt_modes_for_range(struct drm_connector *connector, struct edid *edid,
  929. struct detailed_timing *timing)
  930. {
  931. int i, modes = 0;
  932. struct drm_display_mode *newmode;
  933. struct drm_device *dev = connector->dev;
  934. bool rb = drm_monitor_supports_rb(edid);
  935. for (i = 0; i < num_extra_modes; i++) {
  936. const struct minimode *m = &extra_modes[i];
  937. newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
  938. if (!newmode)
  939. return modes;
  940. fixup_mode_1366x768(newmode);
  941. if (!mode_in_range(newmode, edid, timing)) {
  942. drm_mode_destroy(dev, newmode);
  943. continue;
  944. }
  945. drm_mode_probed_add(connector, newmode);
  946. modes++;
  947. }
  948. return modes;
  949. }
  950. static void
  951. do_inferred_modes(struct detailed_timing *timing, void *c)
  952. {
  953. struct detailed_mode_closure *closure = c;
  954. struct detailed_non_pixel *data = &timing->data.other_data;
  955. struct detailed_data_monitor_range *range = &data->data.range;
  956. if (data->type != EDID_DETAIL_MONITOR_RANGE)
  957. return;
  958. closure->modes += drm_dmt_modes_for_range(closure->connector,
  959. closure->edid,
  960. timing);
  961. if (!version_greater(closure->edid, 1, 1))
  962. return; /* GTF not defined yet */
  963. switch (range->flags) {
  964. case 0x02: /* secondary gtf, XXX could do more */
  965. case 0x00: /* default gtf */
  966. closure->modes += drm_gtf_modes_for_range(closure->connector,
  967. closure->edid,
  968. timing);
  969. break;
  970. case 0x04: /* cvt, only in 1.4+ */
  971. if (!version_greater(closure->edid, 1, 3))
  972. break;
  973. closure->modes += drm_cvt_modes_for_range(closure->connector,
  974. closure->edid,
  975. timing);
  976. break;
  977. case 0x01: /* just the ranges, no formula */
  978. default:
  979. break;
  980. }
  981. }
  982. static int
  983. add_inferred_modes(struct drm_connector *connector, struct edid *edid)
  984. {
  985. struct detailed_mode_closure closure = {
  986. connector, edid, 0, 0, 0
  987. };
  988. if (version_greater(edid, 1, 0))
  989. drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
  990. &closure);
  991. return closure.modes;
  992. }
  993. static int
  994. drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
  995. {
  996. int i, j, m, modes = 0;
  997. struct drm_display_mode *mode;
  998. u8 *est = ((u8 *)timing) + 5;
  999. for (i = 0; i < 6; i++) {
  1000. for (j = 7; j > 0; j--) {
  1001. m = (i * 8) + (7 - j);
  1002. if (m >= ARRAY_SIZE(est3_modes))
  1003. break;
  1004. if (est[i] & (1 << j)) {
  1005. mode = drm_mode_find_dmt(connector->dev,
  1006. est3_modes[m].w,
  1007. est3_modes[m].h,
  1008. est3_modes[m].r,
  1009. est3_modes[m].rb);
  1010. if (mode) {
  1011. drm_mode_probed_add(connector, mode);
  1012. modes++;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. return modes;
  1018. }
  1019. static void
  1020. do_established_modes(struct detailed_timing *timing, void *c)
  1021. {
  1022. struct detailed_mode_closure *closure = c;
  1023. struct detailed_non_pixel *data = &timing->data.other_data;
  1024. if (data->type == EDID_DETAIL_EST_TIMINGS)
  1025. closure->modes += drm_est3_modes(closure->connector, timing);
  1026. }
  1027. /**
  1028. * add_established_modes - get est. modes from EDID and add them
  1029. * @edid: EDID block to scan
  1030. *
  1031. * Each EDID block contains a bitmap of the supported "established modes" list
  1032. * (defined above). Tease them out and add them to the global modes list.
  1033. */
  1034. static int
  1035. add_established_modes(struct drm_connector *connector, struct edid *edid)
  1036. {
  1037. struct drm_device *dev = connector->dev;
  1038. unsigned long est_bits = edid->established_timings.t1 |
  1039. (edid->established_timings.t2 << 8) |
  1040. ((edid->established_timings.mfg_rsvd & 0x80) << 9);
  1041. int i, modes = 0;
  1042. struct detailed_mode_closure closure = {
  1043. connector, edid, 0, 0, 0
  1044. };
  1045. for (i = 0; i <= EDID_EST_TIMINGS; i++) {
  1046. if (est_bits & (1<<i)) {
  1047. struct drm_display_mode *newmode;
  1048. newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
  1049. if (newmode) {
  1050. drm_mode_probed_add(connector, newmode);
  1051. modes++;
  1052. }
  1053. }
  1054. }
  1055. if (version_greater(edid, 1, 0))
  1056. drm_for_each_detailed_block((u8 *)edid,
  1057. do_established_modes, &closure);
  1058. return modes + closure.modes;
  1059. }
  1060. static void
  1061. do_standard_modes(struct detailed_timing *timing, void *c)
  1062. {
  1063. struct detailed_mode_closure *closure = c;
  1064. struct detailed_non_pixel *data = &timing->data.other_data;
  1065. struct drm_connector *connector = closure->connector;
  1066. struct edid *edid = closure->edid;
  1067. if (data->type == EDID_DETAIL_STD_MODES) {
  1068. int i;
  1069. for (i = 0; i < 6; i++) {
  1070. struct std_timing *std;
  1071. struct drm_display_mode *newmode;
  1072. std = &data->data.timings[i];
  1073. newmode = drm_mode_std(connector, edid, std,
  1074. edid->revision);
  1075. if (newmode) {
  1076. drm_mode_probed_add(connector, newmode);
  1077. closure->modes++;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. /**
  1083. * add_standard_modes - get std. modes from EDID and add them
  1084. * @edid: EDID block to scan
  1085. *
  1086. * Standard modes can be calculated using the appropriate standard (DMT,
  1087. * GTF or CVT. Grab them from @edid and add them to the list.
  1088. */
  1089. static int
  1090. add_standard_modes(struct drm_connector *connector, struct edid *edid)
  1091. {
  1092. int i, modes = 0;
  1093. struct detailed_mode_closure closure = {
  1094. connector, edid, 0, 0, 0
  1095. };
  1096. for (i = 0; i < EDID_STD_TIMINGS; i++) {
  1097. struct drm_display_mode *newmode;
  1098. newmode = drm_mode_std(connector, edid,
  1099. &edid->standard_timings[i],
  1100. edid->revision);
  1101. if (newmode) {
  1102. drm_mode_probed_add(connector, newmode);
  1103. modes++;
  1104. }
  1105. }
  1106. if (version_greater(edid, 1, 0))
  1107. drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
  1108. &closure);
  1109. /* XXX should also look for standard codes in VTB blocks */
  1110. return modes + closure.modes;
  1111. }
  1112. static int drm_cvt_modes(struct drm_connector *connector,
  1113. struct detailed_timing *timing)
  1114. {
  1115. int i, j, modes = 0;
  1116. struct drm_display_mode *newmode;
  1117. struct drm_device *dev = connector->dev;
  1118. struct cvt_timing *cvt;
  1119. const int rates[] = { 60, 85, 75, 60, 50 };
  1120. const u8 empty[3] = { 0, 0, 0 };
  1121. for (i = 0; i < 4; i++) {
  1122. int uninitialized_var(width), height;
  1123. cvt = &(timing->data.other_data.data.cvt[i]);
  1124. if (!memcmp(cvt->code, empty, 3))
  1125. continue;
  1126. height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
  1127. switch (cvt->code[1] & 0x0c) {
  1128. case 0x00:
  1129. width = height * 4 / 3;
  1130. break;
  1131. case 0x04:
  1132. width = height * 16 / 9;
  1133. break;
  1134. case 0x08:
  1135. width = height * 16 / 10;
  1136. break;
  1137. case 0x0c:
  1138. width = height * 15 / 9;
  1139. break;
  1140. }
  1141. for (j = 1; j < 5; j++) {
  1142. if (cvt->code[2] & (1 << j)) {
  1143. newmode = drm_cvt_mode(dev, width, height,
  1144. rates[j], j == 0,
  1145. false, false);
  1146. if (newmode) {
  1147. drm_mode_probed_add(connector, newmode);
  1148. modes++;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. return modes;
  1154. }
  1155. static void
  1156. do_cvt_mode(struct detailed_timing *timing, void *c)
  1157. {
  1158. struct detailed_mode_closure *closure = c;
  1159. struct detailed_non_pixel *data = &timing->data.other_data;
  1160. if (data->type == EDID_DETAIL_CVT_3BYTE)
  1161. closure->modes += drm_cvt_modes(closure->connector, timing);
  1162. }
  1163. static int
  1164. add_cvt_modes(struct drm_connector *connector, struct edid *edid)
  1165. {
  1166. struct detailed_mode_closure closure = {
  1167. connector, edid, 0, 0, 0
  1168. };
  1169. if (version_greater(edid, 1, 2))
  1170. drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
  1171. /* XXX should also look for CVT codes in VTB blocks */
  1172. return closure.modes;
  1173. }
  1174. static void
  1175. do_detailed_mode(struct detailed_timing *timing, void *c)
  1176. {
  1177. struct detailed_mode_closure *closure = c;
  1178. struct drm_display_mode *newmode;
  1179. if (timing->pixel_clock) {
  1180. newmode = drm_mode_detailed(closure->connector->dev,
  1181. closure->edid, timing,
  1182. closure->quirks);
  1183. if (!newmode)
  1184. return;
  1185. if (closure->preferred)
  1186. newmode->type |= DRM_MODE_TYPE_PREFERRED;
  1187. drm_mode_probed_add(closure->connector, newmode);
  1188. closure->modes++;
  1189. closure->preferred = 0;
  1190. }
  1191. }
  1192. /*
  1193. * add_detailed_modes - Add modes from detailed timings
  1194. * @connector: attached connector
  1195. * @edid: EDID block to scan
  1196. * @quirks: quirks to apply
  1197. */
  1198. static int
  1199. add_detailed_modes(struct drm_connector *connector, struct edid *edid,
  1200. u32 quirks)
  1201. {
  1202. struct detailed_mode_closure closure = {
  1203. connector,
  1204. edid,
  1205. 1,
  1206. quirks,
  1207. 0
  1208. };
  1209. if (closure.preferred && !version_greater(edid, 1, 3))
  1210. closure.preferred =
  1211. (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
  1212. drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
  1213. return closure.modes;
  1214. }
  1215. #define HDMI_IDENTIFIER 0x000C03
  1216. #define AUDIO_BLOCK 0x01
  1217. #define VIDEO_BLOCK 0x02
  1218. #define VENDOR_BLOCK 0x03
  1219. #define SPEAKER_BLOCK 0x04
  1220. #define EDID_BASIC_AUDIO (1 << 6)
  1221. #define EDID_CEA_YCRCB444 (1 << 5)
  1222. #define EDID_CEA_YCRCB422 (1 << 4)
  1223. /**
  1224. * Search EDID for CEA extension block.
  1225. */
  1226. u8 *drm_find_cea_extension(struct edid *edid)
  1227. {
  1228. u8 *edid_ext = NULL;
  1229. int i;
  1230. /* No EDID or EDID extensions */
  1231. if (edid == NULL || edid->extensions == 0)
  1232. return NULL;
  1233. /* Find CEA extension */
  1234. for (i = 0; i < edid->extensions; i++) {
  1235. edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
  1236. if (edid_ext[0] == CEA_EXT)
  1237. break;
  1238. }
  1239. if (i == edid->extensions)
  1240. return NULL;
  1241. return edid_ext;
  1242. }
  1243. EXPORT_SYMBOL(drm_find_cea_extension);
  1244. static int
  1245. do_cea_modes (struct drm_connector *connector, u8 *db, u8 len)
  1246. {
  1247. struct drm_device *dev = connector->dev;
  1248. u8 * mode, cea_mode;
  1249. int modes = 0;
  1250. for (mode = db; mode < db + len; mode++) {
  1251. cea_mode = (*mode & 127) - 1; /* CEA modes are numbered 1..127 */
  1252. if (cea_mode < drm_num_cea_modes) {
  1253. struct drm_display_mode *newmode;
  1254. newmode = drm_mode_duplicate(dev,
  1255. &edid_cea_modes[cea_mode]);
  1256. if (newmode) {
  1257. drm_mode_probed_add(connector, newmode);
  1258. modes++;
  1259. }
  1260. }
  1261. }
  1262. return modes;
  1263. }
  1264. static int
  1265. add_cea_modes(struct drm_connector *connector, struct edid *edid)
  1266. {
  1267. u8 * cea = drm_find_cea_extension(edid);
  1268. u8 * db, dbl;
  1269. int modes = 0;
  1270. if (cea && cea[1] >= 3) {
  1271. for (db = cea + 4; db < cea + cea[2]; db += dbl + 1) {
  1272. dbl = db[0] & 0x1f;
  1273. if (((db[0] & 0xe0) >> 5) == VIDEO_BLOCK)
  1274. modes += do_cea_modes (connector, db+1, dbl);
  1275. }
  1276. }
  1277. return modes;
  1278. }
  1279. static void
  1280. parse_hdmi_vsdb(struct drm_connector *connector, uint8_t *db)
  1281. {
  1282. connector->eld[5] |= (db[6] >> 7) << 1; /* Supports_AI */
  1283. connector->dvi_dual = db[6] & 1;
  1284. connector->max_tmds_clock = db[7] * 5;
  1285. connector->latency_present[0] = db[8] >> 7;
  1286. connector->latency_present[1] = (db[8] >> 6) & 1;
  1287. connector->video_latency[0] = db[9];
  1288. connector->audio_latency[0] = db[10];
  1289. connector->video_latency[1] = db[11];
  1290. connector->audio_latency[1] = db[12];
  1291. DRM_LOG_KMS("HDMI: DVI dual %d, "
  1292. "max TMDS clock %d, "
  1293. "latency present %d %d, "
  1294. "video latency %d %d, "
  1295. "audio latency %d %d\n",
  1296. connector->dvi_dual,
  1297. connector->max_tmds_clock,
  1298. (int) connector->latency_present[0],
  1299. (int) connector->latency_present[1],
  1300. connector->video_latency[0],
  1301. connector->video_latency[1],
  1302. connector->audio_latency[0],
  1303. connector->audio_latency[1]);
  1304. }
  1305. static void
  1306. monitor_name(struct detailed_timing *t, void *data)
  1307. {
  1308. if (t->data.other_data.type == EDID_DETAIL_MONITOR_NAME)
  1309. *(u8 **)data = t->data.other_data.data.str.str;
  1310. }
  1311. /**
  1312. * drm_edid_to_eld - build ELD from EDID
  1313. * @connector: connector corresponding to the HDMI/DP sink
  1314. * @edid: EDID to parse
  1315. *
  1316. * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver.
  1317. * Some ELD fields are left to the graphics driver caller:
  1318. * - Conn_Type
  1319. * - HDCP
  1320. * - Port_ID
  1321. */
  1322. void drm_edid_to_eld(struct drm_connector *connector, struct edid *edid)
  1323. {
  1324. uint8_t *eld = connector->eld;
  1325. u8 *cea;
  1326. u8 *name;
  1327. u8 *db;
  1328. int sad_count = 0;
  1329. int mnl;
  1330. int dbl;
  1331. memset(eld, 0, sizeof(connector->eld));
  1332. cea = drm_find_cea_extension(edid);
  1333. if (!cea) {
  1334. DRM_DEBUG_KMS("ELD: no CEA Extension found\n");
  1335. return;
  1336. }
  1337. name = NULL;
  1338. drm_for_each_detailed_block((u8 *)edid, monitor_name, &name);
  1339. for (mnl = 0; name && mnl < 13; mnl++) {
  1340. if (name[mnl] == 0x0a)
  1341. break;
  1342. eld[20 + mnl] = name[mnl];
  1343. }
  1344. eld[4] = (cea[1] << 5) | mnl;
  1345. DRM_DEBUG_KMS("ELD monitor %s\n", eld + 20);
  1346. eld[0] = 2 << 3; /* ELD version: 2 */
  1347. eld[16] = edid->mfg_id[0];
  1348. eld[17] = edid->mfg_id[1];
  1349. eld[18] = edid->prod_code[0];
  1350. eld[19] = edid->prod_code[1];
  1351. if (cea[1] >= 3)
  1352. for (db = cea + 4; db < cea + cea[2]; db += dbl + 1) {
  1353. dbl = db[0] & 0x1f;
  1354. switch ((db[0] & 0xe0) >> 5) {
  1355. case AUDIO_BLOCK:
  1356. /* Audio Data Block, contains SADs */
  1357. sad_count = dbl / 3;
  1358. memcpy(eld + 20 + mnl, &db[1], dbl);
  1359. break;
  1360. case SPEAKER_BLOCK:
  1361. /* Speaker Allocation Data Block */
  1362. eld[7] = db[1];
  1363. break;
  1364. case VENDOR_BLOCK:
  1365. /* HDMI Vendor-Specific Data Block */
  1366. if (db[1] == 0x03 && db[2] == 0x0c && db[3] == 0)
  1367. parse_hdmi_vsdb(connector, db);
  1368. break;
  1369. default:
  1370. break;
  1371. }
  1372. }
  1373. eld[5] |= sad_count << 4;
  1374. eld[2] = (20 + mnl + sad_count * 3 + 3) / 4;
  1375. DRM_DEBUG_KMS("ELD size %d, SAD count %d\n", (int)eld[2], sad_count);
  1376. }
  1377. EXPORT_SYMBOL(drm_edid_to_eld);
  1378. /**
  1379. * drm_av_sync_delay - HDMI/DP sink audio-video sync delay in millisecond
  1380. * @connector: connector associated with the HDMI/DP sink
  1381. * @mode: the display mode
  1382. */
  1383. int drm_av_sync_delay(struct drm_connector *connector,
  1384. struct drm_display_mode *mode)
  1385. {
  1386. int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  1387. int a, v;
  1388. if (!connector->latency_present[0])
  1389. return 0;
  1390. if (!connector->latency_present[1])
  1391. i = 0;
  1392. a = connector->audio_latency[i];
  1393. v = connector->video_latency[i];
  1394. /*
  1395. * HDMI/DP sink doesn't support audio or video?
  1396. */
  1397. if (a == 255 || v == 255)
  1398. return 0;
  1399. /*
  1400. * Convert raw EDID values to millisecond.
  1401. * Treat unknown latency as 0ms.
  1402. */
  1403. if (a)
  1404. a = min(2 * (a - 1), 500);
  1405. if (v)
  1406. v = min(2 * (v - 1), 500);
  1407. return max(v - a, 0);
  1408. }
  1409. EXPORT_SYMBOL(drm_av_sync_delay);
  1410. /**
  1411. * drm_select_eld - select one ELD from multiple HDMI/DP sinks
  1412. * @encoder: the encoder just changed display mode
  1413. * @mode: the adjusted display mode
  1414. *
  1415. * It's possible for one encoder to be associated with multiple HDMI/DP sinks.
  1416. * The policy is now hard coded to simply use the first HDMI/DP sink's ELD.
  1417. */
  1418. struct drm_connector *drm_select_eld(struct drm_encoder *encoder,
  1419. struct drm_display_mode *mode)
  1420. {
  1421. struct drm_connector *connector;
  1422. struct drm_device *dev = encoder->dev;
  1423. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1424. if (connector->encoder == encoder && connector->eld[0])
  1425. return connector;
  1426. return NULL;
  1427. }
  1428. EXPORT_SYMBOL(drm_select_eld);
  1429. /**
  1430. * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
  1431. * @edid: monitor EDID information
  1432. *
  1433. * Parse the CEA extension according to CEA-861-B.
  1434. * Return true if HDMI, false if not or unknown.
  1435. */
  1436. bool drm_detect_hdmi_monitor(struct edid *edid)
  1437. {
  1438. u8 *edid_ext;
  1439. int i, hdmi_id;
  1440. int start_offset, end_offset;
  1441. bool is_hdmi = false;
  1442. edid_ext = drm_find_cea_extension(edid);
  1443. if (!edid_ext)
  1444. goto end;
  1445. /* Data block offset in CEA extension block */
  1446. start_offset = 4;
  1447. end_offset = edid_ext[2];
  1448. /*
  1449. * Because HDMI identifier is in Vendor Specific Block,
  1450. * search it from all data blocks of CEA extension.
  1451. */
  1452. for (i = start_offset; i < end_offset;
  1453. /* Increased by data block len */
  1454. i += ((edid_ext[i] & 0x1f) + 1)) {
  1455. /* Find vendor specific block */
  1456. if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
  1457. hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
  1458. edid_ext[i + 3] << 16;
  1459. /* Find HDMI identifier */
  1460. if (hdmi_id == HDMI_IDENTIFIER)
  1461. is_hdmi = true;
  1462. break;
  1463. }
  1464. }
  1465. end:
  1466. return is_hdmi;
  1467. }
  1468. EXPORT_SYMBOL(drm_detect_hdmi_monitor);
  1469. /**
  1470. * drm_detect_monitor_audio - check monitor audio capability
  1471. *
  1472. * Monitor should have CEA extension block.
  1473. * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
  1474. * audio' only. If there is any audio extension block and supported
  1475. * audio format, assume at least 'basic audio' support, even if 'basic
  1476. * audio' is not defined in EDID.
  1477. *
  1478. */
  1479. bool drm_detect_monitor_audio(struct edid *edid)
  1480. {
  1481. u8 *edid_ext;
  1482. int i, j;
  1483. bool has_audio = false;
  1484. int start_offset, end_offset;
  1485. edid_ext = drm_find_cea_extension(edid);
  1486. if (!edid_ext)
  1487. goto end;
  1488. has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
  1489. if (has_audio) {
  1490. DRM_DEBUG_KMS("Monitor has basic audio support\n");
  1491. goto end;
  1492. }
  1493. /* Data block offset in CEA extension block */
  1494. start_offset = 4;
  1495. end_offset = edid_ext[2];
  1496. for (i = start_offset; i < end_offset;
  1497. i += ((edid_ext[i] & 0x1f) + 1)) {
  1498. if ((edid_ext[i] >> 5) == AUDIO_BLOCK) {
  1499. has_audio = true;
  1500. for (j = 1; j < (edid_ext[i] & 0x1f); j += 3)
  1501. DRM_DEBUG_KMS("CEA audio format %d\n",
  1502. (edid_ext[i + j] >> 3) & 0xf);
  1503. goto end;
  1504. }
  1505. }
  1506. end:
  1507. return has_audio;
  1508. }
  1509. EXPORT_SYMBOL(drm_detect_monitor_audio);
  1510. /**
  1511. * drm_add_display_info - pull display info out if present
  1512. * @edid: EDID data
  1513. * @info: display info (attached to connector)
  1514. *
  1515. * Grab any available display info and stuff it into the drm_display_info
  1516. * structure that's part of the connector. Useful for tracking bpp and
  1517. * color spaces.
  1518. */
  1519. static void drm_add_display_info(struct edid *edid,
  1520. struct drm_display_info *info)
  1521. {
  1522. u8 *edid_ext;
  1523. info->width_mm = edid->width_cm * 10;
  1524. info->height_mm = edid->height_cm * 10;
  1525. /* driver figures it out in this case */
  1526. info->bpc = 0;
  1527. info->color_formats = 0;
  1528. if (edid->revision < 3)
  1529. return;
  1530. if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
  1531. return;
  1532. /* Get data from CEA blocks if present */
  1533. edid_ext = drm_find_cea_extension(edid);
  1534. if (edid_ext) {
  1535. info->cea_rev = edid_ext[1];
  1536. /* The existence of a CEA block should imply RGB support */
  1537. info->color_formats = DRM_COLOR_FORMAT_RGB444;
  1538. if (edid_ext[3] & EDID_CEA_YCRCB444)
  1539. info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
  1540. if (edid_ext[3] & EDID_CEA_YCRCB422)
  1541. info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
  1542. }
  1543. /* Only defined for 1.4 with digital displays */
  1544. if (edid->revision < 4)
  1545. return;
  1546. switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
  1547. case DRM_EDID_DIGITAL_DEPTH_6:
  1548. info->bpc = 6;
  1549. break;
  1550. case DRM_EDID_DIGITAL_DEPTH_8:
  1551. info->bpc = 8;
  1552. break;
  1553. case DRM_EDID_DIGITAL_DEPTH_10:
  1554. info->bpc = 10;
  1555. break;
  1556. case DRM_EDID_DIGITAL_DEPTH_12:
  1557. info->bpc = 12;
  1558. break;
  1559. case DRM_EDID_DIGITAL_DEPTH_14:
  1560. info->bpc = 14;
  1561. break;
  1562. case DRM_EDID_DIGITAL_DEPTH_16:
  1563. info->bpc = 16;
  1564. break;
  1565. case DRM_EDID_DIGITAL_DEPTH_UNDEF:
  1566. default:
  1567. info->bpc = 0;
  1568. break;
  1569. }
  1570. info->color_formats |= DRM_COLOR_FORMAT_RGB444;
  1571. if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
  1572. info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
  1573. if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
  1574. info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
  1575. }
  1576. /**
  1577. * drm_add_edid_modes - add modes from EDID data, if available
  1578. * @connector: connector we're probing
  1579. * @edid: edid data
  1580. *
  1581. * Add the specified modes to the connector's mode list.
  1582. *
  1583. * Return number of modes added or 0 if we couldn't find any.
  1584. */
  1585. int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
  1586. {
  1587. int num_modes = 0;
  1588. u32 quirks;
  1589. if (edid == NULL) {
  1590. return 0;
  1591. }
  1592. if (!drm_edid_is_valid(edid)) {
  1593. dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
  1594. drm_get_connector_name(connector));
  1595. return 0;
  1596. }
  1597. quirks = edid_get_quirks(edid);
  1598. /*
  1599. * EDID spec says modes should be preferred in this order:
  1600. * - preferred detailed mode
  1601. * - other detailed modes from base block
  1602. * - detailed modes from extension blocks
  1603. * - CVT 3-byte code modes
  1604. * - standard timing codes
  1605. * - established timing codes
  1606. * - modes inferred from GTF or CVT range information
  1607. *
  1608. * We get this pretty much right.
  1609. *
  1610. * XXX order for additional mode types in extension blocks?
  1611. */
  1612. num_modes += add_detailed_modes(connector, edid, quirks);
  1613. num_modes += add_cvt_modes(connector, edid);
  1614. num_modes += add_standard_modes(connector, edid);
  1615. num_modes += add_established_modes(connector, edid);
  1616. num_modes += add_inferred_modes(connector, edid);
  1617. num_modes += add_cea_modes(connector, edid);
  1618. if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
  1619. edid_fixup_preferred(connector, quirks);
  1620. drm_add_display_info(edid, &connector->display_info);
  1621. return num_modes;
  1622. }
  1623. EXPORT_SYMBOL(drm_add_edid_modes);
  1624. /**
  1625. * drm_add_modes_noedid - add modes for the connectors without EDID
  1626. * @connector: connector we're probing
  1627. * @hdisplay: the horizontal display limit
  1628. * @vdisplay: the vertical display limit
  1629. *
  1630. * Add the specified modes to the connector's mode list. Only when the
  1631. * hdisplay/vdisplay is not beyond the given limit, it will be added.
  1632. *
  1633. * Return number of modes added or 0 if we couldn't find any.
  1634. */
  1635. int drm_add_modes_noedid(struct drm_connector *connector,
  1636. int hdisplay, int vdisplay)
  1637. {
  1638. int i, count, num_modes = 0;
  1639. struct drm_display_mode *mode;
  1640. struct drm_device *dev = connector->dev;
  1641. count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
  1642. if (hdisplay < 0)
  1643. hdisplay = 0;
  1644. if (vdisplay < 0)
  1645. vdisplay = 0;
  1646. for (i = 0; i < count; i++) {
  1647. const struct drm_display_mode *ptr = &drm_dmt_modes[i];
  1648. if (hdisplay && vdisplay) {
  1649. /*
  1650. * Only when two are valid, they will be used to check
  1651. * whether the mode should be added to the mode list of
  1652. * the connector.
  1653. */
  1654. if (ptr->hdisplay > hdisplay ||
  1655. ptr->vdisplay > vdisplay)
  1656. continue;
  1657. }
  1658. if (drm_mode_vrefresh(ptr) > 61)
  1659. continue;
  1660. mode = drm_mode_duplicate(dev, ptr);
  1661. if (mode) {
  1662. drm_mode_probed_add(connector, mode);
  1663. num_modes++;
  1664. }
  1665. }
  1666. return num_modes;
  1667. }
  1668. EXPORT_SYMBOL(drm_add_modes_noedid);