xref: /openbmc/linux/drivers/gpu/drm/drm_modes.c (revision 1e84dadb)
1f453ba04SDave Airlie /*
2f453ba04SDave Airlie  * Copyright © 1997-2003 by The XFree86 Project, Inc.
3f453ba04SDave Airlie  * Copyright © 2007 Dave Airlie
4f453ba04SDave Airlie  * Copyright © 2007-2008 Intel Corporation
5f453ba04SDave Airlie  *   Jesse Barnes <jesse.barnes@intel.com>
6d782c3f9SZhao Yakui  * Copyright 2005-2006 Luc Verhaegen
726bbdadaSZhao Yakui  * Copyright (c) 2001, Andy Ritger  aritger@nvidia.com
8f453ba04SDave Airlie  *
9f453ba04SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
10f453ba04SDave Airlie  * copy of this software and associated documentation files (the "Software"),
11f453ba04SDave Airlie  * to deal in the Software without restriction, including without limitation
12f453ba04SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13f453ba04SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
14f453ba04SDave Airlie  * Software is furnished to do so, subject to the following conditions:
15f453ba04SDave Airlie  *
16f453ba04SDave Airlie  * The above copyright notice and this permission notice shall be included in
17f453ba04SDave Airlie  * all copies or substantial portions of the Software.
18f453ba04SDave Airlie  *
19f453ba04SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20f453ba04SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21f453ba04SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22f453ba04SDave Airlie  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23f453ba04SDave Airlie  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24f453ba04SDave Airlie  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25f453ba04SDave Airlie  * OTHER DEALINGS IN THE SOFTWARE.
26f453ba04SDave Airlie  *
27f453ba04SDave Airlie  * Except as contained in this notice, the name of the copyright holder(s)
28f453ba04SDave Airlie  * and author(s) shall not be used in advertising or otherwise to promote
29f453ba04SDave Airlie  * the sale, use or other dealings in this Software without prior written
30f453ba04SDave Airlie  * authorization from the copyright holder(s) and author(s).
31f453ba04SDave Airlie  */
32f453ba04SDave Airlie 
33e08ab74bSMaxime Ripard #include <linux/ctype.h>
34fb28b3f0SThomas Zimmermann #include <linux/export.h>
35fb28b3f0SThomas Zimmermann #include <linux/fb.h> /* for KHZ2PICOS() */
36f453ba04SDave Airlie #include <linux/list.h>
372c761270SDave Chinner #include <linux/list_sort.h>
38fb28b3f0SThomas Zimmermann #include <linux/of.h>
390500c04eSSam Ravnborg 
4095ae342dSNoralf Trønnes #include <video/of_display_timing.h>
41edb37a95SSteffen Trumtrar #include <video/of_videomode.h>
42ebc64e45SSteffen Trumtrar #include <video/videomode.h>
430500c04eSSam Ravnborg 
440500c04eSSam Ravnborg #include <drm/drm_crtc.h>
450500c04eSSam Ravnborg #include <drm/drm_device.h>
46255490f9SVille Syrjälä #include <drm/drm_edid.h>
4755310008SDaniel Vetter #include <drm/drm_modes.h>
480500c04eSSam Ravnborg #include <drm/drm_print.h>
49f453ba04SDave Airlie 
508bd441b2SDaniel Vetter #include "drm_crtc_internal.h"
518bd441b2SDaniel Vetter 
52f453ba04SDave Airlie /**
533ec0db81SDaniel Vetter  * drm_mode_debug_printmodeline - print a mode to dmesg
54f453ba04SDave Airlie  * @mode: mode to print
55f453ba04SDave Airlie  *
56f453ba04SDave Airlie  * Describe @mode using DRM_DEBUG.
57f453ba04SDave Airlie  */
drm_mode_debug_printmodeline(const struct drm_display_mode * mode)580b3904abSVille Syrjälä void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
59f453ba04SDave Airlie {
6065c7dc18SRob Clark 	DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
61f453ba04SDave Airlie }
62f453ba04SDave Airlie EXPORT_SYMBOL(drm_mode_debug_printmodeline);
63f453ba04SDave Airlie 
64f453ba04SDave Airlie /**
658bd441b2SDaniel Vetter  * drm_mode_create - create a new display mode
668bd441b2SDaniel Vetter  * @dev: DRM device
678bd441b2SDaniel Vetter  *
68f5aabb97SDaniel Vetter  * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
69f5aabb97SDaniel Vetter  * and return it.
708bd441b2SDaniel Vetter  *
71f5aabb97SDaniel Vetter  * Returns:
728bd441b2SDaniel Vetter  * Pointer to new mode on success, NULL on error.
738bd441b2SDaniel Vetter  */
drm_mode_create(struct drm_device * dev)748bd441b2SDaniel Vetter struct drm_display_mode *drm_mode_create(struct drm_device *dev)
758bd441b2SDaniel Vetter {
768bd441b2SDaniel Vetter 	struct drm_display_mode *nmode;
778bd441b2SDaniel Vetter 
788bd441b2SDaniel Vetter 	nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
798bd441b2SDaniel Vetter 	if (!nmode)
808bd441b2SDaniel Vetter 		return NULL;
818bd441b2SDaniel Vetter 
828bd441b2SDaniel Vetter 	return nmode;
838bd441b2SDaniel Vetter }
848bd441b2SDaniel Vetter EXPORT_SYMBOL(drm_mode_create);
858bd441b2SDaniel Vetter 
868bd441b2SDaniel Vetter /**
878bd441b2SDaniel Vetter  * drm_mode_destroy - remove a mode
888bd441b2SDaniel Vetter  * @dev: DRM device
898bd441b2SDaniel Vetter  * @mode: mode to remove
908bd441b2SDaniel Vetter  *
91f5aabb97SDaniel Vetter  * Release @mode's unique ID, then free it @mode structure itself using kfree.
928bd441b2SDaniel Vetter  */
drm_mode_destroy(struct drm_device * dev,struct drm_display_mode * mode)938bd441b2SDaniel Vetter void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
948bd441b2SDaniel Vetter {
958bd441b2SDaniel Vetter 	if (!mode)
968bd441b2SDaniel Vetter 		return;
978bd441b2SDaniel Vetter 
988bd441b2SDaniel Vetter 	kfree(mode);
998bd441b2SDaniel Vetter }
1008bd441b2SDaniel Vetter EXPORT_SYMBOL(drm_mode_destroy);
1018bd441b2SDaniel Vetter 
1028bd441b2SDaniel Vetter /**
103f5aabb97SDaniel Vetter  * drm_mode_probed_add - add a mode to a connector's probed_mode list
1048bd441b2SDaniel Vetter  * @connector: connector the new mode
1058bd441b2SDaniel Vetter  * @mode: mode data
1068bd441b2SDaniel Vetter  *
107f5aabb97SDaniel Vetter  * Add @mode to @connector's probed_mode list for later use. This list should
108f5aabb97SDaniel Vetter  * then in a second step get filtered and all the modes actually supported by
109f5aabb97SDaniel Vetter  * the hardware moved to the @connector's modes list.
1108bd441b2SDaniel Vetter  */
drm_mode_probed_add(struct drm_connector * connector,struct drm_display_mode * mode)1118bd441b2SDaniel Vetter void drm_mode_probed_add(struct drm_connector *connector,
1128bd441b2SDaniel Vetter 			 struct drm_display_mode *mode)
1138bd441b2SDaniel Vetter {
1148bd441b2SDaniel Vetter 	WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1158bd441b2SDaniel Vetter 
1168bd441b2SDaniel Vetter 	list_add_tail(&mode->head, &connector->probed_modes);
1178bd441b2SDaniel Vetter }
1188bd441b2SDaniel Vetter EXPORT_SYMBOL(drm_mode_probed_add);
1198bd441b2SDaniel Vetter 
1204fcd2385SMaxime Ripard enum drm_mode_analog {
1214fcd2385SMaxime Ripard 	DRM_MODE_ANALOG_NTSC, /* 525 lines, 60Hz */
1224fcd2385SMaxime Ripard 	DRM_MODE_ANALOG_PAL, /* 625 lines, 50Hz */
1234fcd2385SMaxime Ripard };
1244fcd2385SMaxime Ripard 
1254fcd2385SMaxime Ripard /*
1264fcd2385SMaxime Ripard  * The timings come from:
1274fcd2385SMaxime Ripard  * - https://web.archive.org/web/20220406232708/http://www.kolumbus.fi/pami1/video/pal_ntsc.html
1284fcd2385SMaxime Ripard  * - https://web.archive.org/web/20220406124914/http://martin.hinner.info/vga/pal.html
1294fcd2385SMaxime Ripard  * - https://web.archive.org/web/20220609202433/http://www.batsocks.co.uk/readme/video_timing.htm
1304fcd2385SMaxime Ripard  */
1314fcd2385SMaxime Ripard #define NTSC_LINE_DURATION_NS		63556U
1324fcd2385SMaxime Ripard #define NTSC_LINES_NUMBER		525
1334fcd2385SMaxime Ripard 
1344fcd2385SMaxime Ripard #define NTSC_HBLK_DURATION_TYP_NS	10900U
1354fcd2385SMaxime Ripard #define NTSC_HBLK_DURATION_MIN_NS	(NTSC_HBLK_DURATION_TYP_NS - 200)
1364fcd2385SMaxime Ripard #define NTSC_HBLK_DURATION_MAX_NS	(NTSC_HBLK_DURATION_TYP_NS + 200)
1374fcd2385SMaxime Ripard 
1384fcd2385SMaxime Ripard #define NTSC_HACT_DURATION_TYP_NS	(NTSC_LINE_DURATION_NS - NTSC_HBLK_DURATION_TYP_NS)
1394fcd2385SMaxime Ripard #define NTSC_HACT_DURATION_MIN_NS	(NTSC_LINE_DURATION_NS - NTSC_HBLK_DURATION_MAX_NS)
1404fcd2385SMaxime Ripard #define NTSC_HACT_DURATION_MAX_NS	(NTSC_LINE_DURATION_NS - NTSC_HBLK_DURATION_MIN_NS)
1414fcd2385SMaxime Ripard 
1424fcd2385SMaxime Ripard #define NTSC_HFP_DURATION_TYP_NS	1500
1434fcd2385SMaxime Ripard #define NTSC_HFP_DURATION_MIN_NS	1270
1444fcd2385SMaxime Ripard #define NTSC_HFP_DURATION_MAX_NS	2220
1454fcd2385SMaxime Ripard 
1464fcd2385SMaxime Ripard #define NTSC_HSLEN_DURATION_TYP_NS	4700
1474fcd2385SMaxime Ripard #define NTSC_HSLEN_DURATION_MIN_NS	(NTSC_HSLEN_DURATION_TYP_NS - 100)
1484fcd2385SMaxime Ripard #define NTSC_HSLEN_DURATION_MAX_NS	(NTSC_HSLEN_DURATION_TYP_NS + 100)
1494fcd2385SMaxime Ripard 
1504fcd2385SMaxime Ripard #define NTSC_HBP_DURATION_TYP_NS	4700
1514fcd2385SMaxime Ripard 
1524fcd2385SMaxime Ripard /*
1534fcd2385SMaxime Ripard  * I couldn't find the actual tolerance for the back porch, so let's
1544fcd2385SMaxime Ripard  * just reuse the sync length ones.
1554fcd2385SMaxime Ripard  */
1564fcd2385SMaxime Ripard #define NTSC_HBP_DURATION_MIN_NS	(NTSC_HBP_DURATION_TYP_NS - 100)
1574fcd2385SMaxime Ripard #define NTSC_HBP_DURATION_MAX_NS	(NTSC_HBP_DURATION_TYP_NS + 100)
1584fcd2385SMaxime Ripard 
1594fcd2385SMaxime Ripard #define PAL_LINE_DURATION_NS		64000U
1604fcd2385SMaxime Ripard #define PAL_LINES_NUMBER		625
1614fcd2385SMaxime Ripard 
1624fcd2385SMaxime Ripard #define PAL_HACT_DURATION_TYP_NS	51950U
1634fcd2385SMaxime Ripard #define PAL_HACT_DURATION_MIN_NS	(PAL_HACT_DURATION_TYP_NS - 100)
1644fcd2385SMaxime Ripard #define PAL_HACT_DURATION_MAX_NS	(PAL_HACT_DURATION_TYP_NS + 400)
1654fcd2385SMaxime Ripard 
1664fcd2385SMaxime Ripard #define PAL_HBLK_DURATION_TYP_NS	(PAL_LINE_DURATION_NS - PAL_HACT_DURATION_TYP_NS)
1674fcd2385SMaxime Ripard #define PAL_HBLK_DURATION_MIN_NS	(PAL_LINE_DURATION_NS - PAL_HACT_DURATION_MAX_NS)
1684fcd2385SMaxime Ripard #define PAL_HBLK_DURATION_MAX_NS	(PAL_LINE_DURATION_NS - PAL_HACT_DURATION_MIN_NS)
1694fcd2385SMaxime Ripard 
1704fcd2385SMaxime Ripard #define PAL_HFP_DURATION_TYP_NS		1650
1714fcd2385SMaxime Ripard #define PAL_HFP_DURATION_MIN_NS		(PAL_HFP_DURATION_TYP_NS - 100)
1724fcd2385SMaxime Ripard #define PAL_HFP_DURATION_MAX_NS		(PAL_HFP_DURATION_TYP_NS + 400)
1734fcd2385SMaxime Ripard 
1744fcd2385SMaxime Ripard #define PAL_HSLEN_DURATION_TYP_NS	4700
1754fcd2385SMaxime Ripard #define PAL_HSLEN_DURATION_MIN_NS	(PAL_HSLEN_DURATION_TYP_NS - 200)
1764fcd2385SMaxime Ripard #define PAL_HSLEN_DURATION_MAX_NS	(PAL_HSLEN_DURATION_TYP_NS + 200)
1774fcd2385SMaxime Ripard 
1784fcd2385SMaxime Ripard #define PAL_HBP_DURATION_TYP_NS		5700
1794fcd2385SMaxime Ripard #define PAL_HBP_DURATION_MIN_NS		(PAL_HBP_DURATION_TYP_NS - 200)
1804fcd2385SMaxime Ripard #define PAL_HBP_DURATION_MAX_NS		(PAL_HBP_DURATION_TYP_NS + 200)
1814fcd2385SMaxime Ripard 
1824fcd2385SMaxime Ripard struct analog_param_field {
1834fcd2385SMaxime Ripard 	unsigned int even, odd;
1844fcd2385SMaxime Ripard };
1854fcd2385SMaxime Ripard 
1864fcd2385SMaxime Ripard #define PARAM_FIELD(_odd, _even)		\
1874fcd2385SMaxime Ripard 	{ .even = _even, .odd = _odd }
1884fcd2385SMaxime Ripard 
1894fcd2385SMaxime Ripard struct analog_param_range {
1904fcd2385SMaxime Ripard 	unsigned int	min, typ, max;
1914fcd2385SMaxime Ripard };
1924fcd2385SMaxime Ripard 
1934fcd2385SMaxime Ripard #define PARAM_RANGE(_min, _typ, _max)		\
1944fcd2385SMaxime Ripard 	{ .min = _min, .typ = _typ, .max = _max }
1954fcd2385SMaxime Ripard 
1964fcd2385SMaxime Ripard struct analog_parameters {
1974fcd2385SMaxime Ripard 	unsigned int			num_lines;
1984fcd2385SMaxime Ripard 	unsigned int			line_duration_ns;
1994fcd2385SMaxime Ripard 
2004fcd2385SMaxime Ripard 	struct analog_param_range	hact_ns;
2014fcd2385SMaxime Ripard 	struct analog_param_range	hfp_ns;
2024fcd2385SMaxime Ripard 	struct analog_param_range	hslen_ns;
2034fcd2385SMaxime Ripard 	struct analog_param_range	hbp_ns;
2044fcd2385SMaxime Ripard 	struct analog_param_range	hblk_ns;
2054fcd2385SMaxime Ripard 
2064fcd2385SMaxime Ripard 	unsigned int			bt601_hfp;
2074fcd2385SMaxime Ripard 
2084fcd2385SMaxime Ripard 	struct analog_param_field	vfp_lines;
2094fcd2385SMaxime Ripard 	struct analog_param_field	vslen_lines;
2104fcd2385SMaxime Ripard 	struct analog_param_field	vbp_lines;
2114fcd2385SMaxime Ripard };
2124fcd2385SMaxime Ripard 
2134fcd2385SMaxime Ripard #define TV_MODE_PARAMETER(_mode, _lines, _line_dur, _hact, _hfp,	\
2144fcd2385SMaxime Ripard 			  _hslen, _hbp, _hblk, _bt601_hfp, _vfp,	\
2154fcd2385SMaxime Ripard 			  _vslen, _vbp)					\
2164fcd2385SMaxime Ripard 	[_mode] = {							\
2174fcd2385SMaxime Ripard 		.num_lines = _lines,					\
2184fcd2385SMaxime Ripard 		.line_duration_ns = _line_dur,				\
2194fcd2385SMaxime Ripard 		.hact_ns = _hact,					\
2204fcd2385SMaxime Ripard 		.hfp_ns = _hfp,						\
2214fcd2385SMaxime Ripard 		.hslen_ns = _hslen,					\
2224fcd2385SMaxime Ripard 		.hbp_ns = _hbp,						\
2234fcd2385SMaxime Ripard 		.hblk_ns = _hblk,					\
2244fcd2385SMaxime Ripard 		.bt601_hfp = _bt601_hfp,				\
2254fcd2385SMaxime Ripard 		.vfp_lines = _vfp,					\
2264fcd2385SMaxime Ripard 		.vslen_lines = _vslen,					\
2274fcd2385SMaxime Ripard 		.vbp_lines = _vbp,					\
2284fcd2385SMaxime Ripard 	}
2294fcd2385SMaxime Ripard 
2304fcd2385SMaxime Ripard static const struct analog_parameters tv_modes_parameters[] = {
2314fcd2385SMaxime Ripard 	TV_MODE_PARAMETER(DRM_MODE_ANALOG_NTSC,
2324fcd2385SMaxime Ripard 			  NTSC_LINES_NUMBER,
2334fcd2385SMaxime Ripard 			  NTSC_LINE_DURATION_NS,
2344fcd2385SMaxime Ripard 			  PARAM_RANGE(NTSC_HACT_DURATION_MIN_NS,
2354fcd2385SMaxime Ripard 				      NTSC_HACT_DURATION_TYP_NS,
2364fcd2385SMaxime Ripard 				      NTSC_HACT_DURATION_MAX_NS),
2374fcd2385SMaxime Ripard 			  PARAM_RANGE(NTSC_HFP_DURATION_MIN_NS,
2384fcd2385SMaxime Ripard 				      NTSC_HFP_DURATION_TYP_NS,
2394fcd2385SMaxime Ripard 				      NTSC_HFP_DURATION_MAX_NS),
2404fcd2385SMaxime Ripard 			  PARAM_RANGE(NTSC_HSLEN_DURATION_MIN_NS,
2414fcd2385SMaxime Ripard 				      NTSC_HSLEN_DURATION_TYP_NS,
2424fcd2385SMaxime Ripard 				      NTSC_HSLEN_DURATION_MAX_NS),
2434fcd2385SMaxime Ripard 			  PARAM_RANGE(NTSC_HBP_DURATION_MIN_NS,
2444fcd2385SMaxime Ripard 				      NTSC_HBP_DURATION_TYP_NS,
2454fcd2385SMaxime Ripard 				      NTSC_HBP_DURATION_MAX_NS),
2464fcd2385SMaxime Ripard 			  PARAM_RANGE(NTSC_HBLK_DURATION_MIN_NS,
2474fcd2385SMaxime Ripard 				      NTSC_HBLK_DURATION_TYP_NS,
2484fcd2385SMaxime Ripard 				      NTSC_HBLK_DURATION_MAX_NS),
2494fcd2385SMaxime Ripard 			  16,
2504fcd2385SMaxime Ripard 			  PARAM_FIELD(3, 3),
2514fcd2385SMaxime Ripard 			  PARAM_FIELD(3, 3),
2524fcd2385SMaxime Ripard 			  PARAM_FIELD(16, 17)),
2534fcd2385SMaxime Ripard 	TV_MODE_PARAMETER(DRM_MODE_ANALOG_PAL,
2544fcd2385SMaxime Ripard 			  PAL_LINES_NUMBER,
2554fcd2385SMaxime Ripard 			  PAL_LINE_DURATION_NS,
2564fcd2385SMaxime Ripard 			  PARAM_RANGE(PAL_HACT_DURATION_MIN_NS,
2574fcd2385SMaxime Ripard 				      PAL_HACT_DURATION_TYP_NS,
2584fcd2385SMaxime Ripard 				      PAL_HACT_DURATION_MAX_NS),
2594fcd2385SMaxime Ripard 			  PARAM_RANGE(PAL_HFP_DURATION_MIN_NS,
2604fcd2385SMaxime Ripard 				      PAL_HFP_DURATION_TYP_NS,
2614fcd2385SMaxime Ripard 				      PAL_HFP_DURATION_MAX_NS),
2624fcd2385SMaxime Ripard 			  PARAM_RANGE(PAL_HSLEN_DURATION_MIN_NS,
2634fcd2385SMaxime Ripard 				      PAL_HSLEN_DURATION_TYP_NS,
2644fcd2385SMaxime Ripard 				      PAL_HSLEN_DURATION_MAX_NS),
2654fcd2385SMaxime Ripard 			  PARAM_RANGE(PAL_HBP_DURATION_MIN_NS,
2664fcd2385SMaxime Ripard 				      PAL_HBP_DURATION_TYP_NS,
2674fcd2385SMaxime Ripard 				      PAL_HBP_DURATION_MAX_NS),
2684fcd2385SMaxime Ripard 			  PARAM_RANGE(PAL_HBLK_DURATION_MIN_NS,
2694fcd2385SMaxime Ripard 				      PAL_HBLK_DURATION_TYP_NS,
2704fcd2385SMaxime Ripard 				      PAL_HBLK_DURATION_MAX_NS),
2714fcd2385SMaxime Ripard 			  12,
2724fcd2385SMaxime Ripard 
2734fcd2385SMaxime Ripard 			  /*
2744fcd2385SMaxime Ripard 			   * The front porch is actually 6 short sync
2754fcd2385SMaxime Ripard 			   * pulses for the even field, and 5 for the
2764fcd2385SMaxime Ripard 			   * odd field. Each sync takes half a life so
2774fcd2385SMaxime Ripard 			   * the odd field front porch is shorter by
2784fcd2385SMaxime Ripard 			   * half a line.
2794fcd2385SMaxime Ripard 			   *
2804fcd2385SMaxime Ripard 			   * In progressive, we're supposed to use 6
2814fcd2385SMaxime Ripard 			   * pulses, so we're fine there
2824fcd2385SMaxime Ripard 			   */
2834fcd2385SMaxime Ripard 			  PARAM_FIELD(3, 2),
2844fcd2385SMaxime Ripard 
2854fcd2385SMaxime Ripard 			  /*
2864fcd2385SMaxime Ripard 			   * The vsync length is 5 long sync pulses,
2874fcd2385SMaxime Ripard 			   * each field taking half a line. We're
2884fcd2385SMaxime Ripard 			   * shorter for both fields by half a line.
2894fcd2385SMaxime Ripard 			   *
2904fcd2385SMaxime Ripard 			   * In progressive, we're supposed to use 5
2914fcd2385SMaxime Ripard 			   * pulses, so we're off by half
2924fcd2385SMaxime Ripard 			   * a line.
2934fcd2385SMaxime Ripard 			   *
2944fcd2385SMaxime Ripard 			   * In interlace, we're now off by half a line
2954fcd2385SMaxime Ripard 			   * for the even field and one line for the odd
2964fcd2385SMaxime Ripard 			   * field.
2974fcd2385SMaxime Ripard 			   */
2984fcd2385SMaxime Ripard 			  PARAM_FIELD(3, 3),
2994fcd2385SMaxime Ripard 
3004fcd2385SMaxime Ripard 			  /*
3014fcd2385SMaxime Ripard 			   * The back porch starts with post-equalizing
3024fcd2385SMaxime Ripard 			   * pulses, consisting in 5 short sync pulses
3034fcd2385SMaxime Ripard 			   * for the even field, 4 for the odd field. In
3044fcd2385SMaxime Ripard 			   * progressive, it's 5 short syncs.
3054fcd2385SMaxime Ripard 			   *
3064fcd2385SMaxime Ripard 			   * In progressive, we thus have 2.5 lines,
3074fcd2385SMaxime Ripard 			   * plus the 0.5 line we were missing
3084fcd2385SMaxime Ripard 			   * previously, so we should use 3 lines.
3094fcd2385SMaxime Ripard 			   *
3104fcd2385SMaxime Ripard 			   * In interlace, the even field is in the
3114fcd2385SMaxime Ripard 			   * exact same case than progressive. For the
3124fcd2385SMaxime Ripard 			   * odd field, we should be using 2 lines but
3134fcd2385SMaxime Ripard 			   * we're one line short, so we'll make up for
3144fcd2385SMaxime Ripard 			   * it here by using 3.
3154fcd2385SMaxime Ripard 			   *
3164fcd2385SMaxime Ripard 			   * The entire blanking area is supposed to
3174fcd2385SMaxime Ripard 			   * take 25 lines, so we also need to account
3184fcd2385SMaxime Ripard 			   * for the rest of the blanking area that
3194fcd2385SMaxime Ripard 			   * can't be in either the front porch or sync
3204fcd2385SMaxime Ripard 			   * period.
3214fcd2385SMaxime Ripard 			   */
3224fcd2385SMaxime Ripard 			  PARAM_FIELD(19, 20)),
3234fcd2385SMaxime Ripard };
3244fcd2385SMaxime Ripard 
fill_analog_mode(struct drm_device * dev,struct drm_display_mode * mode,const struct analog_parameters * params,unsigned long pixel_clock_hz,unsigned int hactive,unsigned int vactive,bool interlace)3254fcd2385SMaxime Ripard static int fill_analog_mode(struct drm_device *dev,
3264fcd2385SMaxime Ripard 			    struct drm_display_mode *mode,
3274fcd2385SMaxime Ripard 			    const struct analog_parameters *params,
3284fcd2385SMaxime Ripard 			    unsigned long pixel_clock_hz,
3294fcd2385SMaxime Ripard 			    unsigned int hactive,
3304fcd2385SMaxime Ripard 			    unsigned int vactive,
3314fcd2385SMaxime Ripard 			    bool interlace)
3324fcd2385SMaxime Ripard {
3334fcd2385SMaxime Ripard 	unsigned long pixel_duration_ns = NSEC_PER_SEC / pixel_clock_hz;
3344fcd2385SMaxime Ripard 	unsigned int htotal, vtotal;
3354fcd2385SMaxime Ripard 	unsigned int max_hact, hact_duration_ns;
3364fcd2385SMaxime Ripard 	unsigned int hblk, hblk_duration_ns;
3374fcd2385SMaxime Ripard 	unsigned int hfp, hfp_duration_ns;
3384fcd2385SMaxime Ripard 	unsigned int hslen, hslen_duration_ns;
3394fcd2385SMaxime Ripard 	unsigned int hbp, hbp_duration_ns;
3404fcd2385SMaxime Ripard 	unsigned int porches, porches_duration_ns;
3414fcd2385SMaxime Ripard 	unsigned int vfp, vfp_min;
3424fcd2385SMaxime Ripard 	unsigned int vbp, vbp_min;
3434fcd2385SMaxime Ripard 	unsigned int vslen;
3444fcd2385SMaxime Ripard 	bool bt601 = false;
3454fcd2385SMaxime Ripard 	int porches_rem;
3464fcd2385SMaxime Ripard 	u64 result;
3474fcd2385SMaxime Ripard 
3484fcd2385SMaxime Ripard 	drm_dbg_kms(dev,
3494fcd2385SMaxime Ripard 		    "Generating a %ux%u%c, %u-line mode with a %lu kHz clock\n",
3504fcd2385SMaxime Ripard 		    hactive, vactive,
3514fcd2385SMaxime Ripard 		    interlace ? 'i' : 'p',
3524fcd2385SMaxime Ripard 		    params->num_lines,
3534fcd2385SMaxime Ripard 		    pixel_clock_hz / 1000);
3544fcd2385SMaxime Ripard 
3554fcd2385SMaxime Ripard 	max_hact = params->hact_ns.max / pixel_duration_ns;
3564fcd2385SMaxime Ripard 	if (pixel_clock_hz == 13500000 && hactive > max_hact && hactive <= 720) {
3574fcd2385SMaxime Ripard 		drm_dbg_kms(dev, "Trying to generate a BT.601 mode. Disabling checks.\n");
3584fcd2385SMaxime Ripard 		bt601 = true;
3594fcd2385SMaxime Ripard 	}
3604fcd2385SMaxime Ripard 
3614fcd2385SMaxime Ripard 	/*
3624fcd2385SMaxime Ripard 	 * Our pixel duration is going to be round down by the division,
3634fcd2385SMaxime Ripard 	 * so rounding up is probably going to introduce even more
3644fcd2385SMaxime Ripard 	 * deviation.
3654fcd2385SMaxime Ripard 	 */
3664fcd2385SMaxime Ripard 	result = (u64)params->line_duration_ns * pixel_clock_hz;
3674fcd2385SMaxime Ripard 	do_div(result, NSEC_PER_SEC);
3684fcd2385SMaxime Ripard 	htotal = result;
3694fcd2385SMaxime Ripard 
3704fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Total Horizontal Number of Pixels: %u\n", htotal);
3714fcd2385SMaxime Ripard 
3724fcd2385SMaxime Ripard 	hact_duration_ns = hactive * pixel_duration_ns;
3734fcd2385SMaxime Ripard 	if (!bt601 &&
3744fcd2385SMaxime Ripard 	    (hact_duration_ns < params->hact_ns.min ||
3754fcd2385SMaxime Ripard 	     hact_duration_ns > params->hact_ns.max)) {
3764fcd2385SMaxime Ripard 		DRM_ERROR("Invalid horizontal active area duration: %uns (min: %u, max %u)\n",
3774fcd2385SMaxime Ripard 			  hact_duration_ns, params->hact_ns.min, params->hact_ns.max);
3784fcd2385SMaxime Ripard 		return -EINVAL;
3794fcd2385SMaxime Ripard 	}
3804fcd2385SMaxime Ripard 
3814fcd2385SMaxime Ripard 	hblk = htotal - hactive;
3824fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Horizontal Blanking Period: %u\n", hblk);
3834fcd2385SMaxime Ripard 
3844fcd2385SMaxime Ripard 	hblk_duration_ns = hblk * pixel_duration_ns;
3854fcd2385SMaxime Ripard 	if (!bt601 &&
3864fcd2385SMaxime Ripard 	    (hblk_duration_ns < params->hblk_ns.min ||
3874fcd2385SMaxime Ripard 	     hblk_duration_ns > params->hblk_ns.max)) {
3884fcd2385SMaxime Ripard 		DRM_ERROR("Invalid horizontal blanking duration: %uns (min: %u, max %u)\n",
3894fcd2385SMaxime Ripard 			  hblk_duration_ns, params->hblk_ns.min, params->hblk_ns.max);
3904fcd2385SMaxime Ripard 		return -EINVAL;
3914fcd2385SMaxime Ripard 	}
3924fcd2385SMaxime Ripard 
3934fcd2385SMaxime Ripard 	hslen = DIV_ROUND_UP(params->hslen_ns.typ, pixel_duration_ns);
3944fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Horizontal Sync Period: %u\n", hslen);
3954fcd2385SMaxime Ripard 
3964fcd2385SMaxime Ripard 	hslen_duration_ns = hslen * pixel_duration_ns;
3974fcd2385SMaxime Ripard 	if (!bt601 &&
3984fcd2385SMaxime Ripard 	    (hslen_duration_ns < params->hslen_ns.min ||
3994fcd2385SMaxime Ripard 	     hslen_duration_ns > params->hslen_ns.max)) {
4004fcd2385SMaxime Ripard 		DRM_ERROR("Invalid horizontal sync duration: %uns (min: %u, max %u)\n",
4014fcd2385SMaxime Ripard 			  hslen_duration_ns, params->hslen_ns.min, params->hslen_ns.max);
4024fcd2385SMaxime Ripard 		return -EINVAL;
4034fcd2385SMaxime Ripard 	}
4044fcd2385SMaxime Ripard 
4054fcd2385SMaxime Ripard 	porches = hblk - hslen;
4064fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Remaining horizontal pixels for both porches: %u\n", porches);
4074fcd2385SMaxime Ripard 
4084fcd2385SMaxime Ripard 	porches_duration_ns = porches * pixel_duration_ns;
4094fcd2385SMaxime Ripard 	if (!bt601 &&
4104fcd2385SMaxime Ripard 	    (porches_duration_ns > (params->hfp_ns.max + params->hbp_ns.max) ||
4114fcd2385SMaxime Ripard 	     porches_duration_ns < (params->hfp_ns.min + params->hbp_ns.min))) {
4124fcd2385SMaxime Ripard 		DRM_ERROR("Invalid horizontal porches duration: %uns\n", porches_duration_ns);
4134fcd2385SMaxime Ripard 		return -EINVAL;
4144fcd2385SMaxime Ripard 	}
4154fcd2385SMaxime Ripard 
4164fcd2385SMaxime Ripard 	if (bt601) {
4174fcd2385SMaxime Ripard 		hfp = params->bt601_hfp;
4184fcd2385SMaxime Ripard 	} else {
4194fcd2385SMaxime Ripard 		unsigned int hfp_min = DIV_ROUND_UP(params->hfp_ns.min,
4204fcd2385SMaxime Ripard 						    pixel_duration_ns);
4214fcd2385SMaxime Ripard 		unsigned int hbp_min = DIV_ROUND_UP(params->hbp_ns.min,
4224fcd2385SMaxime Ripard 						    pixel_duration_ns);
4234fcd2385SMaxime Ripard 		int porches_rem = porches - hfp_min - hbp_min;
4244fcd2385SMaxime Ripard 
4254fcd2385SMaxime Ripard 		hfp = hfp_min + DIV_ROUND_UP(porches_rem, 2);
4264fcd2385SMaxime Ripard 	}
4274fcd2385SMaxime Ripard 
4284fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Horizontal Front Porch: %u\n", hfp);
4294fcd2385SMaxime Ripard 
4304fcd2385SMaxime Ripard 	hfp_duration_ns = hfp * pixel_duration_ns;
4314fcd2385SMaxime Ripard 	if (!bt601 &&
4324fcd2385SMaxime Ripard 	    (hfp_duration_ns < params->hfp_ns.min ||
4334fcd2385SMaxime Ripard 	     hfp_duration_ns > params->hfp_ns.max)) {
4344fcd2385SMaxime Ripard 		DRM_ERROR("Invalid horizontal front porch duration: %uns (min: %u, max %u)\n",
4354fcd2385SMaxime Ripard 			  hfp_duration_ns, params->hfp_ns.min, params->hfp_ns.max);
4364fcd2385SMaxime Ripard 		return -EINVAL;
4374fcd2385SMaxime Ripard 	}
4384fcd2385SMaxime Ripard 
4394fcd2385SMaxime Ripard 	hbp = porches - hfp;
4404fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Horizontal Back Porch: %u\n", hbp);
4414fcd2385SMaxime Ripard 
4424fcd2385SMaxime Ripard 	hbp_duration_ns = hbp * pixel_duration_ns;
4434fcd2385SMaxime Ripard 	if (!bt601 &&
4444fcd2385SMaxime Ripard 	    (hbp_duration_ns < params->hbp_ns.min ||
4454fcd2385SMaxime Ripard 	     hbp_duration_ns > params->hbp_ns.max)) {
4464fcd2385SMaxime Ripard 		DRM_ERROR("Invalid horizontal back porch duration: %uns (min: %u, max %u)\n",
4474fcd2385SMaxime Ripard 			  hbp_duration_ns, params->hbp_ns.min, params->hbp_ns.max);
4484fcd2385SMaxime Ripard 		return -EINVAL;
4494fcd2385SMaxime Ripard 	}
4504fcd2385SMaxime Ripard 
4514fcd2385SMaxime Ripard 	if (htotal != (hactive + hfp + hslen + hbp))
4524fcd2385SMaxime Ripard 		return -EINVAL;
4534fcd2385SMaxime Ripard 
4544fcd2385SMaxime Ripard 	mode->clock = pixel_clock_hz / 1000;
4554fcd2385SMaxime Ripard 	mode->hdisplay = hactive;
4564fcd2385SMaxime Ripard 	mode->hsync_start = mode->hdisplay + hfp;
4574fcd2385SMaxime Ripard 	mode->hsync_end = mode->hsync_start + hslen;
4584fcd2385SMaxime Ripard 	mode->htotal = mode->hsync_end + hbp;
4594fcd2385SMaxime Ripard 
4604fcd2385SMaxime Ripard 	if (interlace) {
4614fcd2385SMaxime Ripard 		vfp_min = params->vfp_lines.even + params->vfp_lines.odd;
4624fcd2385SMaxime Ripard 		vbp_min = params->vbp_lines.even + params->vbp_lines.odd;
4634fcd2385SMaxime Ripard 		vslen = params->vslen_lines.even + params->vslen_lines.odd;
4644fcd2385SMaxime Ripard 	} else {
4654fcd2385SMaxime Ripard 		/*
4664fcd2385SMaxime Ripard 		 * By convention, NTSC (aka 525/60) systems start with
4674fcd2385SMaxime Ripard 		 * the even field, but PAL (aka 625/50) systems start
4684fcd2385SMaxime Ripard 		 * with the odd one.
4694fcd2385SMaxime Ripard 		 *
4704fcd2385SMaxime Ripard 		 * PAL systems also have asymmetric timings between the
4714fcd2385SMaxime Ripard 		 * even and odd field, while NTSC is symmetric.
4724fcd2385SMaxime Ripard 		 *
4734fcd2385SMaxime Ripard 		 * Moreover, if we want to create a progressive mode for
4744fcd2385SMaxime Ripard 		 * PAL, we need to use the odd field timings.
4754fcd2385SMaxime Ripard 		 *
4764fcd2385SMaxime Ripard 		 * Since odd == even for NTSC, we can just use the odd
4774fcd2385SMaxime Ripard 		 * one all the time to simplify the code a bit.
4784fcd2385SMaxime Ripard 		 */
4794fcd2385SMaxime Ripard 		vfp_min = params->vfp_lines.odd;
4804fcd2385SMaxime Ripard 		vbp_min = params->vbp_lines.odd;
4814fcd2385SMaxime Ripard 		vslen = params->vslen_lines.odd;
4824fcd2385SMaxime Ripard 	}
4834fcd2385SMaxime Ripard 
4844fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Vertical Sync Period: %u\n", vslen);
4854fcd2385SMaxime Ripard 
4864fcd2385SMaxime Ripard 	porches = params->num_lines - vactive - vslen;
4874fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Remaining vertical pixels for both porches: %u\n", porches);
4884fcd2385SMaxime Ripard 
4894fcd2385SMaxime Ripard 	porches_rem = porches - vfp_min - vbp_min;
4904fcd2385SMaxime Ripard 	vfp = vfp_min + (porches_rem / 2);
4914fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Vertical Front Porch: %u\n", vfp);
4924fcd2385SMaxime Ripard 
4934fcd2385SMaxime Ripard 	vbp = porches - vfp;
4944fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Vertical Back Porch: %u\n", vbp);
4954fcd2385SMaxime Ripard 
4964fcd2385SMaxime Ripard 	vtotal = vactive + vfp + vslen + vbp;
4974fcd2385SMaxime Ripard 	if (params->num_lines != vtotal) {
4984fcd2385SMaxime Ripard 		DRM_ERROR("Invalid vertical total: %upx (expected %upx)\n",
4994fcd2385SMaxime Ripard 			  vtotal, params->num_lines);
5004fcd2385SMaxime Ripard 		return -EINVAL;
5014fcd2385SMaxime Ripard 	}
5024fcd2385SMaxime Ripard 
5034fcd2385SMaxime Ripard 	mode->vdisplay = vactive;
5044fcd2385SMaxime Ripard 	mode->vsync_start = mode->vdisplay + vfp;
5054fcd2385SMaxime Ripard 	mode->vsync_end = mode->vsync_start + vslen;
5064fcd2385SMaxime Ripard 	mode->vtotal = mode->vsync_end + vbp;
5074fcd2385SMaxime Ripard 
5084fcd2385SMaxime Ripard 	if (mode->vtotal != params->num_lines)
5094fcd2385SMaxime Ripard 		return -EINVAL;
5104fcd2385SMaxime Ripard 
5114fcd2385SMaxime Ripard 	mode->type = DRM_MODE_TYPE_DRIVER;
5124fcd2385SMaxime Ripard 	mode->flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC;
5134fcd2385SMaxime Ripard 	if (interlace)
5144fcd2385SMaxime Ripard 		mode->flags |= DRM_MODE_FLAG_INTERLACE;
5154fcd2385SMaxime Ripard 
5164fcd2385SMaxime Ripard 	drm_mode_set_name(mode);
5174fcd2385SMaxime Ripard 
5184fcd2385SMaxime Ripard 	drm_dbg_kms(dev, "Generated mode " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
5194fcd2385SMaxime Ripard 
5204fcd2385SMaxime Ripard 	return 0;
5214fcd2385SMaxime Ripard }
5224fcd2385SMaxime Ripard 
5234fcd2385SMaxime Ripard /**
5244fcd2385SMaxime Ripard  * drm_analog_tv_mode - create a display mode for an analog TV
5254fcd2385SMaxime Ripard  * @dev: drm device
5264fcd2385SMaxime Ripard  * @tv_mode: TV Mode standard to create a mode for. See DRM_MODE_TV_MODE_*.
5274fcd2385SMaxime Ripard  * @pixel_clock_hz: Pixel Clock Frequency, in Hertz
5284fcd2385SMaxime Ripard  * @hdisplay: hdisplay size
5294fcd2385SMaxime Ripard  * @vdisplay: vdisplay size
5304fcd2385SMaxime Ripard  * @interlace: whether to compute an interlaced mode
5314fcd2385SMaxime Ripard  *
5324fcd2385SMaxime Ripard  * This function creates a struct drm_display_mode instance suited for
5334fcd2385SMaxime Ripard  * an analog TV output, for one of the usual analog TV mode.
5344fcd2385SMaxime Ripard  *
5354fcd2385SMaxime Ripard  * Note that @hdisplay is larger than the usual constraints for the PAL
5364fcd2385SMaxime Ripard  * and NTSC timings, and we'll choose to ignore most timings constraints
5374fcd2385SMaxime Ripard  * to reach those resolutions.
5384fcd2385SMaxime Ripard  *
5394fcd2385SMaxime Ripard  * Returns:
5404fcd2385SMaxime Ripard  *
5414fcd2385SMaxime Ripard  * A pointer to the mode, allocated with drm_mode_create(). Returns NULL
5424fcd2385SMaxime Ripard  * on error.
5434fcd2385SMaxime Ripard  */
drm_analog_tv_mode(struct drm_device * dev,enum drm_connector_tv_mode tv_mode,unsigned long pixel_clock_hz,unsigned int hdisplay,unsigned int vdisplay,bool interlace)5444fcd2385SMaxime Ripard struct drm_display_mode *drm_analog_tv_mode(struct drm_device *dev,
5454fcd2385SMaxime Ripard 					    enum drm_connector_tv_mode tv_mode,
5464fcd2385SMaxime Ripard 					    unsigned long pixel_clock_hz,
5474fcd2385SMaxime Ripard 					    unsigned int hdisplay,
5484fcd2385SMaxime Ripard 					    unsigned int vdisplay,
5494fcd2385SMaxime Ripard 					    bool interlace)
5504fcd2385SMaxime Ripard {
5514fcd2385SMaxime Ripard 	struct drm_display_mode *mode;
5524fcd2385SMaxime Ripard 	enum drm_mode_analog analog;
5534fcd2385SMaxime Ripard 	int ret;
5544fcd2385SMaxime Ripard 
5554fcd2385SMaxime Ripard 	switch (tv_mode) {
5564fcd2385SMaxime Ripard 	case DRM_MODE_TV_MODE_NTSC:
5574fcd2385SMaxime Ripard 		fallthrough;
5584fcd2385SMaxime Ripard 	case DRM_MODE_TV_MODE_NTSC_443:
5594fcd2385SMaxime Ripard 		fallthrough;
5604fcd2385SMaxime Ripard 	case DRM_MODE_TV_MODE_NTSC_J:
5614fcd2385SMaxime Ripard 		fallthrough;
5624fcd2385SMaxime Ripard 	case DRM_MODE_TV_MODE_PAL_M:
5634fcd2385SMaxime Ripard 		analog = DRM_MODE_ANALOG_NTSC;
5644fcd2385SMaxime Ripard 		break;
5654fcd2385SMaxime Ripard 
5664fcd2385SMaxime Ripard 	case DRM_MODE_TV_MODE_PAL:
5674fcd2385SMaxime Ripard 		fallthrough;
5684fcd2385SMaxime Ripard 	case DRM_MODE_TV_MODE_PAL_N:
5694fcd2385SMaxime Ripard 		fallthrough;
5704fcd2385SMaxime Ripard 	case DRM_MODE_TV_MODE_SECAM:
5714fcd2385SMaxime Ripard 		analog = DRM_MODE_ANALOG_PAL;
5724fcd2385SMaxime Ripard 		break;
5734fcd2385SMaxime Ripard 
5744fcd2385SMaxime Ripard 	default:
5754fcd2385SMaxime Ripard 		return NULL;
5764fcd2385SMaxime Ripard 	}
5774fcd2385SMaxime Ripard 
5784fcd2385SMaxime Ripard 	mode = drm_mode_create(dev);
5794fcd2385SMaxime Ripard 	if (!mode)
5804fcd2385SMaxime Ripard 		return NULL;
5814fcd2385SMaxime Ripard 
5824fcd2385SMaxime Ripard 	ret = fill_analog_mode(dev, mode,
5834fcd2385SMaxime Ripard 			       &tv_modes_parameters[analog],
5844fcd2385SMaxime Ripard 			       pixel_clock_hz, hdisplay, vdisplay, interlace);
5854fcd2385SMaxime Ripard 	if (ret)
5864fcd2385SMaxime Ripard 		goto err_free_mode;
5874fcd2385SMaxime Ripard 
5884fcd2385SMaxime Ripard 	return mode;
5894fcd2385SMaxime Ripard 
5904fcd2385SMaxime Ripard err_free_mode:
5914fcd2385SMaxime Ripard 	drm_mode_destroy(dev, mode);
5924fcd2385SMaxime Ripard 	return NULL;
5934fcd2385SMaxime Ripard }
5944fcd2385SMaxime Ripard EXPORT_SYMBOL(drm_analog_tv_mode);
5954fcd2385SMaxime Ripard 
5968bd441b2SDaniel Vetter /**
597f5aabb97SDaniel Vetter  * drm_cvt_mode -create a modeline based on the CVT algorithm
598f5aabb97SDaniel Vetter  * @dev: drm device
599d782c3f9SZhao Yakui  * @hdisplay: hdisplay size
600d782c3f9SZhao Yakui  * @vdisplay: vdisplay size
601d782c3f9SZhao Yakui  * @vrefresh: vrefresh rate
602f5aabb97SDaniel Vetter  * @reduced: whether to use reduced blanking
603f5aabb97SDaniel Vetter  * @interlaced: whether to compute an interlaced mode
604f5aabb97SDaniel Vetter  * @margins: whether to add margins (borders)
605d782c3f9SZhao Yakui  *
606d782c3f9SZhao Yakui  * This function is called to generate the modeline based on CVT algorithm
607d782c3f9SZhao Yakui  * according to the hdisplay, vdisplay, vrefresh.
608d782c3f9SZhao Yakui  * It is based from the VESA(TM) Coordinated Video Timing Generator by
609d782c3f9SZhao Yakui  * Graham Loveridge April 9, 2003 available at
610631dd1a8SJustin P. Mattock  * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
611d782c3f9SZhao Yakui  *
612d782c3f9SZhao Yakui  * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
613d782c3f9SZhao Yakui  * What I have done is to translate it by using integer calculation.
614f5aabb97SDaniel Vetter  *
615f5aabb97SDaniel Vetter  * Returns:
616f5aabb97SDaniel Vetter  * The modeline based on the CVT algorithm stored in a drm_display_mode object.
617f5aabb97SDaniel Vetter  * The display mode object is allocated with drm_mode_create(). Returns NULL
618f5aabb97SDaniel Vetter  * when no mode could be allocated.
619d782c3f9SZhao Yakui  */
drm_cvt_mode(struct drm_device * dev,int hdisplay,int vdisplay,int vrefresh,bool reduced,bool interlaced,bool margins)620d782c3f9SZhao Yakui struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
621d782c3f9SZhao Yakui 				      int vdisplay, int vrefresh,
622d50ba256SDave Airlie 				      bool reduced, bool interlaced, bool margins)
623d782c3f9SZhao Yakui {
6243ec0db81SDaniel Vetter #define HV_FACTOR			1000
625d782c3f9SZhao Yakui 	/* 1) top/bottom margin size (% of height) - default: 1.8, */
626d782c3f9SZhao Yakui #define	CVT_MARGIN_PERCENTAGE		18
627d782c3f9SZhao Yakui 	/* 2) character cell horizontal granularity (pixels) - default 8 */
628d782c3f9SZhao Yakui #define	CVT_H_GRANULARITY		8
629d782c3f9SZhao Yakui 	/* 3) Minimum vertical porch (lines) - default 3 */
630d782c3f9SZhao Yakui #define	CVT_MIN_V_PORCH			3
631d782c3f9SZhao Yakui 	/* 4) Minimum number of vertical back porch lines - default 6 */
632d782c3f9SZhao Yakui #define	CVT_MIN_V_BPORCH		6
633d782c3f9SZhao Yakui 	/* Pixel Clock step (kHz) */
634d782c3f9SZhao Yakui #define CVT_CLOCK_STEP			250
635d782c3f9SZhao Yakui 	struct drm_display_mode *drm_mode;
636d782c3f9SZhao Yakui 	unsigned int vfieldrate, hperiod;
637d782c3f9SZhao Yakui 	int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
638d782c3f9SZhao Yakui 	int interlace;
6398a5bbf32SChris Wilson 	u64 tmp;
640d782c3f9SZhao Yakui 
6417f3bbc0bSDmitry Osipenko 	if (!hdisplay || !vdisplay)
6427f3bbc0bSDmitry Osipenko 		return NULL;
6437f3bbc0bSDmitry Osipenko 
644d782c3f9SZhao Yakui 	/* allocate the drm_display_mode structure. If failure, we will
645d782c3f9SZhao Yakui 	 * return directly
646d782c3f9SZhao Yakui 	 */
647d782c3f9SZhao Yakui 	drm_mode = drm_mode_create(dev);
648d782c3f9SZhao Yakui 	if (!drm_mode)
649d782c3f9SZhao Yakui 		return NULL;
650d782c3f9SZhao Yakui 
651d782c3f9SZhao Yakui 	/* the CVT default refresh rate is 60Hz */
652d782c3f9SZhao Yakui 	if (!vrefresh)
653d782c3f9SZhao Yakui 		vrefresh = 60;
654d782c3f9SZhao Yakui 
655d782c3f9SZhao Yakui 	/* the required field fresh rate */
656d782c3f9SZhao Yakui 	if (interlaced)
657d782c3f9SZhao Yakui 		vfieldrate = vrefresh * 2;
658d782c3f9SZhao Yakui 	else
659d782c3f9SZhao Yakui 		vfieldrate = vrefresh;
660d782c3f9SZhao Yakui 
661d782c3f9SZhao Yakui 	/* horizontal pixels */
662d782c3f9SZhao Yakui 	hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
663d782c3f9SZhao Yakui 
664d782c3f9SZhao Yakui 	/* determine the left&right borders */
665d782c3f9SZhao Yakui 	hmargin = 0;
666d782c3f9SZhao Yakui 	if (margins) {
667d782c3f9SZhao Yakui 		hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
668d782c3f9SZhao Yakui 		hmargin -= hmargin % CVT_H_GRANULARITY;
669d782c3f9SZhao Yakui 	}
670d782c3f9SZhao Yakui 	/* find the total active pixels */
671d782c3f9SZhao Yakui 	drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
672d782c3f9SZhao Yakui 
673d782c3f9SZhao Yakui 	/* find the number of lines per field */
674d782c3f9SZhao Yakui 	if (interlaced)
675d782c3f9SZhao Yakui 		vdisplay_rnd = vdisplay / 2;
676d782c3f9SZhao Yakui 	else
677d782c3f9SZhao Yakui 		vdisplay_rnd = vdisplay;
678d782c3f9SZhao Yakui 
679d782c3f9SZhao Yakui 	/* find the top & bottom borders */
680d782c3f9SZhao Yakui 	vmargin = 0;
681d782c3f9SZhao Yakui 	if (margins)
682d782c3f9SZhao Yakui 		vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
683d782c3f9SZhao Yakui 
684841b4117SFrancisco Jerez 	drm_mode->vdisplay = vdisplay + 2 * vmargin;
685d782c3f9SZhao Yakui 
686d782c3f9SZhao Yakui 	/* Interlaced */
687d782c3f9SZhao Yakui 	if (interlaced)
688d782c3f9SZhao Yakui 		interlace = 1;
689d782c3f9SZhao Yakui 	else
690d782c3f9SZhao Yakui 		interlace = 0;
691d782c3f9SZhao Yakui 
692d782c3f9SZhao Yakui 	/* Determine VSync Width from aspect ratio */
693d782c3f9SZhao Yakui 	if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
694d782c3f9SZhao Yakui 		vsync = 4;
695d782c3f9SZhao Yakui 	else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
696d782c3f9SZhao Yakui 		vsync = 5;
697d782c3f9SZhao Yakui 	else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
698d782c3f9SZhao Yakui 		vsync = 6;
699d782c3f9SZhao Yakui 	else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
700d782c3f9SZhao Yakui 		vsync = 7;
701d782c3f9SZhao Yakui 	else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
702d782c3f9SZhao Yakui 		vsync = 7;
703d782c3f9SZhao Yakui 	else /* custom */
704d782c3f9SZhao Yakui 		vsync = 10;
705d782c3f9SZhao Yakui 
706d782c3f9SZhao Yakui 	if (!reduced) {
707d782c3f9SZhao Yakui 		/* simplify the GTF calculation */
708d782c3f9SZhao Yakui 		/* 4) Minimum time of vertical sync + back porch interval (µs)
709d782c3f9SZhao Yakui 		 * default 550.0
710d782c3f9SZhao Yakui 		 */
711d782c3f9SZhao Yakui 		int tmp1, tmp2;
712d782c3f9SZhao Yakui #define CVT_MIN_VSYNC_BP	550
713d782c3f9SZhao Yakui 		/* 3) Nominal HSync width (% of line period) - default 8 */
714d782c3f9SZhao Yakui #define CVT_HSYNC_PERCENTAGE	8
715d782c3f9SZhao Yakui 		unsigned int hblank_percentage;
716f199af00SBenjamin Gaignard 		int vsyncandback_porch, __maybe_unused vback_porch, hblank;
717d782c3f9SZhao Yakui 
718d782c3f9SZhao Yakui 		/* estimated the horizontal period */
719d782c3f9SZhao Yakui 		tmp1 = HV_FACTOR * 1000000  -
720d782c3f9SZhao Yakui 				CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
721d782c3f9SZhao Yakui 		tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
722d782c3f9SZhao Yakui 				interlace;
723d782c3f9SZhao Yakui 		hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
724d782c3f9SZhao Yakui 
725d782c3f9SZhao Yakui 		tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
726d782c3f9SZhao Yakui 		/* 9. Find number of lines in sync + backporch */
727d782c3f9SZhao Yakui 		if (tmp1 < (vsync + CVT_MIN_V_PORCH))
728d782c3f9SZhao Yakui 			vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
729d782c3f9SZhao Yakui 		else
730d782c3f9SZhao Yakui 			vsyncandback_porch = tmp1;
731d782c3f9SZhao Yakui 		/* 10. Find number of lines in back porch */
732d782c3f9SZhao Yakui 		vback_porch = vsyncandback_porch - vsync;
733d782c3f9SZhao Yakui 		drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
734d782c3f9SZhao Yakui 				vsyncandback_porch + CVT_MIN_V_PORCH;
735d782c3f9SZhao Yakui 		/* 5) Definition of Horizontal blanking time limitation */
736d782c3f9SZhao Yakui 		/* Gradient (%/kHz) - default 600 */
737d782c3f9SZhao Yakui #define CVT_M_FACTOR	600
738d782c3f9SZhao Yakui 		/* Offset (%) - default 40 */
739d782c3f9SZhao Yakui #define CVT_C_FACTOR	40
740d782c3f9SZhao Yakui 		/* Blanking time scaling factor - default 128 */
741d782c3f9SZhao Yakui #define CVT_K_FACTOR	128
742d782c3f9SZhao Yakui 		/* Scaling factor weighting - default 20 */
743d782c3f9SZhao Yakui #define CVT_J_FACTOR	20
744d782c3f9SZhao Yakui #define CVT_M_PRIME	(CVT_M_FACTOR * CVT_K_FACTOR / 256)
745d782c3f9SZhao Yakui #define CVT_C_PRIME	((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
746d782c3f9SZhao Yakui 			 CVT_J_FACTOR)
747d782c3f9SZhao Yakui 		/* 12. Find ideal blanking duty cycle from formula */
748d782c3f9SZhao Yakui 		hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
749d782c3f9SZhao Yakui 					hperiod / 1000;
750d782c3f9SZhao Yakui 		/* 13. Blanking time */
751d782c3f9SZhao Yakui 		if (hblank_percentage < 20 * HV_FACTOR)
752d782c3f9SZhao Yakui 			hblank_percentage = 20 * HV_FACTOR;
753d782c3f9SZhao Yakui 		hblank = drm_mode->hdisplay * hblank_percentage /
754d782c3f9SZhao Yakui 			 (100 * HV_FACTOR - hblank_percentage);
755d782c3f9SZhao Yakui 		hblank -= hblank % (2 * CVT_H_GRANULARITY);
7562a97acd6SYannick Guerrini 		/* 14. find the total pixels per line */
757d782c3f9SZhao Yakui 		drm_mode->htotal = drm_mode->hdisplay + hblank;
758d782c3f9SZhao Yakui 		drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
759d782c3f9SZhao Yakui 		drm_mode->hsync_start = drm_mode->hsync_end -
760d782c3f9SZhao Yakui 			(drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
761d782c3f9SZhao Yakui 		drm_mode->hsync_start += CVT_H_GRANULARITY -
762d782c3f9SZhao Yakui 			drm_mode->hsync_start % CVT_H_GRANULARITY;
763d782c3f9SZhao Yakui 		/* fill the Vsync values */
764d782c3f9SZhao Yakui 		drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
765d782c3f9SZhao Yakui 		drm_mode->vsync_end = drm_mode->vsync_start + vsync;
766d782c3f9SZhao Yakui 	} else {
767d782c3f9SZhao Yakui 		/* Reduced blanking */
768d782c3f9SZhao Yakui 		/* Minimum vertical blanking interval time (µs)- default 460 */
769d782c3f9SZhao Yakui #define CVT_RB_MIN_VBLANK	460
770d782c3f9SZhao Yakui 		/* Fixed number of clocks for horizontal sync */
771d782c3f9SZhao Yakui #define CVT_RB_H_SYNC		32
772d782c3f9SZhao Yakui 		/* Fixed number of clocks for horizontal blanking */
773d782c3f9SZhao Yakui #define CVT_RB_H_BLANK		160
774d782c3f9SZhao Yakui 		/* Fixed number of lines for vertical front porch - default 3*/
775d782c3f9SZhao Yakui #define CVT_RB_VFPORCH		3
776d782c3f9SZhao Yakui 		int vbilines;
777d782c3f9SZhao Yakui 		int tmp1, tmp2;
778d782c3f9SZhao Yakui 		/* 8. Estimate Horizontal period. */
779d782c3f9SZhao Yakui 		tmp1 = HV_FACTOR * 1000000 -
780d782c3f9SZhao Yakui 			CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
781d782c3f9SZhao Yakui 		tmp2 = vdisplay_rnd + 2 * vmargin;
782d782c3f9SZhao Yakui 		hperiod = tmp1 / (tmp2 * vfieldrate);
783d782c3f9SZhao Yakui 		/* 9. Find number of lines in vertical blanking */
784d782c3f9SZhao Yakui 		vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
785d782c3f9SZhao Yakui 		/* 10. Check if vertical blanking is sufficient */
786d782c3f9SZhao Yakui 		if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
787d782c3f9SZhao Yakui 			vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
788d782c3f9SZhao Yakui 		/* 11. Find total number of lines in vertical field */
789d782c3f9SZhao Yakui 		drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
790d782c3f9SZhao Yakui 		/* 12. Find total number of pixels in a line */
791d782c3f9SZhao Yakui 		drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
792d782c3f9SZhao Yakui 		/* Fill in HSync values */
793d782c3f9SZhao Yakui 		drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
794adde0f23SAdam Jackson 		drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
795adde0f23SAdam Jackson 		/* Fill in VSync values */
796adde0f23SAdam Jackson 		drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
797adde0f23SAdam Jackson 		drm_mode->vsync_end = drm_mode->vsync_start + vsync;
798d782c3f9SZhao Yakui 	}
799d782c3f9SZhao Yakui 	/* 15/13. Find pixel clock frequency (kHz for xf86) */
8008a5bbf32SChris Wilson 	tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
8018a5bbf32SChris Wilson 	tmp *= HV_FACTOR * 1000;
8028a5bbf32SChris Wilson 	do_div(tmp, hperiod);
8038a5bbf32SChris Wilson 	tmp -= drm_mode->clock % CVT_CLOCK_STEP;
8048a5bbf32SChris Wilson 	drm_mode->clock = tmp;
805d782c3f9SZhao Yakui 	/* 18/16. Find actual vertical frame frequency */
806d782c3f9SZhao Yakui 	/* ignore - just set the mode flag for interlaced */
807171fdd89SAdam Jackson 	if (interlaced) {
808d782c3f9SZhao Yakui 		drm_mode->vtotal *= 2;
809171fdd89SAdam Jackson 		drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
810171fdd89SAdam Jackson 	}
811d782c3f9SZhao Yakui 	/* Fill the mode line name */
812d782c3f9SZhao Yakui 	drm_mode_set_name(drm_mode);
813d782c3f9SZhao Yakui 	if (reduced)
814d782c3f9SZhao Yakui 		drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
815d782c3f9SZhao Yakui 					DRM_MODE_FLAG_NVSYNC);
816d782c3f9SZhao Yakui 	else
817d782c3f9SZhao Yakui 		drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
818d782c3f9SZhao Yakui 					DRM_MODE_FLAG_NHSYNC);
819d782c3f9SZhao Yakui 
820d782c3f9SZhao Yakui 	return drm_mode;
821d782c3f9SZhao Yakui }
822d782c3f9SZhao Yakui EXPORT_SYMBOL(drm_cvt_mode);
823d782c3f9SZhao Yakui 
824d782c3f9SZhao Yakui /**
825f5aabb97SDaniel Vetter  * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
82626bbdadaSZhao Yakui  * @dev: drm device
82726bbdadaSZhao Yakui  * @hdisplay: hdisplay size
82826bbdadaSZhao Yakui  * @vdisplay: vdisplay size
82926bbdadaSZhao Yakui  * @vrefresh: vrefresh rate.
830f5aabb97SDaniel Vetter  * @interlaced: whether to compute an interlaced mode
831f5aabb97SDaniel Vetter  * @margins: desired margin (borders) size
8323ec0db81SDaniel Vetter  * @GTF_M: extended GTF formula parameters
8333ec0db81SDaniel Vetter  * @GTF_2C: extended GTF formula parameters
8343ec0db81SDaniel Vetter  * @GTF_K: extended GTF formula parameters
8353ec0db81SDaniel Vetter  * @GTF_2J: extended GTF formula parameters
83626bbdadaSZhao Yakui  *
8377a374350SAdam Jackson  * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
8387a374350SAdam Jackson  * in here multiplied by two.  For a C of 40, pass in 80.
839f5aabb97SDaniel Vetter  *
840f5aabb97SDaniel Vetter  * Returns:
841f5aabb97SDaniel Vetter  * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
842f5aabb97SDaniel Vetter  * The display mode object is allocated with drm_mode_create(). Returns NULL
843f5aabb97SDaniel Vetter  * when no mode could be allocated.
84426bbdadaSZhao Yakui  */
8457a374350SAdam Jackson 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)8467a374350SAdam Jackson drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
8477a374350SAdam Jackson 		     int vrefresh, bool interlaced, int margins,
8487a374350SAdam Jackson 		     int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
8497a374350SAdam Jackson {	/* 1) top/bottom margin size (% of height) - default: 1.8, */
85026bbdadaSZhao Yakui #define	GTF_MARGIN_PERCENTAGE		18
85126bbdadaSZhao Yakui 	/* 2) character cell horizontal granularity (pixels) - default 8 */
85226bbdadaSZhao Yakui #define	GTF_CELL_GRAN			8
85326bbdadaSZhao Yakui 	/* 3) Minimum vertical porch (lines) - default 3 */
85426bbdadaSZhao Yakui #define	GTF_MIN_V_PORCH			1
85526bbdadaSZhao Yakui 	/* width of vsync in lines */
85626bbdadaSZhao Yakui #define V_SYNC_RQD			3
85726bbdadaSZhao Yakui 	/* width of hsync as % of total line */
85826bbdadaSZhao Yakui #define H_SYNC_PERCENT			8
85926bbdadaSZhao Yakui 	/* min time of vsync + back porch (microsec) */
86026bbdadaSZhao Yakui #define MIN_VSYNC_PLUS_BP		550
86126bbdadaSZhao Yakui 	/* C' and M' are part of the Blanking Duty Cycle computation */
8627a374350SAdam Jackson #define GTF_C_PRIME	((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
86326bbdadaSZhao Yakui #define GTF_M_PRIME	(GTF_K * GTF_M / 256)
86426bbdadaSZhao Yakui 	struct drm_display_mode *drm_mode;
86526bbdadaSZhao Yakui 	unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
86626bbdadaSZhao Yakui 	int top_margin, bottom_margin;
86726bbdadaSZhao Yakui 	int interlace;
86826bbdadaSZhao Yakui 	unsigned int hfreq_est;
869f199af00SBenjamin Gaignard 	int vsync_plus_bp, __maybe_unused vback_porch;
870f199af00SBenjamin Gaignard 	unsigned int vtotal_lines, __maybe_unused vfieldrate_est;
871f199af00SBenjamin Gaignard 	unsigned int __maybe_unused hperiod;
872f199af00SBenjamin Gaignard 	unsigned int vfield_rate, __maybe_unused vframe_rate;
87326bbdadaSZhao Yakui 	int left_margin, right_margin;
87426bbdadaSZhao Yakui 	unsigned int total_active_pixels, ideal_duty_cycle;
87526bbdadaSZhao Yakui 	unsigned int hblank, total_pixels, pixel_freq;
87626bbdadaSZhao Yakui 	int hsync, hfront_porch, vodd_front_porch_lines;
87726bbdadaSZhao Yakui 	unsigned int tmp1, tmp2;
87826bbdadaSZhao Yakui 
8797f3bbc0bSDmitry Osipenko 	if (!hdisplay || !vdisplay)
8807f3bbc0bSDmitry Osipenko 		return NULL;
8817f3bbc0bSDmitry Osipenko 
88226bbdadaSZhao Yakui 	drm_mode = drm_mode_create(dev);
88326bbdadaSZhao Yakui 	if (!drm_mode)
88426bbdadaSZhao Yakui 		return NULL;
88526bbdadaSZhao Yakui 
88626bbdadaSZhao Yakui 	/* 1. In order to give correct results, the number of horizontal
88726bbdadaSZhao Yakui 	 * pixels requested is first processed to ensure that it is divisible
88826bbdadaSZhao Yakui 	 * by the character size, by rounding it to the nearest character
88926bbdadaSZhao Yakui 	 * cell boundary:
89026bbdadaSZhao Yakui 	 */
89126bbdadaSZhao Yakui 	hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
89226bbdadaSZhao Yakui 	hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
89326bbdadaSZhao Yakui 
89426bbdadaSZhao Yakui 	/* 2. If interlace is requested, the number of vertical lines assumed
89526bbdadaSZhao Yakui 	 * by the calculation must be halved, as the computation calculates
89626bbdadaSZhao Yakui 	 * the number of vertical lines per field.
89726bbdadaSZhao Yakui 	 */
89826bbdadaSZhao Yakui 	if (interlaced)
89926bbdadaSZhao Yakui 		vdisplay_rnd = vdisplay / 2;
90026bbdadaSZhao Yakui 	else
90126bbdadaSZhao Yakui 		vdisplay_rnd = vdisplay;
90226bbdadaSZhao Yakui 
90326bbdadaSZhao Yakui 	/* 3. Find the frame rate required: */
90426bbdadaSZhao Yakui 	if (interlaced)
90526bbdadaSZhao Yakui 		vfieldrate_rqd = vrefresh * 2;
90626bbdadaSZhao Yakui 	else
90726bbdadaSZhao Yakui 		vfieldrate_rqd = vrefresh;
90826bbdadaSZhao Yakui 
90926bbdadaSZhao Yakui 	/* 4. Find number of lines in Top margin: */
91026bbdadaSZhao Yakui 	top_margin = 0;
91126bbdadaSZhao Yakui 	if (margins)
91226bbdadaSZhao Yakui 		top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
91326bbdadaSZhao Yakui 				1000;
91426bbdadaSZhao Yakui 	/* 5. Find number of lines in bottom margin: */
91526bbdadaSZhao Yakui 	bottom_margin = top_margin;
91626bbdadaSZhao Yakui 
91726bbdadaSZhao Yakui 	/* 6. If interlace is required, then set variable interlace: */
91826bbdadaSZhao Yakui 	if (interlaced)
91926bbdadaSZhao Yakui 		interlace = 1;
92026bbdadaSZhao Yakui 	else
92126bbdadaSZhao Yakui 		interlace = 0;
92226bbdadaSZhao Yakui 
92326bbdadaSZhao Yakui 	/* 7. Estimate the Horizontal frequency */
92426bbdadaSZhao Yakui 	{
92526bbdadaSZhao Yakui 		tmp1 = (1000000  - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
92626bbdadaSZhao Yakui 		tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
92726bbdadaSZhao Yakui 				2 + interlace;
92826bbdadaSZhao Yakui 		hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
92926bbdadaSZhao Yakui 	}
93026bbdadaSZhao Yakui 
93126bbdadaSZhao Yakui 	/* 8. Find the number of lines in V sync + back porch */
93226bbdadaSZhao Yakui 	/* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
93326bbdadaSZhao Yakui 	vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
93426bbdadaSZhao Yakui 	vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
93526bbdadaSZhao Yakui 	/*  9. Find the number of lines in V back porch alone: */
93626bbdadaSZhao Yakui 	vback_porch = vsync_plus_bp - V_SYNC_RQD;
93726bbdadaSZhao Yakui 	/*  10. Find the total number of lines in Vertical field period: */
93826bbdadaSZhao Yakui 	vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
93926bbdadaSZhao Yakui 			vsync_plus_bp + GTF_MIN_V_PORCH;
94026bbdadaSZhao Yakui 	/*  11. Estimate the Vertical field frequency: */
94126bbdadaSZhao Yakui 	vfieldrate_est = hfreq_est / vtotal_lines;
94226bbdadaSZhao Yakui 	/*  12. Find the actual horizontal period: */
94326bbdadaSZhao Yakui 	hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
94426bbdadaSZhao Yakui 
94526bbdadaSZhao Yakui 	/*  13. Find the actual Vertical field frequency: */
94626bbdadaSZhao Yakui 	vfield_rate = hfreq_est / vtotal_lines;
94726bbdadaSZhao Yakui 	/*  14. Find the Vertical frame frequency: */
94826bbdadaSZhao Yakui 	if (interlaced)
94926bbdadaSZhao Yakui 		vframe_rate = vfield_rate / 2;
95026bbdadaSZhao Yakui 	else
95126bbdadaSZhao Yakui 		vframe_rate = vfield_rate;
95226bbdadaSZhao Yakui 	/*  15. Find number of pixels in left margin: */
95326bbdadaSZhao Yakui 	if (margins)
95426bbdadaSZhao Yakui 		left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
95526bbdadaSZhao Yakui 				1000;
95626bbdadaSZhao Yakui 	else
95726bbdadaSZhao Yakui 		left_margin = 0;
95826bbdadaSZhao Yakui 
95926bbdadaSZhao Yakui 	/* 16.Find number of pixels in right margin: */
96026bbdadaSZhao Yakui 	right_margin = left_margin;
96126bbdadaSZhao Yakui 	/* 17.Find total number of active pixels in image and left and right */
96226bbdadaSZhao Yakui 	total_active_pixels = hdisplay_rnd + left_margin + right_margin;
96326bbdadaSZhao Yakui 	/* 18.Find the ideal blanking duty cycle from blanking duty cycle */
96426bbdadaSZhao Yakui 	ideal_duty_cycle = GTF_C_PRIME * 1000 -
96526bbdadaSZhao Yakui 				(GTF_M_PRIME * 1000000 / hfreq_est);
96626bbdadaSZhao Yakui 	/* 19.Find the number of pixels in the blanking time to the nearest
96726bbdadaSZhao Yakui 	 * double character cell: */
96826bbdadaSZhao Yakui 	hblank = total_active_pixels * ideal_duty_cycle /
96926bbdadaSZhao Yakui 			(100000 - ideal_duty_cycle);
97026bbdadaSZhao Yakui 	hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
97126bbdadaSZhao Yakui 	hblank = hblank * 2 * GTF_CELL_GRAN;
97226bbdadaSZhao Yakui 	/* 20.Find total number of pixels: */
97326bbdadaSZhao Yakui 	total_pixels = total_active_pixels + hblank;
97426bbdadaSZhao Yakui 	/* 21.Find pixel clock frequency: */
97526bbdadaSZhao Yakui 	pixel_freq = total_pixels * hfreq_est / 1000;
97626bbdadaSZhao Yakui 	/* Stage 1 computations are now complete; I should really pass
97726bbdadaSZhao Yakui 	 * the results to another function and do the Stage 2 computations,
97826bbdadaSZhao Yakui 	 * but I only need a few more values so I'll just append the
97926bbdadaSZhao Yakui 	 * computations here for now */
98026bbdadaSZhao Yakui 	/* 17. Find the number of pixels in the horizontal sync period: */
98126bbdadaSZhao Yakui 	hsync = H_SYNC_PERCENT * total_pixels / 100;
98226bbdadaSZhao Yakui 	hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
98326bbdadaSZhao Yakui 	hsync = hsync * GTF_CELL_GRAN;
98426bbdadaSZhao Yakui 	/* 18. Find the number of pixels in horizontal front porch period */
98526bbdadaSZhao Yakui 	hfront_porch = hblank / 2 - hsync;
98626bbdadaSZhao Yakui 	/*  36. Find the number of lines in the odd front porch period: */
98726bbdadaSZhao Yakui 	vodd_front_porch_lines = GTF_MIN_V_PORCH ;
98826bbdadaSZhao Yakui 
98926bbdadaSZhao Yakui 	/* finally, pack the results in the mode struct */
99026bbdadaSZhao Yakui 	drm_mode->hdisplay = hdisplay_rnd;
99126bbdadaSZhao Yakui 	drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
99226bbdadaSZhao Yakui 	drm_mode->hsync_end = drm_mode->hsync_start + hsync;
99326bbdadaSZhao Yakui 	drm_mode->htotal = total_pixels;
99426bbdadaSZhao Yakui 	drm_mode->vdisplay = vdisplay_rnd;
99526bbdadaSZhao Yakui 	drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
99626bbdadaSZhao Yakui 	drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
99726bbdadaSZhao Yakui 	drm_mode->vtotal = vtotal_lines;
99826bbdadaSZhao Yakui 
99926bbdadaSZhao Yakui 	drm_mode->clock = pixel_freq;
100026bbdadaSZhao Yakui 
100126bbdadaSZhao Yakui 	if (interlaced) {
100226bbdadaSZhao Yakui 		drm_mode->vtotal *= 2;
100326bbdadaSZhao Yakui 		drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
100426bbdadaSZhao Yakui 	}
100526bbdadaSZhao Yakui 
1006171fdd89SAdam Jackson 	drm_mode_set_name(drm_mode);
1007c385e50cSAdam Jackson 	if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
1008171fdd89SAdam Jackson 		drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
1009c385e50cSAdam Jackson 	else
1010c385e50cSAdam Jackson 		drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
1011171fdd89SAdam Jackson 
101226bbdadaSZhao Yakui 	return drm_mode;
101326bbdadaSZhao Yakui }
10147a374350SAdam Jackson EXPORT_SYMBOL(drm_gtf_mode_complex);
10157a374350SAdam Jackson 
10167a374350SAdam Jackson /**
1017f5aabb97SDaniel Vetter  * drm_gtf_mode - create the modeline based on the GTF algorithm
10187a374350SAdam Jackson  * @dev: drm device
10197a374350SAdam Jackson  * @hdisplay: hdisplay size
10207a374350SAdam Jackson  * @vdisplay: vdisplay size
10217a374350SAdam Jackson  * @vrefresh: vrefresh rate.
1022f5aabb97SDaniel Vetter  * @interlaced: whether to compute an interlaced mode
1023f5aabb97SDaniel Vetter  * @margins: desired margin (borders) size
10247a374350SAdam Jackson  *
10257a374350SAdam Jackson  * return the modeline based on GTF algorithm
10267a374350SAdam Jackson  *
10277a374350SAdam Jackson  * This function is to create the modeline based on the GTF algorithm.
10287a374350SAdam Jackson  * Generalized Timing Formula is derived from:
10292e7a5701SDaniel Vetter  *
10307a374350SAdam Jackson  *	GTF Spreadsheet by Andy Morrish (1/5/97)
1031b0487e0dSAlexander A. Klimov  *	available at https://www.vesa.org
10327a374350SAdam Jackson  *
10337a374350SAdam Jackson  * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
10347a374350SAdam Jackson  * What I have done is to translate it by using integer calculation.
10357a374350SAdam Jackson  * I also refer to the function of fb_get_mode in the file of
10367a374350SAdam Jackson  * drivers/video/fbmon.c
10377a374350SAdam Jackson  *
1038da5335b8SDaniel Vetter  * Standard GTF parameters::
1039da5335b8SDaniel Vetter  *
10407a374350SAdam Jackson  *     M = 600
10417a374350SAdam Jackson  *     C = 40
10427a374350SAdam Jackson  *     K = 128
10437a374350SAdam Jackson  *     J = 20
1044f5aabb97SDaniel Vetter  *
1045f5aabb97SDaniel Vetter  * Returns:
1046f5aabb97SDaniel Vetter  * The modeline based on the GTF algorithm stored in a drm_display_mode object.
1047f5aabb97SDaniel Vetter  * The display mode object is allocated with drm_mode_create(). Returns NULL
1048f5aabb97SDaniel Vetter  * when no mode could be allocated.
10497a374350SAdam Jackson  */
10507a374350SAdam Jackson struct drm_display_mode *
drm_gtf_mode(struct drm_device * dev,int hdisplay,int vdisplay,int vrefresh,bool interlaced,int margins)10517a374350SAdam Jackson drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
10523ec0db81SDaniel Vetter 	     bool interlaced, int margins)
10537a374350SAdam Jackson {
10543ec0db81SDaniel Vetter 	return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
10553ec0db81SDaniel Vetter 				    interlaced, margins,
10563ec0db81SDaniel Vetter 				    600, 40 * 2, 128, 20 * 2);
10577a374350SAdam Jackson }
105826bbdadaSZhao Yakui EXPORT_SYMBOL(drm_gtf_mode);
10597a374350SAdam Jackson 
1060a38884f6STomi Valkeinen #ifdef CONFIG_VIDEOMODE_HELPERS
1061f5aabb97SDaniel Vetter /**
1062f5aabb97SDaniel Vetter  * drm_display_mode_from_videomode - fill in @dmode using @vm,
1063f5aabb97SDaniel Vetter  * @vm: videomode structure to use as source
1064f5aabb97SDaniel Vetter  * @dmode: drm_display_mode structure to use as destination
1065f5aabb97SDaniel Vetter  *
1066f5aabb97SDaniel Vetter  * Fills out @dmode using the display mode specified in @vm.
1067f5aabb97SDaniel Vetter  */
drm_display_mode_from_videomode(const struct videomode * vm,struct drm_display_mode * dmode)1068ba0c2422SDaniel Vetter void drm_display_mode_from_videomode(const struct videomode *vm,
1069ebc64e45SSteffen Trumtrar 				     struct drm_display_mode *dmode)
1070ebc64e45SSteffen Trumtrar {
1071ebc64e45SSteffen Trumtrar 	dmode->hdisplay = vm->hactive;
1072ebc64e45SSteffen Trumtrar 	dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
1073ebc64e45SSteffen Trumtrar 	dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
1074ebc64e45SSteffen Trumtrar 	dmode->htotal = dmode->hsync_end + vm->hback_porch;
1075ebc64e45SSteffen Trumtrar 
1076ebc64e45SSteffen Trumtrar 	dmode->vdisplay = vm->vactive;
1077ebc64e45SSteffen Trumtrar 	dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
1078ebc64e45SSteffen Trumtrar 	dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
1079ebc64e45SSteffen Trumtrar 	dmode->vtotal = dmode->vsync_end + vm->vback_porch;
1080ebc64e45SSteffen Trumtrar 
1081ebc64e45SSteffen Trumtrar 	dmode->clock = vm->pixelclock / 1000;
1082ebc64e45SSteffen Trumtrar 
1083ebc64e45SSteffen Trumtrar 	dmode->flags = 0;
108406a33079STomi Valkeinen 	if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
1085ebc64e45SSteffen Trumtrar 		dmode->flags |= DRM_MODE_FLAG_PHSYNC;
108606a33079STomi Valkeinen 	else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
1087ebc64e45SSteffen Trumtrar 		dmode->flags |= DRM_MODE_FLAG_NHSYNC;
108806a33079STomi Valkeinen 	if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
1089ebc64e45SSteffen Trumtrar 		dmode->flags |= DRM_MODE_FLAG_PVSYNC;
109006a33079STomi Valkeinen 	else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
1091ebc64e45SSteffen Trumtrar 		dmode->flags |= DRM_MODE_FLAG_NVSYNC;
109206a33079STomi Valkeinen 	if (vm->flags & DISPLAY_FLAGS_INTERLACED)
1093ebc64e45SSteffen Trumtrar 		dmode->flags |= DRM_MODE_FLAG_INTERLACE;
109406a33079STomi Valkeinen 	if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
1095ebc64e45SSteffen Trumtrar 		dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
1096328a4719SSteffen Trumtrar 	if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
1097328a4719SSteffen Trumtrar 		dmode->flags |= DRM_MODE_FLAG_DBLCLK;
1098ebc64e45SSteffen Trumtrar 	drm_mode_set_name(dmode);
1099ebc64e45SSteffen Trumtrar }
1100ebc64e45SSteffen Trumtrar EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
1101ebc64e45SSteffen Trumtrar 
1102d490f455SSteve Longerbeam /**
1103d490f455SSteve Longerbeam  * drm_display_mode_to_videomode - fill in @vm using @dmode,
1104d490f455SSteve Longerbeam  * @dmode: drm_display_mode structure to use as source
1105d490f455SSteve Longerbeam  * @vm: videomode structure to use as destination
1106d490f455SSteve Longerbeam  *
1107d490f455SSteve Longerbeam  * Fills out @vm using the display mode specified in @dmode.
1108d490f455SSteve Longerbeam  */
drm_display_mode_to_videomode(const struct drm_display_mode * dmode,struct videomode * vm)1109d490f455SSteve Longerbeam void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
1110d490f455SSteve Longerbeam 				   struct videomode *vm)
1111d490f455SSteve Longerbeam {
1112d490f455SSteve Longerbeam 	vm->hactive = dmode->hdisplay;
1113d490f455SSteve Longerbeam 	vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
1114d490f455SSteve Longerbeam 	vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
1115d490f455SSteve Longerbeam 	vm->hback_porch = dmode->htotal - dmode->hsync_end;
1116d490f455SSteve Longerbeam 
1117d490f455SSteve Longerbeam 	vm->vactive = dmode->vdisplay;
1118d490f455SSteve Longerbeam 	vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
1119d490f455SSteve Longerbeam 	vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
1120d490f455SSteve Longerbeam 	vm->vback_porch = dmode->vtotal - dmode->vsync_end;
1121d490f455SSteve Longerbeam 
1122d490f455SSteve Longerbeam 	vm->pixelclock = dmode->clock * 1000;
1123d490f455SSteve Longerbeam 
1124d490f455SSteve Longerbeam 	vm->flags = 0;
1125d490f455SSteve Longerbeam 	if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
1126d490f455SSteve Longerbeam 		vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
1127d490f455SSteve Longerbeam 	else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
1128d490f455SSteve Longerbeam 		vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
1129d490f455SSteve Longerbeam 	if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
1130d490f455SSteve Longerbeam 		vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
1131d490f455SSteve Longerbeam 	else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
1132d490f455SSteve Longerbeam 		vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
1133d490f455SSteve Longerbeam 	if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
1134d490f455SSteve Longerbeam 		vm->flags |= DISPLAY_FLAGS_INTERLACED;
1135d490f455SSteve Longerbeam 	if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
1136d490f455SSteve Longerbeam 		vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
1137d490f455SSteve Longerbeam 	if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
1138d490f455SSteve Longerbeam 		vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
1139d490f455SSteve Longerbeam }
1140d490f455SSteve Longerbeam EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
1141d490f455SSteve Longerbeam 
1142cb34d7f2SLothar Waßmann /**
1143cb34d7f2SLothar Waßmann  * drm_bus_flags_from_videomode - extract information about pixelclk and
1144cb34d7f2SLothar Waßmann  * DE polarity from videomode and store it in a separate variable
1145cb34d7f2SLothar Waßmann  * @vm: videomode structure to use
1146955f60dbSPeter Ujfalusi  * @bus_flags: information about pixelclk, sync and DE polarity will be stored
1147955f60dbSPeter Ujfalusi  * here
1148cb34d7f2SLothar Waßmann  *
114988bc4178SLaurent Pinchart  * Sets DRM_BUS_FLAG_DE_(LOW|HIGH),  DRM_BUS_FLAG_PIXDATA_DRIVE_(POS|NEG)EDGE
115088bc4178SLaurent Pinchart  * and DISPLAY_FLAGS_SYNC_(POS|NEG)EDGE in @bus_flags according to DISPLAY_FLAGS
1151955f60dbSPeter Ujfalusi  * found in @vm
1152cb34d7f2SLothar Waßmann  */
drm_bus_flags_from_videomode(const struct videomode * vm,u32 * bus_flags)1153d72daa0dSLothar Waßmann void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
1154d72daa0dSLothar Waßmann {
1155d72daa0dSLothar Waßmann 	*bus_flags = 0;
1156d72daa0dSLothar Waßmann 	if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
115788bc4178SLaurent Pinchart 		*bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
1158d72daa0dSLothar Waßmann 	if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
115988bc4178SLaurent Pinchart 		*bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
1160d72daa0dSLothar Waßmann 
1161955f60dbSPeter Ujfalusi 	if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE)
116288bc4178SLaurent Pinchart 		*bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
1163955f60dbSPeter Ujfalusi 	if (vm->flags & DISPLAY_FLAGS_SYNC_NEGEDGE)
116488bc4178SLaurent Pinchart 		*bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
1165955f60dbSPeter Ujfalusi 
1166d72daa0dSLothar Waßmann 	if (vm->flags & DISPLAY_FLAGS_DE_LOW)
1167d72daa0dSLothar Waßmann 		*bus_flags |= DRM_BUS_FLAG_DE_LOW;
1168d72daa0dSLothar Waßmann 	if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
1169d72daa0dSLothar Waßmann 		*bus_flags |= DRM_BUS_FLAG_DE_HIGH;
1170d72daa0dSLothar Waßmann }
1171d72daa0dSLothar Waßmann EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
1172d72daa0dSLothar Waßmann 
1173a38884f6STomi Valkeinen #ifdef CONFIG_OF
1174edb37a95SSteffen Trumtrar /**
1175edb37a95SSteffen Trumtrar  * of_get_drm_display_mode - get a drm_display_mode from devicetree
1176edb37a95SSteffen Trumtrar  * @np: device_node with the timing specification
1177edb37a95SSteffen Trumtrar  * @dmode: will be set to the return value
1178955f60dbSPeter Ujfalusi  * @bus_flags: information about pixelclk, sync and DE polarity
1179edb37a95SSteffen Trumtrar  * @index: index into the list of display timings in devicetree
1180edb37a95SSteffen Trumtrar  *
1181edb37a95SSteffen Trumtrar  * This function is expensive and should only be used, if only one mode is to be
1182edb37a95SSteffen Trumtrar  * read from DT. To get multiple modes start with of_get_display_timings and
1183edb37a95SSteffen Trumtrar  * work with that instead.
1184f5aabb97SDaniel Vetter  *
1185f5aabb97SDaniel Vetter  * Returns:
1186f5aabb97SDaniel Vetter  * 0 on success, a negative errno code when no of videomode node was found.
1187edb37a95SSteffen Trumtrar  */
of_get_drm_display_mode(struct device_node * np,struct drm_display_mode * dmode,u32 * bus_flags,int index)1188edb37a95SSteffen Trumtrar int of_get_drm_display_mode(struct device_node *np,
1189fafc79efSLothar Waßmann 			    struct drm_display_mode *dmode, u32 *bus_flags,
1190fafc79efSLothar Waßmann 			    int index)
1191edb37a95SSteffen Trumtrar {
1192edb37a95SSteffen Trumtrar 	struct videomode vm;
1193edb37a95SSteffen Trumtrar 	int ret;
1194edb37a95SSteffen Trumtrar 
1195edb37a95SSteffen Trumtrar 	ret = of_get_videomode(np, &vm, index);
1196edb37a95SSteffen Trumtrar 	if (ret)
1197edb37a95SSteffen Trumtrar 		return ret;
1198edb37a95SSteffen Trumtrar 
1199edb37a95SSteffen Trumtrar 	drm_display_mode_from_videomode(&vm, dmode);
1200fafc79efSLothar Waßmann 	if (bus_flags)
1201fafc79efSLothar Waßmann 		drm_bus_flags_from_videomode(&vm, bus_flags);
1202edb37a95SSteffen Trumtrar 
1203f384d7d5SRob Herring 	pr_debug("%pOF: got %dx%d display mode\n",
1204f384d7d5SRob Herring 		np, vm.hactive, vm.vactive);
1205edb37a95SSteffen Trumtrar 	drm_mode_debug_printmodeline(dmode);
1206edb37a95SSteffen Trumtrar 
1207edb37a95SSteffen Trumtrar 	return 0;
1208edb37a95SSteffen Trumtrar }
1209edb37a95SSteffen Trumtrar EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
121095ae342dSNoralf Trønnes 
121195ae342dSNoralf Trønnes /**
121295ae342dSNoralf Trønnes  * of_get_drm_panel_display_mode - get a panel-timing drm_display_mode from devicetree
121395ae342dSNoralf Trønnes  * @np: device_node with the panel-timing specification
121495ae342dSNoralf Trønnes  * @dmode: will be set to the return value
121595ae342dSNoralf Trønnes  * @bus_flags: information about pixelclk, sync and DE polarity
121695ae342dSNoralf Trønnes  *
1217f470cfe4SMarek Vasut  * The mandatory Device Tree properties width-mm and height-mm
1218f470cfe4SMarek Vasut  * are read and set on the display mode.
121995ae342dSNoralf Trønnes  *
122095ae342dSNoralf Trønnes  * Returns:
122195ae342dSNoralf Trønnes  * Zero on success, negative error code on failure.
122295ae342dSNoralf Trønnes  */
of_get_drm_panel_display_mode(struct device_node * np,struct drm_display_mode * dmode,u32 * bus_flags)122395ae342dSNoralf Trønnes int of_get_drm_panel_display_mode(struct device_node *np,
122495ae342dSNoralf Trønnes 				  struct drm_display_mode *dmode, u32 *bus_flags)
122595ae342dSNoralf Trønnes {
122695ae342dSNoralf Trønnes 	u32 width_mm = 0, height_mm = 0;
122795ae342dSNoralf Trønnes 	struct display_timing timing;
122895ae342dSNoralf Trønnes 	struct videomode vm;
122995ae342dSNoralf Trønnes 	int ret;
123095ae342dSNoralf Trønnes 
123195ae342dSNoralf Trønnes 	ret = of_get_display_timing(np, "panel-timing", &timing);
123295ae342dSNoralf Trønnes 	if (ret)
123395ae342dSNoralf Trønnes 		return ret;
123495ae342dSNoralf Trønnes 
123595ae342dSNoralf Trønnes 	videomode_from_timing(&timing, &vm);
123695ae342dSNoralf Trønnes 
123795ae342dSNoralf Trønnes 	memset(dmode, 0, sizeof(*dmode));
123895ae342dSNoralf Trønnes 	drm_display_mode_from_videomode(&vm, dmode);
123995ae342dSNoralf Trønnes 	if (bus_flags)
124095ae342dSNoralf Trønnes 		drm_bus_flags_from_videomode(&vm, bus_flags);
124195ae342dSNoralf Trønnes 
124295ae342dSNoralf Trønnes 	ret = of_property_read_u32(np, "width-mm", &width_mm);
1243f470cfe4SMarek Vasut 	if (ret)
124495ae342dSNoralf Trønnes 		return ret;
124595ae342dSNoralf Trønnes 
124695ae342dSNoralf Trønnes 	ret = of_property_read_u32(np, "height-mm", &height_mm);
1247f470cfe4SMarek Vasut 	if (ret)
124895ae342dSNoralf Trønnes 		return ret;
124995ae342dSNoralf Trønnes 
125095ae342dSNoralf Trønnes 	dmode->width_mm = width_mm;
125195ae342dSNoralf Trønnes 	dmode->height_mm = height_mm;
125295ae342dSNoralf Trønnes 
125395ae342dSNoralf Trønnes 	drm_mode_debug_printmodeline(dmode);
125495ae342dSNoralf Trønnes 
125595ae342dSNoralf Trønnes 	return 0;
125695ae342dSNoralf Trønnes }
125795ae342dSNoralf Trønnes EXPORT_SYMBOL_GPL(of_get_drm_panel_display_mode);
1258a38884f6STomi Valkeinen #endif /* CONFIG_OF */
1259a38884f6STomi Valkeinen #endif /* CONFIG_VIDEOMODE_HELPERS */
1260edb37a95SSteffen Trumtrar 
126126bbdadaSZhao Yakui /**
1262f453ba04SDave Airlie  * drm_mode_set_name - set the name on a mode
1263f453ba04SDave Airlie  * @mode: name will be set in this mode
1264f453ba04SDave Airlie  *
1265f5aabb97SDaniel Vetter  * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
1266f5aabb97SDaniel Vetter  * with an optional 'i' suffix for interlaced modes.
1267f453ba04SDave Airlie  */
drm_mode_set_name(struct drm_display_mode * mode)1268f453ba04SDave Airlie void drm_mode_set_name(struct drm_display_mode *mode)
1269f453ba04SDave Airlie {
1270171fdd89SAdam Jackson 	bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
1271171fdd89SAdam Jackson 
1272171fdd89SAdam Jackson 	snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
1273171fdd89SAdam Jackson 		 mode->hdisplay, mode->vdisplay,
1274171fdd89SAdam Jackson 		 interlaced ? "i" : "");
1275f453ba04SDave Airlie }
1276f453ba04SDave Airlie EXPORT_SYMBOL(drm_mode_set_name);
1277f453ba04SDave Airlie 
127830ecad77SDaniel Vetter /**
1279f453ba04SDave Airlie  * drm_mode_vrefresh - get the vrefresh of a mode
1280f453ba04SDave Airlie  * @mode: mode
1281f453ba04SDave Airlie  *
1282f5aabb97SDaniel Vetter  * Returns:
1283f5aabb97SDaniel Vetter  * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
1284f5aabb97SDaniel Vetter  * value first if it is not yet set.
1285f453ba04SDave Airlie  */
drm_mode_vrefresh(const struct drm_display_mode * mode)1286b1f559ecSChris Wilson int drm_mode_vrefresh(const struct drm_display_mode *mode)
1287f453ba04SDave Airlie {
12882f0e9d80SVille Syrjälä 	unsigned int num, den;
12892f0e9d80SVille Syrjälä 
12904ed2101cSVille Syrjälä 	if (mode->htotal == 0 || mode->vtotal == 0)
12914ed2101cSVille Syrjälä 		return 0;
12924ed2101cSVille Syrjälä 
12935b34ab52SVille Syrjälä 	num = mode->clock;
12942f0e9d80SVille Syrjälä 	den = mode->htotal * mode->vtotal;
1295f453ba04SDave Airlie 
1296f453ba04SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12972f0e9d80SVille Syrjälä 		num *= 2;
1298f453ba04SDave Airlie 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
12992f0e9d80SVille Syrjälä 		den *= 2;
1300f453ba04SDave Airlie 	if (mode->vscan > 1)
13012f0e9d80SVille Syrjälä 		den *= mode->vscan;
13022f0e9d80SVille Syrjälä 
13035b34ab52SVille Syrjälä 	return DIV_ROUND_CLOSEST_ULL(mul_u32_u32(num, 1000), den);
1304f453ba04SDave Airlie }
1305f453ba04SDave Airlie EXPORT_SYMBOL(drm_mode_vrefresh);
1306f453ba04SDave Airlie 
1307f453ba04SDave Airlie /**
1308196cd5d3SDaniel Vetter  * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
1309196cd5d3SDaniel Vetter  * @mode: mode to query
1310196cd5d3SDaniel Vetter  * @hdisplay: hdisplay value to fill in
1311196cd5d3SDaniel Vetter  * @vdisplay: vdisplay value to fill in
1312196cd5d3SDaniel Vetter  *
1313196cd5d3SDaniel Vetter  * The vdisplay value will be doubled if the specified mode is a stereo mode of
1314196cd5d3SDaniel Vetter  * the appropriate layout.
1315196cd5d3SDaniel Vetter  */
drm_mode_get_hv_timing(const struct drm_display_mode * mode,int * hdisplay,int * vdisplay)1316196cd5d3SDaniel Vetter void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
1317196cd5d3SDaniel Vetter 			    int *hdisplay, int *vdisplay)
1318196cd5d3SDaniel Vetter {
1319563c4a75SVille Syrjälä 	struct drm_display_mode adjusted;
1320563c4a75SVille Syrjälä 
1321563c4a75SVille Syrjälä 	drm_mode_init(&adjusted, mode);
1322196cd5d3SDaniel Vetter 
1323196cd5d3SDaniel Vetter 	drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
1324196cd5d3SDaniel Vetter 	*hdisplay = adjusted.crtc_hdisplay;
1325196cd5d3SDaniel Vetter 	*vdisplay = adjusted.crtc_vdisplay;
1326196cd5d3SDaniel Vetter }
1327196cd5d3SDaniel Vetter EXPORT_SYMBOL(drm_mode_get_hv_timing);
1328196cd5d3SDaniel Vetter 
1329196cd5d3SDaniel Vetter /**
1330f5aabb97SDaniel Vetter  * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
1331f453ba04SDave Airlie  * @p: mode
1332448cce25SDamien Lespiau  * @adjust_flags: a combination of adjustment flags
1333f453ba04SDave Airlie  *
1334f5aabb97SDaniel Vetter  * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
1335448cce25SDamien Lespiau  *
1336448cce25SDamien Lespiau  * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
1337448cce25SDamien Lespiau  *   interlaced modes.
1338448cce25SDamien Lespiau  * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
1339448cce25SDamien Lespiau  *   buffers containing two eyes (only adjust the timings when needed, eg. for
1340448cce25SDamien Lespiau  *   "frame packing" or "side by side full").
1341ecb7e16bSGustavo Padovan  * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
1342ecb7e16bSGustavo Padovan  *   be performed for doublescan and vscan > 1 modes respectively.
1343f453ba04SDave Airlie  */
drm_mode_set_crtcinfo(struct drm_display_mode * p,int adjust_flags)1344f453ba04SDave Airlie void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
1345f453ba04SDave Airlie {
13464f09c77bSVille Syrjälä 	if (!p)
1347f453ba04SDave Airlie 		return;
1348f453ba04SDave Airlie 
1349bde2dcf7SDamien Lespiau 	p->crtc_clock = p->clock;
1350f453ba04SDave Airlie 	p->crtc_hdisplay = p->hdisplay;
1351f453ba04SDave Airlie 	p->crtc_hsync_start = p->hsync_start;
1352f453ba04SDave Airlie 	p->crtc_hsync_end = p->hsync_end;
1353f453ba04SDave Airlie 	p->crtc_htotal = p->htotal;
1354f453ba04SDave Airlie 	p->crtc_hskew = p->hskew;
1355f453ba04SDave Airlie 	p->crtc_vdisplay = p->vdisplay;
1356f453ba04SDave Airlie 	p->crtc_vsync_start = p->vsync_start;
1357f453ba04SDave Airlie 	p->crtc_vsync_end = p->vsync_end;
1358f453ba04SDave Airlie 	p->crtc_vtotal = p->vtotal;
1359f453ba04SDave Airlie 
1360f453ba04SDave Airlie 	if (p->flags & DRM_MODE_FLAG_INTERLACE) {
1361f453ba04SDave Airlie 		if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
1362f453ba04SDave Airlie 			p->crtc_vdisplay /= 2;
1363f453ba04SDave Airlie 			p->crtc_vsync_start /= 2;
1364f453ba04SDave Airlie 			p->crtc_vsync_end /= 2;
1365f453ba04SDave Airlie 			p->crtc_vtotal /= 2;
1366f453ba04SDave Airlie 		}
1367f453ba04SDave Airlie 	}
1368f453ba04SDave Airlie 
1369ecb7e16bSGustavo Padovan 	if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
1370f453ba04SDave Airlie 		if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
1371f453ba04SDave Airlie 			p->crtc_vdisplay *= 2;
1372f453ba04SDave Airlie 			p->crtc_vsync_start *= 2;
1373f453ba04SDave Airlie 			p->crtc_vsync_end *= 2;
1374f453ba04SDave Airlie 			p->crtc_vtotal *= 2;
1375f453ba04SDave Airlie 		}
1376ecb7e16bSGustavo Padovan 	}
1377f453ba04SDave Airlie 
1378ecb7e16bSGustavo Padovan 	if (!(adjust_flags & CRTC_NO_VSCAN)) {
1379f453ba04SDave Airlie 		if (p->vscan > 1) {
1380f453ba04SDave Airlie 			p->crtc_vdisplay *= p->vscan;
1381f453ba04SDave Airlie 			p->crtc_vsync_start *= p->vscan;
1382f453ba04SDave Airlie 			p->crtc_vsync_end *= p->vscan;
1383f453ba04SDave Airlie 			p->crtc_vtotal *= p->vscan;
1384f453ba04SDave Airlie 		}
1385ecb7e16bSGustavo Padovan 	}
1386f453ba04SDave Airlie 
1387448cce25SDamien Lespiau 	if (adjust_flags & CRTC_STEREO_DOUBLE) {
1388448cce25SDamien Lespiau 		unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
1389448cce25SDamien Lespiau 
1390448cce25SDamien Lespiau 		switch (layout) {
1391448cce25SDamien Lespiau 		case DRM_MODE_FLAG_3D_FRAME_PACKING:
1392448cce25SDamien Lespiau 			p->crtc_clock *= 2;
1393448cce25SDamien Lespiau 			p->crtc_vdisplay += p->crtc_vtotal;
1394448cce25SDamien Lespiau 			p->crtc_vsync_start += p->crtc_vtotal;
1395448cce25SDamien Lespiau 			p->crtc_vsync_end += p->crtc_vtotal;
1396448cce25SDamien Lespiau 			p->crtc_vtotal += p->crtc_vtotal;
1397448cce25SDamien Lespiau 			break;
1398448cce25SDamien Lespiau 		}
1399448cce25SDamien Lespiau 	}
1400448cce25SDamien Lespiau 
1401f453ba04SDave Airlie 	p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
1402f453ba04SDave Airlie 	p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
1403f453ba04SDave Airlie 	p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
1404f453ba04SDave Airlie 	p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
1405f453ba04SDave Airlie }
1406f453ba04SDave Airlie EXPORT_SYMBOL(drm_mode_set_crtcinfo);
1407f453ba04SDave Airlie 
1408f453ba04SDave Airlie /**
1409c3c50e8bSVille Syrjälä  * drm_mode_copy - copy the mode
1410c3c50e8bSVille Syrjälä  * @dst: mode to overwrite
1411c3c50e8bSVille Syrjälä  * @src: mode to copy
1412c3c50e8bSVille Syrjälä  *
1413b3d9f59fSVille Syrjälä  * Copy an existing mode into another mode, preserving the
141472e45e92SVille Syrjälä  * list head of the destination mode.
1415c3c50e8bSVille Syrjälä  */
drm_mode_copy(struct drm_display_mode * dst,const struct drm_display_mode * src)1416c3c50e8bSVille Syrjälä void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
1417c3c50e8bSVille Syrjälä {
141872e45e92SVille Syrjälä 	struct list_head head = dst->head;
1419c3c50e8bSVille Syrjälä 
1420c3c50e8bSVille Syrjälä 	*dst = *src;
142172e45e92SVille Syrjälä 	dst->head = head;
1422c3c50e8bSVille Syrjälä }
1423c3c50e8bSVille Syrjälä EXPORT_SYMBOL(drm_mode_copy);
1424c3c50e8bSVille Syrjälä 
1425c3c50e8bSVille Syrjälä /**
14262d3eec89SVille Syrjälä  * drm_mode_init - initialize the mode from another mode
14272d3eec89SVille Syrjälä  * @dst: mode to overwrite
14282d3eec89SVille Syrjälä  * @src: mode to copy
14292d3eec89SVille Syrjälä  *
14302d3eec89SVille Syrjälä  * Copy an existing mode into another mode, zeroing the
14312d3eec89SVille Syrjälä  * list head of the destination mode. Typically used
14322d3eec89SVille Syrjälä  * to guarantee the list head is not left with stack
14332d3eec89SVille Syrjälä  * garbage in on-stack modes.
14342d3eec89SVille Syrjälä  */
drm_mode_init(struct drm_display_mode * dst,const struct drm_display_mode * src)14352d3eec89SVille Syrjälä void drm_mode_init(struct drm_display_mode *dst, const struct drm_display_mode *src)
14362d3eec89SVille Syrjälä {
14372d3eec89SVille Syrjälä 	memset(dst, 0, sizeof(*dst));
14382d3eec89SVille Syrjälä 	drm_mode_copy(dst, src);
14392d3eec89SVille Syrjälä }
14402d3eec89SVille Syrjälä EXPORT_SYMBOL(drm_mode_init);
14412d3eec89SVille Syrjälä 
14422d3eec89SVille Syrjälä /**
1443f453ba04SDave Airlie  * drm_mode_duplicate - allocate and duplicate an existing mode
14443ec0db81SDaniel Vetter  * @dev: drm_device to allocate the duplicated mode for
14453ec0db81SDaniel Vetter  * @mode: mode to duplicate
1446f453ba04SDave Airlie  *
1447f453ba04SDave Airlie  * Just allocate a new mode, copy the existing mode into it, and return
1448f453ba04SDave Airlie  * a pointer to it.  Used to create new instances of established modes.
1449f5aabb97SDaniel Vetter  *
1450f5aabb97SDaniel Vetter  * Returns:
1451f5aabb97SDaniel Vetter  * Pointer to duplicated mode on success, NULL on error.
1452f453ba04SDave Airlie  */
drm_mode_duplicate(struct drm_device * dev,const struct drm_display_mode * mode)1453f453ba04SDave Airlie struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
1454b1f559ecSChris Wilson 					    const struct drm_display_mode *mode)
1455f453ba04SDave Airlie {
1456f453ba04SDave Airlie 	struct drm_display_mode *nmode;
1457f453ba04SDave Airlie 
1458f453ba04SDave Airlie 	nmode = drm_mode_create(dev);
1459f453ba04SDave Airlie 	if (!nmode)
1460f453ba04SDave Airlie 		return NULL;
1461f453ba04SDave Airlie 
1462c3c50e8bSVille Syrjälä 	drm_mode_copy(nmode, mode);
1463c3c50e8bSVille Syrjälä 
1464f453ba04SDave Airlie 	return nmode;
1465f453ba04SDave Airlie }
1466f453ba04SDave Airlie EXPORT_SYMBOL(drm_mode_duplicate);
1467f453ba04SDave Airlie 
drm_mode_match_timings(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)1468dd7c2626SVille Syrjälä static bool drm_mode_match_timings(const struct drm_display_mode *mode1,
1469dd7c2626SVille Syrjälä 				   const struct drm_display_mode *mode2)
1470dd7c2626SVille Syrjälä {
1471dd7c2626SVille Syrjälä 	return mode1->hdisplay == mode2->hdisplay &&
1472dd7c2626SVille Syrjälä 		mode1->hsync_start == mode2->hsync_start &&
1473dd7c2626SVille Syrjälä 		mode1->hsync_end == mode2->hsync_end &&
1474dd7c2626SVille Syrjälä 		mode1->htotal == mode2->htotal &&
1475dd7c2626SVille Syrjälä 		mode1->hskew == mode2->hskew &&
1476dd7c2626SVille Syrjälä 		mode1->vdisplay == mode2->vdisplay &&
1477dd7c2626SVille Syrjälä 		mode1->vsync_start == mode2->vsync_start &&
1478dd7c2626SVille Syrjälä 		mode1->vsync_end == mode2->vsync_end &&
1479dd7c2626SVille Syrjälä 		mode1->vtotal == mode2->vtotal &&
1480dd7c2626SVille Syrjälä 		mode1->vscan == mode2->vscan;
1481dd7c2626SVille Syrjälä }
1482dd7c2626SVille Syrjälä 
drm_mode_match_clock(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)1483dd7c2626SVille Syrjälä static bool drm_mode_match_clock(const struct drm_display_mode *mode1,
1484dd7c2626SVille Syrjälä 				  const struct drm_display_mode *mode2)
1485dd7c2626SVille Syrjälä {
1486dd7c2626SVille Syrjälä 	/*
1487dd7c2626SVille Syrjälä 	 * do clock check convert to PICOS
1488dd7c2626SVille Syrjälä 	 * so fb modes get matched the same
1489dd7c2626SVille Syrjälä 	 */
1490dd7c2626SVille Syrjälä 	if (mode1->clock && mode2->clock)
1491dd7c2626SVille Syrjälä 		return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock);
1492dd7c2626SVille Syrjälä 	else
1493dd7c2626SVille Syrjälä 		return mode1->clock == mode2->clock;
1494dd7c2626SVille Syrjälä }
1495dd7c2626SVille Syrjälä 
drm_mode_match_flags(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)1496dd7c2626SVille Syrjälä static bool drm_mode_match_flags(const struct drm_display_mode *mode1,
1497dd7c2626SVille Syrjälä 				 const struct drm_display_mode *mode2)
1498dd7c2626SVille Syrjälä {
1499dd7c2626SVille Syrjälä 	return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
1500dd7c2626SVille Syrjälä 		(mode2->flags & ~DRM_MODE_FLAG_3D_MASK);
1501dd7c2626SVille Syrjälä }
1502dd7c2626SVille Syrjälä 
drm_mode_match_3d_flags(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)1503dd7c2626SVille Syrjälä static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1,
1504dd7c2626SVille Syrjälä 				    const struct drm_display_mode *mode2)
1505dd7c2626SVille Syrjälä {
1506dd7c2626SVille Syrjälä 	return (mode1->flags & DRM_MODE_FLAG_3D_MASK) ==
1507dd7c2626SVille Syrjälä 		(mode2->flags & DRM_MODE_FLAG_3D_MASK);
1508dd7c2626SVille Syrjälä }
1509dd7c2626SVille Syrjälä 
drm_mode_match_aspect_ratio(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)1510dd7c2626SVille Syrjälä static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1,
1511dd7c2626SVille Syrjälä 					const struct drm_display_mode *mode2)
1512dd7c2626SVille Syrjälä {
1513dd7c2626SVille Syrjälä 	return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio;
1514dd7c2626SVille Syrjälä }
1515dd7c2626SVille Syrjälä 
1516dd7c2626SVille Syrjälä /**
1517dd7c2626SVille Syrjälä  * drm_mode_match - test modes for (partial) equality
1518dd7c2626SVille Syrjälä  * @mode1: first mode
1519dd7c2626SVille Syrjälä  * @mode2: second mode
1520dd7c2626SVille Syrjälä  * @match_flags: which parts need to match (DRM_MODE_MATCH_*)
1521dd7c2626SVille Syrjälä  *
1522dd7c2626SVille Syrjälä  * Check to see if @mode1 and @mode2 are equivalent.
1523dd7c2626SVille Syrjälä  *
1524dd7c2626SVille Syrjälä  * Returns:
1525dd7c2626SVille Syrjälä  * True if the modes are (partially) equal, false otherwise.
1526dd7c2626SVille Syrjälä  */
drm_mode_match(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2,unsigned int match_flags)1527dd7c2626SVille Syrjälä bool drm_mode_match(const struct drm_display_mode *mode1,
1528dd7c2626SVille Syrjälä 		    const struct drm_display_mode *mode2,
1529dd7c2626SVille Syrjälä 		    unsigned int match_flags)
1530dd7c2626SVille Syrjälä {
1531dd7c2626SVille Syrjälä 	if (!mode1 && !mode2)
1532dd7c2626SVille Syrjälä 		return true;
1533dd7c2626SVille Syrjälä 
1534dd7c2626SVille Syrjälä 	if (!mode1 || !mode2)
1535dd7c2626SVille Syrjälä 		return false;
1536dd7c2626SVille Syrjälä 
1537dd7c2626SVille Syrjälä 	if (match_flags & DRM_MODE_MATCH_TIMINGS &&
1538dd7c2626SVille Syrjälä 	    !drm_mode_match_timings(mode1, mode2))
1539dd7c2626SVille Syrjälä 		return false;
1540dd7c2626SVille Syrjälä 
1541dd7c2626SVille Syrjälä 	if (match_flags & DRM_MODE_MATCH_CLOCK &&
1542dd7c2626SVille Syrjälä 	    !drm_mode_match_clock(mode1, mode2))
1543dd7c2626SVille Syrjälä 		return false;
1544dd7c2626SVille Syrjälä 
1545dd7c2626SVille Syrjälä 	if (match_flags & DRM_MODE_MATCH_FLAGS &&
1546dd7c2626SVille Syrjälä 	    !drm_mode_match_flags(mode1, mode2))
1547dd7c2626SVille Syrjälä 		return false;
1548dd7c2626SVille Syrjälä 
1549dd7c2626SVille Syrjälä 	if (match_flags & DRM_MODE_MATCH_3D_FLAGS &&
1550dd7c2626SVille Syrjälä 	    !drm_mode_match_3d_flags(mode1, mode2))
1551dd7c2626SVille Syrjälä 		return false;
1552dd7c2626SVille Syrjälä 
1553dd7c2626SVille Syrjälä 	if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO &&
1554dd7c2626SVille Syrjälä 	    !drm_mode_match_aspect_ratio(mode1, mode2))
1555dd7c2626SVille Syrjälä 		return false;
1556dd7c2626SVille Syrjälä 
1557dd7c2626SVille Syrjälä 	return true;
1558dd7c2626SVille Syrjälä }
1559dd7c2626SVille Syrjälä EXPORT_SYMBOL(drm_mode_match);
1560dd7c2626SVille Syrjälä 
1561f453ba04SDave Airlie /**
1562f453ba04SDave Airlie  * drm_mode_equal - test modes for equality
1563f453ba04SDave Airlie  * @mode1: first mode
1564f453ba04SDave Airlie  * @mode2: second mode
1565f453ba04SDave Airlie  *
1566f453ba04SDave Airlie  * Check to see if @mode1 and @mode2 are equivalent.
1567f453ba04SDave Airlie  *
1568f5aabb97SDaniel Vetter  * Returns:
1569f453ba04SDave Airlie  * True if the modes are equal, false otherwise.
1570f453ba04SDave Airlie  */
drm_mode_equal(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)1571dd7c2626SVille Syrjälä bool drm_mode_equal(const struct drm_display_mode *mode1,
1572dd7c2626SVille Syrjälä 		    const struct drm_display_mode *mode2)
1573f453ba04SDave Airlie {
1574dd7c2626SVille Syrjälä 	return drm_mode_match(mode1, mode2,
1575dd7c2626SVille Syrjälä 			      DRM_MODE_MATCH_TIMINGS |
1576dd7c2626SVille Syrjälä 			      DRM_MODE_MATCH_CLOCK |
1577dd7c2626SVille Syrjälä 			      DRM_MODE_MATCH_FLAGS |
1578222ec161SShashank Sharma 			      DRM_MODE_MATCH_3D_FLAGS|
1579222ec161SShashank Sharma 			      DRM_MODE_MATCH_ASPECT_RATIO);
15804c6bcf44SVille Syrjälä }
15814c6bcf44SVille Syrjälä EXPORT_SYMBOL(drm_mode_equal);
15824c6bcf44SVille Syrjälä 
15834c6bcf44SVille Syrjälä /**
15844c6bcf44SVille Syrjälä  * drm_mode_equal_no_clocks - test modes for equality
15854c6bcf44SVille Syrjälä  * @mode1: first mode
15864c6bcf44SVille Syrjälä  * @mode2: second mode
15874c6bcf44SVille Syrjälä  *
15884c6bcf44SVille Syrjälä  * Check to see if @mode1 and @mode2 are equivalent, but
15894c6bcf44SVille Syrjälä  * don't check the pixel clocks.
15904c6bcf44SVille Syrjälä  *
15914c6bcf44SVille Syrjälä  * Returns:
15924c6bcf44SVille Syrjälä  * True if the modes are equal, false otherwise.
15934c6bcf44SVille Syrjälä  */
drm_mode_equal_no_clocks(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)1594dd7c2626SVille Syrjälä bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1,
1595dd7c2626SVille Syrjälä 			      const struct drm_display_mode *mode2)
15964c6bcf44SVille Syrjälä {
1597dd7c2626SVille Syrjälä 	return drm_mode_match(mode1, mode2,
1598dd7c2626SVille Syrjälä 			      DRM_MODE_MATCH_TIMINGS |
1599dd7c2626SVille Syrjälä 			      DRM_MODE_MATCH_FLAGS |
1600dd7c2626SVille Syrjälä 			      DRM_MODE_MATCH_3D_FLAGS);
16018cc3f23cSVille Syrjälä }
16024c6bcf44SVille Syrjälä EXPORT_SYMBOL(drm_mode_equal_no_clocks);
16038cc3f23cSVille Syrjälä 
16048cc3f23cSVille Syrjälä /**
1605f2ecf2e3SDamien Lespiau  * drm_mode_equal_no_clocks_no_stereo - test modes for equality
16068cc3f23cSVille Syrjälä  * @mode1: first mode
16078cc3f23cSVille Syrjälä  * @mode2: second mode
16088cc3f23cSVille Syrjälä  *
16098cc3f23cSVille Syrjälä  * Check to see if @mode1 and @mode2 are equivalent, but
1610f2ecf2e3SDamien Lespiau  * don't check the pixel clocks nor the stereo layout.
16118cc3f23cSVille Syrjälä  *
1612f5aabb97SDaniel Vetter  * Returns:
16138cc3f23cSVille Syrjälä  * True if the modes are equal, false otherwise.
16148cc3f23cSVille Syrjälä  */
drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)1615f2ecf2e3SDamien Lespiau bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
1616f2ecf2e3SDamien Lespiau 					const struct drm_display_mode *mode2)
16178cc3f23cSVille Syrjälä {
1618dd7c2626SVille Syrjälä 	return drm_mode_match(mode1, mode2,
1619dd7c2626SVille Syrjälä 			      DRM_MODE_MATCH_TIMINGS |
1620dd7c2626SVille Syrjälä 			      DRM_MODE_MATCH_FLAGS);
1621f453ba04SDave Airlie }
1622f2ecf2e3SDamien Lespiau EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
1623f453ba04SDave Airlie 
162475a655e0SVille Syrjälä static enum drm_mode_status
drm_mode_validate_basic(const struct drm_display_mode * mode)1625abc0b144SVille Syrjälä drm_mode_validate_basic(const struct drm_display_mode *mode)
1626abc0b144SVille Syrjälä {
1627c6ed6dadSVille Syrjälä 	if (mode->type & ~DRM_MODE_TYPE_ALL)
1628c6ed6dadSVille Syrjälä 		return MODE_BAD;
1629c6ed6dadSVille Syrjälä 
1630c6ed6dadSVille Syrjälä 	if (mode->flags & ~DRM_MODE_FLAG_ALL)
1631c6ed6dadSVille Syrjälä 		return MODE_BAD;
1632c6ed6dadSVille Syrjälä 
1633064a3e6eSVille Syrjälä 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1634064a3e6eSVille Syrjälä 		return MODE_BAD;
1635064a3e6eSVille Syrjälä 
1636abc0b144SVille Syrjälä 	if (mode->clock == 0)
1637abc0b144SVille Syrjälä 		return MODE_CLOCK_LOW;
1638abc0b144SVille Syrjälä 
1639abc0b144SVille Syrjälä 	if (mode->hdisplay == 0 ||
1640abc0b144SVille Syrjälä 	    mode->hsync_start < mode->hdisplay ||
1641abc0b144SVille Syrjälä 	    mode->hsync_end < mode->hsync_start ||
1642abc0b144SVille Syrjälä 	    mode->htotal < mode->hsync_end)
1643abc0b144SVille Syrjälä 		return MODE_H_ILLEGAL;
1644abc0b144SVille Syrjälä 
1645abc0b144SVille Syrjälä 	if (mode->vdisplay == 0 ||
1646abc0b144SVille Syrjälä 	    mode->vsync_start < mode->vdisplay ||
1647abc0b144SVille Syrjälä 	    mode->vsync_end < mode->vsync_start ||
1648abc0b144SVille Syrjälä 	    mode->vtotal < mode->vsync_end)
1649abc0b144SVille Syrjälä 		return MODE_V_ILLEGAL;
1650abc0b144SVille Syrjälä 
1651abc0b144SVille Syrjälä 	return MODE_OK;
1652abc0b144SVille Syrjälä }
165375a655e0SVille Syrjälä 
165475a655e0SVille Syrjälä /**
165575a655e0SVille Syrjälä  * drm_mode_validate_driver - make sure the mode is somewhat sane
165675a655e0SVille Syrjälä  * @dev: drm device
165775a655e0SVille Syrjälä  * @mode: mode to check
165875a655e0SVille Syrjälä  *
165975a655e0SVille Syrjälä  * First do basic validation on the mode, and then allow the driver
166075a655e0SVille Syrjälä  * to check for device/driver specific limitations via the optional
166175a655e0SVille Syrjälä  * &drm_mode_config_helper_funcs.mode_valid hook.
166275a655e0SVille Syrjälä  *
166375a655e0SVille Syrjälä  * Returns:
166475a655e0SVille Syrjälä  * The mode status
166575a655e0SVille Syrjälä  */
166675a655e0SVille Syrjälä enum drm_mode_status
drm_mode_validate_driver(struct drm_device * dev,const struct drm_display_mode * mode)166775a655e0SVille Syrjälä drm_mode_validate_driver(struct drm_device *dev,
166875a655e0SVille Syrjälä 			const struct drm_display_mode *mode)
166975a655e0SVille Syrjälä {
167075a655e0SVille Syrjälä 	enum drm_mode_status status;
167175a655e0SVille Syrjälä 
167275a655e0SVille Syrjälä 	status = drm_mode_validate_basic(mode);
167375a655e0SVille Syrjälä 	if (status != MODE_OK)
167475a655e0SVille Syrjälä 		return status;
167575a655e0SVille Syrjälä 
167675a655e0SVille Syrjälä 	if (dev->mode_config.funcs->mode_valid)
167775a655e0SVille Syrjälä 		return dev->mode_config.funcs->mode_valid(dev, mode);
167875a655e0SVille Syrjälä 	else
167975a655e0SVille Syrjälä 		return MODE_OK;
168075a655e0SVille Syrjälä }
168175a655e0SVille Syrjälä EXPORT_SYMBOL(drm_mode_validate_driver);
1682abc0b144SVille Syrjälä 
1683abc0b144SVille Syrjälä /**
1684f453ba04SDave Airlie  * drm_mode_validate_size - make sure modes adhere to size constraints
168505acaec3SVille Syrjälä  * @mode: mode to check
1686f453ba04SDave Airlie  * @maxX: maximum width
1687f453ba04SDave Airlie  * @maxY: maximum height
1688f453ba04SDave Airlie  *
1689f5aabb97SDaniel Vetter  * This function is a helper which can be used to validate modes against size
1690f5aabb97SDaniel Vetter  * limitations of the DRM device/connector. If a mode is too big its status
169132197aabSMasanari Iida  * member is updated with the appropriate validation failure code. The list
1692f5aabb97SDaniel Vetter  * itself is not changed.
169305acaec3SVille Syrjälä  *
169405acaec3SVille Syrjälä  * Returns:
169505acaec3SVille Syrjälä  * The mode status
1696f453ba04SDave Airlie  */
169705acaec3SVille Syrjälä enum drm_mode_status
drm_mode_validate_size(const struct drm_display_mode * mode,int maxX,int maxY)169805acaec3SVille Syrjälä drm_mode_validate_size(const struct drm_display_mode *mode,
16993e70292cSDaniel Vetter 		       int maxX, int maxY)
1700f453ba04SDave Airlie {
1701f453ba04SDave Airlie 	if (maxX > 0 && mode->hdisplay > maxX)
170205acaec3SVille Syrjälä 		return MODE_VIRTUAL_X;
1703f453ba04SDave Airlie 
1704f453ba04SDave Airlie 	if (maxY > 0 && mode->vdisplay > maxY)
170505acaec3SVille Syrjälä 		return MODE_VIRTUAL_Y;
170605acaec3SVille Syrjälä 
170705acaec3SVille Syrjälä 	return MODE_OK;
1708f453ba04SDave Airlie }
1709f453ba04SDave Airlie EXPORT_SYMBOL(drm_mode_validate_size);
1710f453ba04SDave Airlie 
1711d8523153SShashank Sharma /**
1712d8523153SShashank Sharma  * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
1713d8523153SShashank Sharma  * @mode: mode to check
1714d8523153SShashank Sharma  * @connector: drm connector under action
1715d8523153SShashank Sharma  *
1716d8523153SShashank Sharma  * This function is a helper which can be used to filter out any YCBCR420
1717d8523153SShashank Sharma  * only mode, when the source doesn't support it.
1718d8523153SShashank Sharma  *
1719d8523153SShashank Sharma  * Returns:
1720d8523153SShashank Sharma  * The mode status
1721d8523153SShashank Sharma  */
1722d8523153SShashank Sharma enum drm_mode_status
drm_mode_validate_ycbcr420(const struct drm_display_mode * mode,struct drm_connector * connector)1723d8523153SShashank Sharma drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
1724d8523153SShashank Sharma 			   struct drm_connector *connector)
1725d8523153SShashank Sharma {
17261c4f39b0SVille Syrjälä 	if (!connector->ycbcr_420_allowed &&
17271c4f39b0SVille Syrjälä 	    drm_mode_is_420_only(&connector->display_info, mode))
17281c4f39b0SVille Syrjälä 		return MODE_NO_420;
1729d8523153SShashank Sharma 
17301c4f39b0SVille Syrjälä 	return MODE_OK;
1731d8523153SShashank Sharma }
1732d8523153SShashank Sharma EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
1733d8523153SShashank Sharma 
1734e4bf44b3SVille Syrjälä #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1735e4bf44b3SVille Syrjälä 
1736e4bf44b3SVille Syrjälä static const char * const drm_mode_status_names[] = {
1737e4bf44b3SVille Syrjälä 	MODE_STATUS(OK),
1738e4bf44b3SVille Syrjälä 	MODE_STATUS(HSYNC),
1739e4bf44b3SVille Syrjälä 	MODE_STATUS(VSYNC),
1740e4bf44b3SVille Syrjälä 	MODE_STATUS(H_ILLEGAL),
1741e4bf44b3SVille Syrjälä 	MODE_STATUS(V_ILLEGAL),
1742e4bf44b3SVille Syrjälä 	MODE_STATUS(BAD_WIDTH),
1743e4bf44b3SVille Syrjälä 	MODE_STATUS(NOMODE),
1744e4bf44b3SVille Syrjälä 	MODE_STATUS(NO_INTERLACE),
1745e4bf44b3SVille Syrjälä 	MODE_STATUS(NO_DBLESCAN),
1746e4bf44b3SVille Syrjälä 	MODE_STATUS(NO_VSCAN),
1747e4bf44b3SVille Syrjälä 	MODE_STATUS(MEM),
1748e4bf44b3SVille Syrjälä 	MODE_STATUS(VIRTUAL_X),
1749e4bf44b3SVille Syrjälä 	MODE_STATUS(VIRTUAL_Y),
1750e4bf44b3SVille Syrjälä 	MODE_STATUS(MEM_VIRT),
1751e4bf44b3SVille Syrjälä 	MODE_STATUS(NOCLOCK),
1752e4bf44b3SVille Syrjälä 	MODE_STATUS(CLOCK_HIGH),
1753e4bf44b3SVille Syrjälä 	MODE_STATUS(CLOCK_LOW),
1754e4bf44b3SVille Syrjälä 	MODE_STATUS(CLOCK_RANGE),
1755e4bf44b3SVille Syrjälä 	MODE_STATUS(BAD_HVALUE),
1756e4bf44b3SVille Syrjälä 	MODE_STATUS(BAD_VVALUE),
1757e4bf44b3SVille Syrjälä 	MODE_STATUS(BAD_VSCAN),
1758e4bf44b3SVille Syrjälä 	MODE_STATUS(HSYNC_NARROW),
1759e4bf44b3SVille Syrjälä 	MODE_STATUS(HSYNC_WIDE),
1760e4bf44b3SVille Syrjälä 	MODE_STATUS(HBLANK_NARROW),
1761e4bf44b3SVille Syrjälä 	MODE_STATUS(HBLANK_WIDE),
1762e4bf44b3SVille Syrjälä 	MODE_STATUS(VSYNC_NARROW),
1763e4bf44b3SVille Syrjälä 	MODE_STATUS(VSYNC_WIDE),
1764e4bf44b3SVille Syrjälä 	MODE_STATUS(VBLANK_NARROW),
1765e4bf44b3SVille Syrjälä 	MODE_STATUS(VBLANK_WIDE),
1766e4bf44b3SVille Syrjälä 	MODE_STATUS(PANEL),
1767e4bf44b3SVille Syrjälä 	MODE_STATUS(INTERLACE_WIDTH),
1768e4bf44b3SVille Syrjälä 	MODE_STATUS(ONE_WIDTH),
1769e4bf44b3SVille Syrjälä 	MODE_STATUS(ONE_HEIGHT),
1770e4bf44b3SVille Syrjälä 	MODE_STATUS(ONE_SIZE),
1771e4bf44b3SVille Syrjälä 	MODE_STATUS(NO_REDUCED),
1772e4bf44b3SVille Syrjälä 	MODE_STATUS(NO_STEREO),
1773d8523153SShashank Sharma 	MODE_STATUS(NO_420),
17745ba89406SVille Syrjälä 	MODE_STATUS(STALE),
1775e4bf44b3SVille Syrjälä 	MODE_STATUS(BAD),
1776e4bf44b3SVille Syrjälä 	MODE_STATUS(ERROR),
1777e4bf44b3SVille Syrjälä };
1778e4bf44b3SVille Syrjälä 
1779e4bf44b3SVille Syrjälä #undef MODE_STATUS
1780e4bf44b3SVille Syrjälä 
drm_get_mode_status_name(enum drm_mode_status status)17816ab0edf4SVille Syrjälä const char *drm_get_mode_status_name(enum drm_mode_status status)
1782e4bf44b3SVille Syrjälä {
1783e4bf44b3SVille Syrjälä 	int index = status + 3;
1784e4bf44b3SVille Syrjälä 
1785e4bf44b3SVille Syrjälä 	if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1786e4bf44b3SVille Syrjälä 		return "";
1787e4bf44b3SVille Syrjälä 
1788e4bf44b3SVille Syrjälä 	return drm_mode_status_names[index];
1789e4bf44b3SVille Syrjälä }
1790e4bf44b3SVille Syrjälä 
1791f453ba04SDave Airlie /**
1792f453ba04SDave Airlie  * drm_mode_prune_invalid - remove invalid modes from mode list
1793f453ba04SDave Airlie  * @dev: DRM device
1794f453ba04SDave Airlie  * @mode_list: list of modes to check
1795f453ba04SDave Airlie  * @verbose: be verbose about it
1796f453ba04SDave Airlie  *
1797f5aabb97SDaniel Vetter  * This helper function can be used to prune a display mode list after
17981e55a53aSMatt Roper  * validation has been completed. All modes whose status is not MODE_OK will be
1799f5aabb97SDaniel Vetter  * removed from the list, and if @verbose the status code and mode name is also
1800f5aabb97SDaniel Vetter  * printed to dmesg.
1801f453ba04SDave Airlie  */
drm_mode_prune_invalid(struct drm_device * dev,struct list_head * mode_list,bool verbose)1802f453ba04SDave Airlie void drm_mode_prune_invalid(struct drm_device *dev,
1803f453ba04SDave Airlie 			    struct list_head *mode_list, bool verbose)
1804f453ba04SDave Airlie {
1805f453ba04SDave Airlie 	struct drm_display_mode *mode, *t;
1806f453ba04SDave Airlie 
1807f453ba04SDave Airlie 	list_for_each_entry_safe(mode, t, mode_list, head) {
1808f453ba04SDave Airlie 		if (mode->status != MODE_OK) {
1809f453ba04SDave Airlie 			list_del(&mode->head);
1810b6415bd7SThomas Zimmermann 			if (mode->type & DRM_MODE_TYPE_USERDEF) {
1811b6415bd7SThomas Zimmermann 				drm_warn(dev, "User-defined mode not supported: "
1812b6415bd7SThomas Zimmermann 					 DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
1813b6415bd7SThomas Zimmermann 			}
1814f453ba04SDave Airlie 			if (verbose) {
1815f453ba04SDave Airlie 				drm_mode_debug_printmodeline(mode);
1816e4bf44b3SVille Syrjälä 				DRM_DEBUG_KMS("Not using %s mode: %s\n",
1817e4bf44b3SVille Syrjälä 					      mode->name,
1818e4bf44b3SVille Syrjälä 					      drm_get_mode_status_name(mode->status));
1819f453ba04SDave Airlie 			}
1820f453ba04SDave Airlie 			drm_mode_destroy(dev, mode);
1821f453ba04SDave Airlie 		}
1822f453ba04SDave Airlie 	}
1823f453ba04SDave Airlie }
1824f453ba04SDave Airlie EXPORT_SYMBOL(drm_mode_prune_invalid);
1825f453ba04SDave Airlie 
1826f453ba04SDave Airlie /**
1827f453ba04SDave Airlie  * drm_mode_compare - compare modes for favorability
18282c761270SDave Chinner  * @priv: unused
1829f453ba04SDave Airlie  * @lh_a: list_head for first mode
1830f453ba04SDave Airlie  * @lh_b: list_head for second mode
1831f453ba04SDave Airlie  *
1832f453ba04SDave Airlie  * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1833f453ba04SDave Airlie  * which is better.
1834f453ba04SDave Airlie  *
1835f5aabb97SDaniel Vetter  * Returns:
1836f453ba04SDave Airlie  * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1837f453ba04SDave Airlie  * positive if @lh_b is better than @lh_a.
1838f453ba04SDave Airlie  */
drm_mode_compare(void * priv,const struct list_head * lh_a,const struct list_head * lh_b)18394f0f586bSSami Tolvanen static int drm_mode_compare(void *priv, const struct list_head *lh_a,
18404f0f586bSSami Tolvanen 			    const struct list_head *lh_b)
1841f453ba04SDave Airlie {
1842f453ba04SDave Airlie 	struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1843f453ba04SDave Airlie 	struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1844f453ba04SDave Airlie 	int diff;
1845f453ba04SDave Airlie 
1846f453ba04SDave Airlie 	diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1847f453ba04SDave Airlie 		((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1848f453ba04SDave Airlie 	if (diff)
1849f453ba04SDave Airlie 		return diff;
1850f453ba04SDave Airlie 	diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1851f453ba04SDave Airlie 	if (diff)
1852f453ba04SDave Airlie 		return diff;
18539bc3cd56SVille Syrjälä 
18540425662fSVille Syrjälä 	diff = drm_mode_vrefresh(b) - drm_mode_vrefresh(a);
18559bc3cd56SVille Syrjälä 	if (diff)
18569bc3cd56SVille Syrjälä 		return diff;
18579bc3cd56SVille Syrjälä 
1858f453ba04SDave Airlie 	diff = b->clock - a->clock;
1859f453ba04SDave Airlie 	return diff;
1860f453ba04SDave Airlie }
1861f453ba04SDave Airlie 
1862f453ba04SDave Airlie /**
1863f453ba04SDave Airlie  * drm_mode_sort - sort mode list
1864f5aabb97SDaniel Vetter  * @mode_list: list of drm_display_mode structures to sort
1865f453ba04SDave Airlie  *
1866f5aabb97SDaniel Vetter  * Sort @mode_list by favorability, moving good modes to the head of the list.
1867f453ba04SDave Airlie  */
drm_mode_sort(struct list_head * mode_list)1868f453ba04SDave Airlie void drm_mode_sort(struct list_head *mode_list)
1869f453ba04SDave Airlie {
18702c761270SDave Chinner 	list_sort(NULL, mode_list, drm_mode_compare);
1871f453ba04SDave Airlie }
1872f453ba04SDave Airlie EXPORT_SYMBOL(drm_mode_sort);
1873f453ba04SDave Airlie 
1874f453ba04SDave Airlie /**
187597e14fbeSDaniel Vetter  * drm_connector_list_update - update the mode list for the connector
1876f453ba04SDave Airlie  * @connector: the connector to update
1877f453ba04SDave Airlie  *
1878f453ba04SDave Airlie  * This moves the modes from the @connector probed_modes list
1879f453ba04SDave Airlie  * to the actual mode list. It compares the probed mode against the current
1880f5aabb97SDaniel Vetter  * list and only adds different/new modes.
1881f5aabb97SDaniel Vetter  *
1882f5aabb97SDaniel Vetter  * This is just a helper functions doesn't validate any modes itself and also
1883f5aabb97SDaniel Vetter  * doesn't prune any invalid modes. Callers need to do that themselves.
1884f453ba04SDave Airlie  */
drm_connector_list_update(struct drm_connector * connector)188597e14fbeSDaniel Vetter void drm_connector_list_update(struct drm_connector *connector)
1886f453ba04SDave Airlie {
1887f453ba04SDave Airlie 	struct drm_display_mode *pmode, *pt;
1888f453ba04SDave Airlie 
188963951385SDaniel Vetter 	WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
189063951385SDaniel Vetter 
18912f8c19e7SVille Syrjälä 	list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
18922f8c19e7SVille Syrjälä 		struct drm_display_mode *mode;
18932f8c19e7SVille Syrjälä 		bool found_it = false;
18942f8c19e7SVille Syrjälä 
1895f453ba04SDave Airlie 		/* go through current modes checking for the new probed mode */
1896f453ba04SDave Airlie 		list_for_each_entry(mode, &connector->modes, head) {
18972f8c19e7SVille Syrjälä 			if (!drm_mode_equal(pmode, mode))
18982f8c19e7SVille Syrjälä 				continue;
18992f8c19e7SVille Syrjälä 
19002f8c19e7SVille Syrjälä 			found_it = true;
1901fc245f88SVille Syrjälä 
1902fc245f88SVille Syrjälä 			/*
1903fc245f88SVille Syrjälä 			 * If the old matching mode is stale (ie. left over
1904fc245f88SVille Syrjälä 			 * from a previous probe) just replace it outright.
1905fc245f88SVille Syrjälä 			 * Otherwise just merge the type bits between all
1906fc245f88SVille Syrjälä 			 * equal probed modes.
1907fc245f88SVille Syrjälä 			 *
1908fc245f88SVille Syrjälä 			 * If two probed modes are considered equal, pick the
1909fc245f88SVille Syrjälä 			 * actual timings from the one that's marked as
1910fc245f88SVille Syrjälä 			 * preferred (in case the match isn't 100%). If
1911fc245f88SVille Syrjälä 			 * multiple or zero preferred modes are present, favor
1912fc245f88SVille Syrjälä 			 * the mode added to the probed_modes list first.
1913fc245f88SVille Syrjälä 			 */
1914fc245f88SVille Syrjälä 			if (mode->status == MODE_STALE) {
1915fc245f88SVille Syrjälä 				drm_mode_copy(mode, pmode);
1916fc245f88SVille Syrjälä 			} else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
1917fc245f88SVille Syrjälä 				   (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
1918fc245f88SVille Syrjälä 				pmode->type |= mode->type;
1919fc245f88SVille Syrjälä 				drm_mode_copy(mode, pmode);
1920fc245f88SVille Syrjälä 			} else {
1921abce1ec9SDave Airlie 				mode->type |= pmode->type;
1922fc245f88SVille Syrjälä 			}
1923fc245f88SVille Syrjälä 
1924f453ba04SDave Airlie 			list_del(&pmode->head);
1925f453ba04SDave Airlie 			drm_mode_destroy(connector->dev, pmode);
1926f453ba04SDave Airlie 			break;
1927f453ba04SDave Airlie 		}
1928f453ba04SDave Airlie 
1929f453ba04SDave Airlie 		if (!found_it) {
1930f453ba04SDave Airlie 			list_move_tail(&pmode->head, &connector->modes);
1931f453ba04SDave Airlie 		}
1932f453ba04SDave Airlie 	}
1933f453ba04SDave Airlie }
193497e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_list_update);
19351794d257SChris Wilson 
drm_mode_parse_cmdline_bpp(const char * str,char ** end_ptr,struct drm_cmdline_mode * mode)1936e08ab74bSMaxime Ripard static int drm_mode_parse_cmdline_bpp(const char *str, char **end_ptr,
1937e08ab74bSMaxime Ripard 				      struct drm_cmdline_mode *mode)
1938e08ab74bSMaxime Ripard {
1939e08ab74bSMaxime Ripard 	unsigned int bpp;
1940e08ab74bSMaxime Ripard 
1941e08ab74bSMaxime Ripard 	if (str[0] != '-')
1942e08ab74bSMaxime Ripard 		return -EINVAL;
1943e08ab74bSMaxime Ripard 
1944e08ab74bSMaxime Ripard 	str++;
1945e08ab74bSMaxime Ripard 	bpp = simple_strtol(str, end_ptr, 10);
1946e08ab74bSMaxime Ripard 	if (*end_ptr == str)
1947e08ab74bSMaxime Ripard 		return -EINVAL;
1948e08ab74bSMaxime Ripard 
1949e08ab74bSMaxime Ripard 	mode->bpp = bpp;
1950e08ab74bSMaxime Ripard 	mode->bpp_specified = true;
1951e08ab74bSMaxime Ripard 
1952e08ab74bSMaxime Ripard 	return 0;
1953e08ab74bSMaxime Ripard }
1954e08ab74bSMaxime Ripard 
drm_mode_parse_cmdline_refresh(const char * str,char ** end_ptr,struct drm_cmdline_mode * mode)1955e08ab74bSMaxime Ripard static int drm_mode_parse_cmdline_refresh(const char *str, char **end_ptr,
1956e08ab74bSMaxime Ripard 					  struct drm_cmdline_mode *mode)
1957e08ab74bSMaxime Ripard {
1958e08ab74bSMaxime Ripard 	unsigned int refresh;
1959e08ab74bSMaxime Ripard 
1960e08ab74bSMaxime Ripard 	if (str[0] != '@')
1961e08ab74bSMaxime Ripard 		return -EINVAL;
1962e08ab74bSMaxime Ripard 
1963e08ab74bSMaxime Ripard 	str++;
1964e08ab74bSMaxime Ripard 	refresh = simple_strtol(str, end_ptr, 10);
1965e08ab74bSMaxime Ripard 	if (*end_ptr == str)
1966e08ab74bSMaxime Ripard 		return -EINVAL;
1967e08ab74bSMaxime Ripard 
1968e08ab74bSMaxime Ripard 	mode->refresh = refresh;
1969e08ab74bSMaxime Ripard 	mode->refresh_specified = true;
1970e08ab74bSMaxime Ripard 
1971e08ab74bSMaxime Ripard 	return 0;
1972e08ab74bSMaxime Ripard }
1973e08ab74bSMaxime Ripard 
drm_mode_parse_cmdline_extra(const char * str,int length,bool freestanding,const struct drm_connector * connector,struct drm_cmdline_mode * mode)1974e08ab74bSMaxime Ripard static int drm_mode_parse_cmdline_extra(const char *str, int length,
1975325d0ab3SMaxime Ripard 					bool freestanding,
1976c0898fcaSArnd Bergmann 					const struct drm_connector *connector,
1977e08ab74bSMaxime Ripard 					struct drm_cmdline_mode *mode)
1978e08ab74bSMaxime Ripard {
1979e08ab74bSMaxime Ripard 	int i;
1980e08ab74bSMaxime Ripard 
1981e08ab74bSMaxime Ripard 	for (i = 0; i < length; i++) {
1982e08ab74bSMaxime Ripard 		switch (str[i]) {
1983e08ab74bSMaxime Ripard 		case 'i':
1984325d0ab3SMaxime Ripard 			if (freestanding)
1985325d0ab3SMaxime Ripard 				return -EINVAL;
1986325d0ab3SMaxime Ripard 
1987e08ab74bSMaxime Ripard 			mode->interlace = true;
1988e08ab74bSMaxime Ripard 			break;
1989e08ab74bSMaxime Ripard 		case 'm':
1990325d0ab3SMaxime Ripard 			if (freestanding)
1991325d0ab3SMaxime Ripard 				return -EINVAL;
1992325d0ab3SMaxime Ripard 
1993e08ab74bSMaxime Ripard 			mode->margins = true;
1994e08ab74bSMaxime Ripard 			break;
1995e08ab74bSMaxime Ripard 		case 'D':
1996e08ab74bSMaxime Ripard 			if (mode->force != DRM_FORCE_UNSPECIFIED)
1997e08ab74bSMaxime Ripard 				return -EINVAL;
1998e08ab74bSMaxime Ripard 
1999e08ab74bSMaxime Ripard 			if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
2000e08ab74bSMaxime Ripard 			    (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
2001e08ab74bSMaxime Ripard 				mode->force = DRM_FORCE_ON;
2002e08ab74bSMaxime Ripard 			else
2003e08ab74bSMaxime Ripard 				mode->force = DRM_FORCE_ON_DIGITAL;
2004e08ab74bSMaxime Ripard 			break;
2005e08ab74bSMaxime Ripard 		case 'd':
2006e08ab74bSMaxime Ripard 			if (mode->force != DRM_FORCE_UNSPECIFIED)
2007e08ab74bSMaxime Ripard 				return -EINVAL;
2008e08ab74bSMaxime Ripard 
2009e08ab74bSMaxime Ripard 			mode->force = DRM_FORCE_OFF;
2010e08ab74bSMaxime Ripard 			break;
2011e08ab74bSMaxime Ripard 		case 'e':
2012e08ab74bSMaxime Ripard 			if (mode->force != DRM_FORCE_UNSPECIFIED)
2013e08ab74bSMaxime Ripard 				return -EINVAL;
2014e08ab74bSMaxime Ripard 
2015e08ab74bSMaxime Ripard 			mode->force = DRM_FORCE_ON;
2016e08ab74bSMaxime Ripard 			break;
2017e08ab74bSMaxime Ripard 		default:
2018e08ab74bSMaxime Ripard 			return -EINVAL;
2019e08ab74bSMaxime Ripard 		}
2020e08ab74bSMaxime Ripard 	}
2021e08ab74bSMaxime Ripard 
2022e08ab74bSMaxime Ripard 	return 0;
2023e08ab74bSMaxime Ripard }
2024e08ab74bSMaxime Ripard 
drm_mode_parse_cmdline_res_mode(const char * str,unsigned int length,bool extras,const struct drm_connector * connector,struct drm_cmdline_mode * mode)2025e08ab74bSMaxime Ripard static int drm_mode_parse_cmdline_res_mode(const char *str, unsigned int length,
2026e08ab74bSMaxime Ripard 					   bool extras,
2027c0898fcaSArnd Bergmann 					   const struct drm_connector *connector,
2028e08ab74bSMaxime Ripard 					   struct drm_cmdline_mode *mode)
2029e08ab74bSMaxime Ripard {
2030e08ab74bSMaxime Ripard 	const char *str_start = str;
2031e08ab74bSMaxime Ripard 	bool rb = false, cvt = false;
2032e08ab74bSMaxime Ripard 	int xres = 0, yres = 0;
2033e08ab74bSMaxime Ripard 	int remaining, i;
2034e08ab74bSMaxime Ripard 	char *end_ptr;
2035e08ab74bSMaxime Ripard 
2036e08ab74bSMaxime Ripard 	xres = simple_strtol(str, &end_ptr, 10);
2037e08ab74bSMaxime Ripard 	if (end_ptr == str)
2038e08ab74bSMaxime Ripard 		return -EINVAL;
2039e08ab74bSMaxime Ripard 
2040e08ab74bSMaxime Ripard 	if (end_ptr[0] != 'x')
2041e08ab74bSMaxime Ripard 		return -EINVAL;
2042e08ab74bSMaxime Ripard 	end_ptr++;
2043e08ab74bSMaxime Ripard 
2044e08ab74bSMaxime Ripard 	str = end_ptr;
2045e08ab74bSMaxime Ripard 	yres = simple_strtol(str, &end_ptr, 10);
2046e08ab74bSMaxime Ripard 	if (end_ptr == str)
2047e08ab74bSMaxime Ripard 		return -EINVAL;
2048e08ab74bSMaxime Ripard 
2049e08ab74bSMaxime Ripard 	remaining = length - (end_ptr - str_start);
2050e08ab74bSMaxime Ripard 	if (remaining < 0)
2051e08ab74bSMaxime Ripard 		return -EINVAL;
2052e08ab74bSMaxime Ripard 
2053e08ab74bSMaxime Ripard 	for (i = 0; i < remaining; i++) {
2054e08ab74bSMaxime Ripard 		switch (end_ptr[i]) {
2055e08ab74bSMaxime Ripard 		case 'M':
2056e08ab74bSMaxime Ripard 			cvt = true;
2057e08ab74bSMaxime Ripard 			break;
2058e08ab74bSMaxime Ripard 		case 'R':
2059e08ab74bSMaxime Ripard 			rb = true;
2060e08ab74bSMaxime Ripard 			break;
2061e08ab74bSMaxime Ripard 		default:
2062e08ab74bSMaxime Ripard 			/*
2063e08ab74bSMaxime Ripard 			 * Try to pass that to our extras parsing
2064e08ab74bSMaxime Ripard 			 * function to handle the case where the
2065e08ab74bSMaxime Ripard 			 * extras are directly after the resolution
2066e08ab74bSMaxime Ripard 			 */
2067e08ab74bSMaxime Ripard 			if (extras) {
2068e08ab74bSMaxime Ripard 				int ret = drm_mode_parse_cmdline_extra(end_ptr + i,
2069e08ab74bSMaxime Ripard 								       1,
2070325d0ab3SMaxime Ripard 								       false,
2071e08ab74bSMaxime Ripard 								       connector,
2072e08ab74bSMaxime Ripard 								       mode);
2073e08ab74bSMaxime Ripard 				if (ret)
2074e08ab74bSMaxime Ripard 					return ret;
2075e08ab74bSMaxime Ripard 			} else {
2076e08ab74bSMaxime Ripard 				return -EINVAL;
2077e08ab74bSMaxime Ripard 			}
2078e08ab74bSMaxime Ripard 		}
2079e08ab74bSMaxime Ripard 	}
2080e08ab74bSMaxime Ripard 
2081e08ab74bSMaxime Ripard 	mode->xres = xres;
2082e08ab74bSMaxime Ripard 	mode->yres = yres;
2083e08ab74bSMaxime Ripard 	mode->cvt = cvt;
2084e08ab74bSMaxime Ripard 	mode->rb = rb;
2085e08ab74bSMaxime Ripard 
2086e08ab74bSMaxime Ripard 	return 0;
2087e08ab74bSMaxime Ripard }
2088e08ab74bSMaxime Ripard 
drm_mode_parse_cmdline_int(const char * delim,unsigned int * int_ret)20898582e244SHans de Goede static int drm_mode_parse_cmdline_int(const char *delim, unsigned int *int_ret)
20908582e244SHans de Goede {
20918582e244SHans de Goede 	const char *value;
20928582e244SHans de Goede 	char *endp;
20938582e244SHans de Goede 
20948582e244SHans de Goede 	/*
20958582e244SHans de Goede 	 * delim must point to the '=', otherwise it is a syntax error and
20960ae865efSCai Huoqing 	 * if delim points to the terminating zero, then delim + 1 will point
20978582e244SHans de Goede 	 * past the end of the string.
20988582e244SHans de Goede 	 */
20998582e244SHans de Goede 	if (*delim != '=')
21008582e244SHans de Goede 		return -EINVAL;
21018582e244SHans de Goede 
21028582e244SHans de Goede 	value = delim + 1;
21038582e244SHans de Goede 	*int_ret = simple_strtol(value, &endp, 10);
21048582e244SHans de Goede 
21058582e244SHans de Goede 	/* Make sure we have parsed something */
21068582e244SHans de Goede 	if (endp == value)
21078582e244SHans de Goede 		return -EINVAL;
21088582e244SHans de Goede 
21098582e244SHans de Goede 	return 0;
21108582e244SHans de Goede }
21118582e244SHans de Goede 
drm_mode_parse_panel_orientation(const char * delim,struct drm_cmdline_mode * mode)21124e7a4a6fSHans de Goede static int drm_mode_parse_panel_orientation(const char *delim,
21134e7a4a6fSHans de Goede 					    struct drm_cmdline_mode *mode)
21144e7a4a6fSHans de Goede {
21154e7a4a6fSHans de Goede 	const char *value;
21164e7a4a6fSHans de Goede 
21174e7a4a6fSHans de Goede 	if (*delim != '=')
21184e7a4a6fSHans de Goede 		return -EINVAL;
21194e7a4a6fSHans de Goede 
21204e7a4a6fSHans de Goede 	value = delim + 1;
21214e7a4a6fSHans de Goede 	delim = strchr(value, ',');
21224e7a4a6fSHans de Goede 	if (!delim)
21234e7a4a6fSHans de Goede 		delim = value + strlen(value);
21244e7a4a6fSHans de Goede 
21254e7a4a6fSHans de Goede 	if (!strncmp(value, "normal", delim - value))
21264e7a4a6fSHans de Goede 		mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
21274e7a4a6fSHans de Goede 	else if (!strncmp(value, "upside_down", delim - value))
21284e7a4a6fSHans de Goede 		mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
21294e7a4a6fSHans de Goede 	else if (!strncmp(value, "left_side_up", delim - value))
21304e7a4a6fSHans de Goede 		mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
21314e7a4a6fSHans de Goede 	else if (!strncmp(value, "right_side_up", delim - value))
21324e7a4a6fSHans de Goede 		mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
21334e7a4a6fSHans de Goede 	else
21344e7a4a6fSHans de Goede 		return -EINVAL;
21354e7a4a6fSHans de Goede 
21364e7a4a6fSHans de Goede 	return 0;
21374e7a4a6fSHans de Goede }
21384e7a4a6fSHans de Goede 
drm_mode_parse_tv_mode(const char * delim,struct drm_cmdline_mode * mode)2139e691c999SMaxime Ripard static int drm_mode_parse_tv_mode(const char *delim,
2140e691c999SMaxime Ripard 				  struct drm_cmdline_mode *mode)
2141e691c999SMaxime Ripard {
2142e691c999SMaxime Ripard 	const char *value;
2143e691c999SMaxime Ripard 	int ret;
2144e691c999SMaxime Ripard 
2145e691c999SMaxime Ripard 	if (*delim != '=')
2146e691c999SMaxime Ripard 		return -EINVAL;
2147e691c999SMaxime Ripard 
2148e691c999SMaxime Ripard 	value = delim + 1;
2149e691c999SMaxime Ripard 	delim = strchr(value, ',');
2150e691c999SMaxime Ripard 	if (!delim)
2151e691c999SMaxime Ripard 		delim = value + strlen(value);
2152e691c999SMaxime Ripard 
2153e691c999SMaxime Ripard 	ret = drm_get_tv_mode_from_name(value, delim - value);
2154e691c999SMaxime Ripard 	if (ret < 0)
2155e691c999SMaxime Ripard 		return ret;
2156e691c999SMaxime Ripard 
2157e691c999SMaxime Ripard 	mode->tv_mode_specified = true;
2158e691c999SMaxime Ripard 	mode->tv_mode = ret;
2159e691c999SMaxime Ripard 
2160e691c999SMaxime Ripard 	return 0;
2161e691c999SMaxime Ripard }
2162e691c999SMaxime Ripard 
drm_mode_parse_cmdline_options(const char * str,bool freestanding,const struct drm_connector * connector,struct drm_cmdline_mode * mode)2163739b200cSHans de Goede static int drm_mode_parse_cmdline_options(const char *str,
216499e2716eSHans de Goede 					  bool freestanding,
2165c0898fcaSArnd Bergmann 					  const struct drm_connector *connector,
21661bf4e092SMaxime Ripard 					  struct drm_cmdline_mode *mode)
21671bf4e092SMaxime Ripard {
21688582e244SHans de Goede 	unsigned int deg, margin, rotation = 0;
2169739b200cSHans de Goede 	const char *delim, *option, *sep;
21701bf4e092SMaxime Ripard 
2171739b200cSHans de Goede 	option = str;
2172739b200cSHans de Goede 	do {
21731bf4e092SMaxime Ripard 		delim = strchr(option, '=');
21741bf4e092SMaxime Ripard 		if (!delim) {
21751bf4e092SMaxime Ripard 			delim = strchr(option, ',');
21761bf4e092SMaxime Ripard 
21771bf4e092SMaxime Ripard 			if (!delim)
2178739b200cSHans de Goede 				delim = option + strlen(option);
21791bf4e092SMaxime Ripard 		}
21801bf4e092SMaxime Ripard 
21811bf4e092SMaxime Ripard 		if (!strncmp(option, "rotate", delim - option)) {
21828582e244SHans de Goede 			if (drm_mode_parse_cmdline_int(delim, &deg))
21831bf4e092SMaxime Ripard 				return -EINVAL;
21841bf4e092SMaxime Ripard 
21851bf4e092SMaxime Ripard 			switch (deg) {
21861bf4e092SMaxime Ripard 			case 0:
21871bf4e092SMaxime Ripard 				rotation |= DRM_MODE_ROTATE_0;
21881bf4e092SMaxime Ripard 				break;
21891bf4e092SMaxime Ripard 
21901bf4e092SMaxime Ripard 			case 90:
21911bf4e092SMaxime Ripard 				rotation |= DRM_MODE_ROTATE_90;
21921bf4e092SMaxime Ripard 				break;
21931bf4e092SMaxime Ripard 
21941bf4e092SMaxime Ripard 			case 180:
21951bf4e092SMaxime Ripard 				rotation |= DRM_MODE_ROTATE_180;
21961bf4e092SMaxime Ripard 				break;
21971bf4e092SMaxime Ripard 
21981bf4e092SMaxime Ripard 			case 270:
21991bf4e092SMaxime Ripard 				rotation |= DRM_MODE_ROTATE_270;
22001bf4e092SMaxime Ripard 				break;
22011bf4e092SMaxime Ripard 
22021bf4e092SMaxime Ripard 			default:
22031bf4e092SMaxime Ripard 				return -EINVAL;
22041bf4e092SMaxime Ripard 			}
22051bf4e092SMaxime Ripard 		} else if (!strncmp(option, "reflect_x", delim - option)) {
22061bf4e092SMaxime Ripard 			rotation |= DRM_MODE_REFLECT_X;
22071bf4e092SMaxime Ripard 		} else if (!strncmp(option, "reflect_y", delim - option)) {
22081bf4e092SMaxime Ripard 			rotation |= DRM_MODE_REFLECT_Y;
22093d46a300SMaxime Ripard 		} else if (!strncmp(option, "margin_right", delim - option)) {
22108582e244SHans de Goede 			if (drm_mode_parse_cmdline_int(delim, &margin))
22113d46a300SMaxime Ripard 				return -EINVAL;
22123d46a300SMaxime Ripard 
22133d46a300SMaxime Ripard 			mode->tv_margins.right = margin;
22143d46a300SMaxime Ripard 		} else if (!strncmp(option, "margin_left", delim - option)) {
22158582e244SHans de Goede 			if (drm_mode_parse_cmdline_int(delim, &margin))
22163d46a300SMaxime Ripard 				return -EINVAL;
22173d46a300SMaxime Ripard 
22183d46a300SMaxime Ripard 			mode->tv_margins.left = margin;
22193d46a300SMaxime Ripard 		} else if (!strncmp(option, "margin_top", delim - option)) {
22208582e244SHans de Goede 			if (drm_mode_parse_cmdline_int(delim, &margin))
22213d46a300SMaxime Ripard 				return -EINVAL;
22223d46a300SMaxime Ripard 
22233d46a300SMaxime Ripard 			mode->tv_margins.top = margin;
22243d46a300SMaxime Ripard 		} else if (!strncmp(option, "margin_bottom", delim - option)) {
22258582e244SHans de Goede 			if (drm_mode_parse_cmdline_int(delim, &margin))
22263d46a300SMaxime Ripard 				return -EINVAL;
22273d46a300SMaxime Ripard 
22283d46a300SMaxime Ripard 			mode->tv_margins.bottom = margin;
22294e7a4a6fSHans de Goede 		} else if (!strncmp(option, "panel_orientation", delim - option)) {
22304e7a4a6fSHans de Goede 			if (drm_mode_parse_panel_orientation(delim, mode))
22314e7a4a6fSHans de Goede 				return -EINVAL;
2232e691c999SMaxime Ripard 		} else if (!strncmp(option, "tv_mode", delim - option)) {
2233e691c999SMaxime Ripard 			if (drm_mode_parse_tv_mode(delim, mode))
2234e691c999SMaxime Ripard 				return -EINVAL;
22351bf4e092SMaxime Ripard 		} else {
22361bf4e092SMaxime Ripard 			return -EINVAL;
22371bf4e092SMaxime Ripard 		}
2238739b200cSHans de Goede 		sep = strchr(delim, ',');
2239739b200cSHans de Goede 		option = sep + 1;
2240739b200cSHans de Goede 	} while (sep);
22411bf4e092SMaxime Ripard 
224299e2716eSHans de Goede 	if (rotation && freestanding)
224399e2716eSHans de Goede 		return -EINVAL;
224499e2716eSHans de Goede 
2245e6980a72SStephan Gerhold 	if (!(rotation & DRM_MODE_ROTATE_MASK))
2246e6980a72SStephan Gerhold 		rotation |= DRM_MODE_ROTATE_0;
2247e6980a72SStephan Gerhold 
2248e6980a72SStephan Gerhold 	/* Make sure there is exactly one rotation defined */
2249e6980a72SStephan Gerhold 	if (!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK))
2250e6980a72SStephan Gerhold 		return -EINVAL;
2251e6980a72SStephan Gerhold 
22521bf4e092SMaxime Ripard 	mode->rotation_reflection = rotation;
22531bf4e092SMaxime Ripard 
22541bf4e092SMaxime Ripard 	return 0;
22551bf4e092SMaxime Ripard }
22561bf4e092SMaxime Ripard 
2257a7ab1553SMaxime Ripard struct drm_named_mode {
2258a7ab1553SMaxime Ripard 	const char *name;
22590e308efeSMaxime Ripard 	unsigned int pixel_clock_khz;
226000dcc4a2SMaxime Ripard 	unsigned int xres;
226100dcc4a2SMaxime Ripard 	unsigned int yres;
226200dcc4a2SMaxime Ripard 	unsigned int flags;
2263e691c999SMaxime Ripard 	unsigned int tv_mode;
2264a7ab1553SMaxime Ripard };
2265a7ab1553SMaxime Ripard 
2266e691c999SMaxime Ripard #define NAMED_MODE(_name, _pclk, _x, _y, _flags, _mode)	\
226700dcc4a2SMaxime Ripard 	{						\
226800dcc4a2SMaxime Ripard 		.name = _name,				\
22690e308efeSMaxime Ripard 		.pixel_clock_khz = _pclk,		\
227000dcc4a2SMaxime Ripard 		.xres = _x,				\
227100dcc4a2SMaxime Ripard 		.yres = _y,				\
227200dcc4a2SMaxime Ripard 		.flags = _flags,			\
2273e691c999SMaxime Ripard 		.tv_mode = _mode,			\
227400dcc4a2SMaxime Ripard 	}
227500dcc4a2SMaxime Ripard 
2276a7ab1553SMaxime Ripard static const struct drm_named_mode drm_named_modes[] = {
2277e691c999SMaxime Ripard 	NAMED_MODE("NTSC", 13500, 720, 480, DRM_MODE_FLAG_INTERLACE, DRM_MODE_TV_MODE_NTSC),
22780740ac38SMaxime Ripard 	NAMED_MODE("NTSC-J", 13500, 720, 480, DRM_MODE_FLAG_INTERLACE, DRM_MODE_TV_MODE_NTSC_J),
2279e691c999SMaxime Ripard 	NAMED_MODE("PAL", 13500, 720, 576, DRM_MODE_FLAG_INTERLACE, DRM_MODE_TV_MODE_PAL),
22800740ac38SMaxime Ripard 	NAMED_MODE("PAL-M", 13500, 720, 480, DRM_MODE_FLAG_INTERLACE, DRM_MODE_TV_MODE_PAL_M),
228137641379SMaxime Ripard };
228237641379SMaxime Ripard 
drm_mode_parse_cmdline_named_mode(const char * name,unsigned int name_end,struct drm_cmdline_mode * cmdline_mode)2283a631bf30SMaxime Ripard static int drm_mode_parse_cmdline_named_mode(const char *name,
2284a631bf30SMaxime Ripard 					     unsigned int name_end,
2285a631bf30SMaxime Ripard 					     struct drm_cmdline_mode *cmdline_mode)
2286a631bf30SMaxime Ripard {
2287a631bf30SMaxime Ripard 	unsigned int i;
2288a631bf30SMaxime Ripard 
2289a631bf30SMaxime Ripard 	if (!name_end)
2290a631bf30SMaxime Ripard 		return 0;
2291a631bf30SMaxime Ripard 
2292a631bf30SMaxime Ripard 	/* If the name starts with a digit, it's not a named mode */
2293a631bf30SMaxime Ripard 	if (isdigit(name[0]))
2294a631bf30SMaxime Ripard 		return 0;
2295a631bf30SMaxime Ripard 
2296a631bf30SMaxime Ripard 	/*
2297a631bf30SMaxime Ripard 	 * If there's an equal sign in the name, the command-line
2298a631bf30SMaxime Ripard 	 * contains only an option and no mode.
2299a631bf30SMaxime Ripard 	 */
2300a631bf30SMaxime Ripard 	if (strnchr(name, name_end, '='))
2301a631bf30SMaxime Ripard 		return 0;
2302a631bf30SMaxime Ripard 
2303a631bf30SMaxime Ripard 	/* The connection status extras can be set without a mode. */
2304a631bf30SMaxime Ripard 	if (name_end == 1 &&
2305a631bf30SMaxime Ripard 	    (name[0] == 'd' || name[0] == 'D' || name[0] == 'e'))
2306a631bf30SMaxime Ripard 		return 0;
2307a631bf30SMaxime Ripard 
2308a631bf30SMaxime Ripard 	/*
2309a631bf30SMaxime Ripard 	 * We're sure we're a named mode at this point, iterate over the
2310a631bf30SMaxime Ripard 	 * list of modes we're aware of.
2311a631bf30SMaxime Ripard 	 */
2312a7ab1553SMaxime Ripard 	for (i = 0; i < ARRAY_SIZE(drm_named_modes); i++) {
2313a7ab1553SMaxime Ripard 		const struct drm_named_mode *mode = &drm_named_modes[i];
2314a631bf30SMaxime Ripard 		int ret;
2315a631bf30SMaxime Ripard 
2316a7ab1553SMaxime Ripard 		ret = str_has_prefix(name, mode->name);
2317a631bf30SMaxime Ripard 		if (ret != name_end)
2318a631bf30SMaxime Ripard 			continue;
2319a631bf30SMaxime Ripard 
23200f9aa074SMaxime Ripard 		strscpy(cmdline_mode->name, mode->name, sizeof(cmdline_mode->name));
23210e308efeSMaxime Ripard 		cmdline_mode->pixel_clock = mode->pixel_clock_khz;
232200dcc4a2SMaxime Ripard 		cmdline_mode->xres = mode->xres;
232300dcc4a2SMaxime Ripard 		cmdline_mode->yres = mode->yres;
232400dcc4a2SMaxime Ripard 		cmdline_mode->interlace = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
2325e691c999SMaxime Ripard 		cmdline_mode->tv_mode = mode->tv_mode;
2326e691c999SMaxime Ripard 		cmdline_mode->tv_mode_specified = true;
2327a631bf30SMaxime Ripard 		cmdline_mode->specified = true;
2328a631bf30SMaxime Ripard 
2329a631bf30SMaxime Ripard 		return 1;
2330a631bf30SMaxime Ripard 	}
2331a631bf30SMaxime Ripard 
2332a631bf30SMaxime Ripard 	return -EINVAL;
2333a631bf30SMaxime Ripard }
2334a631bf30SMaxime Ripard 
23351794d257SChris Wilson /**
2336f5aabb97SDaniel Vetter  * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
2337f5aabb97SDaniel Vetter  * @mode_option: optional per connector mode option
2338f5aabb97SDaniel Vetter  * @connector: connector to parse modeline for
2339f5aabb97SDaniel Vetter  * @mode: preallocated drm_cmdline_mode structure to fill out
23401794d257SChris Wilson  *
2341f5aabb97SDaniel Vetter  * This parses @mode_option command line modeline for modes and options to
2342*1e84dadbSThomas Zimmermann  * configure the connector.
23431794d257SChris Wilson  *
2344f5aabb97SDaniel Vetter  * This uses the same parameters as the fb modedb.c, except for an extra
2345dbd124f0SDaniel Vetter  * force-enable, force-enable-digital and force-disable bit at the end::
2346f5aabb97SDaniel Vetter  *
23471794d257SChris Wilson  *	<xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
23481794d257SChris Wilson  *
23491bf4e092SMaxime Ripard  * Additionals options can be provided following the mode, using a comma to
23501bf4e092SMaxime Ripard  * separate each option. Valid options can be found in
2351bff9e34cSMauro Carvalho Chehab  * Documentation/fb/modedb.rst.
23521bf4e092SMaxime Ripard  *
2353f5aabb97SDaniel Vetter  * The intermediate drm_cmdline_mode structure is required to store additional
23542a97acd6SYannick Guerrini  * options from the command line modline like the force-enable/disable flag.
2355f5aabb97SDaniel Vetter  *
2356f5aabb97SDaniel Vetter  * Returns:
2357f5aabb97SDaniel Vetter  * True if a valid modeline has been parsed, false otherwise.
23581794d257SChris Wilson  */
drm_mode_parse_command_line_for_connector(const char * mode_option,const struct drm_connector * connector,struct drm_cmdline_mode * mode)23591794d257SChris Wilson bool drm_mode_parse_command_line_for_connector(const char *mode_option,
2360c0898fcaSArnd Bergmann 					       const struct drm_connector *connector,
23611794d257SChris Wilson 					       struct drm_cmdline_mode *mode)
23621794d257SChris Wilson {
23631794d257SChris Wilson 	const char *name;
23647b1cce76SHans de Goede 	bool freestanding = false, parse_extras = false;
23651bf4e092SMaxime Ripard 	unsigned int bpp_off = 0, refresh_off = 0, options_off = 0;
2366e08ab74bSMaxime Ripard 	unsigned int mode_end = 0;
236783e14ea3SHans de Goede 	const char *bpp_ptr = NULL, *refresh_ptr = NULL, *extra_ptr = NULL;
236883e14ea3SHans de Goede 	const char *options_ptr = NULL;
2369e08ab74bSMaxime Ripard 	char *bpp_end_ptr = NULL, *refresh_end_ptr = NULL;
2370a631bf30SMaxime Ripard 	int len, ret;
23711794d257SChris Wilson 
2372d1fe276bSHans de Goede 	memset(mode, 0, sizeof(*mode));
23734e7a4a6fSHans de Goede 	mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
23744e7a4a6fSHans de Goede 
2375d1fe276bSHans de Goede 	if (!mode_option)
23761794d257SChris Wilson 		return false;
23771794d257SChris Wilson 
23781794d257SChris Wilson 	name = mode_option;
237904fee895SRolf Eike Beer 
23801bf4e092SMaxime Ripard 	/* Locate the start of named options */
23811bf4e092SMaxime Ripard 	options_ptr = strchr(name, ',');
23821bf4e092SMaxime Ripard 	if (options_ptr)
23831bf4e092SMaxime Ripard 		options_off = options_ptr - name;
238490c258baSMaxime Ripard 	else
238590c258baSMaxime Ripard 		options_off = strlen(name);
238690c258baSMaxime Ripard 
238790c258baSMaxime Ripard 	/* Try to locate the bpp and refresh specifiers, if any */
238890c258baSMaxime Ripard 	bpp_ptr = strnchr(name, options_off, '-');
23898b6e28eaSGeert Uytterhoeven 	while (bpp_ptr && !isdigit(bpp_ptr[1]))
23908b6e28eaSGeert Uytterhoeven 		bpp_ptr = strnchr(bpp_ptr + 1, options_off, '-');
239190c258baSMaxime Ripard 	if (bpp_ptr)
239290c258baSMaxime Ripard 		bpp_off = bpp_ptr - name;
239390c258baSMaxime Ripard 
239490c258baSMaxime Ripard 	refresh_ptr = strnchr(name, options_off, '@');
239590c258baSMaxime Ripard 	if (refresh_ptr)
239690c258baSMaxime Ripard 		refresh_off = refresh_ptr - name;
23971bf4e092SMaxime Ripard 
2398e08ab74bSMaxime Ripard 	/* Locate the end of the name / resolution, and parse it */
23991bf4e092SMaxime Ripard 	if (bpp_ptr) {
2400e08ab74bSMaxime Ripard 		mode_end = bpp_off;
2401e08ab74bSMaxime Ripard 	} else if (refresh_ptr) {
2402e08ab74bSMaxime Ripard 		mode_end = refresh_off;
24031bf4e092SMaxime Ripard 	} else if (options_ptr) {
24041bf4e092SMaxime Ripard 		mode_end = options_off;
2405cfb0881bSHans de Goede 		parse_extras = true;
2406e08ab74bSMaxime Ripard 	} else {
2407e08ab74bSMaxime Ripard 		mode_end = strlen(name);
2408e08ab74bSMaxime Ripard 		parse_extras = true;
2409e08ab74bSMaxime Ripard 	}
2410e08ab74bSMaxime Ripard 
2411a631bf30SMaxime Ripard 	if (!mode_end)
2412a631bf30SMaxime Ripard 		return false;
241337641379SMaxime Ripard 
2414a631bf30SMaxime Ripard 	ret = drm_mode_parse_cmdline_named_mode(name, mode_end, mode);
2415a631bf30SMaxime Ripard 	if (ret < 0)
2416a631bf30SMaxime Ripard 		return false;
2417a631bf30SMaxime Ripard 
2418a631bf30SMaxime Ripard 	/*
2419a631bf30SMaxime Ripard 	 * Having a mode that starts by a letter (and thus is named) and
2420a631bf30SMaxime Ripard 	 * an at-sign (used to specify a refresh rate) is disallowed.
2421a631bf30SMaxime Ripard 	 */
2422a631bf30SMaxime Ripard 	if (ret && refresh_ptr)
2423a631bf30SMaxime Ripard 		return false;
242437641379SMaxime Ripard 
24257b1cce76SHans de Goede 	/* No named mode? Check for a normal mode argument, e.g. 1024x768 */
24267b1cce76SHans de Goede 	if (!mode->specified && isdigit(name[0])) {
2427e08ab74bSMaxime Ripard 		ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
2428e08ab74bSMaxime Ripard 						      parse_extras,
2429e08ab74bSMaxime Ripard 						      connector,
2430e08ab74bSMaxime Ripard 						      mode);
2431e08ab74bSMaxime Ripard 		if (ret)
2432e08ab74bSMaxime Ripard 			return false;
24337b1cce76SHans de Goede 
2434e08ab74bSMaxime Ripard 		mode->specified = true;
24357b1cce76SHans de Goede 	}
24367b1cce76SHans de Goede 
24377b1cce76SHans de Goede 	/* No mode? Check for freestanding extras and/or options */
24387b1cce76SHans de Goede 	if (!mode->specified) {
24397b1cce76SHans de Goede 		unsigned int len = strlen(mode_option);
24407b1cce76SHans de Goede 
24417b1cce76SHans de Goede 		if (bpp_ptr || refresh_ptr)
24427b1cce76SHans de Goede 			return false; /* syntax error */
24437b1cce76SHans de Goede 
24447b1cce76SHans de Goede 		if (len == 1 || (len >= 2 && mode_option[1] == ','))
24457b1cce76SHans de Goede 			extra_ptr = mode_option;
24467b1cce76SHans de Goede 		else
24477b1cce76SHans de Goede 			options_ptr = mode_option - 1;
24487b1cce76SHans de Goede 
24497b1cce76SHans de Goede 		freestanding = true;
24507b1cce76SHans de Goede 	}
2451e08ab74bSMaxime Ripard 
2452e08ab74bSMaxime Ripard 	if (bpp_ptr) {
2453e08ab74bSMaxime Ripard 		ret = drm_mode_parse_cmdline_bpp(bpp_ptr, &bpp_end_ptr, mode);
2454e08ab74bSMaxime Ripard 		if (ret)
245504fee895SRolf Eike Beer 			return false;
24566a2d1637SHans de Goede 
24576a2d1637SHans de Goede 		mode->bpp_specified = true;
245804fee895SRolf Eike Beer 	}
245904fee895SRolf Eike Beer 
2460e08ab74bSMaxime Ripard 	if (refresh_ptr) {
2461e08ab74bSMaxime Ripard 		ret = drm_mode_parse_cmdline_refresh(refresh_ptr,
2462e08ab74bSMaxime Ripard 						     &refresh_end_ptr, mode);
2463e08ab74bSMaxime Ripard 		if (ret)
2464e08ab74bSMaxime Ripard 			return false;
24656a2d1637SHans de Goede 
24666a2d1637SHans de Goede 		mode->refresh_specified = true;
24671794d257SChris Wilson 	}
24681794d257SChris Wilson 
2469e08ab74bSMaxime Ripard 	/*
2470e08ab74bSMaxime Ripard 	 * Locate the end of the bpp / refresh, and parse the extras
2471e08ab74bSMaxime Ripard 	 * if relevant
2472e08ab74bSMaxime Ripard 	 */
2473e08ab74bSMaxime Ripard 	if (bpp_ptr && refresh_ptr)
2474e08ab74bSMaxime Ripard 		extra_ptr = max(bpp_end_ptr, refresh_end_ptr);
2475e08ab74bSMaxime Ripard 	else if (bpp_ptr)
2476e08ab74bSMaxime Ripard 		extra_ptr = bpp_end_ptr;
2477e08ab74bSMaxime Ripard 	else if (refresh_ptr)
2478e08ab74bSMaxime Ripard 		extra_ptr = refresh_end_ptr;
24791794d257SChris Wilson 
2480c2ed3e94SHans de Goede 	if (extra_ptr) {
2481c2ed3e94SHans de Goede 		if (options_ptr)
2482c2ed3e94SHans de Goede 			len = options_ptr - extra_ptr;
2483c2ed3e94SHans de Goede 		else
2484c2ed3e94SHans de Goede 			len = strlen(extra_ptr);
24853aeeb13dSMaxime Ripard 
24867b1cce76SHans de Goede 		ret = drm_mode_parse_cmdline_extra(extra_ptr, len, freestanding,
24873aeeb13dSMaxime Ripard 						   connector, mode);
24883aeeb13dSMaxime Ripard 		if (ret)
24893aeeb13dSMaxime Ripard 			return false;
24901794d257SChris Wilson 	}
24911bf4e092SMaxime Ripard 
24921bf4e092SMaxime Ripard 	if (options_ptr) {
2493739b200cSHans de Goede 		ret = drm_mode_parse_cmdline_options(options_ptr + 1,
24947b1cce76SHans de Goede 						     freestanding,
24951bf4e092SMaxime Ripard 						     connector, mode);
24961bf4e092SMaxime Ripard 		if (ret)
24971bf4e092SMaxime Ripard 			return false;
24983aeeb13dSMaxime Ripard 	}
24991794d257SChris Wilson 
25001794d257SChris Wilson 	return true;
25011794d257SChris Wilson }
25021794d257SChris Wilson EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
25031794d257SChris Wilson 
drm_named_mode(struct drm_device * dev,struct drm_cmdline_mode * cmd)2504fedcaf72SMaxime Ripard static struct drm_display_mode *drm_named_mode(struct drm_device *dev,
2505fedcaf72SMaxime Ripard 					       struct drm_cmdline_mode *cmd)
2506fedcaf72SMaxime Ripard {
2507fedcaf72SMaxime Ripard 	unsigned int i;
2508fedcaf72SMaxime Ripard 
2509fedcaf72SMaxime Ripard 	for (i = 0; i < ARRAY_SIZE(drm_named_modes); i++) {
2510fedcaf72SMaxime Ripard 		const struct drm_named_mode *named_mode = &drm_named_modes[i];
2511fedcaf72SMaxime Ripard 
2512fedcaf72SMaxime Ripard 		if (strcmp(cmd->name, named_mode->name))
2513fedcaf72SMaxime Ripard 			continue;
2514fedcaf72SMaxime Ripard 
2515fedcaf72SMaxime Ripard 		if (!cmd->tv_mode_specified)
2516fedcaf72SMaxime Ripard 			continue;
2517fedcaf72SMaxime Ripard 
2518fedcaf72SMaxime Ripard 		return drm_analog_tv_mode(dev,
2519fedcaf72SMaxime Ripard 					  named_mode->tv_mode,
2520fedcaf72SMaxime Ripard 					  named_mode->pixel_clock_khz * 1000,
2521fedcaf72SMaxime Ripard 					  named_mode->xres,
2522fedcaf72SMaxime Ripard 					  named_mode->yres,
2523fedcaf72SMaxime Ripard 					  named_mode->flags & DRM_MODE_FLAG_INTERLACE);
2524fedcaf72SMaxime Ripard 	}
2525fedcaf72SMaxime Ripard 
2526fedcaf72SMaxime Ripard 	return NULL;
2527fedcaf72SMaxime Ripard }
2528fedcaf72SMaxime Ripard 
2529f5aabb97SDaniel Vetter /**
2530f5aabb97SDaniel Vetter  * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
2531f5aabb97SDaniel Vetter  * @dev: DRM device to create the new mode for
2532f5aabb97SDaniel Vetter  * @cmd: input command line modeline
2533f5aabb97SDaniel Vetter  *
2534f5aabb97SDaniel Vetter  * Returns:
2535f5aabb97SDaniel Vetter  * Pointer to converted mode on success, NULL on error.
2536f5aabb97SDaniel Vetter  */
25371794d257SChris Wilson struct drm_display_mode *
drm_mode_create_from_cmdline_mode(struct drm_device * dev,struct drm_cmdline_mode * cmd)25381794d257SChris Wilson drm_mode_create_from_cmdline_mode(struct drm_device *dev,
25391794d257SChris Wilson 				  struct drm_cmdline_mode *cmd)
25401794d257SChris Wilson {
25411794d257SChris Wilson 	struct drm_display_mode *mode;
25421794d257SChris Wilson 
254319a9a0efSVille Syrjälä 	if (cmd->xres == 0 || cmd->yres == 0)
254419a9a0efSVille Syrjälä 		return NULL;
254519a9a0efSVille Syrjälä 
2546fedcaf72SMaxime Ripard 	if (strlen(cmd->name))
2547fedcaf72SMaxime Ripard 		mode = drm_named_mode(dev, cmd);
2548fedcaf72SMaxime Ripard 	else if (cmd->cvt)
25491794d257SChris Wilson 		mode = drm_cvt_mode(dev,
25501794d257SChris Wilson 				    cmd->xres, cmd->yres,
25511794d257SChris Wilson 				    cmd->refresh_specified ? cmd->refresh : 60,
25521794d257SChris Wilson 				    cmd->rb, cmd->interlace,
25531794d257SChris Wilson 				    cmd->margins);
25541794d257SChris Wilson 	else
25551794d257SChris Wilson 		mode = drm_gtf_mode(dev,
25561794d257SChris Wilson 				    cmd->xres, cmd->yres,
25571794d257SChris Wilson 				    cmd->refresh_specified ? cmd->refresh : 60,
25581794d257SChris Wilson 				    cmd->interlace,
25591794d257SChris Wilson 				    cmd->margins);
25601794d257SChris Wilson 	if (!mode)
25611794d257SChris Wilson 		return NULL;
25621794d257SChris Wilson 
2563eaf99c74SChris Wilson 	mode->type |= DRM_MODE_TYPE_USERDEF;
2564fdf35a6bSTakashi Iwai 	/* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
2565969218feSTakashi Iwai 	if (cmd->xres == 1366)
2566969218feSTakashi Iwai 		drm_mode_fixup_1366x768(mode);
25671794d257SChris Wilson 	drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
25681794d257SChris Wilson 	return mode;
25691794d257SChris Wilson }
25701794d257SChris Wilson EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
2571934a8a89SDaniel Stone 
2572934a8a89SDaniel Stone /**
2573e9d2871fSMauro Carvalho Chehab  * drm_mode_convert_to_umode - convert a drm_display_mode into a modeinfo
2574934a8a89SDaniel Stone  * @out: drm_mode_modeinfo struct to return to the user
2575934a8a89SDaniel Stone  * @in: drm_display_mode to use
2576934a8a89SDaniel Stone  *
2577934a8a89SDaniel Stone  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
2578934a8a89SDaniel Stone  * the user.
2579934a8a89SDaniel Stone  */
drm_mode_convert_to_umode(struct drm_mode_modeinfo * out,const struct drm_display_mode * in)2580934a8a89SDaniel Stone void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
2581934a8a89SDaniel Stone 			       const struct drm_display_mode *in)
2582934a8a89SDaniel Stone {
2583934a8a89SDaniel Stone 	out->clock = in->clock;
2584934a8a89SDaniel Stone 	out->hdisplay = in->hdisplay;
2585934a8a89SDaniel Stone 	out->hsync_start = in->hsync_start;
2586934a8a89SDaniel Stone 	out->hsync_end = in->hsync_end;
2587934a8a89SDaniel Stone 	out->htotal = in->htotal;
2588934a8a89SDaniel Stone 	out->hskew = in->hskew;
2589934a8a89SDaniel Stone 	out->vdisplay = in->vdisplay;
2590934a8a89SDaniel Stone 	out->vsync_start = in->vsync_start;
2591934a8a89SDaniel Stone 	out->vsync_end = in->vsync_end;
2592934a8a89SDaniel Stone 	out->vtotal = in->vtotal;
2593934a8a89SDaniel Stone 	out->vscan = in->vscan;
25940425662fSVille Syrjälä 	out->vrefresh = drm_mode_vrefresh(in);
2595934a8a89SDaniel Stone 	out->flags = in->flags;
2596934a8a89SDaniel Stone 	out->type = in->type;
2597222ec161SShashank Sharma 
2598222ec161SShashank Sharma 	switch (in->picture_aspect_ratio) {
2599222ec161SShashank Sharma 	case HDMI_PICTURE_ASPECT_4_3:
2600222ec161SShashank Sharma 		out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
2601222ec161SShashank Sharma 		break;
2602222ec161SShashank Sharma 	case HDMI_PICTURE_ASPECT_16_9:
2603222ec161SShashank Sharma 		out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
2604222ec161SShashank Sharma 		break;
2605900aa8adSShashank Sharma 	case HDMI_PICTURE_ASPECT_64_27:
2606900aa8adSShashank Sharma 		out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
2607900aa8adSShashank Sharma 		break;
2608900aa8adSShashank Sharma 	case HDMI_PICTURE_ASPECT_256_135:
2609900aa8adSShashank Sharma 		out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
2610900aa8adSShashank Sharma 		break;
2611222ec161SShashank Sharma 	default:
26129f396ab4SVille Syrjälä 		WARN(1, "Invalid aspect ratio (0%x) on mode\n",
26139f396ab4SVille Syrjälä 		     in->picture_aspect_ratio);
2614df561f66SGustavo A. R. Silva 		fallthrough;
26159f396ab4SVille Syrjälä 	case HDMI_PICTURE_ASPECT_NONE:
2616222ec161SShashank Sharma 		out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
2617222ec161SShashank Sharma 		break;
2618222ec161SShashank Sharma 	}
2619222ec161SShashank Sharma 
2620934a8a89SDaniel Stone 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
2621934a8a89SDaniel Stone 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
2622934a8a89SDaniel Stone }
2623934a8a89SDaniel Stone 
2624934a8a89SDaniel Stone /**
2625e9d2871fSMauro Carvalho Chehab  * drm_mode_convert_umode - convert a modeinfo into a drm_display_mode
262675a655e0SVille Syrjälä  * @dev: drm device
2627934a8a89SDaniel Stone  * @out: drm_display_mode to return to the user
2628934a8a89SDaniel Stone  * @in: drm_mode_modeinfo to use
2629934a8a89SDaniel Stone  *
2630934a8a89SDaniel Stone  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
2631934a8a89SDaniel Stone  * the caller.
2632934a8a89SDaniel Stone  *
2633934a8a89SDaniel Stone  * Returns:
2634934a8a89SDaniel Stone  * Zero on success, negative errno on failure.
2635934a8a89SDaniel Stone  */
drm_mode_convert_umode(struct drm_device * dev,struct drm_display_mode * out,const struct drm_mode_modeinfo * in)263675a655e0SVille Syrjälä int drm_mode_convert_umode(struct drm_device *dev,
263775a655e0SVille Syrjälä 			   struct drm_display_mode *out,
2638934a8a89SDaniel Stone 			   const struct drm_mode_modeinfo *in)
2639934a8a89SDaniel Stone {
2640df550548SVille Syrjälä 	if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
2641df550548SVille Syrjälä 		return -ERANGE;
2642934a8a89SDaniel Stone 
2643934a8a89SDaniel Stone 	out->clock = in->clock;
2644934a8a89SDaniel Stone 	out->hdisplay = in->hdisplay;
2645934a8a89SDaniel Stone 	out->hsync_start = in->hsync_start;
2646934a8a89SDaniel Stone 	out->hsync_end = in->hsync_end;
2647934a8a89SDaniel Stone 	out->htotal = in->htotal;
2648934a8a89SDaniel Stone 	out->hskew = in->hskew;
2649934a8a89SDaniel Stone 	out->vdisplay = in->vdisplay;
2650934a8a89SDaniel Stone 	out->vsync_start = in->vsync_start;
2651934a8a89SDaniel Stone 	out->vsync_end = in->vsync_end;
2652934a8a89SDaniel Stone 	out->vtotal = in->vtotal;
2653934a8a89SDaniel Stone 	out->vscan = in->vscan;
2654934a8a89SDaniel Stone 	out->flags = in->flags;
2655a01c4773SVille Syrjälä 	/*
2656a01c4773SVille Syrjälä 	 * Old xf86-video-vmware (possibly others too) used to
26570ae865efSCai Huoqing 	 * leave 'type' uninitialized. Just ignore any bits we
2658a01c4773SVille Syrjälä 	 * don't like. It's a just hint after all, and more
2659a01c4773SVille Syrjälä 	 * useful for the kernel->userspace direction anyway.
2660a01c4773SVille Syrjälä 	 */
2661a01c4773SVille Syrjälä 	out->type = in->type & DRM_MODE_TYPE_ALL;
2662934a8a89SDaniel Stone 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
2663934a8a89SDaniel Stone 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
2664934a8a89SDaniel Stone 
2665222ec161SShashank Sharma 	/* Clearing picture aspect ratio bits from out flags,
2666222ec161SShashank Sharma 	 * as the aspect-ratio information is not stored in
2667222ec161SShashank Sharma 	 * flags for kernel-mode, but in picture_aspect_ratio.
2668222ec161SShashank Sharma 	 */
2669222ec161SShashank Sharma 	out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
2670222ec161SShashank Sharma 
2671222ec161SShashank Sharma 	switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
2672222ec161SShashank Sharma 	case DRM_MODE_FLAG_PIC_AR_4_3:
2673700710e9SVille Syrjälä 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3;
2674222ec161SShashank Sharma 		break;
2675222ec161SShashank Sharma 	case DRM_MODE_FLAG_PIC_AR_16_9:
2676700710e9SVille Syrjälä 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9;
2677222ec161SShashank Sharma 		break;
2678900aa8adSShashank Sharma 	case DRM_MODE_FLAG_PIC_AR_64_27:
2679700710e9SVille Syrjälä 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27;
2680900aa8adSShashank Sharma 		break;
2681900aa8adSShashank Sharma 	case DRM_MODE_FLAG_PIC_AR_256_135:
2682700710e9SVille Syrjälä 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135;
2683900aa8adSShashank Sharma 		break;
2684993a8067SVille Syrjälä 	case DRM_MODE_FLAG_PIC_AR_NONE:
2685222ec161SShashank Sharma 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
2686222ec161SShashank Sharma 		break;
2687993a8067SVille Syrjälä 	default:
2688993a8067SVille Syrjälä 		return -EINVAL;
2689222ec161SShashank Sharma 	}
2690222ec161SShashank Sharma 
269175a655e0SVille Syrjälä 	out->status = drm_mode_validate_driver(dev, out);
2692934a8a89SDaniel Stone 	if (out->status != MODE_OK)
2693df550548SVille Syrjälä 		return -EINVAL;
2694934a8a89SDaniel Stone 
2695b201e743STomi Valkeinen 	drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
2696b201e743STomi Valkeinen 
2697df550548SVille Syrjälä 	return 0;
2698934a8a89SDaniel Stone }
26992570fe25SShashank Sharma 
27002570fe25SShashank Sharma /**
27012570fe25SShashank Sharma  * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
27022570fe25SShashank Sharma  * output format
27032570fe25SShashank Sharma  *
27048d0873a2SSean Paul  * @display: display under action
27052570fe25SShashank Sharma  * @mode: video mode to be tested.
27062570fe25SShashank Sharma  *
27072570fe25SShashank Sharma  * Returns:
27082570fe25SShashank Sharma  * true if the mode can be supported in YCBCR420 format
27092570fe25SShashank Sharma  * false if not.
27102570fe25SShashank Sharma  */
drm_mode_is_420_only(const struct drm_display_info * display,const struct drm_display_mode * mode)27112570fe25SShashank Sharma bool drm_mode_is_420_only(const struct drm_display_info *display,
27122570fe25SShashank Sharma 			  const struct drm_display_mode *mode)
27132570fe25SShashank Sharma {
27142570fe25SShashank Sharma 	u8 vic = drm_match_cea_mode(mode);
27152570fe25SShashank Sharma 
27162570fe25SShashank Sharma 	return test_bit(vic, display->hdmi.y420_vdb_modes);
27172570fe25SShashank Sharma }
27182570fe25SShashank Sharma EXPORT_SYMBOL(drm_mode_is_420_only);
27192570fe25SShashank Sharma 
27202570fe25SShashank Sharma /**
27212570fe25SShashank Sharma  * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
27222570fe25SShashank Sharma  * output format also (along with RGB/YCBCR444/422)
27232570fe25SShashank Sharma  *
27242570fe25SShashank Sharma  * @display: display under action.
27252570fe25SShashank Sharma  * @mode: video mode to be tested.
27262570fe25SShashank Sharma  *
27272570fe25SShashank Sharma  * Returns:
27282570fe25SShashank Sharma  * true if the mode can be support YCBCR420 format
27292570fe25SShashank Sharma  * false if not.
27302570fe25SShashank Sharma  */
drm_mode_is_420_also(const struct drm_display_info * display,const struct drm_display_mode * mode)27312570fe25SShashank Sharma bool drm_mode_is_420_also(const struct drm_display_info *display,
27322570fe25SShashank Sharma 			  const struct drm_display_mode *mode)
27332570fe25SShashank Sharma {
27342570fe25SShashank Sharma 	u8 vic = drm_match_cea_mode(mode);
27352570fe25SShashank Sharma 
27362570fe25SShashank Sharma 	return test_bit(vic, display->hdmi.y420_cmdb_modes);
27372570fe25SShashank Sharma }
27382570fe25SShashank Sharma EXPORT_SYMBOL(drm_mode_is_420_also);
27392570fe25SShashank Sharma /**
27402570fe25SShashank Sharma  * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
27412570fe25SShashank Sharma  * output format
27422570fe25SShashank Sharma  *
27432570fe25SShashank Sharma  * @display: display under action.
27442570fe25SShashank Sharma  * @mode: video mode to be tested.
27452570fe25SShashank Sharma  *
27462570fe25SShashank Sharma  * Returns:
27472570fe25SShashank Sharma  * true if the mode can be supported in YCBCR420 format
27482570fe25SShashank Sharma  * false if not.
27492570fe25SShashank Sharma  */
drm_mode_is_420(const struct drm_display_info * display,const struct drm_display_mode * mode)27502570fe25SShashank Sharma bool drm_mode_is_420(const struct drm_display_info *display,
27512570fe25SShashank Sharma 		     const struct drm_display_mode *mode)
27522570fe25SShashank Sharma {
27532570fe25SShashank Sharma 	return drm_mode_is_420_only(display, mode) ||
27542570fe25SShashank Sharma 		drm_mode_is_420_also(display, mode);
27552570fe25SShashank Sharma }
27562570fe25SShashank Sharma EXPORT_SYMBOL(drm_mode_is_420);
2757