drm_edid.c 40 KB

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