drm_modes.c 36 KB

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