drm_modes.c 36 KB

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