drm_modes.c 29 KB

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