drm_modes.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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 " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
  50. }
  51. EXPORT_SYMBOL(drm_mode_debug_printmodeline);
  52. /**
  53. * drm_mode_create - create a new display mode
  54. * @dev: DRM device
  55. *
  56. * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
  57. * and return it.
  58. *
  59. * Returns:
  60. * Pointer to new mode on success, NULL on error.
  61. */
  62. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  63. {
  64. struct drm_display_mode *nmode;
  65. nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  66. if (!nmode)
  67. return NULL;
  68. if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
  69. kfree(nmode);
  70. return NULL;
  71. }
  72. return nmode;
  73. }
  74. EXPORT_SYMBOL(drm_mode_create);
  75. /**
  76. * drm_mode_destroy - remove a mode
  77. * @dev: DRM device
  78. * @mode: mode to remove
  79. *
  80. * Release @mode's unique ID, then free it @mode structure itself using kfree.
  81. */
  82. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  83. {
  84. if (!mode)
  85. return;
  86. drm_mode_object_unregister(dev, &mode->base);
  87. kfree(mode);
  88. }
  89. EXPORT_SYMBOL(drm_mode_destroy);
  90. /**
  91. * drm_mode_probed_add - add a mode to a connector's probed_mode list
  92. * @connector: connector the new mode
  93. * @mode: mode data
  94. *
  95. * Add @mode to @connector's probed_mode list for later use. This list should
  96. * then in a second step get filtered and all the modes actually supported by
  97. * the hardware moved to the @connector's modes list.
  98. */
  99. void drm_mode_probed_add(struct drm_connector *connector,
  100. struct drm_display_mode *mode)
  101. {
  102. WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
  103. list_add_tail(&mode->head, &connector->probed_modes);
  104. }
  105. EXPORT_SYMBOL(drm_mode_probed_add);
  106. /**
  107. * drm_cvt_mode -create a modeline based on the CVT algorithm
  108. * @dev: drm device
  109. * @hdisplay: hdisplay size
  110. * @vdisplay: vdisplay size
  111. * @vrefresh: vrefresh rate
  112. * @reduced: whether to use reduced blanking
  113. * @interlaced: whether to compute an interlaced mode
  114. * @margins: whether to add margins (borders)
  115. *
  116. * This function is called to generate the modeline based on CVT algorithm
  117. * according to the hdisplay, vdisplay, vrefresh.
  118. * It is based from the VESA(TM) Coordinated Video Timing Generator by
  119. * Graham Loveridge April 9, 2003 available at
  120. * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
  121. *
  122. * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
  123. * What I have done is to translate it by using integer calculation.
  124. *
  125. * Returns:
  126. * The modeline based on the CVT algorithm stored in a drm_display_mode object.
  127. * The display mode object is allocated with drm_mode_create(). Returns NULL
  128. * when no mode could be allocated.
  129. */
  130. struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
  131. int vdisplay, int vrefresh,
  132. bool reduced, bool interlaced, bool margins)
  133. {
  134. #define HV_FACTOR 1000
  135. /* 1) top/bottom margin size (% of height) - default: 1.8, */
  136. #define CVT_MARGIN_PERCENTAGE 18
  137. /* 2) character cell horizontal granularity (pixels) - default 8 */
  138. #define CVT_H_GRANULARITY 8
  139. /* 3) Minimum vertical porch (lines) - default 3 */
  140. #define CVT_MIN_V_PORCH 3
  141. /* 4) Minimum number of vertical back porch lines - default 6 */
  142. #define CVT_MIN_V_BPORCH 6
  143. /* Pixel Clock step (kHz) */
  144. #define CVT_CLOCK_STEP 250
  145. struct drm_display_mode *drm_mode;
  146. unsigned int vfieldrate, hperiod;
  147. int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
  148. int interlace;
  149. u64 tmp;
  150. /* allocate the drm_display_mode structure. If failure, we will
  151. * return directly
  152. */
  153. drm_mode = drm_mode_create(dev);
  154. if (!drm_mode)
  155. return NULL;
  156. /* the CVT default refresh rate is 60Hz */
  157. if (!vrefresh)
  158. vrefresh = 60;
  159. /* the required field fresh rate */
  160. if (interlaced)
  161. vfieldrate = vrefresh * 2;
  162. else
  163. vfieldrate = vrefresh;
  164. /* horizontal pixels */
  165. hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
  166. /* determine the left&right borders */
  167. hmargin = 0;
  168. if (margins) {
  169. hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  170. hmargin -= hmargin % CVT_H_GRANULARITY;
  171. }
  172. /* find the total active pixels */
  173. drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
  174. /* find the number of lines per field */
  175. if (interlaced)
  176. vdisplay_rnd = vdisplay / 2;
  177. else
  178. vdisplay_rnd = vdisplay;
  179. /* find the top & bottom borders */
  180. vmargin = 0;
  181. if (margins)
  182. vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
  183. drm_mode->vdisplay = vdisplay + 2 * vmargin;
  184. /* Interlaced */
  185. if (interlaced)
  186. interlace = 1;
  187. else
  188. interlace = 0;
  189. /* Determine VSync Width from aspect ratio */
  190. if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
  191. vsync = 4;
  192. else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
  193. vsync = 5;
  194. else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
  195. vsync = 6;
  196. else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
  197. vsync = 7;
  198. else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
  199. vsync = 7;
  200. else /* custom */
  201. vsync = 10;
  202. if (!reduced) {
  203. /* simplify the GTF calculation */
  204. /* 4) Minimum time of vertical sync + back porch interval (µs)
  205. * default 550.0
  206. */
  207. int tmp1, tmp2;
  208. #define CVT_MIN_VSYNC_BP 550
  209. /* 3) Nominal HSync width (% of line period) - default 8 */
  210. #define CVT_HSYNC_PERCENTAGE 8
  211. unsigned int hblank_percentage;
  212. int vsyncandback_porch, vback_porch, hblank;
  213. /* estimated the horizontal period */
  214. tmp1 = HV_FACTOR * 1000000 -
  215. CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
  216. tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
  217. interlace;
  218. hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
  219. tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
  220. /* 9. Find number of lines in sync + backporch */
  221. if (tmp1 < (vsync + CVT_MIN_V_PORCH))
  222. vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
  223. else
  224. vsyncandback_porch = tmp1;
  225. /* 10. Find number of lines in back porch */
  226. vback_porch = vsyncandback_porch - vsync;
  227. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
  228. vsyncandback_porch + CVT_MIN_V_PORCH;
  229. /* 5) Definition of Horizontal blanking time limitation */
  230. /* Gradient (%/kHz) - default 600 */
  231. #define CVT_M_FACTOR 600
  232. /* Offset (%) - default 40 */
  233. #define CVT_C_FACTOR 40
  234. /* Blanking time scaling factor - default 128 */
  235. #define CVT_K_FACTOR 128
  236. /* Scaling factor weighting - default 20 */
  237. #define CVT_J_FACTOR 20
  238. #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
  239. #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
  240. CVT_J_FACTOR)
  241. /* 12. Find ideal blanking duty cycle from formula */
  242. hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
  243. hperiod / 1000;
  244. /* 13. Blanking time */
  245. if (hblank_percentage < 20 * HV_FACTOR)
  246. hblank_percentage = 20 * HV_FACTOR;
  247. hblank = drm_mode->hdisplay * hblank_percentage /
  248. (100 * HV_FACTOR - hblank_percentage);
  249. hblank -= hblank % (2 * CVT_H_GRANULARITY);
  250. /* 14. find the total pixels per line */
  251. drm_mode->htotal = drm_mode->hdisplay + hblank;
  252. drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
  253. drm_mode->hsync_start = drm_mode->hsync_end -
  254. (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
  255. drm_mode->hsync_start += CVT_H_GRANULARITY -
  256. drm_mode->hsync_start % CVT_H_GRANULARITY;
  257. /* fill the Vsync values */
  258. drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
  259. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  260. } else {
  261. /* Reduced blanking */
  262. /* Minimum vertical blanking interval time (µs)- default 460 */
  263. #define CVT_RB_MIN_VBLANK 460
  264. /* Fixed number of clocks for horizontal sync */
  265. #define CVT_RB_H_SYNC 32
  266. /* Fixed number of clocks for horizontal blanking */
  267. #define CVT_RB_H_BLANK 160
  268. /* Fixed number of lines for vertical front porch - default 3*/
  269. #define CVT_RB_VFPORCH 3
  270. int vbilines;
  271. int tmp1, tmp2;
  272. /* 8. Estimate Horizontal period. */
  273. tmp1 = HV_FACTOR * 1000000 -
  274. CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
  275. tmp2 = vdisplay_rnd + 2 * vmargin;
  276. hperiod = tmp1 / (tmp2 * vfieldrate);
  277. /* 9. Find number of lines in vertical blanking */
  278. vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
  279. /* 10. Check if vertical blanking is sufficient */
  280. if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
  281. vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
  282. /* 11. Find total number of lines in vertical field */
  283. drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
  284. /* 12. Find total number of pixels in a line */
  285. drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
  286. /* Fill in HSync values */
  287. drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
  288. drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
  289. /* Fill in VSync values */
  290. drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
  291. drm_mode->vsync_end = drm_mode->vsync_start + vsync;
  292. }
  293. /* 15/13. Find pixel clock frequency (kHz for xf86) */
  294. tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
  295. tmp *= HV_FACTOR * 1000;
  296. do_div(tmp, hperiod);
  297. tmp -= drm_mode->clock % CVT_CLOCK_STEP;
  298. drm_mode->clock = tmp;
  299. /* 18/16. Find actual vertical frame frequency */
  300. /* ignore - just set the mode flag for interlaced */
  301. if (interlaced) {
  302. drm_mode->vtotal *= 2;
  303. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  304. }
  305. /* Fill the mode line name */
  306. drm_mode_set_name(drm_mode);
  307. if (reduced)
  308. drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
  309. DRM_MODE_FLAG_NVSYNC);
  310. else
  311. drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
  312. DRM_MODE_FLAG_NHSYNC);
  313. return drm_mode;
  314. }
  315. EXPORT_SYMBOL(drm_cvt_mode);
  316. /**
  317. * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
  318. * @dev: drm device
  319. * @hdisplay: hdisplay size
  320. * @vdisplay: vdisplay size
  321. * @vrefresh: vrefresh rate.
  322. * @interlaced: whether to compute an interlaced mode
  323. * @margins: desired margin (borders) size
  324. * @GTF_M: extended GTF formula parameters
  325. * @GTF_2C: extended GTF formula parameters
  326. * @GTF_K: extended GTF formula parameters
  327. * @GTF_2J: extended GTF formula parameters
  328. *
  329. * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
  330. * in here multiplied by two. For a C of 40, pass in 80.
  331. *
  332. * Returns:
  333. * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
  334. * The display mode object is allocated with drm_mode_create(). Returns NULL
  335. * when no mode could be allocated.
  336. */
  337. struct drm_display_mode *
  338. drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
  339. int vrefresh, bool interlaced, int margins,
  340. int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
  341. { /* 1) top/bottom margin size (% of height) - default: 1.8, */
  342. #define GTF_MARGIN_PERCENTAGE 18
  343. /* 2) character cell horizontal granularity (pixels) - default 8 */
  344. #define GTF_CELL_GRAN 8
  345. /* 3) Minimum vertical porch (lines) - default 3 */
  346. #define GTF_MIN_V_PORCH 1
  347. /* width of vsync in lines */
  348. #define V_SYNC_RQD 3
  349. /* width of hsync as % of total line */
  350. #define H_SYNC_PERCENT 8
  351. /* min time of vsync + back porch (microsec) */
  352. #define MIN_VSYNC_PLUS_BP 550
  353. /* C' and M' are part of the Blanking Duty Cycle computation */
  354. #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
  355. #define GTF_M_PRIME (GTF_K * GTF_M / 256)
  356. struct drm_display_mode *drm_mode;
  357. unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
  358. int top_margin, bottom_margin;
  359. int interlace;
  360. unsigned int hfreq_est;
  361. int vsync_plus_bp, vback_porch;
  362. unsigned int vtotal_lines, vfieldrate_est, hperiod;
  363. unsigned int vfield_rate, vframe_rate;
  364. int left_margin, right_margin;
  365. unsigned int total_active_pixels, ideal_duty_cycle;
  366. unsigned int hblank, total_pixels, pixel_freq;
  367. int hsync, hfront_porch, vodd_front_porch_lines;
  368. unsigned int tmp1, tmp2;
  369. drm_mode = drm_mode_create(dev);
  370. if (!drm_mode)
  371. return NULL;
  372. /* 1. In order to give correct results, the number of horizontal
  373. * pixels requested is first processed to ensure that it is divisible
  374. * by the character size, by rounding it to the nearest character
  375. * cell boundary:
  376. */
  377. hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  378. hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
  379. /* 2. If interlace is requested, the number of vertical lines assumed
  380. * by the calculation must be halved, as the computation calculates
  381. * the number of vertical lines per field.
  382. */
  383. if (interlaced)
  384. vdisplay_rnd = vdisplay / 2;
  385. else
  386. vdisplay_rnd = vdisplay;
  387. /* 3. Find the frame rate required: */
  388. if (interlaced)
  389. vfieldrate_rqd = vrefresh * 2;
  390. else
  391. vfieldrate_rqd = vrefresh;
  392. /* 4. Find number of lines in Top margin: */
  393. top_margin = 0;
  394. if (margins)
  395. top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  396. 1000;
  397. /* 5. Find number of lines in bottom margin: */
  398. bottom_margin = top_margin;
  399. /* 6. If interlace is required, then set variable interlace: */
  400. if (interlaced)
  401. interlace = 1;
  402. else
  403. interlace = 0;
  404. /* 7. Estimate the Horizontal frequency */
  405. {
  406. tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
  407. tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
  408. 2 + interlace;
  409. hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
  410. }
  411. /* 8. Find the number of lines in V sync + back porch */
  412. /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
  413. vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
  414. vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
  415. /* 9. Find the number of lines in V back porch alone: */
  416. vback_porch = vsync_plus_bp - V_SYNC_RQD;
  417. /* 10. Find the total number of lines in Vertical field period: */
  418. vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
  419. vsync_plus_bp + GTF_MIN_V_PORCH;
  420. /* 11. Estimate the Vertical field frequency: */
  421. vfieldrate_est = hfreq_est / vtotal_lines;
  422. /* 12. Find the actual horizontal period: */
  423. hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
  424. /* 13. Find the actual Vertical field frequency: */
  425. vfield_rate = hfreq_est / vtotal_lines;
  426. /* 14. Find the Vertical frame frequency: */
  427. if (interlaced)
  428. vframe_rate = vfield_rate / 2;
  429. else
  430. vframe_rate = vfield_rate;
  431. /* 15. Find number of pixels in left margin: */
  432. if (margins)
  433. left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
  434. 1000;
  435. else
  436. left_margin = 0;
  437. /* 16.Find number of pixels in right margin: */
  438. right_margin = left_margin;
  439. /* 17.Find total number of active pixels in image and left and right */
  440. total_active_pixels = hdisplay_rnd + left_margin + right_margin;
  441. /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
  442. ideal_duty_cycle = GTF_C_PRIME * 1000 -
  443. (GTF_M_PRIME * 1000000 / hfreq_est);
  444. /* 19.Find the number of pixels in the blanking time to the nearest
  445. * double character cell: */
  446. hblank = total_active_pixels * ideal_duty_cycle /
  447. (100000 - ideal_duty_cycle);
  448. hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
  449. hblank = hblank * 2 * GTF_CELL_GRAN;
  450. /* 20.Find total number of pixels: */
  451. total_pixels = total_active_pixels + hblank;
  452. /* 21.Find pixel clock frequency: */
  453. pixel_freq = total_pixels * hfreq_est / 1000;
  454. /* Stage 1 computations are now complete; I should really pass
  455. * the results to another function and do the Stage 2 computations,
  456. * but I only need a few more values so I'll just append the
  457. * computations here for now */
  458. /* 17. Find the number of pixels in the horizontal sync period: */
  459. hsync = H_SYNC_PERCENT * total_pixels / 100;
  460. hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
  461. hsync = hsync * GTF_CELL_GRAN;
  462. /* 18. Find the number of pixels in horizontal front porch period */
  463. hfront_porch = hblank / 2 - hsync;
  464. /* 36. Find the number of lines in the odd front porch period: */
  465. vodd_front_porch_lines = GTF_MIN_V_PORCH ;
  466. /* finally, pack the results in the mode struct */
  467. drm_mode->hdisplay = hdisplay_rnd;
  468. drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
  469. drm_mode->hsync_end = drm_mode->hsync_start + hsync;
  470. drm_mode->htotal = total_pixels;
  471. drm_mode->vdisplay = vdisplay_rnd;
  472. drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
  473. drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
  474. drm_mode->vtotal = vtotal_lines;
  475. drm_mode->clock = pixel_freq;
  476. if (interlaced) {
  477. drm_mode->vtotal *= 2;
  478. drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
  479. }
  480. drm_mode_set_name(drm_mode);
  481. if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
  482. drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
  483. else
  484. drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
  485. return drm_mode;
  486. }
  487. EXPORT_SYMBOL(drm_gtf_mode_complex);
  488. /**
  489. * drm_gtf_mode - create the modeline based on the GTF algorithm
  490. * @dev: drm device
  491. * @hdisplay: hdisplay size
  492. * @vdisplay: vdisplay size
  493. * @vrefresh: vrefresh rate.
  494. * @interlaced: whether to compute an interlaced mode
  495. * @margins: desired margin (borders) size
  496. *
  497. * return the modeline based on GTF algorithm
  498. *
  499. * This function is to create the modeline based on the GTF algorithm.
  500. * Generalized Timing Formula is derived from:
  501. *
  502. * GTF Spreadsheet by Andy Morrish (1/5/97)
  503. * available at http://www.vesa.org
  504. *
  505. * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
  506. * What I have done is to translate it by using integer calculation.
  507. * I also refer to the function of fb_get_mode in the file of
  508. * drivers/video/fbmon.c
  509. *
  510. * Standard GTF parameters::
  511. *
  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. /**
  570. * drm_display_mode_to_videomode - fill in @vm using @dmode,
  571. * @dmode: drm_display_mode structure to use as source
  572. * @vm: videomode structure to use as destination
  573. *
  574. * Fills out @vm using the display mode specified in @dmode.
  575. */
  576. void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
  577. struct videomode *vm)
  578. {
  579. vm->hactive = dmode->hdisplay;
  580. vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
  581. vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
  582. vm->hback_porch = dmode->htotal - dmode->hsync_end;
  583. vm->vactive = dmode->vdisplay;
  584. vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
  585. vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
  586. vm->vback_porch = dmode->vtotal - dmode->vsync_end;
  587. vm->pixelclock = dmode->clock * 1000;
  588. vm->flags = 0;
  589. if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
  590. vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
  591. else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
  592. vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
  593. if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
  594. vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
  595. else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
  596. vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
  597. if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
  598. vm->flags |= DISPLAY_FLAGS_INTERLACED;
  599. if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
  600. vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  601. if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
  602. vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
  603. }
  604. EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
  605. /**
  606. * drm_bus_flags_from_videomode - extract information about pixelclk and
  607. * DE polarity from videomode and store it in a separate variable
  608. * @vm: videomode structure to use
  609. * @bus_flags: information about pixelclk and DE polarity will be stored here
  610. *
  611. * Sets DRM_BUS_FLAG_DE_(LOW|HIGH) and DRM_BUS_FLAG_PIXDATA_(POS|NEG)EDGE
  612. * in @bus_flags according to DISPLAY_FLAGS found in @vm
  613. */
  614. void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
  615. {
  616. *bus_flags = 0;
  617. if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
  618. *bus_flags |= DRM_BUS_FLAG_PIXDATA_POSEDGE;
  619. if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
  620. *bus_flags |= DRM_BUS_FLAG_PIXDATA_NEGEDGE;
  621. if (vm->flags & DISPLAY_FLAGS_DE_LOW)
  622. *bus_flags |= DRM_BUS_FLAG_DE_LOW;
  623. if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
  624. *bus_flags |= DRM_BUS_FLAG_DE_HIGH;
  625. }
  626. EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
  627. #ifdef CONFIG_OF
  628. /**
  629. * of_get_drm_display_mode - get a drm_display_mode from devicetree
  630. * @np: device_node with the timing specification
  631. * @dmode: will be set to the return value
  632. * @bus_flags: information about pixelclk and DE polarity
  633. * @index: index into the list of display timings in devicetree
  634. *
  635. * This function is expensive and should only be used, if only one mode is to be
  636. * read from DT. To get multiple modes start with of_get_display_timings and
  637. * work with that instead.
  638. *
  639. * Returns:
  640. * 0 on success, a negative errno code when no of videomode node was found.
  641. */
  642. int of_get_drm_display_mode(struct device_node *np,
  643. struct drm_display_mode *dmode, u32 *bus_flags,
  644. int index)
  645. {
  646. struct videomode vm;
  647. int ret;
  648. ret = of_get_videomode(np, &vm, index);
  649. if (ret)
  650. return ret;
  651. drm_display_mode_from_videomode(&vm, dmode);
  652. if (bus_flags)
  653. drm_bus_flags_from_videomode(&vm, bus_flags);
  654. pr_debug("%s: got %dx%d display mode from %s\n",
  655. of_node_full_name(np), vm.hactive, vm.vactive, np->name);
  656. drm_mode_debug_printmodeline(dmode);
  657. return 0;
  658. }
  659. EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
  660. #endif /* CONFIG_OF */
  661. #endif /* CONFIG_VIDEOMODE_HELPERS */
  662. /**
  663. * drm_mode_set_name - set the name on a mode
  664. * @mode: name will be set in this mode
  665. *
  666. * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
  667. * with an optional 'i' suffix for interlaced modes.
  668. */
  669. void drm_mode_set_name(struct drm_display_mode *mode)
  670. {
  671. bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  672. snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
  673. mode->hdisplay, mode->vdisplay,
  674. interlaced ? "i" : "");
  675. }
  676. EXPORT_SYMBOL(drm_mode_set_name);
  677. /**
  678. * drm_mode_hsync - get the hsync of a mode
  679. * @mode: mode
  680. *
  681. * Returns:
  682. * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
  683. * value first if it is not yet set.
  684. */
  685. int drm_mode_hsync(const struct drm_display_mode *mode)
  686. {
  687. unsigned int calc_val;
  688. if (mode->hsync)
  689. return mode->hsync;
  690. if (mode->htotal < 0)
  691. return 0;
  692. calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
  693. calc_val += 500; /* round to 1000Hz */
  694. calc_val /= 1000; /* truncate to kHz */
  695. return calc_val;
  696. }
  697. EXPORT_SYMBOL(drm_mode_hsync);
  698. /**
  699. * drm_mode_vrefresh - get the vrefresh of a mode
  700. * @mode: mode
  701. *
  702. * Returns:
  703. * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
  704. * value first if it is not yet set.
  705. */
  706. int drm_mode_vrefresh(const struct drm_display_mode *mode)
  707. {
  708. int refresh = 0;
  709. unsigned int calc_val;
  710. if (mode->vrefresh > 0)
  711. refresh = mode->vrefresh;
  712. else if (mode->htotal > 0 && mode->vtotal > 0) {
  713. int vtotal;
  714. vtotal = mode->vtotal;
  715. /* work out vrefresh the value will be x1000 */
  716. calc_val = (mode->clock * 1000);
  717. calc_val /= mode->htotal;
  718. refresh = (calc_val + vtotal / 2) / vtotal;
  719. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  720. refresh *= 2;
  721. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  722. refresh /= 2;
  723. if (mode->vscan > 1)
  724. refresh /= mode->vscan;
  725. }
  726. return refresh;
  727. }
  728. EXPORT_SYMBOL(drm_mode_vrefresh);
  729. /**
  730. * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  731. * @mode: mode to query
  732. * @hdisplay: hdisplay value to fill in
  733. * @vdisplay: vdisplay value to fill in
  734. *
  735. * The vdisplay value will be doubled if the specified mode is a stereo mode of
  736. * the appropriate layout.
  737. */
  738. void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
  739. int *hdisplay, int *vdisplay)
  740. {
  741. struct drm_display_mode adjusted = *mode;
  742. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  743. *hdisplay = adjusted.crtc_hdisplay;
  744. *vdisplay = adjusted.crtc_vdisplay;
  745. }
  746. EXPORT_SYMBOL(drm_mode_get_hv_timing);
  747. /**
  748. * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
  749. * @p: mode
  750. * @adjust_flags: a combination of adjustment flags
  751. *
  752. * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
  753. *
  754. * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
  755. * interlaced modes.
  756. * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
  757. * buffers containing two eyes (only adjust the timings when needed, eg. for
  758. * "frame packing" or "side by side full").
  759. * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
  760. * be performed for doublescan and vscan > 1 modes respectively.
  761. */
  762. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  763. {
  764. if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
  765. return;
  766. p->crtc_clock = p->clock;
  767. p->crtc_hdisplay = p->hdisplay;
  768. p->crtc_hsync_start = p->hsync_start;
  769. p->crtc_hsync_end = p->hsync_end;
  770. p->crtc_htotal = p->htotal;
  771. p->crtc_hskew = p->hskew;
  772. p->crtc_vdisplay = p->vdisplay;
  773. p->crtc_vsync_start = p->vsync_start;
  774. p->crtc_vsync_end = p->vsync_end;
  775. p->crtc_vtotal = p->vtotal;
  776. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  777. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  778. p->crtc_vdisplay /= 2;
  779. p->crtc_vsync_start /= 2;
  780. p->crtc_vsync_end /= 2;
  781. p->crtc_vtotal /= 2;
  782. }
  783. }
  784. if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
  785. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  786. p->crtc_vdisplay *= 2;
  787. p->crtc_vsync_start *= 2;
  788. p->crtc_vsync_end *= 2;
  789. p->crtc_vtotal *= 2;
  790. }
  791. }
  792. if (!(adjust_flags & CRTC_NO_VSCAN)) {
  793. if (p->vscan > 1) {
  794. p->crtc_vdisplay *= p->vscan;
  795. p->crtc_vsync_start *= p->vscan;
  796. p->crtc_vsync_end *= p->vscan;
  797. p->crtc_vtotal *= p->vscan;
  798. }
  799. }
  800. if (adjust_flags & CRTC_STEREO_DOUBLE) {
  801. unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
  802. switch (layout) {
  803. case DRM_MODE_FLAG_3D_FRAME_PACKING:
  804. p->crtc_clock *= 2;
  805. p->crtc_vdisplay += p->crtc_vtotal;
  806. p->crtc_vsync_start += p->crtc_vtotal;
  807. p->crtc_vsync_end += p->crtc_vtotal;
  808. p->crtc_vtotal += p->crtc_vtotal;
  809. break;
  810. }
  811. }
  812. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  813. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  814. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  815. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  816. }
  817. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  818. /**
  819. * drm_mode_copy - copy the mode
  820. * @dst: mode to overwrite
  821. * @src: mode to copy
  822. *
  823. * Copy an existing mode into another mode, preserving the object id and
  824. * list head of the destination mode.
  825. */
  826. void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
  827. {
  828. int id = dst->base.id;
  829. struct list_head head = dst->head;
  830. *dst = *src;
  831. dst->base.id = id;
  832. dst->head = head;
  833. }
  834. EXPORT_SYMBOL(drm_mode_copy);
  835. /**
  836. * drm_mode_duplicate - allocate and duplicate an existing mode
  837. * @dev: drm_device to allocate the duplicated mode for
  838. * @mode: mode to duplicate
  839. *
  840. * Just allocate a new mode, copy the existing mode into it, and return
  841. * a pointer to it. Used to create new instances of established modes.
  842. *
  843. * Returns:
  844. * Pointer to duplicated mode on success, NULL on error.
  845. */
  846. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  847. const struct drm_display_mode *mode)
  848. {
  849. struct drm_display_mode *nmode;
  850. nmode = drm_mode_create(dev);
  851. if (!nmode)
  852. return NULL;
  853. drm_mode_copy(nmode, mode);
  854. return nmode;
  855. }
  856. EXPORT_SYMBOL(drm_mode_duplicate);
  857. /**
  858. * drm_mode_equal - test modes for equality
  859. * @mode1: first mode
  860. * @mode2: second mode
  861. *
  862. * Check to see if @mode1 and @mode2 are equivalent.
  863. *
  864. * Returns:
  865. * True if the modes are equal, false otherwise.
  866. */
  867. bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
  868. {
  869. if (!mode1 && !mode2)
  870. return true;
  871. if (!mode1 || !mode2)
  872. return false;
  873. /* do clock check convert to PICOS so fb modes get matched
  874. * the same */
  875. if (mode1->clock && mode2->clock) {
  876. if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
  877. return false;
  878. } else if (mode1->clock != mode2->clock)
  879. return false;
  880. return drm_mode_equal_no_clocks(mode1, mode2);
  881. }
  882. EXPORT_SYMBOL(drm_mode_equal);
  883. /**
  884. * drm_mode_equal_no_clocks - test modes for equality
  885. * @mode1: first mode
  886. * @mode2: second mode
  887. *
  888. * Check to see if @mode1 and @mode2 are equivalent, but
  889. * don't check the pixel clocks.
  890. *
  891. * Returns:
  892. * True if the modes are equal, false otherwise.
  893. */
  894. bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
  895. {
  896. if ((mode1->flags & DRM_MODE_FLAG_3D_MASK) !=
  897. (mode2->flags & DRM_MODE_FLAG_3D_MASK))
  898. return false;
  899. return drm_mode_equal_no_clocks_no_stereo(mode1, mode2);
  900. }
  901. EXPORT_SYMBOL(drm_mode_equal_no_clocks);
  902. /**
  903. * drm_mode_equal_no_clocks_no_stereo - test modes for equality
  904. * @mode1: first mode
  905. * @mode2: second mode
  906. *
  907. * Check to see if @mode1 and @mode2 are equivalent, but
  908. * don't check the pixel clocks nor the stereo layout.
  909. *
  910. * Returns:
  911. * True if the modes are equal, false otherwise.
  912. */
  913. bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
  914. const struct drm_display_mode *mode2)
  915. {
  916. if (mode1->hdisplay == mode2->hdisplay &&
  917. mode1->hsync_start == mode2->hsync_start &&
  918. mode1->hsync_end == mode2->hsync_end &&
  919. mode1->htotal == mode2->htotal &&
  920. mode1->hskew == mode2->hskew &&
  921. mode1->vdisplay == mode2->vdisplay &&
  922. mode1->vsync_start == mode2->vsync_start &&
  923. mode1->vsync_end == mode2->vsync_end &&
  924. mode1->vtotal == mode2->vtotal &&
  925. mode1->vscan == mode2->vscan &&
  926. (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
  927. (mode2->flags & ~DRM_MODE_FLAG_3D_MASK))
  928. return true;
  929. return false;
  930. }
  931. EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
  932. /**
  933. * drm_mode_validate_basic - make sure the mode is somewhat sane
  934. * @mode: mode to check
  935. *
  936. * Check that the mode timings are at least somewhat reasonable.
  937. * Any hardware specific limits are left up for each driver to check.
  938. *
  939. * Returns:
  940. * The mode status
  941. */
  942. enum drm_mode_status
  943. drm_mode_validate_basic(const struct drm_display_mode *mode)
  944. {
  945. if (mode->clock == 0)
  946. return MODE_CLOCK_LOW;
  947. if (mode->hdisplay == 0 ||
  948. mode->hsync_start < mode->hdisplay ||
  949. mode->hsync_end < mode->hsync_start ||
  950. mode->htotal < mode->hsync_end)
  951. return MODE_H_ILLEGAL;
  952. if (mode->vdisplay == 0 ||
  953. mode->vsync_start < mode->vdisplay ||
  954. mode->vsync_end < mode->vsync_start ||
  955. mode->vtotal < mode->vsync_end)
  956. return MODE_V_ILLEGAL;
  957. return MODE_OK;
  958. }
  959. EXPORT_SYMBOL(drm_mode_validate_basic);
  960. /**
  961. * drm_mode_validate_size - make sure modes adhere to size constraints
  962. * @mode: mode to check
  963. * @maxX: maximum width
  964. * @maxY: maximum height
  965. *
  966. * This function is a helper which can be used to validate modes against size
  967. * limitations of the DRM device/connector. If a mode is too big its status
  968. * member is updated with the appropriate validation failure code. The list
  969. * itself is not changed.
  970. *
  971. * Returns:
  972. * The mode status
  973. */
  974. enum drm_mode_status
  975. drm_mode_validate_size(const struct drm_display_mode *mode,
  976. int maxX, int maxY)
  977. {
  978. if (maxX > 0 && mode->hdisplay > maxX)
  979. return MODE_VIRTUAL_X;
  980. if (maxY > 0 && mode->vdisplay > maxY)
  981. return MODE_VIRTUAL_Y;
  982. return MODE_OK;
  983. }
  984. EXPORT_SYMBOL(drm_mode_validate_size);
  985. #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
  986. static const char * const drm_mode_status_names[] = {
  987. MODE_STATUS(OK),
  988. MODE_STATUS(HSYNC),
  989. MODE_STATUS(VSYNC),
  990. MODE_STATUS(H_ILLEGAL),
  991. MODE_STATUS(V_ILLEGAL),
  992. MODE_STATUS(BAD_WIDTH),
  993. MODE_STATUS(NOMODE),
  994. MODE_STATUS(NO_INTERLACE),
  995. MODE_STATUS(NO_DBLESCAN),
  996. MODE_STATUS(NO_VSCAN),
  997. MODE_STATUS(MEM),
  998. MODE_STATUS(VIRTUAL_X),
  999. MODE_STATUS(VIRTUAL_Y),
  1000. MODE_STATUS(MEM_VIRT),
  1001. MODE_STATUS(NOCLOCK),
  1002. MODE_STATUS(CLOCK_HIGH),
  1003. MODE_STATUS(CLOCK_LOW),
  1004. MODE_STATUS(CLOCK_RANGE),
  1005. MODE_STATUS(BAD_HVALUE),
  1006. MODE_STATUS(BAD_VVALUE),
  1007. MODE_STATUS(BAD_VSCAN),
  1008. MODE_STATUS(HSYNC_NARROW),
  1009. MODE_STATUS(HSYNC_WIDE),
  1010. MODE_STATUS(HBLANK_NARROW),
  1011. MODE_STATUS(HBLANK_WIDE),
  1012. MODE_STATUS(VSYNC_NARROW),
  1013. MODE_STATUS(VSYNC_WIDE),
  1014. MODE_STATUS(VBLANK_NARROW),
  1015. MODE_STATUS(VBLANK_WIDE),
  1016. MODE_STATUS(PANEL),
  1017. MODE_STATUS(INTERLACE_WIDTH),
  1018. MODE_STATUS(ONE_WIDTH),
  1019. MODE_STATUS(ONE_HEIGHT),
  1020. MODE_STATUS(ONE_SIZE),
  1021. MODE_STATUS(NO_REDUCED),
  1022. MODE_STATUS(NO_STEREO),
  1023. MODE_STATUS(STALE),
  1024. MODE_STATUS(BAD),
  1025. MODE_STATUS(ERROR),
  1026. };
  1027. #undef MODE_STATUS
  1028. static const char *drm_get_mode_status_name(enum drm_mode_status status)
  1029. {
  1030. int index = status + 3;
  1031. if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
  1032. return "";
  1033. return drm_mode_status_names[index];
  1034. }
  1035. /**
  1036. * drm_mode_prune_invalid - remove invalid modes from mode list
  1037. * @dev: DRM device
  1038. * @mode_list: list of modes to check
  1039. * @verbose: be verbose about it
  1040. *
  1041. * This helper function can be used to prune a display mode list after
  1042. * validation has been completed. All modes who's status is not MODE_OK will be
  1043. * removed from the list, and if @verbose the status code and mode name is also
  1044. * printed to dmesg.
  1045. */
  1046. void drm_mode_prune_invalid(struct drm_device *dev,
  1047. struct list_head *mode_list, bool verbose)
  1048. {
  1049. struct drm_display_mode *mode, *t;
  1050. list_for_each_entry_safe(mode, t, mode_list, head) {
  1051. if (mode->status != MODE_OK) {
  1052. list_del(&mode->head);
  1053. if (verbose) {
  1054. drm_mode_debug_printmodeline(mode);
  1055. DRM_DEBUG_KMS("Not using %s mode: %s\n",
  1056. mode->name,
  1057. drm_get_mode_status_name(mode->status));
  1058. }
  1059. drm_mode_destroy(dev, mode);
  1060. }
  1061. }
  1062. }
  1063. EXPORT_SYMBOL(drm_mode_prune_invalid);
  1064. /**
  1065. * drm_mode_compare - compare modes for favorability
  1066. * @priv: unused
  1067. * @lh_a: list_head for first mode
  1068. * @lh_b: list_head for second mode
  1069. *
  1070. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  1071. * which is better.
  1072. *
  1073. * Returns:
  1074. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  1075. * positive if @lh_b is better than @lh_a.
  1076. */
  1077. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  1078. {
  1079. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  1080. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  1081. int diff;
  1082. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  1083. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  1084. if (diff)
  1085. return diff;
  1086. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  1087. if (diff)
  1088. return diff;
  1089. diff = b->vrefresh - a->vrefresh;
  1090. if (diff)
  1091. return diff;
  1092. diff = b->clock - a->clock;
  1093. return diff;
  1094. }
  1095. /**
  1096. * drm_mode_sort - sort mode list
  1097. * @mode_list: list of drm_display_mode structures to sort
  1098. *
  1099. * Sort @mode_list by favorability, moving good modes to the head of the list.
  1100. */
  1101. void drm_mode_sort(struct list_head *mode_list)
  1102. {
  1103. list_sort(NULL, mode_list, drm_mode_compare);
  1104. }
  1105. EXPORT_SYMBOL(drm_mode_sort);
  1106. /**
  1107. * drm_mode_connector_list_update - update the mode list for the connector
  1108. * @connector: the connector to update
  1109. *
  1110. * This moves the modes from the @connector probed_modes list
  1111. * to the actual mode list. It compares the probed mode against the current
  1112. * list and only adds different/new modes.
  1113. *
  1114. * This is just a helper functions doesn't validate any modes itself and also
  1115. * doesn't prune any invalid modes. Callers need to do that themselves.
  1116. */
  1117. void drm_mode_connector_list_update(struct drm_connector *connector)
  1118. {
  1119. struct drm_display_mode *pmode, *pt;
  1120. WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
  1121. list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
  1122. struct drm_display_mode *mode;
  1123. bool found_it = false;
  1124. /* go through current modes checking for the new probed mode */
  1125. list_for_each_entry(mode, &connector->modes, head) {
  1126. if (!drm_mode_equal(pmode, mode))
  1127. continue;
  1128. found_it = true;
  1129. /*
  1130. * If the old matching mode is stale (ie. left over
  1131. * from a previous probe) just replace it outright.
  1132. * Otherwise just merge the type bits between all
  1133. * equal probed modes.
  1134. *
  1135. * If two probed modes are considered equal, pick the
  1136. * actual timings from the one that's marked as
  1137. * preferred (in case the match isn't 100%). If
  1138. * multiple or zero preferred modes are present, favor
  1139. * the mode added to the probed_modes list first.
  1140. */
  1141. if (mode->status == MODE_STALE) {
  1142. drm_mode_copy(mode, pmode);
  1143. } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
  1144. (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
  1145. pmode->type |= mode->type;
  1146. drm_mode_copy(mode, pmode);
  1147. } else {
  1148. mode->type |= pmode->type;
  1149. }
  1150. list_del(&pmode->head);
  1151. drm_mode_destroy(connector->dev, pmode);
  1152. break;
  1153. }
  1154. if (!found_it) {
  1155. list_move_tail(&pmode->head, &connector->modes);
  1156. }
  1157. }
  1158. }
  1159. EXPORT_SYMBOL(drm_mode_connector_list_update);
  1160. /**
  1161. * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
  1162. * @mode_option: optional per connector mode option
  1163. * @connector: connector to parse modeline for
  1164. * @mode: preallocated drm_cmdline_mode structure to fill out
  1165. *
  1166. * This parses @mode_option command line modeline for modes and options to
  1167. * configure the connector. If @mode_option is NULL the default command line
  1168. * modeline in fb_mode_option will be parsed instead.
  1169. *
  1170. * This uses the same parameters as the fb modedb.c, except for an extra
  1171. * force-enable, force-enable-digital and force-disable bit at the end:
  1172. *
  1173. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  1174. *
  1175. * The intermediate drm_cmdline_mode structure is required to store additional
  1176. * options from the command line modline like the force-enable/disable flag.
  1177. *
  1178. * Returns:
  1179. * True if a valid modeline has been parsed, false otherwise.
  1180. */
  1181. bool drm_mode_parse_command_line_for_connector(const char *mode_option,
  1182. struct drm_connector *connector,
  1183. struct drm_cmdline_mode *mode)
  1184. {
  1185. const char *name;
  1186. unsigned int namelen;
  1187. bool res_specified = false, bpp_specified = false, refresh_specified = false;
  1188. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  1189. bool yres_specified = false, cvt = false, rb = false;
  1190. bool interlace = false, margins = false, was_digit = false;
  1191. int i;
  1192. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  1193. #ifdef CONFIG_FB
  1194. if (!mode_option)
  1195. mode_option = fb_mode_option;
  1196. #endif
  1197. if (!mode_option) {
  1198. mode->specified = false;
  1199. return false;
  1200. }
  1201. name = mode_option;
  1202. namelen = strlen(name);
  1203. for (i = namelen-1; i >= 0; i--) {
  1204. switch (name[i]) {
  1205. case '@':
  1206. if (!refresh_specified && !bpp_specified &&
  1207. !yres_specified && !cvt && !rb && was_digit) {
  1208. refresh = simple_strtol(&name[i+1], NULL, 10);
  1209. refresh_specified = true;
  1210. was_digit = false;
  1211. } else
  1212. goto done;
  1213. break;
  1214. case '-':
  1215. if (!bpp_specified && !yres_specified && !cvt &&
  1216. !rb && was_digit) {
  1217. bpp = simple_strtol(&name[i+1], NULL, 10);
  1218. bpp_specified = true;
  1219. was_digit = false;
  1220. } else
  1221. goto done;
  1222. break;
  1223. case 'x':
  1224. if (!yres_specified && was_digit) {
  1225. yres = simple_strtol(&name[i+1], NULL, 10);
  1226. yres_specified = true;
  1227. was_digit = false;
  1228. } else
  1229. goto done;
  1230. break;
  1231. case '0' ... '9':
  1232. was_digit = true;
  1233. break;
  1234. case 'M':
  1235. if (yres_specified || cvt || was_digit)
  1236. goto done;
  1237. cvt = true;
  1238. break;
  1239. case 'R':
  1240. if (yres_specified || cvt || rb || was_digit)
  1241. goto done;
  1242. rb = true;
  1243. break;
  1244. case 'm':
  1245. if (cvt || yres_specified || was_digit)
  1246. goto done;
  1247. margins = true;
  1248. break;
  1249. case 'i':
  1250. if (cvt || yres_specified || was_digit)
  1251. goto done;
  1252. interlace = true;
  1253. break;
  1254. case 'e':
  1255. if (yres_specified || bpp_specified || refresh_specified ||
  1256. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1257. goto done;
  1258. force = DRM_FORCE_ON;
  1259. break;
  1260. case 'D':
  1261. if (yres_specified || bpp_specified || refresh_specified ||
  1262. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1263. goto done;
  1264. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  1265. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  1266. force = DRM_FORCE_ON;
  1267. else
  1268. force = DRM_FORCE_ON_DIGITAL;
  1269. break;
  1270. case 'd':
  1271. if (yres_specified || bpp_specified || refresh_specified ||
  1272. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1273. goto done;
  1274. force = DRM_FORCE_OFF;
  1275. break;
  1276. default:
  1277. goto done;
  1278. }
  1279. }
  1280. if (i < 0 && yres_specified) {
  1281. char *ch;
  1282. xres = simple_strtol(name, &ch, 10);
  1283. if ((ch != NULL) && (*ch == 'x'))
  1284. res_specified = true;
  1285. else
  1286. i = ch - name;
  1287. } else if (!yres_specified && was_digit) {
  1288. /* catch mode that begins with digits but has no 'x' */
  1289. i = 0;
  1290. }
  1291. done:
  1292. if (i >= 0) {
  1293. pr_warn("[drm] parse error at position %i in video mode '%s'\n",
  1294. i, name);
  1295. mode->specified = false;
  1296. return false;
  1297. }
  1298. if (res_specified) {
  1299. mode->specified = true;
  1300. mode->xres = xres;
  1301. mode->yres = yres;
  1302. }
  1303. if (refresh_specified) {
  1304. mode->refresh_specified = true;
  1305. mode->refresh = refresh;
  1306. }
  1307. if (bpp_specified) {
  1308. mode->bpp_specified = true;
  1309. mode->bpp = bpp;
  1310. }
  1311. mode->rb = rb;
  1312. mode->cvt = cvt;
  1313. mode->interlace = interlace;
  1314. mode->margins = margins;
  1315. mode->force = force;
  1316. return true;
  1317. }
  1318. EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
  1319. /**
  1320. * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
  1321. * @dev: DRM device to create the new mode for
  1322. * @cmd: input command line modeline
  1323. *
  1324. * Returns:
  1325. * Pointer to converted mode on success, NULL on error.
  1326. */
  1327. struct drm_display_mode *
  1328. drm_mode_create_from_cmdline_mode(struct drm_device *dev,
  1329. struct drm_cmdline_mode *cmd)
  1330. {
  1331. struct drm_display_mode *mode;
  1332. if (cmd->cvt)
  1333. mode = drm_cvt_mode(dev,
  1334. cmd->xres, cmd->yres,
  1335. cmd->refresh_specified ? cmd->refresh : 60,
  1336. cmd->rb, cmd->interlace,
  1337. cmd->margins);
  1338. else
  1339. mode = drm_gtf_mode(dev,
  1340. cmd->xres, cmd->yres,
  1341. cmd->refresh_specified ? cmd->refresh : 60,
  1342. cmd->interlace,
  1343. cmd->margins);
  1344. if (!mode)
  1345. return NULL;
  1346. mode->type |= DRM_MODE_TYPE_USERDEF;
  1347. /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
  1348. if (cmd->xres == 1366)
  1349. drm_mode_fixup_1366x768(mode);
  1350. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1351. return mode;
  1352. }
  1353. EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
  1354. /**
  1355. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1356. * @out: drm_mode_modeinfo struct to return to the user
  1357. * @in: drm_display_mode to use
  1358. *
  1359. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1360. * the user.
  1361. */
  1362. void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
  1363. const struct drm_display_mode *in)
  1364. {
  1365. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1366. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1367. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1368. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1369. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1370. "timing values too large for mode info\n");
  1371. out->clock = in->clock;
  1372. out->hdisplay = in->hdisplay;
  1373. out->hsync_start = in->hsync_start;
  1374. out->hsync_end = in->hsync_end;
  1375. out->htotal = in->htotal;
  1376. out->hskew = in->hskew;
  1377. out->vdisplay = in->vdisplay;
  1378. out->vsync_start = in->vsync_start;
  1379. out->vsync_end = in->vsync_end;
  1380. out->vtotal = in->vtotal;
  1381. out->vscan = in->vscan;
  1382. out->vrefresh = in->vrefresh;
  1383. out->flags = in->flags;
  1384. out->type = in->type;
  1385. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1386. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1387. }
  1388. /**
  1389. * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
  1390. * @out: drm_display_mode to return to the user
  1391. * @in: drm_mode_modeinfo to use
  1392. *
  1393. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1394. * the caller.
  1395. *
  1396. * Returns:
  1397. * Zero on success, negative errno on failure.
  1398. */
  1399. int drm_mode_convert_umode(struct drm_display_mode *out,
  1400. const struct drm_mode_modeinfo *in)
  1401. {
  1402. int ret = -EINVAL;
  1403. if (in->clock > INT_MAX || in->vrefresh > INT_MAX) {
  1404. ret = -ERANGE;
  1405. goto out;
  1406. }
  1407. if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1408. goto out;
  1409. out->clock = in->clock;
  1410. out->hdisplay = in->hdisplay;
  1411. out->hsync_start = in->hsync_start;
  1412. out->hsync_end = in->hsync_end;
  1413. out->htotal = in->htotal;
  1414. out->hskew = in->hskew;
  1415. out->vdisplay = in->vdisplay;
  1416. out->vsync_start = in->vsync_start;
  1417. out->vsync_end = in->vsync_end;
  1418. out->vtotal = in->vtotal;
  1419. out->vscan = in->vscan;
  1420. out->vrefresh = in->vrefresh;
  1421. out->flags = in->flags;
  1422. out->type = in->type;
  1423. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1424. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1425. out->status = drm_mode_validate_basic(out);
  1426. if (out->status != MODE_OK)
  1427. goto out;
  1428. drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
  1429. ret = 0;
  1430. out:
  1431. return ret;
  1432. }