drm_modes.c 47 KB

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