drm_edid.c 45 KB

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