drm_edid.c 46 KB

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