drm_modes.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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 "drmP.h"
  35. #include "drm.h"
  36. #include "drm_crtc.h"
  37. /**
  38. * drm_mode_debug_printmodeline - debug print a mode
  39. * @dev: DRM device
  40. * @mode: mode to print
  41. *
  42. * LOCKING:
  43. * None.
  44. *
  45. * Describe @mode using DRM_DEBUG.
  46. */
  47. void drm_mode_debug_printmodeline(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_cvt_mode -create a modeline based on CVT algorithm
  60. * @dev: DRM device
  61. * @hdisplay: hdisplay size
  62. * @vdisplay: vdisplay size
  63. * @vrefresh : vrefresh rate
  64. * @reduced : Whether the GTF calculation is simplified
  65. * @interlaced:Whether the interlace is supported
  66. *
  67. * LOCKING:
  68. * none.
  69. *
  70. * return the modeline based on CVT algorithm
  71. *
  72. * This function is called to generate the modeline based on CVT algorithm
  73. * according to the hdisplay, vdisplay, vrefresh.
  74. * It is based from the VESA(TM) Coordinated Video Timing Generator by
  75. * Graham Loveridge April 9, 2003 available at
  76. * http://www.vesa.org/public/CVT/CVTd6r1.xls
  77. *
  78. * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
  79. * What I have done is to translate it by using integer calculation.
  80. */
  81. #define HV_FACTOR 1000
  82. struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
  83. int vdisplay, int vrefresh,
  84. bool reduced, bool interlaced, bool margins)
  85. {
  86. /* 1) top/bottom margin size (% of height) - default: 1.8, */
  87. #define CVT_MARGIN_PERCENTAGE 18
  88. /* 2) character cell horizontal granularity (pixels) - default 8 */
  89. #define CVT_H_GRANULARITY 8
  90. /* 3) Minimum vertical porch (lines) - default 3 */
  91. #define CVT_MIN_V_PORCH 3
  92. /* 4) Minimum number of vertical back porch lines - default 6 */
  93. #define CVT_MIN_V_BPORCH 6
  94. /* Pixel Clock step (kHz) */
  95. #define CVT_CLOCK_STEP 250
  96. struct drm_display_mode *drm_mode;
  97. unsigned int vfieldrate, hperiod;
  98. int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
  99. int interlace;
  100. /* allocate the drm_display_mode structure. If failure, we will
  101. * return directly
  102. */
  103. drm_mode = drm_mode_create(dev);
  104. if (!drm_mode)
  105. return NULL;
  106. /* the CVT default refresh rate is 60Hz */
  107. if (!vrefresh)
  108. vrefresh = 60;
  109. /* the required field fresh rate */
  110. if (interlaced)
  111. vfieldrate = vrefresh * 2;
  112. else
  113. vfieldrate = vrefresh;
  114. /* horizontal pixels */
  115. hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
  116. /* determine the left&right borders */
  117. hmargin = 0;
  118. if (margins) {
  119. hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  120. hmargin -= hmargin % CVT_H_GRANULARITY;
  121. }
  122. /* find the total active pixels */
  123. drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
  124. /* find the number of lines per field */
  125. if (interlaced)
  126. vdisplay_rnd = vdisplay / 2;
  127. else
  128. vdisplay_rnd = vdisplay;
  129. /* find the top & bottom borders */
  130. vmargin = 0;
  131. if (margins)
  132. vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  133. drm_mode->vdisplay = vdisplay + 2 * vmargin;
  134. /* Interlaced */
  135. if (interlaced)
  136. interlace = 1;
  137. else
  138. interlace = 0;
  139. /* Determine VSync Width from aspect ratio */
  140. if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
  141. vsync = 4;
  142. else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
  143. vsync = 5;
  144. else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
  145. vsync = 6;
  146. else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
  147. vsync = 7;
  148. else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
  149. vsync = 7;
  150. else /* custom */
  151. vsync = 10;
  152. if (!reduced) {
  153. /* simplify the GTF calculation */
  154. /* 4) Minimum time of vertical sync + back porch interval (µs)
  155. * default 550.0
  156. */
  157. int tmp1, tmp2;
  158. #define CVT_MIN_VSYNC_BP 550
  159. /* 3) Nominal HSync width (% of line period) - default 8 */
  160. #define CVT_HSYNC_PERCENTAGE 8
  161. unsigned int hblank_percentage;
  162. int vsyncandback_porch, vback_porch, hblank;
  163. /* estimated the horizontal period */
  164. tmp1 = HV_FACTOR * 1000000 -
  165. CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
  166. tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
  167. interlace;
  168. hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
  169. tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
  170. /* 9. Find number of lines in sync + backporch */
  171. if (tmp1 < (vsync + CVT_MIN_V_PORCH))
  172. vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
  173. else
  174. vsyncandback_porch = tmp1;
  175. /* 10. Find number of lines in back porch */
  176. vback_porch = vsyncandback_porch - vsync;
  177. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
  178. vsyncandback_porch + CVT_MIN_V_PORCH;
  179. /* 5) Definition of Horizontal blanking time limitation */
  180. /* Gradient (%/kHz) - default 600 */
  181. #define CVT_M_FACTOR 600
  182. /* Offset (%) - default 40 */
  183. #define CVT_C_FACTOR 40
  184. /* Blanking time scaling factor - default 128 */
  185. #define CVT_K_FACTOR 128
  186. /* Scaling factor weighting - default 20 */
  187. #define CVT_J_FACTOR 20
  188. #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
  189. #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
  190. CVT_J_FACTOR)
  191. /* 12. Find ideal blanking duty cycle from formula */
  192. hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
  193. hperiod / 1000;
  194. /* 13. Blanking time */
  195. if (hblank_percentage < 20 * HV_FACTOR)
  196. hblank_percentage = 20 * HV_FACTOR;
  197. hblank = drm_mode->hdisplay * hblank_percentage /
  198. (100 * HV_FACTOR - hblank_percentage);
  199. hblank -= hblank % (2 * CVT_H_GRANULARITY);
  200. /* 14. find the total pixes per line */
  201. drm_mode->htotal = drm_mode->hdisplay + hblank;
  202. drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
  203. drm_mode->hsync_start = drm_mode->hsync_end -
  204. (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
  205. drm_mode->hsync_start += CVT_H_GRANULARITY -
  206. drm_mode->hsync_start % CVT_H_GRANULARITY;
  207. /* fill the Vsync values */
  208. drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
  209. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  210. } else {
  211. /* Reduced blanking */
  212. /* Minimum vertical blanking interval time (µs)- default 460 */
  213. #define CVT_RB_MIN_VBLANK 460
  214. /* Fixed number of clocks for horizontal sync */
  215. #define CVT_RB_H_SYNC 32
  216. /* Fixed number of clocks for horizontal blanking */
  217. #define CVT_RB_H_BLANK 160
  218. /* Fixed number of lines for vertical front porch - default 3*/
  219. #define CVT_RB_VFPORCH 3
  220. int vbilines;
  221. int tmp1, tmp2;
  222. /* 8. Estimate Horizontal period. */
  223. tmp1 = HV_FACTOR * 1000000 -
  224. CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
  225. tmp2 = vdisplay_rnd + 2 * vmargin;
  226. hperiod = tmp1 / (tmp2 * vfieldrate);
  227. /* 9. Find number of lines in vertical blanking */
  228. vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
  229. /* 10. Check if vertical blanking is sufficient */
  230. if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
  231. vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
  232. /* 11. Find total number of lines in vertical field */
  233. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
  234. /* 12. Find total number of pixels in a line */
  235. drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
  236. /* Fill in HSync values */
  237. drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
  238. drm_mode->hsync_start = drm_mode->hsync_end = CVT_RB_H_SYNC;
  239. }
  240. /* 15/13. Find pixel clock frequency (kHz for xf86) */
  241. drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
  242. drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
  243. /* 18/16. Find actual vertical frame frequency */
  244. /* ignore - just set the mode flag for interlaced */
  245. if (interlaced) {
  246. drm_mode->vtotal *= 2;
  247. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  248. }
  249. /* Fill the mode line name */
  250. drm_mode_set_name(drm_mode);
  251. if (reduced)
  252. drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
  253. DRM_MODE_FLAG_NVSYNC);
  254. else
  255. drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
  256. DRM_MODE_FLAG_NHSYNC);
  257. return drm_mode;
  258. }
  259. EXPORT_SYMBOL(drm_cvt_mode);
  260. /**
  261. * drm_gtf_mode_complex - create the modeline based on full GTF algorithm
  262. *
  263. * @dev :drm device
  264. * @hdisplay :hdisplay size
  265. * @vdisplay :vdisplay size
  266. * @vrefresh :vrefresh rate.
  267. * @interlaced :whether the interlace is supported
  268. * @margins :desired margin size
  269. * @GTF_[MCKJ] :extended GTF formula parameters
  270. *
  271. * LOCKING.
  272. * none.
  273. *
  274. * return the modeline based on full GTF algorithm.
  275. *
  276. * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
  277. * in here multiplied by two. For a C of 40, pass in 80.
  278. */
  279. struct drm_display_mode *
  280. drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
  281. int vrefresh, bool interlaced, int margins,
  282. int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
  283. { /* 1) top/bottom margin size (% of height) - default: 1.8, */
  284. #define GTF_MARGIN_PERCENTAGE 18
  285. /* 2) character cell horizontal granularity (pixels) - default 8 */
  286. #define GTF_CELL_GRAN 8
  287. /* 3) Minimum vertical porch (lines) - default 3 */
  288. #define GTF_MIN_V_PORCH 1
  289. /* width of vsync in lines */
  290. #define V_SYNC_RQD 3
  291. /* width of hsync as % of total line */
  292. #define H_SYNC_PERCENT 8
  293. /* min time of vsync + back porch (microsec) */
  294. #define MIN_VSYNC_PLUS_BP 550
  295. /* C' and M' are part of the Blanking Duty Cycle computation */
  296. #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
  297. #define GTF_M_PRIME (GTF_K * GTF_M / 256)
  298. struct drm_display_mode *drm_mode;
  299. unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
  300. int top_margin, bottom_margin;
  301. int interlace;
  302. unsigned int hfreq_est;
  303. int vsync_plus_bp, vback_porch;
  304. unsigned int vtotal_lines, vfieldrate_est, hperiod;
  305. unsigned int vfield_rate, vframe_rate;
  306. int left_margin, right_margin;
  307. unsigned int total_active_pixels, ideal_duty_cycle;
  308. unsigned int hblank, total_pixels, pixel_freq;
  309. int hsync, hfront_porch, vodd_front_porch_lines;
  310. unsigned int tmp1, tmp2;
  311. drm_mode = drm_mode_create(dev);
  312. if (!drm_mode)
  313. return NULL;
  314. /* 1. In order to give correct results, the number of horizontal
  315. * pixels requested is first processed to ensure that it is divisible
  316. * by the character size, by rounding it to the nearest character
  317. * cell boundary:
  318. */
  319. hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  320. hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
  321. /* 2. If interlace is requested, the number of vertical lines assumed
  322. * by the calculation must be halved, as the computation calculates
  323. * the number of vertical lines per field.
  324. */
  325. if (interlaced)
  326. vdisplay_rnd = vdisplay / 2;
  327. else
  328. vdisplay_rnd = vdisplay;
  329. /* 3. Find the frame rate required: */
  330. if (interlaced)
  331. vfieldrate_rqd = vrefresh * 2;
  332. else
  333. vfieldrate_rqd = vrefresh;
  334. /* 4. Find number of lines in Top margin: */
  335. top_margin = 0;
  336. if (margins)
  337. top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  338. 1000;
  339. /* 5. Find number of lines in bottom margin: */
  340. bottom_margin = top_margin;
  341. /* 6. If interlace is required, then set variable interlace: */
  342. if (interlaced)
  343. interlace = 1;
  344. else
  345. interlace = 0;
  346. /* 7. Estimate the Horizontal frequency */
  347. {
  348. tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
  349. tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
  350. 2 + interlace;
  351. hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
  352. }
  353. /* 8. Find the number of lines in V sync + back porch */
  354. /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
  355. vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
  356. vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
  357. /* 9. Find the number of lines in V back porch alone: */
  358. vback_porch = vsync_plus_bp - V_SYNC_RQD;
  359. /* 10. Find the total number of lines in Vertical field period: */
  360. vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
  361. vsync_plus_bp + GTF_MIN_V_PORCH;
  362. /* 11. Estimate the Vertical field frequency: */
  363. vfieldrate_est = hfreq_est / vtotal_lines;
  364. /* 12. Find the actual horizontal period: */
  365. hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
  366. /* 13. Find the actual Vertical field frequency: */
  367. vfield_rate = hfreq_est / vtotal_lines;
  368. /* 14. Find the Vertical frame frequency: */
  369. if (interlaced)
  370. vframe_rate = vfield_rate / 2;
  371. else
  372. vframe_rate = vfield_rate;
  373. /* 15. Find number of pixels in left margin: */
  374. if (margins)
  375. left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  376. 1000;
  377. else
  378. left_margin = 0;
  379. /* 16.Find number of pixels in right margin: */
  380. right_margin = left_margin;
  381. /* 17.Find total number of active pixels in image and left and right */
  382. total_active_pixels = hdisplay_rnd + left_margin + right_margin;
  383. /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
  384. ideal_duty_cycle = GTF_C_PRIME * 1000 -
  385. (GTF_M_PRIME * 1000000 / hfreq_est);
  386. /* 19.Find the number of pixels in the blanking time to the nearest
  387. * double character cell: */
  388. hblank = total_active_pixels * ideal_duty_cycle /
  389. (100000 - ideal_duty_cycle);
  390. hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
  391. hblank = hblank * 2 * GTF_CELL_GRAN;
  392. /* 20.Find total number of pixels: */
  393. total_pixels = total_active_pixels + hblank;
  394. /* 21.Find pixel clock frequency: */
  395. pixel_freq = total_pixels * hfreq_est / 1000;
  396. /* Stage 1 computations are now complete; I should really pass
  397. * the results to another function and do the Stage 2 computations,
  398. * but I only need a few more values so I'll just append the
  399. * computations here for now */
  400. /* 17. Find the number of pixels in the horizontal sync period: */
  401. hsync = H_SYNC_PERCENT * total_pixels / 100;
  402. hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  403. hsync = hsync * GTF_CELL_GRAN;
  404. /* 18. Find the number of pixels in horizontal front porch period */
  405. hfront_porch = hblank / 2 - hsync;
  406. /* 36. Find the number of lines in the odd front porch period: */
  407. vodd_front_porch_lines = GTF_MIN_V_PORCH ;
  408. /* finally, pack the results in the mode struct */
  409. drm_mode->hdisplay = hdisplay_rnd;
  410. drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
  411. drm_mode->hsync_end = drm_mode->hsync_start + hsync;
  412. drm_mode->htotal = total_pixels;
  413. drm_mode->vdisplay = vdisplay_rnd;
  414. drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
  415. drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
  416. drm_mode->vtotal = vtotal_lines;
  417. drm_mode->clock = pixel_freq;
  418. if (interlaced) {
  419. drm_mode->vtotal *= 2;
  420. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  421. }
  422. drm_mode_set_name(drm_mode);
  423. if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
  424. drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
  425. else
  426. drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
  427. return drm_mode;
  428. }
  429. EXPORT_SYMBOL(drm_gtf_mode_complex);
  430. /**
  431. * drm_gtf_mode - create the modeline based on GTF algorithm
  432. *
  433. * @dev :drm device
  434. * @hdisplay :hdisplay size
  435. * @vdisplay :vdisplay size
  436. * @vrefresh :vrefresh rate.
  437. * @interlaced :whether the interlace is supported
  438. * @margins :whether the margin is supported
  439. *
  440. * LOCKING.
  441. * none.
  442. *
  443. * return the modeline based on GTF algorithm
  444. *
  445. * This function is to create the modeline based on the GTF algorithm.
  446. * Generalized Timing Formula is derived from:
  447. * GTF Spreadsheet by Andy Morrish (1/5/97)
  448. * available at http://www.vesa.org
  449. *
  450. * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
  451. * What I have done is to translate it by using integer calculation.
  452. * I also refer to the function of fb_get_mode in the file of
  453. * drivers/video/fbmon.c
  454. *
  455. * Standard GTF parameters:
  456. * M = 600
  457. * C = 40
  458. * K = 128
  459. * J = 20
  460. */
  461. struct drm_display_mode *
  462. drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
  463. bool lace, int margins)
  464. {
  465. return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh, lace,
  466. margins, 600, 40 * 2, 128, 20 * 2);
  467. }
  468. EXPORT_SYMBOL(drm_gtf_mode);
  469. /**
  470. * drm_mode_set_name - set the name on a mode
  471. * @mode: name will be set in this mode
  472. *
  473. * LOCKING:
  474. * None.
  475. *
  476. * Set the name of @mode to a standard format.
  477. */
  478. void drm_mode_set_name(struct drm_display_mode *mode)
  479. {
  480. bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  481. snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
  482. mode->hdisplay, mode->vdisplay,
  483. interlaced ? "i" : "");
  484. }
  485. EXPORT_SYMBOL(drm_mode_set_name);
  486. /**
  487. * drm_mode_list_concat - move modes from one list to another
  488. * @head: source list
  489. * @new: dst list
  490. *
  491. * LOCKING:
  492. * Caller must ensure both lists are locked.
  493. *
  494. * Move all the modes from @head to @new.
  495. */
  496. void drm_mode_list_concat(struct list_head *head, struct list_head *new)
  497. {
  498. struct list_head *entry, *tmp;
  499. list_for_each_safe(entry, tmp, head) {
  500. list_move_tail(entry, new);
  501. }
  502. }
  503. EXPORT_SYMBOL(drm_mode_list_concat);
  504. /**
  505. * drm_mode_width - get the width of a mode
  506. * @mode: mode
  507. *
  508. * LOCKING:
  509. * None.
  510. *
  511. * Return @mode's width (hdisplay) value.
  512. *
  513. * FIXME: is this needed?
  514. *
  515. * RETURNS:
  516. * @mode->hdisplay
  517. */
  518. int drm_mode_width(struct drm_display_mode *mode)
  519. {
  520. return mode->hdisplay;
  521. }
  522. EXPORT_SYMBOL(drm_mode_width);
  523. /**
  524. * drm_mode_height - get the height of a mode
  525. * @mode: mode
  526. *
  527. * LOCKING:
  528. * None.
  529. *
  530. * Return @mode's height (vdisplay) value.
  531. *
  532. * FIXME: is this needed?
  533. *
  534. * RETURNS:
  535. * @mode->vdisplay
  536. */
  537. int drm_mode_height(struct drm_display_mode *mode)
  538. {
  539. return mode->vdisplay;
  540. }
  541. EXPORT_SYMBOL(drm_mode_height);
  542. /** drm_mode_hsync - get the hsync of a mode
  543. * @mode: mode
  544. *
  545. * LOCKING:
  546. * None.
  547. *
  548. * Return @modes's hsync rate in kHz, rounded to the nearest int.
  549. */
  550. int drm_mode_hsync(struct drm_display_mode *mode)
  551. {
  552. unsigned int calc_val;
  553. if (mode->hsync)
  554. return mode->hsync;
  555. if (mode->htotal < 0)
  556. return 0;
  557. calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
  558. calc_val += 500; /* round to 1000Hz */
  559. calc_val /= 1000; /* truncate to kHz */
  560. return calc_val;
  561. }
  562. EXPORT_SYMBOL(drm_mode_hsync);
  563. /**
  564. * drm_mode_vrefresh - get the vrefresh of a mode
  565. * @mode: mode
  566. *
  567. * LOCKING:
  568. * None.
  569. *
  570. * Return @mode's vrefresh rate in Hz or calculate it if necessary.
  571. *
  572. * FIXME: why is this needed? shouldn't vrefresh be set already?
  573. *
  574. * RETURNS:
  575. * Vertical refresh rate. It will be the result of actual value plus 0.5.
  576. * If it is 70.288, it will return 70Hz.
  577. * If it is 59.6, it will return 60Hz.
  578. */
  579. int drm_mode_vrefresh(struct drm_display_mode *mode)
  580. {
  581. int refresh = 0;
  582. unsigned int calc_val;
  583. if (mode->vrefresh > 0)
  584. refresh = mode->vrefresh;
  585. else if (mode->htotal > 0 && mode->vtotal > 0) {
  586. int vtotal;
  587. vtotal = mode->vtotal;
  588. /* work out vrefresh the value will be x1000 */
  589. calc_val = (mode->clock * 1000);
  590. calc_val /= mode->htotal;
  591. refresh = (calc_val + vtotal / 2) / vtotal;
  592. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  593. refresh *= 2;
  594. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  595. refresh /= 2;
  596. if (mode->vscan > 1)
  597. refresh /= mode->vscan;
  598. }
  599. return refresh;
  600. }
  601. EXPORT_SYMBOL(drm_mode_vrefresh);
  602. /**
  603. * drm_mode_set_crtcinfo - set CRTC modesetting parameters
  604. * @p: mode
  605. * @adjust_flags: unused? (FIXME)
  606. *
  607. * LOCKING:
  608. * None.
  609. *
  610. * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
  611. */
  612. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  613. {
  614. if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
  615. return;
  616. p->crtc_hdisplay = p->hdisplay;
  617. p->crtc_hsync_start = p->hsync_start;
  618. p->crtc_hsync_end = p->hsync_end;
  619. p->crtc_htotal = p->htotal;
  620. p->crtc_hskew = p->hskew;
  621. p->crtc_vdisplay = p->vdisplay;
  622. p->crtc_vsync_start = p->vsync_start;
  623. p->crtc_vsync_end = p->vsync_end;
  624. p->crtc_vtotal = p->vtotal;
  625. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  626. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  627. p->crtc_vdisplay /= 2;
  628. p->crtc_vsync_start /= 2;
  629. p->crtc_vsync_end /= 2;
  630. p->crtc_vtotal /= 2;
  631. }
  632. p->crtc_vtotal |= 1;
  633. }
  634. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  635. p->crtc_vdisplay *= 2;
  636. p->crtc_vsync_start *= 2;
  637. p->crtc_vsync_end *= 2;
  638. p->crtc_vtotal *= 2;
  639. }
  640. if (p->vscan > 1) {
  641. p->crtc_vdisplay *= p->vscan;
  642. p->crtc_vsync_start *= p->vscan;
  643. p->crtc_vsync_end *= p->vscan;
  644. p->crtc_vtotal *= p->vscan;
  645. }
  646. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  647. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  648. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  649. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  650. p->crtc_hadjusted = false;
  651. p->crtc_vadjusted = false;
  652. }
  653. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  654. /**
  655. * drm_mode_duplicate - allocate and duplicate an existing mode
  656. * @m: mode to duplicate
  657. *
  658. * LOCKING:
  659. * None.
  660. *
  661. * Just allocate a new mode, copy the existing mode into it, and return
  662. * a pointer to it. Used to create new instances of established modes.
  663. */
  664. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  665. struct drm_display_mode *mode)
  666. {
  667. struct drm_display_mode *nmode;
  668. int new_id;
  669. nmode = drm_mode_create(dev);
  670. if (!nmode)
  671. return NULL;
  672. new_id = nmode->base.id;
  673. *nmode = *mode;
  674. nmode->base.id = new_id;
  675. INIT_LIST_HEAD(&nmode->head);
  676. return nmode;
  677. }
  678. EXPORT_SYMBOL(drm_mode_duplicate);
  679. /**
  680. * drm_mode_equal - test modes for equality
  681. * @mode1: first mode
  682. * @mode2: second mode
  683. *
  684. * LOCKING:
  685. * None.
  686. *
  687. * Check to see if @mode1 and @mode2 are equivalent.
  688. *
  689. * RETURNS:
  690. * True if the modes are equal, false otherwise.
  691. */
  692. bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
  693. {
  694. /* do clock check convert to PICOS so fb modes get matched
  695. * the same */
  696. if (mode1->clock && mode2->clock) {
  697. if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
  698. return false;
  699. } else if (mode1->clock != mode2->clock)
  700. return false;
  701. if (mode1->hdisplay == mode2->hdisplay &&
  702. mode1->hsync_start == mode2->hsync_start &&
  703. mode1->hsync_end == mode2->hsync_end &&
  704. mode1->htotal == mode2->htotal &&
  705. mode1->hskew == mode2->hskew &&
  706. mode1->vdisplay == mode2->vdisplay &&
  707. mode1->vsync_start == mode2->vsync_start &&
  708. mode1->vsync_end == mode2->vsync_end &&
  709. mode1->vtotal == mode2->vtotal &&
  710. mode1->vscan == mode2->vscan &&
  711. mode1->flags == mode2->flags)
  712. return true;
  713. return false;
  714. }
  715. EXPORT_SYMBOL(drm_mode_equal);
  716. /**
  717. * drm_mode_validate_size - make sure modes adhere to size constraints
  718. * @dev: DRM device
  719. * @mode_list: list of modes to check
  720. * @maxX: maximum width
  721. * @maxY: maximum height
  722. * @maxPitch: max pitch
  723. *
  724. * LOCKING:
  725. * Caller must hold a lock protecting @mode_list.
  726. *
  727. * The DRM device (@dev) has size and pitch limits. Here we validate the
  728. * modes we probed for @dev against those limits and set their status as
  729. * necessary.
  730. */
  731. void drm_mode_validate_size(struct drm_device *dev,
  732. struct list_head *mode_list,
  733. int maxX, int maxY, int maxPitch)
  734. {
  735. struct drm_display_mode *mode;
  736. list_for_each_entry(mode, mode_list, head) {
  737. if (maxPitch > 0 && mode->hdisplay > maxPitch)
  738. mode->status = MODE_BAD_WIDTH;
  739. if (maxX > 0 && mode->hdisplay > maxX)
  740. mode->status = MODE_VIRTUAL_X;
  741. if (maxY > 0 && mode->vdisplay > maxY)
  742. mode->status = MODE_VIRTUAL_Y;
  743. }
  744. }
  745. EXPORT_SYMBOL(drm_mode_validate_size);
  746. /**
  747. * drm_mode_validate_clocks - validate modes against clock limits
  748. * @dev: DRM device
  749. * @mode_list: list of modes to check
  750. * @min: minimum clock rate array
  751. * @max: maximum clock rate array
  752. * @n_ranges: number of clock ranges (size of arrays)
  753. *
  754. * LOCKING:
  755. * Caller must hold a lock protecting @mode_list.
  756. *
  757. * Some code may need to check a mode list against the clock limits of the
  758. * device in question. This function walks the mode list, testing to make
  759. * sure each mode falls within a given range (defined by @min and @max
  760. * arrays) and sets @mode->status as needed.
  761. */
  762. void drm_mode_validate_clocks(struct drm_device *dev,
  763. struct list_head *mode_list,
  764. int *min, int *max, int n_ranges)
  765. {
  766. struct drm_display_mode *mode;
  767. int i;
  768. list_for_each_entry(mode, mode_list, head) {
  769. bool good = false;
  770. for (i = 0; i < n_ranges; i++) {
  771. if (mode->clock >= min[i] && mode->clock <= max[i]) {
  772. good = true;
  773. break;
  774. }
  775. }
  776. if (!good)
  777. mode->status = MODE_CLOCK_RANGE;
  778. }
  779. }
  780. EXPORT_SYMBOL(drm_mode_validate_clocks);
  781. /**
  782. * drm_mode_prune_invalid - remove invalid modes from mode list
  783. * @dev: DRM device
  784. * @mode_list: list of modes to check
  785. * @verbose: be verbose about it
  786. *
  787. * LOCKING:
  788. * Caller must hold a lock protecting @mode_list.
  789. *
  790. * Once mode list generation is complete, a caller can use this routine to
  791. * remove invalid modes from a mode list. If any of the modes have a
  792. * status other than %MODE_OK, they are removed from @mode_list and freed.
  793. */
  794. void drm_mode_prune_invalid(struct drm_device *dev,
  795. struct list_head *mode_list, bool verbose)
  796. {
  797. struct drm_display_mode *mode, *t;
  798. list_for_each_entry_safe(mode, t, mode_list, head) {
  799. if (mode->status != MODE_OK) {
  800. list_del(&mode->head);
  801. if (verbose) {
  802. drm_mode_debug_printmodeline(mode);
  803. DRM_DEBUG_KMS("Not using %s mode %d\n",
  804. mode->name, mode->status);
  805. }
  806. drm_mode_destroy(dev, mode);
  807. }
  808. }
  809. }
  810. EXPORT_SYMBOL(drm_mode_prune_invalid);
  811. /**
  812. * drm_mode_compare - compare modes for favorability
  813. * @priv: unused
  814. * @lh_a: list_head for first mode
  815. * @lh_b: list_head for second mode
  816. *
  817. * LOCKING:
  818. * None.
  819. *
  820. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  821. * which is better.
  822. *
  823. * RETURNS:
  824. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  825. * positive if @lh_b is better than @lh_a.
  826. */
  827. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  828. {
  829. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  830. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  831. int diff;
  832. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  833. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  834. if (diff)
  835. return diff;
  836. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  837. if (diff)
  838. return diff;
  839. diff = b->clock - a->clock;
  840. return diff;
  841. }
  842. /**
  843. * drm_mode_sort - sort mode list
  844. * @mode_list: list to sort
  845. *
  846. * LOCKING:
  847. * Caller must hold a lock protecting @mode_list.
  848. *
  849. * Sort @mode_list by favorability, putting good modes first.
  850. */
  851. void drm_mode_sort(struct list_head *mode_list)
  852. {
  853. list_sort(NULL, mode_list, drm_mode_compare);
  854. }
  855. EXPORT_SYMBOL(drm_mode_sort);
  856. /**
  857. * drm_mode_connector_list_update - update the mode list for the connector
  858. * @connector: the connector to update
  859. *
  860. * LOCKING:
  861. * Caller must hold a lock protecting @mode_list.
  862. *
  863. * This moves the modes from the @connector probed_modes list
  864. * to the actual mode list. It compares the probed mode against the current
  865. * list and only adds different modes. All modes unverified after this point
  866. * will be removed by the prune invalid modes.
  867. */
  868. void drm_mode_connector_list_update(struct drm_connector *connector)
  869. {
  870. struct drm_display_mode *mode;
  871. struct drm_display_mode *pmode, *pt;
  872. int found_it;
  873. list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
  874. head) {
  875. found_it = 0;
  876. /* go through current modes checking for the new probed mode */
  877. list_for_each_entry(mode, &connector->modes, head) {
  878. if (drm_mode_equal(pmode, mode)) {
  879. found_it = 1;
  880. /* if equal delete the probed mode */
  881. mode->status = pmode->status;
  882. /* Merge type bits together */
  883. mode->type |= pmode->type;
  884. list_del(&pmode->head);
  885. drm_mode_destroy(connector->dev, pmode);
  886. break;
  887. }
  888. }
  889. if (!found_it) {
  890. list_move_tail(&pmode->head, &connector->modes);
  891. }
  892. }
  893. }
  894. EXPORT_SYMBOL(drm_mode_connector_list_update);