drm_modes.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. /** drm_mode_hsync - get the hsync of a mode
  517. * @mode: mode
  518. *
  519. * LOCKING:
  520. * None.
  521. *
  522. * Return @modes's hsync rate in kHz, rounded to the nearest int.
  523. */
  524. int drm_mode_hsync(struct drm_display_mode *mode)
  525. {
  526. unsigned int calc_val;
  527. if (mode->hsync)
  528. return mode->hsync;
  529. if (mode->htotal < 0)
  530. return 0;
  531. calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
  532. calc_val += 500; /* round to 1000Hz */
  533. calc_val /= 1000; /* truncate to kHz */
  534. return calc_val;
  535. }
  536. EXPORT_SYMBOL(drm_mode_hsync);
  537. /**
  538. * drm_mode_vrefresh - get the vrefresh of a mode
  539. * @mode: mode
  540. *
  541. * LOCKING:
  542. * None.
  543. *
  544. * Return @mode's vrefresh rate in Hz or calculate it if necessary.
  545. *
  546. * FIXME: why is this needed? shouldn't vrefresh be set already?
  547. *
  548. * RETURNS:
  549. * Vertical refresh rate. It will be the result of actual value plus 0.5.
  550. * If it is 70.288, it will return 70Hz.
  551. * If it is 59.6, it will return 60Hz.
  552. */
  553. int drm_mode_vrefresh(struct drm_display_mode *mode)
  554. {
  555. int refresh = 0;
  556. unsigned int calc_val;
  557. if (mode->vrefresh > 0)
  558. refresh = mode->vrefresh;
  559. else if (mode->htotal > 0 && mode->vtotal > 0) {
  560. int vtotal;
  561. vtotal = mode->vtotal;
  562. /* work out vrefresh the value will be x1000 */
  563. calc_val = (mode->clock * 1000);
  564. calc_val /= mode->htotal;
  565. refresh = (calc_val + vtotal / 2) / vtotal;
  566. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  567. refresh *= 2;
  568. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  569. refresh /= 2;
  570. if (mode->vscan > 1)
  571. refresh /= mode->vscan;
  572. }
  573. return refresh;
  574. }
  575. EXPORT_SYMBOL(drm_mode_vrefresh);
  576. /**
  577. * drm_mode_set_crtcinfo - set CRTC modesetting parameters
  578. * @p: mode
  579. * @adjust_flags: unused? (FIXME)
  580. *
  581. * LOCKING:
  582. * None.
  583. *
  584. * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
  585. */
  586. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  587. {
  588. if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
  589. return;
  590. p->crtc_hdisplay = p->hdisplay;
  591. p->crtc_hsync_start = p->hsync_start;
  592. p->crtc_hsync_end = p->hsync_end;
  593. p->crtc_htotal = p->htotal;
  594. p->crtc_hskew = p->hskew;
  595. p->crtc_vdisplay = p->vdisplay;
  596. p->crtc_vsync_start = p->vsync_start;
  597. p->crtc_vsync_end = p->vsync_end;
  598. p->crtc_vtotal = p->vtotal;
  599. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  600. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  601. p->crtc_vdisplay /= 2;
  602. p->crtc_vsync_start /= 2;
  603. p->crtc_vsync_end /= 2;
  604. p->crtc_vtotal /= 2;
  605. }
  606. p->crtc_vtotal |= 1;
  607. }
  608. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  609. p->crtc_vdisplay *= 2;
  610. p->crtc_vsync_start *= 2;
  611. p->crtc_vsync_end *= 2;
  612. p->crtc_vtotal *= 2;
  613. }
  614. if (p->vscan > 1) {
  615. p->crtc_vdisplay *= p->vscan;
  616. p->crtc_vsync_start *= p->vscan;
  617. p->crtc_vsync_end *= p->vscan;
  618. p->crtc_vtotal *= p->vscan;
  619. }
  620. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  621. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  622. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  623. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  624. p->crtc_hadjusted = false;
  625. p->crtc_vadjusted = false;
  626. }
  627. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  628. /**
  629. * drm_mode_duplicate - allocate and duplicate an existing mode
  630. * @m: mode to duplicate
  631. *
  632. * LOCKING:
  633. * None.
  634. *
  635. * Just allocate a new mode, copy the existing mode into it, and return
  636. * a pointer to it. Used to create new instances of established modes.
  637. */
  638. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  639. struct drm_display_mode *mode)
  640. {
  641. struct drm_display_mode *nmode;
  642. int new_id;
  643. nmode = drm_mode_create(dev);
  644. if (!nmode)
  645. return NULL;
  646. new_id = nmode->base.id;
  647. *nmode = *mode;
  648. nmode->base.id = new_id;
  649. INIT_LIST_HEAD(&nmode->head);
  650. return nmode;
  651. }
  652. EXPORT_SYMBOL(drm_mode_duplicate);
  653. /**
  654. * drm_mode_equal - test modes for equality
  655. * @mode1: first mode
  656. * @mode2: second mode
  657. *
  658. * LOCKING:
  659. * None.
  660. *
  661. * Check to see if @mode1 and @mode2 are equivalent.
  662. *
  663. * RETURNS:
  664. * True if the modes are equal, false otherwise.
  665. */
  666. bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
  667. {
  668. /* do clock check convert to PICOS so fb modes get matched
  669. * the same */
  670. if (mode1->clock && mode2->clock) {
  671. if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
  672. return false;
  673. } else if (mode1->clock != mode2->clock)
  674. return false;
  675. if (mode1->hdisplay == mode2->hdisplay &&
  676. mode1->hsync_start == mode2->hsync_start &&
  677. mode1->hsync_end == mode2->hsync_end &&
  678. mode1->htotal == mode2->htotal &&
  679. mode1->hskew == mode2->hskew &&
  680. mode1->vdisplay == mode2->vdisplay &&
  681. mode1->vsync_start == mode2->vsync_start &&
  682. mode1->vsync_end == mode2->vsync_end &&
  683. mode1->vtotal == mode2->vtotal &&
  684. mode1->vscan == mode2->vscan &&
  685. mode1->flags == mode2->flags)
  686. return true;
  687. return false;
  688. }
  689. EXPORT_SYMBOL(drm_mode_equal);
  690. /**
  691. * drm_mode_validate_size - make sure modes adhere to size constraints
  692. * @dev: DRM device
  693. * @mode_list: list of modes to check
  694. * @maxX: maximum width
  695. * @maxY: maximum height
  696. * @maxPitch: max pitch
  697. *
  698. * LOCKING:
  699. * Caller must hold a lock protecting @mode_list.
  700. *
  701. * The DRM device (@dev) has size and pitch limits. Here we validate the
  702. * modes we probed for @dev against those limits and set their status as
  703. * necessary.
  704. */
  705. void drm_mode_validate_size(struct drm_device *dev,
  706. struct list_head *mode_list,
  707. int maxX, int maxY, int maxPitch)
  708. {
  709. struct drm_display_mode *mode;
  710. list_for_each_entry(mode, mode_list, head) {
  711. if (maxPitch > 0 && mode->hdisplay > maxPitch)
  712. mode->status = MODE_BAD_WIDTH;
  713. if (maxX > 0 && mode->hdisplay > maxX)
  714. mode->status = MODE_VIRTUAL_X;
  715. if (maxY > 0 && mode->vdisplay > maxY)
  716. mode->status = MODE_VIRTUAL_Y;
  717. }
  718. }
  719. EXPORT_SYMBOL(drm_mode_validate_size);
  720. /**
  721. * drm_mode_validate_clocks - validate modes against clock limits
  722. * @dev: DRM device
  723. * @mode_list: list of modes to check
  724. * @min: minimum clock rate array
  725. * @max: maximum clock rate array
  726. * @n_ranges: number of clock ranges (size of arrays)
  727. *
  728. * LOCKING:
  729. * Caller must hold a lock protecting @mode_list.
  730. *
  731. * Some code may need to check a mode list against the clock limits of the
  732. * device in question. This function walks the mode list, testing to make
  733. * sure each mode falls within a given range (defined by @min and @max
  734. * arrays) and sets @mode->status as needed.
  735. */
  736. void drm_mode_validate_clocks(struct drm_device *dev,
  737. struct list_head *mode_list,
  738. int *min, int *max, int n_ranges)
  739. {
  740. struct drm_display_mode *mode;
  741. int i;
  742. list_for_each_entry(mode, mode_list, head) {
  743. bool good = false;
  744. for (i = 0; i < n_ranges; i++) {
  745. if (mode->clock >= min[i] && mode->clock <= max[i]) {
  746. good = true;
  747. break;
  748. }
  749. }
  750. if (!good)
  751. mode->status = MODE_CLOCK_RANGE;
  752. }
  753. }
  754. EXPORT_SYMBOL(drm_mode_validate_clocks);
  755. /**
  756. * drm_mode_prune_invalid - remove invalid modes from mode list
  757. * @dev: DRM device
  758. * @mode_list: list of modes to check
  759. * @verbose: be verbose about it
  760. *
  761. * LOCKING:
  762. * Caller must hold a lock protecting @mode_list.
  763. *
  764. * Once mode list generation is complete, a caller can use this routine to
  765. * remove invalid modes from a mode list. If any of the modes have a
  766. * status other than %MODE_OK, they are removed from @mode_list and freed.
  767. */
  768. void drm_mode_prune_invalid(struct drm_device *dev,
  769. struct list_head *mode_list, bool verbose)
  770. {
  771. struct drm_display_mode *mode, *t;
  772. list_for_each_entry_safe(mode, t, mode_list, head) {
  773. if (mode->status != MODE_OK) {
  774. list_del(&mode->head);
  775. if (verbose) {
  776. drm_mode_debug_printmodeline(mode);
  777. DRM_DEBUG_KMS("Not using %s mode %d\n",
  778. mode->name, mode->status);
  779. }
  780. drm_mode_destroy(dev, mode);
  781. }
  782. }
  783. }
  784. EXPORT_SYMBOL(drm_mode_prune_invalid);
  785. /**
  786. * drm_mode_compare - compare modes for favorability
  787. * @lh_a: list_head for first mode
  788. * @lh_b: list_head for second mode
  789. *
  790. * LOCKING:
  791. * None.
  792. *
  793. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  794. * which is better.
  795. *
  796. * RETURNS:
  797. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  798. * positive if @lh_b is better than @lh_a.
  799. */
  800. static int drm_mode_compare(struct list_head *lh_a, struct list_head *lh_b)
  801. {
  802. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  803. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  804. int diff;
  805. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  806. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  807. if (diff)
  808. return diff;
  809. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  810. if (diff)
  811. return diff;
  812. diff = b->clock - a->clock;
  813. return diff;
  814. }
  815. /* FIXME: what we don't have a list sort function? */
  816. /* list sort from Mark J Roberts (mjr@znex.org) */
  817. void list_sort(struct list_head *head,
  818. int (*cmp)(struct list_head *a, struct list_head *b))
  819. {
  820. struct list_head *p, *q, *e, *list, *tail, *oldhead;
  821. int insize, nmerges, psize, qsize, i;
  822. list = head->next;
  823. list_del(head);
  824. insize = 1;
  825. for (;;) {
  826. p = oldhead = list;
  827. list = tail = NULL;
  828. nmerges = 0;
  829. while (p) {
  830. nmerges++;
  831. q = p;
  832. psize = 0;
  833. for (i = 0; i < insize; i++) {
  834. psize++;
  835. q = q->next == oldhead ? NULL : q->next;
  836. if (!q)
  837. break;
  838. }
  839. qsize = insize;
  840. while (psize > 0 || (qsize > 0 && q)) {
  841. if (!psize) {
  842. e = q;
  843. q = q->next;
  844. qsize--;
  845. if (q == oldhead)
  846. q = NULL;
  847. } else if (!qsize || !q) {
  848. e = p;
  849. p = p->next;
  850. psize--;
  851. if (p == oldhead)
  852. p = NULL;
  853. } else if (cmp(p, q) <= 0) {
  854. e = p;
  855. p = p->next;
  856. psize--;
  857. if (p == oldhead)
  858. p = NULL;
  859. } else {
  860. e = q;
  861. q = q->next;
  862. qsize--;
  863. if (q == oldhead)
  864. q = NULL;
  865. }
  866. if (tail)
  867. tail->next = e;
  868. else
  869. list = e;
  870. e->prev = tail;
  871. tail = e;
  872. }
  873. p = q;
  874. }
  875. tail->next = list;
  876. list->prev = tail;
  877. if (nmerges <= 1)
  878. break;
  879. insize *= 2;
  880. }
  881. head->next = list;
  882. head->prev = list->prev;
  883. list->prev->next = head;
  884. list->prev = head;
  885. }
  886. /**
  887. * drm_mode_sort - sort mode list
  888. * @mode_list: list to sort
  889. *
  890. * LOCKING:
  891. * Caller must hold a lock protecting @mode_list.
  892. *
  893. * Sort @mode_list by favorability, putting good modes first.
  894. */
  895. void drm_mode_sort(struct list_head *mode_list)
  896. {
  897. list_sort(mode_list, drm_mode_compare);
  898. }
  899. EXPORT_SYMBOL(drm_mode_sort);
  900. /**
  901. * drm_mode_connector_list_update - update the mode list for the connector
  902. * @connector: the connector to update
  903. *
  904. * LOCKING:
  905. * Caller must hold a lock protecting @mode_list.
  906. *
  907. * This moves the modes from the @connector probed_modes list
  908. * to the actual mode list. It compares the probed mode against the current
  909. * list and only adds different modes. All modes unverified after this point
  910. * will be removed by the prune invalid modes.
  911. */
  912. void drm_mode_connector_list_update(struct drm_connector *connector)
  913. {
  914. struct drm_display_mode *mode;
  915. struct drm_display_mode *pmode, *pt;
  916. int found_it;
  917. list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
  918. head) {
  919. found_it = 0;
  920. /* go through current modes checking for the new probed mode */
  921. list_for_each_entry(mode, &connector->modes, head) {
  922. if (drm_mode_equal(pmode, mode)) {
  923. found_it = 1;
  924. /* if equal delete the probed mode */
  925. mode->status = pmode->status;
  926. /* Merge type bits together */
  927. mode->type |= pmode->type;
  928. list_del(&pmode->head);
  929. drm_mode_destroy(connector->dev, pmode);
  930. break;
  931. }
  932. }
  933. if (!found_it) {
  934. list_move_tail(&pmode->head, &connector->modes);
  935. }
  936. }
  937. }
  938. EXPORT_SYMBOL(drm_mode_connector_list_update);