drm_modes.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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_list_concat - move modes from one list to another
  553. * @head: source list
  554. * @new: dst list
  555. *
  556. * LOCKING:
  557. * Caller must ensure both lists are locked.
  558. *
  559. * Move all the modes from @head to @new.
  560. */
  561. void drm_mode_list_concat(struct list_head *head, struct list_head *new)
  562. {
  563. struct list_head *entry, *tmp;
  564. list_for_each_safe(entry, tmp, head) {
  565. list_move_tail(entry, new);
  566. }
  567. }
  568. EXPORT_SYMBOL(drm_mode_list_concat);
  569. /**
  570. * drm_mode_width - get the width of a mode
  571. * @mode: mode
  572. *
  573. * LOCKING:
  574. * None.
  575. *
  576. * Return @mode's width (hdisplay) value.
  577. *
  578. * FIXME: is this needed?
  579. *
  580. * RETURNS:
  581. * @mode->hdisplay
  582. */
  583. int drm_mode_width(const struct drm_display_mode *mode)
  584. {
  585. return mode->hdisplay;
  586. }
  587. EXPORT_SYMBOL(drm_mode_width);
  588. /**
  589. * drm_mode_height - get the height of a mode
  590. * @mode: mode
  591. *
  592. * LOCKING:
  593. * None.
  594. *
  595. * Return @mode's height (vdisplay) value.
  596. *
  597. * FIXME: is this needed?
  598. *
  599. * RETURNS:
  600. * @mode->vdisplay
  601. */
  602. int drm_mode_height(const struct drm_display_mode *mode)
  603. {
  604. return mode->vdisplay;
  605. }
  606. EXPORT_SYMBOL(drm_mode_height);
  607. /** drm_mode_hsync - get the hsync of a mode
  608. * @mode: mode
  609. *
  610. * LOCKING:
  611. * None.
  612. *
  613. * Return @modes's hsync rate in kHz, rounded to the nearest int.
  614. */
  615. int drm_mode_hsync(const struct drm_display_mode *mode)
  616. {
  617. unsigned int calc_val;
  618. if (mode->hsync)
  619. return mode->hsync;
  620. if (mode->htotal < 0)
  621. return 0;
  622. calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
  623. calc_val += 500; /* round to 1000Hz */
  624. calc_val /= 1000; /* truncate to kHz */
  625. return calc_val;
  626. }
  627. EXPORT_SYMBOL(drm_mode_hsync);
  628. /**
  629. * drm_mode_vrefresh - get the vrefresh of a mode
  630. * @mode: mode
  631. *
  632. * LOCKING:
  633. * None.
  634. *
  635. * Return @mode's vrefresh rate in Hz or calculate it if necessary.
  636. *
  637. * FIXME: why is this needed? shouldn't vrefresh be set already?
  638. *
  639. * RETURNS:
  640. * Vertical refresh rate. It will be the result of actual value plus 0.5.
  641. * If it is 70.288, it will return 70Hz.
  642. * If it is 59.6, it will return 60Hz.
  643. */
  644. int drm_mode_vrefresh(const struct drm_display_mode *mode)
  645. {
  646. int refresh = 0;
  647. unsigned int calc_val;
  648. if (mode->vrefresh > 0)
  649. refresh = mode->vrefresh;
  650. else if (mode->htotal > 0 && mode->vtotal > 0) {
  651. int vtotal;
  652. vtotal = mode->vtotal;
  653. /* work out vrefresh the value will be x1000 */
  654. calc_val = (mode->clock * 1000);
  655. calc_val /= mode->htotal;
  656. refresh = (calc_val + vtotal / 2) / vtotal;
  657. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  658. refresh *= 2;
  659. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  660. refresh /= 2;
  661. if (mode->vscan > 1)
  662. refresh /= mode->vscan;
  663. }
  664. return refresh;
  665. }
  666. EXPORT_SYMBOL(drm_mode_vrefresh);
  667. /**
  668. * drm_mode_set_crtcinfo - set CRTC modesetting parameters
  669. * @p: mode
  670. * @adjust_flags: unused? (FIXME)
  671. *
  672. * LOCKING:
  673. * None.
  674. *
  675. * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
  676. */
  677. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  678. {
  679. if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
  680. return;
  681. p->crtc_hdisplay = p->hdisplay;
  682. p->crtc_hsync_start = p->hsync_start;
  683. p->crtc_hsync_end = p->hsync_end;
  684. p->crtc_htotal = p->htotal;
  685. p->crtc_hskew = p->hskew;
  686. p->crtc_vdisplay = p->vdisplay;
  687. p->crtc_vsync_start = p->vsync_start;
  688. p->crtc_vsync_end = p->vsync_end;
  689. p->crtc_vtotal = p->vtotal;
  690. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  691. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  692. p->crtc_vdisplay /= 2;
  693. p->crtc_vsync_start /= 2;
  694. p->crtc_vsync_end /= 2;
  695. p->crtc_vtotal /= 2;
  696. }
  697. }
  698. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  699. p->crtc_vdisplay *= 2;
  700. p->crtc_vsync_start *= 2;
  701. p->crtc_vsync_end *= 2;
  702. p->crtc_vtotal *= 2;
  703. }
  704. if (p->vscan > 1) {
  705. p->crtc_vdisplay *= p->vscan;
  706. p->crtc_vsync_start *= p->vscan;
  707. p->crtc_vsync_end *= p->vscan;
  708. p->crtc_vtotal *= p->vscan;
  709. }
  710. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  711. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  712. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  713. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  714. }
  715. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  716. /**
  717. * drm_mode_copy - copy the mode
  718. * @dst: mode to overwrite
  719. * @src: mode to copy
  720. *
  721. * LOCKING:
  722. * None.
  723. *
  724. * Copy an existing mode into another mode, preserving the object id and
  725. * list head of the destination mode.
  726. */
  727. void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
  728. {
  729. int id = dst->base.id;
  730. struct list_head head = dst->head;
  731. *dst = *src;
  732. dst->base.id = id;
  733. dst->head = head;
  734. }
  735. EXPORT_SYMBOL(drm_mode_copy);
  736. /**
  737. * drm_mode_duplicate - allocate and duplicate an existing mode
  738. * @m: mode to duplicate
  739. *
  740. * LOCKING:
  741. * None.
  742. *
  743. * Just allocate a new mode, copy the existing mode into it, and return
  744. * a pointer to it. Used to create new instances of established modes.
  745. */
  746. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  747. const struct drm_display_mode *mode)
  748. {
  749. struct drm_display_mode *nmode;
  750. nmode = drm_mode_create(dev);
  751. if (!nmode)
  752. return NULL;
  753. drm_mode_copy(nmode, mode);
  754. return nmode;
  755. }
  756. EXPORT_SYMBOL(drm_mode_duplicate);
  757. /**
  758. * drm_mode_equal - test modes for equality
  759. * @mode1: first mode
  760. * @mode2: second mode
  761. *
  762. * LOCKING:
  763. * None.
  764. *
  765. * Check to see if @mode1 and @mode2 are equivalent.
  766. *
  767. * RETURNS:
  768. * True if the modes are equal, false otherwise.
  769. */
  770. bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
  771. {
  772. /* do clock check convert to PICOS so fb modes get matched
  773. * the same */
  774. if (mode1->clock && mode2->clock) {
  775. if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
  776. return false;
  777. } else if (mode1->clock != mode2->clock)
  778. return false;
  779. return drm_mode_equal_no_clocks(mode1, mode2);
  780. }
  781. EXPORT_SYMBOL(drm_mode_equal);
  782. /**
  783. * drm_mode_equal_no_clocks - test modes for equality
  784. * @mode1: first mode
  785. * @mode2: second mode
  786. *
  787. * LOCKING:
  788. * None.
  789. *
  790. * Check to see if @mode1 and @mode2 are equivalent, but
  791. * don't check the pixel clocks.
  792. *
  793. * RETURNS:
  794. * True if the modes are equal, false otherwise.
  795. */
  796. bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
  797. {
  798. if (mode1->hdisplay == mode2->hdisplay &&
  799. mode1->hsync_start == mode2->hsync_start &&
  800. mode1->hsync_end == mode2->hsync_end &&
  801. mode1->htotal == mode2->htotal &&
  802. mode1->hskew == mode2->hskew &&
  803. mode1->vdisplay == mode2->vdisplay &&
  804. mode1->vsync_start == mode2->vsync_start &&
  805. mode1->vsync_end == mode2->vsync_end &&
  806. mode1->vtotal == mode2->vtotal &&
  807. mode1->vscan == mode2->vscan &&
  808. mode1->flags == mode2->flags)
  809. return true;
  810. return false;
  811. }
  812. EXPORT_SYMBOL(drm_mode_equal_no_clocks);
  813. /**
  814. * drm_mode_validate_size - make sure modes adhere to size constraints
  815. * @dev: DRM device
  816. * @mode_list: list of modes to check
  817. * @maxX: maximum width
  818. * @maxY: maximum height
  819. * @maxPitch: max pitch
  820. *
  821. * LOCKING:
  822. * Caller must hold a lock protecting @mode_list.
  823. *
  824. * The DRM device (@dev) has size and pitch limits. Here we validate the
  825. * modes we probed for @dev against those limits and set their status as
  826. * necessary.
  827. */
  828. void drm_mode_validate_size(struct drm_device *dev,
  829. struct list_head *mode_list,
  830. int maxX, int maxY, int maxPitch)
  831. {
  832. struct drm_display_mode *mode;
  833. list_for_each_entry(mode, mode_list, head) {
  834. if (maxPitch > 0 && mode->hdisplay > maxPitch)
  835. mode->status = MODE_BAD_WIDTH;
  836. if (maxX > 0 && mode->hdisplay > maxX)
  837. mode->status = MODE_VIRTUAL_X;
  838. if (maxY > 0 && mode->vdisplay > maxY)
  839. mode->status = MODE_VIRTUAL_Y;
  840. }
  841. }
  842. EXPORT_SYMBOL(drm_mode_validate_size);
  843. /**
  844. * drm_mode_prune_invalid - remove invalid modes from mode list
  845. * @dev: DRM device
  846. * @mode_list: list of modes to check
  847. * @verbose: be verbose about it
  848. *
  849. * LOCKING:
  850. * Caller must hold a lock protecting @mode_list.
  851. *
  852. * Once mode list generation is complete, a caller can use this routine to
  853. * remove invalid modes from a mode list. If any of the modes have a
  854. * status other than %MODE_OK, they are removed from @mode_list and freed.
  855. */
  856. void drm_mode_prune_invalid(struct drm_device *dev,
  857. struct list_head *mode_list, bool verbose)
  858. {
  859. struct drm_display_mode *mode, *t;
  860. list_for_each_entry_safe(mode, t, mode_list, head) {
  861. if (mode->status != MODE_OK) {
  862. list_del(&mode->head);
  863. if (verbose) {
  864. drm_mode_debug_printmodeline(mode);
  865. DRM_DEBUG_KMS("Not using %s mode %d\n",
  866. mode->name, mode->status);
  867. }
  868. drm_mode_destroy(dev, mode);
  869. }
  870. }
  871. }
  872. EXPORT_SYMBOL(drm_mode_prune_invalid);
  873. /**
  874. * drm_mode_compare - compare modes for favorability
  875. * @priv: unused
  876. * @lh_a: list_head for first mode
  877. * @lh_b: list_head for second mode
  878. *
  879. * LOCKING:
  880. * None.
  881. *
  882. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  883. * which is better.
  884. *
  885. * RETURNS:
  886. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  887. * positive if @lh_b is better than @lh_a.
  888. */
  889. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  890. {
  891. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  892. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  893. int diff;
  894. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  895. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  896. if (diff)
  897. return diff;
  898. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  899. if (diff)
  900. return diff;
  901. diff = b->vrefresh - a->vrefresh;
  902. if (diff)
  903. return diff;
  904. diff = b->clock - a->clock;
  905. return diff;
  906. }
  907. /**
  908. * drm_mode_sort - sort mode list
  909. * @mode_list: list to sort
  910. *
  911. * LOCKING:
  912. * Caller must hold a lock protecting @mode_list.
  913. *
  914. * Sort @mode_list by favorability, putting good modes first.
  915. */
  916. void drm_mode_sort(struct list_head *mode_list)
  917. {
  918. list_sort(NULL, mode_list, drm_mode_compare);
  919. }
  920. EXPORT_SYMBOL(drm_mode_sort);
  921. /**
  922. * drm_mode_connector_list_update - update the mode list for the connector
  923. * @connector: the connector to update
  924. *
  925. * LOCKING:
  926. * Caller must hold a lock protecting @mode_list.
  927. *
  928. * This moves the modes from the @connector probed_modes list
  929. * to the actual mode list. It compares the probed mode against the current
  930. * list and only adds different modes. All modes unverified after this point
  931. * will be removed by the prune invalid modes.
  932. */
  933. void drm_mode_connector_list_update(struct drm_connector *connector)
  934. {
  935. struct drm_display_mode *mode;
  936. struct drm_display_mode *pmode, *pt;
  937. int found_it;
  938. list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
  939. head) {
  940. found_it = 0;
  941. /* go through current modes checking for the new probed mode */
  942. list_for_each_entry(mode, &connector->modes, head) {
  943. if (drm_mode_equal(pmode, mode)) {
  944. found_it = 1;
  945. /* if equal delete the probed mode */
  946. mode->status = pmode->status;
  947. /* Merge type bits together */
  948. mode->type |= pmode->type;
  949. list_del(&pmode->head);
  950. drm_mode_destroy(connector->dev, pmode);
  951. break;
  952. }
  953. }
  954. if (!found_it) {
  955. list_move_tail(&pmode->head, &connector->modes);
  956. }
  957. }
  958. }
  959. EXPORT_SYMBOL(drm_mode_connector_list_update);
  960. /**
  961. * drm_mode_parse_command_line_for_connector - parse command line for connector
  962. * @mode_option - per connector mode option
  963. * @connector - connector to parse line for
  964. *
  965. * This parses the connector specific then generic command lines for
  966. * modes and options to configure the connector.
  967. *
  968. * This uses the same parameters as the fb modedb.c, except for extra
  969. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  970. *
  971. * enable/enable Digital/disable bit at the end
  972. */
  973. bool drm_mode_parse_command_line_for_connector(const char *mode_option,
  974. struct drm_connector *connector,
  975. struct drm_cmdline_mode *mode)
  976. {
  977. const char *name;
  978. unsigned int namelen;
  979. bool res_specified = false, bpp_specified = false, refresh_specified = false;
  980. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  981. bool yres_specified = false, cvt = false, rb = false;
  982. bool interlace = false, margins = false, was_digit = false;
  983. int i;
  984. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  985. #ifdef CONFIG_FB
  986. if (!mode_option)
  987. mode_option = fb_mode_option;
  988. #endif
  989. if (!mode_option) {
  990. mode->specified = false;
  991. return false;
  992. }
  993. name = mode_option;
  994. namelen = strlen(name);
  995. for (i = namelen-1; i >= 0; i--) {
  996. switch (name[i]) {
  997. case '@':
  998. if (!refresh_specified && !bpp_specified &&
  999. !yres_specified && !cvt && !rb && was_digit) {
  1000. refresh = simple_strtol(&name[i+1], NULL, 10);
  1001. refresh_specified = true;
  1002. was_digit = false;
  1003. } else
  1004. goto done;
  1005. break;
  1006. case '-':
  1007. if (!bpp_specified && !yres_specified && !cvt &&
  1008. !rb && was_digit) {
  1009. bpp = simple_strtol(&name[i+1], NULL, 10);
  1010. bpp_specified = true;
  1011. was_digit = false;
  1012. } else
  1013. goto done;
  1014. break;
  1015. case 'x':
  1016. if (!yres_specified && was_digit) {
  1017. yres = simple_strtol(&name[i+1], NULL, 10);
  1018. yres_specified = true;
  1019. was_digit = false;
  1020. } else
  1021. goto done;
  1022. break;
  1023. case '0' ... '9':
  1024. was_digit = true;
  1025. break;
  1026. case 'M':
  1027. if (yres_specified || cvt || was_digit)
  1028. goto done;
  1029. cvt = true;
  1030. break;
  1031. case 'R':
  1032. if (yres_specified || cvt || rb || was_digit)
  1033. goto done;
  1034. rb = true;
  1035. break;
  1036. case 'm':
  1037. if (cvt || yres_specified || was_digit)
  1038. goto done;
  1039. margins = true;
  1040. break;
  1041. case 'i':
  1042. if (cvt || yres_specified || was_digit)
  1043. goto done;
  1044. interlace = true;
  1045. break;
  1046. case 'e':
  1047. if (yres_specified || bpp_specified || refresh_specified ||
  1048. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1049. goto done;
  1050. force = DRM_FORCE_ON;
  1051. break;
  1052. case 'D':
  1053. if (yres_specified || bpp_specified || refresh_specified ||
  1054. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1055. goto done;
  1056. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  1057. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  1058. force = DRM_FORCE_ON;
  1059. else
  1060. force = DRM_FORCE_ON_DIGITAL;
  1061. break;
  1062. case 'd':
  1063. if (yres_specified || bpp_specified || refresh_specified ||
  1064. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1065. goto done;
  1066. force = DRM_FORCE_OFF;
  1067. break;
  1068. default:
  1069. goto done;
  1070. }
  1071. }
  1072. if (i < 0 && yres_specified) {
  1073. char *ch;
  1074. xres = simple_strtol(name, &ch, 10);
  1075. if ((ch != NULL) && (*ch == 'x'))
  1076. res_specified = true;
  1077. else
  1078. i = ch - name;
  1079. } else if (!yres_specified && was_digit) {
  1080. /* catch mode that begins with digits but has no 'x' */
  1081. i = 0;
  1082. }
  1083. done:
  1084. if (i >= 0) {
  1085. printk(KERN_WARNING
  1086. "parse error at position %i in video mode '%s'\n",
  1087. i, name);
  1088. mode->specified = false;
  1089. return false;
  1090. }
  1091. if (res_specified) {
  1092. mode->specified = true;
  1093. mode->xres = xres;
  1094. mode->yres = yres;
  1095. }
  1096. if (refresh_specified) {
  1097. mode->refresh_specified = true;
  1098. mode->refresh = refresh;
  1099. }
  1100. if (bpp_specified) {
  1101. mode->bpp_specified = true;
  1102. mode->bpp = bpp;
  1103. }
  1104. mode->rb = rb;
  1105. mode->cvt = cvt;
  1106. mode->interlace = interlace;
  1107. mode->margins = margins;
  1108. mode->force = force;
  1109. return true;
  1110. }
  1111. EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
  1112. struct drm_display_mode *
  1113. drm_mode_create_from_cmdline_mode(struct drm_device *dev,
  1114. struct drm_cmdline_mode *cmd)
  1115. {
  1116. struct drm_display_mode *mode;
  1117. if (cmd->cvt)
  1118. mode = drm_cvt_mode(dev,
  1119. cmd->xres, cmd->yres,
  1120. cmd->refresh_specified ? cmd->refresh : 60,
  1121. cmd->rb, cmd->interlace,
  1122. cmd->margins);
  1123. else
  1124. mode = drm_gtf_mode(dev,
  1125. cmd->xres, cmd->yres,
  1126. cmd->refresh_specified ? cmd->refresh : 60,
  1127. cmd->interlace,
  1128. cmd->margins);
  1129. if (!mode)
  1130. return NULL;
  1131. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1132. return mode;
  1133. }
  1134. EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);