drm_modes.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. /*
  2. * Copyright © 1997-2003 by The XFree86 Project, Inc.
  3. * Copyright © 2007 Dave Airlie
  4. * Copyright © 2007-2008 Intel Corporation
  5. * Jesse Barnes <jesse.barnes@intel.com>
  6. * Copyright 2005-2006 Luc Verhaegen
  7. * Copyright (c) 2001, Andy Ritger aritger@nvidia.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. * Except as contained in this notice, the name of the copyright holder(s)
  28. * and author(s) shall not be used in advertising or otherwise to promote
  29. * the sale, use or other dealings in this Software without prior written
  30. * authorization from the copyright holder(s) and author(s).
  31. */
  32. #include <linux/list.h>
  33. #include <linux/list_sort.h>
  34. #include <linux/export.h>
  35. #include <drm/drmP.h>
  36. #include <drm/drm_crtc.h>
  37. #include <video/of_videomode.h>
  38. #include <video/videomode.h>
  39. #include <drm/drm_modes.h>
  40. #include "drm_crtc_internal.h"
  41. /**
  42. * drm_mode_debug_printmodeline - print a mode to dmesg
  43. * @mode: mode to print
  44. *
  45. * Describe @mode using DRM_DEBUG.
  46. */
  47. void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
  48. {
  49. DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
  50. }
  51. EXPORT_SYMBOL(drm_mode_debug_printmodeline);
  52. /**
  53. * drm_mode_create - create a new display mode
  54. * @dev: DRM device
  55. *
  56. * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
  57. * and return it.
  58. *
  59. * Returns:
  60. * Pointer to new mode on success, NULL on error.
  61. */
  62. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  63. {
  64. struct drm_display_mode *nmode;
  65. nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  66. if (!nmode)
  67. return NULL;
  68. if (drm_mode_object_add(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
  69. kfree(nmode);
  70. return NULL;
  71. }
  72. return nmode;
  73. }
  74. EXPORT_SYMBOL(drm_mode_create);
  75. /**
  76. * drm_mode_destroy - remove a mode
  77. * @dev: DRM device
  78. * @mode: mode to remove
  79. *
  80. * Release @mode's unique ID, then free it @mode structure itself using kfree.
  81. */
  82. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  83. {
  84. if (!mode)
  85. return;
  86. drm_mode_object_unregister(dev, &mode->base);
  87. kfree(mode);
  88. }
  89. EXPORT_SYMBOL(drm_mode_destroy);
  90. /**
  91. * drm_mode_probed_add - add a mode to a connector's probed_mode list
  92. * @connector: connector the new mode
  93. * @mode: mode data
  94. *
  95. * Add @mode to @connector's probed_mode list for later use. This list should
  96. * then in a second step get filtered and all the modes actually supported by
  97. * the hardware moved to the @connector's modes list.
  98. */
  99. void drm_mode_probed_add(struct drm_connector *connector,
  100. struct drm_display_mode *mode)
  101. {
  102. WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
  103. list_add_tail(&mode->head, &connector->probed_modes);
  104. }
  105. EXPORT_SYMBOL(drm_mode_probed_add);
  106. /**
  107. * drm_cvt_mode -create a modeline based on the CVT algorithm
  108. * @dev: drm device
  109. * @hdisplay: hdisplay size
  110. * @vdisplay: vdisplay size
  111. * @vrefresh: vrefresh rate
  112. * @reduced: whether to use reduced blanking
  113. * @interlaced: whether to compute an interlaced mode
  114. * @margins: whether to add margins (borders)
  115. *
  116. * This function is called to generate the modeline based on CVT algorithm
  117. * according to the hdisplay, vdisplay, vrefresh.
  118. * It is based from the VESA(TM) Coordinated Video Timing Generator by
  119. * Graham Loveridge April 9, 2003 available at
  120. * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
  121. *
  122. * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
  123. * What I have done is to translate it by using integer calculation.
  124. *
  125. * Returns:
  126. * The modeline based on the CVT algorithm stored in a drm_display_mode object.
  127. * The display mode object is allocated with drm_mode_create(). Returns NULL
  128. * when no mode could be allocated.
  129. */
  130. struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
  131. int vdisplay, int vrefresh,
  132. bool reduced, bool interlaced, bool margins)
  133. {
  134. #define HV_FACTOR 1000
  135. /* 1) top/bottom margin size (% of height) - default: 1.8, */
  136. #define CVT_MARGIN_PERCENTAGE 18
  137. /* 2) character cell horizontal granularity (pixels) - default 8 */
  138. #define CVT_H_GRANULARITY 8
  139. /* 3) Minimum vertical porch (lines) - default 3 */
  140. #define CVT_MIN_V_PORCH 3
  141. /* 4) Minimum number of vertical back porch lines - default 6 */
  142. #define CVT_MIN_V_BPORCH 6
  143. /* Pixel Clock step (kHz) */
  144. #define CVT_CLOCK_STEP 250
  145. struct drm_display_mode *drm_mode;
  146. unsigned int vfieldrate, hperiod;
  147. int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
  148. int interlace;
  149. u64 tmp;
  150. /* allocate the drm_display_mode structure. If failure, we will
  151. * return directly
  152. */
  153. drm_mode = drm_mode_create(dev);
  154. if (!drm_mode)
  155. return NULL;
  156. /* the CVT default refresh rate is 60Hz */
  157. if (!vrefresh)
  158. vrefresh = 60;
  159. /* the required field fresh rate */
  160. if (interlaced)
  161. vfieldrate = vrefresh * 2;
  162. else
  163. vfieldrate = vrefresh;
  164. /* horizontal pixels */
  165. hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
  166. /* determine the left&right borders */
  167. hmargin = 0;
  168. if (margins) {
  169. hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  170. hmargin -= hmargin % CVT_H_GRANULARITY;
  171. }
  172. /* find the total active pixels */
  173. drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
  174. /* find the number of lines per field */
  175. if (interlaced)
  176. vdisplay_rnd = vdisplay / 2;
  177. else
  178. vdisplay_rnd = vdisplay;
  179. /* find the top & bottom borders */
  180. vmargin = 0;
  181. if (margins)
  182. vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  183. drm_mode->vdisplay = vdisplay + 2 * vmargin;
  184. /* Interlaced */
  185. if (interlaced)
  186. interlace = 1;
  187. else
  188. interlace = 0;
  189. /* Determine VSync Width from aspect ratio */
  190. if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
  191. vsync = 4;
  192. else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
  193. vsync = 5;
  194. else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
  195. vsync = 6;
  196. else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
  197. vsync = 7;
  198. else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
  199. vsync = 7;
  200. else /* custom */
  201. vsync = 10;
  202. if (!reduced) {
  203. /* simplify the GTF calculation */
  204. /* 4) Minimum time of vertical sync + back porch interval (µs)
  205. * default 550.0
  206. */
  207. int tmp1, tmp2;
  208. #define CVT_MIN_VSYNC_BP 550
  209. /* 3) Nominal HSync width (% of line period) - default 8 */
  210. #define CVT_HSYNC_PERCENTAGE 8
  211. unsigned int hblank_percentage;
  212. int vsyncandback_porch, vback_porch, hblank;
  213. /* estimated the horizontal period */
  214. tmp1 = HV_FACTOR * 1000000 -
  215. CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
  216. tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
  217. interlace;
  218. hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
  219. tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
  220. /* 9. Find number of lines in sync + backporch */
  221. if (tmp1 < (vsync + CVT_MIN_V_PORCH))
  222. vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
  223. else
  224. vsyncandback_porch = tmp1;
  225. /* 10. Find number of lines in back porch */
  226. vback_porch = vsyncandback_porch - vsync;
  227. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
  228. vsyncandback_porch + CVT_MIN_V_PORCH;
  229. /* 5) Definition of Horizontal blanking time limitation */
  230. /* Gradient (%/kHz) - default 600 */
  231. #define CVT_M_FACTOR 600
  232. /* Offset (%) - default 40 */
  233. #define CVT_C_FACTOR 40
  234. /* Blanking time scaling factor - default 128 */
  235. #define CVT_K_FACTOR 128
  236. /* Scaling factor weighting - default 20 */
  237. #define CVT_J_FACTOR 20
  238. #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
  239. #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
  240. CVT_J_FACTOR)
  241. /* 12. Find ideal blanking duty cycle from formula */
  242. hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
  243. hperiod / 1000;
  244. /* 13. Blanking time */
  245. if (hblank_percentage < 20 * HV_FACTOR)
  246. hblank_percentage = 20 * HV_FACTOR;
  247. hblank = drm_mode->hdisplay * hblank_percentage /
  248. (100 * HV_FACTOR - hblank_percentage);
  249. hblank -= hblank % (2 * CVT_H_GRANULARITY);
  250. /* 14. find the total pixels per line */
  251. drm_mode->htotal = drm_mode->hdisplay + hblank;
  252. drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
  253. drm_mode->hsync_start = drm_mode->hsync_end -
  254. (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
  255. drm_mode->hsync_start += CVT_H_GRANULARITY -
  256. drm_mode->hsync_start % CVT_H_GRANULARITY;
  257. /* fill the Vsync values */
  258. drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
  259. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  260. } else {
  261. /* Reduced blanking */
  262. /* Minimum vertical blanking interval time (µs)- default 460 */
  263. #define CVT_RB_MIN_VBLANK 460
  264. /* Fixed number of clocks for horizontal sync */
  265. #define CVT_RB_H_SYNC 32
  266. /* Fixed number of clocks for horizontal blanking */
  267. #define CVT_RB_H_BLANK 160
  268. /* Fixed number of lines for vertical front porch - default 3*/
  269. #define CVT_RB_VFPORCH 3
  270. int vbilines;
  271. int tmp1, tmp2;
  272. /* 8. Estimate Horizontal period. */
  273. tmp1 = HV_FACTOR * 1000000 -
  274. CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
  275. tmp2 = vdisplay_rnd + 2 * vmargin;
  276. hperiod = tmp1 / (tmp2 * vfieldrate);
  277. /* 9. Find number of lines in vertical blanking */
  278. vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
  279. /* 10. Check if vertical blanking is sufficient */
  280. if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
  281. vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
  282. /* 11. Find total number of lines in vertical field */
  283. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
  284. /* 12. Find total number of pixels in a line */
  285. drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
  286. /* Fill in HSync values */
  287. drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
  288. drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
  289. /* Fill in VSync values */
  290. drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
  291. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  292. }
  293. /* 15/13. Find pixel clock frequency (kHz for xf86) */
  294. tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
  295. tmp *= HV_FACTOR * 1000;
  296. do_div(tmp, hperiod);
  297. tmp -= drm_mode->clock % CVT_CLOCK_STEP;
  298. drm_mode->clock = tmp;
  299. /* 18/16. Find actual vertical frame frequency */
  300. /* ignore - just set the mode flag for interlaced */
  301. if (interlaced) {
  302. drm_mode->vtotal *= 2;
  303. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  304. }
  305. /* Fill the mode line name */
  306. drm_mode_set_name(drm_mode);
  307. if (reduced)
  308. drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
  309. DRM_MODE_FLAG_NVSYNC);
  310. else
  311. drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
  312. DRM_MODE_FLAG_NHSYNC);
  313. return drm_mode;
  314. }
  315. EXPORT_SYMBOL(drm_cvt_mode);
  316. /**
  317. * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
  318. * @dev: drm device
  319. * @hdisplay: hdisplay size
  320. * @vdisplay: vdisplay size
  321. * @vrefresh: vrefresh rate.
  322. * @interlaced: whether to compute an interlaced mode
  323. * @margins: desired margin (borders) size
  324. * @GTF_M: extended GTF formula parameters
  325. * @GTF_2C: extended GTF formula parameters
  326. * @GTF_K: extended GTF formula parameters
  327. * @GTF_2J: extended GTF formula parameters
  328. *
  329. * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
  330. * in here multiplied by two. For a C of 40, pass in 80.
  331. *
  332. * Returns:
  333. * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
  334. * The display mode object is allocated with drm_mode_create(). Returns NULL
  335. * when no mode could be allocated.
  336. */
  337. struct drm_display_mode *
  338. drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
  339. int vrefresh, bool interlaced, int margins,
  340. int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
  341. { /* 1) top/bottom margin size (% of height) - default: 1.8, */
  342. #define GTF_MARGIN_PERCENTAGE 18
  343. /* 2) character cell horizontal granularity (pixels) - default 8 */
  344. #define GTF_CELL_GRAN 8
  345. /* 3) Minimum vertical porch (lines) - default 3 */
  346. #define GTF_MIN_V_PORCH 1
  347. /* width of vsync in lines */
  348. #define V_SYNC_RQD 3
  349. /* width of hsync as % of total line */
  350. #define H_SYNC_PERCENT 8
  351. /* min time of vsync + back porch (microsec) */
  352. #define MIN_VSYNC_PLUS_BP 550
  353. /* C' and M' are part of the Blanking Duty Cycle computation */
  354. #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
  355. #define GTF_M_PRIME (GTF_K * GTF_M / 256)
  356. struct drm_display_mode *drm_mode;
  357. unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
  358. int top_margin, bottom_margin;
  359. int interlace;
  360. unsigned int hfreq_est;
  361. int vsync_plus_bp, vback_porch;
  362. unsigned int vtotal_lines, vfieldrate_est, hperiod;
  363. unsigned int vfield_rate, vframe_rate;
  364. int left_margin, right_margin;
  365. unsigned int total_active_pixels, ideal_duty_cycle;
  366. unsigned int hblank, total_pixels, pixel_freq;
  367. int hsync, hfront_porch, vodd_front_porch_lines;
  368. unsigned int tmp1, tmp2;
  369. drm_mode = drm_mode_create(dev);
  370. if (!drm_mode)
  371. return NULL;
  372. /* 1. In order to give correct results, the number of horizontal
  373. * pixels requested is first processed to ensure that it is divisible
  374. * by the character size, by rounding it to the nearest character
  375. * cell boundary:
  376. */
  377. hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  378. hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
  379. /* 2. If interlace is requested, the number of vertical lines assumed
  380. * by the calculation must be halved, as the computation calculates
  381. * the number of vertical lines per field.
  382. */
  383. if (interlaced)
  384. vdisplay_rnd = vdisplay / 2;
  385. else
  386. vdisplay_rnd = vdisplay;
  387. /* 3. Find the frame rate required: */
  388. if (interlaced)
  389. vfieldrate_rqd = vrefresh * 2;
  390. else
  391. vfieldrate_rqd = vrefresh;
  392. /* 4. Find number of lines in Top margin: */
  393. top_margin = 0;
  394. if (margins)
  395. top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  396. 1000;
  397. /* 5. Find number of lines in bottom margin: */
  398. bottom_margin = top_margin;
  399. /* 6. If interlace is required, then set variable interlace: */
  400. if (interlaced)
  401. interlace = 1;
  402. else
  403. interlace = 0;
  404. /* 7. Estimate the Horizontal frequency */
  405. {
  406. tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
  407. tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
  408. 2 + interlace;
  409. hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
  410. }
  411. /* 8. Find the number of lines in V sync + back porch */
  412. /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
  413. vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
  414. vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
  415. /* 9. Find the number of lines in V back porch alone: */
  416. vback_porch = vsync_plus_bp - V_SYNC_RQD;
  417. /* 10. Find the total number of lines in Vertical field period: */
  418. vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
  419. vsync_plus_bp + GTF_MIN_V_PORCH;
  420. /* 11. Estimate the Vertical field frequency: */
  421. vfieldrate_est = hfreq_est / vtotal_lines;
  422. /* 12. Find the actual horizontal period: */
  423. hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
  424. /* 13. Find the actual Vertical field frequency: */
  425. vfield_rate = hfreq_est / vtotal_lines;
  426. /* 14. Find the Vertical frame frequency: */
  427. if (interlaced)
  428. vframe_rate = vfield_rate / 2;
  429. else
  430. vframe_rate = vfield_rate;
  431. /* 15. Find number of pixels in left margin: */
  432. if (margins)
  433. left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  434. 1000;
  435. else
  436. left_margin = 0;
  437. /* 16.Find number of pixels in right margin: */
  438. right_margin = left_margin;
  439. /* 17.Find total number of active pixels in image and left and right */
  440. total_active_pixels = hdisplay_rnd + left_margin + right_margin;
  441. /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
  442. ideal_duty_cycle = GTF_C_PRIME * 1000 -
  443. (GTF_M_PRIME * 1000000 / hfreq_est);
  444. /* 19.Find the number of pixels in the blanking time to the nearest
  445. * double character cell: */
  446. hblank = total_active_pixels * ideal_duty_cycle /
  447. (100000 - ideal_duty_cycle);
  448. hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
  449. hblank = hblank * 2 * GTF_CELL_GRAN;
  450. /* 20.Find total number of pixels: */
  451. total_pixels = total_active_pixels + hblank;
  452. /* 21.Find pixel clock frequency: */
  453. pixel_freq = total_pixels * hfreq_est / 1000;
  454. /* Stage 1 computations are now complete; I should really pass
  455. * the results to another function and do the Stage 2 computations,
  456. * but I only need a few more values so I'll just append the
  457. * computations here for now */
  458. /* 17. Find the number of pixels in the horizontal sync period: */
  459. hsync = H_SYNC_PERCENT * total_pixels / 100;
  460. hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  461. hsync = hsync * GTF_CELL_GRAN;
  462. /* 18. Find the number of pixels in horizontal front porch period */
  463. hfront_porch = hblank / 2 - hsync;
  464. /* 36. Find the number of lines in the odd front porch period: */
  465. vodd_front_porch_lines = GTF_MIN_V_PORCH ;
  466. /* finally, pack the results in the mode struct */
  467. drm_mode->hdisplay = hdisplay_rnd;
  468. drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
  469. drm_mode->hsync_end = drm_mode->hsync_start + hsync;
  470. drm_mode->htotal = total_pixels;
  471. drm_mode->vdisplay = vdisplay_rnd;
  472. drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
  473. drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
  474. drm_mode->vtotal = vtotal_lines;
  475. drm_mode->clock = pixel_freq;
  476. if (interlaced) {
  477. drm_mode->vtotal *= 2;
  478. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  479. }
  480. drm_mode_set_name(drm_mode);
  481. if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
  482. drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
  483. else
  484. drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
  485. return drm_mode;
  486. }
  487. EXPORT_SYMBOL(drm_gtf_mode_complex);
  488. /**
  489. * drm_gtf_mode - create the modeline based on the GTF algorithm
  490. * @dev: drm device
  491. * @hdisplay: hdisplay size
  492. * @vdisplay: vdisplay size
  493. * @vrefresh: vrefresh rate.
  494. * @interlaced: whether to compute an interlaced mode
  495. * @margins: desired margin (borders) size
  496. *
  497. * return the modeline based on GTF algorithm
  498. *
  499. * This function is to create the modeline based on the GTF algorithm.
  500. * Generalized Timing Formula is derived from:
  501. *
  502. * GTF Spreadsheet by Andy Morrish (1/5/97)
  503. * available at http://www.vesa.org
  504. *
  505. * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
  506. * What I have done is to translate it by using integer calculation.
  507. * I also refer to the function of fb_get_mode in the file of
  508. * drivers/video/fbmon.c
  509. *
  510. * Standard GTF parameters::
  511. *
  512. * M = 600
  513. * C = 40
  514. * K = 128
  515. * J = 20
  516. *
  517. * Returns:
  518. * The modeline based on the GTF algorithm stored in a drm_display_mode object.
  519. * The display mode object is allocated with drm_mode_create(). Returns NULL
  520. * when no mode could be allocated.
  521. */
  522. struct drm_display_mode *
  523. drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
  524. bool interlaced, int margins)
  525. {
  526. return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
  527. interlaced, margins,
  528. 600, 40 * 2, 128, 20 * 2);
  529. }
  530. EXPORT_SYMBOL(drm_gtf_mode);
  531. #ifdef CONFIG_VIDEOMODE_HELPERS
  532. /**
  533. * drm_display_mode_from_videomode - fill in @dmode using @vm,
  534. * @vm: videomode structure to use as source
  535. * @dmode: drm_display_mode structure to use as destination
  536. *
  537. * Fills out @dmode using the display mode specified in @vm.
  538. */
  539. void drm_display_mode_from_videomode(const struct videomode *vm,
  540. struct drm_display_mode *dmode)
  541. {
  542. dmode->hdisplay = vm->hactive;
  543. dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
  544. dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
  545. dmode->htotal = dmode->hsync_end + vm->hback_porch;
  546. dmode->vdisplay = vm->vactive;
  547. dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
  548. dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
  549. dmode->vtotal = dmode->vsync_end + vm->vback_porch;
  550. dmode->clock = vm->pixelclock / 1000;
  551. dmode->flags = 0;
  552. if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
  553. dmode->flags |= DRM_MODE_FLAG_PHSYNC;
  554. else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
  555. dmode->flags |= DRM_MODE_FLAG_NHSYNC;
  556. if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
  557. dmode->flags |= DRM_MODE_FLAG_PVSYNC;
  558. else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
  559. dmode->flags |= DRM_MODE_FLAG_NVSYNC;
  560. if (vm->flags & DISPLAY_FLAGS_INTERLACED)
  561. dmode->flags |= DRM_MODE_FLAG_INTERLACE;
  562. if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
  563. dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
  564. if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
  565. dmode->flags |= DRM_MODE_FLAG_DBLCLK;
  566. drm_mode_set_name(dmode);
  567. }
  568. EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
  569. /**
  570. * drm_display_mode_to_videomode - fill in @vm using @dmode,
  571. * @dmode: drm_display_mode structure to use as source
  572. * @vm: videomode structure to use as destination
  573. *
  574. * Fills out @vm using the display mode specified in @dmode.
  575. */
  576. void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
  577. struct videomode *vm)
  578. {
  579. vm->hactive = dmode->hdisplay;
  580. vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
  581. vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
  582. vm->hback_porch = dmode->htotal - dmode->hsync_end;
  583. vm->vactive = dmode->vdisplay;
  584. vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
  585. vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
  586. vm->vback_porch = dmode->vtotal - dmode->vsync_end;
  587. vm->pixelclock = dmode->clock * 1000;
  588. vm->flags = 0;
  589. if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
  590. vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
  591. else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
  592. vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
  593. if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
  594. vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
  595. else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
  596. vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
  597. if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
  598. vm->flags |= DISPLAY_FLAGS_INTERLACED;
  599. if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
  600. vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  601. if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
  602. vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
  603. }
  604. EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
  605. /**
  606. * drm_bus_flags_from_videomode - extract information about pixelclk and
  607. * DE polarity from videomode and store it in a separate variable
  608. * @vm: videomode structure to use
  609. * @bus_flags: information about pixelclk, sync and DE polarity will be stored
  610. * here
  611. *
  612. * Sets DRM_BUS_FLAG_DE_(LOW|HIGH), DRM_BUS_FLAG_PIXDATA_(POS|NEG)EDGE and
  613. * DISPLAY_FLAGS_SYNC_(POS|NEG)EDGE in @bus_flags according to DISPLAY_FLAGS
  614. * found in @vm
  615. */
  616. void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
  617. {
  618. *bus_flags = 0;
  619. if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
  620. *bus_flags |= DRM_BUS_FLAG_PIXDATA_POSEDGE;
  621. if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
  622. *bus_flags |= DRM_BUS_FLAG_PIXDATA_NEGEDGE;
  623. if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE)
  624. *bus_flags |= DRM_BUS_FLAG_SYNC_POSEDGE;
  625. if (vm->flags & DISPLAY_FLAGS_SYNC_NEGEDGE)
  626. *bus_flags |= DRM_BUS_FLAG_SYNC_NEGEDGE;
  627. if (vm->flags & DISPLAY_FLAGS_DE_LOW)
  628. *bus_flags |= DRM_BUS_FLAG_DE_LOW;
  629. if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
  630. *bus_flags |= DRM_BUS_FLAG_DE_HIGH;
  631. }
  632. EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
  633. #ifdef CONFIG_OF
  634. /**
  635. * of_get_drm_display_mode - get a drm_display_mode from devicetree
  636. * @np: device_node with the timing specification
  637. * @dmode: will be set to the return value
  638. * @bus_flags: information about pixelclk, sync and DE polarity
  639. * @index: index into the list of display timings in devicetree
  640. *
  641. * This function is expensive and should only be used, if only one mode is to be
  642. * read from DT. To get multiple modes start with of_get_display_timings and
  643. * work with that instead.
  644. *
  645. * Returns:
  646. * 0 on success, a negative errno code when no of videomode node was found.
  647. */
  648. int of_get_drm_display_mode(struct device_node *np,
  649. struct drm_display_mode *dmode, u32 *bus_flags,
  650. int index)
  651. {
  652. struct videomode vm;
  653. int ret;
  654. ret = of_get_videomode(np, &vm, index);
  655. if (ret)
  656. return ret;
  657. drm_display_mode_from_videomode(&vm, dmode);
  658. if (bus_flags)
  659. drm_bus_flags_from_videomode(&vm, bus_flags);
  660. pr_debug("%pOF: got %dx%d display mode from %s\n",
  661. np, vm.hactive, vm.vactive, np->name);
  662. drm_mode_debug_printmodeline(dmode);
  663. return 0;
  664. }
  665. EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
  666. #endif /* CONFIG_OF */
  667. #endif /* CONFIG_VIDEOMODE_HELPERS */
  668. /**
  669. * drm_mode_set_name - set the name on a mode
  670. * @mode: name will be set in this mode
  671. *
  672. * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
  673. * with an optional 'i' suffix for interlaced modes.
  674. */
  675. void drm_mode_set_name(struct drm_display_mode *mode)
  676. {
  677. bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  678. snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
  679. mode->hdisplay, mode->vdisplay,
  680. interlaced ? "i" : "");
  681. }
  682. EXPORT_SYMBOL(drm_mode_set_name);
  683. /**
  684. * drm_mode_hsync - get the hsync of a mode
  685. * @mode: mode
  686. *
  687. * Returns:
  688. * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
  689. * value first if it is not yet set.
  690. */
  691. int drm_mode_hsync(const struct drm_display_mode *mode)
  692. {
  693. unsigned int calc_val;
  694. if (mode->hsync)
  695. return mode->hsync;
  696. if (mode->htotal <= 0)
  697. return 0;
  698. calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
  699. calc_val += 500; /* round to 1000Hz */
  700. calc_val /= 1000; /* truncate to kHz */
  701. return calc_val;
  702. }
  703. EXPORT_SYMBOL(drm_mode_hsync);
  704. /**
  705. * drm_mode_vrefresh - get the vrefresh of a mode
  706. * @mode: mode
  707. *
  708. * Returns:
  709. * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
  710. * value first if it is not yet set.
  711. */
  712. int drm_mode_vrefresh(const struct drm_display_mode *mode)
  713. {
  714. int refresh = 0;
  715. if (mode->vrefresh > 0)
  716. refresh = mode->vrefresh;
  717. else if (mode->htotal > 0 && mode->vtotal > 0) {
  718. unsigned int num, den;
  719. num = mode->clock * 1000;
  720. den = mode->htotal * mode->vtotal;
  721. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  722. num *= 2;
  723. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  724. den *= 2;
  725. if (mode->vscan > 1)
  726. den *= mode->vscan;
  727. refresh = DIV_ROUND_CLOSEST(num, den);
  728. }
  729. return refresh;
  730. }
  731. EXPORT_SYMBOL(drm_mode_vrefresh);
  732. /**
  733. * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  734. * @mode: mode to query
  735. * @hdisplay: hdisplay value to fill in
  736. * @vdisplay: vdisplay value to fill in
  737. *
  738. * The vdisplay value will be doubled if the specified mode is a stereo mode of
  739. * the appropriate layout.
  740. */
  741. void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
  742. int *hdisplay, int *vdisplay)
  743. {
  744. struct drm_display_mode adjusted = *mode;
  745. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  746. *hdisplay = adjusted.crtc_hdisplay;
  747. *vdisplay = adjusted.crtc_vdisplay;
  748. }
  749. EXPORT_SYMBOL(drm_mode_get_hv_timing);
  750. /**
  751. * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
  752. * @p: mode
  753. * @adjust_flags: a combination of adjustment flags
  754. *
  755. * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
  756. *
  757. * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
  758. * interlaced modes.
  759. * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
  760. * buffers containing two eyes (only adjust the timings when needed, eg. for
  761. * "frame packing" or "side by side full").
  762. * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
  763. * be performed for doublescan and vscan > 1 modes respectively.
  764. */
  765. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  766. {
  767. if (!p)
  768. return;
  769. p->crtc_clock = p->clock;
  770. p->crtc_hdisplay = p->hdisplay;
  771. p->crtc_hsync_start = p->hsync_start;
  772. p->crtc_hsync_end = p->hsync_end;
  773. p->crtc_htotal = p->htotal;
  774. p->crtc_hskew = p->hskew;
  775. p->crtc_vdisplay = p->vdisplay;
  776. p->crtc_vsync_start = p->vsync_start;
  777. p->crtc_vsync_end = p->vsync_end;
  778. p->crtc_vtotal = p->vtotal;
  779. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  780. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  781. p->crtc_vdisplay /= 2;
  782. p->crtc_vsync_start /= 2;
  783. p->crtc_vsync_end /= 2;
  784. p->crtc_vtotal /= 2;
  785. }
  786. }
  787. if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
  788. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  789. p->crtc_vdisplay *= 2;
  790. p->crtc_vsync_start *= 2;
  791. p->crtc_vsync_end *= 2;
  792. p->crtc_vtotal *= 2;
  793. }
  794. }
  795. if (!(adjust_flags & CRTC_NO_VSCAN)) {
  796. if (p->vscan > 1) {
  797. p->crtc_vdisplay *= p->vscan;
  798. p->crtc_vsync_start *= p->vscan;
  799. p->crtc_vsync_end *= p->vscan;
  800. p->crtc_vtotal *= p->vscan;
  801. }
  802. }
  803. if (adjust_flags & CRTC_STEREO_DOUBLE) {
  804. unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
  805. switch (layout) {
  806. case DRM_MODE_FLAG_3D_FRAME_PACKING:
  807. p->crtc_clock *= 2;
  808. p->crtc_vdisplay += p->crtc_vtotal;
  809. p->crtc_vsync_start += p->crtc_vtotal;
  810. p->crtc_vsync_end += p->crtc_vtotal;
  811. p->crtc_vtotal += p->crtc_vtotal;
  812. break;
  813. }
  814. }
  815. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  816. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  817. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  818. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  819. }
  820. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  821. /**
  822. * drm_mode_copy - copy the mode
  823. * @dst: mode to overwrite
  824. * @src: mode to copy
  825. *
  826. * Copy an existing mode into another mode, preserving the object id and
  827. * list head of the destination mode.
  828. */
  829. void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
  830. {
  831. int id = dst->base.id;
  832. struct list_head head = dst->head;
  833. *dst = *src;
  834. dst->base.id = id;
  835. dst->head = head;
  836. }
  837. EXPORT_SYMBOL(drm_mode_copy);
  838. /**
  839. * drm_mode_duplicate - allocate and duplicate an existing mode
  840. * @dev: drm_device to allocate the duplicated mode for
  841. * @mode: mode to duplicate
  842. *
  843. * Just allocate a new mode, copy the existing mode into it, and return
  844. * a pointer to it. Used to create new instances of established modes.
  845. *
  846. * Returns:
  847. * Pointer to duplicated mode on success, NULL on error.
  848. */
  849. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  850. const struct drm_display_mode *mode)
  851. {
  852. struct drm_display_mode *nmode;
  853. nmode = drm_mode_create(dev);
  854. if (!nmode)
  855. return NULL;
  856. drm_mode_copy(nmode, mode);
  857. return nmode;
  858. }
  859. EXPORT_SYMBOL(drm_mode_duplicate);
  860. static bool drm_mode_match_timings(const struct drm_display_mode *mode1,
  861. const struct drm_display_mode *mode2)
  862. {
  863. return mode1->hdisplay == mode2->hdisplay &&
  864. mode1->hsync_start == mode2->hsync_start &&
  865. mode1->hsync_end == mode2->hsync_end &&
  866. mode1->htotal == mode2->htotal &&
  867. mode1->hskew == mode2->hskew &&
  868. mode1->vdisplay == mode2->vdisplay &&
  869. mode1->vsync_start == mode2->vsync_start &&
  870. mode1->vsync_end == mode2->vsync_end &&
  871. mode1->vtotal == mode2->vtotal &&
  872. mode1->vscan == mode2->vscan;
  873. }
  874. static bool drm_mode_match_clock(const struct drm_display_mode *mode1,
  875. const struct drm_display_mode *mode2)
  876. {
  877. /*
  878. * do clock check convert to PICOS
  879. * so fb modes get matched the same
  880. */
  881. if (mode1->clock && mode2->clock)
  882. return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock);
  883. else
  884. return mode1->clock == mode2->clock;
  885. }
  886. static bool drm_mode_match_flags(const struct drm_display_mode *mode1,
  887. const struct drm_display_mode *mode2)
  888. {
  889. return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
  890. (mode2->flags & ~DRM_MODE_FLAG_3D_MASK);
  891. }
  892. static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1,
  893. const struct drm_display_mode *mode2)
  894. {
  895. return (mode1->flags & DRM_MODE_FLAG_3D_MASK) ==
  896. (mode2->flags & DRM_MODE_FLAG_3D_MASK);
  897. }
  898. static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1,
  899. const struct drm_display_mode *mode2)
  900. {
  901. return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio;
  902. }
  903. /**
  904. * drm_mode_match - test modes for (partial) equality
  905. * @mode1: first mode
  906. * @mode2: second mode
  907. * @match_flags: which parts need to match (DRM_MODE_MATCH_*)
  908. *
  909. * Check to see if @mode1 and @mode2 are equivalent.
  910. *
  911. * Returns:
  912. * True if the modes are (partially) equal, false otherwise.
  913. */
  914. bool drm_mode_match(const struct drm_display_mode *mode1,
  915. const struct drm_display_mode *mode2,
  916. unsigned int match_flags)
  917. {
  918. if (!mode1 && !mode2)
  919. return true;
  920. if (!mode1 || !mode2)
  921. return false;
  922. if (match_flags & DRM_MODE_MATCH_TIMINGS &&
  923. !drm_mode_match_timings(mode1, mode2))
  924. return false;
  925. if (match_flags & DRM_MODE_MATCH_CLOCK &&
  926. !drm_mode_match_clock(mode1, mode2))
  927. return false;
  928. if (match_flags & DRM_MODE_MATCH_FLAGS &&
  929. !drm_mode_match_flags(mode1, mode2))
  930. return false;
  931. if (match_flags & DRM_MODE_MATCH_3D_FLAGS &&
  932. !drm_mode_match_3d_flags(mode1, mode2))
  933. return false;
  934. if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO &&
  935. !drm_mode_match_aspect_ratio(mode1, mode2))
  936. return false;
  937. return true;
  938. }
  939. EXPORT_SYMBOL(drm_mode_match);
  940. /**
  941. * drm_mode_equal - test modes for equality
  942. * @mode1: first mode
  943. * @mode2: second mode
  944. *
  945. * Check to see if @mode1 and @mode2 are equivalent.
  946. *
  947. * Returns:
  948. * True if the modes are equal, false otherwise.
  949. */
  950. bool drm_mode_equal(const struct drm_display_mode *mode1,
  951. const struct drm_display_mode *mode2)
  952. {
  953. return drm_mode_match(mode1, mode2,
  954. DRM_MODE_MATCH_TIMINGS |
  955. DRM_MODE_MATCH_CLOCK |
  956. DRM_MODE_MATCH_FLAGS |
  957. DRM_MODE_MATCH_3D_FLAGS|
  958. DRM_MODE_MATCH_ASPECT_RATIO);
  959. }
  960. EXPORT_SYMBOL(drm_mode_equal);
  961. /**
  962. * drm_mode_equal_no_clocks - test modes for equality
  963. * @mode1: first mode
  964. * @mode2: second mode
  965. *
  966. * Check to see if @mode1 and @mode2 are equivalent, but
  967. * don't check the pixel clocks.
  968. *
  969. * Returns:
  970. * True if the modes are equal, false otherwise.
  971. */
  972. bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1,
  973. const struct drm_display_mode *mode2)
  974. {
  975. return drm_mode_match(mode1, mode2,
  976. DRM_MODE_MATCH_TIMINGS |
  977. DRM_MODE_MATCH_FLAGS |
  978. DRM_MODE_MATCH_3D_FLAGS);
  979. }
  980. EXPORT_SYMBOL(drm_mode_equal_no_clocks);
  981. /**
  982. * drm_mode_equal_no_clocks_no_stereo - test modes for equality
  983. * @mode1: first mode
  984. * @mode2: second mode
  985. *
  986. * Check to see if @mode1 and @mode2 are equivalent, but
  987. * don't check the pixel clocks nor the stereo layout.
  988. *
  989. * Returns:
  990. * True if the modes are equal, false otherwise.
  991. */
  992. bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
  993. const struct drm_display_mode *mode2)
  994. {
  995. return drm_mode_match(mode1, mode2,
  996. DRM_MODE_MATCH_TIMINGS |
  997. DRM_MODE_MATCH_FLAGS);
  998. }
  999. EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
  1000. static enum drm_mode_status
  1001. drm_mode_validate_basic(const struct drm_display_mode *mode)
  1002. {
  1003. if (mode->type & ~DRM_MODE_TYPE_ALL)
  1004. return MODE_BAD;
  1005. if (mode->flags & ~DRM_MODE_FLAG_ALL)
  1006. return MODE_BAD;
  1007. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1008. return MODE_BAD;
  1009. if (mode->clock == 0)
  1010. return MODE_CLOCK_LOW;
  1011. if (mode->hdisplay == 0 ||
  1012. mode->hsync_start < mode->hdisplay ||
  1013. mode->hsync_end < mode->hsync_start ||
  1014. mode->htotal < mode->hsync_end)
  1015. return MODE_H_ILLEGAL;
  1016. if (mode->vdisplay == 0 ||
  1017. mode->vsync_start < mode->vdisplay ||
  1018. mode->vsync_end < mode->vsync_start ||
  1019. mode->vtotal < mode->vsync_end)
  1020. return MODE_V_ILLEGAL;
  1021. return MODE_OK;
  1022. }
  1023. /**
  1024. * drm_mode_validate_driver - make sure the mode is somewhat sane
  1025. * @dev: drm device
  1026. * @mode: mode to check
  1027. *
  1028. * First do basic validation on the mode, and then allow the driver
  1029. * to check for device/driver specific limitations via the optional
  1030. * &drm_mode_config_helper_funcs.mode_valid hook.
  1031. *
  1032. * Returns:
  1033. * The mode status
  1034. */
  1035. enum drm_mode_status
  1036. drm_mode_validate_driver(struct drm_device *dev,
  1037. const struct drm_display_mode *mode)
  1038. {
  1039. enum drm_mode_status status;
  1040. status = drm_mode_validate_basic(mode);
  1041. if (status != MODE_OK)
  1042. return status;
  1043. if (dev->mode_config.funcs->mode_valid)
  1044. return dev->mode_config.funcs->mode_valid(dev, mode);
  1045. else
  1046. return MODE_OK;
  1047. }
  1048. EXPORT_SYMBOL(drm_mode_validate_driver);
  1049. /**
  1050. * drm_mode_validate_size - make sure modes adhere to size constraints
  1051. * @mode: mode to check
  1052. * @maxX: maximum width
  1053. * @maxY: maximum height
  1054. *
  1055. * This function is a helper which can be used to validate modes against size
  1056. * limitations of the DRM device/connector. If a mode is too big its status
  1057. * member is updated with the appropriate validation failure code. The list
  1058. * itself is not changed.
  1059. *
  1060. * Returns:
  1061. * The mode status
  1062. */
  1063. enum drm_mode_status
  1064. drm_mode_validate_size(const struct drm_display_mode *mode,
  1065. int maxX, int maxY)
  1066. {
  1067. if (maxX > 0 && mode->hdisplay > maxX)
  1068. return MODE_VIRTUAL_X;
  1069. if (maxY > 0 && mode->vdisplay > maxY)
  1070. return MODE_VIRTUAL_Y;
  1071. return MODE_OK;
  1072. }
  1073. EXPORT_SYMBOL(drm_mode_validate_size);
  1074. /**
  1075. * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
  1076. * @mode: mode to check
  1077. * @connector: drm connector under action
  1078. *
  1079. * This function is a helper which can be used to filter out any YCBCR420
  1080. * only mode, when the source doesn't support it.
  1081. *
  1082. * Returns:
  1083. * The mode status
  1084. */
  1085. enum drm_mode_status
  1086. drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
  1087. struct drm_connector *connector)
  1088. {
  1089. u8 vic = drm_match_cea_mode(mode);
  1090. enum drm_mode_status status = MODE_OK;
  1091. struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
  1092. if (test_bit(vic, hdmi->y420_vdb_modes)) {
  1093. if (!connector->ycbcr_420_allowed)
  1094. status = MODE_NO_420;
  1095. }
  1096. return status;
  1097. }
  1098. EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
  1099. #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
  1100. static const char * const drm_mode_status_names[] = {
  1101. MODE_STATUS(OK),
  1102. MODE_STATUS(HSYNC),
  1103. MODE_STATUS(VSYNC),
  1104. MODE_STATUS(H_ILLEGAL),
  1105. MODE_STATUS(V_ILLEGAL),
  1106. MODE_STATUS(BAD_WIDTH),
  1107. MODE_STATUS(NOMODE),
  1108. MODE_STATUS(NO_INTERLACE),
  1109. MODE_STATUS(NO_DBLESCAN),
  1110. MODE_STATUS(NO_VSCAN),
  1111. MODE_STATUS(MEM),
  1112. MODE_STATUS(VIRTUAL_X),
  1113. MODE_STATUS(VIRTUAL_Y),
  1114. MODE_STATUS(MEM_VIRT),
  1115. MODE_STATUS(NOCLOCK),
  1116. MODE_STATUS(CLOCK_HIGH),
  1117. MODE_STATUS(CLOCK_LOW),
  1118. MODE_STATUS(CLOCK_RANGE),
  1119. MODE_STATUS(BAD_HVALUE),
  1120. MODE_STATUS(BAD_VVALUE),
  1121. MODE_STATUS(BAD_VSCAN),
  1122. MODE_STATUS(HSYNC_NARROW),
  1123. MODE_STATUS(HSYNC_WIDE),
  1124. MODE_STATUS(HBLANK_NARROW),
  1125. MODE_STATUS(HBLANK_WIDE),
  1126. MODE_STATUS(VSYNC_NARROW),
  1127. MODE_STATUS(VSYNC_WIDE),
  1128. MODE_STATUS(VBLANK_NARROW),
  1129. MODE_STATUS(VBLANK_WIDE),
  1130. MODE_STATUS(PANEL),
  1131. MODE_STATUS(INTERLACE_WIDTH),
  1132. MODE_STATUS(ONE_WIDTH),
  1133. MODE_STATUS(ONE_HEIGHT),
  1134. MODE_STATUS(ONE_SIZE),
  1135. MODE_STATUS(NO_REDUCED),
  1136. MODE_STATUS(NO_STEREO),
  1137. MODE_STATUS(NO_420),
  1138. MODE_STATUS(STALE),
  1139. MODE_STATUS(BAD),
  1140. MODE_STATUS(ERROR),
  1141. };
  1142. #undef MODE_STATUS
  1143. const char *drm_get_mode_status_name(enum drm_mode_status status)
  1144. {
  1145. int index = status + 3;
  1146. if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
  1147. return "";
  1148. return drm_mode_status_names[index];
  1149. }
  1150. /**
  1151. * drm_mode_prune_invalid - remove invalid modes from mode list
  1152. * @dev: DRM device
  1153. * @mode_list: list of modes to check
  1154. * @verbose: be verbose about it
  1155. *
  1156. * This helper function can be used to prune a display mode list after
  1157. * validation has been completed. All modes who's status is not MODE_OK will be
  1158. * removed from the list, and if @verbose the status code and mode name is also
  1159. * printed to dmesg.
  1160. */
  1161. void drm_mode_prune_invalid(struct drm_device *dev,
  1162. struct list_head *mode_list, bool verbose)
  1163. {
  1164. struct drm_display_mode *mode, *t;
  1165. list_for_each_entry_safe(mode, t, mode_list, head) {
  1166. if (mode->status != MODE_OK) {
  1167. list_del(&mode->head);
  1168. if (verbose) {
  1169. drm_mode_debug_printmodeline(mode);
  1170. DRM_DEBUG_KMS("Not using %s mode: %s\n",
  1171. mode->name,
  1172. drm_get_mode_status_name(mode->status));
  1173. }
  1174. drm_mode_destroy(dev, mode);
  1175. }
  1176. }
  1177. }
  1178. EXPORT_SYMBOL(drm_mode_prune_invalid);
  1179. /**
  1180. * drm_mode_compare - compare modes for favorability
  1181. * @priv: unused
  1182. * @lh_a: list_head for first mode
  1183. * @lh_b: list_head for second mode
  1184. *
  1185. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  1186. * which is better.
  1187. *
  1188. * Returns:
  1189. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  1190. * positive if @lh_b is better than @lh_a.
  1191. */
  1192. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  1193. {
  1194. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  1195. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  1196. int diff;
  1197. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  1198. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  1199. if (diff)
  1200. return diff;
  1201. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  1202. if (diff)
  1203. return diff;
  1204. diff = b->vrefresh - a->vrefresh;
  1205. if (diff)
  1206. return diff;
  1207. diff = b->clock - a->clock;
  1208. return diff;
  1209. }
  1210. /**
  1211. * drm_mode_sort - sort mode list
  1212. * @mode_list: list of drm_display_mode structures to sort
  1213. *
  1214. * Sort @mode_list by favorability, moving good modes to the head of the list.
  1215. */
  1216. void drm_mode_sort(struct list_head *mode_list)
  1217. {
  1218. list_sort(NULL, mode_list, drm_mode_compare);
  1219. }
  1220. EXPORT_SYMBOL(drm_mode_sort);
  1221. /**
  1222. * drm_connector_list_update - update the mode list for the connector
  1223. * @connector: the connector to update
  1224. *
  1225. * This moves the modes from the @connector probed_modes list
  1226. * to the actual mode list. It compares the probed mode against the current
  1227. * list and only adds different/new modes.
  1228. *
  1229. * This is just a helper functions doesn't validate any modes itself and also
  1230. * doesn't prune any invalid modes. Callers need to do that themselves.
  1231. */
  1232. void drm_connector_list_update(struct drm_connector *connector)
  1233. {
  1234. struct drm_display_mode *pmode, *pt;
  1235. WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
  1236. list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
  1237. struct drm_display_mode *mode;
  1238. bool found_it = false;
  1239. /* go through current modes checking for the new probed mode */
  1240. list_for_each_entry(mode, &connector->modes, head) {
  1241. if (!drm_mode_equal(pmode, mode))
  1242. continue;
  1243. found_it = true;
  1244. /*
  1245. * If the old matching mode is stale (ie. left over
  1246. * from a previous probe) just replace it outright.
  1247. * Otherwise just merge the type bits between all
  1248. * equal probed modes.
  1249. *
  1250. * If two probed modes are considered equal, pick the
  1251. * actual timings from the one that's marked as
  1252. * preferred (in case the match isn't 100%). If
  1253. * multiple or zero preferred modes are present, favor
  1254. * the mode added to the probed_modes list first.
  1255. */
  1256. if (mode->status == MODE_STALE) {
  1257. drm_mode_copy(mode, pmode);
  1258. } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
  1259. (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
  1260. pmode->type |= mode->type;
  1261. drm_mode_copy(mode, pmode);
  1262. } else {
  1263. mode->type |= pmode->type;
  1264. }
  1265. list_del(&pmode->head);
  1266. drm_mode_destroy(connector->dev, pmode);
  1267. break;
  1268. }
  1269. if (!found_it) {
  1270. list_move_tail(&pmode->head, &connector->modes);
  1271. }
  1272. }
  1273. }
  1274. EXPORT_SYMBOL(drm_connector_list_update);
  1275. /**
  1276. * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
  1277. * @mode_option: optional per connector mode option
  1278. * @connector: connector to parse modeline for
  1279. * @mode: preallocated drm_cmdline_mode structure to fill out
  1280. *
  1281. * This parses @mode_option command line modeline for modes and options to
  1282. * configure the connector. If @mode_option is NULL the default command line
  1283. * modeline in fb_mode_option will be parsed instead.
  1284. *
  1285. * This uses the same parameters as the fb modedb.c, except for an extra
  1286. * force-enable, force-enable-digital and force-disable bit at the end::
  1287. *
  1288. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  1289. *
  1290. * The intermediate drm_cmdline_mode structure is required to store additional
  1291. * options from the command line modline like the force-enable/disable flag.
  1292. *
  1293. * Returns:
  1294. * True if a valid modeline has been parsed, false otherwise.
  1295. */
  1296. bool drm_mode_parse_command_line_for_connector(const char *mode_option,
  1297. struct drm_connector *connector,
  1298. struct drm_cmdline_mode *mode)
  1299. {
  1300. const char *name;
  1301. unsigned int namelen;
  1302. bool res_specified = false, bpp_specified = false, refresh_specified = false;
  1303. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  1304. bool yres_specified = false, cvt = false, rb = false;
  1305. bool interlace = false, margins = false, was_digit = false;
  1306. int i;
  1307. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  1308. #ifdef CONFIG_FB
  1309. if (!mode_option)
  1310. mode_option = fb_mode_option;
  1311. #endif
  1312. if (!mode_option) {
  1313. mode->specified = false;
  1314. return false;
  1315. }
  1316. name = mode_option;
  1317. namelen = strlen(name);
  1318. for (i = namelen-1; i >= 0; i--) {
  1319. switch (name[i]) {
  1320. case '@':
  1321. if (!refresh_specified && !bpp_specified &&
  1322. !yres_specified && !cvt && !rb && was_digit) {
  1323. refresh = simple_strtol(&name[i+1], NULL, 10);
  1324. refresh_specified = true;
  1325. was_digit = false;
  1326. } else
  1327. goto done;
  1328. break;
  1329. case '-':
  1330. if (!bpp_specified && !yres_specified && !cvt &&
  1331. !rb && was_digit) {
  1332. bpp = simple_strtol(&name[i+1], NULL, 10);
  1333. bpp_specified = true;
  1334. was_digit = false;
  1335. } else
  1336. goto done;
  1337. break;
  1338. case 'x':
  1339. if (!yres_specified && was_digit) {
  1340. yres = simple_strtol(&name[i+1], NULL, 10);
  1341. yres_specified = true;
  1342. was_digit = false;
  1343. } else
  1344. goto done;
  1345. break;
  1346. case '0' ... '9':
  1347. was_digit = true;
  1348. break;
  1349. case 'M':
  1350. if (yres_specified || cvt || was_digit)
  1351. goto done;
  1352. cvt = true;
  1353. break;
  1354. case 'R':
  1355. if (yres_specified || cvt || rb || was_digit)
  1356. goto done;
  1357. rb = true;
  1358. break;
  1359. case 'm':
  1360. if (cvt || yres_specified || was_digit)
  1361. goto done;
  1362. margins = true;
  1363. break;
  1364. case 'i':
  1365. if (cvt || yres_specified || was_digit)
  1366. goto done;
  1367. interlace = true;
  1368. break;
  1369. case 'e':
  1370. if (yres_specified || bpp_specified || refresh_specified ||
  1371. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1372. goto done;
  1373. force = DRM_FORCE_ON;
  1374. break;
  1375. case 'D':
  1376. if (yres_specified || bpp_specified || refresh_specified ||
  1377. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1378. goto done;
  1379. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  1380. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  1381. force = DRM_FORCE_ON;
  1382. else
  1383. force = DRM_FORCE_ON_DIGITAL;
  1384. break;
  1385. case 'd':
  1386. if (yres_specified || bpp_specified || refresh_specified ||
  1387. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1388. goto done;
  1389. force = DRM_FORCE_OFF;
  1390. break;
  1391. default:
  1392. goto done;
  1393. }
  1394. }
  1395. if (i < 0 && yres_specified) {
  1396. char *ch;
  1397. xres = simple_strtol(name, &ch, 10);
  1398. if ((ch != NULL) && (*ch == 'x'))
  1399. res_specified = true;
  1400. else
  1401. i = ch - name;
  1402. } else if (!yres_specified && was_digit) {
  1403. /* catch mode that begins with digits but has no 'x' */
  1404. i = 0;
  1405. }
  1406. done:
  1407. if (i >= 0) {
  1408. pr_warn("[drm] parse error at position %i in video mode '%s'\n",
  1409. i, name);
  1410. mode->specified = false;
  1411. return false;
  1412. }
  1413. if (res_specified) {
  1414. mode->specified = true;
  1415. mode->xres = xres;
  1416. mode->yres = yres;
  1417. }
  1418. if (refresh_specified) {
  1419. mode->refresh_specified = true;
  1420. mode->refresh = refresh;
  1421. }
  1422. if (bpp_specified) {
  1423. mode->bpp_specified = true;
  1424. mode->bpp = bpp;
  1425. }
  1426. mode->rb = rb;
  1427. mode->cvt = cvt;
  1428. mode->interlace = interlace;
  1429. mode->margins = margins;
  1430. mode->force = force;
  1431. return true;
  1432. }
  1433. EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
  1434. /**
  1435. * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
  1436. * @dev: DRM device to create the new mode for
  1437. * @cmd: input command line modeline
  1438. *
  1439. * Returns:
  1440. * Pointer to converted mode on success, NULL on error.
  1441. */
  1442. struct drm_display_mode *
  1443. drm_mode_create_from_cmdline_mode(struct drm_device *dev,
  1444. struct drm_cmdline_mode *cmd)
  1445. {
  1446. struct drm_display_mode *mode;
  1447. if (cmd->cvt)
  1448. mode = drm_cvt_mode(dev,
  1449. cmd->xres, cmd->yres,
  1450. cmd->refresh_specified ? cmd->refresh : 60,
  1451. cmd->rb, cmd->interlace,
  1452. cmd->margins);
  1453. else
  1454. mode = drm_gtf_mode(dev,
  1455. cmd->xres, cmd->yres,
  1456. cmd->refresh_specified ? cmd->refresh : 60,
  1457. cmd->interlace,
  1458. cmd->margins);
  1459. if (!mode)
  1460. return NULL;
  1461. mode->type |= DRM_MODE_TYPE_USERDEF;
  1462. /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
  1463. if (cmd->xres == 1366)
  1464. drm_mode_fixup_1366x768(mode);
  1465. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1466. return mode;
  1467. }
  1468. EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
  1469. /**
  1470. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1471. * @out: drm_mode_modeinfo struct to return to the user
  1472. * @in: drm_display_mode to use
  1473. *
  1474. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1475. * the user.
  1476. */
  1477. void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
  1478. const struct drm_display_mode *in)
  1479. {
  1480. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1481. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1482. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1483. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1484. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1485. "timing values too large for mode info\n");
  1486. out->clock = in->clock;
  1487. out->hdisplay = in->hdisplay;
  1488. out->hsync_start = in->hsync_start;
  1489. out->hsync_end = in->hsync_end;
  1490. out->htotal = in->htotal;
  1491. out->hskew = in->hskew;
  1492. out->vdisplay = in->vdisplay;
  1493. out->vsync_start = in->vsync_start;
  1494. out->vsync_end = in->vsync_end;
  1495. out->vtotal = in->vtotal;
  1496. out->vscan = in->vscan;
  1497. out->vrefresh = in->vrefresh;
  1498. out->flags = in->flags;
  1499. out->type = in->type;
  1500. switch (in->picture_aspect_ratio) {
  1501. case HDMI_PICTURE_ASPECT_4_3:
  1502. out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
  1503. break;
  1504. case HDMI_PICTURE_ASPECT_16_9:
  1505. out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
  1506. break;
  1507. case HDMI_PICTURE_ASPECT_64_27:
  1508. out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
  1509. break;
  1510. case HDMI_PICTURE_ASPECT_256_135:
  1511. out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
  1512. break;
  1513. case HDMI_PICTURE_ASPECT_RESERVED:
  1514. default:
  1515. out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
  1516. break;
  1517. }
  1518. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1519. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1520. }
  1521. /**
  1522. * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
  1523. * @dev: drm device
  1524. * @out: drm_display_mode to return to the user
  1525. * @in: drm_mode_modeinfo to use
  1526. *
  1527. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1528. * the caller.
  1529. *
  1530. * Returns:
  1531. * Zero on success, negative errno on failure.
  1532. */
  1533. int drm_mode_convert_umode(struct drm_device *dev,
  1534. struct drm_display_mode *out,
  1535. const struct drm_mode_modeinfo *in)
  1536. {
  1537. if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1538. return -ERANGE;
  1539. out->clock = in->clock;
  1540. out->hdisplay = in->hdisplay;
  1541. out->hsync_start = in->hsync_start;
  1542. out->hsync_end = in->hsync_end;
  1543. out->htotal = in->htotal;
  1544. out->hskew = in->hskew;
  1545. out->vdisplay = in->vdisplay;
  1546. out->vsync_start = in->vsync_start;
  1547. out->vsync_end = in->vsync_end;
  1548. out->vtotal = in->vtotal;
  1549. out->vscan = in->vscan;
  1550. out->vrefresh = in->vrefresh;
  1551. out->flags = in->flags;
  1552. /*
  1553. * Old xf86-video-vmware (possibly others too) used to
  1554. * leave 'type' unititialized. Just ignore any bits we
  1555. * don't like. It's a just hint after all, and more
  1556. * useful for the kernel->userspace direction anyway.
  1557. */
  1558. out->type = in->type & DRM_MODE_TYPE_ALL;
  1559. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1560. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1561. /* Clearing picture aspect ratio bits from out flags,
  1562. * as the aspect-ratio information is not stored in
  1563. * flags for kernel-mode, but in picture_aspect_ratio.
  1564. */
  1565. out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
  1566. switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
  1567. case DRM_MODE_FLAG_PIC_AR_4_3:
  1568. out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_4_3;
  1569. break;
  1570. case DRM_MODE_FLAG_PIC_AR_16_9:
  1571. out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_16_9;
  1572. break;
  1573. case DRM_MODE_FLAG_PIC_AR_64_27:
  1574. out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_64_27;
  1575. break;
  1576. case DRM_MODE_FLAG_PIC_AR_256_135:
  1577. out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_256_135;
  1578. break;
  1579. default:
  1580. out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
  1581. break;
  1582. }
  1583. out->status = drm_mode_validate_driver(dev, out);
  1584. if (out->status != MODE_OK)
  1585. return -EINVAL;
  1586. drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
  1587. return 0;
  1588. }
  1589. /**
  1590. * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
  1591. * output format
  1592. *
  1593. * @display: display under action
  1594. * @mode: video mode to be tested.
  1595. *
  1596. * Returns:
  1597. * true if the mode can be supported in YCBCR420 format
  1598. * false if not.
  1599. */
  1600. bool drm_mode_is_420_only(const struct drm_display_info *display,
  1601. const struct drm_display_mode *mode)
  1602. {
  1603. u8 vic = drm_match_cea_mode(mode);
  1604. return test_bit(vic, display->hdmi.y420_vdb_modes);
  1605. }
  1606. EXPORT_SYMBOL(drm_mode_is_420_only);
  1607. /**
  1608. * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
  1609. * output format also (along with RGB/YCBCR444/422)
  1610. *
  1611. * @display: display under action.
  1612. * @mode: video mode to be tested.
  1613. *
  1614. * Returns:
  1615. * true if the mode can be support YCBCR420 format
  1616. * false if not.
  1617. */
  1618. bool drm_mode_is_420_also(const struct drm_display_info *display,
  1619. const struct drm_display_mode *mode)
  1620. {
  1621. u8 vic = drm_match_cea_mode(mode);
  1622. return test_bit(vic, display->hdmi.y420_cmdb_modes);
  1623. }
  1624. EXPORT_SYMBOL(drm_mode_is_420_also);
  1625. /**
  1626. * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
  1627. * output format
  1628. *
  1629. * @display: display under action.
  1630. * @mode: video mode to be tested.
  1631. *
  1632. * Returns:
  1633. * true if the mode can be supported in YCBCR420 format
  1634. * false if not.
  1635. */
  1636. bool drm_mode_is_420(const struct drm_display_info *display,
  1637. const struct drm_display_mode *mode)
  1638. {
  1639. return drm_mode_is_420_only(display, mode) ||
  1640. drm_mode_is_420_also(display, mode);
  1641. }
  1642. EXPORT_SYMBOL(drm_mode_is_420);