drm_edid.c 50 KB

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