drm_modes.c 36 KB

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