drm_modes.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. /* Fill the mode line name */
  248. drm_mode_set_name(drm_mode);
  249. if (reduced)
  250. drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
  251. DRM_MODE_FLAG_NVSYNC);
  252. else
  253. drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
  254. DRM_MODE_FLAG_NHSYNC);
  255. if (interlaced)
  256. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  257. return drm_mode;
  258. }
  259. EXPORT_SYMBOL(drm_cvt_mode);
  260. /**
  261. * drm_gtf_mode - create the modeline based on 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 :whether the margin is supported
  269. *
  270. * LOCKING.
  271. * none.
  272. *
  273. * return the modeline based on GTF algorithm
  274. *
  275. * This function is to create the modeline based on the GTF algorithm.
  276. * Generalized Timing Formula is derived from:
  277. * GTF Spreadsheet by Andy Morrish (1/5/97)
  278. * available at http://www.vesa.org
  279. *
  280. * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
  281. * What I have done is to translate it by using integer calculation.
  282. * I also refer to the function of fb_get_mode in the file of
  283. * drivers/video/fbmon.c
  284. */
  285. struct drm_display_mode *drm_gtf_mode(struct drm_device *dev, int hdisplay,
  286. int vdisplay, int vrefresh,
  287. bool interlaced, int margins)
  288. {
  289. /* 1) top/bottom margin size (% of height) - default: 1.8, */
  290. #define GTF_MARGIN_PERCENTAGE 18
  291. /* 2) character cell horizontal granularity (pixels) - default 8 */
  292. #define GTF_CELL_GRAN 8
  293. /* 3) Minimum vertical porch (lines) - default 3 */
  294. #define GTF_MIN_V_PORCH 1
  295. /* width of vsync in lines */
  296. #define V_SYNC_RQD 3
  297. /* width of hsync as % of total line */
  298. #define H_SYNC_PERCENT 8
  299. /* min time of vsync + back porch (microsec) */
  300. #define MIN_VSYNC_PLUS_BP 550
  301. /* blanking formula gradient */
  302. #define GTF_M 600
  303. /* blanking formula offset */
  304. #define GTF_C 40
  305. /* blanking formula scaling factor */
  306. #define GTF_K 128
  307. /* blanking formula scaling factor */
  308. #define GTF_J 20
  309. /* C' and M' are part of the Blanking Duty Cycle computation */
  310. #define GTF_C_PRIME (((GTF_C - GTF_J) * GTF_K / 256) + GTF_J)
  311. #define GTF_M_PRIME (GTF_K * GTF_M / 256)
  312. struct drm_display_mode *drm_mode;
  313. unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
  314. int top_margin, bottom_margin;
  315. int interlace;
  316. unsigned int hfreq_est;
  317. int vsync_plus_bp, vback_porch;
  318. unsigned int vtotal_lines, vfieldrate_est, hperiod;
  319. unsigned int vfield_rate, vframe_rate;
  320. int left_margin, right_margin;
  321. unsigned int total_active_pixels, ideal_duty_cycle;
  322. unsigned int hblank, total_pixels, pixel_freq;
  323. int hsync, hfront_porch, vodd_front_porch_lines;
  324. unsigned int tmp1, tmp2;
  325. drm_mode = drm_mode_create(dev);
  326. if (!drm_mode)
  327. return NULL;
  328. /* 1. In order to give correct results, the number of horizontal
  329. * pixels requested is first processed to ensure that it is divisible
  330. * by the character size, by rounding it to the nearest character
  331. * cell boundary:
  332. */
  333. hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  334. hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
  335. /* 2. If interlace is requested, the number of vertical lines assumed
  336. * by the calculation must be halved, as the computation calculates
  337. * the number of vertical lines per field.
  338. */
  339. if (interlaced)
  340. vdisplay_rnd = vdisplay / 2;
  341. else
  342. vdisplay_rnd = vdisplay;
  343. /* 3. Find the frame rate required: */
  344. if (interlaced)
  345. vfieldrate_rqd = vrefresh * 2;
  346. else
  347. vfieldrate_rqd = vrefresh;
  348. /* 4. Find number of lines in Top margin: */
  349. top_margin = 0;
  350. if (margins)
  351. top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  352. 1000;
  353. /* 5. Find number of lines in bottom margin: */
  354. bottom_margin = top_margin;
  355. /* 6. If interlace is required, then set variable interlace: */
  356. if (interlaced)
  357. interlace = 1;
  358. else
  359. interlace = 0;
  360. /* 7. Estimate the Horizontal frequency */
  361. {
  362. tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
  363. tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
  364. 2 + interlace;
  365. hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
  366. }
  367. /* 8. Find the number of lines in V sync + back porch */
  368. /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
  369. vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
  370. vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
  371. /* 9. Find the number of lines in V back porch alone: */
  372. vback_porch = vsync_plus_bp - V_SYNC_RQD;
  373. /* 10. Find the total number of lines in Vertical field period: */
  374. vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
  375. vsync_plus_bp + GTF_MIN_V_PORCH;
  376. /* 11. Estimate the Vertical field frequency: */
  377. vfieldrate_est = hfreq_est / vtotal_lines;
  378. /* 12. Find the actual horizontal period: */
  379. hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
  380. /* 13. Find the actual Vertical field frequency: */
  381. vfield_rate = hfreq_est / vtotal_lines;
  382. /* 14. Find the Vertical frame frequency: */
  383. if (interlaced)
  384. vframe_rate = vfield_rate / 2;
  385. else
  386. vframe_rate = vfield_rate;
  387. /* 15. Find number of pixels in left margin: */
  388. if (margins)
  389. left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  390. 1000;
  391. else
  392. left_margin = 0;
  393. /* 16.Find number of pixels in right margin: */
  394. right_margin = left_margin;
  395. /* 17.Find total number of active pixels in image and left and right */
  396. total_active_pixels = hdisplay_rnd + left_margin + right_margin;
  397. /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
  398. ideal_duty_cycle = GTF_C_PRIME * 1000 -
  399. (GTF_M_PRIME * 1000000 / hfreq_est);
  400. /* 19.Find the number of pixels in the blanking time to the nearest
  401. * double character cell: */
  402. hblank = total_active_pixels * ideal_duty_cycle /
  403. (100000 - ideal_duty_cycle);
  404. hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
  405. hblank = hblank * 2 * GTF_CELL_GRAN;
  406. /* 20.Find total number of pixels: */
  407. total_pixels = total_active_pixels + hblank;
  408. /* 21.Find pixel clock frequency: */
  409. pixel_freq = total_pixels * hfreq_est / 1000;
  410. /* Stage 1 computations are now complete; I should really pass
  411. * the results to another function and do the Stage 2 computations,
  412. * but I only need a few more values so I'll just append the
  413. * computations here for now */
  414. /* 17. Find the number of pixels in the horizontal sync period: */
  415. hsync = H_SYNC_PERCENT * total_pixels / 100;
  416. hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  417. hsync = hsync * GTF_CELL_GRAN;
  418. /* 18. Find the number of pixels in horizontal front porch period */
  419. hfront_porch = hblank / 2 - hsync;
  420. /* 36. Find the number of lines in the odd front porch period: */
  421. vodd_front_porch_lines = GTF_MIN_V_PORCH ;
  422. /* finally, pack the results in the mode struct */
  423. drm_mode->hdisplay = hdisplay_rnd;
  424. drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
  425. drm_mode->hsync_end = drm_mode->hsync_start + hsync;
  426. drm_mode->htotal = total_pixels;
  427. drm_mode->vdisplay = vdisplay_rnd;
  428. drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
  429. drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
  430. drm_mode->vtotal = vtotal_lines;
  431. drm_mode->clock = pixel_freq;
  432. drm_mode_set_name(drm_mode);
  433. drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
  434. if (interlaced) {
  435. drm_mode->vtotal *= 2;
  436. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  437. }
  438. return drm_mode;
  439. }
  440. EXPORT_SYMBOL(drm_gtf_mode);
  441. /**
  442. * drm_mode_set_name - set the name on a mode
  443. * @mode: name will be set in this mode
  444. *
  445. * LOCKING:
  446. * None.
  447. *
  448. * Set the name of @mode to a standard format.
  449. */
  450. void drm_mode_set_name(struct drm_display_mode *mode)
  451. {
  452. snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d", mode->hdisplay,
  453. mode->vdisplay);
  454. }
  455. EXPORT_SYMBOL(drm_mode_set_name);
  456. /**
  457. * drm_mode_list_concat - move modes from one list to another
  458. * @head: source list
  459. * @new: dst list
  460. *
  461. * LOCKING:
  462. * Caller must ensure both lists are locked.
  463. *
  464. * Move all the modes from @head to @new.
  465. */
  466. void drm_mode_list_concat(struct list_head *head, struct list_head *new)
  467. {
  468. struct list_head *entry, *tmp;
  469. list_for_each_safe(entry, tmp, head) {
  470. list_move_tail(entry, new);
  471. }
  472. }
  473. EXPORT_SYMBOL(drm_mode_list_concat);
  474. /**
  475. * drm_mode_width - get the width of a mode
  476. * @mode: mode
  477. *
  478. * LOCKING:
  479. * None.
  480. *
  481. * Return @mode's width (hdisplay) value.
  482. *
  483. * FIXME: is this needed?
  484. *
  485. * RETURNS:
  486. * @mode->hdisplay
  487. */
  488. int drm_mode_width(struct drm_display_mode *mode)
  489. {
  490. return mode->hdisplay;
  491. }
  492. EXPORT_SYMBOL(drm_mode_width);
  493. /**
  494. * drm_mode_height - get the height of a mode
  495. * @mode: mode
  496. *
  497. * LOCKING:
  498. * None.
  499. *
  500. * Return @mode's height (vdisplay) value.
  501. *
  502. * FIXME: is this needed?
  503. *
  504. * RETURNS:
  505. * @mode->vdisplay
  506. */
  507. int drm_mode_height(struct drm_display_mode *mode)
  508. {
  509. return mode->vdisplay;
  510. }
  511. EXPORT_SYMBOL(drm_mode_height);
  512. /** drm_mode_hsync - get the hsync of a mode
  513. * @mode: mode
  514. *
  515. * LOCKING:
  516. * None.
  517. *
  518. * Return @modes's hsync rate in kHz, rounded to the nearest int.
  519. */
  520. int drm_mode_hsync(struct drm_display_mode *mode)
  521. {
  522. unsigned int calc_val;
  523. if (mode->hsync)
  524. return mode->hsync;
  525. if (mode->htotal < 0)
  526. return 0;
  527. calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
  528. calc_val += 500; /* round to 1000Hz */
  529. calc_val /= 1000; /* truncate to kHz */
  530. return calc_val;
  531. }
  532. EXPORT_SYMBOL(drm_mode_hsync);
  533. /**
  534. * drm_mode_vrefresh - get the vrefresh of a mode
  535. * @mode: mode
  536. *
  537. * LOCKING:
  538. * None.
  539. *
  540. * Return @mode's vrefresh rate in Hz or calculate it if necessary.
  541. *
  542. * FIXME: why is this needed? shouldn't vrefresh be set already?
  543. *
  544. * RETURNS:
  545. * Vertical refresh rate. It will be the result of actual value plus 0.5.
  546. * If it is 70.288, it will return 70Hz.
  547. * If it is 59.6, it will return 60Hz.
  548. */
  549. int drm_mode_vrefresh(struct drm_display_mode *mode)
  550. {
  551. int refresh = 0;
  552. unsigned int calc_val;
  553. if (mode->vrefresh > 0)
  554. refresh = mode->vrefresh;
  555. else if (mode->htotal > 0 && mode->vtotal > 0) {
  556. int vtotal;
  557. vtotal = mode->vtotal;
  558. /* work out vrefresh the value will be x1000 */
  559. calc_val = (mode->clock * 1000);
  560. calc_val /= mode->htotal;
  561. refresh = (calc_val + vtotal / 2) / vtotal;
  562. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  563. refresh *= 2;
  564. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  565. refresh /= 2;
  566. if (mode->vscan > 1)
  567. refresh /= mode->vscan;
  568. }
  569. return refresh;
  570. }
  571. EXPORT_SYMBOL(drm_mode_vrefresh);
  572. /**
  573. * drm_mode_set_crtcinfo - set CRTC modesetting parameters
  574. * @p: mode
  575. * @adjust_flags: unused? (FIXME)
  576. *
  577. * LOCKING:
  578. * None.
  579. *
  580. * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
  581. */
  582. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  583. {
  584. if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
  585. return;
  586. p->crtc_hdisplay = p->hdisplay;
  587. p->crtc_hsync_start = p->hsync_start;
  588. p->crtc_hsync_end = p->hsync_end;
  589. p->crtc_htotal = p->htotal;
  590. p->crtc_hskew = p->hskew;
  591. p->crtc_vdisplay = p->vdisplay;
  592. p->crtc_vsync_start = p->vsync_start;
  593. p->crtc_vsync_end = p->vsync_end;
  594. p->crtc_vtotal = p->vtotal;
  595. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  596. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  597. p->crtc_vdisplay /= 2;
  598. p->crtc_vsync_start /= 2;
  599. p->crtc_vsync_end /= 2;
  600. p->crtc_vtotal /= 2;
  601. }
  602. p->crtc_vtotal |= 1;
  603. }
  604. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  605. p->crtc_vdisplay *= 2;
  606. p->crtc_vsync_start *= 2;
  607. p->crtc_vsync_end *= 2;
  608. p->crtc_vtotal *= 2;
  609. }
  610. if (p->vscan > 1) {
  611. p->crtc_vdisplay *= p->vscan;
  612. p->crtc_vsync_start *= p->vscan;
  613. p->crtc_vsync_end *= p->vscan;
  614. p->crtc_vtotal *= p->vscan;
  615. }
  616. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  617. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  618. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  619. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  620. p->crtc_hadjusted = false;
  621. p->crtc_vadjusted = false;
  622. }
  623. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  624. /**
  625. * drm_mode_duplicate - allocate and duplicate an existing mode
  626. * @m: mode to duplicate
  627. *
  628. * LOCKING:
  629. * None.
  630. *
  631. * Just allocate a new mode, copy the existing mode into it, and return
  632. * a pointer to it. Used to create new instances of established modes.
  633. */
  634. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  635. struct drm_display_mode *mode)
  636. {
  637. struct drm_display_mode *nmode;
  638. int new_id;
  639. nmode = drm_mode_create(dev);
  640. if (!nmode)
  641. return NULL;
  642. new_id = nmode->base.id;
  643. *nmode = *mode;
  644. nmode->base.id = new_id;
  645. INIT_LIST_HEAD(&nmode->head);
  646. return nmode;
  647. }
  648. EXPORT_SYMBOL(drm_mode_duplicate);
  649. /**
  650. * drm_mode_equal - test modes for equality
  651. * @mode1: first mode
  652. * @mode2: second mode
  653. *
  654. * LOCKING:
  655. * None.
  656. *
  657. * Check to see if @mode1 and @mode2 are equivalent.
  658. *
  659. * RETURNS:
  660. * True if the modes are equal, false otherwise.
  661. */
  662. bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
  663. {
  664. /* do clock check convert to PICOS so fb modes get matched
  665. * the same */
  666. if (mode1->clock && mode2->clock) {
  667. if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
  668. return false;
  669. } else if (mode1->clock != mode2->clock)
  670. return false;
  671. if (mode1->hdisplay == mode2->hdisplay &&
  672. mode1->hsync_start == mode2->hsync_start &&
  673. mode1->hsync_end == mode2->hsync_end &&
  674. mode1->htotal == mode2->htotal &&
  675. mode1->hskew == mode2->hskew &&
  676. mode1->vdisplay == mode2->vdisplay &&
  677. mode1->vsync_start == mode2->vsync_start &&
  678. mode1->vsync_end == mode2->vsync_end &&
  679. mode1->vtotal == mode2->vtotal &&
  680. mode1->vscan == mode2->vscan &&
  681. mode1->flags == mode2->flags)
  682. return true;
  683. return false;
  684. }
  685. EXPORT_SYMBOL(drm_mode_equal);
  686. /**
  687. * drm_mode_validate_size - make sure modes adhere to size constraints
  688. * @dev: DRM device
  689. * @mode_list: list of modes to check
  690. * @maxX: maximum width
  691. * @maxY: maximum height
  692. * @maxPitch: max pitch
  693. *
  694. * LOCKING:
  695. * Caller must hold a lock protecting @mode_list.
  696. *
  697. * The DRM device (@dev) has size and pitch limits. Here we validate the
  698. * modes we probed for @dev against those limits and set their status as
  699. * necessary.
  700. */
  701. void drm_mode_validate_size(struct drm_device *dev,
  702. struct list_head *mode_list,
  703. int maxX, int maxY, int maxPitch)
  704. {
  705. struct drm_display_mode *mode;
  706. list_for_each_entry(mode, mode_list, head) {
  707. if (maxPitch > 0 && mode->hdisplay > maxPitch)
  708. mode->status = MODE_BAD_WIDTH;
  709. if (maxX > 0 && mode->hdisplay > maxX)
  710. mode->status = MODE_VIRTUAL_X;
  711. if (maxY > 0 && mode->vdisplay > maxY)
  712. mode->status = MODE_VIRTUAL_Y;
  713. }
  714. }
  715. EXPORT_SYMBOL(drm_mode_validate_size);
  716. /**
  717. * drm_mode_validate_clocks - validate modes against clock limits
  718. * @dev: DRM device
  719. * @mode_list: list of modes to check
  720. * @min: minimum clock rate array
  721. * @max: maximum clock rate array
  722. * @n_ranges: number of clock ranges (size of arrays)
  723. *
  724. * LOCKING:
  725. * Caller must hold a lock protecting @mode_list.
  726. *
  727. * Some code may need to check a mode list against the clock limits of the
  728. * device in question. This function walks the mode list, testing to make
  729. * sure each mode falls within a given range (defined by @min and @max
  730. * arrays) and sets @mode->status as needed.
  731. */
  732. void drm_mode_validate_clocks(struct drm_device *dev,
  733. struct list_head *mode_list,
  734. int *min, int *max, int n_ranges)
  735. {
  736. struct drm_display_mode *mode;
  737. int i;
  738. list_for_each_entry(mode, mode_list, head) {
  739. bool good = false;
  740. for (i = 0; i < n_ranges; i++) {
  741. if (mode->clock >= min[i] && mode->clock <= max[i]) {
  742. good = true;
  743. break;
  744. }
  745. }
  746. if (!good)
  747. mode->status = MODE_CLOCK_RANGE;
  748. }
  749. }
  750. EXPORT_SYMBOL(drm_mode_validate_clocks);
  751. /**
  752. * drm_mode_prune_invalid - remove invalid modes from mode list
  753. * @dev: DRM device
  754. * @mode_list: list of modes to check
  755. * @verbose: be verbose about it
  756. *
  757. * LOCKING:
  758. * Caller must hold a lock protecting @mode_list.
  759. *
  760. * Once mode list generation is complete, a caller can use this routine to
  761. * remove invalid modes from a mode list. If any of the modes have a
  762. * status other than %MODE_OK, they are removed from @mode_list and freed.
  763. */
  764. void drm_mode_prune_invalid(struct drm_device *dev,
  765. struct list_head *mode_list, bool verbose)
  766. {
  767. struct drm_display_mode *mode, *t;
  768. list_for_each_entry_safe(mode, t, mode_list, head) {
  769. if (mode->status != MODE_OK) {
  770. list_del(&mode->head);
  771. if (verbose) {
  772. drm_mode_debug_printmodeline(mode);
  773. DRM_DEBUG_KMS("Not using %s mode %d\n",
  774. mode->name, mode->status);
  775. }
  776. drm_mode_destroy(dev, mode);
  777. }
  778. }
  779. }
  780. EXPORT_SYMBOL(drm_mode_prune_invalid);
  781. /**
  782. * drm_mode_compare - compare modes for favorability
  783. * @priv: unused
  784. * @lh_a: list_head for first mode
  785. * @lh_b: list_head for second mode
  786. *
  787. * LOCKING:
  788. * None.
  789. *
  790. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  791. * which is better.
  792. *
  793. * RETURNS:
  794. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  795. * positive if @lh_b is better than @lh_a.
  796. */
  797. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  798. {
  799. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  800. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  801. int diff;
  802. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  803. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  804. if (diff)
  805. return diff;
  806. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  807. if (diff)
  808. return diff;
  809. diff = b->clock - a->clock;
  810. return diff;
  811. }
  812. /**
  813. * drm_mode_sort - sort mode list
  814. * @mode_list: list to sort
  815. *
  816. * LOCKING:
  817. * Caller must hold a lock protecting @mode_list.
  818. *
  819. * Sort @mode_list by favorability, putting good modes first.
  820. */
  821. void drm_mode_sort(struct list_head *mode_list)
  822. {
  823. list_sort(NULL, mode_list, drm_mode_compare);
  824. }
  825. EXPORT_SYMBOL(drm_mode_sort);
  826. /**
  827. * drm_mode_connector_list_update - update the mode list for the connector
  828. * @connector: the connector to update
  829. *
  830. * LOCKING:
  831. * Caller must hold a lock protecting @mode_list.
  832. *
  833. * This moves the modes from the @connector probed_modes list
  834. * to the actual mode list. It compares the probed mode against the current
  835. * list and only adds different modes. All modes unverified after this point
  836. * will be removed by the prune invalid modes.
  837. */
  838. void drm_mode_connector_list_update(struct drm_connector *connector)
  839. {
  840. struct drm_display_mode *mode;
  841. struct drm_display_mode *pmode, *pt;
  842. int found_it;
  843. list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
  844. head) {
  845. found_it = 0;
  846. /* go through current modes checking for the new probed mode */
  847. list_for_each_entry(mode, &connector->modes, head) {
  848. if (drm_mode_equal(pmode, mode)) {
  849. found_it = 1;
  850. /* if equal delete the probed mode */
  851. mode->status = pmode->status;
  852. /* Merge type bits together */
  853. mode->type |= pmode->type;
  854. list_del(&pmode->head);
  855. drm_mode_destroy(connector->dev, pmode);
  856. break;
  857. }
  858. }
  859. if (!found_it) {
  860. list_move_tail(&pmode->head, &connector->modes);
  861. }
  862. }
  863. }
  864. EXPORT_SYMBOL(drm_mode_connector_list_update);