drm_edid.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  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. /* 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. if (memchr_inv(in_edid, 0, length))
  269. return false;
  270. return true;
  271. }
  272. static u8 *
  273. drm_do_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
  274. {
  275. int i, j = 0, valid_extensions = 0;
  276. u8 *block, *new;
  277. bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
  278. if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
  279. return NULL;
  280. /* base block fetch */
  281. for (i = 0; i < 4; i++) {
  282. if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
  283. goto out;
  284. if (drm_edid_block_valid(block, 0, print_bad_edid))
  285. break;
  286. if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
  287. connector->null_edid_counter++;
  288. goto carp;
  289. }
  290. }
  291. if (i == 4)
  292. goto carp;
  293. /* if there's no extensions, we're done */
  294. if (block[0x7e] == 0)
  295. return block;
  296. new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, GFP_KERNEL);
  297. if (!new)
  298. goto out;
  299. block = new;
  300. for (j = 1; j <= block[0x7e]; j++) {
  301. for (i = 0; i < 4; i++) {
  302. if (drm_do_probe_ddc_edid(adapter,
  303. block + (valid_extensions + 1) * EDID_LENGTH,
  304. j, EDID_LENGTH))
  305. goto out;
  306. if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH, j, print_bad_edid)) {
  307. valid_extensions++;
  308. break;
  309. }
  310. }
  311. if (i == 4)
  312. dev_warn(connector->dev->dev,
  313. "%s: Ignoring invalid EDID block %d.\n",
  314. drm_get_connector_name(connector), j);
  315. }
  316. if (valid_extensions != block[0x7e]) {
  317. block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
  318. block[0x7e] = valid_extensions;
  319. new = krealloc(block, (valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
  320. if (!new)
  321. goto out;
  322. block = new;
  323. }
  324. return block;
  325. carp:
  326. if (print_bad_edid) {
  327. dev_warn(connector->dev->dev, "%s: EDID block %d invalid.\n",
  328. drm_get_connector_name(connector), j);
  329. }
  330. connector->bad_edid_counter++;
  331. out:
  332. kfree(block);
  333. return NULL;
  334. }
  335. /**
  336. * Probe DDC presence.
  337. *
  338. * \param adapter : i2c device adaptor
  339. * \return 1 on success
  340. */
  341. bool
  342. drm_probe_ddc(struct i2c_adapter *adapter)
  343. {
  344. unsigned char out;
  345. return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
  346. }
  347. EXPORT_SYMBOL(drm_probe_ddc);
  348. /**
  349. * drm_get_edid - get EDID data, if available
  350. * @connector: connector we're probing
  351. * @adapter: i2c adapter to use for DDC
  352. *
  353. * Poke the given i2c channel to grab EDID data if possible. If found,
  354. * attach it to the connector.
  355. *
  356. * Return edid data or NULL if we couldn't find any.
  357. */
  358. struct edid *drm_get_edid(struct drm_connector *connector,
  359. struct i2c_adapter *adapter)
  360. {
  361. struct edid *edid = NULL;
  362. if (drm_probe_ddc(adapter))
  363. edid = (struct edid *)drm_do_get_edid(connector, adapter);
  364. return edid;
  365. }
  366. EXPORT_SYMBOL(drm_get_edid);
  367. /*** EDID parsing ***/
  368. /**
  369. * edid_vendor - match a string against EDID's obfuscated vendor field
  370. * @edid: EDID to match
  371. * @vendor: vendor string
  372. *
  373. * Returns true if @vendor is in @edid, false otherwise
  374. */
  375. static bool edid_vendor(struct edid *edid, char *vendor)
  376. {
  377. char edid_vendor[3];
  378. edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
  379. edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
  380. ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
  381. edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
  382. return !strncmp(edid_vendor, vendor, 3);
  383. }
  384. /**
  385. * edid_get_quirks - return quirk flags for a given EDID
  386. * @edid: EDID to process
  387. *
  388. * This tells subsequent routines what fixes they need to apply.
  389. */
  390. static u32 edid_get_quirks(struct edid *edid)
  391. {
  392. struct edid_quirk *quirk;
  393. int i;
  394. for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
  395. quirk = &edid_quirk_list[i];
  396. if (edid_vendor(edid, quirk->vendor) &&
  397. (EDID_PRODUCT_ID(edid) == quirk->product_id))
  398. return quirk->quirks;
  399. }
  400. return 0;
  401. }
  402. #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
  403. #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
  404. /**
  405. * edid_fixup_preferred - set preferred modes based on quirk list
  406. * @connector: has mode list to fix up
  407. * @quirks: quirks list
  408. *
  409. * Walk the mode list for @connector, clearing the preferred status
  410. * on existing modes and setting it anew for the right mode ala @quirks.
  411. */
  412. static void edid_fixup_preferred(struct drm_connector *connector,
  413. u32 quirks)
  414. {
  415. struct drm_display_mode *t, *cur_mode, *preferred_mode;
  416. int target_refresh = 0;
  417. if (list_empty(&connector->probed_modes))
  418. return;
  419. if (quirks & EDID_QUIRK_PREFER_LARGE_60)
  420. target_refresh = 60;
  421. if (quirks & EDID_QUIRK_PREFER_LARGE_75)
  422. target_refresh = 75;
  423. preferred_mode = list_first_entry(&connector->probed_modes,
  424. struct drm_display_mode, head);
  425. list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
  426. cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  427. if (cur_mode == preferred_mode)
  428. continue;
  429. /* Largest mode is preferred */
  430. if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
  431. preferred_mode = cur_mode;
  432. /* At a given size, try to get closest to target refresh */
  433. if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
  434. MODE_REFRESH_DIFF(cur_mode, target_refresh) <
  435. MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
  436. preferred_mode = cur_mode;
  437. }
  438. }
  439. preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
  440. }
  441. static bool
  442. mode_is_rb(const struct drm_display_mode *mode)
  443. {
  444. return (mode->htotal - mode->hdisplay == 160) &&
  445. (mode->hsync_end - mode->hdisplay == 80) &&
  446. (mode->hsync_end - mode->hsync_start == 32) &&
  447. (mode->vsync_start - mode->vdisplay == 3);
  448. }
  449. /*
  450. * drm_mode_find_dmt - Create a copy of a mode if present in DMT
  451. * @dev: Device to duplicate against
  452. * @hsize: Mode width
  453. * @vsize: Mode height
  454. * @fresh: Mode refresh rate
  455. * @rb: Mode reduced-blanking-ness
  456. *
  457. * Walk the DMT mode list looking for a match for the given parameters.
  458. * Return a newly allocated copy of the mode, or NULL if not found.
  459. */
  460. struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
  461. int hsize, int vsize, int fresh,
  462. bool rb)
  463. {
  464. int i;
  465. for (i = 0; i < drm_num_dmt_modes; i++) {
  466. const struct drm_display_mode *ptr = &drm_dmt_modes[i];
  467. if (hsize != ptr->hdisplay)
  468. continue;
  469. if (vsize != ptr->vdisplay)
  470. continue;
  471. if (fresh != drm_mode_vrefresh(ptr))
  472. continue;
  473. if (rb != mode_is_rb(ptr))
  474. continue;
  475. return drm_mode_duplicate(dev, ptr);
  476. }
  477. return NULL;
  478. }
  479. EXPORT_SYMBOL(drm_mode_find_dmt);
  480. typedef void detailed_cb(struct detailed_timing *timing, void *closure);
  481. static void
  482. cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
  483. {
  484. int i, n = 0;
  485. u8 d = ext[0x02];
  486. u8 *det_base = ext + d;
  487. n = (127 - d) / 18;
  488. for (i = 0; i < n; i++)
  489. cb((struct detailed_timing *)(det_base + 18 * i), closure);
  490. }
  491. static void
  492. vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
  493. {
  494. unsigned int i, n = min((int)ext[0x02], 6);
  495. u8 *det_base = ext + 5;
  496. if (ext[0x01] != 1)
  497. return; /* unknown version */
  498. for (i = 0; i < n; i++)
  499. cb((struct detailed_timing *)(det_base + 18 * i), closure);
  500. }
  501. static void
  502. drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
  503. {
  504. int i;
  505. struct edid *edid = (struct edid *)raw_edid;
  506. if (edid == NULL)
  507. return;
  508. for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
  509. cb(&(edid->detailed_timings[i]), closure);
  510. for (i = 1; i <= raw_edid[0x7e]; i++) {
  511. u8 *ext = raw_edid + (i * EDID_LENGTH);
  512. switch (*ext) {
  513. case CEA_EXT:
  514. cea_for_each_detailed_block(ext, cb, closure);
  515. break;
  516. case VTB_EXT:
  517. vtb_for_each_detailed_block(ext, cb, closure);
  518. break;
  519. default:
  520. break;
  521. }
  522. }
  523. }
  524. static void
  525. is_rb(struct detailed_timing *t, void *data)
  526. {
  527. u8 *r = (u8 *)t;
  528. if (r[3] == EDID_DETAIL_MONITOR_RANGE)
  529. if (r[15] & 0x10)
  530. *(bool *)data = true;
  531. }
  532. /* EDID 1.4 defines this explicitly. For EDID 1.3, we guess, badly. */
  533. static bool
  534. drm_monitor_supports_rb(struct edid *edid)
  535. {
  536. if (edid->revision >= 4) {
  537. bool ret = false;
  538. drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
  539. return ret;
  540. }
  541. return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
  542. }
  543. static void
  544. find_gtf2(struct detailed_timing *t, void *data)
  545. {
  546. u8 *r = (u8 *)t;
  547. if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
  548. *(u8 **)data = r;
  549. }
  550. /* Secondary GTF curve kicks in above some break frequency */
  551. static int
  552. drm_gtf2_hbreak(struct edid *edid)
  553. {
  554. u8 *r = NULL;
  555. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  556. return r ? (r[12] * 2) : 0;
  557. }
  558. static int
  559. drm_gtf2_2c(struct edid *edid)
  560. {
  561. u8 *r = NULL;
  562. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  563. return r ? r[13] : 0;
  564. }
  565. static int
  566. drm_gtf2_m(struct edid *edid)
  567. {
  568. u8 *r = NULL;
  569. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  570. return r ? (r[15] << 8) + r[14] : 0;
  571. }
  572. static int
  573. drm_gtf2_k(struct edid *edid)
  574. {
  575. u8 *r = NULL;
  576. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  577. return r ? r[16] : 0;
  578. }
  579. static int
  580. drm_gtf2_2j(struct edid *edid)
  581. {
  582. u8 *r = NULL;
  583. drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
  584. return r ? r[17] : 0;
  585. }
  586. /**
  587. * standard_timing_level - get std. timing level(CVT/GTF/DMT)
  588. * @edid: EDID block to scan
  589. */
  590. static int standard_timing_level(struct edid *edid)
  591. {
  592. if (edid->revision >= 2) {
  593. if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
  594. return LEVEL_CVT;
  595. if (drm_gtf2_hbreak(edid))
  596. return LEVEL_GTF2;
  597. return LEVEL_GTF;
  598. }
  599. return LEVEL_DMT;
  600. }
  601. /*
  602. * 0 is reserved. The spec says 0x01 fill for unused timings. Some old
  603. * monitors fill with ascii space (0x20) instead.
  604. */
  605. static int
  606. bad_std_timing(u8 a, u8 b)
  607. {
  608. return (a == 0x00 && b == 0x00) ||
  609. (a == 0x01 && b == 0x01) ||
  610. (a == 0x20 && b == 0x20);
  611. }
  612. /**
  613. * drm_mode_std - convert standard mode info (width, height, refresh) into mode
  614. * @t: standard timing params
  615. * @timing_level: standard timing level
  616. *
  617. * Take the standard timing params (in this case width, aspect, and refresh)
  618. * and convert them into a real mode using CVT/GTF/DMT.
  619. */
  620. static struct drm_display_mode *
  621. drm_mode_std(struct drm_connector *connector, struct edid *edid,
  622. struct std_timing *t, int revision)
  623. {
  624. struct drm_device *dev = connector->dev;
  625. struct drm_display_mode *m, *mode = NULL;
  626. int hsize, vsize;
  627. int vrefresh_rate;
  628. unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
  629. >> EDID_TIMING_ASPECT_SHIFT;
  630. unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
  631. >> EDID_TIMING_VFREQ_SHIFT;
  632. int timing_level = standard_timing_level(edid);
  633. if (bad_std_timing(t->hsize, t->vfreq_aspect))
  634. return NULL;
  635. /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
  636. hsize = t->hsize * 8 + 248;
  637. /* vrefresh_rate = vfreq + 60 */
  638. vrefresh_rate = vfreq + 60;
  639. /* the vdisplay is calculated based on the aspect ratio */
  640. if (aspect_ratio == 0) {
  641. if (revision < 3)
  642. vsize = hsize;
  643. else
  644. vsize = (hsize * 10) / 16;
  645. } else if (aspect_ratio == 1)
  646. vsize = (hsize * 3) / 4;
  647. else if (aspect_ratio == 2)
  648. vsize = (hsize * 4) / 5;
  649. else
  650. vsize = (hsize * 9) / 16;
  651. /* HDTV hack, part 1 */
  652. if (vrefresh_rate == 60 &&
  653. ((hsize == 1360 && vsize == 765) ||
  654. (hsize == 1368 && vsize == 769))) {
  655. hsize = 1366;
  656. vsize = 768;
  657. }
  658. /*
  659. * If this connector already has a mode for this size and refresh
  660. * rate (because it came from detailed or CVT info), use that
  661. * instead. This way we don't have to guess at interlace or
  662. * reduced blanking.
  663. */
  664. list_for_each_entry(m, &connector->probed_modes, head)
  665. if (m->hdisplay == hsize && m->vdisplay == vsize &&
  666. drm_mode_vrefresh(m) == vrefresh_rate)
  667. return NULL;
  668. /* HDTV hack, part 2 */
  669. if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
  670. mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
  671. false);
  672. mode->hdisplay = 1366;
  673. mode->hsync_start = mode->hsync_start - 1;
  674. mode->hsync_end = mode->hsync_end - 1;
  675. return mode;
  676. }
  677. /* check whether it can be found in default mode table */
  678. if (drm_monitor_supports_rb(edid)) {
  679. mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
  680. true);
  681. if (mode)
  682. return mode;
  683. }
  684. mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
  685. if (mode)
  686. return mode;
  687. /* okay, generate it */
  688. switch (timing_level) {
  689. case LEVEL_DMT:
  690. break;
  691. case LEVEL_GTF:
  692. mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
  693. break;
  694. case LEVEL_GTF2:
  695. /*
  696. * This is potentially wrong if there's ever a monitor with
  697. * more than one ranges section, each claiming a different
  698. * secondary GTF curve. Please don't do that.
  699. */
  700. mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
  701. if (!mode)
  702. return NULL;
  703. if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
  704. drm_mode_destroy(dev, mode);
  705. mode = drm_gtf_mode_complex(dev, hsize, vsize,
  706. vrefresh_rate, 0, 0,
  707. drm_gtf2_m(edid),
  708. drm_gtf2_2c(edid),
  709. drm_gtf2_k(edid),
  710. drm_gtf2_2j(edid));
  711. }
  712. break;
  713. case LEVEL_CVT:
  714. mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
  715. false);
  716. break;
  717. }
  718. return mode;
  719. }
  720. /*
  721. * EDID is delightfully ambiguous about how interlaced modes are to be
  722. * encoded. Our internal representation is of frame height, but some
  723. * HDTV detailed timings are encoded as field height.
  724. *
  725. * The format list here is from CEA, in frame size. Technically we
  726. * should be checking refresh rate too. Whatever.
  727. */
  728. static void
  729. drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
  730. struct detailed_pixel_timing *pt)
  731. {
  732. int i;
  733. static const struct {
  734. int w, h;
  735. } cea_interlaced[] = {
  736. { 1920, 1080 },
  737. { 720, 480 },
  738. { 1440, 480 },
  739. { 2880, 480 },
  740. { 720, 576 },
  741. { 1440, 576 },
  742. { 2880, 576 },
  743. };
  744. if (!(pt->misc & DRM_EDID_PT_INTERLACED))
  745. return;
  746. for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
  747. if ((mode->hdisplay == cea_interlaced[i].w) &&
  748. (mode->vdisplay == cea_interlaced[i].h / 2)) {
  749. mode->vdisplay *= 2;
  750. mode->vsync_start *= 2;
  751. mode->vsync_end *= 2;
  752. mode->vtotal *= 2;
  753. mode->vtotal |= 1;
  754. }
  755. }
  756. mode->flags |= DRM_MODE_FLAG_INTERLACE;
  757. }
  758. /**
  759. * drm_mode_detailed - create a new mode from an EDID detailed timing section
  760. * @dev: DRM device (needed to create new mode)
  761. * @edid: EDID block
  762. * @timing: EDID detailed timing info
  763. * @quirks: quirks to apply
  764. *
  765. * An EDID detailed timing block contains enough info for us to create and
  766. * return a new struct drm_display_mode.
  767. */
  768. static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
  769. struct edid *edid,
  770. struct detailed_timing *timing,
  771. u32 quirks)
  772. {
  773. struct drm_display_mode *mode;
  774. struct detailed_pixel_timing *pt = &timing->data.pixel_data;
  775. unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
  776. unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
  777. unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
  778. unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
  779. unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
  780. unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
  781. unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
  782. unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
  783. /* ignore tiny modes */
  784. if (hactive < 64 || vactive < 64)
  785. return NULL;
  786. if (pt->misc & DRM_EDID_PT_STEREO) {
  787. printk(KERN_WARNING "stereo mode not supported\n");
  788. return NULL;
  789. }
  790. if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
  791. printk(KERN_WARNING "composite sync not supported\n");
  792. }
  793. /* it is incorrect if hsync/vsync width is zero */
  794. if (!hsync_pulse_width || !vsync_pulse_width) {
  795. DRM_DEBUG_KMS("Incorrect Detailed timing. "
  796. "Wrong Hsync/Vsync pulse width\n");
  797. return NULL;
  798. }
  799. if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
  800. mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
  801. if (!mode)
  802. return NULL;
  803. goto set_size;
  804. }
  805. mode = drm_mode_create(dev);
  806. if (!mode)
  807. return NULL;
  808. if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
  809. timing->pixel_clock = cpu_to_le16(1088);
  810. mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
  811. mode->hdisplay = hactive;
  812. mode->hsync_start = mode->hdisplay + hsync_offset;
  813. mode->hsync_end = mode->hsync_start + hsync_pulse_width;
  814. mode->htotal = mode->hdisplay + hblank;
  815. mode->vdisplay = vactive;
  816. mode->vsync_start = mode->vdisplay + vsync_offset;
  817. mode->vsync_end = mode->vsync_start + vsync_pulse_width;
  818. mode->vtotal = mode->vdisplay + vblank;
  819. /* Some EDIDs have bogus h/vtotal values */
  820. if (mode->hsync_end > mode->htotal)
  821. mode->htotal = mode->hsync_end + 1;
  822. if (mode->vsync_end > mode->vtotal)
  823. mode->vtotal = mode->vsync_end + 1;
  824. drm_mode_do_interlace_quirk(mode, pt);
  825. if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
  826. pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
  827. }
  828. mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
  829. DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  830. mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
  831. DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  832. set_size:
  833. mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
  834. mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
  835. if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
  836. mode->width_mm *= 10;
  837. mode->height_mm *= 10;
  838. }
  839. if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
  840. mode->width_mm = edid->width_cm * 10;
  841. mode->height_mm = edid->height_cm * 10;
  842. }
  843. mode->type = DRM_MODE_TYPE_DRIVER;
  844. drm_mode_set_name(mode);
  845. return mode;
  846. }
  847. static bool
  848. mode_in_hsync_range(const struct drm_display_mode *mode,
  849. struct edid *edid, u8 *t)
  850. {
  851. int hsync, hmin, hmax;
  852. hmin = t[7];
  853. if (edid->revision >= 4)
  854. hmin += ((t[4] & 0x04) ? 255 : 0);
  855. hmax = t[8];
  856. if (edid->revision >= 4)
  857. hmax += ((t[4] & 0x08) ? 255 : 0);
  858. hsync = drm_mode_hsync(mode);
  859. return (hsync <= hmax && hsync >= hmin);
  860. }
  861. static bool
  862. mode_in_vsync_range(const struct drm_display_mode *mode,
  863. struct edid *edid, u8 *t)
  864. {
  865. int vsync, vmin, vmax;
  866. vmin = t[5];
  867. if (edid->revision >= 4)
  868. vmin += ((t[4] & 0x01) ? 255 : 0);
  869. vmax = t[6];
  870. if (edid->revision >= 4)
  871. vmax += ((t[4] & 0x02) ? 255 : 0);
  872. vsync = drm_mode_vrefresh(mode);
  873. return (vsync <= vmax && vsync >= vmin);
  874. }
  875. static u32
  876. range_pixel_clock(struct edid *edid, u8 *t)
  877. {
  878. /* unspecified */
  879. if (t[9] == 0 || t[9] == 255)
  880. return 0;
  881. /* 1.4 with CVT support gives us real precision, yay */
  882. if (edid->revision >= 4 && t[10] == 0x04)
  883. return (t[9] * 10000) - ((t[12] >> 2) * 250);
  884. /* 1.3 is pathetic, so fuzz up a bit */
  885. return t[9] * 10000 + 5001;
  886. }
  887. static bool
  888. mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
  889. struct detailed_timing *timing)
  890. {
  891. u32 max_clock;
  892. u8 *t = (u8 *)timing;
  893. if (!mode_in_hsync_range(mode, edid, t))
  894. return false;
  895. if (!mode_in_vsync_range(mode, edid, t))
  896. return false;
  897. if ((max_clock = range_pixel_clock(edid, t)))
  898. if (mode->clock > max_clock)
  899. return false;
  900. /* 1.4 max horizontal check */
  901. if (edid->revision >= 4 && t[10] == 0x04)
  902. if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
  903. return false;
  904. if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
  905. return false;
  906. return true;
  907. }
  908. static bool valid_inferred_mode(const struct drm_connector *connector,
  909. const struct drm_display_mode *mode)
  910. {
  911. struct drm_display_mode *m;
  912. bool ok = false;
  913. list_for_each_entry(m, &connector->probed_modes, head) {
  914. if (mode->hdisplay == m->hdisplay &&
  915. mode->vdisplay == m->vdisplay &&
  916. drm_mode_vrefresh(mode) == drm_mode_vrefresh(m))
  917. return false; /* duplicated */
  918. if (mode->hdisplay <= m->hdisplay &&
  919. mode->vdisplay <= m->vdisplay)
  920. ok = true;
  921. }
  922. return ok;
  923. }
  924. static int
  925. drm_dmt_modes_for_range(struct drm_connector *connector, struct edid *edid,
  926. struct detailed_timing *timing)
  927. {
  928. int i, modes = 0;
  929. struct drm_display_mode *newmode;
  930. struct drm_device *dev = connector->dev;
  931. for (i = 0; i < drm_num_dmt_modes; i++) {
  932. if (mode_in_range(drm_dmt_modes + i, edid, timing) &&
  933. valid_inferred_mode(connector, drm_dmt_modes + i)) {
  934. newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
  935. if (newmode) {
  936. drm_mode_probed_add(connector, newmode);
  937. modes++;
  938. }
  939. }
  940. }
  941. return modes;
  942. }
  943. /* fix up 1366x768 mode from 1368x768;
  944. * GFT/CVT can't express 1366 width which isn't dividable by 8
  945. */
  946. static void fixup_mode_1366x768(struct drm_display_mode *mode)
  947. {
  948. if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
  949. mode->hdisplay = 1366;
  950. mode->hsync_start--;
  951. mode->hsync_end--;
  952. drm_mode_set_name(mode);
  953. }
  954. }
  955. static int
  956. drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
  957. struct detailed_timing *timing)
  958. {
  959. int i, modes = 0;
  960. struct drm_display_mode *newmode;
  961. struct drm_device *dev = connector->dev;
  962. for (i = 0; i < num_extra_modes; i++) {
  963. const struct minimode *m = &extra_modes[i];
  964. newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
  965. if (!newmode)
  966. return modes;
  967. fixup_mode_1366x768(newmode);
  968. if (!mode_in_range(newmode, edid, timing) ||
  969. !valid_inferred_mode(connector, newmode)) {
  970. drm_mode_destroy(dev, newmode);
  971. continue;
  972. }
  973. drm_mode_probed_add(connector, newmode);
  974. modes++;
  975. }
  976. return modes;
  977. }
  978. static int
  979. drm_cvt_modes_for_range(struct drm_connector *connector, struct edid *edid,
  980. struct detailed_timing *timing)
  981. {
  982. int i, modes = 0;
  983. struct drm_display_mode *newmode;
  984. struct drm_device *dev = connector->dev;
  985. bool rb = drm_monitor_supports_rb(edid);
  986. for (i = 0; i < num_extra_modes; i++) {
  987. const struct minimode *m = &extra_modes[i];
  988. newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
  989. if (!newmode)
  990. return modes;
  991. fixup_mode_1366x768(newmode);
  992. if (!mode_in_range(newmode, edid, timing) ||
  993. !valid_inferred_mode(connector, newmode)) {
  994. drm_mode_destroy(dev, newmode);
  995. continue;
  996. }
  997. drm_mode_probed_add(connector, newmode);
  998. modes++;
  999. }
  1000. return modes;
  1001. }
  1002. static void
  1003. do_inferred_modes(struct detailed_timing *timing, void *c)
  1004. {
  1005. struct detailed_mode_closure *closure = c;
  1006. struct detailed_non_pixel *data = &timing->data.other_data;
  1007. struct detailed_data_monitor_range *range = &data->data.range;
  1008. if (data->type != EDID_DETAIL_MONITOR_RANGE)
  1009. return;
  1010. closure->modes += drm_dmt_modes_for_range(closure->connector,
  1011. closure->edid,
  1012. timing);
  1013. if (!version_greater(closure->edid, 1, 1))
  1014. return; /* GTF not defined yet */
  1015. switch (range->flags) {
  1016. case 0x02: /* secondary gtf, XXX could do more */
  1017. case 0x00: /* default gtf */
  1018. closure->modes += drm_gtf_modes_for_range(closure->connector,
  1019. closure->edid,
  1020. timing);
  1021. break;
  1022. case 0x04: /* cvt, only in 1.4+ */
  1023. if (!version_greater(closure->edid, 1, 3))
  1024. break;
  1025. closure->modes += drm_cvt_modes_for_range(closure->connector,
  1026. closure->edid,
  1027. timing);
  1028. break;
  1029. case 0x01: /* just the ranges, no formula */
  1030. default:
  1031. break;
  1032. }
  1033. }
  1034. static int
  1035. add_inferred_modes(struct drm_connector *connector, struct edid *edid)
  1036. {
  1037. struct detailed_mode_closure closure = {
  1038. connector, edid, 0, 0, 0
  1039. };
  1040. if (version_greater(edid, 1, 0))
  1041. drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
  1042. &closure);
  1043. return closure.modes;
  1044. }
  1045. static int
  1046. drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
  1047. {
  1048. int i, j, m, modes = 0;
  1049. struct drm_display_mode *mode;
  1050. u8 *est = ((u8 *)timing) + 5;
  1051. for (i = 0; i < 6; i++) {
  1052. for (j = 7; j > 0; j--) {
  1053. m = (i * 8) + (7 - j);
  1054. if (m >= ARRAY_SIZE(est3_modes))
  1055. break;
  1056. if (est[i] & (1 << j)) {
  1057. mode = drm_mode_find_dmt(connector->dev,
  1058. est3_modes[m].w,
  1059. est3_modes[m].h,
  1060. est3_modes[m].r,
  1061. est3_modes[m].rb);
  1062. if (mode) {
  1063. drm_mode_probed_add(connector, mode);
  1064. modes++;
  1065. }
  1066. }
  1067. }
  1068. }
  1069. return modes;
  1070. }
  1071. static void
  1072. do_established_modes(struct detailed_timing *timing, void *c)
  1073. {
  1074. struct detailed_mode_closure *closure = c;
  1075. struct detailed_non_pixel *data = &timing->data.other_data;
  1076. if (data->type == EDID_DETAIL_EST_TIMINGS)
  1077. closure->modes += drm_est3_modes(closure->connector, timing);
  1078. }
  1079. /**
  1080. * add_established_modes - get est. modes from EDID and add them
  1081. * @edid: EDID block to scan
  1082. *
  1083. * Each EDID block contains a bitmap of the supported "established modes" list
  1084. * (defined above). Tease them out and add them to the global modes list.
  1085. */
  1086. static int
  1087. add_established_modes(struct drm_connector *connector, struct edid *edid)
  1088. {
  1089. struct drm_device *dev = connector->dev;
  1090. unsigned long est_bits = edid->established_timings.t1 |
  1091. (edid->established_timings.t2 << 8) |
  1092. ((edid->established_timings.mfg_rsvd & 0x80) << 9);
  1093. int i, modes = 0;
  1094. struct detailed_mode_closure closure = {
  1095. connector, edid, 0, 0, 0
  1096. };
  1097. for (i = 0; i <= EDID_EST_TIMINGS; i++) {
  1098. if (est_bits & (1<<i)) {
  1099. struct drm_display_mode *newmode;
  1100. newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
  1101. if (newmode) {
  1102. drm_mode_probed_add(connector, newmode);
  1103. modes++;
  1104. }
  1105. }
  1106. }
  1107. if (version_greater(edid, 1, 0))
  1108. drm_for_each_detailed_block((u8 *)edid,
  1109. do_established_modes, &closure);
  1110. return modes + closure.modes;
  1111. }
  1112. static void
  1113. do_standard_modes(struct detailed_timing *timing, void *c)
  1114. {
  1115. struct detailed_mode_closure *closure = c;
  1116. struct detailed_non_pixel *data = &timing->data.other_data;
  1117. struct drm_connector *connector = closure->connector;
  1118. struct edid *edid = closure->edid;
  1119. if (data->type == EDID_DETAIL_STD_MODES) {
  1120. int i;
  1121. for (i = 0; i < 6; i++) {
  1122. struct std_timing *std;
  1123. struct drm_display_mode *newmode;
  1124. std = &data->data.timings[i];
  1125. newmode = drm_mode_std(connector, edid, std,
  1126. edid->revision);
  1127. if (newmode) {
  1128. drm_mode_probed_add(connector, newmode);
  1129. closure->modes++;
  1130. }
  1131. }
  1132. }
  1133. }
  1134. /**
  1135. * add_standard_modes - get std. modes from EDID and add them
  1136. * @edid: EDID block to scan
  1137. *
  1138. * Standard modes can be calculated using the appropriate standard (DMT,
  1139. * GTF or CVT. Grab them from @edid and add them to the list.
  1140. */
  1141. static int
  1142. add_standard_modes(struct drm_connector *connector, struct edid *edid)
  1143. {
  1144. int i, modes = 0;
  1145. struct detailed_mode_closure closure = {
  1146. connector, edid, 0, 0, 0
  1147. };
  1148. for (i = 0; i < EDID_STD_TIMINGS; i++) {
  1149. struct drm_display_mode *newmode;
  1150. newmode = drm_mode_std(connector, edid,
  1151. &edid->standard_timings[i],
  1152. edid->revision);
  1153. if (newmode) {
  1154. drm_mode_probed_add(connector, newmode);
  1155. modes++;
  1156. }
  1157. }
  1158. if (version_greater(edid, 1, 0))
  1159. drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
  1160. &closure);
  1161. /* XXX should also look for standard codes in VTB blocks */
  1162. return modes + closure.modes;
  1163. }
  1164. static int drm_cvt_modes(struct drm_connector *connector,
  1165. struct detailed_timing *timing)
  1166. {
  1167. int i, j, modes = 0;
  1168. struct drm_display_mode *newmode;
  1169. struct drm_device *dev = connector->dev;
  1170. struct cvt_timing *cvt;
  1171. const int rates[] = { 60, 85, 75, 60, 50 };
  1172. const u8 empty[3] = { 0, 0, 0 };
  1173. for (i = 0; i < 4; i++) {
  1174. int uninitialized_var(width), height;
  1175. cvt = &(timing->data.other_data.data.cvt[i]);
  1176. if (!memcmp(cvt->code, empty, 3))
  1177. continue;
  1178. height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
  1179. switch (cvt->code[1] & 0x0c) {
  1180. case 0x00:
  1181. width = height * 4 / 3;
  1182. break;
  1183. case 0x04:
  1184. width = height * 16 / 9;
  1185. break;
  1186. case 0x08:
  1187. width = height * 16 / 10;
  1188. break;
  1189. case 0x0c:
  1190. width = height * 15 / 9;
  1191. break;
  1192. }
  1193. for (j = 1; j < 5; j++) {
  1194. if (cvt->code[2] & (1 << j)) {
  1195. newmode = drm_cvt_mode(dev, width, height,
  1196. rates[j], j == 0,
  1197. false, false);
  1198. if (newmode) {
  1199. drm_mode_probed_add(connector, newmode);
  1200. modes++;
  1201. }
  1202. }
  1203. }
  1204. }
  1205. return modes;
  1206. }
  1207. static void
  1208. do_cvt_mode(struct detailed_timing *timing, void *c)
  1209. {
  1210. struct detailed_mode_closure *closure = c;
  1211. struct detailed_non_pixel *data = &timing->data.other_data;
  1212. if (data->type == EDID_DETAIL_CVT_3BYTE)
  1213. closure->modes += drm_cvt_modes(closure->connector, timing);
  1214. }
  1215. static int
  1216. add_cvt_modes(struct drm_connector *connector, struct edid *edid)
  1217. {
  1218. struct detailed_mode_closure closure = {
  1219. connector, edid, 0, 0, 0
  1220. };
  1221. if (version_greater(edid, 1, 2))
  1222. drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
  1223. /* XXX should also look for CVT codes in VTB blocks */
  1224. return closure.modes;
  1225. }
  1226. static void
  1227. do_detailed_mode(struct detailed_timing *timing, void *c)
  1228. {
  1229. struct detailed_mode_closure *closure = c;
  1230. struct drm_display_mode *newmode;
  1231. if (timing->pixel_clock) {
  1232. newmode = drm_mode_detailed(closure->connector->dev,
  1233. closure->edid, timing,
  1234. closure->quirks);
  1235. if (!newmode)
  1236. return;
  1237. if (closure->preferred)
  1238. newmode->type |= DRM_MODE_TYPE_PREFERRED;
  1239. drm_mode_probed_add(closure->connector, newmode);
  1240. closure->modes++;
  1241. closure->preferred = 0;
  1242. }
  1243. }
  1244. /*
  1245. * add_detailed_modes - Add modes from detailed timings
  1246. * @connector: attached connector
  1247. * @edid: EDID block to scan
  1248. * @quirks: quirks to apply
  1249. */
  1250. static int
  1251. add_detailed_modes(struct drm_connector *connector, struct edid *edid,
  1252. u32 quirks)
  1253. {
  1254. struct detailed_mode_closure closure = {
  1255. connector,
  1256. edid,
  1257. 1,
  1258. quirks,
  1259. 0
  1260. };
  1261. if (closure.preferred && !version_greater(edid, 1, 3))
  1262. closure.preferred =
  1263. (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
  1264. drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
  1265. return closure.modes;
  1266. }
  1267. #define HDMI_IDENTIFIER 0x000C03
  1268. #define AUDIO_BLOCK 0x01
  1269. #define VIDEO_BLOCK 0x02
  1270. #define VENDOR_BLOCK 0x03
  1271. #define SPEAKER_BLOCK 0x04
  1272. #define EDID_BASIC_AUDIO (1 << 6)
  1273. #define EDID_CEA_YCRCB444 (1 << 5)
  1274. #define EDID_CEA_YCRCB422 (1 << 4)
  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_add_display_info - pull display info out if present
  1628. * @edid: EDID data
  1629. * @info: display info (attached to connector)
  1630. *
  1631. * Grab any available display info and stuff it into the drm_display_info
  1632. * structure that's part of the connector. Useful for tracking bpp and
  1633. * color spaces.
  1634. */
  1635. static void drm_add_display_info(struct edid *edid,
  1636. struct drm_display_info *info)
  1637. {
  1638. u8 *edid_ext;
  1639. info->width_mm = edid->width_cm * 10;
  1640. info->height_mm = edid->height_cm * 10;
  1641. /* driver figures it out in this case */
  1642. info->bpc = 0;
  1643. info->color_formats = 0;
  1644. if (edid->revision < 3)
  1645. return;
  1646. if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
  1647. return;
  1648. /* Get data from CEA blocks if present */
  1649. edid_ext = drm_find_cea_extension(edid);
  1650. if (edid_ext) {
  1651. info->cea_rev = edid_ext[1];
  1652. /* The existence of a CEA block should imply RGB support */
  1653. info->color_formats = DRM_COLOR_FORMAT_RGB444;
  1654. if (edid_ext[3] & EDID_CEA_YCRCB444)
  1655. info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
  1656. if (edid_ext[3] & EDID_CEA_YCRCB422)
  1657. info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
  1658. }
  1659. /* Only defined for 1.4 with digital displays */
  1660. if (edid->revision < 4)
  1661. return;
  1662. switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
  1663. case DRM_EDID_DIGITAL_DEPTH_6:
  1664. info->bpc = 6;
  1665. break;
  1666. case DRM_EDID_DIGITAL_DEPTH_8:
  1667. info->bpc = 8;
  1668. break;
  1669. case DRM_EDID_DIGITAL_DEPTH_10:
  1670. info->bpc = 10;
  1671. break;
  1672. case DRM_EDID_DIGITAL_DEPTH_12:
  1673. info->bpc = 12;
  1674. break;
  1675. case DRM_EDID_DIGITAL_DEPTH_14:
  1676. info->bpc = 14;
  1677. break;
  1678. case DRM_EDID_DIGITAL_DEPTH_16:
  1679. info->bpc = 16;
  1680. break;
  1681. case DRM_EDID_DIGITAL_DEPTH_UNDEF:
  1682. default:
  1683. info->bpc = 0;
  1684. break;
  1685. }
  1686. info->color_formats |= DRM_COLOR_FORMAT_RGB444;
  1687. if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
  1688. info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
  1689. if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
  1690. info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
  1691. }
  1692. /**
  1693. * drm_add_edid_modes - add modes from EDID data, if available
  1694. * @connector: connector we're probing
  1695. * @edid: edid data
  1696. *
  1697. * Add the specified modes to the connector's mode list.
  1698. *
  1699. * Return number of modes added or 0 if we couldn't find any.
  1700. */
  1701. int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
  1702. {
  1703. int num_modes = 0;
  1704. u32 quirks;
  1705. if (edid == NULL) {
  1706. return 0;
  1707. }
  1708. if (!drm_edid_is_valid(edid)) {
  1709. dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
  1710. drm_get_connector_name(connector));
  1711. return 0;
  1712. }
  1713. quirks = edid_get_quirks(edid);
  1714. /*
  1715. * EDID spec says modes should be preferred in this order:
  1716. * - preferred detailed mode
  1717. * - other detailed modes from base block
  1718. * - detailed modes from extension blocks
  1719. * - CVT 3-byte code modes
  1720. * - standard timing codes
  1721. * - established timing codes
  1722. * - modes inferred from GTF or CVT range information
  1723. *
  1724. * We get this pretty much right.
  1725. *
  1726. * XXX order for additional mode types in extension blocks?
  1727. */
  1728. num_modes += add_detailed_modes(connector, edid, quirks);
  1729. num_modes += add_cvt_modes(connector, edid);
  1730. num_modes += add_standard_modes(connector, edid);
  1731. num_modes += add_established_modes(connector, edid);
  1732. num_modes += add_inferred_modes(connector, edid);
  1733. num_modes += add_cea_modes(connector, edid);
  1734. if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
  1735. edid_fixup_preferred(connector, quirks);
  1736. drm_add_display_info(edid, &connector->display_info);
  1737. return num_modes;
  1738. }
  1739. EXPORT_SYMBOL(drm_add_edid_modes);
  1740. /**
  1741. * drm_add_modes_noedid - add modes for the connectors without EDID
  1742. * @connector: connector we're probing
  1743. * @hdisplay: the horizontal display limit
  1744. * @vdisplay: the vertical display limit
  1745. *
  1746. * Add the specified modes to the connector's mode list. Only when the
  1747. * hdisplay/vdisplay is not beyond the given limit, it will be added.
  1748. *
  1749. * Return number of modes added or 0 if we couldn't find any.
  1750. */
  1751. int drm_add_modes_noedid(struct drm_connector *connector,
  1752. int hdisplay, int vdisplay)
  1753. {
  1754. int i, count, num_modes = 0;
  1755. struct drm_display_mode *mode;
  1756. struct drm_device *dev = connector->dev;
  1757. count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
  1758. if (hdisplay < 0)
  1759. hdisplay = 0;
  1760. if (vdisplay < 0)
  1761. vdisplay = 0;
  1762. for (i = 0; i < count; i++) {
  1763. const struct drm_display_mode *ptr = &drm_dmt_modes[i];
  1764. if (hdisplay && vdisplay) {
  1765. /*
  1766. * Only when two are valid, they will be used to check
  1767. * whether the mode should be added to the mode list of
  1768. * the connector.
  1769. */
  1770. if (ptr->hdisplay > hdisplay ||
  1771. ptr->vdisplay > vdisplay)
  1772. continue;
  1773. }
  1774. if (drm_mode_vrefresh(ptr) > 61)
  1775. continue;
  1776. mode = drm_mode_duplicate(dev, ptr);
  1777. if (mode) {
  1778. drm_mode_probed_add(connector, mode);
  1779. num_modes++;
  1780. }
  1781. }
  1782. return num_modes;
  1783. }
  1784. EXPORT_SYMBOL(drm_add_modes_noedid);
  1785. /**
  1786. * drm_mode_cea_vic - return the CEA-861 VIC of a given mode
  1787. * @mode: mode
  1788. *
  1789. * RETURNS:
  1790. * The VIC number, 0 in case it's not a CEA-861 mode.
  1791. */
  1792. uint8_t drm_mode_cea_vic(const struct drm_display_mode *mode)
  1793. {
  1794. uint8_t i;
  1795. for (i = 0; i < drm_num_cea_modes; i++)
  1796. if (drm_mode_equal(mode, &edid_cea_modes[i]))
  1797. return i + 1;
  1798. return 0;
  1799. }
  1800. EXPORT_SYMBOL(drm_mode_cea_vic);