drm_edid.c 51 KB

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