drm_modes.c 38 KB

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