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