xref: /dflybsd-src/sys/dev/drm/drm_modes.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
15718399fSFrançois Tigeot /*
25718399fSFrançois Tigeot  * Copyright © 1997-2003 by The XFree86 Project, Inc.
35718399fSFrançois Tigeot  * Copyright © 2007 Dave Airlie
45718399fSFrançois Tigeot  * Copyright © 2007-2008 Intel Corporation
55718399fSFrançois Tigeot  *   Jesse Barnes <jesse.barnes@intel.com>
65718399fSFrançois Tigeot  * Copyright 2005-2006 Luc Verhaegen
75718399fSFrançois Tigeot  * Copyright (c) 2001, Andy Ritger  aritger@nvidia.com
85718399fSFrançois Tigeot  *
95718399fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
105718399fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
115718399fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
125718399fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
135718399fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
145718399fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
155718399fSFrançois Tigeot  *
165718399fSFrançois Tigeot  * The above copyright notice and this permission notice shall be included in
175718399fSFrançois Tigeot  * all copies or substantial portions of the Software.
185718399fSFrançois Tigeot  *
195718399fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
205718399fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
215718399fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
225718399fSFrançois Tigeot  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
235718399fSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
245718399fSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
255718399fSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
265718399fSFrançois Tigeot  *
275718399fSFrançois Tigeot  * Except as contained in this notice, the name of the copyright holder(s)
285718399fSFrançois Tigeot  * and author(s) shall not be used in advertising or otherwise to promote
295718399fSFrançois Tigeot  * the sale, use or other dealings in this Software without prior written
305718399fSFrançois Tigeot  * authorization from the copyright holder(s) and author(s).
315718399fSFrançois Tigeot  */
325718399fSFrançois Tigeot 
336e29dde8SFrançois Tigeot #include <linux/list.h>
34dcc49d6fSFrançois Tigeot #include <linux/list_sort.h>
35af4b81b9SFrançois Tigeot #include <linux/export.h>
3618e26a6dSFrançois Tigeot #include <drm/drmP.h>
3718e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
38a85cb24fSFrançois Tigeot #include <video/of_videomode.h>
392c9916cdSFrançois Tigeot #include <video/videomode.h>
40ba55f2f5SFrançois Tigeot #include <drm/drm_modes.h>
41ba55f2f5SFrançois Tigeot 
42ba55f2f5SFrançois Tigeot #include "drm_crtc_internal.h"
435718399fSFrançois Tigeot 
445718399fSFrançois Tigeot /**
45ba55f2f5SFrançois Tigeot  * drm_mode_debug_printmodeline - print a mode to dmesg
465718399fSFrançois Tigeot  * @mode: mode to print
475718399fSFrançois Tigeot  *
485718399fSFrançois Tigeot  * Describe @mode using DRM_DEBUG.
495718399fSFrançois Tigeot  */
drm_mode_debug_printmodeline(const struct drm_display_mode * mode)50af4b81b9SFrançois Tigeot void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
515718399fSFrançois Tigeot {
524be47400SFrançois Tigeot 	DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
535718399fSFrançois Tigeot }
54af4b81b9SFrançois Tigeot EXPORT_SYMBOL(drm_mode_debug_printmodeline);
555718399fSFrançois Tigeot 
565718399fSFrançois Tigeot /**
57ba55f2f5SFrançois Tigeot  * drm_mode_create - create a new display mode
585718399fSFrançois Tigeot  * @dev: DRM device
59ba55f2f5SFrançois Tigeot  *
60ba55f2f5SFrançois Tigeot  * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
61ba55f2f5SFrançois Tigeot  * and return it.
62ba55f2f5SFrançois Tigeot  *
63ba55f2f5SFrançois Tigeot  * Returns:
64ba55f2f5SFrançois Tigeot  * Pointer to new mode on success, NULL on error.
65ba55f2f5SFrançois Tigeot  */
drm_mode_create(struct drm_device * dev)66ba55f2f5SFrançois Tigeot struct drm_display_mode *drm_mode_create(struct drm_device *dev)
67ba55f2f5SFrançois Tigeot {
68ba55f2f5SFrançois Tigeot 	struct drm_display_mode *nmode;
69ba55f2f5SFrançois Tigeot 
70ba55f2f5SFrançois Tigeot 	nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
71ba55f2f5SFrançois Tigeot 	if (!nmode)
72ba55f2f5SFrançois Tigeot 		return NULL;
73ba55f2f5SFrançois Tigeot 
74a85cb24fSFrançois Tigeot 	if (drm_mode_object_add(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
75ba55f2f5SFrançois Tigeot 		kfree(nmode);
76ba55f2f5SFrançois Tigeot 		return NULL;
77ba55f2f5SFrançois Tigeot 	}
78ba55f2f5SFrançois Tigeot 
79ba55f2f5SFrançois Tigeot 	return nmode;
80ba55f2f5SFrançois Tigeot }
81ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_mode_create);
82ba55f2f5SFrançois Tigeot 
83ba55f2f5SFrançois Tigeot /**
84ba55f2f5SFrançois Tigeot  * drm_mode_destroy - remove a mode
85ba55f2f5SFrançois Tigeot  * @dev: DRM device
86ba55f2f5SFrançois Tigeot  * @mode: mode to remove
87ba55f2f5SFrançois Tigeot  *
88ba55f2f5SFrançois Tigeot  * Release @mode's unique ID, then free it @mode structure itself using kfree.
89ba55f2f5SFrançois Tigeot  */
drm_mode_destroy(struct drm_device * dev,struct drm_display_mode * mode)90ba55f2f5SFrançois Tigeot void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
91ba55f2f5SFrançois Tigeot {
92ba55f2f5SFrançois Tigeot 	if (!mode)
93ba55f2f5SFrançois Tigeot 		return;
94ba55f2f5SFrançois Tigeot 
958621f407SFrançois Tigeot 	drm_mode_object_unregister(dev, &mode->base);
96ba55f2f5SFrançois Tigeot 
97ba55f2f5SFrançois Tigeot 	kfree(mode);
98ba55f2f5SFrançois Tigeot }
99ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_mode_destroy);
100ba55f2f5SFrançois Tigeot 
101ba55f2f5SFrançois Tigeot /**
102ba55f2f5SFrançois Tigeot  * drm_mode_probed_add - add a mode to a connector's probed_mode list
103ba55f2f5SFrançois Tigeot  * @connector: connector the new mode
104ba55f2f5SFrançois Tigeot  * @mode: mode data
105ba55f2f5SFrançois Tigeot  *
106ba55f2f5SFrançois Tigeot  * Add @mode to @connector's probed_mode list for later use. This list should
107ba55f2f5SFrançois Tigeot  * then in a second step get filtered and all the modes actually supported by
108ba55f2f5SFrançois Tigeot  * the hardware moved to the @connector's modes list.
109ba55f2f5SFrançois Tigeot  */
drm_mode_probed_add(struct drm_connector * connector,struct drm_display_mode * mode)110ba55f2f5SFrançois Tigeot void drm_mode_probed_add(struct drm_connector *connector,
111ba55f2f5SFrançois Tigeot 			 struct drm_display_mode *mode)
112ba55f2f5SFrançois Tigeot {
113ba55f2f5SFrançois Tigeot 	WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
114ba55f2f5SFrançois Tigeot 
115ba55f2f5SFrançois Tigeot 	list_add_tail(&mode->head, &connector->probed_modes);
116ba55f2f5SFrançois Tigeot }
117ba55f2f5SFrançois Tigeot EXPORT_SYMBOL(drm_mode_probed_add);
118ba55f2f5SFrançois Tigeot 
119ba55f2f5SFrançois Tigeot /**
120ba55f2f5SFrançois Tigeot  * drm_cvt_mode -create a modeline based on the CVT algorithm
121ba55f2f5SFrançois Tigeot  * @dev: drm device
1225718399fSFrançois Tigeot  * @hdisplay: hdisplay size
1235718399fSFrançois Tigeot  * @vdisplay: vdisplay size
1245718399fSFrançois Tigeot  * @vrefresh: vrefresh rate
125ba55f2f5SFrançois Tigeot  * @reduced: whether to use reduced blanking
126ba55f2f5SFrançois Tigeot  * @interlaced: whether to compute an interlaced mode
127ba55f2f5SFrançois Tigeot  * @margins: whether to add margins (borders)
1285718399fSFrançois Tigeot  *
1295718399fSFrançois Tigeot  * This function is called to generate the modeline based on CVT algorithm
1305718399fSFrançois Tigeot  * according to the hdisplay, vdisplay, vrefresh.
1315718399fSFrançois Tigeot  * It is based from the VESA(TM) Coordinated Video Timing Generator by
1325718399fSFrançois Tigeot  * Graham Loveridge April 9, 2003 available at
1335718399fSFrançois Tigeot  * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
1345718399fSFrançois Tigeot  *
1355718399fSFrançois Tigeot  * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
1365718399fSFrançois Tigeot  * What I have done is to translate it by using integer calculation.
137ba55f2f5SFrançois Tigeot  *
138ba55f2f5SFrançois Tigeot  * Returns:
139ba55f2f5SFrançois Tigeot  * The modeline based on the CVT algorithm stored in a drm_display_mode object.
140ba55f2f5SFrançois Tigeot  * The display mode object is allocated with drm_mode_create(). Returns NULL
141ba55f2f5SFrançois Tigeot  * when no mode could be allocated.
1425718399fSFrançois Tigeot  */
drm_cvt_mode(struct drm_device * dev,int hdisplay,int vdisplay,int vrefresh,bool reduced,bool interlaced,bool margins)1435718399fSFrançois Tigeot struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
1445718399fSFrançois Tigeot 				      int vdisplay, int vrefresh,
1455718399fSFrançois Tigeot 				      bool reduced, bool interlaced, bool margins)
1465718399fSFrançois Tigeot {
147ba55f2f5SFrançois Tigeot #define HV_FACTOR			1000
1485718399fSFrançois Tigeot 	/* 1) top/bottom margin size (% of height) - default: 1.8, */
1495718399fSFrançois Tigeot #define	CVT_MARGIN_PERCENTAGE		18
1505718399fSFrançois Tigeot 	/* 2) character cell horizontal granularity (pixels) - default 8 */
1515718399fSFrançois Tigeot #define	CVT_H_GRANULARITY		8
1525718399fSFrançois Tigeot 	/* 3) Minimum vertical porch (lines) - default 3 */
1535718399fSFrançois Tigeot #define	CVT_MIN_V_PORCH			3
1545718399fSFrançois Tigeot 	/* 4) Minimum number of vertical back porch lines - default 6 */
1555718399fSFrançois Tigeot #define	CVT_MIN_V_BPORCH		6
1565718399fSFrançois Tigeot 	/* Pixel Clock step (kHz) */
1575718399fSFrançois Tigeot #define CVT_CLOCK_STEP			250
1585718399fSFrançois Tigeot 	struct drm_display_mode *drm_mode;
1595718399fSFrançois Tigeot 	unsigned int vfieldrate, hperiod;
1605718399fSFrançois Tigeot 	int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
1615718399fSFrançois Tigeot 	int interlace;
1624be47400SFrançois Tigeot 	u64 tmp;
1635718399fSFrançois Tigeot 
1645718399fSFrançois Tigeot 	/* allocate the drm_display_mode structure. If failure, we will
1655718399fSFrançois Tigeot 	 * return directly
1665718399fSFrançois Tigeot 	 */
1675718399fSFrançois Tigeot 	drm_mode = drm_mode_create(dev);
1685718399fSFrançois Tigeot 	if (!drm_mode)
1695718399fSFrançois Tigeot 		return NULL;
1705718399fSFrançois Tigeot 
1715718399fSFrançois Tigeot 	/* the CVT default refresh rate is 60Hz */
1725718399fSFrançois Tigeot 	if (!vrefresh)
1735718399fSFrançois Tigeot 		vrefresh = 60;
1745718399fSFrançois Tigeot 
1755718399fSFrançois Tigeot 	/* the required field fresh rate */
1765718399fSFrançois Tigeot 	if (interlaced)
1775718399fSFrançois Tigeot 		vfieldrate = vrefresh * 2;
1785718399fSFrançois Tigeot 	else
1795718399fSFrançois Tigeot 		vfieldrate = vrefresh;
1805718399fSFrançois Tigeot 
1815718399fSFrançois Tigeot 	/* horizontal pixels */
1825718399fSFrançois Tigeot 	hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
1835718399fSFrançois Tigeot 
1845718399fSFrançois Tigeot 	/* determine the left&right borders */
1855718399fSFrançois Tigeot 	hmargin = 0;
1865718399fSFrançois Tigeot 	if (margins) {
1875718399fSFrançois Tigeot 		hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
1885718399fSFrançois Tigeot 		hmargin -= hmargin % CVT_H_GRANULARITY;
1895718399fSFrançois Tigeot 	}
1905718399fSFrançois Tigeot 	/* find the total active pixels */
1915718399fSFrançois Tigeot 	drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
1925718399fSFrançois Tigeot 
1935718399fSFrançois Tigeot 	/* find the number of lines per field */
1945718399fSFrançois Tigeot 	if (interlaced)
1955718399fSFrançois Tigeot 		vdisplay_rnd = vdisplay / 2;
1965718399fSFrançois Tigeot 	else
1975718399fSFrançois Tigeot 		vdisplay_rnd = vdisplay;
1985718399fSFrançois Tigeot 
1995718399fSFrançois Tigeot 	/* find the top & bottom borders */
2005718399fSFrançois Tigeot 	vmargin = 0;
2015718399fSFrançois Tigeot 	if (margins)
2025718399fSFrançois Tigeot 		vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
2035718399fSFrançois Tigeot 
2045718399fSFrançois Tigeot 	drm_mode->vdisplay = vdisplay + 2 * vmargin;
2055718399fSFrançois Tigeot 
2065718399fSFrançois Tigeot 	/* Interlaced */
2075718399fSFrançois Tigeot 	if (interlaced)
2085718399fSFrançois Tigeot 		interlace = 1;
2095718399fSFrançois Tigeot 	else
2105718399fSFrançois Tigeot 		interlace = 0;
2115718399fSFrançois Tigeot 
2125718399fSFrançois Tigeot 	/* Determine VSync Width from aspect ratio */
2135718399fSFrançois Tigeot 	if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
2145718399fSFrançois Tigeot 		vsync = 4;
2155718399fSFrançois Tigeot 	else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
2165718399fSFrançois Tigeot 		vsync = 5;
2175718399fSFrançois Tigeot 	else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
2185718399fSFrançois Tigeot 		vsync = 6;
2195718399fSFrançois Tigeot 	else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
2205718399fSFrançois Tigeot 		vsync = 7;
2215718399fSFrançois Tigeot 	else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
2225718399fSFrançois Tigeot 		vsync = 7;
2235718399fSFrançois Tigeot 	else /* custom */
2245718399fSFrançois Tigeot 		vsync = 10;
2255718399fSFrançois Tigeot 
2265718399fSFrançois Tigeot 	if (!reduced) {
2275718399fSFrançois Tigeot 		/* simplify the GTF calculation */
2285718399fSFrançois Tigeot 		/* 4) Minimum time of vertical sync + back porch interval (µs)
2295718399fSFrançois Tigeot 		 * default 550.0
2305718399fSFrançois Tigeot 		 */
2315718399fSFrançois Tigeot 		int tmp1, tmp2;
2325718399fSFrançois Tigeot #define CVT_MIN_VSYNC_BP	550
2335718399fSFrançois Tigeot 		/* 3) Nominal HSync width (% of line period) - default 8 */
2345718399fSFrançois Tigeot #define CVT_HSYNC_PERCENTAGE	8
2355718399fSFrançois Tigeot 		unsigned int hblank_percentage;
2365718399fSFrançois Tigeot 		int vsyncandback_porch, vback_porch, hblank;
2375718399fSFrançois Tigeot 
2385718399fSFrançois Tigeot 		/* estimated the horizontal period */
2395718399fSFrançois Tigeot 		tmp1 = HV_FACTOR * 1000000  -
2405718399fSFrançois Tigeot 				CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
2415718399fSFrançois Tigeot 		tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
2425718399fSFrançois Tigeot 				interlace;
2435718399fSFrançois Tigeot 		hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
2445718399fSFrançois Tigeot 
2455718399fSFrançois Tigeot 		tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
2465718399fSFrançois Tigeot 		/* 9. Find number of lines in sync + backporch */
2475718399fSFrançois Tigeot 		if (tmp1 < (vsync + CVT_MIN_V_PORCH))
2485718399fSFrançois Tigeot 			vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
2495718399fSFrançois Tigeot 		else
2505718399fSFrançois Tigeot 			vsyncandback_porch = tmp1;
2515718399fSFrançois Tigeot 		/* 10. Find number of lines in back porch */
2525718399fSFrançois Tigeot 		vback_porch = vsyncandback_porch - vsync;
2535718399fSFrançois Tigeot 		drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
2545718399fSFrançois Tigeot 				vsyncandback_porch + CVT_MIN_V_PORCH;
2555718399fSFrançois Tigeot 		/* 5) Definition of Horizontal blanking time limitation */
2565718399fSFrançois Tigeot 		/* Gradient (%/kHz) - default 600 */
2575718399fSFrançois Tigeot #define CVT_M_FACTOR	600
2585718399fSFrançois Tigeot 		/* Offset (%) - default 40 */
2595718399fSFrançois Tigeot #define CVT_C_FACTOR	40
2605718399fSFrançois Tigeot 		/* Blanking time scaling factor - default 128 */
2615718399fSFrançois Tigeot #define CVT_K_FACTOR	128
2625718399fSFrançois Tigeot 		/* Scaling factor weighting - default 20 */
2635718399fSFrançois Tigeot #define CVT_J_FACTOR	20
2645718399fSFrançois Tigeot #define CVT_M_PRIME	(CVT_M_FACTOR * CVT_K_FACTOR / 256)
2655718399fSFrançois Tigeot #define CVT_C_PRIME	((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
2665718399fSFrançois Tigeot 			 CVT_J_FACTOR)
2675718399fSFrançois Tigeot 		/* 12. Find ideal blanking duty cycle from formula */
2685718399fSFrançois Tigeot 		hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
2695718399fSFrançois Tigeot 					hperiod / 1000;
2705718399fSFrançois Tigeot 		/* 13. Blanking time */
2715718399fSFrançois Tigeot 		if (hblank_percentage < 20 * HV_FACTOR)
2725718399fSFrançois Tigeot 			hblank_percentage = 20 * HV_FACTOR;
2735718399fSFrançois Tigeot 		hblank = drm_mode->hdisplay * hblank_percentage /
2745718399fSFrançois Tigeot 			 (100 * HV_FACTOR - hblank_percentage);
2755718399fSFrançois Tigeot 		hblank -= hblank % (2 * CVT_H_GRANULARITY);
276477eb7f9SFrançois Tigeot 		/* 14. find the total pixels per line */
2775718399fSFrançois Tigeot 		drm_mode->htotal = drm_mode->hdisplay + hblank;
2785718399fSFrançois Tigeot 		drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
2795718399fSFrançois Tigeot 		drm_mode->hsync_start = drm_mode->hsync_end -
2805718399fSFrançois Tigeot 			(drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
2815718399fSFrançois Tigeot 		drm_mode->hsync_start += CVT_H_GRANULARITY -
2825718399fSFrançois Tigeot 			drm_mode->hsync_start % CVT_H_GRANULARITY;
2835718399fSFrançois Tigeot 		/* fill the Vsync values */
2845718399fSFrançois Tigeot 		drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
2855718399fSFrançois Tigeot 		drm_mode->vsync_end = drm_mode->vsync_start + vsync;
2865718399fSFrançois Tigeot 	} else {
2875718399fSFrançois Tigeot 		/* Reduced blanking */
2885718399fSFrançois Tigeot 		/* Minimum vertical blanking interval time (µs)- default 460 */
2895718399fSFrançois Tigeot #define CVT_RB_MIN_VBLANK	460
2905718399fSFrançois Tigeot 		/* Fixed number of clocks for horizontal sync */
2915718399fSFrançois Tigeot #define CVT_RB_H_SYNC		32
2925718399fSFrançois Tigeot 		/* Fixed number of clocks for horizontal blanking */
2935718399fSFrançois Tigeot #define CVT_RB_H_BLANK		160
2945718399fSFrançois Tigeot 		/* Fixed number of lines for vertical front porch - default 3*/
2955718399fSFrançois Tigeot #define CVT_RB_VFPORCH		3
2965718399fSFrançois Tigeot 		int vbilines;
2975718399fSFrançois Tigeot 		int tmp1, tmp2;
2985718399fSFrançois Tigeot 		/* 8. Estimate Horizontal period. */
2995718399fSFrançois Tigeot 		tmp1 = HV_FACTOR * 1000000 -
3005718399fSFrançois Tigeot 			CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
3015718399fSFrançois Tigeot 		tmp2 = vdisplay_rnd + 2 * vmargin;
3025718399fSFrançois Tigeot 		hperiod = tmp1 / (tmp2 * vfieldrate);
3035718399fSFrançois Tigeot 		/* 9. Find number of lines in vertical blanking */
3045718399fSFrançois Tigeot 		vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
3055718399fSFrançois Tigeot 		/* 10. Check if vertical blanking is sufficient */
3065718399fSFrançois Tigeot 		if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
3075718399fSFrançois Tigeot 			vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
3085718399fSFrançois Tigeot 		/* 11. Find total number of lines in vertical field */
3095718399fSFrançois Tigeot 		drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
3105718399fSFrançois Tigeot 		/* 12. Find total number of pixels in a line */
3115718399fSFrançois Tigeot 		drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
3125718399fSFrançois Tigeot 		/* Fill in HSync values */
3135718399fSFrançois Tigeot 		drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
3145718399fSFrançois Tigeot 		drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
3155718399fSFrançois Tigeot 		/* Fill in VSync values */
3165718399fSFrançois Tigeot 		drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
3175718399fSFrançois Tigeot 		drm_mode->vsync_end = drm_mode->vsync_start + vsync;
3185718399fSFrançois Tigeot 	}
3195718399fSFrançois Tigeot 	/* 15/13. Find pixel clock frequency (kHz for xf86) */
3204be47400SFrançois Tigeot 	tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
3214be47400SFrançois Tigeot 	tmp *= HV_FACTOR * 1000;
3224be47400SFrançois Tigeot 	do_div(tmp, hperiod);
3234be47400SFrançois Tigeot 	tmp -= drm_mode->clock % CVT_CLOCK_STEP;
3244be47400SFrançois Tigeot 	drm_mode->clock = tmp;
3255718399fSFrançois Tigeot 	/* 18/16. Find actual vertical frame frequency */
3265718399fSFrançois Tigeot 	/* ignore - just set the mode flag for interlaced */
3275718399fSFrançois Tigeot 	if (interlaced) {
3285718399fSFrançois Tigeot 		drm_mode->vtotal *= 2;
3295718399fSFrançois Tigeot 		drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
3305718399fSFrançois Tigeot 	}
3315718399fSFrançois Tigeot 	/* Fill the mode line name */
3325718399fSFrançois Tigeot 	drm_mode_set_name(drm_mode);
3335718399fSFrançois Tigeot 	if (reduced)
3345718399fSFrançois Tigeot 		drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
3355718399fSFrançois Tigeot 					DRM_MODE_FLAG_NVSYNC);
3365718399fSFrançois Tigeot 	else
3375718399fSFrançois Tigeot 		drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
3385718399fSFrançois Tigeot 					DRM_MODE_FLAG_NHSYNC);
3395718399fSFrançois Tigeot 
3405718399fSFrançois Tigeot 	return drm_mode;
3415718399fSFrançois Tigeot }
3426e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_cvt_mode);
3435718399fSFrançois Tigeot 
3445718399fSFrançois Tigeot /**
345ba55f2f5SFrançois Tigeot  * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
3465718399fSFrançois Tigeot  * @dev: drm device
3475718399fSFrançois Tigeot  * @hdisplay: hdisplay size
3485718399fSFrançois Tigeot  * @vdisplay: vdisplay size
3495718399fSFrançois Tigeot  * @vrefresh: vrefresh rate.
350ba55f2f5SFrançois Tigeot  * @interlaced: whether to compute an interlaced mode
351ba55f2f5SFrançois Tigeot  * @margins: desired margin (borders) size
352ba55f2f5SFrançois Tigeot  * @GTF_M: extended GTF formula parameters
353ba55f2f5SFrançois Tigeot  * @GTF_2C: extended GTF formula parameters
354ba55f2f5SFrançois Tigeot  * @GTF_K: extended GTF formula parameters
355ba55f2f5SFrançois Tigeot  * @GTF_2J: extended GTF formula parameters
3565718399fSFrançois Tigeot  *
3575718399fSFrançois Tigeot  * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
3585718399fSFrançois Tigeot  * in here multiplied by two.  For a C of 40, pass in 80.
359ba55f2f5SFrançois Tigeot  *
360ba55f2f5SFrançois Tigeot  * Returns:
361ba55f2f5SFrançois Tigeot  * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
362ba55f2f5SFrançois Tigeot  * The display mode object is allocated with drm_mode_create(). Returns NULL
363ba55f2f5SFrançois Tigeot  * when no mode could be allocated.
3645718399fSFrançois Tigeot  */
3655718399fSFrançois Tigeot struct drm_display_mode *
drm_gtf_mode_complex(struct drm_device * dev,int hdisplay,int vdisplay,int vrefresh,bool interlaced,int margins,int GTF_M,int GTF_2C,int GTF_K,int GTF_2J)3665718399fSFrançois Tigeot drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
3675718399fSFrançois Tigeot 		     int vrefresh, bool interlaced, int margins,
3685718399fSFrançois Tigeot 		     int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
3695718399fSFrançois Tigeot {	/* 1) top/bottom margin size (% of height) - default: 1.8, */
3705718399fSFrançois Tigeot #define	GTF_MARGIN_PERCENTAGE		18
3715718399fSFrançois Tigeot 	/* 2) character cell horizontal granularity (pixels) - default 8 */
3725718399fSFrançois Tigeot #define	GTF_CELL_GRAN			8
3735718399fSFrançois Tigeot 	/* 3) Minimum vertical porch (lines) - default 3 */
3745718399fSFrançois Tigeot #define	GTF_MIN_V_PORCH			1
3755718399fSFrançois Tigeot 	/* width of vsync in lines */
3765718399fSFrançois Tigeot #define V_SYNC_RQD			3
3775718399fSFrançois Tigeot 	/* width of hsync as % of total line */
3785718399fSFrançois Tigeot #define H_SYNC_PERCENT			8
3795718399fSFrançois Tigeot 	/* min time of vsync + back porch (microsec) */
3805718399fSFrançois Tigeot #define MIN_VSYNC_PLUS_BP		550
3815718399fSFrançois Tigeot 	/* C' and M' are part of the Blanking Duty Cycle computation */
3825718399fSFrançois Tigeot #define GTF_C_PRIME	((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
3835718399fSFrançois Tigeot #define GTF_M_PRIME	(GTF_K * GTF_M / 256)
3845718399fSFrançois Tigeot 	struct drm_display_mode *drm_mode;
3855718399fSFrançois Tigeot 	unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
3865718399fSFrançois Tigeot 	int top_margin, bottom_margin;
3875718399fSFrançois Tigeot 	int interlace;
3885718399fSFrançois Tigeot 	unsigned int hfreq_est;
3895718399fSFrançois Tigeot 	int vsync_plus_bp, vback_porch;
3905718399fSFrançois Tigeot 	unsigned int vtotal_lines, vfieldrate_est, hperiod;
3915718399fSFrançois Tigeot 	unsigned int vfield_rate, vframe_rate;
3925718399fSFrançois Tigeot 	int left_margin, right_margin;
3935718399fSFrançois Tigeot 	unsigned int total_active_pixels, ideal_duty_cycle;
3945718399fSFrançois Tigeot 	unsigned int hblank, total_pixels, pixel_freq;
3955718399fSFrançois Tigeot 	int hsync, hfront_porch, vodd_front_porch_lines;
3965718399fSFrançois Tigeot 	unsigned int tmp1, tmp2;
3975718399fSFrançois Tigeot 
3985718399fSFrançois Tigeot 	drm_mode = drm_mode_create(dev);
3995718399fSFrançois Tigeot 	if (!drm_mode)
4005718399fSFrançois Tigeot 		return NULL;
4015718399fSFrançois Tigeot 
4025718399fSFrançois Tigeot 	/* 1. In order to give correct results, the number of horizontal
4035718399fSFrançois Tigeot 	 * pixels requested is first processed to ensure that it is divisible
4045718399fSFrançois Tigeot 	 * by the character size, by rounding it to the nearest character
4055718399fSFrançois Tigeot 	 * cell boundary:
4065718399fSFrançois Tigeot 	 */
4075718399fSFrançois Tigeot 	hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
4085718399fSFrançois Tigeot 	hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
4095718399fSFrançois Tigeot 
4105718399fSFrançois Tigeot 	/* 2. If interlace is requested, the number of vertical lines assumed
4115718399fSFrançois Tigeot 	 * by the calculation must be halved, as the computation calculates
4125718399fSFrançois Tigeot 	 * the number of vertical lines per field.
4135718399fSFrançois Tigeot 	 */
4145718399fSFrançois Tigeot 	if (interlaced)
4155718399fSFrançois Tigeot 		vdisplay_rnd = vdisplay / 2;
4165718399fSFrançois Tigeot 	else
4175718399fSFrançois Tigeot 		vdisplay_rnd = vdisplay;
4185718399fSFrançois Tigeot 
4195718399fSFrançois Tigeot 	/* 3. Find the frame rate required: */
4205718399fSFrançois Tigeot 	if (interlaced)
4215718399fSFrançois Tigeot 		vfieldrate_rqd = vrefresh * 2;
4225718399fSFrançois Tigeot 	else
4235718399fSFrançois Tigeot 		vfieldrate_rqd = vrefresh;
4245718399fSFrançois Tigeot 
4255718399fSFrançois Tigeot 	/* 4. Find number of lines in Top margin: */
4265718399fSFrançois Tigeot 	top_margin = 0;
4275718399fSFrançois Tigeot 	if (margins)
4285718399fSFrançois Tigeot 		top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
4295718399fSFrançois Tigeot 				1000;
4305718399fSFrançois Tigeot 	/* 5. Find number of lines in bottom margin: */
4315718399fSFrançois Tigeot 	bottom_margin = top_margin;
4325718399fSFrançois Tigeot 
4335718399fSFrançois Tigeot 	/* 6. If interlace is required, then set variable interlace: */
4345718399fSFrançois Tigeot 	if (interlaced)
4355718399fSFrançois Tigeot 		interlace = 1;
4365718399fSFrançois Tigeot 	else
4375718399fSFrançois Tigeot 		interlace = 0;
4385718399fSFrançois Tigeot 
4395718399fSFrançois Tigeot 	/* 7. Estimate the Horizontal frequency */
4405718399fSFrançois Tigeot 	{
4415718399fSFrançois Tigeot 		tmp1 = (1000000  - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
4425718399fSFrançois Tigeot 		tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
4435718399fSFrançois Tigeot 				2 + interlace;
4445718399fSFrançois Tigeot 		hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
4455718399fSFrançois Tigeot 	}
4465718399fSFrançois Tigeot 
4475718399fSFrançois Tigeot 	/* 8. Find the number of lines in V sync + back porch */
4485718399fSFrançois Tigeot 	/* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
4495718399fSFrançois Tigeot 	vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
4505718399fSFrançois Tigeot 	vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
4515718399fSFrançois Tigeot 	/*  9. Find the number of lines in V back porch alone: */
4525718399fSFrançois Tigeot 	vback_porch = vsync_plus_bp - V_SYNC_RQD;
4535718399fSFrançois Tigeot 	/*  10. Find the total number of lines in Vertical field period: */
4545718399fSFrançois Tigeot 	vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
4555718399fSFrançois Tigeot 			vsync_plus_bp + GTF_MIN_V_PORCH;
4565718399fSFrançois Tigeot 	/*  11. Estimate the Vertical field frequency: */
4575718399fSFrançois Tigeot 	vfieldrate_est = hfreq_est / vtotal_lines;
4585718399fSFrançois Tigeot 	/*  12. Find the actual horizontal period: */
4595718399fSFrançois Tigeot 	hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
4605718399fSFrançois Tigeot 
4615718399fSFrançois Tigeot 	/*  13. Find the actual Vertical field frequency: */
4625718399fSFrançois Tigeot 	vfield_rate = hfreq_est / vtotal_lines;
4635718399fSFrançois Tigeot 	/*  14. Find the Vertical frame frequency: */
4645718399fSFrançois Tigeot 	if (interlaced)
4655718399fSFrançois Tigeot 		vframe_rate = vfield_rate / 2;
4665718399fSFrançois Tigeot 	else
4675718399fSFrançois Tigeot 		vframe_rate = vfield_rate;
4685718399fSFrançois Tigeot 	/*  15. Find number of pixels in left margin: */
4695718399fSFrançois Tigeot 	if (margins)
4705718399fSFrançois Tigeot 		left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
4715718399fSFrançois Tigeot 				1000;
4725718399fSFrançois Tigeot 	else
4735718399fSFrançois Tigeot 		left_margin = 0;
4745718399fSFrançois Tigeot 
4755718399fSFrançois Tigeot 	/* 16.Find number of pixels in right margin: */
4765718399fSFrançois Tigeot 	right_margin = left_margin;
4775718399fSFrançois Tigeot 	/* 17.Find total number of active pixels in image and left and right */
4785718399fSFrançois Tigeot 	total_active_pixels = hdisplay_rnd + left_margin + right_margin;
4795718399fSFrançois Tigeot 	/* 18.Find the ideal blanking duty cycle from blanking duty cycle */
4805718399fSFrançois Tigeot 	ideal_duty_cycle = GTF_C_PRIME * 1000 -
4815718399fSFrançois Tigeot 				(GTF_M_PRIME * 1000000 / hfreq_est);
4825718399fSFrançois Tigeot 	/* 19.Find the number of pixels in the blanking time to the nearest
4835718399fSFrançois Tigeot 	 * double character cell: */
4845718399fSFrançois Tigeot 	hblank = total_active_pixels * ideal_duty_cycle /
4855718399fSFrançois Tigeot 			(100000 - ideal_duty_cycle);
4865718399fSFrançois Tigeot 	hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
4875718399fSFrançois Tigeot 	hblank = hblank * 2 * GTF_CELL_GRAN;
4885718399fSFrançois Tigeot 	/* 20.Find total number of pixels: */
4895718399fSFrançois Tigeot 	total_pixels = total_active_pixels + hblank;
4905718399fSFrançois Tigeot 	/* 21.Find pixel clock frequency: */
4915718399fSFrançois Tigeot 	pixel_freq = total_pixels * hfreq_est / 1000;
4925718399fSFrançois Tigeot 	/* Stage 1 computations are now complete; I should really pass
4935718399fSFrançois Tigeot 	 * the results to another function and do the Stage 2 computations,
4945718399fSFrançois Tigeot 	 * but I only need a few more values so I'll just append the
4955718399fSFrançois Tigeot 	 * computations here for now */
4965718399fSFrançois Tigeot 	/* 17. Find the number of pixels in the horizontal sync period: */
4975718399fSFrançois Tigeot 	hsync = H_SYNC_PERCENT * total_pixels / 100;
4985718399fSFrançois Tigeot 	hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
4995718399fSFrançois Tigeot 	hsync = hsync * GTF_CELL_GRAN;
5005718399fSFrançois Tigeot 	/* 18. Find the number of pixels in horizontal front porch period */
5015718399fSFrançois Tigeot 	hfront_porch = hblank / 2 - hsync;
5025718399fSFrançois Tigeot 	/*  36. Find the number of lines in the odd front porch period: */
5035718399fSFrançois Tigeot 	vodd_front_porch_lines = GTF_MIN_V_PORCH ;
5045718399fSFrançois Tigeot 
5055718399fSFrançois Tigeot 	/* finally, pack the results in the mode struct */
5065718399fSFrançois Tigeot 	drm_mode->hdisplay = hdisplay_rnd;
5075718399fSFrançois Tigeot 	drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
5085718399fSFrançois Tigeot 	drm_mode->hsync_end = drm_mode->hsync_start + hsync;
5095718399fSFrançois Tigeot 	drm_mode->htotal = total_pixels;
5105718399fSFrançois Tigeot 	drm_mode->vdisplay = vdisplay_rnd;
5115718399fSFrançois Tigeot 	drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
5125718399fSFrançois Tigeot 	drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
5135718399fSFrançois Tigeot 	drm_mode->vtotal = vtotal_lines;
5145718399fSFrançois Tigeot 
5155718399fSFrançois Tigeot 	drm_mode->clock = pixel_freq;
5165718399fSFrançois Tigeot 
5175718399fSFrançois Tigeot 	if (interlaced) {
5185718399fSFrançois Tigeot 		drm_mode->vtotal *= 2;
5195718399fSFrançois Tigeot 		drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
5205718399fSFrançois Tigeot 	}
5215718399fSFrançois Tigeot 
5225718399fSFrançois Tigeot 	drm_mode_set_name(drm_mode);
5235718399fSFrançois Tigeot 	if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
5245718399fSFrançois Tigeot 		drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
5255718399fSFrançois Tigeot 	else
5265718399fSFrançois Tigeot 		drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
5275718399fSFrançois Tigeot 
5285718399fSFrançois Tigeot 	return drm_mode;
5295718399fSFrançois Tigeot }
5306e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_gtf_mode_complex);
5315718399fSFrançois Tigeot 
5325718399fSFrançois Tigeot /**
533ba55f2f5SFrançois Tigeot  * drm_gtf_mode - create the modeline based on the GTF algorithm
5345718399fSFrançois Tigeot  * @dev: drm device
5355718399fSFrançois Tigeot  * @hdisplay: hdisplay size
5365718399fSFrançois Tigeot  * @vdisplay: vdisplay size
5375718399fSFrançois Tigeot  * @vrefresh: vrefresh rate.
538ba55f2f5SFrançois Tigeot  * @interlaced: whether to compute an interlaced mode
539ba55f2f5SFrançois Tigeot  * @margins: desired margin (borders) size
5405718399fSFrançois Tigeot  *
5415718399fSFrançois Tigeot  * return the modeline based on GTF algorithm
5425718399fSFrançois Tigeot  *
5435718399fSFrançois Tigeot  * This function is to create the modeline based on the GTF algorithm.
5445718399fSFrançois Tigeot  * Generalized Timing Formula is derived from:
5451dedbd3bSFrançois Tigeot  *
5465718399fSFrançois Tigeot  *	GTF Spreadsheet by Andy Morrish (1/5/97)
5475718399fSFrançois Tigeot  *	available at http://www.vesa.org
5485718399fSFrançois Tigeot  *
5495718399fSFrançois Tigeot  * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
5505718399fSFrançois Tigeot  * What I have done is to translate it by using integer calculation.
5515718399fSFrançois Tigeot  * I also refer to the function of fb_get_mode in the file of
5525718399fSFrançois Tigeot  * drivers/video/fbmon.c
5535718399fSFrançois Tigeot  *
5541dedbd3bSFrançois Tigeot  * Standard GTF parameters::
5551dedbd3bSFrançois Tigeot  *
5565718399fSFrançois Tigeot  *     M = 600
5575718399fSFrançois Tigeot  *     C = 40
5585718399fSFrançois Tigeot  *     K = 128
5595718399fSFrançois Tigeot  *     J = 20
560ba55f2f5SFrançois Tigeot  *
561ba55f2f5SFrançois Tigeot  * Returns:
562ba55f2f5SFrançois Tigeot  * The modeline based on the GTF algorithm stored in a drm_display_mode object.
563ba55f2f5SFrançois Tigeot  * The display mode object is allocated with drm_mode_create(). Returns NULL
564ba55f2f5SFrançois Tigeot  * when no mode could be allocated.
5655718399fSFrançois Tigeot  */
5665718399fSFrançois Tigeot struct drm_display_mode *
drm_gtf_mode(struct drm_device * dev,int hdisplay,int vdisplay,int vrefresh,bool interlaced,int margins)5675718399fSFrançois Tigeot drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
568ba55f2f5SFrançois Tigeot 	     bool interlaced, int margins)
5695718399fSFrançois Tigeot {
570ba55f2f5SFrançois Tigeot 	return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
571ba55f2f5SFrançois Tigeot 				    interlaced, margins,
572ba55f2f5SFrançois Tigeot 				    600, 40 * 2, 128, 20 * 2);
5735718399fSFrançois Tigeot }
5746e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_gtf_mode);
5755718399fSFrançois Tigeot 
5769edbd4a0SFrançois Tigeot #ifdef CONFIG_VIDEOMODE_HELPERS
577ba55f2f5SFrançois Tigeot /**
578ba55f2f5SFrançois Tigeot  * drm_display_mode_from_videomode - fill in @dmode using @vm,
579ba55f2f5SFrançois Tigeot  * @vm: videomode structure to use as source
580ba55f2f5SFrançois Tigeot  * @dmode: drm_display_mode structure to use as destination
581ba55f2f5SFrançois Tigeot  *
582ba55f2f5SFrançois Tigeot  * Fills out @dmode using the display mode specified in @vm.
583ba55f2f5SFrançois Tigeot  */
drm_display_mode_from_videomode(const struct videomode * vm,struct drm_display_mode * dmode)584ba55f2f5SFrançois Tigeot void drm_display_mode_from_videomode(const struct videomode *vm,
585d82bf20eSFrançois Tigeot 				     struct drm_display_mode *dmode)
586d82bf20eSFrançois Tigeot {
587d82bf20eSFrançois Tigeot 	dmode->hdisplay = vm->hactive;
588d82bf20eSFrançois Tigeot 	dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
589d82bf20eSFrançois Tigeot 	dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
590d82bf20eSFrançois Tigeot 	dmode->htotal = dmode->hsync_end + vm->hback_porch;
591d82bf20eSFrançois Tigeot 
592d82bf20eSFrançois Tigeot 	dmode->vdisplay = vm->vactive;
593d82bf20eSFrançois Tigeot 	dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
594d82bf20eSFrançois Tigeot 	dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
595d82bf20eSFrançois Tigeot 	dmode->vtotal = dmode->vsync_end + vm->vback_porch;
596d82bf20eSFrançois Tigeot 
597d82bf20eSFrançois Tigeot 	dmode->clock = vm->pixelclock / 1000;
598d82bf20eSFrançois Tigeot 
599d82bf20eSFrançois Tigeot 	dmode->flags = 0;
6009edbd4a0SFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
601d82bf20eSFrançois Tigeot 		dmode->flags |= DRM_MODE_FLAG_PHSYNC;
6029edbd4a0SFrançois Tigeot 	else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
603d82bf20eSFrançois Tigeot 		dmode->flags |= DRM_MODE_FLAG_NHSYNC;
6049edbd4a0SFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
605d82bf20eSFrançois Tigeot 		dmode->flags |= DRM_MODE_FLAG_PVSYNC;
6069edbd4a0SFrançois Tigeot 	else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
607d82bf20eSFrançois Tigeot 		dmode->flags |= DRM_MODE_FLAG_NVSYNC;
6089edbd4a0SFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_INTERLACED)
609d82bf20eSFrançois Tigeot 		dmode->flags |= DRM_MODE_FLAG_INTERLACE;
6109edbd4a0SFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
611d82bf20eSFrançois Tigeot 		dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
6129edbd4a0SFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
6139edbd4a0SFrançois Tigeot 		dmode->flags |= DRM_MODE_FLAG_DBLCLK;
614d82bf20eSFrançois Tigeot 	drm_mode_set_name(dmode);
615d82bf20eSFrançois Tigeot }
616d82bf20eSFrançois Tigeot 
6172c9916cdSFrançois Tigeot /**
6182c9916cdSFrançois Tigeot  * drm_display_mode_to_videomode - fill in @vm using @dmode,
6192c9916cdSFrançois Tigeot  * @dmode: drm_display_mode structure to use as source
6202c9916cdSFrançois Tigeot  * @vm: videomode structure to use as destination
6212c9916cdSFrançois Tigeot  *
6222c9916cdSFrançois Tigeot  * Fills out @vm using the display mode specified in @dmode.
6232c9916cdSFrançois Tigeot  */
drm_display_mode_to_videomode(const struct drm_display_mode * dmode,struct videomode * vm)6242c9916cdSFrançois Tigeot void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
6252c9916cdSFrançois Tigeot 				   struct videomode *vm)
6262c9916cdSFrançois Tigeot {
6272c9916cdSFrançois Tigeot 	vm->hactive = dmode->hdisplay;
6282c9916cdSFrançois Tigeot 	vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
6292c9916cdSFrançois Tigeot 	vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
6302c9916cdSFrançois Tigeot 	vm->hback_porch = dmode->htotal - dmode->hsync_end;
6312c9916cdSFrançois Tigeot 
6322c9916cdSFrançois Tigeot 	vm->vactive = dmode->vdisplay;
6332c9916cdSFrançois Tigeot 	vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
6342c9916cdSFrançois Tigeot 	vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
6352c9916cdSFrançois Tigeot 	vm->vback_porch = dmode->vtotal - dmode->vsync_end;
6362c9916cdSFrançois Tigeot 
6372c9916cdSFrançois Tigeot 	vm->pixelclock = dmode->clock * 1000;
6382c9916cdSFrançois Tigeot 
6392c9916cdSFrançois Tigeot 	vm->flags = 0;
6402c9916cdSFrançois Tigeot 	if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
6412c9916cdSFrançois Tigeot 		vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
6422c9916cdSFrançois Tigeot 	else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
6432c9916cdSFrançois Tigeot 		vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
6442c9916cdSFrançois Tigeot 	if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
6452c9916cdSFrançois Tigeot 		vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
6462c9916cdSFrançois Tigeot 	else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
6472c9916cdSFrançois Tigeot 		vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
6482c9916cdSFrançois Tigeot 	if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
6492c9916cdSFrançois Tigeot 		vm->flags |= DISPLAY_FLAGS_INTERLACED;
6502c9916cdSFrançois Tigeot 	if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
6512c9916cdSFrançois Tigeot 		vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
6522c9916cdSFrançois Tigeot 	if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
6532c9916cdSFrançois Tigeot 		vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
6542c9916cdSFrançois Tigeot }
6552c9916cdSFrançois Tigeot 
6561dedbd3bSFrançois Tigeot /**
6571dedbd3bSFrançois Tigeot  * drm_bus_flags_from_videomode - extract information about pixelclk and
6581dedbd3bSFrançois Tigeot  * DE polarity from videomode and store it in a separate variable
6591dedbd3bSFrançois Tigeot  * @vm: videomode structure to use
6601dedbd3bSFrançois Tigeot  * @bus_flags: information about pixelclk and DE polarity will be stored here
6611dedbd3bSFrançois Tigeot  *
6621dedbd3bSFrançois Tigeot  * Sets DRM_BUS_FLAG_DE_(LOW|HIGH) and DRM_BUS_FLAG_PIXDATA_(POS|NEG)EDGE
6631dedbd3bSFrançois Tigeot  * in @bus_flags according to DISPLAY_FLAGS found in @vm
6641dedbd3bSFrançois Tigeot  */
drm_bus_flags_from_videomode(const struct videomode * vm,u32 * bus_flags)6651dedbd3bSFrançois Tigeot void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
6661dedbd3bSFrançois Tigeot {
6671dedbd3bSFrançois Tigeot 	*bus_flags = 0;
6681dedbd3bSFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
6691dedbd3bSFrançois Tigeot 		*bus_flags |= DRM_BUS_FLAG_PIXDATA_POSEDGE;
6701dedbd3bSFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
6711dedbd3bSFrançois Tigeot 		*bus_flags |= DRM_BUS_FLAG_PIXDATA_NEGEDGE;
6721dedbd3bSFrançois Tigeot 
6731dedbd3bSFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_DE_LOW)
6741dedbd3bSFrançois Tigeot 		*bus_flags |= DRM_BUS_FLAG_DE_LOW;
6751dedbd3bSFrançois Tigeot 	if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
6761dedbd3bSFrançois Tigeot 		*bus_flags |= DRM_BUS_FLAG_DE_HIGH;
6771dedbd3bSFrançois Tigeot }
6781dedbd3bSFrançois Tigeot EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
6791dedbd3bSFrançois Tigeot 
6809edbd4a0SFrançois Tigeot #ifdef CONFIG_OF
681d82bf20eSFrançois Tigeot /**
682d82bf20eSFrançois Tigeot  * of_get_drm_display_mode - get a drm_display_mode from devicetree
683d82bf20eSFrançois Tigeot  * @np: device_node with the timing specification
684d82bf20eSFrançois Tigeot  * @dmode: will be set to the return value
6851dedbd3bSFrançois Tigeot  * @bus_flags: information about pixelclk and DE polarity
686d82bf20eSFrançois Tigeot  * @index: index into the list of display timings in devicetree
687d82bf20eSFrançois Tigeot  *
688d82bf20eSFrançois Tigeot  * This function is expensive and should only be used, if only one mode is to be
689d82bf20eSFrançois Tigeot  * read from DT. To get multiple modes start with of_get_display_timings and
690d82bf20eSFrançois Tigeot  * work with that instead.
691ba55f2f5SFrançois Tigeot  *
692ba55f2f5SFrançois Tigeot  * Returns:
693ba55f2f5SFrançois Tigeot  * 0 on success, a negative errno code when no of videomode node was found.
694d82bf20eSFrançois Tigeot  */
of_get_drm_display_mode(struct device_node * np,struct drm_display_mode * dmode,u32 * bus_flags,int index)695d82bf20eSFrançois Tigeot int of_get_drm_display_mode(struct device_node *np,
6961dedbd3bSFrançois Tigeot 			    struct drm_display_mode *dmode, u32 *bus_flags,
6971dedbd3bSFrançois Tigeot 			    int index)
698d82bf20eSFrançois Tigeot {
699d82bf20eSFrançois Tigeot 	struct videomode vm;
700d82bf20eSFrançois Tigeot 	int ret;
701d82bf20eSFrançois Tigeot 
702d82bf20eSFrançois Tigeot 	ret = of_get_videomode(np, &vm, index);
703d82bf20eSFrançois Tigeot 	if (ret)
704d82bf20eSFrançois Tigeot 		return ret;
705d82bf20eSFrançois Tigeot 
706d82bf20eSFrançois Tigeot 	drm_display_mode_from_videomode(&vm, dmode);
7071dedbd3bSFrançois Tigeot 	if (bus_flags)
7081dedbd3bSFrançois Tigeot 		drm_bus_flags_from_videomode(&vm, bus_flags);
709d82bf20eSFrançois Tigeot 
710*3f2dd94aSFrançois Tigeot 	pr_debug("%pOF: got %dx%d display mode from %s\n",
711*3f2dd94aSFrançois Tigeot 		np, vm.hactive, vm.vactive, np->name);
712d82bf20eSFrançois Tigeot 	drm_mode_debug_printmodeline(dmode);
713d82bf20eSFrançois Tigeot 
714d82bf20eSFrançois Tigeot 	return 0;
715d82bf20eSFrançois Tigeot }
7169edbd4a0SFrançois Tigeot #endif /* CONFIG_OF */
7179edbd4a0SFrançois Tigeot #endif /* CONFIG_VIDEOMODE_HELPERS */
718d82bf20eSFrançois Tigeot 
7195718399fSFrançois Tigeot /**
7205718399fSFrançois Tigeot  * drm_mode_set_name - set the name on a mode
7215718399fSFrançois Tigeot  * @mode: name will be set in this mode
7225718399fSFrançois Tigeot  *
723ba55f2f5SFrançois Tigeot  * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
724ba55f2f5SFrançois Tigeot  * with an optional 'i' suffix for interlaced modes.
7255718399fSFrançois Tigeot  */
drm_mode_set_name(struct drm_display_mode * mode)7265718399fSFrançois Tigeot void drm_mode_set_name(struct drm_display_mode *mode)
7275718399fSFrançois Tigeot {
7285718399fSFrançois Tigeot 	bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
7295718399fSFrançois Tigeot 
730a85cb24fSFrançois Tigeot 	snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
7315718399fSFrançois Tigeot 		 mode->hdisplay, mode->vdisplay,
7325718399fSFrançois Tigeot 		 interlaced ? "i" : "");
7335718399fSFrançois Tigeot }
7346e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_set_name);
7355718399fSFrançois Tigeot 
736aee94f86SFrançois Tigeot /**
737aee94f86SFrançois Tigeot  * drm_mode_hsync - get the hsync of a mode
7385718399fSFrançois Tigeot  * @mode: mode
7395718399fSFrançois Tigeot  *
740ba55f2f5SFrançois Tigeot  * Returns:
741ba55f2f5SFrançois Tigeot  * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
742ba55f2f5SFrançois Tigeot  * value first if it is not yet set.
7435718399fSFrançois Tigeot  */
drm_mode_hsync(const struct drm_display_mode * mode)7445718399fSFrançois Tigeot int drm_mode_hsync(const struct drm_display_mode *mode)
7455718399fSFrançois Tigeot {
7465718399fSFrançois Tigeot 	unsigned int calc_val;
7475718399fSFrançois Tigeot 
7485718399fSFrançois Tigeot 	if (mode->hsync)
7495718399fSFrançois Tigeot 		return mode->hsync;
7505718399fSFrançois Tigeot 
7515718399fSFrançois Tigeot 	if (mode->htotal < 0)
7525718399fSFrançois Tigeot 		return 0;
7535718399fSFrançois Tigeot 
7545718399fSFrançois Tigeot 	calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
7555718399fSFrançois Tigeot 	calc_val += 500;				/* round to 1000Hz */
7565718399fSFrançois Tigeot 	calc_val /= 1000;				/* truncate to kHz */
7575718399fSFrançois Tigeot 
7585718399fSFrançois Tigeot 	return calc_val;
7595718399fSFrançois Tigeot }
7606e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_hsync);
7615718399fSFrançois Tigeot 
7625718399fSFrançois Tigeot /**
7635718399fSFrançois Tigeot  * drm_mode_vrefresh - get the vrefresh of a mode
7645718399fSFrançois Tigeot  * @mode: mode
7655718399fSFrançois Tigeot  *
766ba55f2f5SFrançois Tigeot  * Returns:
767ba55f2f5SFrançois Tigeot  * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
768ba55f2f5SFrançois Tigeot  * value first if it is not yet set.
7695718399fSFrançois Tigeot  */
drm_mode_vrefresh(const struct drm_display_mode * mode)7705718399fSFrançois Tigeot int drm_mode_vrefresh(const struct drm_display_mode *mode)
7715718399fSFrançois Tigeot {
7725718399fSFrançois Tigeot 	int refresh = 0;
7735718399fSFrançois Tigeot 	unsigned int calc_val;
7745718399fSFrançois Tigeot 
7755718399fSFrançois Tigeot 	if (mode->vrefresh > 0)
7765718399fSFrançois Tigeot 		refresh = mode->vrefresh;
7775718399fSFrançois Tigeot 	else if (mode->htotal > 0 && mode->vtotal > 0) {
7785718399fSFrançois Tigeot 		int vtotal;
7795718399fSFrançois Tigeot 		vtotal = mode->vtotal;
7805718399fSFrançois Tigeot 		/* work out vrefresh the value will be x1000 */
7815718399fSFrançois Tigeot 		calc_val = (mode->clock * 1000);
7825718399fSFrançois Tigeot 		calc_val /= mode->htotal;
7835718399fSFrançois Tigeot 		refresh = (calc_val + vtotal / 2) / vtotal;
7845718399fSFrançois Tigeot 
7855718399fSFrançois Tigeot 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7865718399fSFrançois Tigeot 			refresh *= 2;
7875718399fSFrançois Tigeot 		if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
7885718399fSFrançois Tigeot 			refresh /= 2;
7895718399fSFrançois Tigeot 		if (mode->vscan > 1)
7905718399fSFrançois Tigeot 			refresh /= mode->vscan;
7915718399fSFrançois Tigeot 	}
7925718399fSFrançois Tigeot 	return refresh;
7935718399fSFrançois Tigeot }
7946e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_vrefresh);
7955718399fSFrançois Tigeot 
7965718399fSFrançois Tigeot /**
797a85cb24fSFrançois Tigeot  * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
798a85cb24fSFrançois Tigeot  * @mode: mode to query
799a85cb24fSFrançois Tigeot  * @hdisplay: hdisplay value to fill in
800a85cb24fSFrançois Tigeot  * @vdisplay: vdisplay value to fill in
801a85cb24fSFrançois Tigeot  *
802a85cb24fSFrançois Tigeot  * The vdisplay value will be doubled if the specified mode is a stereo mode of
803a85cb24fSFrançois Tigeot  * the appropriate layout.
804a85cb24fSFrançois Tigeot  */
drm_mode_get_hv_timing(const struct drm_display_mode * mode,int * hdisplay,int * vdisplay)805a85cb24fSFrançois Tigeot void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
806a85cb24fSFrançois Tigeot 			    int *hdisplay, int *vdisplay)
807a85cb24fSFrançois Tigeot {
808a85cb24fSFrançois Tigeot 	struct drm_display_mode adjusted = *mode;
809a85cb24fSFrançois Tigeot 
810a85cb24fSFrançois Tigeot 	drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
811a85cb24fSFrançois Tigeot 	*hdisplay = adjusted.crtc_hdisplay;
812a85cb24fSFrançois Tigeot 	*vdisplay = adjusted.crtc_vdisplay;
813a85cb24fSFrançois Tigeot }
814a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_mode_get_hv_timing);
815a85cb24fSFrançois Tigeot 
816a85cb24fSFrançois Tigeot /**
817ba55f2f5SFrançois Tigeot  * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
8185718399fSFrançois Tigeot  * @p: mode
8199edbd4a0SFrançois Tigeot  * @adjust_flags: a combination of adjustment flags
8205718399fSFrançois Tigeot  *
821ba55f2f5SFrançois Tigeot  * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
8229edbd4a0SFrançois Tigeot  *
8239edbd4a0SFrançois Tigeot  * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
8249edbd4a0SFrançois Tigeot  *   interlaced modes.
8259edbd4a0SFrançois Tigeot  * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
8269edbd4a0SFrançois Tigeot  *   buffers containing two eyes (only adjust the timings when needed, eg. for
8279edbd4a0SFrançois Tigeot  *   "frame packing" or "side by side full").
8282c9916cdSFrançois Tigeot  * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
8292c9916cdSFrançois Tigeot  *   be performed for doublescan and vscan > 1 modes respectively.
8305718399fSFrançois Tigeot  */
drm_mode_set_crtcinfo(struct drm_display_mode * p,int adjust_flags)8315718399fSFrançois Tigeot void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
8325718399fSFrançois Tigeot {
8335718399fSFrançois Tigeot 	if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
8345718399fSFrançois Tigeot 		return;
8355718399fSFrançois Tigeot 
8369edbd4a0SFrançois Tigeot 	p->crtc_clock = p->clock;
8375718399fSFrançois Tigeot 	p->crtc_hdisplay = p->hdisplay;
8385718399fSFrançois Tigeot 	p->crtc_hsync_start = p->hsync_start;
8395718399fSFrançois Tigeot 	p->crtc_hsync_end = p->hsync_end;
8405718399fSFrançois Tigeot 	p->crtc_htotal = p->htotal;
8415718399fSFrançois Tigeot 	p->crtc_hskew = p->hskew;
8425718399fSFrançois Tigeot 	p->crtc_vdisplay = p->vdisplay;
8435718399fSFrançois Tigeot 	p->crtc_vsync_start = p->vsync_start;
8445718399fSFrançois Tigeot 	p->crtc_vsync_end = p->vsync_end;
8455718399fSFrançois Tigeot 	p->crtc_vtotal = p->vtotal;
8465718399fSFrançois Tigeot 
8475718399fSFrançois Tigeot 	if (p->flags & DRM_MODE_FLAG_INTERLACE) {
8485718399fSFrançois Tigeot 		if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
8495718399fSFrançois Tigeot 			p->crtc_vdisplay /= 2;
8505718399fSFrançois Tigeot 			p->crtc_vsync_start /= 2;
8515718399fSFrançois Tigeot 			p->crtc_vsync_end /= 2;
8525718399fSFrançois Tigeot 			p->crtc_vtotal /= 2;
8535718399fSFrançois Tigeot 		}
8545718399fSFrançois Tigeot 	}
8555718399fSFrançois Tigeot 
8562c9916cdSFrançois Tigeot 	if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
8575718399fSFrançois Tigeot 		if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
8585718399fSFrançois Tigeot 			p->crtc_vdisplay *= 2;
8595718399fSFrançois Tigeot 			p->crtc_vsync_start *= 2;
8605718399fSFrançois Tigeot 			p->crtc_vsync_end *= 2;
8615718399fSFrançois Tigeot 			p->crtc_vtotal *= 2;
8625718399fSFrançois Tigeot 		}
8632c9916cdSFrançois Tigeot 	}
8645718399fSFrançois Tigeot 
8652c9916cdSFrançois Tigeot 	if (!(adjust_flags & CRTC_NO_VSCAN)) {
8665718399fSFrançois Tigeot 		if (p->vscan > 1) {
8675718399fSFrançois Tigeot 			p->crtc_vdisplay *= p->vscan;
8685718399fSFrançois Tigeot 			p->crtc_vsync_start *= p->vscan;
8695718399fSFrançois Tigeot 			p->crtc_vsync_end *= p->vscan;
8705718399fSFrançois Tigeot 			p->crtc_vtotal *= p->vscan;
8715718399fSFrançois Tigeot 		}
8722c9916cdSFrançois Tigeot 	}
8735718399fSFrançois Tigeot 
8749edbd4a0SFrançois Tigeot 	if (adjust_flags & CRTC_STEREO_DOUBLE) {
8759edbd4a0SFrançois Tigeot 		unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
8769edbd4a0SFrançois Tigeot 
8779edbd4a0SFrançois Tigeot 		switch (layout) {
8789edbd4a0SFrançois Tigeot 		case DRM_MODE_FLAG_3D_FRAME_PACKING:
8799edbd4a0SFrançois Tigeot 			p->crtc_clock *= 2;
8809edbd4a0SFrançois Tigeot 			p->crtc_vdisplay += p->crtc_vtotal;
8819edbd4a0SFrançois Tigeot 			p->crtc_vsync_start += p->crtc_vtotal;
8829edbd4a0SFrançois Tigeot 			p->crtc_vsync_end += p->crtc_vtotal;
8839edbd4a0SFrançois Tigeot 			p->crtc_vtotal += p->crtc_vtotal;
8849edbd4a0SFrançois Tigeot 			break;
8859edbd4a0SFrançois Tigeot 		}
8869edbd4a0SFrançois Tigeot 	}
8879edbd4a0SFrançois Tigeot 
8885718399fSFrançois Tigeot 	p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
8895718399fSFrançois Tigeot 	p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
8905718399fSFrançois Tigeot 	p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
8915718399fSFrançois Tigeot 	p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
8925718399fSFrançois Tigeot }
893af4b81b9SFrançois Tigeot EXPORT_SYMBOL(drm_mode_set_crtcinfo);
8945718399fSFrançois Tigeot 
8956e29dde8SFrançois Tigeot /**
8966e29dde8SFrançois Tigeot  * drm_mode_copy - copy the mode
8976e29dde8SFrançois Tigeot  * @dst: mode to overwrite
8986e29dde8SFrançois Tigeot  * @src: mode to copy
8996e29dde8SFrançois Tigeot  *
9009edbd4a0SFrançois Tigeot  * Copy an existing mode into another mode, preserving the object id and
9019edbd4a0SFrançois Tigeot  * list head of the destination mode.
9026e29dde8SFrançois Tigeot  */
drm_mode_copy(struct drm_display_mode * dst,const struct drm_display_mode * src)9036e29dde8SFrançois Tigeot void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
9046e29dde8SFrançois Tigeot {
9056e29dde8SFrançois Tigeot 	int id = dst->base.id;
9069edbd4a0SFrançois Tigeot 	struct list_head head = dst->head;
9076e29dde8SFrançois Tigeot 
9086e29dde8SFrançois Tigeot 	*dst = *src;
9096e29dde8SFrançois Tigeot 	dst->base.id = id;
9109edbd4a0SFrançois Tigeot 	dst->head = head;
9116e29dde8SFrançois Tigeot }
9126e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_copy);
9136e29dde8SFrançois Tigeot 
9145718399fSFrançois Tigeot /**
9155718399fSFrançois Tigeot  * drm_mode_duplicate - allocate and duplicate an existing mode
916ba55f2f5SFrançois Tigeot  * @dev: drm_device to allocate the duplicated mode for
917ba55f2f5SFrançois Tigeot  * @mode: mode to duplicate
9185718399fSFrançois Tigeot  *
9195718399fSFrançois Tigeot  * Just allocate a new mode, copy the existing mode into it, and return
9205718399fSFrançois Tigeot  * a pointer to it.  Used to create new instances of established modes.
921ba55f2f5SFrançois Tigeot  *
922ba55f2f5SFrançois Tigeot  * Returns:
923ba55f2f5SFrançois Tigeot  * Pointer to duplicated mode on success, NULL on error.
9245718399fSFrançois Tigeot  */
drm_mode_duplicate(struct drm_device * dev,const struct drm_display_mode * mode)9255718399fSFrançois Tigeot struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
9265718399fSFrançois Tigeot 					    const struct drm_display_mode *mode)
9275718399fSFrançois Tigeot {
9285718399fSFrançois Tigeot 	struct drm_display_mode *nmode;
9295718399fSFrançois Tigeot 
9305718399fSFrançois Tigeot 	nmode = drm_mode_create(dev);
9315718399fSFrançois Tigeot 	if (!nmode)
9325718399fSFrançois Tigeot 		return NULL;
9335718399fSFrançois Tigeot 
9346e29dde8SFrançois Tigeot 	drm_mode_copy(nmode, mode);
9356e29dde8SFrançois Tigeot 
9365718399fSFrançois Tigeot 	return nmode;
9375718399fSFrançois Tigeot }
9386e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_duplicate);
9395718399fSFrançois Tigeot 
9405718399fSFrançois Tigeot /**
9415718399fSFrançois Tigeot  * drm_mode_equal - test modes for equality
9425718399fSFrançois Tigeot  * @mode1: first mode
9435718399fSFrançois Tigeot  * @mode2: second mode
9445718399fSFrançois Tigeot  *
9455718399fSFrançois Tigeot  * Check to see if @mode1 and @mode2 are equivalent.
9465718399fSFrançois Tigeot  *
947ba55f2f5SFrançois Tigeot  * Returns:
948af4b81b9SFrançois Tigeot  * True if the modes are equal, false otherwise.
9495718399fSFrançois Tigeot  */
drm_mode_equal(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)950af4b81b9SFrançois Tigeot bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
9515718399fSFrançois Tigeot {
952477eb7f9SFrançois Tigeot 	if (!mode1 && !mode2)
953477eb7f9SFrançois Tigeot 		return true;
954477eb7f9SFrançois Tigeot 
955477eb7f9SFrançois Tigeot 	if (!mode1 || !mode2)
956477eb7f9SFrançois Tigeot 		return false;
957477eb7f9SFrançois Tigeot 
9585718399fSFrançois Tigeot 	/* do clock check convert to PICOS so fb modes get matched
9595718399fSFrançois Tigeot 	 * the same */
9605718399fSFrançois Tigeot 	if (mode1->clock && mode2->clock) {
9615718399fSFrançois Tigeot 		if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
9625718399fSFrançois Tigeot 			return false;
9635718399fSFrançois Tigeot 	} else if (mode1->clock != mode2->clock)
9645718399fSFrançois Tigeot 		return false;
9655718399fSFrançois Tigeot 
966aee94f86SFrançois Tigeot 	return drm_mode_equal_no_clocks(mode1, mode2);
967aee94f86SFrançois Tigeot }
968aee94f86SFrançois Tigeot EXPORT_SYMBOL(drm_mode_equal);
969aee94f86SFrançois Tigeot 
970aee94f86SFrançois Tigeot /**
971aee94f86SFrançois Tigeot  * drm_mode_equal_no_clocks - test modes for equality
972aee94f86SFrançois Tigeot  * @mode1: first mode
973aee94f86SFrançois Tigeot  * @mode2: second mode
974aee94f86SFrançois Tigeot  *
975aee94f86SFrançois Tigeot  * Check to see if @mode1 and @mode2 are equivalent, but
976aee94f86SFrançois Tigeot  * don't check the pixel clocks.
977aee94f86SFrançois Tigeot  *
978aee94f86SFrançois Tigeot  * Returns:
979aee94f86SFrançois Tigeot  * True if the modes are equal, false otherwise.
980aee94f86SFrançois Tigeot  */
drm_mode_equal_no_clocks(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)981aee94f86SFrançois Tigeot bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
982aee94f86SFrançois Tigeot {
9839edbd4a0SFrançois Tigeot 	if ((mode1->flags & DRM_MODE_FLAG_3D_MASK) !=
9849edbd4a0SFrançois Tigeot 	    (mode2->flags & DRM_MODE_FLAG_3D_MASK))
9859edbd4a0SFrançois Tigeot 		return false;
9869edbd4a0SFrançois Tigeot 
9879edbd4a0SFrançois Tigeot 	return drm_mode_equal_no_clocks_no_stereo(mode1, mode2);
9889edbd4a0SFrançois Tigeot }
989aee94f86SFrançois Tigeot EXPORT_SYMBOL(drm_mode_equal_no_clocks);
9909edbd4a0SFrançois Tigeot 
9919edbd4a0SFrançois Tigeot /**
9929edbd4a0SFrançois Tigeot  * drm_mode_equal_no_clocks_no_stereo - test modes for equality
9939edbd4a0SFrançois Tigeot  * @mode1: first mode
9949edbd4a0SFrançois Tigeot  * @mode2: second mode
9959edbd4a0SFrançois Tigeot  *
9969edbd4a0SFrançois Tigeot  * Check to see if @mode1 and @mode2 are equivalent, but
9979edbd4a0SFrançois Tigeot  * don't check the pixel clocks nor the stereo layout.
9989edbd4a0SFrançois Tigeot  *
999ba55f2f5SFrançois Tigeot  * Returns:
10009edbd4a0SFrançois Tigeot  * True if the modes are equal, false otherwise.
10019edbd4a0SFrançois Tigeot  */
drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)10029edbd4a0SFrançois Tigeot bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
10039edbd4a0SFrançois Tigeot 					const struct drm_display_mode *mode2)
10049edbd4a0SFrançois Tigeot {
10055718399fSFrançois Tigeot 	if (mode1->hdisplay == mode2->hdisplay &&
10065718399fSFrançois Tigeot 	    mode1->hsync_start == mode2->hsync_start &&
10075718399fSFrançois Tigeot 	    mode1->hsync_end == mode2->hsync_end &&
10085718399fSFrançois Tigeot 	    mode1->htotal == mode2->htotal &&
10095718399fSFrançois Tigeot 	    mode1->hskew == mode2->hskew &&
10105718399fSFrançois Tigeot 	    mode1->vdisplay == mode2->vdisplay &&
10115718399fSFrançois Tigeot 	    mode1->vsync_start == mode2->vsync_start &&
10125718399fSFrançois Tigeot 	    mode1->vsync_end == mode2->vsync_end &&
10135718399fSFrançois Tigeot 	    mode1->vtotal == mode2->vtotal &&
10145718399fSFrançois Tigeot 	    mode1->vscan == mode2->vscan &&
10159edbd4a0SFrançois Tigeot 	    (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
10169edbd4a0SFrançois Tigeot 	     (mode2->flags & ~DRM_MODE_FLAG_3D_MASK))
10175718399fSFrançois Tigeot 		return true;
10185718399fSFrançois Tigeot 
10195718399fSFrançois Tigeot 	return false;
10205718399fSFrançois Tigeot }
10219edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
10225718399fSFrançois Tigeot 
10235718399fSFrançois Tigeot /**
10242c9916cdSFrançois Tigeot  * drm_mode_validate_basic - make sure the mode is somewhat sane
10252c9916cdSFrançois Tigeot  * @mode: mode to check
10262c9916cdSFrançois Tigeot  *
10272c9916cdSFrançois Tigeot  * Check that the mode timings are at least somewhat reasonable.
10282c9916cdSFrançois Tigeot  * Any hardware specific limits are left up for each driver to check.
10292c9916cdSFrançois Tigeot  *
10302c9916cdSFrançois Tigeot  * Returns:
10312c9916cdSFrançois Tigeot  * The mode status
10322c9916cdSFrançois Tigeot  */
10332c9916cdSFrançois Tigeot enum drm_mode_status
drm_mode_validate_basic(const struct drm_display_mode * mode)10342c9916cdSFrançois Tigeot drm_mode_validate_basic(const struct drm_display_mode *mode)
10352c9916cdSFrançois Tigeot {
10362c9916cdSFrançois Tigeot 	if (mode->clock == 0)
10372c9916cdSFrançois Tigeot 		return MODE_CLOCK_LOW;
10382c9916cdSFrançois Tigeot 
10392c9916cdSFrançois Tigeot 	if (mode->hdisplay == 0 ||
10402c9916cdSFrançois Tigeot 	    mode->hsync_start < mode->hdisplay ||
10412c9916cdSFrançois Tigeot 	    mode->hsync_end < mode->hsync_start ||
10422c9916cdSFrançois Tigeot 	    mode->htotal < mode->hsync_end)
10432c9916cdSFrançois Tigeot 		return MODE_H_ILLEGAL;
10442c9916cdSFrançois Tigeot 
10452c9916cdSFrançois Tigeot 	if (mode->vdisplay == 0 ||
10462c9916cdSFrançois Tigeot 	    mode->vsync_start < mode->vdisplay ||
10472c9916cdSFrançois Tigeot 	    mode->vsync_end < mode->vsync_start ||
10482c9916cdSFrançois Tigeot 	    mode->vtotal < mode->vsync_end)
10492c9916cdSFrançois Tigeot 		return MODE_V_ILLEGAL;
10502c9916cdSFrançois Tigeot 
10512c9916cdSFrançois Tigeot 	return MODE_OK;
10522c9916cdSFrançois Tigeot }
10532c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_mode_validate_basic);
10542c9916cdSFrançois Tigeot 
10552c9916cdSFrançois Tigeot /**
10565718399fSFrançois Tigeot  * drm_mode_validate_size - make sure modes adhere to size constraints
10572c9916cdSFrançois Tigeot  * @mode: mode to check
10585718399fSFrançois Tigeot  * @maxX: maximum width
10595718399fSFrançois Tigeot  * @maxY: maximum height
10605718399fSFrançois Tigeot  *
1061ba55f2f5SFrançois Tigeot  * This function is a helper which can be used to validate modes against size
1062ba55f2f5SFrançois Tigeot  * limitations of the DRM device/connector. If a mode is too big its status
10632c9916cdSFrançois Tigeot  * member is updated with the appropriate validation failure code. The list
1064ba55f2f5SFrançois Tigeot  * itself is not changed.
10652c9916cdSFrançois Tigeot  *
10662c9916cdSFrançois Tigeot  * Returns:
10672c9916cdSFrançois Tigeot  * The mode status
10685718399fSFrançois Tigeot  */
10692c9916cdSFrançois Tigeot enum drm_mode_status
drm_mode_validate_size(const struct drm_display_mode * mode,int maxX,int maxY)10702c9916cdSFrançois Tigeot drm_mode_validate_size(const struct drm_display_mode *mode,
1071ba55f2f5SFrançois Tigeot 		       int maxX, int maxY)
10725718399fSFrançois Tigeot {
10735718399fSFrançois Tigeot 	if (maxX > 0 && mode->hdisplay > maxX)
10742c9916cdSFrançois Tigeot 		return MODE_VIRTUAL_X;
10755718399fSFrançois Tigeot 
10765718399fSFrançois Tigeot 	if (maxY > 0 && mode->vdisplay > maxY)
10772c9916cdSFrançois Tigeot 		return MODE_VIRTUAL_Y;
10782c9916cdSFrançois Tigeot 
10792c9916cdSFrançois Tigeot 	return MODE_OK;
10805718399fSFrançois Tigeot }
10816e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_validate_size);
10825718399fSFrançois Tigeot 
1083*3f2dd94aSFrançois Tigeot /**
1084*3f2dd94aSFrançois Tigeot  * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
1085*3f2dd94aSFrançois Tigeot  * @mode: mode to check
1086*3f2dd94aSFrançois Tigeot  * @connector: drm connector under action
1087*3f2dd94aSFrançois Tigeot  *
1088*3f2dd94aSFrançois Tigeot  * This function is a helper which can be used to filter out any YCBCR420
1089*3f2dd94aSFrançois Tigeot  * only mode, when the source doesn't support it.
1090*3f2dd94aSFrançois Tigeot  *
1091*3f2dd94aSFrançois Tigeot  * Returns:
1092*3f2dd94aSFrançois Tigeot  * The mode status
1093*3f2dd94aSFrançois Tigeot  */
1094*3f2dd94aSFrançois Tigeot enum drm_mode_status
drm_mode_validate_ycbcr420(const struct drm_display_mode * mode,struct drm_connector * connector)1095*3f2dd94aSFrançois Tigeot drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
1096*3f2dd94aSFrançois Tigeot 			   struct drm_connector *connector)
1097*3f2dd94aSFrançois Tigeot {
1098*3f2dd94aSFrançois Tigeot 	u8 vic = drm_match_cea_mode(mode);
1099*3f2dd94aSFrançois Tigeot 	enum drm_mode_status status = MODE_OK;
1100*3f2dd94aSFrançois Tigeot 	struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
1101*3f2dd94aSFrançois Tigeot 
1102*3f2dd94aSFrançois Tigeot 	if (test_bit(vic, hdmi->y420_vdb_modes)) {
1103*3f2dd94aSFrançois Tigeot 		if (!connector->ycbcr_420_allowed)
1104*3f2dd94aSFrançois Tigeot 			status = MODE_NO_420;
1105*3f2dd94aSFrançois Tigeot 	}
1106*3f2dd94aSFrançois Tigeot 
1107*3f2dd94aSFrançois Tigeot 	return status;
1108*3f2dd94aSFrançois Tigeot }
1109*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
1110*3f2dd94aSFrançois Tigeot 
1111aee94f86SFrançois Tigeot #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1112aee94f86SFrançois Tigeot 
1113aee94f86SFrançois Tigeot static const char * const drm_mode_status_names[] = {
1114aee94f86SFrançois Tigeot 	MODE_STATUS(OK),
1115aee94f86SFrançois Tigeot 	MODE_STATUS(HSYNC),
1116aee94f86SFrançois Tigeot 	MODE_STATUS(VSYNC),
1117aee94f86SFrançois Tigeot 	MODE_STATUS(H_ILLEGAL),
1118aee94f86SFrançois Tigeot 	MODE_STATUS(V_ILLEGAL),
1119aee94f86SFrançois Tigeot 	MODE_STATUS(BAD_WIDTH),
1120aee94f86SFrançois Tigeot 	MODE_STATUS(NOMODE),
1121aee94f86SFrançois Tigeot 	MODE_STATUS(NO_INTERLACE),
1122aee94f86SFrançois Tigeot 	MODE_STATUS(NO_DBLESCAN),
1123aee94f86SFrançois Tigeot 	MODE_STATUS(NO_VSCAN),
1124aee94f86SFrançois Tigeot 	MODE_STATUS(MEM),
1125aee94f86SFrançois Tigeot 	MODE_STATUS(VIRTUAL_X),
1126aee94f86SFrançois Tigeot 	MODE_STATUS(VIRTUAL_Y),
1127aee94f86SFrançois Tigeot 	MODE_STATUS(MEM_VIRT),
1128aee94f86SFrançois Tigeot 	MODE_STATUS(NOCLOCK),
1129aee94f86SFrançois Tigeot 	MODE_STATUS(CLOCK_HIGH),
1130aee94f86SFrançois Tigeot 	MODE_STATUS(CLOCK_LOW),
1131aee94f86SFrançois Tigeot 	MODE_STATUS(CLOCK_RANGE),
1132aee94f86SFrançois Tigeot 	MODE_STATUS(BAD_HVALUE),
1133aee94f86SFrançois Tigeot 	MODE_STATUS(BAD_VVALUE),
1134aee94f86SFrançois Tigeot 	MODE_STATUS(BAD_VSCAN),
1135aee94f86SFrançois Tigeot 	MODE_STATUS(HSYNC_NARROW),
1136aee94f86SFrançois Tigeot 	MODE_STATUS(HSYNC_WIDE),
1137aee94f86SFrançois Tigeot 	MODE_STATUS(HBLANK_NARROW),
1138aee94f86SFrançois Tigeot 	MODE_STATUS(HBLANK_WIDE),
1139aee94f86SFrançois Tigeot 	MODE_STATUS(VSYNC_NARROW),
1140aee94f86SFrançois Tigeot 	MODE_STATUS(VSYNC_WIDE),
1141aee94f86SFrançois Tigeot 	MODE_STATUS(VBLANK_NARROW),
1142aee94f86SFrançois Tigeot 	MODE_STATUS(VBLANK_WIDE),
1143aee94f86SFrançois Tigeot 	MODE_STATUS(PANEL),
1144aee94f86SFrançois Tigeot 	MODE_STATUS(INTERLACE_WIDTH),
1145aee94f86SFrançois Tigeot 	MODE_STATUS(ONE_WIDTH),
1146aee94f86SFrançois Tigeot 	MODE_STATUS(ONE_HEIGHT),
1147aee94f86SFrançois Tigeot 	MODE_STATUS(ONE_SIZE),
1148aee94f86SFrançois Tigeot 	MODE_STATUS(NO_REDUCED),
1149aee94f86SFrançois Tigeot 	MODE_STATUS(NO_STEREO),
1150*3f2dd94aSFrançois Tigeot 	MODE_STATUS(NO_420),
1151aee94f86SFrançois Tigeot 	MODE_STATUS(STALE),
1152aee94f86SFrançois Tigeot 	MODE_STATUS(BAD),
1153aee94f86SFrançois Tigeot 	MODE_STATUS(ERROR),
1154aee94f86SFrançois Tigeot };
1155aee94f86SFrançois Tigeot 
1156aee94f86SFrançois Tigeot #undef MODE_STATUS
1157aee94f86SFrançois Tigeot 
drm_get_mode_status_name(enum drm_mode_status status)1158aee94f86SFrançois Tigeot static const char *drm_get_mode_status_name(enum drm_mode_status status)
1159aee94f86SFrançois Tigeot {
1160aee94f86SFrançois Tigeot 	int index = status + 3;
1161aee94f86SFrançois Tigeot 
1162aee94f86SFrançois Tigeot 	if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1163aee94f86SFrançois Tigeot 		return "";
1164aee94f86SFrançois Tigeot 
1165aee94f86SFrançois Tigeot 	return drm_mode_status_names[index];
1166aee94f86SFrançois Tigeot }
1167aee94f86SFrançois Tigeot 
11685718399fSFrançois Tigeot /**
11695718399fSFrançois Tigeot  * drm_mode_prune_invalid - remove invalid modes from mode list
11705718399fSFrançois Tigeot  * @dev: DRM device
11715718399fSFrançois Tigeot  * @mode_list: list of modes to check
11725718399fSFrançois Tigeot  * @verbose: be verbose about it
11735718399fSFrançois Tigeot  *
1174ba55f2f5SFrançois Tigeot  * This helper function can be used to prune a display mode list after
1175ba55f2f5SFrançois Tigeot  * validation has been completed. All modes who's status is not MODE_OK will be
1176ba55f2f5SFrançois Tigeot  * removed from the list, and if @verbose the status code and mode name is also
1177ba55f2f5SFrançois Tigeot  * printed to dmesg.
11785718399fSFrançois Tigeot  */
drm_mode_prune_invalid(struct drm_device * dev,struct list_head * mode_list,bool verbose)11795718399fSFrançois Tigeot void drm_mode_prune_invalid(struct drm_device *dev,
11805718399fSFrançois Tigeot 			    struct list_head *mode_list, bool verbose)
11815718399fSFrançois Tigeot {
11825718399fSFrançois Tigeot 	struct drm_display_mode *mode, *t;
11835718399fSFrançois Tigeot 
11845718399fSFrançois Tigeot 	list_for_each_entry_safe(mode, t, mode_list, head) {
11855718399fSFrançois Tigeot 		if (mode->status != MODE_OK) {
11865718399fSFrançois Tigeot 			list_del(&mode->head);
11875718399fSFrançois Tigeot 			if (verbose) {
11885718399fSFrançois Tigeot 				drm_mode_debug_printmodeline(mode);
1189aee94f86SFrançois Tigeot 				DRM_DEBUG_KMS("Not using %s mode: %s\n",
1190aee94f86SFrançois Tigeot 					      mode->name,
1191aee94f86SFrançois Tigeot 					      drm_get_mode_status_name(mode->status));
11925718399fSFrançois Tigeot 			}
11935718399fSFrançois Tigeot 			drm_mode_destroy(dev, mode);
11945718399fSFrançois Tigeot 		}
11955718399fSFrançois Tigeot 	}
11965718399fSFrançois Tigeot }
11976e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_prune_invalid);
11985718399fSFrançois Tigeot 
11995718399fSFrançois Tigeot /**
12005718399fSFrançois Tigeot  * drm_mode_compare - compare modes for favorability
12015718399fSFrançois Tigeot  * @priv: unused
12025718399fSFrançois Tigeot  * @lh_a: list_head for first mode
12035718399fSFrançois Tigeot  * @lh_b: list_head for second mode
12045718399fSFrançois Tigeot  *
12055718399fSFrançois Tigeot  * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
12065718399fSFrançois Tigeot  * which is better.
12075718399fSFrançois Tigeot  *
1208ba55f2f5SFrançois Tigeot  * Returns:
12095718399fSFrançois Tigeot  * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
12105718399fSFrançois Tigeot  * positive if @lh_b is better than @lh_a.
12115718399fSFrançois Tigeot  */
drm_mode_compare(void * priv,struct list_head * lh_a,struct list_head * lh_b)12125718399fSFrançois Tigeot static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
12135718399fSFrançois Tigeot {
12145718399fSFrançois Tigeot 	struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
12155718399fSFrançois Tigeot 	struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
12165718399fSFrançois Tigeot 	int diff;
12175718399fSFrançois Tigeot 
12185718399fSFrançois Tigeot 	diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
12195718399fSFrançois Tigeot 		((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
12205718399fSFrançois Tigeot 	if (diff)
12215718399fSFrançois Tigeot 		return diff;
12225718399fSFrançois Tigeot 	diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
12235718399fSFrançois Tigeot 	if (diff)
12245718399fSFrançois Tigeot 		return diff;
12259edbd4a0SFrançois Tigeot 
12269edbd4a0SFrançois Tigeot 	diff = b->vrefresh - a->vrefresh;
12279edbd4a0SFrançois Tigeot 	if (diff)
12289edbd4a0SFrançois Tigeot 		return diff;
12299edbd4a0SFrançois Tigeot 
12305718399fSFrançois Tigeot 	diff = b->clock - a->clock;
12315718399fSFrançois Tigeot 	return diff;
12325718399fSFrançois Tigeot }
12335718399fSFrançois Tigeot 
12345718399fSFrançois Tigeot /**
12355718399fSFrançois Tigeot  * drm_mode_sort - sort mode list
1236ba55f2f5SFrançois Tigeot  * @mode_list: list of drm_display_mode structures to sort
12375718399fSFrançois Tigeot  *
1238ba55f2f5SFrançois Tigeot  * Sort @mode_list by favorability, moving good modes to the head of the list.
12395718399fSFrançois Tigeot  */
drm_mode_sort(struct list_head * mode_list)12405718399fSFrançois Tigeot void drm_mode_sort(struct list_head *mode_list)
12415718399fSFrançois Tigeot {
1242dcc49d6fSFrançois Tigeot 	list_sort(NULL, mode_list, drm_mode_compare);
12435718399fSFrançois Tigeot }
12446e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_sort);
12455718399fSFrançois Tigeot 
12465718399fSFrançois Tigeot /**
12475718399fSFrançois Tigeot  * drm_mode_connector_list_update - update the mode list for the connector
12485718399fSFrançois Tigeot  * @connector: the connector to update
12495718399fSFrançois Tigeot  *
12505718399fSFrançois Tigeot  * This moves the modes from the @connector probed_modes list
12515718399fSFrançois Tigeot  * to the actual mode list. It compares the probed mode against the current
1252ba55f2f5SFrançois Tigeot  * list and only adds different/new modes.
1253ba55f2f5SFrançois Tigeot  *
1254ba55f2f5SFrançois Tigeot  * This is just a helper functions doesn't validate any modes itself and also
1255ba55f2f5SFrançois Tigeot  * doesn't prune any invalid modes. Callers need to do that themselves.
12565718399fSFrançois Tigeot  */
drm_mode_connector_list_update(struct drm_connector * connector)1257aee94f86SFrançois Tigeot void drm_mode_connector_list_update(struct drm_connector *connector)
12585718399fSFrançois Tigeot {
12595718399fSFrançois Tigeot 	struct drm_display_mode *pmode, *pt;
12605718399fSFrançois Tigeot 
1261ba55f2f5SFrançois Tigeot 	WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1262ba55f2f5SFrançois Tigeot 
1263aee94f86SFrançois Tigeot 	list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
1264aee94f86SFrançois Tigeot 		struct drm_display_mode *mode;
1265aee94f86SFrançois Tigeot 		bool found_it = false;
1266aee94f86SFrançois Tigeot 
12675718399fSFrançois Tigeot 		/* go through current modes checking for the new probed mode */
12685718399fSFrançois Tigeot 		list_for_each_entry(mode, &connector->modes, head) {
1269aee94f86SFrançois Tigeot 			if (!drm_mode_equal(pmode, mode))
1270aee94f86SFrançois Tigeot 				continue;
1271aee94f86SFrançois Tigeot 
1272aee94f86SFrançois Tigeot 			found_it = true;
1273aee94f86SFrançois Tigeot 
1274aee94f86SFrançois Tigeot 			/*
1275aee94f86SFrançois Tigeot 			 * If the old matching mode is stale (ie. left over
1276aee94f86SFrançois Tigeot 			 * from a previous probe) just replace it outright.
1277aee94f86SFrançois Tigeot 			 * Otherwise just merge the type bits between all
1278aee94f86SFrançois Tigeot 			 * equal probed modes.
1279aee94f86SFrançois Tigeot 			 *
1280aee94f86SFrançois Tigeot 			 * If two probed modes are considered equal, pick the
1281aee94f86SFrançois Tigeot 			 * actual timings from the one that's marked as
1282aee94f86SFrançois Tigeot 			 * preferred (in case the match isn't 100%). If
1283aee94f86SFrançois Tigeot 			 * multiple or zero preferred modes are present, favor
1284aee94f86SFrançois Tigeot 			 * the mode added to the probed_modes list first.
1285aee94f86SFrançois Tigeot 			 */
1286aee94f86SFrançois Tigeot 			if (mode->status == MODE_STALE) {
1287aee94f86SFrançois Tigeot 				drm_mode_copy(mode, pmode);
1288aee94f86SFrançois Tigeot 			} else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
1289aee94f86SFrançois Tigeot 				   (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
1290aee94f86SFrançois Tigeot 				pmode->type |= mode->type;
1291aee94f86SFrançois Tigeot 				drm_mode_copy(mode, pmode);
1292aee94f86SFrançois Tigeot 			} else {
12935718399fSFrançois Tigeot 				mode->type |= pmode->type;
1294aee94f86SFrançois Tigeot 			}
1295aee94f86SFrançois Tigeot 
12965718399fSFrançois Tigeot 			list_del(&pmode->head);
12975718399fSFrançois Tigeot 			drm_mode_destroy(connector->dev, pmode);
12985718399fSFrançois Tigeot 			break;
12995718399fSFrançois Tigeot 		}
13005718399fSFrançois Tigeot 
13015718399fSFrançois Tigeot 		if (!found_it) {
13025718399fSFrançois Tigeot 			list_move_tail(&pmode->head, &connector->modes);
13035718399fSFrançois Tigeot 		}
13045718399fSFrançois Tigeot 	}
13055718399fSFrançois Tigeot }
13066e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_connector_list_update);
13075718399fSFrançois Tigeot 
13085718399fSFrançois Tigeot /**
1309ba55f2f5SFrançois Tigeot  * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1310ba55f2f5SFrançois Tigeot  * @mode_option: optional per connector mode option
1311ba55f2f5SFrançois Tigeot  * @connector: connector to parse modeline for
1312ba55f2f5SFrançois Tigeot  * @mode: preallocated drm_cmdline_mode structure to fill out
13135718399fSFrançois Tigeot  *
1314ba55f2f5SFrançois Tigeot  * This parses @mode_option command line modeline for modes and options to
1315ba55f2f5SFrançois Tigeot  * configure the connector. If @mode_option is NULL the default command line
1316ba55f2f5SFrançois Tigeot  * modeline in fb_mode_option will be parsed instead.
13175718399fSFrançois Tigeot  *
1318ba55f2f5SFrançois Tigeot  * This uses the same parameters as the fb modedb.c, except for an extra
1319ba55f2f5SFrançois Tigeot  * force-enable, force-enable-digital and force-disable bit at the end:
1320ba55f2f5SFrançois Tigeot  *
13215718399fSFrançois Tigeot  * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
13225718399fSFrançois Tigeot  *
1323ba55f2f5SFrançois Tigeot  * The intermediate drm_cmdline_mode structure is required to store additional
1324477eb7f9SFrançois Tigeot  * options from the command line modline like the force-enable/disable flag.
1325ba55f2f5SFrançois Tigeot  *
1326ba55f2f5SFrançois Tigeot  * Returns:
1327ba55f2f5SFrançois Tigeot  * True if a valid modeline has been parsed, false otherwise.
13285718399fSFrançois Tigeot  */
drm_mode_parse_command_line_for_connector(const char * mode_option,struct drm_connector * connector,struct drm_cmdline_mode * mode)13295718399fSFrançois Tigeot bool drm_mode_parse_command_line_for_connector(const char *mode_option,
13305718399fSFrançois Tigeot 					       struct drm_connector *connector,
13315718399fSFrançois Tigeot 					       struct drm_cmdline_mode *mode)
13325718399fSFrançois Tigeot {
13335718399fSFrançois Tigeot 	const char *name;
13345718399fSFrançois Tigeot 	unsigned int namelen;
13355718399fSFrançois Tigeot 	bool res_specified = false, bpp_specified = false, refresh_specified = false;
13365718399fSFrançois Tigeot 	unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
13375718399fSFrançois Tigeot 	bool yres_specified = false, cvt = false, rb = false;
13385718399fSFrançois Tigeot 	bool interlace = false, margins = false, was_digit = false;
13395718399fSFrançois Tigeot 	int i;
13405718399fSFrançois Tigeot 	enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
13415718399fSFrançois Tigeot 
1342d82bf20eSFrançois Tigeot #ifdef CONFIG_FB
13435718399fSFrançois Tigeot 	if (!mode_option)
13445718399fSFrançois Tigeot 		mode_option = fb_mode_option;
13455718399fSFrançois Tigeot #endif
13465718399fSFrançois Tigeot 
13475718399fSFrançois Tigeot 	if (!mode_option) {
13485718399fSFrançois Tigeot 		mode->specified = false;
13495718399fSFrançois Tigeot 		return false;
13505718399fSFrançois Tigeot 	}
13515718399fSFrançois Tigeot 
13525718399fSFrançois Tigeot 	name = mode_option;
13535718399fSFrançois Tigeot 	namelen = strlen(name);
13545718399fSFrançois Tigeot 	for (i = namelen-1; i >= 0; i--) {
13555718399fSFrançois Tigeot 		switch (name[i]) {
13565718399fSFrançois Tigeot 		case '@':
13575718399fSFrançois Tigeot 			if (!refresh_specified && !bpp_specified &&
13585718399fSFrançois Tigeot 			    !yres_specified && !cvt && !rb && was_digit) {
13596e29dde8SFrançois Tigeot 				refresh = simple_strtol(&name[i+1], NULL, 10);
13605718399fSFrançois Tigeot 				refresh_specified = true;
13615718399fSFrançois Tigeot 				was_digit = false;
13625718399fSFrançois Tigeot 			} else
13635718399fSFrançois Tigeot 				goto done;
13645718399fSFrançois Tigeot 			break;
13655718399fSFrançois Tigeot 		case '-':
13665718399fSFrançois Tigeot 			if (!bpp_specified && !yres_specified && !cvt &&
13675718399fSFrançois Tigeot 			    !rb && was_digit) {
13686e29dde8SFrançois Tigeot 				bpp = simple_strtol(&name[i+1], NULL, 10);
13695718399fSFrançois Tigeot 				bpp_specified = true;
13705718399fSFrançois Tigeot 				was_digit = false;
13715718399fSFrançois Tigeot 			} else
13725718399fSFrançois Tigeot 				goto done;
13735718399fSFrançois Tigeot 			break;
13745718399fSFrançois Tigeot 		case 'x':
13755718399fSFrançois Tigeot 			if (!yres_specified && was_digit) {
13766e29dde8SFrançois Tigeot 				yres = simple_strtol(&name[i+1], NULL, 10);
13775718399fSFrançois Tigeot 				yres_specified = true;
13785718399fSFrançois Tigeot 				was_digit = false;
13795718399fSFrançois Tigeot 			} else
13805718399fSFrançois Tigeot 				goto done;
13819edbd4a0SFrançois Tigeot 			break;
13825718399fSFrançois Tigeot 		case '0' ... '9':
13835718399fSFrançois Tigeot 			was_digit = true;
13845718399fSFrançois Tigeot 			break;
13855718399fSFrançois Tigeot 		case 'M':
13865718399fSFrançois Tigeot 			if (yres_specified || cvt || was_digit)
13875718399fSFrançois Tigeot 				goto done;
13885718399fSFrançois Tigeot 			cvt = true;
13895718399fSFrançois Tigeot 			break;
13905718399fSFrançois Tigeot 		case 'R':
13915718399fSFrançois Tigeot 			if (yres_specified || cvt || rb || was_digit)
13925718399fSFrançois Tigeot 				goto done;
13935718399fSFrançois Tigeot 			rb = true;
13945718399fSFrançois Tigeot 			break;
13955718399fSFrançois Tigeot 		case 'm':
13965718399fSFrançois Tigeot 			if (cvt || yres_specified || was_digit)
13975718399fSFrançois Tigeot 				goto done;
13985718399fSFrançois Tigeot 			margins = true;
13995718399fSFrançois Tigeot 			break;
14005718399fSFrançois Tigeot 		case 'i':
14015718399fSFrançois Tigeot 			if (cvt || yres_specified || was_digit)
14025718399fSFrançois Tigeot 				goto done;
14035718399fSFrançois Tigeot 			interlace = true;
14045718399fSFrançois Tigeot 			break;
14055718399fSFrançois Tigeot 		case 'e':
14065718399fSFrançois Tigeot 			if (yres_specified || bpp_specified || refresh_specified ||
14075718399fSFrançois Tigeot 			    was_digit || (force != DRM_FORCE_UNSPECIFIED))
14085718399fSFrançois Tigeot 				goto done;
14095718399fSFrançois Tigeot 
14105718399fSFrançois Tigeot 			force = DRM_FORCE_ON;
14115718399fSFrançois Tigeot 			break;
14125718399fSFrançois Tigeot 		case 'D':
14135718399fSFrançois Tigeot 			if (yres_specified || bpp_specified || refresh_specified ||
14145718399fSFrançois Tigeot 			    was_digit || (force != DRM_FORCE_UNSPECIFIED))
14155718399fSFrançois Tigeot 				goto done;
14165718399fSFrançois Tigeot 
14175718399fSFrançois Tigeot 			if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
14185718399fSFrançois Tigeot 			    (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
14195718399fSFrançois Tigeot 				force = DRM_FORCE_ON;
14205718399fSFrançois Tigeot 			else
14215718399fSFrançois Tigeot 				force = DRM_FORCE_ON_DIGITAL;
14225718399fSFrançois Tigeot 			break;
14235718399fSFrançois Tigeot 		case 'd':
14245718399fSFrançois Tigeot 			if (yres_specified || bpp_specified || refresh_specified ||
14255718399fSFrançois Tigeot 			    was_digit || (force != DRM_FORCE_UNSPECIFIED))
14265718399fSFrançois Tigeot 				goto done;
14275718399fSFrançois Tigeot 
14285718399fSFrançois Tigeot 			force = DRM_FORCE_OFF;
14295718399fSFrançois Tigeot 			break;
14305718399fSFrançois Tigeot 		default:
14315718399fSFrançois Tigeot 			goto done;
14325718399fSFrançois Tigeot 		}
14335718399fSFrançois Tigeot 	}
14345718399fSFrançois Tigeot 
14355718399fSFrançois Tigeot 	if (i < 0 && yres_specified) {
14365718399fSFrançois Tigeot 		char *ch;
14376e29dde8SFrançois Tigeot 		xres = simple_strtol(name, &ch, 10);
14385718399fSFrançois Tigeot 		if ((ch != NULL) && (*ch == 'x'))
14395718399fSFrançois Tigeot 			res_specified = true;
14405718399fSFrançois Tigeot 		else
14415718399fSFrançois Tigeot 			i = ch - name;
14425718399fSFrançois Tigeot 	} else if (!yres_specified && was_digit) {
14435718399fSFrançois Tigeot 		/* catch mode that begins with digits but has no 'x' */
14445718399fSFrançois Tigeot 		i = 0;
14455718399fSFrançois Tigeot 	}
14465718399fSFrançois Tigeot done:
14475718399fSFrançois Tigeot 	if (i >= 0) {
1448c0e85e96SFrançois Tigeot 		pr_warn("[drm] parse error at position %i in video mode '%s'\n",
14495718399fSFrançois Tigeot 			i, name);
14505718399fSFrançois Tigeot 		mode->specified = false;
14515718399fSFrançois Tigeot 		return false;
14525718399fSFrançois Tigeot 	}
14535718399fSFrançois Tigeot 
14545718399fSFrançois Tigeot 	if (res_specified) {
14555718399fSFrançois Tigeot 		mode->specified = true;
14565718399fSFrançois Tigeot 		mode->xres = xres;
14575718399fSFrançois Tigeot 		mode->yres = yres;
14585718399fSFrançois Tigeot 	}
14595718399fSFrançois Tigeot 
14605718399fSFrançois Tigeot 	if (refresh_specified) {
14615718399fSFrançois Tigeot 		mode->refresh_specified = true;
14625718399fSFrançois Tigeot 		mode->refresh = refresh;
14635718399fSFrançois Tigeot 	}
14645718399fSFrançois Tigeot 
14655718399fSFrançois Tigeot 	if (bpp_specified) {
14665718399fSFrançois Tigeot 		mode->bpp_specified = true;
14675718399fSFrançois Tigeot 		mode->bpp = bpp;
14685718399fSFrançois Tigeot 	}
14695718399fSFrançois Tigeot 	mode->rb = rb;
14705718399fSFrançois Tigeot 	mode->cvt = cvt;
14715718399fSFrançois Tigeot 	mode->interlace = interlace;
14725718399fSFrançois Tigeot 	mode->margins = margins;
14735718399fSFrançois Tigeot 	mode->force = force;
14745718399fSFrançois Tigeot 
14755718399fSFrançois Tigeot 	return true;
14765718399fSFrançois Tigeot }
14776e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
14785718399fSFrançois Tigeot 
1479ba55f2f5SFrançois Tigeot /**
1480ba55f2f5SFrançois Tigeot  * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1481ba55f2f5SFrançois Tigeot  * @dev: DRM device to create the new mode for
1482ba55f2f5SFrançois Tigeot  * @cmd: input command line modeline
1483ba55f2f5SFrançois Tigeot  *
1484ba55f2f5SFrançois Tigeot  * Returns:
1485ba55f2f5SFrançois Tigeot  * Pointer to converted mode on success, NULL on error.
1486ba55f2f5SFrançois Tigeot  */
14875718399fSFrançois Tigeot struct drm_display_mode *
drm_mode_create_from_cmdline_mode(struct drm_device * dev,struct drm_cmdline_mode * cmd)14885718399fSFrançois Tigeot drm_mode_create_from_cmdline_mode(struct drm_device *dev,
14895718399fSFrançois Tigeot 				  struct drm_cmdline_mode *cmd)
14905718399fSFrançois Tigeot {
14915718399fSFrançois Tigeot 	struct drm_display_mode *mode;
14925718399fSFrançois Tigeot 
14935718399fSFrançois Tigeot 	if (cmd->cvt)
14945718399fSFrançois Tigeot 		mode = drm_cvt_mode(dev,
14955718399fSFrançois Tigeot 				    cmd->xres, cmd->yres,
14965718399fSFrançois Tigeot 				    cmd->refresh_specified ? cmd->refresh : 60,
14975718399fSFrançois Tigeot 				    cmd->rb, cmd->interlace,
14985718399fSFrançois Tigeot 				    cmd->margins);
14995718399fSFrançois Tigeot 	else
15005718399fSFrançois Tigeot 		mode = drm_gtf_mode(dev,
15015718399fSFrançois Tigeot 				    cmd->xres, cmd->yres,
15025718399fSFrançois Tigeot 				    cmd->refresh_specified ? cmd->refresh : 60,
15035718399fSFrançois Tigeot 				    cmd->interlace,
15045718399fSFrançois Tigeot 				    cmd->margins);
15055718399fSFrançois Tigeot 	if (!mode)
15065718399fSFrançois Tigeot 		return NULL;
15075718399fSFrançois Tigeot 
15081b13d190SFrançois Tigeot 	mode->type |= DRM_MODE_TYPE_USERDEF;
15094be47400SFrançois Tigeot 	/* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
1510a85cb24fSFrançois Tigeot 	if (cmd->xres == 1366)
1511a85cb24fSFrançois Tigeot 		drm_mode_fixup_1366x768(mode);
15125718399fSFrançois Tigeot 	drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
15135718399fSFrançois Tigeot 	return mode;
15145718399fSFrançois Tigeot }
15156e29dde8SFrançois Tigeot EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
151619c468b4SFrançois Tigeot 
151719c468b4SFrançois Tigeot /**
151819c468b4SFrançois Tigeot  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
151919c468b4SFrançois Tigeot  * @out: drm_mode_modeinfo struct to return to the user
152019c468b4SFrançois Tigeot  * @in: drm_display_mode to use
152119c468b4SFrançois Tigeot  *
152219c468b4SFrançois Tigeot  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
152319c468b4SFrançois Tigeot  * the user.
152419c468b4SFrançois Tigeot  */
drm_mode_convert_to_umode(struct drm_mode_modeinfo * out,const struct drm_display_mode * in)152519c468b4SFrançois Tigeot void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
152619c468b4SFrançois Tigeot 			       const struct drm_display_mode *in)
152719c468b4SFrançois Tigeot {
152819c468b4SFrançois Tigeot 	WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
152919c468b4SFrançois Tigeot 	     in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
153019c468b4SFrançois Tigeot 	     in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
153119c468b4SFrançois Tigeot 	     in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
153219c468b4SFrançois Tigeot 	     in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
153319c468b4SFrançois Tigeot 	     "timing values too large for mode info\n");
153419c468b4SFrançois Tigeot 
153519c468b4SFrançois Tigeot 	out->clock = in->clock;
153619c468b4SFrançois Tigeot 	out->hdisplay = in->hdisplay;
153719c468b4SFrançois Tigeot 	out->hsync_start = in->hsync_start;
153819c468b4SFrançois Tigeot 	out->hsync_end = in->hsync_end;
153919c468b4SFrançois Tigeot 	out->htotal = in->htotal;
154019c468b4SFrançois Tigeot 	out->hskew = in->hskew;
154119c468b4SFrançois Tigeot 	out->vdisplay = in->vdisplay;
154219c468b4SFrançois Tigeot 	out->vsync_start = in->vsync_start;
154319c468b4SFrançois Tigeot 	out->vsync_end = in->vsync_end;
154419c468b4SFrançois Tigeot 	out->vtotal = in->vtotal;
154519c468b4SFrançois Tigeot 	out->vscan = in->vscan;
154619c468b4SFrançois Tigeot 	out->vrefresh = in->vrefresh;
154719c468b4SFrançois Tigeot 	out->flags = in->flags;
154819c468b4SFrançois Tigeot 	out->type = in->type;
154919c468b4SFrançois Tigeot 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
155019c468b4SFrançois Tigeot 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
155119c468b4SFrançois Tigeot }
155219c468b4SFrançois Tigeot 
155319c468b4SFrançois Tigeot /**
155419c468b4SFrançois Tigeot  * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
155519c468b4SFrançois Tigeot  * @out: drm_display_mode to return to the user
155619c468b4SFrançois Tigeot  * @in: drm_mode_modeinfo to use
155719c468b4SFrançois Tigeot  *
155819c468b4SFrançois Tigeot  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
155919c468b4SFrançois Tigeot  * the caller.
156019c468b4SFrançois Tigeot  *
156119c468b4SFrançois Tigeot  * Returns:
156219c468b4SFrançois Tigeot  * Zero on success, negative errno on failure.
156319c468b4SFrançois Tigeot  */
drm_mode_convert_umode(struct drm_display_mode * out,const struct drm_mode_modeinfo * in)156419c468b4SFrançois Tigeot int drm_mode_convert_umode(struct drm_display_mode *out,
156519c468b4SFrançois Tigeot 			   const struct drm_mode_modeinfo *in)
156619c468b4SFrançois Tigeot {
156719c468b4SFrançois Tigeot 	int ret = -EINVAL;
156819c468b4SFrançois Tigeot 
156919c468b4SFrançois Tigeot 	if (in->clock > INT_MAX || in->vrefresh > INT_MAX) {
157019c468b4SFrançois Tigeot 		ret = -ERANGE;
157119c468b4SFrançois Tigeot 		goto out;
157219c468b4SFrançois Tigeot 	}
157319c468b4SFrançois Tigeot 
157419c468b4SFrançois Tigeot 	if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
157519c468b4SFrançois Tigeot 		goto out;
157619c468b4SFrançois Tigeot 
157719c468b4SFrançois Tigeot 	out->clock = in->clock;
157819c468b4SFrançois Tigeot 	out->hdisplay = in->hdisplay;
157919c468b4SFrançois Tigeot 	out->hsync_start = in->hsync_start;
158019c468b4SFrançois Tigeot 	out->hsync_end = in->hsync_end;
158119c468b4SFrançois Tigeot 	out->htotal = in->htotal;
158219c468b4SFrançois Tigeot 	out->hskew = in->hskew;
158319c468b4SFrançois Tigeot 	out->vdisplay = in->vdisplay;
158419c468b4SFrançois Tigeot 	out->vsync_start = in->vsync_start;
158519c468b4SFrançois Tigeot 	out->vsync_end = in->vsync_end;
158619c468b4SFrançois Tigeot 	out->vtotal = in->vtotal;
158719c468b4SFrançois Tigeot 	out->vscan = in->vscan;
158819c468b4SFrançois Tigeot 	out->vrefresh = in->vrefresh;
158919c468b4SFrançois Tigeot 	out->flags = in->flags;
159019c468b4SFrançois Tigeot 	out->type = in->type;
159119c468b4SFrançois Tigeot 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
159219c468b4SFrançois Tigeot 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
159319c468b4SFrançois Tigeot 
159419c468b4SFrançois Tigeot 	out->status = drm_mode_validate_basic(out);
159519c468b4SFrançois Tigeot 	if (out->status != MODE_OK)
159619c468b4SFrançois Tigeot 		goto out;
159719c468b4SFrançois Tigeot 
15988621f407SFrançois Tigeot 	drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
15998621f407SFrançois Tigeot 
160019c468b4SFrançois Tigeot 	ret = 0;
160119c468b4SFrançois Tigeot 
160219c468b4SFrançois Tigeot out:
160319c468b4SFrançois Tigeot 	return ret;
160419c468b4SFrançois Tigeot }
1605*3f2dd94aSFrançois Tigeot 
1606*3f2dd94aSFrançois Tigeot /**
1607*3f2dd94aSFrançois Tigeot  * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
1608*3f2dd94aSFrançois Tigeot  * output format
1609*3f2dd94aSFrançois Tigeot  *
1610*3f2dd94aSFrançois Tigeot  * @display: display under action
1611*3f2dd94aSFrançois Tigeot  * @mode: video mode to be tested.
1612*3f2dd94aSFrançois Tigeot  *
1613*3f2dd94aSFrançois Tigeot  * Returns:
1614*3f2dd94aSFrançois Tigeot  * true if the mode can be supported in YCBCR420 format
1615*3f2dd94aSFrançois Tigeot  * false if not.
1616*3f2dd94aSFrançois Tigeot  */
drm_mode_is_420_only(const struct drm_display_info * display,const struct drm_display_mode * mode)1617*3f2dd94aSFrançois Tigeot bool drm_mode_is_420_only(const struct drm_display_info *display,
1618*3f2dd94aSFrançois Tigeot 			  const struct drm_display_mode *mode)
1619*3f2dd94aSFrançois Tigeot {
1620*3f2dd94aSFrançois Tigeot 	u8 vic = drm_match_cea_mode(mode);
1621*3f2dd94aSFrançois Tigeot 
1622*3f2dd94aSFrançois Tigeot 	return test_bit(vic, display->hdmi.y420_vdb_modes);
1623*3f2dd94aSFrançois Tigeot }
1624*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_mode_is_420_only);
1625*3f2dd94aSFrançois Tigeot 
1626*3f2dd94aSFrançois Tigeot /**
1627*3f2dd94aSFrançois Tigeot  * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
1628*3f2dd94aSFrançois Tigeot  * output format also (along with RGB/YCBCR444/422)
1629*3f2dd94aSFrançois Tigeot  *
1630*3f2dd94aSFrançois Tigeot  * @display: display under action.
1631*3f2dd94aSFrançois Tigeot  * @mode: video mode to be tested.
1632*3f2dd94aSFrançois Tigeot  *
1633*3f2dd94aSFrançois Tigeot  * Returns:
1634*3f2dd94aSFrançois Tigeot  * true if the mode can be support YCBCR420 format
1635*3f2dd94aSFrançois Tigeot  * false if not.
1636*3f2dd94aSFrançois Tigeot  */
drm_mode_is_420_also(const struct drm_display_info * display,const struct drm_display_mode * mode)1637*3f2dd94aSFrançois Tigeot bool drm_mode_is_420_also(const struct drm_display_info *display,
1638*3f2dd94aSFrançois Tigeot 			  const struct drm_display_mode *mode)
1639*3f2dd94aSFrançois Tigeot {
1640*3f2dd94aSFrançois Tigeot 	u8 vic = drm_match_cea_mode(mode);
1641*3f2dd94aSFrançois Tigeot 
1642*3f2dd94aSFrançois Tigeot 	return test_bit(vic, display->hdmi.y420_cmdb_modes);
1643*3f2dd94aSFrançois Tigeot }
1644*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_mode_is_420_also);
1645*3f2dd94aSFrançois Tigeot /**
1646*3f2dd94aSFrançois Tigeot  * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
1647*3f2dd94aSFrançois Tigeot  * output format
1648*3f2dd94aSFrançois Tigeot  *
1649*3f2dd94aSFrançois Tigeot  * @display: display under action.
1650*3f2dd94aSFrançois Tigeot  * @mode: video mode to be tested.
1651*3f2dd94aSFrançois Tigeot  *
1652*3f2dd94aSFrançois Tigeot  * Returns:
1653*3f2dd94aSFrançois Tigeot  * true if the mode can be supported in YCBCR420 format
1654*3f2dd94aSFrançois Tigeot  * false if not.
1655*3f2dd94aSFrançois Tigeot  */
drm_mode_is_420(const struct drm_display_info * display,const struct drm_display_mode * mode)1656*3f2dd94aSFrançois Tigeot bool drm_mode_is_420(const struct drm_display_info *display,
1657*3f2dd94aSFrançois Tigeot 		     const struct drm_display_mode *mode)
1658*3f2dd94aSFrançois Tigeot {
1659*3f2dd94aSFrançois Tigeot 	return drm_mode_is_420_only(display, mode) ||
1660*3f2dd94aSFrançois Tigeot 		drm_mode_is_420_also(display, mode);
1661*3f2dd94aSFrançois Tigeot }
1662*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_mode_is_420);
1663