drm_modes.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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 <linux/export.h>
  35. #include <drm/drmP.h>
  36. #include <drm/drm_crtc.h>
  37. #include <video/of_videomode.h>
  38. #include <video/videomode.h>
  39. /**
  40. * drm_mode_debug_printmodeline - debug print a mode
  41. * @dev: DRM device
  42. * @mode: mode to print
  43. *
  44. * LOCKING:
  45. * None.
  46. *
  47. * Describe @mode using DRM_DEBUG.
  48. */
  49. void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
  50. {
  51. DRM_DEBUG_KMS("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d "
  52. "0x%x 0x%x\n",
  53. mode->base.id, mode->name, mode->vrefresh, mode->clock,
  54. mode->hdisplay, mode->hsync_start,
  55. mode->hsync_end, mode->htotal,
  56. mode->vdisplay, mode->vsync_start,
  57. mode->vsync_end, mode->vtotal, mode->type, mode->flags);
  58. }
  59. EXPORT_SYMBOL(drm_mode_debug_printmodeline);
  60. /**
  61. * drm_cvt_mode -create a modeline based on CVT algorithm
  62. * @dev: DRM device
  63. * @hdisplay: hdisplay size
  64. * @vdisplay: vdisplay size
  65. * @vrefresh : vrefresh rate
  66. * @reduced : Whether the GTF calculation is simplified
  67. * @interlaced:Whether the interlace is supported
  68. *
  69. * LOCKING:
  70. * none.
  71. *
  72. * return the modeline based on CVT algorithm
  73. *
  74. * This function is called to generate the modeline based on CVT algorithm
  75. * according to the hdisplay, vdisplay, vrefresh.
  76. * It is based from the VESA(TM) Coordinated Video Timing Generator by
  77. * Graham Loveridge April 9, 2003 available at
  78. * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
  79. *
  80. * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
  81. * What I have done is to translate it by using integer calculation.
  82. */
  83. #define HV_FACTOR 1000
  84. struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
  85. int vdisplay, int vrefresh,
  86. bool reduced, bool interlaced, bool margins)
  87. {
  88. /* 1) top/bottom margin size (% of height) - default: 1.8, */
  89. #define CVT_MARGIN_PERCENTAGE 18
  90. /* 2) character cell horizontal granularity (pixels) - default 8 */
  91. #define CVT_H_GRANULARITY 8
  92. /* 3) Minimum vertical porch (lines) - default 3 */
  93. #define CVT_MIN_V_PORCH 3
  94. /* 4) Minimum number of vertical back porch lines - default 6 */
  95. #define CVT_MIN_V_BPORCH 6
  96. /* Pixel Clock step (kHz) */
  97. #define CVT_CLOCK_STEP 250
  98. struct drm_display_mode *drm_mode;
  99. unsigned int vfieldrate, hperiod;
  100. int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
  101. int interlace;
  102. /* allocate the drm_display_mode structure. If failure, we will
  103. * return directly
  104. */
  105. drm_mode = drm_mode_create(dev);
  106. if (!drm_mode)
  107. return NULL;
  108. /* the CVT default refresh rate is 60Hz */
  109. if (!vrefresh)
  110. vrefresh = 60;
  111. /* the required field fresh rate */
  112. if (interlaced)
  113. vfieldrate = vrefresh * 2;
  114. else
  115. vfieldrate = vrefresh;
  116. /* horizontal pixels */
  117. hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
  118. /* determine the left&right borders */
  119. hmargin = 0;
  120. if (margins) {
  121. hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  122. hmargin -= hmargin % CVT_H_GRANULARITY;
  123. }
  124. /* find the total active pixels */
  125. drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
  126. /* find the number of lines per field */
  127. if (interlaced)
  128. vdisplay_rnd = vdisplay / 2;
  129. else
  130. vdisplay_rnd = vdisplay;
  131. /* find the top & bottom borders */
  132. vmargin = 0;
  133. if (margins)
  134. vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  135. drm_mode->vdisplay = vdisplay + 2 * vmargin;
  136. /* Interlaced */
  137. if (interlaced)
  138. interlace = 1;
  139. else
  140. interlace = 0;
  141. /* Determine VSync Width from aspect ratio */
  142. if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
  143. vsync = 4;
  144. else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
  145. vsync = 5;
  146. else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
  147. vsync = 6;
  148. else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
  149. vsync = 7;
  150. else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
  151. vsync = 7;
  152. else /* custom */
  153. vsync = 10;
  154. if (!reduced) {
  155. /* simplify the GTF calculation */
  156. /* 4) Minimum time of vertical sync + back porch interval (µs)
  157. * default 550.0
  158. */
  159. int tmp1, tmp2;
  160. #define CVT_MIN_VSYNC_BP 550
  161. /* 3) Nominal HSync width (% of line period) - default 8 */
  162. #define CVT_HSYNC_PERCENTAGE 8
  163. unsigned int hblank_percentage;
  164. int vsyncandback_porch, vback_porch, hblank;
  165. /* estimated the horizontal period */
  166. tmp1 = HV_FACTOR * 1000000 -
  167. CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
  168. tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
  169. interlace;
  170. hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
  171. tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
  172. /* 9. Find number of lines in sync + backporch */
  173. if (tmp1 < (vsync + CVT_MIN_V_PORCH))
  174. vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
  175. else
  176. vsyncandback_porch = tmp1;
  177. /* 10. Find number of lines in back porch */
  178. vback_porch = vsyncandback_porch - vsync;
  179. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
  180. vsyncandback_porch + CVT_MIN_V_PORCH;
  181. /* 5) Definition of Horizontal blanking time limitation */
  182. /* Gradient (%/kHz) - default 600 */
  183. #define CVT_M_FACTOR 600
  184. /* Offset (%) - default 40 */
  185. #define CVT_C_FACTOR 40
  186. /* Blanking time scaling factor - default 128 */
  187. #define CVT_K_FACTOR 128
  188. /* Scaling factor weighting - default 20 */
  189. #define CVT_J_FACTOR 20
  190. #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
  191. #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
  192. CVT_J_FACTOR)
  193. /* 12. Find ideal blanking duty cycle from formula */
  194. hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
  195. hperiod / 1000;
  196. /* 13. Blanking time */
  197. if (hblank_percentage < 20 * HV_FACTOR)
  198. hblank_percentage = 20 * HV_FACTOR;
  199. hblank = drm_mode->hdisplay * hblank_percentage /
  200. (100 * HV_FACTOR - hblank_percentage);
  201. hblank -= hblank % (2 * CVT_H_GRANULARITY);
  202. /* 14. find the total pixes per line */
  203. drm_mode->htotal = drm_mode->hdisplay + hblank;
  204. drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
  205. drm_mode->hsync_start = drm_mode->hsync_end -
  206. (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
  207. drm_mode->hsync_start += CVT_H_GRANULARITY -
  208. drm_mode->hsync_start % CVT_H_GRANULARITY;
  209. /* fill the Vsync values */
  210. drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
  211. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  212. } else {
  213. /* Reduced blanking */
  214. /* Minimum vertical blanking interval time (µs)- default 460 */
  215. #define CVT_RB_MIN_VBLANK 460
  216. /* Fixed number of clocks for horizontal sync */
  217. #define CVT_RB_H_SYNC 32
  218. /* Fixed number of clocks for horizontal blanking */
  219. #define CVT_RB_H_BLANK 160
  220. /* Fixed number of lines for vertical front porch - default 3*/
  221. #define CVT_RB_VFPORCH 3
  222. int vbilines;
  223. int tmp1, tmp2;
  224. /* 8. Estimate Horizontal period. */
  225. tmp1 = HV_FACTOR * 1000000 -
  226. CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
  227. tmp2 = vdisplay_rnd + 2 * vmargin;
  228. hperiod = tmp1 / (tmp2 * vfieldrate);
  229. /* 9. Find number of lines in vertical blanking */
  230. vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
  231. /* 10. Check if vertical blanking is sufficient */
  232. if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
  233. vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
  234. /* 11. Find total number of lines in vertical field */
  235. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
  236. /* 12. Find total number of pixels in a line */
  237. drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
  238. /* Fill in HSync values */
  239. drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
  240. drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
  241. /* Fill in VSync values */
  242. drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
  243. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  244. }
  245. /* 15/13. Find pixel clock frequency (kHz for xf86) */
  246. drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
  247. drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
  248. /* 18/16. Find actual vertical frame frequency */
  249. /* ignore - just set the mode flag for interlaced */
  250. if (interlaced) {
  251. drm_mode->vtotal *= 2;
  252. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  253. }
  254. /* Fill the mode line name */
  255. drm_mode_set_name(drm_mode);
  256. if (reduced)
  257. drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
  258. DRM_MODE_FLAG_NVSYNC);
  259. else
  260. drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
  261. DRM_MODE_FLAG_NHSYNC);
  262. return drm_mode;
  263. }
  264. EXPORT_SYMBOL(drm_cvt_mode);
  265. /**
  266. * drm_gtf_mode_complex - create the modeline based on full GTF algorithm
  267. *
  268. * @dev :drm device
  269. * @hdisplay :hdisplay size
  270. * @vdisplay :vdisplay size
  271. * @vrefresh :vrefresh rate.
  272. * @interlaced :whether the interlace is supported
  273. * @margins :desired margin size
  274. * @GTF_[MCKJ] :extended GTF formula parameters
  275. *
  276. * LOCKING.
  277. * none.
  278. *
  279. * return the modeline based on full GTF algorithm.
  280. *
  281. * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
  282. * in here multiplied by two. For a C of 40, pass in 80.
  283. */
  284. struct drm_display_mode *
  285. drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
  286. int vrefresh, bool interlaced, int margins,
  287. int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
  288. { /* 1) top/bottom margin size (% of height) - default: 1.8, */
  289. #define GTF_MARGIN_PERCENTAGE 18
  290. /* 2) character cell horizontal granularity (pixels) - default 8 */
  291. #define GTF_CELL_GRAN 8
  292. /* 3) Minimum vertical porch (lines) - default 3 */
  293. #define GTF_MIN_V_PORCH 1
  294. /* width of vsync in lines */
  295. #define V_SYNC_RQD 3
  296. /* width of hsync as % of total line */
  297. #define H_SYNC_PERCENT 8
  298. /* min time of vsync + back porch (microsec) */
  299. #define MIN_VSYNC_PLUS_BP 550
  300. /* C' and M' are part of the Blanking Duty Cycle computation */
  301. #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
  302. #define GTF_M_PRIME (GTF_K * GTF_M / 256)
  303. struct drm_display_mode *drm_mode;
  304. unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
  305. int top_margin, bottom_margin;
  306. int interlace;
  307. unsigned int hfreq_est;
  308. int vsync_plus_bp, vback_porch;
  309. unsigned int vtotal_lines, vfieldrate_est, hperiod;
  310. unsigned int vfield_rate, vframe_rate;
  311. int left_margin, right_margin;
  312. unsigned int total_active_pixels, ideal_duty_cycle;
  313. unsigned int hblank, total_pixels, pixel_freq;
  314. int hsync, hfront_porch, vodd_front_porch_lines;
  315. unsigned int tmp1, tmp2;
  316. drm_mode = drm_mode_create(dev);
  317. if (!drm_mode)
  318. return NULL;
  319. /* 1. In order to give correct results, the number of horizontal
  320. * pixels requested is first processed to ensure that it is divisible
  321. * by the character size, by rounding it to the nearest character
  322. * cell boundary:
  323. */
  324. hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  325. hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
  326. /* 2. If interlace is requested, the number of vertical lines assumed
  327. * by the calculation must be halved, as the computation calculates
  328. * the number of vertical lines per field.
  329. */
  330. if (interlaced)
  331. vdisplay_rnd = vdisplay / 2;
  332. else
  333. vdisplay_rnd = vdisplay;
  334. /* 3. Find the frame rate required: */
  335. if (interlaced)
  336. vfieldrate_rqd = vrefresh * 2;
  337. else
  338. vfieldrate_rqd = vrefresh;
  339. /* 4. Find number of lines in Top margin: */
  340. top_margin = 0;
  341. if (margins)
  342. top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  343. 1000;
  344. /* 5. Find number of lines in bottom margin: */
  345. bottom_margin = top_margin;
  346. /* 6. If interlace is required, then set variable interlace: */
  347. if (interlaced)
  348. interlace = 1;
  349. else
  350. interlace = 0;
  351. /* 7. Estimate the Horizontal frequency */
  352. {
  353. tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
  354. tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
  355. 2 + interlace;
  356. hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
  357. }
  358. /* 8. Find the number of lines in V sync + back porch */
  359. /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
  360. vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
  361. vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
  362. /* 9. Find the number of lines in V back porch alone: */
  363. vback_porch = vsync_plus_bp - V_SYNC_RQD;
  364. /* 10. Find the total number of lines in Vertical field period: */
  365. vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
  366. vsync_plus_bp + GTF_MIN_V_PORCH;
  367. /* 11. Estimate the Vertical field frequency: */
  368. vfieldrate_est = hfreq_est / vtotal_lines;
  369. /* 12. Find the actual horizontal period: */
  370. hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
  371. /* 13. Find the actual Vertical field frequency: */
  372. vfield_rate = hfreq_est / vtotal_lines;
  373. /* 14. Find the Vertical frame frequency: */
  374. if (interlaced)
  375. vframe_rate = vfield_rate / 2;
  376. else
  377. vframe_rate = vfield_rate;
  378. /* 15. Find number of pixels in left margin: */
  379. if (margins)
  380. left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  381. 1000;
  382. else
  383. left_margin = 0;
  384. /* 16.Find number of pixels in right margin: */
  385. right_margin = left_margin;
  386. /* 17.Find total number of active pixels in image and left and right */
  387. total_active_pixels = hdisplay_rnd + left_margin + right_margin;
  388. /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
  389. ideal_duty_cycle = GTF_C_PRIME * 1000 -
  390. (GTF_M_PRIME * 1000000 / hfreq_est);
  391. /* 19.Find the number of pixels in the blanking time to the nearest
  392. * double character cell: */
  393. hblank = total_active_pixels * ideal_duty_cycle /
  394. (100000 - ideal_duty_cycle);
  395. hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
  396. hblank = hblank * 2 * GTF_CELL_GRAN;
  397. /* 20.Find total number of pixels: */
  398. total_pixels = total_active_pixels + hblank;
  399. /* 21.Find pixel clock frequency: */
  400. pixel_freq = total_pixels * hfreq_est / 1000;
  401. /* Stage 1 computations are now complete; I should really pass
  402. * the results to another function and do the Stage 2 computations,
  403. * but I only need a few more values so I'll just append the
  404. * computations here for now */
  405. /* 17. Find the number of pixels in the horizontal sync period: */
  406. hsync = H_SYNC_PERCENT * total_pixels / 100;
  407. hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  408. hsync = hsync * GTF_CELL_GRAN;
  409. /* 18. Find the number of pixels in horizontal front porch period */
  410. hfront_porch = hblank / 2 - hsync;
  411. /* 36. Find the number of lines in the odd front porch period: */
  412. vodd_front_porch_lines = GTF_MIN_V_PORCH ;
  413. /* finally, pack the results in the mode struct */
  414. drm_mode->hdisplay = hdisplay_rnd;
  415. drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
  416. drm_mode->hsync_end = drm_mode->hsync_start + hsync;
  417. drm_mode->htotal = total_pixels;
  418. drm_mode->vdisplay = vdisplay_rnd;
  419. drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
  420. drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
  421. drm_mode->vtotal = vtotal_lines;
  422. drm_mode->clock = pixel_freq;
  423. if (interlaced) {
  424. drm_mode->vtotal *= 2;
  425. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  426. }
  427. drm_mode_set_name(drm_mode);
  428. if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
  429. drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
  430. else
  431. drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
  432. return drm_mode;
  433. }
  434. EXPORT_SYMBOL(drm_gtf_mode_complex);
  435. /**
  436. * drm_gtf_mode - create the modeline based on GTF algorithm
  437. *
  438. * @dev :drm device
  439. * @hdisplay :hdisplay size
  440. * @vdisplay :vdisplay size
  441. * @vrefresh :vrefresh rate.
  442. * @interlaced :whether the interlace is supported
  443. * @margins :whether the margin is supported
  444. *
  445. * LOCKING.
  446. * none.
  447. *
  448. * return the modeline based on GTF algorithm
  449. *
  450. * This function is to create the modeline based on the GTF algorithm.
  451. * Generalized Timing Formula is derived from:
  452. * GTF Spreadsheet by Andy Morrish (1/5/97)
  453. * available at http://www.vesa.org
  454. *
  455. * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
  456. * What I have done is to translate it by using integer calculation.
  457. * I also refer to the function of fb_get_mode in the file of
  458. * drivers/video/fbmon.c
  459. *
  460. * Standard GTF parameters:
  461. * M = 600
  462. * C = 40
  463. * K = 128
  464. * J = 20
  465. */
  466. struct drm_display_mode *
  467. drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
  468. bool lace, int margins)
  469. {
  470. return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh, lace,
  471. margins, 600, 40 * 2, 128, 20 * 2);
  472. }
  473. EXPORT_SYMBOL(drm_gtf_mode);
  474. #ifdef CONFIG_VIDEOMODE_HELPERS
  475. int drm_display_mode_from_videomode(const struct videomode *vm,
  476. struct drm_display_mode *dmode)
  477. {
  478. dmode->hdisplay = vm->hactive;
  479. dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
  480. dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
  481. dmode->htotal = dmode->hsync_end + vm->hback_porch;
  482. dmode->vdisplay = vm->vactive;
  483. dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
  484. dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
  485. dmode->vtotal = dmode->vsync_end + vm->vback_porch;
  486. dmode->clock = vm->pixelclock / 1000;
  487. dmode->flags = 0;
  488. if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
  489. dmode->flags |= DRM_MODE_FLAG_PHSYNC;
  490. else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
  491. dmode->flags |= DRM_MODE_FLAG_NHSYNC;
  492. if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
  493. dmode->flags |= DRM_MODE_FLAG_PVSYNC;
  494. else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
  495. dmode->flags |= DRM_MODE_FLAG_NVSYNC;
  496. if (vm->flags & DISPLAY_FLAGS_INTERLACED)
  497. dmode->flags |= DRM_MODE_FLAG_INTERLACE;
  498. if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
  499. dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
  500. if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
  501. dmode->flags |= DRM_MODE_FLAG_DBLCLK;
  502. drm_mode_set_name(dmode);
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
  506. #ifdef CONFIG_OF
  507. /**
  508. * of_get_drm_display_mode - get a drm_display_mode from devicetree
  509. * @np: device_node with the timing specification
  510. * @dmode: will be set to the return value
  511. * @index: index into the list of display timings in devicetree
  512. *
  513. * This function is expensive and should only be used, if only one mode is to be
  514. * read from DT. To get multiple modes start with of_get_display_timings and
  515. * work with that instead.
  516. */
  517. int of_get_drm_display_mode(struct device_node *np,
  518. struct drm_display_mode *dmode, int index)
  519. {
  520. struct videomode vm;
  521. int ret;
  522. ret = of_get_videomode(np, &vm, index);
  523. if (ret)
  524. return ret;
  525. drm_display_mode_from_videomode(&vm, dmode);
  526. pr_debug("%s: got %dx%d display mode from %s\n",
  527. of_node_full_name(np), vm.hactive, vm.vactive, np->name);
  528. drm_mode_debug_printmodeline(dmode);
  529. return 0;
  530. }
  531. EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
  532. #endif /* CONFIG_OF */
  533. #endif /* CONFIG_VIDEOMODE_HELPERS */
  534. /**
  535. * drm_mode_set_name - set the name on a mode
  536. * @mode: name will be set in this mode
  537. *
  538. * LOCKING:
  539. * None.
  540. *
  541. * Set the name of @mode to a standard format.
  542. */
  543. void drm_mode_set_name(struct drm_display_mode *mode)
  544. {
  545. bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  546. snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
  547. mode->hdisplay, mode->vdisplay,
  548. interlaced ? "i" : "");
  549. }
  550. EXPORT_SYMBOL(drm_mode_set_name);
  551. /**
  552. * drm_mode_width - get the width of a mode
  553. * @mode: mode
  554. *
  555. * LOCKING:
  556. * None.
  557. *
  558. * Return @mode's width (hdisplay) value.
  559. *
  560. * FIXME: is this needed?
  561. *
  562. * RETURNS:
  563. * @mode->hdisplay
  564. */
  565. int drm_mode_width(const struct drm_display_mode *mode)
  566. {
  567. return mode->hdisplay;
  568. }
  569. EXPORT_SYMBOL(drm_mode_width);
  570. /**
  571. * drm_mode_height - get the height of a mode
  572. * @mode: mode
  573. *
  574. * LOCKING:
  575. * None.
  576. *
  577. * Return @mode's height (vdisplay) value.
  578. *
  579. * FIXME: is this needed?
  580. *
  581. * RETURNS:
  582. * @mode->vdisplay
  583. */
  584. int drm_mode_height(const struct drm_display_mode *mode)
  585. {
  586. return mode->vdisplay;
  587. }
  588. EXPORT_SYMBOL(drm_mode_height);
  589. /** drm_mode_hsync - get the hsync of a mode
  590. * @mode: mode
  591. *
  592. * LOCKING:
  593. * None.
  594. *
  595. * Return @modes's hsync rate in kHz, rounded to the nearest int.
  596. */
  597. int drm_mode_hsync(const struct drm_display_mode *mode)
  598. {
  599. unsigned int calc_val;
  600. if (mode->hsync)
  601. return mode->hsync;
  602. if (mode->htotal < 0)
  603. return 0;
  604. calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
  605. calc_val += 500; /* round to 1000Hz */
  606. calc_val /= 1000; /* truncate to kHz */
  607. return calc_val;
  608. }
  609. EXPORT_SYMBOL(drm_mode_hsync);
  610. /**
  611. * drm_mode_vrefresh - get the vrefresh of a mode
  612. * @mode: mode
  613. *
  614. * LOCKING:
  615. * None.
  616. *
  617. * Return @mode's vrefresh rate in Hz or calculate it if necessary.
  618. *
  619. * FIXME: why is this needed? shouldn't vrefresh be set already?
  620. *
  621. * RETURNS:
  622. * Vertical refresh rate. It will be the result of actual value plus 0.5.
  623. * If it is 70.288, it will return 70Hz.
  624. * If it is 59.6, it will return 60Hz.
  625. */
  626. int drm_mode_vrefresh(const struct drm_display_mode *mode)
  627. {
  628. int refresh = 0;
  629. unsigned int calc_val;
  630. if (mode->vrefresh > 0)
  631. refresh = mode->vrefresh;
  632. else if (mode->htotal > 0 && mode->vtotal > 0) {
  633. int vtotal;
  634. vtotal = mode->vtotal;
  635. /* work out vrefresh the value will be x1000 */
  636. calc_val = (mode->clock * 1000);
  637. calc_val /= mode->htotal;
  638. refresh = (calc_val + vtotal / 2) / vtotal;
  639. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  640. refresh *= 2;
  641. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  642. refresh /= 2;
  643. if (mode->vscan > 1)
  644. refresh /= mode->vscan;
  645. }
  646. return refresh;
  647. }
  648. EXPORT_SYMBOL(drm_mode_vrefresh);
  649. /**
  650. * drm_mode_set_crtcinfo - set CRTC modesetting parameters
  651. * @p: mode
  652. * @adjust_flags: a combination of adjustment flags
  653. *
  654. * LOCKING:
  655. * None.
  656. *
  657. * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
  658. *
  659. * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
  660. * interlaced modes.
  661. * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
  662. * buffers containing two eyes (only adjust the timings when needed, eg. for
  663. * "frame packing" or "side by side full").
  664. */
  665. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  666. {
  667. if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
  668. return;
  669. p->crtc_clock = p->clock;
  670. p->crtc_hdisplay = p->hdisplay;
  671. p->crtc_hsync_start = p->hsync_start;
  672. p->crtc_hsync_end = p->hsync_end;
  673. p->crtc_htotal = p->htotal;
  674. p->crtc_hskew = p->hskew;
  675. p->crtc_vdisplay = p->vdisplay;
  676. p->crtc_vsync_start = p->vsync_start;
  677. p->crtc_vsync_end = p->vsync_end;
  678. p->crtc_vtotal = p->vtotal;
  679. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  680. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  681. p->crtc_vdisplay /= 2;
  682. p->crtc_vsync_start /= 2;
  683. p->crtc_vsync_end /= 2;
  684. p->crtc_vtotal /= 2;
  685. }
  686. }
  687. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  688. p->crtc_vdisplay *= 2;
  689. p->crtc_vsync_start *= 2;
  690. p->crtc_vsync_end *= 2;
  691. p->crtc_vtotal *= 2;
  692. }
  693. if (p->vscan > 1) {
  694. p->crtc_vdisplay *= p->vscan;
  695. p->crtc_vsync_start *= p->vscan;
  696. p->crtc_vsync_end *= p->vscan;
  697. p->crtc_vtotal *= p->vscan;
  698. }
  699. if (adjust_flags & CRTC_STEREO_DOUBLE) {
  700. unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
  701. switch (layout) {
  702. case DRM_MODE_FLAG_3D_FRAME_PACKING:
  703. p->crtc_clock *= 2;
  704. p->crtc_vdisplay += p->crtc_vtotal;
  705. p->crtc_vsync_start += p->crtc_vtotal;
  706. p->crtc_vsync_end += p->crtc_vtotal;
  707. p->crtc_vtotal += p->crtc_vtotal;
  708. break;
  709. }
  710. }
  711. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  712. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  713. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  714. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  715. }
  716. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  717. /**
  718. * drm_mode_copy - copy the mode
  719. * @dst: mode to overwrite
  720. * @src: mode to copy
  721. *
  722. * LOCKING:
  723. * None.
  724. *
  725. * Copy an existing mode into another mode, preserving the object id and
  726. * list head of the destination mode.
  727. */
  728. void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
  729. {
  730. int id = dst->base.id;
  731. struct list_head head = dst->head;
  732. *dst = *src;
  733. dst->base.id = id;
  734. dst->head = head;
  735. }
  736. EXPORT_SYMBOL(drm_mode_copy);
  737. /**
  738. * drm_mode_duplicate - allocate and duplicate an existing mode
  739. * @m: mode to duplicate
  740. *
  741. * LOCKING:
  742. * None.
  743. *
  744. * Just allocate a new mode, copy the existing mode into it, and return
  745. * a pointer to it. Used to create new instances of established modes.
  746. */
  747. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  748. const struct drm_display_mode *mode)
  749. {
  750. struct drm_display_mode *nmode;
  751. nmode = drm_mode_create(dev);
  752. if (!nmode)
  753. return NULL;
  754. drm_mode_copy(nmode, mode);
  755. return nmode;
  756. }
  757. EXPORT_SYMBOL(drm_mode_duplicate);
  758. /**
  759. * drm_mode_equal - test modes for equality
  760. * @mode1: first mode
  761. * @mode2: second mode
  762. *
  763. * LOCKING:
  764. * None.
  765. *
  766. * Check to see if @mode1 and @mode2 are equivalent.
  767. *
  768. * RETURNS:
  769. * True if the modes are equal, false otherwise.
  770. */
  771. bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
  772. {
  773. /* do clock check convert to PICOS so fb modes get matched
  774. * the same */
  775. if (mode1->clock && mode2->clock) {
  776. if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
  777. return false;
  778. } else if (mode1->clock != mode2->clock)
  779. return false;
  780. if ((mode1->flags & DRM_MODE_FLAG_3D_MASK) !=
  781. (mode2->flags & DRM_MODE_FLAG_3D_MASK))
  782. return false;
  783. return drm_mode_equal_no_clocks_no_stereo(mode1, mode2);
  784. }
  785. EXPORT_SYMBOL(drm_mode_equal);
  786. /**
  787. * drm_mode_equal_no_clocks_no_stereo - test modes for equality
  788. * @mode1: first mode
  789. * @mode2: second mode
  790. *
  791. * LOCKING:
  792. * None.
  793. *
  794. * Check to see if @mode1 and @mode2 are equivalent, but
  795. * don't check the pixel clocks nor the stereo layout.
  796. *
  797. * RETURNS:
  798. * True if the modes are equal, false otherwise.
  799. */
  800. bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
  801. const struct drm_display_mode *mode2)
  802. {
  803. if (mode1->hdisplay == mode2->hdisplay &&
  804. mode1->hsync_start == mode2->hsync_start &&
  805. mode1->hsync_end == mode2->hsync_end &&
  806. mode1->htotal == mode2->htotal &&
  807. mode1->hskew == mode2->hskew &&
  808. mode1->vdisplay == mode2->vdisplay &&
  809. mode1->vsync_start == mode2->vsync_start &&
  810. mode1->vsync_end == mode2->vsync_end &&
  811. mode1->vtotal == mode2->vtotal &&
  812. mode1->vscan == mode2->vscan &&
  813. (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
  814. (mode2->flags & ~DRM_MODE_FLAG_3D_MASK))
  815. return true;
  816. return false;
  817. }
  818. EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
  819. /**
  820. * drm_mode_validate_size - make sure modes adhere to size constraints
  821. * @dev: DRM device
  822. * @mode_list: list of modes to check
  823. * @maxX: maximum width
  824. * @maxY: maximum height
  825. * @maxPitch: max pitch
  826. *
  827. * LOCKING:
  828. * Caller must hold a lock protecting @mode_list.
  829. *
  830. * The DRM device (@dev) has size and pitch limits. Here we validate the
  831. * modes we probed for @dev against those limits and set their status as
  832. * necessary.
  833. */
  834. void drm_mode_validate_size(struct drm_device *dev,
  835. struct list_head *mode_list,
  836. int maxX, int maxY, int maxPitch)
  837. {
  838. struct drm_display_mode *mode;
  839. list_for_each_entry(mode, mode_list, head) {
  840. if (maxPitch > 0 && mode->hdisplay > maxPitch)
  841. mode->status = MODE_BAD_WIDTH;
  842. if (maxX > 0 && mode->hdisplay > maxX)
  843. mode->status = MODE_VIRTUAL_X;
  844. if (maxY > 0 && mode->vdisplay > maxY)
  845. mode->status = MODE_VIRTUAL_Y;
  846. }
  847. }
  848. EXPORT_SYMBOL(drm_mode_validate_size);
  849. /**
  850. * drm_mode_prune_invalid - remove invalid modes from mode list
  851. * @dev: DRM device
  852. * @mode_list: list of modes to check
  853. * @verbose: be verbose about it
  854. *
  855. * LOCKING:
  856. * Caller must hold a lock protecting @mode_list.
  857. *
  858. * Once mode list generation is complete, a caller can use this routine to
  859. * remove invalid modes from a mode list. If any of the modes have a
  860. * status other than %MODE_OK, they are removed from @mode_list and freed.
  861. */
  862. void drm_mode_prune_invalid(struct drm_device *dev,
  863. struct list_head *mode_list, bool verbose)
  864. {
  865. struct drm_display_mode *mode, *t;
  866. list_for_each_entry_safe(mode, t, mode_list, head) {
  867. if (mode->status != MODE_OK) {
  868. list_del(&mode->head);
  869. if (verbose) {
  870. drm_mode_debug_printmodeline(mode);
  871. DRM_DEBUG_KMS("Not using %s mode %d\n",
  872. mode->name, mode->status);
  873. }
  874. drm_mode_destroy(dev, mode);
  875. }
  876. }
  877. }
  878. EXPORT_SYMBOL(drm_mode_prune_invalid);
  879. /**
  880. * drm_mode_compare - compare modes for favorability
  881. * @priv: unused
  882. * @lh_a: list_head for first mode
  883. * @lh_b: list_head for second mode
  884. *
  885. * LOCKING:
  886. * None.
  887. *
  888. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  889. * which is better.
  890. *
  891. * RETURNS:
  892. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  893. * positive if @lh_b is better than @lh_a.
  894. */
  895. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  896. {
  897. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  898. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  899. int diff;
  900. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  901. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  902. if (diff)
  903. return diff;
  904. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  905. if (diff)
  906. return diff;
  907. diff = b->vrefresh - a->vrefresh;
  908. if (diff)
  909. return diff;
  910. diff = b->clock - a->clock;
  911. return diff;
  912. }
  913. /**
  914. * drm_mode_sort - sort mode list
  915. * @mode_list: list to sort
  916. *
  917. * LOCKING:
  918. * Caller must hold a lock protecting @mode_list.
  919. *
  920. * Sort @mode_list by favorability, putting good modes first.
  921. */
  922. void drm_mode_sort(struct list_head *mode_list)
  923. {
  924. list_sort(NULL, mode_list, drm_mode_compare);
  925. }
  926. EXPORT_SYMBOL(drm_mode_sort);
  927. /**
  928. * drm_mode_connector_list_update - update the mode list for the connector
  929. * @connector: the connector to update
  930. *
  931. * LOCKING:
  932. * Caller must hold a lock protecting @mode_list.
  933. *
  934. * This moves the modes from the @connector probed_modes list
  935. * to the actual mode list. It compares the probed mode against the current
  936. * list and only adds different modes. All modes unverified after this point
  937. * will be removed by the prune invalid modes.
  938. */
  939. void drm_mode_connector_list_update(struct drm_connector *connector)
  940. {
  941. struct drm_display_mode *mode;
  942. struct drm_display_mode *pmode, *pt;
  943. int found_it;
  944. list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
  945. head) {
  946. found_it = 0;
  947. /* go through current modes checking for the new probed mode */
  948. list_for_each_entry(mode, &connector->modes, head) {
  949. if (drm_mode_equal(pmode, mode)) {
  950. found_it = 1;
  951. /* if equal delete the probed mode */
  952. mode->status = pmode->status;
  953. /* Merge type bits together */
  954. mode->type |= pmode->type;
  955. list_del(&pmode->head);
  956. drm_mode_destroy(connector->dev, pmode);
  957. break;
  958. }
  959. }
  960. if (!found_it) {
  961. list_move_tail(&pmode->head, &connector->modes);
  962. }
  963. }
  964. }
  965. EXPORT_SYMBOL(drm_mode_connector_list_update);
  966. /**
  967. * drm_mode_parse_command_line_for_connector - parse command line for connector
  968. * @mode_option - per connector mode option
  969. * @connector - connector to parse line for
  970. *
  971. * This parses the connector specific then generic command lines for
  972. * modes and options to configure the connector.
  973. *
  974. * This uses the same parameters as the fb modedb.c, except for extra
  975. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  976. *
  977. * enable/enable Digital/disable bit at the end
  978. */
  979. bool drm_mode_parse_command_line_for_connector(const char *mode_option,
  980. struct drm_connector *connector,
  981. struct drm_cmdline_mode *mode)
  982. {
  983. const char *name;
  984. unsigned int namelen;
  985. bool res_specified = false, bpp_specified = false, refresh_specified = false;
  986. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  987. bool yres_specified = false, cvt = false, rb = false;
  988. bool interlace = false, margins = false, was_digit = false;
  989. int i;
  990. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  991. #ifdef CONFIG_FB
  992. if (!mode_option)
  993. mode_option = fb_mode_option;
  994. #endif
  995. if (!mode_option) {
  996. mode->specified = false;
  997. return false;
  998. }
  999. name = mode_option;
  1000. namelen = strlen(name);
  1001. for (i = namelen-1; i >= 0; i--) {
  1002. switch (name[i]) {
  1003. case '@':
  1004. if (!refresh_specified && !bpp_specified &&
  1005. !yres_specified && !cvt && !rb && was_digit) {
  1006. refresh = simple_strtol(&name[i+1], NULL, 10);
  1007. refresh_specified = true;
  1008. was_digit = false;
  1009. } else
  1010. goto done;
  1011. break;
  1012. case '-':
  1013. if (!bpp_specified && !yres_specified && !cvt &&
  1014. !rb && was_digit) {
  1015. bpp = simple_strtol(&name[i+1], NULL, 10);
  1016. bpp_specified = true;
  1017. was_digit = false;
  1018. } else
  1019. goto done;
  1020. break;
  1021. case 'x':
  1022. if (!yres_specified && was_digit) {
  1023. yres = simple_strtol(&name[i+1], NULL, 10);
  1024. yres_specified = true;
  1025. was_digit = false;
  1026. } else
  1027. goto done;
  1028. break;
  1029. case '0' ... '9':
  1030. was_digit = true;
  1031. break;
  1032. case 'M':
  1033. if (yres_specified || cvt || was_digit)
  1034. goto done;
  1035. cvt = true;
  1036. break;
  1037. case 'R':
  1038. if (yres_specified || cvt || rb || was_digit)
  1039. goto done;
  1040. rb = true;
  1041. break;
  1042. case 'm':
  1043. if (cvt || yres_specified || was_digit)
  1044. goto done;
  1045. margins = true;
  1046. break;
  1047. case 'i':
  1048. if (cvt || yres_specified || was_digit)
  1049. goto done;
  1050. interlace = true;
  1051. break;
  1052. case 'e':
  1053. if (yres_specified || bpp_specified || refresh_specified ||
  1054. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1055. goto done;
  1056. force = DRM_FORCE_ON;
  1057. break;
  1058. case 'D':
  1059. if (yres_specified || bpp_specified || refresh_specified ||
  1060. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1061. goto done;
  1062. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  1063. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  1064. force = DRM_FORCE_ON;
  1065. else
  1066. force = DRM_FORCE_ON_DIGITAL;
  1067. break;
  1068. case 'd':
  1069. if (yres_specified || bpp_specified || refresh_specified ||
  1070. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1071. goto done;
  1072. force = DRM_FORCE_OFF;
  1073. break;
  1074. default:
  1075. goto done;
  1076. }
  1077. }
  1078. if (i < 0 && yres_specified) {
  1079. char *ch;
  1080. xres = simple_strtol(name, &ch, 10);
  1081. if ((ch != NULL) && (*ch == 'x'))
  1082. res_specified = true;
  1083. else
  1084. i = ch - name;
  1085. } else if (!yres_specified && was_digit) {
  1086. /* catch mode that begins with digits but has no 'x' */
  1087. i = 0;
  1088. }
  1089. done:
  1090. if (i >= 0) {
  1091. printk(KERN_WARNING
  1092. "parse error at position %i in video mode '%s'\n",
  1093. i, name);
  1094. mode->specified = false;
  1095. return false;
  1096. }
  1097. if (res_specified) {
  1098. mode->specified = true;
  1099. mode->xres = xres;
  1100. mode->yres = yres;
  1101. }
  1102. if (refresh_specified) {
  1103. mode->refresh_specified = true;
  1104. mode->refresh = refresh;
  1105. }
  1106. if (bpp_specified) {
  1107. mode->bpp_specified = true;
  1108. mode->bpp = bpp;
  1109. }
  1110. mode->rb = rb;
  1111. mode->cvt = cvt;
  1112. mode->interlace = interlace;
  1113. mode->margins = margins;
  1114. mode->force = force;
  1115. return true;
  1116. }
  1117. EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
  1118. struct drm_display_mode *
  1119. drm_mode_create_from_cmdline_mode(struct drm_device *dev,
  1120. struct drm_cmdline_mode *cmd)
  1121. {
  1122. struct drm_display_mode *mode;
  1123. if (cmd->cvt)
  1124. mode = drm_cvt_mode(dev,
  1125. cmd->xres, cmd->yres,
  1126. cmd->refresh_specified ? cmd->refresh : 60,
  1127. cmd->rb, cmd->interlace,
  1128. cmd->margins);
  1129. else
  1130. mode = drm_gtf_mode(dev,
  1131. cmd->xres, cmd->yres,
  1132. cmd->refresh_specified ? cmd->refresh : 60,
  1133. cmd->interlace,
  1134. cmd->margins);
  1135. if (!mode)
  1136. return NULL;
  1137. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1138. return mode;
  1139. }
  1140. EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);