drm_modes.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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 %d:\"%s\" %d %d %d %d %d %d %d %d %d %d "
  50. "0x%x 0x%x\n",
  51. mode->base.id, mode->name, mode->vrefresh, mode->clock,
  52. mode->hdisplay, mode->hsync_start,
  53. mode->hsync_end, mode->htotal,
  54. mode->vdisplay, mode->vsync_start,
  55. mode->vsync_end, mode->vtotal, mode->type, mode->flags);
  56. }
  57. EXPORT_SYMBOL(drm_mode_debug_printmodeline);
  58. /**
  59. * drm_mode_create - create a new display mode
  60. * @dev: DRM device
  61. *
  62. * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
  63. * and return it.
  64. *
  65. * Returns:
  66. * Pointer to new mode on success, NULL on error.
  67. */
  68. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  69. {
  70. struct drm_display_mode *nmode;
  71. nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  72. if (!nmode)
  73. return NULL;
  74. if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
  75. kfree(nmode);
  76. return NULL;
  77. }
  78. return nmode;
  79. }
  80. EXPORT_SYMBOL(drm_mode_create);
  81. /**
  82. * drm_mode_destroy - remove a mode
  83. * @dev: DRM device
  84. * @mode: mode to remove
  85. *
  86. * Release @mode's unique ID, then free it @mode structure itself using kfree.
  87. */
  88. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  89. {
  90. if (!mode)
  91. return;
  92. drm_mode_object_put(dev, &mode->base);
  93. kfree(mode);
  94. }
  95. EXPORT_SYMBOL(drm_mode_destroy);
  96. /**
  97. * drm_mode_probed_add - add a mode to a connector's probed_mode list
  98. * @connector: connector the new mode
  99. * @mode: mode data
  100. *
  101. * Add @mode to @connector's probed_mode list for later use. This list should
  102. * then in a second step get filtered and all the modes actually supported by
  103. * the hardware moved to the @connector's modes list.
  104. */
  105. void drm_mode_probed_add(struct drm_connector *connector,
  106. struct drm_display_mode *mode)
  107. {
  108. WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
  109. list_add_tail(&mode->head, &connector->probed_modes);
  110. }
  111. EXPORT_SYMBOL(drm_mode_probed_add);
  112. /**
  113. * drm_cvt_mode -create a modeline based on the CVT algorithm
  114. * @dev: drm device
  115. * @hdisplay: hdisplay size
  116. * @vdisplay: vdisplay size
  117. * @vrefresh: vrefresh rate
  118. * @reduced: whether to use reduced blanking
  119. * @interlaced: whether to compute an interlaced mode
  120. * @margins: whether to add margins (borders)
  121. *
  122. * This function is called to generate the modeline based on CVT algorithm
  123. * according to the hdisplay, vdisplay, vrefresh.
  124. * It is based from the VESA(TM) Coordinated Video Timing Generator by
  125. * Graham Loveridge April 9, 2003 available at
  126. * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
  127. *
  128. * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
  129. * What I have done is to translate it by using integer calculation.
  130. *
  131. * Returns:
  132. * The modeline based on the CVT algorithm stored in a drm_display_mode object.
  133. * The display mode object is allocated with drm_mode_create(). Returns NULL
  134. * when no mode could be allocated.
  135. */
  136. struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
  137. int vdisplay, int vrefresh,
  138. bool reduced, bool interlaced, bool margins)
  139. {
  140. #define HV_FACTOR 1000
  141. /* 1) top/bottom margin size (% of height) - default: 1.8, */
  142. #define CVT_MARGIN_PERCENTAGE 18
  143. /* 2) character cell horizontal granularity (pixels) - default 8 */
  144. #define CVT_H_GRANULARITY 8
  145. /* 3) Minimum vertical porch (lines) - default 3 */
  146. #define CVT_MIN_V_PORCH 3
  147. /* 4) Minimum number of vertical back porch lines - default 6 */
  148. #define CVT_MIN_V_BPORCH 6
  149. /* Pixel Clock step (kHz) */
  150. #define CVT_CLOCK_STEP 250
  151. struct drm_display_mode *drm_mode;
  152. unsigned int vfieldrate, hperiod;
  153. int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
  154. int interlace;
  155. /* allocate the drm_display_mode structure. If failure, we will
  156. * return directly
  157. */
  158. drm_mode = drm_mode_create(dev);
  159. if (!drm_mode)
  160. return NULL;
  161. /* the CVT default refresh rate is 60Hz */
  162. if (!vrefresh)
  163. vrefresh = 60;
  164. /* the required field fresh rate */
  165. if (interlaced)
  166. vfieldrate = vrefresh * 2;
  167. else
  168. vfieldrate = vrefresh;
  169. /* horizontal pixels */
  170. hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
  171. /* determine the left&right borders */
  172. hmargin = 0;
  173. if (margins) {
  174. hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  175. hmargin -= hmargin % CVT_H_GRANULARITY;
  176. }
  177. /* find the total active pixels */
  178. drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
  179. /* find the number of lines per field */
  180. if (interlaced)
  181. vdisplay_rnd = vdisplay / 2;
  182. else
  183. vdisplay_rnd = vdisplay;
  184. /* find the top & bottom borders */
  185. vmargin = 0;
  186. if (margins)
  187. vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  188. drm_mode->vdisplay = vdisplay + 2 * vmargin;
  189. /* Interlaced */
  190. if (interlaced)
  191. interlace = 1;
  192. else
  193. interlace = 0;
  194. /* Determine VSync Width from aspect ratio */
  195. if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
  196. vsync = 4;
  197. else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
  198. vsync = 5;
  199. else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
  200. vsync = 6;
  201. else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
  202. vsync = 7;
  203. else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
  204. vsync = 7;
  205. else /* custom */
  206. vsync = 10;
  207. if (!reduced) {
  208. /* simplify the GTF calculation */
  209. /* 4) Minimum time of vertical sync + back porch interval (µs)
  210. * default 550.0
  211. */
  212. int tmp1, tmp2;
  213. #define CVT_MIN_VSYNC_BP 550
  214. /* 3) Nominal HSync width (% of line period) - default 8 */
  215. #define CVT_HSYNC_PERCENTAGE 8
  216. unsigned int hblank_percentage;
  217. int vsyncandback_porch, vback_porch, hblank;
  218. /* estimated the horizontal period */
  219. tmp1 = HV_FACTOR * 1000000 -
  220. CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
  221. tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
  222. interlace;
  223. hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
  224. tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
  225. /* 9. Find number of lines in sync + backporch */
  226. if (tmp1 < (vsync + CVT_MIN_V_PORCH))
  227. vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
  228. else
  229. vsyncandback_porch = tmp1;
  230. /* 10. Find number of lines in back porch */
  231. vback_porch = vsyncandback_porch - vsync;
  232. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
  233. vsyncandback_porch + CVT_MIN_V_PORCH;
  234. /* 5) Definition of Horizontal blanking time limitation */
  235. /* Gradient (%/kHz) - default 600 */
  236. #define CVT_M_FACTOR 600
  237. /* Offset (%) - default 40 */
  238. #define CVT_C_FACTOR 40
  239. /* Blanking time scaling factor - default 128 */
  240. #define CVT_K_FACTOR 128
  241. /* Scaling factor weighting - default 20 */
  242. #define CVT_J_FACTOR 20
  243. #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
  244. #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
  245. CVT_J_FACTOR)
  246. /* 12. Find ideal blanking duty cycle from formula */
  247. hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
  248. hperiod / 1000;
  249. /* 13. Blanking time */
  250. if (hblank_percentage < 20 * HV_FACTOR)
  251. hblank_percentage = 20 * HV_FACTOR;
  252. hblank = drm_mode->hdisplay * hblank_percentage /
  253. (100 * HV_FACTOR - hblank_percentage);
  254. hblank -= hblank % (2 * CVT_H_GRANULARITY);
  255. /* 14. find the total pixels per line */
  256. drm_mode->htotal = drm_mode->hdisplay + hblank;
  257. drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
  258. drm_mode->hsync_start = drm_mode->hsync_end -
  259. (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
  260. drm_mode->hsync_start += CVT_H_GRANULARITY -
  261. drm_mode->hsync_start % CVT_H_GRANULARITY;
  262. /* fill the Vsync values */
  263. drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
  264. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  265. } else {
  266. /* Reduced blanking */
  267. /* Minimum vertical blanking interval time (µs)- default 460 */
  268. #define CVT_RB_MIN_VBLANK 460
  269. /* Fixed number of clocks for horizontal sync */
  270. #define CVT_RB_H_SYNC 32
  271. /* Fixed number of clocks for horizontal blanking */
  272. #define CVT_RB_H_BLANK 160
  273. /* Fixed number of lines for vertical front porch - default 3*/
  274. #define CVT_RB_VFPORCH 3
  275. int vbilines;
  276. int tmp1, tmp2;
  277. /* 8. Estimate Horizontal period. */
  278. tmp1 = HV_FACTOR * 1000000 -
  279. CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
  280. tmp2 = vdisplay_rnd + 2 * vmargin;
  281. hperiod = tmp1 / (tmp2 * vfieldrate);
  282. /* 9. Find number of lines in vertical blanking */
  283. vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
  284. /* 10. Check if vertical blanking is sufficient */
  285. if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
  286. vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
  287. /* 11. Find total number of lines in vertical field */
  288. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
  289. /* 12. Find total number of pixels in a line */
  290. drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
  291. /* Fill in HSync values */
  292. drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
  293. drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
  294. /* Fill in VSync values */
  295. drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
  296. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  297. }
  298. /* 15/13. Find pixel clock frequency (kHz for xf86) */
  299. drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
  300. drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
  301. /* 18/16. Find actual vertical frame frequency */
  302. /* ignore - just set the mode flag for interlaced */
  303. if (interlaced) {
  304. drm_mode->vtotal *= 2;
  305. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  306. }
  307. /* Fill the mode line name */
  308. drm_mode_set_name(drm_mode);
  309. if (reduced)
  310. drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
  311. DRM_MODE_FLAG_NVSYNC);
  312. else
  313. drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
  314. DRM_MODE_FLAG_NHSYNC);
  315. return drm_mode;
  316. }
  317. EXPORT_SYMBOL(drm_cvt_mode);
  318. /**
  319. * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
  320. * @dev: drm device
  321. * @hdisplay: hdisplay size
  322. * @vdisplay: vdisplay size
  323. * @vrefresh: vrefresh rate.
  324. * @interlaced: whether to compute an interlaced mode
  325. * @margins: desired margin (borders) size
  326. * @GTF_M: extended GTF formula parameters
  327. * @GTF_2C: extended GTF formula parameters
  328. * @GTF_K: extended GTF formula parameters
  329. * @GTF_2J: extended GTF formula parameters
  330. *
  331. * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
  332. * in here multiplied by two. For a C of 40, pass in 80.
  333. *
  334. * Returns:
  335. * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
  336. * The display mode object is allocated with drm_mode_create(). Returns NULL
  337. * when no mode could be allocated.
  338. */
  339. struct drm_display_mode *
  340. drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
  341. int vrefresh, bool interlaced, int margins,
  342. int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
  343. { /* 1) top/bottom margin size (% of height) - default: 1.8, */
  344. #define GTF_MARGIN_PERCENTAGE 18
  345. /* 2) character cell horizontal granularity (pixels) - default 8 */
  346. #define GTF_CELL_GRAN 8
  347. /* 3) Minimum vertical porch (lines) - default 3 */
  348. #define GTF_MIN_V_PORCH 1
  349. /* width of vsync in lines */
  350. #define V_SYNC_RQD 3
  351. /* width of hsync as % of total line */
  352. #define H_SYNC_PERCENT 8
  353. /* min time of vsync + back porch (microsec) */
  354. #define MIN_VSYNC_PLUS_BP 550
  355. /* C' and M' are part of the Blanking Duty Cycle computation */
  356. #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
  357. #define GTF_M_PRIME (GTF_K * GTF_M / 256)
  358. struct drm_display_mode *drm_mode;
  359. unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
  360. int top_margin, bottom_margin;
  361. int interlace;
  362. unsigned int hfreq_est;
  363. int vsync_plus_bp, vback_porch;
  364. unsigned int vtotal_lines, vfieldrate_est, hperiod;
  365. unsigned int vfield_rate, vframe_rate;
  366. int left_margin, right_margin;
  367. unsigned int total_active_pixels, ideal_duty_cycle;
  368. unsigned int hblank, total_pixels, pixel_freq;
  369. int hsync, hfront_porch, vodd_front_porch_lines;
  370. unsigned int tmp1, tmp2;
  371. drm_mode = drm_mode_create(dev);
  372. if (!drm_mode)
  373. return NULL;
  374. /* 1. In order to give correct results, the number of horizontal
  375. * pixels requested is first processed to ensure that it is divisible
  376. * by the character size, by rounding it to the nearest character
  377. * cell boundary:
  378. */
  379. hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  380. hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
  381. /* 2. If interlace is requested, the number of vertical lines assumed
  382. * by the calculation must be halved, as the computation calculates
  383. * the number of vertical lines per field.
  384. */
  385. if (interlaced)
  386. vdisplay_rnd = vdisplay / 2;
  387. else
  388. vdisplay_rnd = vdisplay;
  389. /* 3. Find the frame rate required: */
  390. if (interlaced)
  391. vfieldrate_rqd = vrefresh * 2;
  392. else
  393. vfieldrate_rqd = vrefresh;
  394. /* 4. Find number of lines in Top margin: */
  395. top_margin = 0;
  396. if (margins)
  397. top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  398. 1000;
  399. /* 5. Find number of lines in bottom margin: */
  400. bottom_margin = top_margin;
  401. /* 6. If interlace is required, then set variable interlace: */
  402. if (interlaced)
  403. interlace = 1;
  404. else
  405. interlace = 0;
  406. /* 7. Estimate the Horizontal frequency */
  407. {
  408. tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
  409. tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
  410. 2 + interlace;
  411. hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
  412. }
  413. /* 8. Find the number of lines in V sync + back porch */
  414. /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
  415. vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
  416. vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
  417. /* 9. Find the number of lines in V back porch alone: */
  418. vback_porch = vsync_plus_bp - V_SYNC_RQD;
  419. /* 10. Find the total number of lines in Vertical field period: */
  420. vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
  421. vsync_plus_bp + GTF_MIN_V_PORCH;
  422. /* 11. Estimate the Vertical field frequency: */
  423. vfieldrate_est = hfreq_est / vtotal_lines;
  424. /* 12. Find the actual horizontal period: */
  425. hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
  426. /* 13. Find the actual Vertical field frequency: */
  427. vfield_rate = hfreq_est / vtotal_lines;
  428. /* 14. Find the Vertical frame frequency: */
  429. if (interlaced)
  430. vframe_rate = vfield_rate / 2;
  431. else
  432. vframe_rate = vfield_rate;
  433. /* 15. Find number of pixels in left margin: */
  434. if (margins)
  435. left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  436. 1000;
  437. else
  438. left_margin = 0;
  439. /* 16.Find number of pixels in right margin: */
  440. right_margin = left_margin;
  441. /* 17.Find total number of active pixels in image and left and right */
  442. total_active_pixels = hdisplay_rnd + left_margin + right_margin;
  443. /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
  444. ideal_duty_cycle = GTF_C_PRIME * 1000 -
  445. (GTF_M_PRIME * 1000000 / hfreq_est);
  446. /* 19.Find the number of pixels in the blanking time to the nearest
  447. * double character cell: */
  448. hblank = total_active_pixels * ideal_duty_cycle /
  449. (100000 - ideal_duty_cycle);
  450. hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
  451. hblank = hblank * 2 * GTF_CELL_GRAN;
  452. /* 20.Find total number of pixels: */
  453. total_pixels = total_active_pixels + hblank;
  454. /* 21.Find pixel clock frequency: */
  455. pixel_freq = total_pixels * hfreq_est / 1000;
  456. /* Stage 1 computations are now complete; I should really pass
  457. * the results to another function and do the Stage 2 computations,
  458. * but I only need a few more values so I'll just append the
  459. * computations here for now */
  460. /* 17. Find the number of pixels in the horizontal sync period: */
  461. hsync = H_SYNC_PERCENT * total_pixels / 100;
  462. hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  463. hsync = hsync * GTF_CELL_GRAN;
  464. /* 18. Find the number of pixels in horizontal front porch period */
  465. hfront_porch = hblank / 2 - hsync;
  466. /* 36. Find the number of lines in the odd front porch period: */
  467. vodd_front_porch_lines = GTF_MIN_V_PORCH ;
  468. /* finally, pack the results in the mode struct */
  469. drm_mode->hdisplay = hdisplay_rnd;
  470. drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
  471. drm_mode->hsync_end = drm_mode->hsync_start + hsync;
  472. drm_mode->htotal = total_pixels;
  473. drm_mode->vdisplay = vdisplay_rnd;
  474. drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
  475. drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
  476. drm_mode->vtotal = vtotal_lines;
  477. drm_mode->clock = pixel_freq;
  478. if (interlaced) {
  479. drm_mode->vtotal *= 2;
  480. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  481. }
  482. drm_mode_set_name(drm_mode);
  483. if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
  484. drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
  485. else
  486. drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
  487. return drm_mode;
  488. }
  489. EXPORT_SYMBOL(drm_gtf_mode_complex);
  490. /**
  491. * drm_gtf_mode - create the modeline based on the GTF algorithm
  492. * @dev: drm device
  493. * @hdisplay: hdisplay size
  494. * @vdisplay: vdisplay size
  495. * @vrefresh: vrefresh rate.
  496. * @interlaced: whether to compute an interlaced mode
  497. * @margins: desired margin (borders) size
  498. *
  499. * return the modeline based on GTF algorithm
  500. *
  501. * This function is to create the modeline based on the GTF algorithm.
  502. * Generalized Timing Formula is derived from:
  503. * GTF Spreadsheet by Andy Morrish (1/5/97)
  504. * available at http://www.vesa.org
  505. *
  506. * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
  507. * What I have done is to translate it by using integer calculation.
  508. * I also refer to the function of fb_get_mode in the file of
  509. * drivers/video/fbmon.c
  510. *
  511. * Standard GTF parameters:
  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. #ifdef CONFIG_OF
  606. /**
  607. * of_get_drm_display_mode - get a drm_display_mode from devicetree
  608. * @np: device_node with the timing specification
  609. * @dmode: will be set to the return value
  610. * @index: index into the list of display timings in devicetree
  611. *
  612. * This function is expensive and should only be used, if only one mode is to be
  613. * read from DT. To get multiple modes start with of_get_display_timings and
  614. * work with that instead.
  615. *
  616. * Returns:
  617. * 0 on success, a negative errno code when no of videomode node was found.
  618. */
  619. int of_get_drm_display_mode(struct device_node *np,
  620. struct drm_display_mode *dmode, int index)
  621. {
  622. struct videomode vm;
  623. int ret;
  624. ret = of_get_videomode(np, &vm, index);
  625. if (ret)
  626. return ret;
  627. drm_display_mode_from_videomode(&vm, dmode);
  628. pr_debug("%s: got %dx%d display mode from %s\n",
  629. of_node_full_name(np), vm.hactive, vm.vactive, np->name);
  630. drm_mode_debug_printmodeline(dmode);
  631. return 0;
  632. }
  633. EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
  634. #endif /* CONFIG_OF */
  635. #endif /* CONFIG_VIDEOMODE_HELPERS */
  636. /**
  637. * drm_mode_set_name - set the name on a mode
  638. * @mode: name will be set in this mode
  639. *
  640. * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
  641. * with an optional 'i' suffix for interlaced modes.
  642. */
  643. void drm_mode_set_name(struct drm_display_mode *mode)
  644. {
  645. bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  646. snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
  647. mode->hdisplay, mode->vdisplay,
  648. interlaced ? "i" : "");
  649. }
  650. EXPORT_SYMBOL(drm_mode_set_name);
  651. /** drm_mode_hsync - get the hsync of a mode
  652. * @mode: mode
  653. *
  654. * Returns:
  655. * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
  656. * value first if it is not yet set.
  657. */
  658. int drm_mode_hsync(const struct drm_display_mode *mode)
  659. {
  660. unsigned int calc_val;
  661. if (mode->hsync)
  662. return mode->hsync;
  663. if (mode->htotal < 0)
  664. return 0;
  665. calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
  666. calc_val += 500; /* round to 1000Hz */
  667. calc_val /= 1000; /* truncate to kHz */
  668. return calc_val;
  669. }
  670. EXPORT_SYMBOL(drm_mode_hsync);
  671. /**
  672. * drm_mode_vrefresh - get the vrefresh of a mode
  673. * @mode: mode
  674. *
  675. * Returns:
  676. * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
  677. * value first if it is not yet set.
  678. */
  679. int drm_mode_vrefresh(const struct drm_display_mode *mode)
  680. {
  681. int refresh = 0;
  682. unsigned int calc_val;
  683. if (mode->vrefresh > 0)
  684. refresh = mode->vrefresh;
  685. else if (mode->htotal > 0 && mode->vtotal > 0) {
  686. int vtotal;
  687. vtotal = mode->vtotal;
  688. /* work out vrefresh the value will be x1000 */
  689. calc_val = (mode->clock * 1000);
  690. calc_val /= mode->htotal;
  691. refresh = (calc_val + vtotal / 2) / vtotal;
  692. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  693. refresh *= 2;
  694. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  695. refresh /= 2;
  696. if (mode->vscan > 1)
  697. refresh /= mode->vscan;
  698. }
  699. return refresh;
  700. }
  701. EXPORT_SYMBOL(drm_mode_vrefresh);
  702. /**
  703. * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
  704. * @p: mode
  705. * @adjust_flags: a combination of adjustment flags
  706. *
  707. * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
  708. *
  709. * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
  710. * interlaced modes.
  711. * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
  712. * buffers containing two eyes (only adjust the timings when needed, eg. for
  713. * "frame packing" or "side by side full").
  714. * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
  715. * be performed for doublescan and vscan > 1 modes respectively.
  716. */
  717. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  718. {
  719. if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
  720. return;
  721. p->crtc_clock = p->clock;
  722. p->crtc_hdisplay = p->hdisplay;
  723. p->crtc_hsync_start = p->hsync_start;
  724. p->crtc_hsync_end = p->hsync_end;
  725. p->crtc_htotal = p->htotal;
  726. p->crtc_hskew = p->hskew;
  727. p->crtc_vdisplay = p->vdisplay;
  728. p->crtc_vsync_start = p->vsync_start;
  729. p->crtc_vsync_end = p->vsync_end;
  730. p->crtc_vtotal = p->vtotal;
  731. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  732. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  733. p->crtc_vdisplay /= 2;
  734. p->crtc_vsync_start /= 2;
  735. p->crtc_vsync_end /= 2;
  736. p->crtc_vtotal /= 2;
  737. }
  738. }
  739. if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
  740. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  741. p->crtc_vdisplay *= 2;
  742. p->crtc_vsync_start *= 2;
  743. p->crtc_vsync_end *= 2;
  744. p->crtc_vtotal *= 2;
  745. }
  746. }
  747. if (!(adjust_flags & CRTC_NO_VSCAN)) {
  748. if (p->vscan > 1) {
  749. p->crtc_vdisplay *= p->vscan;
  750. p->crtc_vsync_start *= p->vscan;
  751. p->crtc_vsync_end *= p->vscan;
  752. p->crtc_vtotal *= p->vscan;
  753. }
  754. }
  755. if (adjust_flags & CRTC_STEREO_DOUBLE) {
  756. unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
  757. switch (layout) {
  758. case DRM_MODE_FLAG_3D_FRAME_PACKING:
  759. p->crtc_clock *= 2;
  760. p->crtc_vdisplay += p->crtc_vtotal;
  761. p->crtc_vsync_start += p->crtc_vtotal;
  762. p->crtc_vsync_end += p->crtc_vtotal;
  763. p->crtc_vtotal += p->crtc_vtotal;
  764. break;
  765. }
  766. }
  767. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  768. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  769. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  770. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  771. }
  772. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  773. /**
  774. * drm_mode_copy - copy the mode
  775. * @dst: mode to overwrite
  776. * @src: mode to copy
  777. *
  778. * Copy an existing mode into another mode, preserving the object id and
  779. * list head of the destination mode.
  780. */
  781. void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
  782. {
  783. int id = dst->base.id;
  784. struct list_head head = dst->head;
  785. *dst = *src;
  786. dst->base.id = id;
  787. dst->head = head;
  788. }
  789. EXPORT_SYMBOL(drm_mode_copy);
  790. /**
  791. * drm_mode_duplicate - allocate and duplicate an existing mode
  792. * @dev: drm_device to allocate the duplicated mode for
  793. * @mode: mode to duplicate
  794. *
  795. * Just allocate a new mode, copy the existing mode into it, and return
  796. * a pointer to it. Used to create new instances of established modes.
  797. *
  798. * Returns:
  799. * Pointer to duplicated mode on success, NULL on error.
  800. */
  801. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  802. const struct drm_display_mode *mode)
  803. {
  804. struct drm_display_mode *nmode;
  805. nmode = drm_mode_create(dev);
  806. if (!nmode)
  807. return NULL;
  808. drm_mode_copy(nmode, mode);
  809. return nmode;
  810. }
  811. EXPORT_SYMBOL(drm_mode_duplicate);
  812. /**
  813. * drm_mode_equal - test modes for equality
  814. * @mode1: first mode
  815. * @mode2: second mode
  816. *
  817. * Check to see if @mode1 and @mode2 are equivalent.
  818. *
  819. * Returns:
  820. * True if the modes are equal, false otherwise.
  821. */
  822. bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
  823. {
  824. if (!mode1 && !mode2)
  825. return true;
  826. if (!mode1 || !mode2)
  827. return false;
  828. /* do clock check convert to PICOS so fb modes get matched
  829. * the same */
  830. if (mode1->clock && mode2->clock) {
  831. if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
  832. return false;
  833. } else if (mode1->clock != mode2->clock)
  834. return false;
  835. return drm_mode_equal_no_clocks(mode1, mode2);
  836. }
  837. EXPORT_SYMBOL(drm_mode_equal);
  838. /**
  839. * drm_mode_equal_no_clocks - test modes for equality
  840. * @mode1: first mode
  841. * @mode2: second mode
  842. *
  843. * Check to see if @mode1 and @mode2 are equivalent, but
  844. * don't check the pixel clocks.
  845. *
  846. * Returns:
  847. * True if the modes are equal, false otherwise.
  848. */
  849. bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
  850. {
  851. if ((mode1->flags & DRM_MODE_FLAG_3D_MASK) !=
  852. (mode2->flags & DRM_MODE_FLAG_3D_MASK))
  853. return false;
  854. return drm_mode_equal_no_clocks_no_stereo(mode1, mode2);
  855. }
  856. EXPORT_SYMBOL(drm_mode_equal_no_clocks);
  857. /**
  858. * drm_mode_equal_no_clocks_no_stereo - test modes for equality
  859. * @mode1: first mode
  860. * @mode2: second mode
  861. *
  862. * Check to see if @mode1 and @mode2 are equivalent, but
  863. * don't check the pixel clocks nor the stereo layout.
  864. *
  865. * Returns:
  866. * True if the modes are equal, false otherwise.
  867. */
  868. bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
  869. const struct drm_display_mode *mode2)
  870. {
  871. if (mode1->hdisplay == mode2->hdisplay &&
  872. mode1->hsync_start == mode2->hsync_start &&
  873. mode1->hsync_end == mode2->hsync_end &&
  874. mode1->htotal == mode2->htotal &&
  875. mode1->hskew == mode2->hskew &&
  876. mode1->vdisplay == mode2->vdisplay &&
  877. mode1->vsync_start == mode2->vsync_start &&
  878. mode1->vsync_end == mode2->vsync_end &&
  879. mode1->vtotal == mode2->vtotal &&
  880. mode1->vscan == mode2->vscan &&
  881. (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
  882. (mode2->flags & ~DRM_MODE_FLAG_3D_MASK))
  883. return true;
  884. return false;
  885. }
  886. EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
  887. /**
  888. * drm_mode_validate_basic - make sure the mode is somewhat sane
  889. * @mode: mode to check
  890. *
  891. * Check that the mode timings are at least somewhat reasonable.
  892. * Any hardware specific limits are left up for each driver to check.
  893. *
  894. * Returns:
  895. * The mode status
  896. */
  897. enum drm_mode_status
  898. drm_mode_validate_basic(const struct drm_display_mode *mode)
  899. {
  900. if (mode->clock == 0)
  901. return MODE_CLOCK_LOW;
  902. if (mode->hdisplay == 0 ||
  903. mode->hsync_start < mode->hdisplay ||
  904. mode->hsync_end < mode->hsync_start ||
  905. mode->htotal < mode->hsync_end)
  906. return MODE_H_ILLEGAL;
  907. if (mode->vdisplay == 0 ||
  908. mode->vsync_start < mode->vdisplay ||
  909. mode->vsync_end < mode->vsync_start ||
  910. mode->vtotal < mode->vsync_end)
  911. return MODE_V_ILLEGAL;
  912. return MODE_OK;
  913. }
  914. EXPORT_SYMBOL(drm_mode_validate_basic);
  915. /**
  916. * drm_mode_validate_size - make sure modes adhere to size constraints
  917. * @mode: mode to check
  918. * @maxX: maximum width
  919. * @maxY: maximum height
  920. *
  921. * This function is a helper which can be used to validate modes against size
  922. * limitations of the DRM device/connector. If a mode is too big its status
  923. * member is updated with the appropriate validation failure code. The list
  924. * itself is not changed.
  925. *
  926. * Returns:
  927. * The mode status
  928. */
  929. enum drm_mode_status
  930. drm_mode_validate_size(const struct drm_display_mode *mode,
  931. int maxX, int maxY)
  932. {
  933. if (maxX > 0 && mode->hdisplay > maxX)
  934. return MODE_VIRTUAL_X;
  935. if (maxY > 0 && mode->vdisplay > maxY)
  936. return MODE_VIRTUAL_Y;
  937. return MODE_OK;
  938. }
  939. EXPORT_SYMBOL(drm_mode_validate_size);
  940. #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
  941. static const char * const drm_mode_status_names[] = {
  942. MODE_STATUS(OK),
  943. MODE_STATUS(HSYNC),
  944. MODE_STATUS(VSYNC),
  945. MODE_STATUS(H_ILLEGAL),
  946. MODE_STATUS(V_ILLEGAL),
  947. MODE_STATUS(BAD_WIDTH),
  948. MODE_STATUS(NOMODE),
  949. MODE_STATUS(NO_INTERLACE),
  950. MODE_STATUS(NO_DBLESCAN),
  951. MODE_STATUS(NO_VSCAN),
  952. MODE_STATUS(MEM),
  953. MODE_STATUS(VIRTUAL_X),
  954. MODE_STATUS(VIRTUAL_Y),
  955. MODE_STATUS(MEM_VIRT),
  956. MODE_STATUS(NOCLOCK),
  957. MODE_STATUS(CLOCK_HIGH),
  958. MODE_STATUS(CLOCK_LOW),
  959. MODE_STATUS(CLOCK_RANGE),
  960. MODE_STATUS(BAD_HVALUE),
  961. MODE_STATUS(BAD_VVALUE),
  962. MODE_STATUS(BAD_VSCAN),
  963. MODE_STATUS(HSYNC_NARROW),
  964. MODE_STATUS(HSYNC_WIDE),
  965. MODE_STATUS(HBLANK_NARROW),
  966. MODE_STATUS(HBLANK_WIDE),
  967. MODE_STATUS(VSYNC_NARROW),
  968. MODE_STATUS(VSYNC_WIDE),
  969. MODE_STATUS(VBLANK_NARROW),
  970. MODE_STATUS(VBLANK_WIDE),
  971. MODE_STATUS(PANEL),
  972. MODE_STATUS(INTERLACE_WIDTH),
  973. MODE_STATUS(ONE_WIDTH),
  974. MODE_STATUS(ONE_HEIGHT),
  975. MODE_STATUS(ONE_SIZE),
  976. MODE_STATUS(NO_REDUCED),
  977. MODE_STATUS(NO_STEREO),
  978. MODE_STATUS(UNVERIFIED),
  979. MODE_STATUS(BAD),
  980. MODE_STATUS(ERROR),
  981. };
  982. #undef MODE_STATUS
  983. static const char *drm_get_mode_status_name(enum drm_mode_status status)
  984. {
  985. int index = status + 3;
  986. if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
  987. return "";
  988. return drm_mode_status_names[index];
  989. }
  990. /**
  991. * drm_mode_prune_invalid - remove invalid modes from mode list
  992. * @dev: DRM device
  993. * @mode_list: list of modes to check
  994. * @verbose: be verbose about it
  995. *
  996. * This helper function can be used to prune a display mode list after
  997. * validation has been completed. All modes who's status is not MODE_OK will be
  998. * removed from the list, and if @verbose the status code and mode name is also
  999. * printed to dmesg.
  1000. */
  1001. void drm_mode_prune_invalid(struct drm_device *dev,
  1002. struct list_head *mode_list, bool verbose)
  1003. {
  1004. struct drm_display_mode *mode, *t;
  1005. list_for_each_entry_safe(mode, t, mode_list, head) {
  1006. if (mode->status != MODE_OK) {
  1007. list_del(&mode->head);
  1008. if (verbose) {
  1009. drm_mode_debug_printmodeline(mode);
  1010. DRM_DEBUG_KMS("Not using %s mode: %s\n",
  1011. mode->name,
  1012. drm_get_mode_status_name(mode->status));
  1013. }
  1014. drm_mode_destroy(dev, mode);
  1015. }
  1016. }
  1017. }
  1018. EXPORT_SYMBOL(drm_mode_prune_invalid);
  1019. /**
  1020. * drm_mode_compare - compare modes for favorability
  1021. * @priv: unused
  1022. * @lh_a: list_head for first mode
  1023. * @lh_b: list_head for second mode
  1024. *
  1025. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  1026. * which is better.
  1027. *
  1028. * Returns:
  1029. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  1030. * positive if @lh_b is better than @lh_a.
  1031. */
  1032. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  1033. {
  1034. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  1035. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  1036. int diff;
  1037. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  1038. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  1039. if (diff)
  1040. return diff;
  1041. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  1042. if (diff)
  1043. return diff;
  1044. diff = b->vrefresh - a->vrefresh;
  1045. if (diff)
  1046. return diff;
  1047. diff = b->clock - a->clock;
  1048. return diff;
  1049. }
  1050. /**
  1051. * drm_mode_sort - sort mode list
  1052. * @mode_list: list of drm_display_mode structures to sort
  1053. *
  1054. * Sort @mode_list by favorability, moving good modes to the head of the list.
  1055. */
  1056. void drm_mode_sort(struct list_head *mode_list)
  1057. {
  1058. list_sort(NULL, mode_list, drm_mode_compare);
  1059. }
  1060. EXPORT_SYMBOL(drm_mode_sort);
  1061. /**
  1062. * drm_mode_connector_list_update - update the mode list for the connector
  1063. * @connector: the connector to update
  1064. * @merge_type_bits: whether to merge or overwrite type bits
  1065. *
  1066. * This moves the modes from the @connector probed_modes list
  1067. * to the actual mode list. It compares the probed mode against the current
  1068. * list and only adds different/new modes.
  1069. *
  1070. * This is just a helper functions doesn't validate any modes itself and also
  1071. * doesn't prune any invalid modes. Callers need to do that themselves.
  1072. */
  1073. void drm_mode_connector_list_update(struct drm_connector *connector,
  1074. bool merge_type_bits)
  1075. {
  1076. struct drm_display_mode *mode;
  1077. struct drm_display_mode *pmode, *pt;
  1078. int found_it;
  1079. WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
  1080. list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
  1081. head) {
  1082. found_it = 0;
  1083. /* go through current modes checking for the new probed mode */
  1084. list_for_each_entry(mode, &connector->modes, head) {
  1085. if (drm_mode_equal(pmode, mode)) {
  1086. found_it = 1;
  1087. /* if equal delete the probed mode */
  1088. mode->status = pmode->status;
  1089. /* Merge type bits together */
  1090. if (merge_type_bits)
  1091. mode->type |= pmode->type;
  1092. else
  1093. mode->type = pmode->type;
  1094. list_del(&pmode->head);
  1095. drm_mode_destroy(connector->dev, pmode);
  1096. break;
  1097. }
  1098. }
  1099. if (!found_it) {
  1100. list_move_tail(&pmode->head, &connector->modes);
  1101. }
  1102. }
  1103. }
  1104. EXPORT_SYMBOL(drm_mode_connector_list_update);
  1105. /**
  1106. * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
  1107. * @mode_option: optional per connector mode option
  1108. * @connector: connector to parse modeline for
  1109. * @mode: preallocated drm_cmdline_mode structure to fill out
  1110. *
  1111. * This parses @mode_option command line modeline for modes and options to
  1112. * configure the connector. If @mode_option is NULL the default command line
  1113. * modeline in fb_mode_option will be parsed instead.
  1114. *
  1115. * This uses the same parameters as the fb modedb.c, except for an extra
  1116. * force-enable, force-enable-digital and force-disable bit at the end:
  1117. *
  1118. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  1119. *
  1120. * The intermediate drm_cmdline_mode structure is required to store additional
  1121. * options from the command line modline like the force-enable/disable flag.
  1122. *
  1123. * Returns:
  1124. * True if a valid modeline has been parsed, false otherwise.
  1125. */
  1126. bool drm_mode_parse_command_line_for_connector(const char *mode_option,
  1127. struct drm_connector *connector,
  1128. struct drm_cmdline_mode *mode)
  1129. {
  1130. const char *name;
  1131. unsigned int namelen;
  1132. bool res_specified = false, bpp_specified = false, refresh_specified = false;
  1133. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  1134. bool yres_specified = false, cvt = false, rb = false;
  1135. bool interlace = false, margins = false, was_digit = false;
  1136. int i, err;
  1137. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  1138. #ifdef CONFIG_FB
  1139. if (!mode_option)
  1140. mode_option = fb_mode_option;
  1141. #endif
  1142. if (!mode_option) {
  1143. mode->specified = false;
  1144. return false;
  1145. }
  1146. name = mode_option;
  1147. namelen = strlen(name);
  1148. for (i = namelen-1; i >= 0; i--) {
  1149. switch (name[i]) {
  1150. case '@':
  1151. if (!refresh_specified && !bpp_specified &&
  1152. !yres_specified && !cvt && !rb && was_digit) {
  1153. err = kstrtouint(&name[i + 1], 10, &refresh);
  1154. if (err)
  1155. return false;
  1156. refresh_specified = true;
  1157. was_digit = false;
  1158. } else
  1159. goto done;
  1160. break;
  1161. case '-':
  1162. if (!bpp_specified && !yres_specified && !cvt &&
  1163. !rb && was_digit) {
  1164. err = kstrtouint(&name[i + 1], 10, &bpp);
  1165. if (err)
  1166. return false;
  1167. bpp_specified = true;
  1168. was_digit = false;
  1169. } else
  1170. goto done;
  1171. break;
  1172. case 'x':
  1173. if (!yres_specified && was_digit) {
  1174. err = kstrtouint(&name[i + 1], 10, &yres);
  1175. if (err)
  1176. return false;
  1177. yres_specified = true;
  1178. was_digit = false;
  1179. } else
  1180. goto done;
  1181. break;
  1182. case '0' ... '9':
  1183. was_digit = true;
  1184. break;
  1185. case 'M':
  1186. if (yres_specified || cvt || was_digit)
  1187. goto done;
  1188. cvt = true;
  1189. break;
  1190. case 'R':
  1191. if (yres_specified || cvt || rb || was_digit)
  1192. goto done;
  1193. rb = true;
  1194. break;
  1195. case 'm':
  1196. if (cvt || yres_specified || was_digit)
  1197. goto done;
  1198. margins = true;
  1199. break;
  1200. case 'i':
  1201. if (cvt || yres_specified || was_digit)
  1202. goto done;
  1203. interlace = true;
  1204. break;
  1205. case 'e':
  1206. if (yres_specified || bpp_specified || refresh_specified ||
  1207. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1208. goto done;
  1209. force = DRM_FORCE_ON;
  1210. break;
  1211. case 'D':
  1212. if (yres_specified || bpp_specified || refresh_specified ||
  1213. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1214. goto done;
  1215. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  1216. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  1217. force = DRM_FORCE_ON;
  1218. else
  1219. force = DRM_FORCE_ON_DIGITAL;
  1220. break;
  1221. case 'd':
  1222. if (yres_specified || bpp_specified || refresh_specified ||
  1223. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1224. goto done;
  1225. force = DRM_FORCE_OFF;
  1226. break;
  1227. default:
  1228. goto done;
  1229. }
  1230. }
  1231. if (i < 0 && yres_specified) {
  1232. char *ch;
  1233. xres = simple_strtol(name, &ch, 10);
  1234. if ((ch != NULL) && (*ch == 'x'))
  1235. res_specified = true;
  1236. else
  1237. i = ch - name;
  1238. } else if (!yres_specified && was_digit) {
  1239. /* catch mode that begins with digits but has no 'x' */
  1240. i = 0;
  1241. }
  1242. done:
  1243. if (i >= 0) {
  1244. printk(KERN_WARNING
  1245. "parse error at position %i in video mode '%s'\n",
  1246. i, name);
  1247. mode->specified = false;
  1248. return false;
  1249. }
  1250. if (res_specified) {
  1251. mode->specified = true;
  1252. mode->xres = xres;
  1253. mode->yres = yres;
  1254. }
  1255. if (refresh_specified) {
  1256. mode->refresh_specified = true;
  1257. mode->refresh = refresh;
  1258. }
  1259. if (bpp_specified) {
  1260. mode->bpp_specified = true;
  1261. mode->bpp = bpp;
  1262. }
  1263. mode->rb = rb;
  1264. mode->cvt = cvt;
  1265. mode->interlace = interlace;
  1266. mode->margins = margins;
  1267. mode->force = force;
  1268. return true;
  1269. }
  1270. EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
  1271. /**
  1272. * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
  1273. * @dev: DRM device to create the new mode for
  1274. * @cmd: input command line modeline
  1275. *
  1276. * Returns:
  1277. * Pointer to converted mode on success, NULL on error.
  1278. */
  1279. struct drm_display_mode *
  1280. drm_mode_create_from_cmdline_mode(struct drm_device *dev,
  1281. struct drm_cmdline_mode *cmd)
  1282. {
  1283. struct drm_display_mode *mode;
  1284. if (cmd->cvt)
  1285. mode = drm_cvt_mode(dev,
  1286. cmd->xres, cmd->yres,
  1287. cmd->refresh_specified ? cmd->refresh : 60,
  1288. cmd->rb, cmd->interlace,
  1289. cmd->margins);
  1290. else
  1291. mode = drm_gtf_mode(dev,
  1292. cmd->xres, cmd->yres,
  1293. cmd->refresh_specified ? cmd->refresh : 60,
  1294. cmd->interlace,
  1295. cmd->margins);
  1296. if (!mode)
  1297. return NULL;
  1298. mode->type |= DRM_MODE_TYPE_USERDEF;
  1299. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1300. return mode;
  1301. }
  1302. EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
  1303. /**
  1304. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1305. * @out: drm_mode_modeinfo struct to return to the user
  1306. * @in: drm_display_mode to use
  1307. *
  1308. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1309. * the user.
  1310. */
  1311. void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
  1312. const struct drm_display_mode *in)
  1313. {
  1314. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1315. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1316. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1317. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1318. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1319. "timing values too large for mode info\n");
  1320. out->clock = in->clock;
  1321. out->hdisplay = in->hdisplay;
  1322. out->hsync_start = in->hsync_start;
  1323. out->hsync_end = in->hsync_end;
  1324. out->htotal = in->htotal;
  1325. out->hskew = in->hskew;
  1326. out->vdisplay = in->vdisplay;
  1327. out->vsync_start = in->vsync_start;
  1328. out->vsync_end = in->vsync_end;
  1329. out->vtotal = in->vtotal;
  1330. out->vscan = in->vscan;
  1331. out->vrefresh = in->vrefresh;
  1332. out->flags = in->flags;
  1333. out->type = in->type;
  1334. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1335. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1336. }
  1337. /**
  1338. * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
  1339. * @out: drm_display_mode to return to the user
  1340. * @in: drm_mode_modeinfo to use
  1341. *
  1342. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1343. * the caller.
  1344. *
  1345. * Returns:
  1346. * Zero on success, negative errno on failure.
  1347. */
  1348. int drm_mode_convert_umode(struct drm_display_mode *out,
  1349. const struct drm_mode_modeinfo *in)
  1350. {
  1351. int ret = -EINVAL;
  1352. if (in->clock > INT_MAX || in->vrefresh > INT_MAX) {
  1353. ret = -ERANGE;
  1354. goto out;
  1355. }
  1356. if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1357. goto out;
  1358. out->clock = in->clock;
  1359. out->hdisplay = in->hdisplay;
  1360. out->hsync_start = in->hsync_start;
  1361. out->hsync_end = in->hsync_end;
  1362. out->htotal = in->htotal;
  1363. out->hskew = in->hskew;
  1364. out->vdisplay = in->vdisplay;
  1365. out->vsync_start = in->vsync_start;
  1366. out->vsync_end = in->vsync_end;
  1367. out->vtotal = in->vtotal;
  1368. out->vscan = in->vscan;
  1369. out->vrefresh = in->vrefresh;
  1370. out->flags = in->flags;
  1371. out->type = in->type;
  1372. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1373. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1374. out->status = drm_mode_validate_basic(out);
  1375. if (out->status != MODE_OK)
  1376. goto out;
  1377. ret = 0;
  1378. out:
  1379. return ret;
  1380. }