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