drm_edid.c 50 KB

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