drm_edid.c 53 KB

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