drm_modes.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. #if IS_ENABLED(CONFIG_VIDEOMODE)
  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->dmt_flags & VESA_DMT_HSYNC_HIGH)
  489. dmode->flags |= DRM_MODE_FLAG_PHSYNC;
  490. else if (vm->dmt_flags & VESA_DMT_HSYNC_LOW)
  491. dmode->flags |= DRM_MODE_FLAG_NHSYNC;
  492. if (vm->dmt_flags & VESA_DMT_VSYNC_HIGH)
  493. dmode->flags |= DRM_MODE_FLAG_PVSYNC;
  494. else if (vm->dmt_flags & VESA_DMT_VSYNC_LOW)
  495. dmode->flags |= DRM_MODE_FLAG_NVSYNC;
  496. if (vm->data_flags & DISPLAY_FLAGS_INTERLACED)
  497. dmode->flags |= DRM_MODE_FLAG_INTERLACE;
  498. if (vm->data_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. #endif
  505. #if IS_ENABLED(CONFIG_OF_VIDEOMODE)
  506. /**
  507. * of_get_drm_display_mode - get a drm_display_mode from devicetree
  508. * @np: device_node with the timing specification
  509. * @dmode: will be set to the return value
  510. * @index: index into the list of display timings in devicetree
  511. *
  512. * This function is expensive and should only be used, if only one mode is to be
  513. * read from DT. To get multiple modes start with of_get_display_timings and
  514. * work with that instead.
  515. */
  516. int of_get_drm_display_mode(struct device_node *np,
  517. struct drm_display_mode *dmode, int index)
  518. {
  519. struct videomode vm;
  520. int ret;
  521. ret = of_get_videomode(np, &vm, index);
  522. if (ret)
  523. return ret;
  524. drm_display_mode_from_videomode(&vm, dmode);
  525. pr_debug("%s: got %dx%d display mode from %s\n",
  526. of_node_full_name(np), vm.hactive, vm.vactive, np->name);
  527. drm_mode_debug_printmodeline(dmode);
  528. return 0;
  529. }
  530. EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
  531. #endif
  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. return drm_mode_equal_no_clocks(mode1, mode2);
  777. }
  778. EXPORT_SYMBOL(drm_mode_equal);
  779. /**
  780. * drm_mode_equal_no_clocks - test modes for equality
  781. * @mode1: first mode
  782. * @mode2: second mode
  783. *
  784. * LOCKING:
  785. * None.
  786. *
  787. * Check to see if @mode1 and @mode2 are equivalent, but
  788. * don't check the pixel clocks.
  789. *
  790. * RETURNS:
  791. * True if the modes are equal, false otherwise.
  792. */
  793. bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
  794. {
  795. if (mode1->hdisplay == mode2->hdisplay &&
  796. mode1->hsync_start == mode2->hsync_start &&
  797. mode1->hsync_end == mode2->hsync_end &&
  798. mode1->htotal == mode2->htotal &&
  799. mode1->hskew == mode2->hskew &&
  800. mode1->vdisplay == mode2->vdisplay &&
  801. mode1->vsync_start == mode2->vsync_start &&
  802. mode1->vsync_end == mode2->vsync_end &&
  803. mode1->vtotal == mode2->vtotal &&
  804. mode1->vscan == mode2->vscan &&
  805. mode1->flags == mode2->flags)
  806. return true;
  807. return false;
  808. }
  809. EXPORT_SYMBOL(drm_mode_equal_no_clocks);
  810. /**
  811. * drm_mode_validate_size - make sure modes adhere to size constraints
  812. * @dev: DRM device
  813. * @mode_list: list of modes to check
  814. * @maxX: maximum width
  815. * @maxY: maximum height
  816. * @maxPitch: max pitch
  817. *
  818. * LOCKING:
  819. * Caller must hold a lock protecting @mode_list.
  820. *
  821. * The DRM device (@dev) has size and pitch limits. Here we validate the
  822. * modes we probed for @dev against those limits and set their status as
  823. * necessary.
  824. */
  825. void drm_mode_validate_size(struct drm_device *dev,
  826. struct list_head *mode_list,
  827. int maxX, int maxY, int maxPitch)
  828. {
  829. struct drm_display_mode *mode;
  830. list_for_each_entry(mode, mode_list, head) {
  831. if (maxPitch > 0 && mode->hdisplay > maxPitch)
  832. mode->status = MODE_BAD_WIDTH;
  833. if (maxX > 0 && mode->hdisplay > maxX)
  834. mode->status = MODE_VIRTUAL_X;
  835. if (maxY > 0 && mode->vdisplay > maxY)
  836. mode->status = MODE_VIRTUAL_Y;
  837. }
  838. }
  839. EXPORT_SYMBOL(drm_mode_validate_size);
  840. /**
  841. * drm_mode_validate_clocks - validate modes against clock limits
  842. * @dev: DRM device
  843. * @mode_list: list of modes to check
  844. * @min: minimum clock rate array
  845. * @max: maximum clock rate array
  846. * @n_ranges: number of clock ranges (size of arrays)
  847. *
  848. * LOCKING:
  849. * Caller must hold a lock protecting @mode_list.
  850. *
  851. * Some code may need to check a mode list against the clock limits of the
  852. * device in question. This function walks the mode list, testing to make
  853. * sure each mode falls within a given range (defined by @min and @max
  854. * arrays) and sets @mode->status as needed.
  855. */
  856. void drm_mode_validate_clocks(struct drm_device *dev,
  857. struct list_head *mode_list,
  858. int *min, int *max, int n_ranges)
  859. {
  860. struct drm_display_mode *mode;
  861. int i;
  862. list_for_each_entry(mode, mode_list, head) {
  863. bool good = false;
  864. for (i = 0; i < n_ranges; i++) {
  865. if (mode->clock >= min[i] && mode->clock <= max[i]) {
  866. good = true;
  867. break;
  868. }
  869. }
  870. if (!good)
  871. mode->status = MODE_CLOCK_RANGE;
  872. }
  873. }
  874. EXPORT_SYMBOL(drm_mode_validate_clocks);
  875. /**
  876. * drm_mode_prune_invalid - remove invalid modes from mode list
  877. * @dev: DRM device
  878. * @mode_list: list of modes to check
  879. * @verbose: be verbose about it
  880. *
  881. * LOCKING:
  882. * Caller must hold a lock protecting @mode_list.
  883. *
  884. * Once mode list generation is complete, a caller can use this routine to
  885. * remove invalid modes from a mode list. If any of the modes have a
  886. * status other than %MODE_OK, they are removed from @mode_list and freed.
  887. */
  888. void drm_mode_prune_invalid(struct drm_device *dev,
  889. struct list_head *mode_list, bool verbose)
  890. {
  891. struct drm_display_mode *mode, *t;
  892. list_for_each_entry_safe(mode, t, mode_list, head) {
  893. if (mode->status != MODE_OK) {
  894. list_del(&mode->head);
  895. if (verbose) {
  896. drm_mode_debug_printmodeline(mode);
  897. DRM_DEBUG_KMS("Not using %s mode %d\n",
  898. mode->name, mode->status);
  899. }
  900. drm_mode_destroy(dev, mode);
  901. }
  902. }
  903. }
  904. EXPORT_SYMBOL(drm_mode_prune_invalid);
  905. /**
  906. * drm_mode_compare - compare modes for favorability
  907. * @priv: unused
  908. * @lh_a: list_head for first mode
  909. * @lh_b: list_head for second mode
  910. *
  911. * LOCKING:
  912. * None.
  913. *
  914. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  915. * which is better.
  916. *
  917. * RETURNS:
  918. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  919. * positive if @lh_b is better than @lh_a.
  920. */
  921. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  922. {
  923. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  924. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  925. int diff;
  926. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  927. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  928. if (diff)
  929. return diff;
  930. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  931. if (diff)
  932. return diff;
  933. diff = b->clock - a->clock;
  934. return diff;
  935. }
  936. /**
  937. * drm_mode_sort - sort mode list
  938. * @mode_list: list to sort
  939. *
  940. * LOCKING:
  941. * Caller must hold a lock protecting @mode_list.
  942. *
  943. * Sort @mode_list by favorability, putting good modes first.
  944. */
  945. void drm_mode_sort(struct list_head *mode_list)
  946. {
  947. list_sort(NULL, mode_list, drm_mode_compare);
  948. }
  949. EXPORT_SYMBOL(drm_mode_sort);
  950. /**
  951. * drm_mode_connector_list_update - update the mode list for the connector
  952. * @connector: the connector to update
  953. *
  954. * LOCKING:
  955. * Caller must hold a lock protecting @mode_list.
  956. *
  957. * This moves the modes from the @connector probed_modes list
  958. * to the actual mode list. It compares the probed mode against the current
  959. * list and only adds different modes. All modes unverified after this point
  960. * will be removed by the prune invalid modes.
  961. */
  962. void drm_mode_connector_list_update(struct drm_connector *connector)
  963. {
  964. struct drm_display_mode *mode;
  965. struct drm_display_mode *pmode, *pt;
  966. int found_it;
  967. list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
  968. head) {
  969. found_it = 0;
  970. /* go through current modes checking for the new probed mode */
  971. list_for_each_entry(mode, &connector->modes, head) {
  972. if (drm_mode_equal(pmode, mode)) {
  973. found_it = 1;
  974. /* if equal delete the probed mode */
  975. mode->status = pmode->status;
  976. /* Merge type bits together */
  977. mode->type |= pmode->type;
  978. list_del(&pmode->head);
  979. drm_mode_destroy(connector->dev, pmode);
  980. break;
  981. }
  982. }
  983. if (!found_it) {
  984. list_move_tail(&pmode->head, &connector->modes);
  985. }
  986. }
  987. }
  988. EXPORT_SYMBOL(drm_mode_connector_list_update);
  989. /**
  990. * drm_mode_parse_command_line_for_connector - parse command line for connector
  991. * @mode_option - per connector mode option
  992. * @connector - connector to parse line for
  993. *
  994. * This parses the connector specific then generic command lines for
  995. * modes and options to configure the connector.
  996. *
  997. * This uses the same parameters as the fb modedb.c, except for extra
  998. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  999. *
  1000. * enable/enable Digital/disable bit at the end
  1001. */
  1002. bool drm_mode_parse_command_line_for_connector(const char *mode_option,
  1003. struct drm_connector *connector,
  1004. struct drm_cmdline_mode *mode)
  1005. {
  1006. const char *name;
  1007. unsigned int namelen;
  1008. bool res_specified = false, bpp_specified = false, refresh_specified = false;
  1009. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  1010. bool yres_specified = false, cvt = false, rb = false;
  1011. bool interlace = false, margins = false, was_digit = false;
  1012. int i;
  1013. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  1014. #ifdef CONFIG_FB
  1015. if (!mode_option)
  1016. mode_option = fb_mode_option;
  1017. #endif
  1018. if (!mode_option) {
  1019. mode->specified = false;
  1020. return false;
  1021. }
  1022. name = mode_option;
  1023. namelen = strlen(name);
  1024. for (i = namelen-1; i >= 0; i--) {
  1025. switch (name[i]) {
  1026. case '@':
  1027. if (!refresh_specified && !bpp_specified &&
  1028. !yres_specified && !cvt && !rb && was_digit) {
  1029. refresh = simple_strtol(&name[i+1], NULL, 10);
  1030. refresh_specified = true;
  1031. was_digit = false;
  1032. } else
  1033. goto done;
  1034. break;
  1035. case '-':
  1036. if (!bpp_specified && !yres_specified && !cvt &&
  1037. !rb && was_digit) {
  1038. bpp = simple_strtol(&name[i+1], NULL, 10);
  1039. bpp_specified = true;
  1040. was_digit = false;
  1041. } else
  1042. goto done;
  1043. break;
  1044. case 'x':
  1045. if (!yres_specified && was_digit) {
  1046. yres = simple_strtol(&name[i+1], NULL, 10);
  1047. yres_specified = true;
  1048. was_digit = false;
  1049. } else
  1050. goto done;
  1051. break;
  1052. case '0' ... '9':
  1053. was_digit = true;
  1054. break;
  1055. case 'M':
  1056. if (yres_specified || cvt || was_digit)
  1057. goto done;
  1058. cvt = true;
  1059. break;
  1060. case 'R':
  1061. if (yres_specified || cvt || rb || was_digit)
  1062. goto done;
  1063. rb = true;
  1064. break;
  1065. case 'm':
  1066. if (cvt || yres_specified || was_digit)
  1067. goto done;
  1068. margins = true;
  1069. break;
  1070. case 'i':
  1071. if (cvt || yres_specified || was_digit)
  1072. goto done;
  1073. interlace = true;
  1074. break;
  1075. case 'e':
  1076. if (yres_specified || bpp_specified || refresh_specified ||
  1077. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1078. goto done;
  1079. force = DRM_FORCE_ON;
  1080. break;
  1081. case 'D':
  1082. if (yres_specified || bpp_specified || refresh_specified ||
  1083. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1084. goto done;
  1085. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  1086. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  1087. force = DRM_FORCE_ON;
  1088. else
  1089. force = DRM_FORCE_ON_DIGITAL;
  1090. break;
  1091. case 'd':
  1092. if (yres_specified || bpp_specified || refresh_specified ||
  1093. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1094. goto done;
  1095. force = DRM_FORCE_OFF;
  1096. break;
  1097. default:
  1098. goto done;
  1099. }
  1100. }
  1101. if (i < 0 && yres_specified) {
  1102. char *ch;
  1103. xres = simple_strtol(name, &ch, 10);
  1104. if ((ch != NULL) && (*ch == 'x'))
  1105. res_specified = true;
  1106. else
  1107. i = ch - name;
  1108. } else if (!yres_specified && was_digit) {
  1109. /* catch mode that begins with digits but has no 'x' */
  1110. i = 0;
  1111. }
  1112. done:
  1113. if (i >= 0) {
  1114. printk(KERN_WARNING
  1115. "parse error at position %i in video mode '%s'\n",
  1116. i, name);
  1117. mode->specified = false;
  1118. return false;
  1119. }
  1120. if (res_specified) {
  1121. mode->specified = true;
  1122. mode->xres = xres;
  1123. mode->yres = yres;
  1124. }
  1125. if (refresh_specified) {
  1126. mode->refresh_specified = true;
  1127. mode->refresh = refresh;
  1128. }
  1129. if (bpp_specified) {
  1130. mode->bpp_specified = true;
  1131. mode->bpp = bpp;
  1132. }
  1133. mode->rb = rb;
  1134. mode->cvt = cvt;
  1135. mode->interlace = interlace;
  1136. mode->margins = margins;
  1137. mode->force = force;
  1138. return true;
  1139. }
  1140. EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
  1141. struct drm_display_mode *
  1142. drm_mode_create_from_cmdline_mode(struct drm_device *dev,
  1143. struct drm_cmdline_mode *cmd)
  1144. {
  1145. struct drm_display_mode *mode;
  1146. if (cmd->cvt)
  1147. mode = drm_cvt_mode(dev,
  1148. cmd->xres, cmd->yres,
  1149. cmd->refresh_specified ? cmd->refresh : 60,
  1150. cmd->rb, cmd->interlace,
  1151. cmd->margins);
  1152. else
  1153. mode = drm_gtf_mode(dev,
  1154. cmd->xres, cmd->yres,
  1155. cmd->refresh_specified ? cmd->refresh : 60,
  1156. cmd->interlace,
  1157. cmd->margins);
  1158. if (!mode)
  1159. return NULL;
  1160. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1161. return mode;
  1162. }
  1163. EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);