drm_modes.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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_add(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("%pOF: got %dx%d display mode from %s\n",
  655. 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. if (mode->vrefresh > 0)
  710. refresh = mode->vrefresh;
  711. else if (mode->htotal > 0 && mode->vtotal > 0) {
  712. unsigned int num, den;
  713. num = mode->clock * 1000;
  714. den = mode->htotal * mode->vtotal;
  715. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  716. num *= 2;
  717. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  718. den *= 2;
  719. if (mode->vscan > 1)
  720. den *= mode->vscan;
  721. refresh = DIV_ROUND_CLOSEST(num, den);
  722. }
  723. return refresh;
  724. }
  725. EXPORT_SYMBOL(drm_mode_vrefresh);
  726. /**
  727. * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  728. * @mode: mode to query
  729. * @hdisplay: hdisplay value to fill in
  730. * @vdisplay: vdisplay value to fill in
  731. *
  732. * The vdisplay value will be doubled if the specified mode is a stereo mode of
  733. * the appropriate layout.
  734. */
  735. void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
  736. int *hdisplay, int *vdisplay)
  737. {
  738. struct drm_display_mode adjusted = *mode;
  739. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  740. *hdisplay = adjusted.crtc_hdisplay;
  741. *vdisplay = adjusted.crtc_vdisplay;
  742. }
  743. EXPORT_SYMBOL(drm_mode_get_hv_timing);
  744. /**
  745. * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
  746. * @p: mode
  747. * @adjust_flags: a combination of adjustment flags
  748. *
  749. * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
  750. *
  751. * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
  752. * interlaced modes.
  753. * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
  754. * buffers containing two eyes (only adjust the timings when needed, eg. for
  755. * "frame packing" or "side by side full").
  756. * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
  757. * be performed for doublescan and vscan > 1 modes respectively.
  758. */
  759. void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
  760. {
  761. if (!p)
  762. return;
  763. p->crtc_clock = p->clock;
  764. p->crtc_hdisplay = p->hdisplay;
  765. p->crtc_hsync_start = p->hsync_start;
  766. p->crtc_hsync_end = p->hsync_end;
  767. p->crtc_htotal = p->htotal;
  768. p->crtc_hskew = p->hskew;
  769. p->crtc_vdisplay = p->vdisplay;
  770. p->crtc_vsync_start = p->vsync_start;
  771. p->crtc_vsync_end = p->vsync_end;
  772. p->crtc_vtotal = p->vtotal;
  773. if (p->flags & DRM_MODE_FLAG_INTERLACE) {
  774. if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
  775. p->crtc_vdisplay /= 2;
  776. p->crtc_vsync_start /= 2;
  777. p->crtc_vsync_end /= 2;
  778. p->crtc_vtotal /= 2;
  779. }
  780. }
  781. if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
  782. if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
  783. p->crtc_vdisplay *= 2;
  784. p->crtc_vsync_start *= 2;
  785. p->crtc_vsync_end *= 2;
  786. p->crtc_vtotal *= 2;
  787. }
  788. }
  789. if (!(adjust_flags & CRTC_NO_VSCAN)) {
  790. if (p->vscan > 1) {
  791. p->crtc_vdisplay *= p->vscan;
  792. p->crtc_vsync_start *= p->vscan;
  793. p->crtc_vsync_end *= p->vscan;
  794. p->crtc_vtotal *= p->vscan;
  795. }
  796. }
  797. if (adjust_flags & CRTC_STEREO_DOUBLE) {
  798. unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
  799. switch (layout) {
  800. case DRM_MODE_FLAG_3D_FRAME_PACKING:
  801. p->crtc_clock *= 2;
  802. p->crtc_vdisplay += p->crtc_vtotal;
  803. p->crtc_vsync_start += p->crtc_vtotal;
  804. p->crtc_vsync_end += p->crtc_vtotal;
  805. p->crtc_vtotal += p->crtc_vtotal;
  806. break;
  807. }
  808. }
  809. p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
  810. p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
  811. p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
  812. p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
  813. }
  814. EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  815. /**
  816. * drm_mode_copy - copy the mode
  817. * @dst: mode to overwrite
  818. * @src: mode to copy
  819. *
  820. * Copy an existing mode into another mode, preserving the object id and
  821. * list head of the destination mode.
  822. */
  823. void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
  824. {
  825. int id = dst->base.id;
  826. struct list_head head = dst->head;
  827. *dst = *src;
  828. dst->base.id = id;
  829. dst->head = head;
  830. }
  831. EXPORT_SYMBOL(drm_mode_copy);
  832. /**
  833. * drm_mode_duplicate - allocate and duplicate an existing mode
  834. * @dev: drm_device to allocate the duplicated mode for
  835. * @mode: mode to duplicate
  836. *
  837. * Just allocate a new mode, copy the existing mode into it, and return
  838. * a pointer to it. Used to create new instances of established modes.
  839. *
  840. * Returns:
  841. * Pointer to duplicated mode on success, NULL on error.
  842. */
  843. struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
  844. const struct drm_display_mode *mode)
  845. {
  846. struct drm_display_mode *nmode;
  847. nmode = drm_mode_create(dev);
  848. if (!nmode)
  849. return NULL;
  850. drm_mode_copy(nmode, mode);
  851. return nmode;
  852. }
  853. EXPORT_SYMBOL(drm_mode_duplicate);
  854. static bool drm_mode_match_timings(const struct drm_display_mode *mode1,
  855. const struct drm_display_mode *mode2)
  856. {
  857. return mode1->hdisplay == mode2->hdisplay &&
  858. mode1->hsync_start == mode2->hsync_start &&
  859. mode1->hsync_end == mode2->hsync_end &&
  860. mode1->htotal == mode2->htotal &&
  861. mode1->hskew == mode2->hskew &&
  862. mode1->vdisplay == mode2->vdisplay &&
  863. mode1->vsync_start == mode2->vsync_start &&
  864. mode1->vsync_end == mode2->vsync_end &&
  865. mode1->vtotal == mode2->vtotal &&
  866. mode1->vscan == mode2->vscan;
  867. }
  868. static bool drm_mode_match_clock(const struct drm_display_mode *mode1,
  869. const struct drm_display_mode *mode2)
  870. {
  871. /*
  872. * do clock check convert to PICOS
  873. * so fb modes get matched the same
  874. */
  875. if (mode1->clock && mode2->clock)
  876. return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock);
  877. else
  878. return mode1->clock == mode2->clock;
  879. }
  880. static bool drm_mode_match_flags(const struct drm_display_mode *mode1,
  881. const struct drm_display_mode *mode2)
  882. {
  883. return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
  884. (mode2->flags & ~DRM_MODE_FLAG_3D_MASK);
  885. }
  886. static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1,
  887. const struct drm_display_mode *mode2)
  888. {
  889. return (mode1->flags & DRM_MODE_FLAG_3D_MASK) ==
  890. (mode2->flags & DRM_MODE_FLAG_3D_MASK);
  891. }
  892. static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1,
  893. const struct drm_display_mode *mode2)
  894. {
  895. return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio;
  896. }
  897. /**
  898. * drm_mode_match - test modes for (partial) equality
  899. * @mode1: first mode
  900. * @mode2: second mode
  901. * @match_flags: which parts need to match (DRM_MODE_MATCH_*)
  902. *
  903. * Check to see if @mode1 and @mode2 are equivalent.
  904. *
  905. * Returns:
  906. * True if the modes are (partially) equal, false otherwise.
  907. */
  908. bool drm_mode_match(const struct drm_display_mode *mode1,
  909. const struct drm_display_mode *mode2,
  910. unsigned int match_flags)
  911. {
  912. if (!mode1 && !mode2)
  913. return true;
  914. if (!mode1 || !mode2)
  915. return false;
  916. if (match_flags & DRM_MODE_MATCH_TIMINGS &&
  917. !drm_mode_match_timings(mode1, mode2))
  918. return false;
  919. if (match_flags & DRM_MODE_MATCH_CLOCK &&
  920. !drm_mode_match_clock(mode1, mode2))
  921. return false;
  922. if (match_flags & DRM_MODE_MATCH_FLAGS &&
  923. !drm_mode_match_flags(mode1, mode2))
  924. return false;
  925. if (match_flags & DRM_MODE_MATCH_3D_FLAGS &&
  926. !drm_mode_match_3d_flags(mode1, mode2))
  927. return false;
  928. if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO &&
  929. !drm_mode_match_aspect_ratio(mode1, mode2))
  930. return false;
  931. return true;
  932. }
  933. EXPORT_SYMBOL(drm_mode_match);
  934. /**
  935. * drm_mode_equal - test modes for equality
  936. * @mode1: first mode
  937. * @mode2: second mode
  938. *
  939. * Check to see if @mode1 and @mode2 are equivalent.
  940. *
  941. * Returns:
  942. * True if the modes are equal, false otherwise.
  943. */
  944. bool drm_mode_equal(const struct drm_display_mode *mode1,
  945. const struct drm_display_mode *mode2)
  946. {
  947. return drm_mode_match(mode1, mode2,
  948. DRM_MODE_MATCH_TIMINGS |
  949. DRM_MODE_MATCH_CLOCK |
  950. DRM_MODE_MATCH_FLAGS |
  951. DRM_MODE_MATCH_3D_FLAGS|
  952. DRM_MODE_MATCH_ASPECT_RATIO);
  953. }
  954. EXPORT_SYMBOL(drm_mode_equal);
  955. /**
  956. * drm_mode_equal_no_clocks - test modes for equality
  957. * @mode1: first mode
  958. * @mode2: second mode
  959. *
  960. * Check to see if @mode1 and @mode2 are equivalent, but
  961. * don't check the pixel clocks.
  962. *
  963. * Returns:
  964. * True if the modes are equal, false otherwise.
  965. */
  966. bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1,
  967. const struct drm_display_mode *mode2)
  968. {
  969. return drm_mode_match(mode1, mode2,
  970. DRM_MODE_MATCH_TIMINGS |
  971. DRM_MODE_MATCH_FLAGS |
  972. DRM_MODE_MATCH_3D_FLAGS);
  973. }
  974. EXPORT_SYMBOL(drm_mode_equal_no_clocks);
  975. /**
  976. * drm_mode_equal_no_clocks_no_stereo - test modes for equality
  977. * @mode1: first mode
  978. * @mode2: second mode
  979. *
  980. * Check to see if @mode1 and @mode2 are equivalent, but
  981. * don't check the pixel clocks nor the stereo layout.
  982. *
  983. * Returns:
  984. * True if the modes are equal, false otherwise.
  985. */
  986. bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
  987. const struct drm_display_mode *mode2)
  988. {
  989. return drm_mode_match(mode1, mode2,
  990. DRM_MODE_MATCH_TIMINGS |
  991. DRM_MODE_MATCH_FLAGS);
  992. }
  993. EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
  994. static enum drm_mode_status
  995. drm_mode_validate_basic(const struct drm_display_mode *mode)
  996. {
  997. if (mode->type & ~DRM_MODE_TYPE_ALL)
  998. return MODE_BAD;
  999. if (mode->flags & ~DRM_MODE_FLAG_ALL)
  1000. return MODE_BAD;
  1001. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1002. return MODE_BAD;
  1003. if (mode->clock == 0)
  1004. return MODE_CLOCK_LOW;
  1005. if (mode->hdisplay == 0 ||
  1006. mode->hsync_start < mode->hdisplay ||
  1007. mode->hsync_end < mode->hsync_start ||
  1008. mode->htotal < mode->hsync_end)
  1009. return MODE_H_ILLEGAL;
  1010. if (mode->vdisplay == 0 ||
  1011. mode->vsync_start < mode->vdisplay ||
  1012. mode->vsync_end < mode->vsync_start ||
  1013. mode->vtotal < mode->vsync_end)
  1014. return MODE_V_ILLEGAL;
  1015. return MODE_OK;
  1016. }
  1017. /**
  1018. * drm_mode_validate_driver - make sure the mode is somewhat sane
  1019. * @dev: drm device
  1020. * @mode: mode to check
  1021. *
  1022. * First do basic validation on the mode, and then allow the driver
  1023. * to check for device/driver specific limitations via the optional
  1024. * &drm_mode_config_helper_funcs.mode_valid hook.
  1025. *
  1026. * Returns:
  1027. * The mode status
  1028. */
  1029. enum drm_mode_status
  1030. drm_mode_validate_driver(struct drm_device *dev,
  1031. const struct drm_display_mode *mode)
  1032. {
  1033. enum drm_mode_status status;
  1034. status = drm_mode_validate_basic(mode);
  1035. if (status != MODE_OK)
  1036. return status;
  1037. if (dev->mode_config.funcs->mode_valid)
  1038. return dev->mode_config.funcs->mode_valid(dev, mode);
  1039. else
  1040. return MODE_OK;
  1041. }
  1042. EXPORT_SYMBOL(drm_mode_validate_driver);
  1043. /**
  1044. * drm_mode_validate_size - make sure modes adhere to size constraints
  1045. * @mode: mode to check
  1046. * @maxX: maximum width
  1047. * @maxY: maximum height
  1048. *
  1049. * This function is a helper which can be used to validate modes against size
  1050. * limitations of the DRM device/connector. If a mode is too big its status
  1051. * member is updated with the appropriate validation failure code. The list
  1052. * itself is not changed.
  1053. *
  1054. * Returns:
  1055. * The mode status
  1056. */
  1057. enum drm_mode_status
  1058. drm_mode_validate_size(const struct drm_display_mode *mode,
  1059. int maxX, int maxY)
  1060. {
  1061. if (maxX > 0 && mode->hdisplay > maxX)
  1062. return MODE_VIRTUAL_X;
  1063. if (maxY > 0 && mode->vdisplay > maxY)
  1064. return MODE_VIRTUAL_Y;
  1065. return MODE_OK;
  1066. }
  1067. EXPORT_SYMBOL(drm_mode_validate_size);
  1068. /**
  1069. * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
  1070. * @mode: mode to check
  1071. * @connector: drm connector under action
  1072. *
  1073. * This function is a helper which can be used to filter out any YCBCR420
  1074. * only mode, when the source doesn't support it.
  1075. *
  1076. * Returns:
  1077. * The mode status
  1078. */
  1079. enum drm_mode_status
  1080. drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
  1081. struct drm_connector *connector)
  1082. {
  1083. u8 vic = drm_match_cea_mode(mode);
  1084. enum drm_mode_status status = MODE_OK;
  1085. struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
  1086. if (test_bit(vic, hdmi->y420_vdb_modes)) {
  1087. if (!connector->ycbcr_420_allowed)
  1088. status = MODE_NO_420;
  1089. }
  1090. return status;
  1091. }
  1092. EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
  1093. #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
  1094. static const char * const drm_mode_status_names[] = {
  1095. MODE_STATUS(OK),
  1096. MODE_STATUS(HSYNC),
  1097. MODE_STATUS(VSYNC),
  1098. MODE_STATUS(H_ILLEGAL),
  1099. MODE_STATUS(V_ILLEGAL),
  1100. MODE_STATUS(BAD_WIDTH),
  1101. MODE_STATUS(NOMODE),
  1102. MODE_STATUS(NO_INTERLACE),
  1103. MODE_STATUS(NO_DBLESCAN),
  1104. MODE_STATUS(NO_VSCAN),
  1105. MODE_STATUS(MEM),
  1106. MODE_STATUS(VIRTUAL_X),
  1107. MODE_STATUS(VIRTUAL_Y),
  1108. MODE_STATUS(MEM_VIRT),
  1109. MODE_STATUS(NOCLOCK),
  1110. MODE_STATUS(CLOCK_HIGH),
  1111. MODE_STATUS(CLOCK_LOW),
  1112. MODE_STATUS(CLOCK_RANGE),
  1113. MODE_STATUS(BAD_HVALUE),
  1114. MODE_STATUS(BAD_VVALUE),
  1115. MODE_STATUS(BAD_VSCAN),
  1116. MODE_STATUS(HSYNC_NARROW),
  1117. MODE_STATUS(HSYNC_WIDE),
  1118. MODE_STATUS(HBLANK_NARROW),
  1119. MODE_STATUS(HBLANK_WIDE),
  1120. MODE_STATUS(VSYNC_NARROW),
  1121. MODE_STATUS(VSYNC_WIDE),
  1122. MODE_STATUS(VBLANK_NARROW),
  1123. MODE_STATUS(VBLANK_WIDE),
  1124. MODE_STATUS(PANEL),
  1125. MODE_STATUS(INTERLACE_WIDTH),
  1126. MODE_STATUS(ONE_WIDTH),
  1127. MODE_STATUS(ONE_HEIGHT),
  1128. MODE_STATUS(ONE_SIZE),
  1129. MODE_STATUS(NO_REDUCED),
  1130. MODE_STATUS(NO_STEREO),
  1131. MODE_STATUS(NO_420),
  1132. MODE_STATUS(STALE),
  1133. MODE_STATUS(BAD),
  1134. MODE_STATUS(ERROR),
  1135. };
  1136. #undef MODE_STATUS
  1137. static const char *drm_get_mode_status_name(enum drm_mode_status status)
  1138. {
  1139. int index = status + 3;
  1140. if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
  1141. return "";
  1142. return drm_mode_status_names[index];
  1143. }
  1144. /**
  1145. * drm_mode_prune_invalid - remove invalid modes from mode list
  1146. * @dev: DRM device
  1147. * @mode_list: list of modes to check
  1148. * @verbose: be verbose about it
  1149. *
  1150. * This helper function can be used to prune a display mode list after
  1151. * validation has been completed. All modes who's status is not MODE_OK will be
  1152. * removed from the list, and if @verbose the status code and mode name is also
  1153. * printed to dmesg.
  1154. */
  1155. void drm_mode_prune_invalid(struct drm_device *dev,
  1156. struct list_head *mode_list, bool verbose)
  1157. {
  1158. struct drm_display_mode *mode, *t;
  1159. list_for_each_entry_safe(mode, t, mode_list, head) {
  1160. if (mode->status != MODE_OK) {
  1161. list_del(&mode->head);
  1162. if (verbose) {
  1163. drm_mode_debug_printmodeline(mode);
  1164. DRM_DEBUG_KMS("Not using %s mode: %s\n",
  1165. mode->name,
  1166. drm_get_mode_status_name(mode->status));
  1167. }
  1168. drm_mode_destroy(dev, mode);
  1169. }
  1170. }
  1171. }
  1172. EXPORT_SYMBOL(drm_mode_prune_invalid);
  1173. /**
  1174. * drm_mode_compare - compare modes for favorability
  1175. * @priv: unused
  1176. * @lh_a: list_head for first mode
  1177. * @lh_b: list_head for second mode
  1178. *
  1179. * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
  1180. * which is better.
  1181. *
  1182. * Returns:
  1183. * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
  1184. * positive if @lh_b is better than @lh_a.
  1185. */
  1186. static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
  1187. {
  1188. struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
  1189. struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
  1190. int diff;
  1191. diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
  1192. ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
  1193. if (diff)
  1194. return diff;
  1195. diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
  1196. if (diff)
  1197. return diff;
  1198. diff = b->vrefresh - a->vrefresh;
  1199. if (diff)
  1200. return diff;
  1201. diff = b->clock - a->clock;
  1202. return diff;
  1203. }
  1204. /**
  1205. * drm_mode_sort - sort mode list
  1206. * @mode_list: list of drm_display_mode structures to sort
  1207. *
  1208. * Sort @mode_list by favorability, moving good modes to the head of the list.
  1209. */
  1210. void drm_mode_sort(struct list_head *mode_list)
  1211. {
  1212. list_sort(NULL, mode_list, drm_mode_compare);
  1213. }
  1214. EXPORT_SYMBOL(drm_mode_sort);
  1215. /**
  1216. * drm_mode_connector_list_update - update the mode list for the connector
  1217. * @connector: the connector to update
  1218. *
  1219. * This moves the modes from the @connector probed_modes list
  1220. * to the actual mode list. It compares the probed mode against the current
  1221. * list and only adds different/new modes.
  1222. *
  1223. * This is just a helper functions doesn't validate any modes itself and also
  1224. * doesn't prune any invalid modes. Callers need to do that themselves.
  1225. */
  1226. void drm_mode_connector_list_update(struct drm_connector *connector)
  1227. {
  1228. struct drm_display_mode *pmode, *pt;
  1229. WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
  1230. list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
  1231. struct drm_display_mode *mode;
  1232. bool found_it = false;
  1233. /* go through current modes checking for the new probed mode */
  1234. list_for_each_entry(mode, &connector->modes, head) {
  1235. if (!drm_mode_equal(pmode, mode))
  1236. continue;
  1237. found_it = true;
  1238. /*
  1239. * If the old matching mode is stale (ie. left over
  1240. * from a previous probe) just replace it outright.
  1241. * Otherwise just merge the type bits between all
  1242. * equal probed modes.
  1243. *
  1244. * If two probed modes are considered equal, pick the
  1245. * actual timings from the one that's marked as
  1246. * preferred (in case the match isn't 100%). If
  1247. * multiple or zero preferred modes are present, favor
  1248. * the mode added to the probed_modes list first.
  1249. */
  1250. if (mode->status == MODE_STALE) {
  1251. drm_mode_copy(mode, pmode);
  1252. } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
  1253. (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
  1254. pmode->type |= mode->type;
  1255. drm_mode_copy(mode, pmode);
  1256. } else {
  1257. mode->type |= pmode->type;
  1258. }
  1259. list_del(&pmode->head);
  1260. drm_mode_destroy(connector->dev, pmode);
  1261. break;
  1262. }
  1263. if (!found_it) {
  1264. list_move_tail(&pmode->head, &connector->modes);
  1265. }
  1266. }
  1267. }
  1268. EXPORT_SYMBOL(drm_mode_connector_list_update);
  1269. /**
  1270. * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
  1271. * @mode_option: optional per connector mode option
  1272. * @connector: connector to parse modeline for
  1273. * @mode: preallocated drm_cmdline_mode structure to fill out
  1274. *
  1275. * This parses @mode_option command line modeline for modes and options to
  1276. * configure the connector. If @mode_option is NULL the default command line
  1277. * modeline in fb_mode_option will be parsed instead.
  1278. *
  1279. * This uses the same parameters as the fb modedb.c, except for an extra
  1280. * force-enable, force-enable-digital and force-disable bit at the end::
  1281. *
  1282. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  1283. *
  1284. * The intermediate drm_cmdline_mode structure is required to store additional
  1285. * options from the command line modline like the force-enable/disable flag.
  1286. *
  1287. * Returns:
  1288. * True if a valid modeline has been parsed, false otherwise.
  1289. */
  1290. bool drm_mode_parse_command_line_for_connector(const char *mode_option,
  1291. struct drm_connector *connector,
  1292. struct drm_cmdline_mode *mode)
  1293. {
  1294. const char *name;
  1295. unsigned int namelen;
  1296. bool res_specified = false, bpp_specified = false, refresh_specified = false;
  1297. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  1298. bool yres_specified = false, cvt = false, rb = false;
  1299. bool interlace = false, margins = false, was_digit = false;
  1300. int i;
  1301. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  1302. #ifdef CONFIG_FB
  1303. if (!mode_option)
  1304. mode_option = fb_mode_option;
  1305. #endif
  1306. if (!mode_option) {
  1307. mode->specified = false;
  1308. return false;
  1309. }
  1310. name = mode_option;
  1311. namelen = strlen(name);
  1312. for (i = namelen-1; i >= 0; i--) {
  1313. switch (name[i]) {
  1314. case '@':
  1315. if (!refresh_specified && !bpp_specified &&
  1316. !yres_specified && !cvt && !rb && was_digit) {
  1317. refresh = simple_strtol(&name[i+1], NULL, 10);
  1318. refresh_specified = true;
  1319. was_digit = false;
  1320. } else
  1321. goto done;
  1322. break;
  1323. case '-':
  1324. if (!bpp_specified && !yres_specified && !cvt &&
  1325. !rb && was_digit) {
  1326. bpp = simple_strtol(&name[i+1], NULL, 10);
  1327. bpp_specified = true;
  1328. was_digit = false;
  1329. } else
  1330. goto done;
  1331. break;
  1332. case 'x':
  1333. if (!yres_specified && was_digit) {
  1334. yres = simple_strtol(&name[i+1], NULL, 10);
  1335. yres_specified = true;
  1336. was_digit = false;
  1337. } else
  1338. goto done;
  1339. break;
  1340. case '0' ... '9':
  1341. was_digit = true;
  1342. break;
  1343. case 'M':
  1344. if (yres_specified || cvt || was_digit)
  1345. goto done;
  1346. cvt = true;
  1347. break;
  1348. case 'R':
  1349. if (yres_specified || cvt || rb || was_digit)
  1350. goto done;
  1351. rb = true;
  1352. break;
  1353. case 'm':
  1354. if (cvt || yres_specified || was_digit)
  1355. goto done;
  1356. margins = true;
  1357. break;
  1358. case 'i':
  1359. if (cvt || yres_specified || was_digit)
  1360. goto done;
  1361. interlace = true;
  1362. break;
  1363. case 'e':
  1364. if (yres_specified || bpp_specified || refresh_specified ||
  1365. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1366. goto done;
  1367. force = DRM_FORCE_ON;
  1368. break;
  1369. case 'D':
  1370. if (yres_specified || bpp_specified || refresh_specified ||
  1371. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1372. goto done;
  1373. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  1374. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  1375. force = DRM_FORCE_ON;
  1376. else
  1377. force = DRM_FORCE_ON_DIGITAL;
  1378. break;
  1379. case 'd':
  1380. if (yres_specified || bpp_specified || refresh_specified ||
  1381. was_digit || (force != DRM_FORCE_UNSPECIFIED))
  1382. goto done;
  1383. force = DRM_FORCE_OFF;
  1384. break;
  1385. default:
  1386. goto done;
  1387. }
  1388. }
  1389. if (i < 0 && yres_specified) {
  1390. char *ch;
  1391. xres = simple_strtol(name, &ch, 10);
  1392. if ((ch != NULL) && (*ch == 'x'))
  1393. res_specified = true;
  1394. else
  1395. i = ch - name;
  1396. } else if (!yres_specified && was_digit) {
  1397. /* catch mode that begins with digits but has no 'x' */
  1398. i = 0;
  1399. }
  1400. done:
  1401. if (i >= 0) {
  1402. pr_warn("[drm] parse error at position %i in video mode '%s'\n",
  1403. i, name);
  1404. mode->specified = false;
  1405. return false;
  1406. }
  1407. if (res_specified) {
  1408. mode->specified = true;
  1409. mode->xres = xres;
  1410. mode->yres = yres;
  1411. }
  1412. if (refresh_specified) {
  1413. mode->refresh_specified = true;
  1414. mode->refresh = refresh;
  1415. }
  1416. if (bpp_specified) {
  1417. mode->bpp_specified = true;
  1418. mode->bpp = bpp;
  1419. }
  1420. mode->rb = rb;
  1421. mode->cvt = cvt;
  1422. mode->interlace = interlace;
  1423. mode->margins = margins;
  1424. mode->force = force;
  1425. return true;
  1426. }
  1427. EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
  1428. /**
  1429. * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
  1430. * @dev: DRM device to create the new mode for
  1431. * @cmd: input command line modeline
  1432. *
  1433. * Returns:
  1434. * Pointer to converted mode on success, NULL on error.
  1435. */
  1436. struct drm_display_mode *
  1437. drm_mode_create_from_cmdline_mode(struct drm_device *dev,
  1438. struct drm_cmdline_mode *cmd)
  1439. {
  1440. struct drm_display_mode *mode;
  1441. if (cmd->cvt)
  1442. mode = drm_cvt_mode(dev,
  1443. cmd->xres, cmd->yres,
  1444. cmd->refresh_specified ? cmd->refresh : 60,
  1445. cmd->rb, cmd->interlace,
  1446. cmd->margins);
  1447. else
  1448. mode = drm_gtf_mode(dev,
  1449. cmd->xres, cmd->yres,
  1450. cmd->refresh_specified ? cmd->refresh : 60,
  1451. cmd->interlace,
  1452. cmd->margins);
  1453. if (!mode)
  1454. return NULL;
  1455. mode->type |= DRM_MODE_TYPE_USERDEF;
  1456. /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
  1457. if (cmd->xres == 1366)
  1458. drm_mode_fixup_1366x768(mode);
  1459. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1460. return mode;
  1461. }
  1462. EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
  1463. /**
  1464. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1465. * @out: drm_mode_modeinfo struct to return to the user
  1466. * @in: drm_display_mode to use
  1467. *
  1468. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1469. * the user.
  1470. */
  1471. void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
  1472. const struct drm_display_mode *in)
  1473. {
  1474. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1475. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1476. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1477. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1478. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1479. "timing values too large for mode info\n");
  1480. out->clock = in->clock;
  1481. out->hdisplay = in->hdisplay;
  1482. out->hsync_start = in->hsync_start;
  1483. out->hsync_end = in->hsync_end;
  1484. out->htotal = in->htotal;
  1485. out->hskew = in->hskew;
  1486. out->vdisplay = in->vdisplay;
  1487. out->vsync_start = in->vsync_start;
  1488. out->vsync_end = in->vsync_end;
  1489. out->vtotal = in->vtotal;
  1490. out->vscan = in->vscan;
  1491. out->vrefresh = in->vrefresh;
  1492. out->flags = in->flags;
  1493. out->type = in->type;
  1494. switch (in->picture_aspect_ratio) {
  1495. case HDMI_PICTURE_ASPECT_4_3:
  1496. out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
  1497. break;
  1498. case HDMI_PICTURE_ASPECT_16_9:
  1499. out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
  1500. break;
  1501. case HDMI_PICTURE_ASPECT_64_27:
  1502. out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
  1503. break;
  1504. case HDMI_PICTURE_ASPECT_256_135:
  1505. out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
  1506. break;
  1507. case HDMI_PICTURE_ASPECT_RESERVED:
  1508. default:
  1509. out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
  1510. break;
  1511. }
  1512. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1513. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1514. }
  1515. /**
  1516. * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
  1517. * @dev: drm device
  1518. * @out: drm_display_mode to return to the user
  1519. * @in: drm_mode_modeinfo to use
  1520. *
  1521. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1522. * the caller.
  1523. *
  1524. * Returns:
  1525. * Zero on success, negative errno on failure.
  1526. */
  1527. int drm_mode_convert_umode(struct drm_device *dev,
  1528. struct drm_display_mode *out,
  1529. const struct drm_mode_modeinfo *in)
  1530. {
  1531. if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1532. return -ERANGE;
  1533. out->clock = in->clock;
  1534. out->hdisplay = in->hdisplay;
  1535. out->hsync_start = in->hsync_start;
  1536. out->hsync_end = in->hsync_end;
  1537. out->htotal = in->htotal;
  1538. out->hskew = in->hskew;
  1539. out->vdisplay = in->vdisplay;
  1540. out->vsync_start = in->vsync_start;
  1541. out->vsync_end = in->vsync_end;
  1542. out->vtotal = in->vtotal;
  1543. out->vscan = in->vscan;
  1544. out->vrefresh = in->vrefresh;
  1545. out->flags = in->flags;
  1546. /*
  1547. * Old xf86-video-vmware (possibly others too) used to
  1548. * leave 'type' unititialized. Just ignore any bits we
  1549. * don't like. It's a just hint after all, and more
  1550. * useful for the kernel->userspace direction anyway.
  1551. */
  1552. out->type = in->type & DRM_MODE_TYPE_ALL;
  1553. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1554. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1555. /* Clearing picture aspect ratio bits from out flags,
  1556. * as the aspect-ratio information is not stored in
  1557. * flags for kernel-mode, but in picture_aspect_ratio.
  1558. */
  1559. out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
  1560. switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
  1561. case DRM_MODE_FLAG_PIC_AR_4_3:
  1562. out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_4_3;
  1563. break;
  1564. case DRM_MODE_FLAG_PIC_AR_16_9:
  1565. out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_16_9;
  1566. break;
  1567. case DRM_MODE_FLAG_PIC_AR_64_27:
  1568. out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_64_27;
  1569. break;
  1570. case DRM_MODE_FLAG_PIC_AR_256_135:
  1571. out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_256_135;
  1572. break;
  1573. default:
  1574. out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
  1575. break;
  1576. }
  1577. out->status = drm_mode_validate_driver(dev, out);
  1578. if (out->status != MODE_OK)
  1579. return -EINVAL;
  1580. drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
  1581. return 0;
  1582. }
  1583. /**
  1584. * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
  1585. * output format
  1586. *
  1587. * @display: display under action
  1588. * @mode: video mode to be tested.
  1589. *
  1590. * Returns:
  1591. * true if the mode can be supported in YCBCR420 format
  1592. * false if not.
  1593. */
  1594. bool drm_mode_is_420_only(const struct drm_display_info *display,
  1595. const struct drm_display_mode *mode)
  1596. {
  1597. u8 vic = drm_match_cea_mode(mode);
  1598. return test_bit(vic, display->hdmi.y420_vdb_modes);
  1599. }
  1600. EXPORT_SYMBOL(drm_mode_is_420_only);
  1601. /**
  1602. * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
  1603. * output format also (along with RGB/YCBCR444/422)
  1604. *
  1605. * @display: display under action.
  1606. * @mode: video mode to be tested.
  1607. *
  1608. * Returns:
  1609. * true if the mode can be support YCBCR420 format
  1610. * false if not.
  1611. */
  1612. bool drm_mode_is_420_also(const struct drm_display_info *display,
  1613. const struct drm_display_mode *mode)
  1614. {
  1615. u8 vic = drm_match_cea_mode(mode);
  1616. return test_bit(vic, display->hdmi.y420_cmdb_modes);
  1617. }
  1618. EXPORT_SYMBOL(drm_mode_is_420_also);
  1619. /**
  1620. * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
  1621. * output format
  1622. *
  1623. * @display: display under action.
  1624. * @mode: video mode to be tested.
  1625. *
  1626. * Returns:
  1627. * true if the mode can be supported in YCBCR420 format
  1628. * false if not.
  1629. */
  1630. bool drm_mode_is_420(const struct drm_display_info *display,
  1631. const struct drm_display_mode *mode)
  1632. {
  1633. return drm_mode_is_420_only(display, mode) ||
  1634. drm_mode_is_420_also(display, mode);
  1635. }
  1636. EXPORT_SYMBOL(drm_mode_is_420);