drm_modes.c 29 KB

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