Home | History | Annotate | Line # | Download | only in drm
      1  1.11  riastrad /*	$NetBSD: drm_modes.c,v 1.11 2021/12/19 01:13:59 riastradh Exp $	*/
      2   1.7  riastrad 
      3   1.1  riastrad /*
      4   1.1  riastrad  * Copyright  1997-2003 by The XFree86 Project, Inc.
      5   1.1  riastrad  * Copyright  2007 Dave Airlie
      6   1.1  riastrad  * Copyright  2007-2008 Intel Corporation
      7   1.1  riastrad  *   Jesse Barnes <jesse.barnes (at) intel.com>
      8   1.1  riastrad  * Copyright 2005-2006 Luc Verhaegen
      9   1.1  riastrad  * Copyright (c) 2001, Andy Ritger  aritger (at) nvidia.com
     10   1.1  riastrad  *
     11   1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     12   1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     13   1.1  riastrad  * to deal in the Software without restriction, including without limitation
     14   1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     15   1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     16   1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     17   1.1  riastrad  *
     18   1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     19   1.1  riastrad  * all copies or substantial portions of the Software.
     20   1.1  riastrad  *
     21   1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     22   1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     23   1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     24   1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     25   1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     26   1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     27   1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     28   1.1  riastrad  *
     29   1.1  riastrad  * Except as contained in this notice, the name of the copyright holder(s)
     30   1.1  riastrad  * and author(s) shall not be used in advertising or otherwise to promote
     31   1.1  riastrad  * the sale, use or other dealings in this Software without prior written
     32   1.1  riastrad  * authorization from the copyright holder(s) and author(s).
     33   1.1  riastrad  */
     34   1.1  riastrad 
     35   1.7  riastrad #include <sys/cdefs.h>
     36  1.11  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_modes.c,v 1.11 2021/12/19 01:13:59 riastradh Exp $");
     37   1.7  riastrad 
     38  1.10  riastrad #include <linux/ctype.h>
     39   1.1  riastrad #include <linux/list.h>
     40   1.1  riastrad #include <linux/list_sort.h>
     41   1.1  riastrad #include <linux/export.h>
     42  1.11  riastrad #include <asm/div64.h>
     43  1.10  riastrad 
     44   1.5  riastrad #ifdef CONFIG_VIDEOMODE_HELPERS
     45   1.5  riastrad #ifdef CONFIG_OF
     46   1.4  riastrad #include <video/of_videomode.h>
     47   1.5  riastrad #endif
     48   1.4  riastrad #include <video/videomode.h>
     49   1.5  riastrad #endif
     50  1.10  riastrad 
     51  1.10  riastrad #include <drm/drm_crtc.h>
     52  1.10  riastrad #include <drm/drm_device.h>
     53   1.4  riastrad #include <drm/drm_modes.h>
     54  1.10  riastrad #include <drm/drm_print.h>
     55   1.4  riastrad 
     56   1.4  riastrad #include "drm_crtc_internal.h"
     57   1.1  riastrad 
     58   1.1  riastrad /**
     59   1.4  riastrad  * drm_mode_debug_printmodeline - print a mode to dmesg
     60   1.1  riastrad  * @mode: mode to print
     61   1.1  riastrad  *
     62   1.1  riastrad  * Describe @mode using DRM_DEBUG.
     63   1.1  riastrad  */
     64   1.1  riastrad void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
     65   1.1  riastrad {
     66  1.10  riastrad 	DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
     67   1.1  riastrad }
     68   1.1  riastrad EXPORT_SYMBOL(drm_mode_debug_printmodeline);
     69   1.1  riastrad 
     70   1.1  riastrad /**
     71   1.4  riastrad  * drm_mode_create - create a new display mode
     72   1.4  riastrad  * @dev: DRM device
     73   1.4  riastrad  *
     74   1.4  riastrad  * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
     75   1.4  riastrad  * and return it.
     76   1.4  riastrad  *
     77   1.4  riastrad  * Returns:
     78   1.4  riastrad  * Pointer to new mode on success, NULL on error.
     79   1.4  riastrad  */
     80   1.4  riastrad struct drm_display_mode *drm_mode_create(struct drm_device *dev)
     81   1.4  riastrad {
     82   1.4  riastrad 	struct drm_display_mode *nmode;
     83   1.4  riastrad 
     84   1.4  riastrad 	nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
     85   1.4  riastrad 	if (!nmode)
     86   1.4  riastrad 		return NULL;
     87   1.4  riastrad 
     88   1.4  riastrad 	return nmode;
     89   1.4  riastrad }
     90   1.4  riastrad EXPORT_SYMBOL(drm_mode_create);
     91   1.4  riastrad 
     92   1.4  riastrad /**
     93   1.4  riastrad  * drm_mode_destroy - remove a mode
     94   1.1  riastrad  * @dev: DRM device
     95   1.4  riastrad  * @mode: mode to remove
     96   1.4  riastrad  *
     97   1.4  riastrad  * Release @mode's unique ID, then free it @mode structure itself using kfree.
     98   1.4  riastrad  */
     99   1.4  riastrad void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
    100   1.4  riastrad {
    101   1.4  riastrad 	if (!mode)
    102   1.4  riastrad 		return;
    103   1.4  riastrad 
    104   1.4  riastrad 	kfree(mode);
    105   1.4  riastrad }
    106   1.4  riastrad EXPORT_SYMBOL(drm_mode_destroy);
    107   1.4  riastrad 
    108   1.4  riastrad /**
    109   1.4  riastrad  * drm_mode_probed_add - add a mode to a connector's probed_mode list
    110   1.4  riastrad  * @connector: connector the new mode
    111   1.4  riastrad  * @mode: mode data
    112   1.4  riastrad  *
    113   1.4  riastrad  * Add @mode to @connector's probed_mode list for later use. This list should
    114   1.4  riastrad  * then in a second step get filtered and all the modes actually supported by
    115   1.4  riastrad  * the hardware moved to the @connector's modes list.
    116   1.4  riastrad  */
    117   1.4  riastrad void drm_mode_probed_add(struct drm_connector *connector,
    118   1.4  riastrad 			 struct drm_display_mode *mode)
    119   1.4  riastrad {
    120   1.4  riastrad 	WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
    121   1.4  riastrad 
    122   1.4  riastrad 	list_add_tail(&mode->head, &connector->probed_modes);
    123   1.4  riastrad }
    124   1.4  riastrad EXPORT_SYMBOL(drm_mode_probed_add);
    125   1.4  riastrad 
    126   1.4  riastrad /**
    127   1.4  riastrad  * drm_cvt_mode -create a modeline based on the CVT algorithm
    128   1.4  riastrad  * @dev: drm device
    129   1.1  riastrad  * @hdisplay: hdisplay size
    130   1.1  riastrad  * @vdisplay: vdisplay size
    131   1.4  riastrad  * @vrefresh: vrefresh rate
    132   1.4  riastrad  * @reduced: whether to use reduced blanking
    133   1.4  riastrad  * @interlaced: whether to compute an interlaced mode
    134   1.4  riastrad  * @margins: whether to add margins (borders)
    135   1.1  riastrad  *
    136   1.1  riastrad  * This function is called to generate the modeline based on CVT algorithm
    137   1.1  riastrad  * according to the hdisplay, vdisplay, vrefresh.
    138   1.1  riastrad  * It is based from the VESA(TM) Coordinated Video Timing Generator by
    139   1.1  riastrad  * Graham Loveridge April 9, 2003 available at
    140   1.1  riastrad  * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
    141   1.1  riastrad  *
    142   1.1  riastrad  * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
    143   1.1  riastrad  * What I have done is to translate it by using integer calculation.
    144   1.4  riastrad  *
    145   1.4  riastrad  * Returns:
    146   1.4  riastrad  * The modeline based on the CVT algorithm stored in a drm_display_mode object.
    147   1.4  riastrad  * The display mode object is allocated with drm_mode_create(). Returns NULL
    148   1.4  riastrad  * when no mode could be allocated.
    149   1.1  riastrad  */
    150   1.1  riastrad struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
    151   1.1  riastrad 				      int vdisplay, int vrefresh,
    152   1.1  riastrad 				      bool reduced, bool interlaced, bool margins)
    153   1.1  riastrad {
    154   1.4  riastrad #define HV_FACTOR			1000
    155   1.1  riastrad 	/* 1) top/bottom margin size (% of height) - default: 1.8, */
    156   1.1  riastrad #define	CVT_MARGIN_PERCENTAGE		18
    157   1.1  riastrad 	/* 2) character cell horizontal granularity (pixels) - default 8 */
    158   1.1  riastrad #define	CVT_H_GRANULARITY		8
    159   1.1  riastrad 	/* 3) Minimum vertical porch (lines) - default 3 */
    160   1.1  riastrad #define	CVT_MIN_V_PORCH			3
    161   1.1  riastrad 	/* 4) Minimum number of vertical back porch lines - default 6 */
    162   1.1  riastrad #define	CVT_MIN_V_BPORCH		6
    163   1.1  riastrad 	/* Pixel Clock step (kHz) */
    164   1.1  riastrad #define CVT_CLOCK_STEP			250
    165   1.1  riastrad 	struct drm_display_mode *drm_mode;
    166   1.1  riastrad 	unsigned int vfieldrate, hperiod;
    167   1.1  riastrad 	int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
    168   1.1  riastrad 	int interlace;
    169  1.10  riastrad 	u64 tmp;
    170  1.10  riastrad 
    171  1.10  riastrad 	if (!hdisplay || !vdisplay)
    172  1.10  riastrad 		return NULL;
    173   1.1  riastrad 
    174   1.1  riastrad 	/* allocate the drm_display_mode structure. If failure, we will
    175   1.1  riastrad 	 * return directly
    176   1.1  riastrad 	 */
    177   1.1  riastrad 	drm_mode = drm_mode_create(dev);
    178   1.1  riastrad 	if (!drm_mode)
    179   1.1  riastrad 		return NULL;
    180   1.1  riastrad 
    181   1.1  riastrad 	/* the CVT default refresh rate is 60Hz */
    182   1.1  riastrad 	if (!vrefresh)
    183   1.1  riastrad 		vrefresh = 60;
    184   1.1  riastrad 
    185   1.1  riastrad 	/* the required field fresh rate */
    186   1.1  riastrad 	if (interlaced)
    187   1.1  riastrad 		vfieldrate = vrefresh * 2;
    188   1.1  riastrad 	else
    189   1.1  riastrad 		vfieldrate = vrefresh;
    190   1.1  riastrad 
    191   1.1  riastrad 	/* horizontal pixels */
    192   1.1  riastrad 	hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
    193   1.1  riastrad 
    194   1.1  riastrad 	/* determine the left&right borders */
    195   1.1  riastrad 	hmargin = 0;
    196   1.1  riastrad 	if (margins) {
    197   1.1  riastrad 		hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
    198   1.1  riastrad 		hmargin -= hmargin % CVT_H_GRANULARITY;
    199   1.1  riastrad 	}
    200   1.1  riastrad 	/* find the total active pixels */
    201   1.1  riastrad 	drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
    202   1.1  riastrad 
    203   1.1  riastrad 	/* find the number of lines per field */
    204   1.1  riastrad 	if (interlaced)
    205   1.1  riastrad 		vdisplay_rnd = vdisplay / 2;
    206   1.1  riastrad 	else
    207   1.1  riastrad 		vdisplay_rnd = vdisplay;
    208   1.1  riastrad 
    209   1.1  riastrad 	/* find the top & bottom borders */
    210   1.1  riastrad 	vmargin = 0;
    211   1.1  riastrad 	if (margins)
    212   1.1  riastrad 		vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
    213   1.1  riastrad 
    214   1.1  riastrad 	drm_mode->vdisplay = vdisplay + 2 * vmargin;
    215   1.1  riastrad 
    216   1.1  riastrad 	/* Interlaced */
    217   1.1  riastrad 	if (interlaced)
    218   1.1  riastrad 		interlace = 1;
    219   1.1  riastrad 	else
    220   1.1  riastrad 		interlace = 0;
    221   1.1  riastrad 
    222   1.1  riastrad 	/* Determine VSync Width from aspect ratio */
    223   1.1  riastrad 	if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
    224   1.1  riastrad 		vsync = 4;
    225   1.1  riastrad 	else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
    226   1.1  riastrad 		vsync = 5;
    227   1.1  riastrad 	else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
    228   1.1  riastrad 		vsync = 6;
    229   1.1  riastrad 	else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
    230   1.1  riastrad 		vsync = 7;
    231   1.1  riastrad 	else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
    232   1.1  riastrad 		vsync = 7;
    233   1.1  riastrad 	else /* custom */
    234   1.1  riastrad 		vsync = 10;
    235   1.1  riastrad 
    236   1.1  riastrad 	if (!reduced) {
    237   1.1  riastrad 		/* simplify the GTF calculation */
    238   1.1  riastrad 		/* 4) Minimum time of vertical sync + back porch interval (s)
    239   1.1  riastrad 		 * default 550.0
    240   1.1  riastrad 		 */
    241   1.1  riastrad 		int tmp1, tmp2;
    242   1.1  riastrad #define CVT_MIN_VSYNC_BP	550
    243   1.1  riastrad 		/* 3) Nominal HSync width (% of line period) - default 8 */
    244   1.1  riastrad #define CVT_HSYNC_PERCENTAGE	8
    245   1.1  riastrad 		unsigned int hblank_percentage;
    246  1.10  riastrad 		int vsyncandback_porch, __maybe_unused vback_porch, hblank;
    247   1.1  riastrad 
    248   1.1  riastrad 		/* estimated the horizontal period */
    249   1.1  riastrad 		tmp1 = HV_FACTOR * 1000000  -
    250   1.1  riastrad 				CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
    251   1.1  riastrad 		tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
    252   1.1  riastrad 				interlace;
    253   1.1  riastrad 		hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
    254   1.1  riastrad 
    255   1.1  riastrad 		tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
    256   1.1  riastrad 		/* 9. Find number of lines in sync + backporch */
    257   1.1  riastrad 		if (tmp1 < (vsync + CVT_MIN_V_PORCH))
    258   1.1  riastrad 			vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
    259   1.1  riastrad 		else
    260   1.1  riastrad 			vsyncandback_porch = tmp1;
    261   1.1  riastrad 		/* 10. Find number of lines in back porch */
    262   1.1  riastrad 		vback_porch = vsyncandback_porch - vsync;
    263   1.1  riastrad 		drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
    264   1.1  riastrad 				vsyncandback_porch + CVT_MIN_V_PORCH;
    265   1.1  riastrad 		/* 5) Definition of Horizontal blanking time limitation */
    266   1.1  riastrad 		/* Gradient (%/kHz) - default 600 */
    267   1.1  riastrad #define CVT_M_FACTOR	600
    268   1.1  riastrad 		/* Offset (%) - default 40 */
    269   1.1  riastrad #define CVT_C_FACTOR	40
    270   1.1  riastrad 		/* Blanking time scaling factor - default 128 */
    271   1.1  riastrad #define CVT_K_FACTOR	128
    272   1.1  riastrad 		/* Scaling factor weighting - default 20 */
    273   1.1  riastrad #define CVT_J_FACTOR	20
    274   1.1  riastrad #define CVT_M_PRIME	(CVT_M_FACTOR * CVT_K_FACTOR / 256)
    275   1.1  riastrad #define CVT_C_PRIME	((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
    276   1.1  riastrad 			 CVT_J_FACTOR)
    277   1.1  riastrad 		/* 12. Find ideal blanking duty cycle from formula */
    278   1.1  riastrad 		hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
    279   1.1  riastrad 					hperiod / 1000;
    280   1.1  riastrad 		/* 13. Blanking time */
    281   1.1  riastrad 		if (hblank_percentage < 20 * HV_FACTOR)
    282   1.1  riastrad 			hblank_percentage = 20 * HV_FACTOR;
    283   1.1  riastrad 		hblank = drm_mode->hdisplay * hblank_percentage /
    284   1.1  riastrad 			 (100 * HV_FACTOR - hblank_percentage);
    285   1.1  riastrad 		hblank -= hblank % (2 * CVT_H_GRANULARITY);
    286   1.7  riastrad 		/* 14. find the total pixels per line */
    287   1.1  riastrad 		drm_mode->htotal = drm_mode->hdisplay + hblank;
    288   1.1  riastrad 		drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
    289   1.1  riastrad 		drm_mode->hsync_start = drm_mode->hsync_end -
    290   1.1  riastrad 			(drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
    291   1.1  riastrad 		drm_mode->hsync_start += CVT_H_GRANULARITY -
    292   1.1  riastrad 			drm_mode->hsync_start % CVT_H_GRANULARITY;
    293   1.1  riastrad 		/* fill the Vsync values */
    294   1.1  riastrad 		drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
    295   1.1  riastrad 		drm_mode->vsync_end = drm_mode->vsync_start + vsync;
    296   1.1  riastrad 	} else {
    297   1.1  riastrad 		/* Reduced blanking */
    298   1.1  riastrad 		/* Minimum vertical blanking interval time (s)- default 460 */
    299   1.1  riastrad #define CVT_RB_MIN_VBLANK	460
    300   1.1  riastrad 		/* Fixed number of clocks for horizontal sync */
    301   1.1  riastrad #define CVT_RB_H_SYNC		32
    302   1.1  riastrad 		/* Fixed number of clocks for horizontal blanking */
    303   1.1  riastrad #define CVT_RB_H_BLANK		160
    304   1.1  riastrad 		/* Fixed number of lines for vertical front porch - default 3*/
    305   1.1  riastrad #define CVT_RB_VFPORCH		3
    306   1.1  riastrad 		int vbilines;
    307   1.1  riastrad 		int tmp1, tmp2;
    308   1.1  riastrad 		/* 8. Estimate Horizontal period. */
    309   1.1  riastrad 		tmp1 = HV_FACTOR * 1000000 -
    310   1.1  riastrad 			CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
    311   1.1  riastrad 		tmp2 = vdisplay_rnd + 2 * vmargin;
    312   1.1  riastrad 		hperiod = tmp1 / (tmp2 * vfieldrate);
    313   1.1  riastrad 		/* 9. Find number of lines in vertical blanking */
    314   1.1  riastrad 		vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
    315   1.1  riastrad 		/* 10. Check if vertical blanking is sufficient */
    316   1.1  riastrad 		if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
    317   1.1  riastrad 			vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
    318   1.1  riastrad 		/* 11. Find total number of lines in vertical field */
    319   1.1  riastrad 		drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
    320   1.1  riastrad 		/* 12. Find total number of pixels in a line */
    321   1.1  riastrad 		drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
    322   1.1  riastrad 		/* Fill in HSync values */
    323   1.1  riastrad 		drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
    324   1.1  riastrad 		drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
    325   1.1  riastrad 		/* Fill in VSync values */
    326   1.1  riastrad 		drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
    327   1.1  riastrad 		drm_mode->vsync_end = drm_mode->vsync_start + vsync;
    328   1.1  riastrad 	}
    329   1.1  riastrad 	/* 15/13. Find pixel clock frequency (kHz for xf86) */
    330  1.10  riastrad 	tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
    331  1.10  riastrad 	tmp *= HV_FACTOR * 1000;
    332  1.10  riastrad 	do_div(tmp, hperiod);
    333  1.10  riastrad 	tmp -= drm_mode->clock % CVT_CLOCK_STEP;
    334  1.10  riastrad 	drm_mode->clock = tmp;
    335   1.1  riastrad 	/* 18/16. Find actual vertical frame frequency */
    336   1.1  riastrad 	/* ignore - just set the mode flag for interlaced */
    337   1.1  riastrad 	if (interlaced) {
    338   1.1  riastrad 		drm_mode->vtotal *= 2;
    339   1.1  riastrad 		drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
    340   1.1  riastrad 	}
    341   1.1  riastrad 	/* Fill the mode line name */
    342   1.1  riastrad 	drm_mode_set_name(drm_mode);
    343   1.1  riastrad 	if (reduced)
    344   1.1  riastrad 		drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
    345   1.1  riastrad 					DRM_MODE_FLAG_NVSYNC);
    346   1.1  riastrad 	else
    347   1.1  riastrad 		drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
    348   1.1  riastrad 					DRM_MODE_FLAG_NHSYNC);
    349   1.1  riastrad 
    350   1.1  riastrad 	return drm_mode;
    351   1.1  riastrad }
    352   1.1  riastrad EXPORT_SYMBOL(drm_cvt_mode);
    353   1.1  riastrad 
    354   1.1  riastrad /**
    355   1.4  riastrad  * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
    356   1.4  riastrad  * @dev: drm device
    357   1.4  riastrad  * @hdisplay: hdisplay size
    358   1.4  riastrad  * @vdisplay: vdisplay size
    359   1.4  riastrad  * @vrefresh: vrefresh rate.
    360   1.4  riastrad  * @interlaced: whether to compute an interlaced mode
    361   1.4  riastrad  * @margins: desired margin (borders) size
    362   1.4  riastrad  * @GTF_M: extended GTF formula parameters
    363   1.4  riastrad  * @GTF_2C: extended GTF formula parameters
    364   1.4  riastrad  * @GTF_K: extended GTF formula parameters
    365   1.4  riastrad  * @GTF_2J: extended GTF formula parameters
    366   1.1  riastrad  *
    367   1.1  riastrad  * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
    368   1.1  riastrad  * in here multiplied by two.  For a C of 40, pass in 80.
    369   1.4  riastrad  *
    370   1.4  riastrad  * Returns:
    371   1.4  riastrad  * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
    372   1.4  riastrad  * The display mode object is allocated with drm_mode_create(). Returns NULL
    373   1.4  riastrad  * when no mode could be allocated.
    374   1.1  riastrad  */
    375   1.1  riastrad struct drm_display_mode *
    376   1.1  riastrad drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
    377   1.1  riastrad 		     int vrefresh, bool interlaced, int margins,
    378   1.1  riastrad 		     int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
    379   1.1  riastrad {	/* 1) top/bottom margin size (% of height) - default: 1.8, */
    380   1.1  riastrad #define	GTF_MARGIN_PERCENTAGE		18
    381   1.1  riastrad 	/* 2) character cell horizontal granularity (pixels) - default 8 */
    382   1.1  riastrad #define	GTF_CELL_GRAN			8
    383   1.1  riastrad 	/* 3) Minimum vertical porch (lines) - default 3 */
    384   1.1  riastrad #define	GTF_MIN_V_PORCH			1
    385   1.1  riastrad 	/* width of vsync in lines */
    386   1.1  riastrad #define V_SYNC_RQD			3
    387   1.1  riastrad 	/* width of hsync as % of total line */
    388   1.1  riastrad #define H_SYNC_PERCENT			8
    389   1.1  riastrad 	/* min time of vsync + back porch (microsec) */
    390   1.1  riastrad #define MIN_VSYNC_PLUS_BP		550
    391   1.1  riastrad 	/* C' and M' are part of the Blanking Duty Cycle computation */
    392   1.1  riastrad #define GTF_C_PRIME	((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
    393   1.1  riastrad #define GTF_M_PRIME	(GTF_K * GTF_M / 256)
    394   1.1  riastrad 	struct drm_display_mode *drm_mode;
    395   1.1  riastrad 	unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
    396   1.1  riastrad 	int top_margin, bottom_margin;
    397   1.1  riastrad 	int interlace;
    398   1.1  riastrad 	unsigned int hfreq_est;
    399  1.10  riastrad 	int vsync_plus_bp, __maybe_unused vback_porch;
    400  1.10  riastrad 	unsigned int vtotal_lines, __maybe_unused vfieldrate_est;
    401  1.10  riastrad 	unsigned int __maybe_unused hperiod;
    402  1.10  riastrad 	unsigned int vfield_rate, __maybe_unused vframe_rate;
    403   1.1  riastrad 	int left_margin, right_margin;
    404   1.1  riastrad 	unsigned int total_active_pixels, ideal_duty_cycle;
    405   1.1  riastrad 	unsigned int hblank, total_pixels, pixel_freq;
    406   1.1  riastrad 	int hsync, hfront_porch, vodd_front_porch_lines;
    407   1.1  riastrad 	unsigned int tmp1, tmp2;
    408   1.1  riastrad 
    409  1.10  riastrad 	if (!hdisplay || !vdisplay)
    410  1.10  riastrad 		return NULL;
    411  1.10  riastrad 
    412   1.1  riastrad 	drm_mode = drm_mode_create(dev);
    413   1.1  riastrad 	if (!drm_mode)
    414   1.1  riastrad 		return NULL;
    415   1.1  riastrad 
    416   1.1  riastrad 	/* 1. In order to give correct results, the number of horizontal
    417   1.1  riastrad 	 * pixels requested is first processed to ensure that it is divisible
    418   1.1  riastrad 	 * by the character size, by rounding it to the nearest character
    419   1.1  riastrad 	 * cell boundary:
    420   1.1  riastrad 	 */
    421   1.1  riastrad 	hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
    422   1.1  riastrad 	hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
    423   1.1  riastrad 
    424   1.1  riastrad 	/* 2. If interlace is requested, the number of vertical lines assumed
    425   1.1  riastrad 	 * by the calculation must be halved, as the computation calculates
    426   1.1  riastrad 	 * the number of vertical lines per field.
    427   1.1  riastrad 	 */
    428   1.1  riastrad 	if (interlaced)
    429   1.1  riastrad 		vdisplay_rnd = vdisplay / 2;
    430   1.1  riastrad 	else
    431   1.1  riastrad 		vdisplay_rnd = vdisplay;
    432   1.1  riastrad 
    433   1.1  riastrad 	/* 3. Find the frame rate required: */
    434   1.1  riastrad 	if (interlaced)
    435   1.1  riastrad 		vfieldrate_rqd = vrefresh * 2;
    436   1.1  riastrad 	else
    437   1.1  riastrad 		vfieldrate_rqd = vrefresh;
    438   1.1  riastrad 
    439   1.1  riastrad 	/* 4. Find number of lines in Top margin: */
    440   1.1  riastrad 	top_margin = 0;
    441   1.1  riastrad 	if (margins)
    442   1.1  riastrad 		top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
    443   1.1  riastrad 				1000;
    444   1.1  riastrad 	/* 5. Find number of lines in bottom margin: */
    445   1.1  riastrad 	bottom_margin = top_margin;
    446   1.1  riastrad 
    447   1.1  riastrad 	/* 6. If interlace is required, then set variable interlace: */
    448   1.1  riastrad 	if (interlaced)
    449   1.1  riastrad 		interlace = 1;
    450   1.1  riastrad 	else
    451   1.1  riastrad 		interlace = 0;
    452   1.1  riastrad 
    453   1.1  riastrad 	/* 7. Estimate the Horizontal frequency */
    454   1.1  riastrad 	{
    455   1.1  riastrad 		tmp1 = (1000000  - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
    456   1.1  riastrad 		tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
    457   1.1  riastrad 				2 + interlace;
    458   1.1  riastrad 		hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
    459   1.1  riastrad 	}
    460   1.1  riastrad 
    461   1.1  riastrad 	/* 8. Find the number of lines in V sync + back porch */
    462   1.1  riastrad 	/* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
    463   1.1  riastrad 	vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
    464   1.1  riastrad 	vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
    465   1.1  riastrad 	/*  9. Find the number of lines in V back porch alone: */
    466   1.1  riastrad 	vback_porch = vsync_plus_bp - V_SYNC_RQD;
    467   1.1  riastrad 	/*  10. Find the total number of lines in Vertical field period: */
    468   1.1  riastrad 	vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
    469   1.1  riastrad 			vsync_plus_bp + GTF_MIN_V_PORCH;
    470   1.1  riastrad 	/*  11. Estimate the Vertical field frequency: */
    471   1.1  riastrad 	vfieldrate_est = hfreq_est / vtotal_lines;
    472   1.1  riastrad 	/*  12. Find the actual horizontal period: */
    473   1.1  riastrad 	hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
    474   1.1  riastrad 
    475   1.1  riastrad 	/*  13. Find the actual Vertical field frequency: */
    476   1.1  riastrad 	vfield_rate = hfreq_est / vtotal_lines;
    477   1.1  riastrad 	/*  14. Find the Vertical frame frequency: */
    478   1.1  riastrad 	if (interlaced)
    479   1.1  riastrad 		vframe_rate = vfield_rate / 2;
    480   1.1  riastrad 	else
    481   1.1  riastrad 		vframe_rate = vfield_rate;
    482   1.1  riastrad 	/*  15. Find number of pixels in left margin: */
    483   1.1  riastrad 	if (margins)
    484   1.1  riastrad 		left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
    485   1.1  riastrad 				1000;
    486   1.1  riastrad 	else
    487   1.1  riastrad 		left_margin = 0;
    488   1.1  riastrad 
    489   1.1  riastrad 	/* 16.Find number of pixels in right margin: */
    490   1.1  riastrad 	right_margin = left_margin;
    491   1.1  riastrad 	/* 17.Find total number of active pixels in image and left and right */
    492   1.1  riastrad 	total_active_pixels = hdisplay_rnd + left_margin + right_margin;
    493   1.1  riastrad 	/* 18.Find the ideal blanking duty cycle from blanking duty cycle */
    494   1.1  riastrad 	ideal_duty_cycle = GTF_C_PRIME * 1000 -
    495   1.1  riastrad 				(GTF_M_PRIME * 1000000 / hfreq_est);
    496   1.1  riastrad 	/* 19.Find the number of pixels in the blanking time to the nearest
    497   1.1  riastrad 	 * double character cell: */
    498   1.1  riastrad 	hblank = total_active_pixels * ideal_duty_cycle /
    499   1.1  riastrad 			(100000 - ideal_duty_cycle);
    500   1.1  riastrad 	hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
    501   1.1  riastrad 	hblank = hblank * 2 * GTF_CELL_GRAN;
    502   1.1  riastrad 	/* 20.Find total number of pixels: */
    503   1.1  riastrad 	total_pixels = total_active_pixels + hblank;
    504   1.1  riastrad 	/* 21.Find pixel clock frequency: */
    505   1.1  riastrad 	pixel_freq = total_pixels * hfreq_est / 1000;
    506   1.1  riastrad 	/* Stage 1 computations are now complete; I should really pass
    507   1.1  riastrad 	 * the results to another function and do the Stage 2 computations,
    508   1.1  riastrad 	 * but I only need a few more values so I'll just append the
    509   1.1  riastrad 	 * computations here for now */
    510   1.1  riastrad 	/* 17. Find the number of pixels in the horizontal sync period: */
    511   1.1  riastrad 	hsync = H_SYNC_PERCENT * total_pixels / 100;
    512   1.1  riastrad 	hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
    513   1.1  riastrad 	hsync = hsync * GTF_CELL_GRAN;
    514   1.1  riastrad 	/* 18. Find the number of pixels in horizontal front porch period */
    515   1.1  riastrad 	hfront_porch = hblank / 2 - hsync;
    516   1.1  riastrad 	/*  36. Find the number of lines in the odd front porch period: */
    517   1.1  riastrad 	vodd_front_porch_lines = GTF_MIN_V_PORCH ;
    518   1.1  riastrad 
    519   1.1  riastrad 	/* finally, pack the results in the mode struct */
    520   1.1  riastrad 	drm_mode->hdisplay = hdisplay_rnd;
    521   1.1  riastrad 	drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
    522   1.1  riastrad 	drm_mode->hsync_end = drm_mode->hsync_start + hsync;
    523   1.1  riastrad 	drm_mode->htotal = total_pixels;
    524   1.1  riastrad 	drm_mode->vdisplay = vdisplay_rnd;
    525   1.1  riastrad 	drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
    526   1.1  riastrad 	drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
    527   1.1  riastrad 	drm_mode->vtotal = vtotal_lines;
    528   1.1  riastrad 
    529   1.1  riastrad 	drm_mode->clock = pixel_freq;
    530   1.1  riastrad 
    531   1.1  riastrad 	if (interlaced) {
    532   1.1  riastrad 		drm_mode->vtotal *= 2;
    533   1.1  riastrad 		drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
    534   1.1  riastrad 	}
    535   1.1  riastrad 
    536   1.1  riastrad 	drm_mode_set_name(drm_mode);
    537   1.1  riastrad 	if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
    538   1.1  riastrad 		drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
    539   1.1  riastrad 	else
    540   1.1  riastrad 		drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
    541   1.1  riastrad 
    542   1.1  riastrad 	return drm_mode;
    543   1.1  riastrad }
    544   1.1  riastrad EXPORT_SYMBOL(drm_gtf_mode_complex);
    545   1.1  riastrad 
    546   1.1  riastrad /**
    547   1.4  riastrad  * drm_gtf_mode - create the modeline based on the GTF algorithm
    548   1.4  riastrad  * @dev: drm device
    549   1.4  riastrad  * @hdisplay: hdisplay size
    550   1.4  riastrad  * @vdisplay: vdisplay size
    551   1.4  riastrad  * @vrefresh: vrefresh rate.
    552   1.4  riastrad  * @interlaced: whether to compute an interlaced mode
    553   1.4  riastrad  * @margins: desired margin (borders) size
    554   1.1  riastrad  *
    555   1.1  riastrad  * return the modeline based on GTF algorithm
    556   1.1  riastrad  *
    557   1.1  riastrad  * This function is to create the modeline based on the GTF algorithm.
    558   1.1  riastrad  * Generalized Timing Formula is derived from:
    559  1.10  riastrad  *
    560   1.1  riastrad  *	GTF Spreadsheet by Andy Morrish (1/5/97)
    561   1.1  riastrad  *	available at http://www.vesa.org
    562   1.1  riastrad  *
    563   1.1  riastrad  * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
    564   1.1  riastrad  * What I have done is to translate it by using integer calculation.
    565   1.1  riastrad  * I also refer to the function of fb_get_mode in the file of
    566   1.1  riastrad  * drivers/video/fbmon.c
    567   1.1  riastrad  *
    568  1.10  riastrad  * Standard GTF parameters::
    569  1.10  riastrad  *
    570  1.10  riastrad  *     M = 600
    571  1.10  riastrad  *     C = 40
    572  1.10  riastrad  *     K = 128
    573  1.10  riastrad  *     J = 20
    574   1.4  riastrad  *
    575   1.4  riastrad  * Returns:
    576   1.4  riastrad  * The modeline based on the GTF algorithm stored in a drm_display_mode object.
    577   1.4  riastrad  * The display mode object is allocated with drm_mode_create(). Returns NULL
    578   1.4  riastrad  * when no mode could be allocated.
    579   1.1  riastrad  */
    580   1.1  riastrad struct drm_display_mode *
    581   1.1  riastrad drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
    582   1.4  riastrad 	     bool interlaced, int margins)
    583   1.1  riastrad {
    584   1.4  riastrad 	return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
    585   1.4  riastrad 				    interlaced, margins,
    586   1.4  riastrad 				    600, 40 * 2, 128, 20 * 2);
    587   1.1  riastrad }
    588   1.1  riastrad EXPORT_SYMBOL(drm_gtf_mode);
    589   1.1  riastrad 
    590   1.4  riastrad #ifdef CONFIG_VIDEOMODE_HELPERS
    591   1.1  riastrad /**
    592   1.4  riastrad  * drm_display_mode_from_videomode - fill in @dmode using @vm,
    593   1.4  riastrad  * @vm: videomode structure to use as source
    594   1.4  riastrad  * @dmode: drm_display_mode structure to use as destination
    595   1.4  riastrad  *
    596   1.4  riastrad  * Fills out @dmode using the display mode specified in @vm.
    597   1.4  riastrad  */
    598   1.4  riastrad void drm_display_mode_from_videomode(const struct videomode *vm,
    599   1.4  riastrad 				     struct drm_display_mode *dmode)
    600   1.4  riastrad {
    601   1.4  riastrad 	dmode->hdisplay = vm->hactive;
    602   1.4  riastrad 	dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
    603   1.4  riastrad 	dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
    604   1.4  riastrad 	dmode->htotal = dmode->hsync_end + vm->hback_porch;
    605   1.4  riastrad 
    606   1.4  riastrad 	dmode->vdisplay = vm->vactive;
    607   1.4  riastrad 	dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
    608   1.4  riastrad 	dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
    609   1.4  riastrad 	dmode->vtotal = dmode->vsync_end + vm->vback_porch;
    610   1.4  riastrad 
    611   1.4  riastrad 	dmode->clock = vm->pixelclock / 1000;
    612   1.4  riastrad 
    613   1.4  riastrad 	dmode->flags = 0;
    614   1.4  riastrad 	if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
    615   1.4  riastrad 		dmode->flags |= DRM_MODE_FLAG_PHSYNC;
    616   1.4  riastrad 	else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
    617   1.4  riastrad 		dmode->flags |= DRM_MODE_FLAG_NHSYNC;
    618   1.4  riastrad 	if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
    619   1.4  riastrad 		dmode->flags |= DRM_MODE_FLAG_PVSYNC;
    620   1.4  riastrad 	else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
    621   1.4  riastrad 		dmode->flags |= DRM_MODE_FLAG_NVSYNC;
    622   1.4  riastrad 	if (vm->flags & DISPLAY_FLAGS_INTERLACED)
    623   1.4  riastrad 		dmode->flags |= DRM_MODE_FLAG_INTERLACE;
    624   1.4  riastrad 	if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
    625   1.4  riastrad 		dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
    626   1.4  riastrad 	if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
    627   1.4  riastrad 		dmode->flags |= DRM_MODE_FLAG_DBLCLK;
    628   1.4  riastrad 	drm_mode_set_name(dmode);
    629   1.1  riastrad }
    630   1.4  riastrad EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
    631   1.1  riastrad 
    632   1.7  riastrad /**
    633   1.7  riastrad  * drm_display_mode_to_videomode - fill in @vm using @dmode,
    634   1.7  riastrad  * @dmode: drm_display_mode structure to use as source
    635   1.7  riastrad  * @vm: videomode structure to use as destination
    636   1.7  riastrad  *
    637   1.7  riastrad  * Fills out @vm using the display mode specified in @dmode.
    638   1.7  riastrad  */
    639   1.7  riastrad void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
    640   1.7  riastrad 				   struct videomode *vm)
    641   1.7  riastrad {
    642   1.7  riastrad 	vm->hactive = dmode->hdisplay;
    643   1.7  riastrad 	vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
    644   1.7  riastrad 	vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
    645   1.7  riastrad 	vm->hback_porch = dmode->htotal - dmode->hsync_end;
    646   1.7  riastrad 
    647   1.7  riastrad 	vm->vactive = dmode->vdisplay;
    648   1.7  riastrad 	vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
    649   1.7  riastrad 	vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
    650   1.7  riastrad 	vm->vback_porch = dmode->vtotal - dmode->vsync_end;
    651   1.7  riastrad 
    652   1.7  riastrad 	vm->pixelclock = dmode->clock * 1000;
    653   1.7  riastrad 
    654   1.7  riastrad 	vm->flags = 0;
    655   1.7  riastrad 	if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
    656   1.7  riastrad 		vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
    657   1.7  riastrad 	else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
    658   1.7  riastrad 		vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
    659   1.7  riastrad 	if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
    660   1.7  riastrad 		vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
    661   1.7  riastrad 	else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
    662   1.7  riastrad 		vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
    663   1.7  riastrad 	if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
    664   1.7  riastrad 		vm->flags |= DISPLAY_FLAGS_INTERLACED;
    665   1.7  riastrad 	if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
    666   1.7  riastrad 		vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
    667   1.7  riastrad 	if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
    668   1.7  riastrad 		vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
    669   1.7  riastrad }
    670   1.7  riastrad EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
    671   1.7  riastrad 
    672  1.10  riastrad /**
    673  1.10  riastrad  * drm_bus_flags_from_videomode - extract information about pixelclk and
    674  1.10  riastrad  * DE polarity from videomode and store it in a separate variable
    675  1.10  riastrad  * @vm: videomode structure to use
    676  1.10  riastrad  * @bus_flags: information about pixelclk, sync and DE polarity will be stored
    677  1.10  riastrad  * here
    678  1.10  riastrad  *
    679  1.10  riastrad  * Sets DRM_BUS_FLAG_DE_(LOW|HIGH),  DRM_BUS_FLAG_PIXDATA_DRIVE_(POS|NEG)EDGE
    680  1.10  riastrad  * and DISPLAY_FLAGS_SYNC_(POS|NEG)EDGE in @bus_flags according to DISPLAY_FLAGS
    681  1.10  riastrad  * found in @vm
    682  1.10  riastrad  */
    683  1.10  riastrad void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
    684  1.10  riastrad {
    685  1.10  riastrad 	*bus_flags = 0;
    686  1.10  riastrad 	if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
    687  1.10  riastrad 		*bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
    688  1.10  riastrad 	if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
    689  1.10  riastrad 		*bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
    690  1.10  riastrad 
    691  1.10  riastrad 	if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE)
    692  1.10  riastrad 		*bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
    693  1.10  riastrad 	if (vm->flags & DISPLAY_FLAGS_SYNC_NEGEDGE)
    694  1.10  riastrad 		*bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
    695  1.10  riastrad 
    696  1.10  riastrad 	if (vm->flags & DISPLAY_FLAGS_DE_LOW)
    697  1.10  riastrad 		*bus_flags |= DRM_BUS_FLAG_DE_LOW;
    698  1.10  riastrad 	if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
    699  1.10  riastrad 		*bus_flags |= DRM_BUS_FLAG_DE_HIGH;
    700  1.10  riastrad }
    701  1.10  riastrad EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
    702  1.10  riastrad 
    703   1.4  riastrad #ifdef CONFIG_OF
    704   1.1  riastrad /**
    705   1.4  riastrad  * of_get_drm_display_mode - get a drm_display_mode from devicetree
    706   1.4  riastrad  * @np: device_node with the timing specification
    707   1.4  riastrad  * @dmode: will be set to the return value
    708  1.10  riastrad  * @bus_flags: information about pixelclk, sync and DE polarity
    709   1.4  riastrad  * @index: index into the list of display timings in devicetree
    710   1.1  riastrad  *
    711   1.4  riastrad  * This function is expensive and should only be used, if only one mode is to be
    712   1.4  riastrad  * read from DT. To get multiple modes start with of_get_display_timings and
    713   1.4  riastrad  * work with that instead.
    714   1.1  riastrad  *
    715   1.4  riastrad  * Returns:
    716   1.4  riastrad  * 0 on success, a negative errno code when no of videomode node was found.
    717   1.1  riastrad  */
    718   1.4  riastrad int of_get_drm_display_mode(struct device_node *np,
    719  1.10  riastrad 			    struct drm_display_mode *dmode, u32 *bus_flags,
    720  1.10  riastrad 			    int index)
    721   1.1  riastrad {
    722   1.4  riastrad 	struct videomode vm;
    723   1.4  riastrad 	int ret;
    724   1.1  riastrad 
    725   1.4  riastrad 	ret = of_get_videomode(np, &vm, index);
    726   1.4  riastrad 	if (ret)
    727   1.4  riastrad 		return ret;
    728   1.1  riastrad 
    729   1.4  riastrad 	drm_display_mode_from_videomode(&vm, dmode);
    730  1.10  riastrad 	if (bus_flags)
    731  1.10  riastrad 		drm_bus_flags_from_videomode(&vm, bus_flags);
    732   1.1  riastrad 
    733  1.10  riastrad 	pr_debug("%pOF: got %dx%d display mode\n",
    734  1.10  riastrad 		np, vm.hactive, vm.vactive);
    735   1.4  riastrad 	drm_mode_debug_printmodeline(dmode);
    736   1.1  riastrad 
    737   1.4  riastrad 	return 0;
    738   1.1  riastrad }
    739   1.4  riastrad EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
    740   1.4  riastrad #endif /* CONFIG_OF */
    741   1.4  riastrad #endif /* CONFIG_VIDEOMODE_HELPERS */
    742   1.1  riastrad 
    743   1.1  riastrad /**
    744   1.4  riastrad  * drm_mode_set_name - set the name on a mode
    745   1.4  riastrad  * @mode: name will be set in this mode
    746   1.1  riastrad  *
    747   1.4  riastrad  * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
    748   1.4  riastrad  * with an optional 'i' suffix for interlaced modes.
    749   1.1  riastrad  */
    750   1.4  riastrad void drm_mode_set_name(struct drm_display_mode *mode)
    751   1.1  riastrad {
    752   1.4  riastrad 	bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
    753   1.4  riastrad 
    754   1.4  riastrad 	snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
    755   1.4  riastrad 		 mode->hdisplay, mode->vdisplay,
    756   1.4  riastrad 		 interlaced ? "i" : "");
    757   1.1  riastrad }
    758   1.4  riastrad EXPORT_SYMBOL(drm_mode_set_name);
    759   1.1  riastrad 
    760  1.10  riastrad /**
    761  1.10  riastrad  * drm_mode_hsync - get the hsync of a mode
    762   1.1  riastrad  * @mode: mode
    763   1.1  riastrad  *
    764   1.4  riastrad  * Returns:
    765   1.4  riastrad  * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
    766   1.4  riastrad  * value first if it is not yet set.
    767   1.1  riastrad  */
    768   1.1  riastrad int drm_mode_hsync(const struct drm_display_mode *mode)
    769   1.1  riastrad {
    770   1.1  riastrad 	unsigned int calc_val;
    771   1.1  riastrad 
    772   1.1  riastrad 	if (mode->hsync)
    773   1.1  riastrad 		return mode->hsync;
    774   1.1  riastrad 
    775  1.10  riastrad 	if (mode->htotal <= 0)
    776   1.1  riastrad 		return 0;
    777   1.1  riastrad 
    778   1.1  riastrad 	calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
    779   1.1  riastrad 	calc_val += 500;				/* round to 1000Hz */
    780   1.1  riastrad 	calc_val /= 1000;				/* truncate to kHz */
    781   1.1  riastrad 
    782   1.1  riastrad 	return calc_val;
    783   1.1  riastrad }
    784   1.1  riastrad EXPORT_SYMBOL(drm_mode_hsync);
    785   1.1  riastrad 
    786   1.1  riastrad /**
    787   1.1  riastrad  * drm_mode_vrefresh - get the vrefresh of a mode
    788   1.1  riastrad  * @mode: mode
    789   1.1  riastrad  *
    790   1.4  riastrad  * Returns:
    791   1.4  riastrad  * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
    792   1.4  riastrad  * value first if it is not yet set.
    793   1.1  riastrad  */
    794   1.1  riastrad int drm_mode_vrefresh(const struct drm_display_mode *mode)
    795   1.1  riastrad {
    796   1.1  riastrad 	int refresh = 0;
    797   1.1  riastrad 
    798   1.1  riastrad 	if (mode->vrefresh > 0)
    799   1.1  riastrad 		refresh = mode->vrefresh;
    800   1.1  riastrad 	else if (mode->htotal > 0 && mode->vtotal > 0) {
    801  1.10  riastrad 		unsigned int num, den;
    802  1.10  riastrad 
    803  1.10  riastrad 		num = mode->clock * 1000;
    804  1.10  riastrad 		den = mode->htotal * mode->vtotal;
    805   1.1  riastrad 
    806   1.1  riastrad 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
    807  1.10  riastrad 			num *= 2;
    808   1.1  riastrad 		if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
    809  1.10  riastrad 			den *= 2;
    810   1.1  riastrad 		if (mode->vscan > 1)
    811  1.10  riastrad 			den *= mode->vscan;
    812  1.10  riastrad 
    813  1.10  riastrad 		refresh = DIV_ROUND_CLOSEST(num, den);
    814   1.1  riastrad 	}
    815   1.1  riastrad 	return refresh;
    816   1.1  riastrad }
    817   1.1  riastrad EXPORT_SYMBOL(drm_mode_vrefresh);
    818   1.1  riastrad 
    819   1.1  riastrad /**
    820  1.10  riastrad  * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
    821  1.10  riastrad  * @mode: mode to query
    822  1.10  riastrad  * @hdisplay: hdisplay value to fill in
    823  1.10  riastrad  * @vdisplay: vdisplay value to fill in
    824  1.10  riastrad  *
    825  1.10  riastrad  * The vdisplay value will be doubled if the specified mode is a stereo mode of
    826  1.10  riastrad  * the appropriate layout.
    827  1.10  riastrad  */
    828  1.10  riastrad void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
    829  1.10  riastrad 			    int *hdisplay, int *vdisplay)
    830  1.10  riastrad {
    831  1.10  riastrad 	struct drm_display_mode adjusted = *mode;
    832  1.10  riastrad 
    833  1.10  riastrad 	drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
    834  1.10  riastrad 	*hdisplay = adjusted.crtc_hdisplay;
    835  1.10  riastrad 	*vdisplay = adjusted.crtc_vdisplay;
    836  1.10  riastrad }
    837  1.10  riastrad EXPORT_SYMBOL(drm_mode_get_hv_timing);
    838  1.10  riastrad 
    839  1.10  riastrad /**
    840   1.4  riastrad  * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
    841   1.1  riastrad  * @p: mode
    842   1.4  riastrad  * @adjust_flags: a combination of adjustment flags
    843   1.1  riastrad  *
    844   1.4  riastrad  * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
    845   1.1  riastrad  *
    846   1.4  riastrad  * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
    847   1.4  riastrad  *   interlaced modes.
    848   1.4  riastrad  * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
    849   1.4  riastrad  *   buffers containing two eyes (only adjust the timings when needed, eg. for
    850   1.4  riastrad  *   "frame packing" or "side by side full").
    851   1.7  riastrad  * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
    852   1.7  riastrad  *   be performed for doublescan and vscan > 1 modes respectively.
    853   1.1  riastrad  */
    854   1.1  riastrad void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
    855   1.1  riastrad {
    856  1.10  riastrad 	if (!p)
    857   1.1  riastrad 		return;
    858   1.1  riastrad 
    859   1.4  riastrad 	p->crtc_clock = p->clock;
    860   1.1  riastrad 	p->crtc_hdisplay = p->hdisplay;
    861   1.1  riastrad 	p->crtc_hsync_start = p->hsync_start;
    862   1.1  riastrad 	p->crtc_hsync_end = p->hsync_end;
    863   1.1  riastrad 	p->crtc_htotal = p->htotal;
    864   1.1  riastrad 	p->crtc_hskew = p->hskew;
    865   1.1  riastrad 	p->crtc_vdisplay = p->vdisplay;
    866   1.1  riastrad 	p->crtc_vsync_start = p->vsync_start;
    867   1.1  riastrad 	p->crtc_vsync_end = p->vsync_end;
    868   1.1  riastrad 	p->crtc_vtotal = p->vtotal;
    869   1.1  riastrad 
    870   1.1  riastrad 	if (p->flags & DRM_MODE_FLAG_INTERLACE) {
    871   1.1  riastrad 		if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
    872   1.1  riastrad 			p->crtc_vdisplay /= 2;
    873   1.1  riastrad 			p->crtc_vsync_start /= 2;
    874   1.1  riastrad 			p->crtc_vsync_end /= 2;
    875   1.1  riastrad 			p->crtc_vtotal /= 2;
    876   1.1  riastrad 		}
    877   1.1  riastrad 	}
    878   1.1  riastrad 
    879   1.7  riastrad 	if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
    880   1.7  riastrad 		if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
    881   1.7  riastrad 			p->crtc_vdisplay *= 2;
    882   1.7  riastrad 			p->crtc_vsync_start *= 2;
    883   1.7  riastrad 			p->crtc_vsync_end *= 2;
    884   1.7  riastrad 			p->crtc_vtotal *= 2;
    885   1.7  riastrad 		}
    886   1.1  riastrad 	}
    887   1.1  riastrad 
    888   1.7  riastrad 	if (!(adjust_flags & CRTC_NO_VSCAN)) {
    889   1.7  riastrad 		if (p->vscan > 1) {
    890   1.7  riastrad 			p->crtc_vdisplay *= p->vscan;
    891   1.7  riastrad 			p->crtc_vsync_start *= p->vscan;
    892   1.7  riastrad 			p->crtc_vsync_end *= p->vscan;
    893   1.7  riastrad 			p->crtc_vtotal *= p->vscan;
    894   1.7  riastrad 		}
    895   1.1  riastrad 	}
    896   1.1  riastrad 
    897   1.4  riastrad 	if (adjust_flags & CRTC_STEREO_DOUBLE) {
    898   1.4  riastrad 		unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
    899   1.4  riastrad 
    900   1.4  riastrad 		switch (layout) {
    901   1.4  riastrad 		case DRM_MODE_FLAG_3D_FRAME_PACKING:
    902   1.4  riastrad 			p->crtc_clock *= 2;
    903   1.4  riastrad 			p->crtc_vdisplay += p->crtc_vtotal;
    904   1.4  riastrad 			p->crtc_vsync_start += p->crtc_vtotal;
    905   1.4  riastrad 			p->crtc_vsync_end += p->crtc_vtotal;
    906   1.4  riastrad 			p->crtc_vtotal += p->crtc_vtotal;
    907   1.4  riastrad 			break;
    908   1.4  riastrad 		}
    909   1.4  riastrad 	}
    910   1.4  riastrad 
    911   1.1  riastrad 	p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
    912   1.1  riastrad 	p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
    913   1.1  riastrad 	p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
    914   1.1  riastrad 	p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
    915   1.1  riastrad }
    916   1.1  riastrad EXPORT_SYMBOL(drm_mode_set_crtcinfo);
    917   1.1  riastrad 
    918   1.1  riastrad /**
    919   1.1  riastrad  * drm_mode_copy - copy the mode
    920   1.1  riastrad  * @dst: mode to overwrite
    921   1.1  riastrad  * @src: mode to copy
    922   1.1  riastrad  *
    923   1.4  riastrad  * Copy an existing mode into another mode, preserving the object id and
    924   1.4  riastrad  * list head of the destination mode.
    925   1.1  riastrad  */
    926   1.1  riastrad void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
    927   1.1  riastrad {
    928   1.4  riastrad 	struct list_head head = dst->head;
    929   1.1  riastrad 
    930   1.1  riastrad 	*dst = *src;
    931   1.4  riastrad 	dst->head = head;
    932   1.1  riastrad }
    933   1.1  riastrad EXPORT_SYMBOL(drm_mode_copy);
    934   1.1  riastrad 
    935   1.1  riastrad /**
    936   1.1  riastrad  * drm_mode_duplicate - allocate and duplicate an existing mode
    937   1.4  riastrad  * @dev: drm_device to allocate the duplicated mode for
    938   1.4  riastrad  * @mode: mode to duplicate
    939   1.1  riastrad  *
    940   1.1  riastrad  * Just allocate a new mode, copy the existing mode into it, and return
    941   1.1  riastrad  * a pointer to it.  Used to create new instances of established modes.
    942   1.4  riastrad  *
    943   1.4  riastrad  * Returns:
    944   1.4  riastrad  * Pointer to duplicated mode on success, NULL on error.
    945   1.1  riastrad  */
    946   1.1  riastrad struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
    947   1.1  riastrad 					    const struct drm_display_mode *mode)
    948   1.1  riastrad {
    949   1.1  riastrad 	struct drm_display_mode *nmode;
    950   1.1  riastrad 
    951   1.1  riastrad 	nmode = drm_mode_create(dev);
    952   1.1  riastrad 	if (!nmode)
    953   1.1  riastrad 		return NULL;
    954   1.1  riastrad 
    955   1.1  riastrad 	drm_mode_copy(nmode, mode);
    956   1.1  riastrad 
    957   1.1  riastrad 	return nmode;
    958   1.1  riastrad }
    959   1.1  riastrad EXPORT_SYMBOL(drm_mode_duplicate);
    960   1.1  riastrad 
    961  1.10  riastrad static bool drm_mode_match_timings(const struct drm_display_mode *mode1,
    962  1.10  riastrad 				   const struct drm_display_mode *mode2)
    963  1.10  riastrad {
    964  1.10  riastrad 	return mode1->hdisplay == mode2->hdisplay &&
    965  1.10  riastrad 		mode1->hsync_start == mode2->hsync_start &&
    966  1.10  riastrad 		mode1->hsync_end == mode2->hsync_end &&
    967  1.10  riastrad 		mode1->htotal == mode2->htotal &&
    968  1.10  riastrad 		mode1->hskew == mode2->hskew &&
    969  1.10  riastrad 		mode1->vdisplay == mode2->vdisplay &&
    970  1.10  riastrad 		mode1->vsync_start == mode2->vsync_start &&
    971  1.10  riastrad 		mode1->vsync_end == mode2->vsync_end &&
    972  1.10  riastrad 		mode1->vtotal == mode2->vtotal &&
    973  1.10  riastrad 		mode1->vscan == mode2->vscan;
    974  1.10  riastrad }
    975  1.10  riastrad 
    976  1.10  riastrad static bool drm_mode_match_clock(const struct drm_display_mode *mode1,
    977  1.10  riastrad 				  const struct drm_display_mode *mode2)
    978  1.10  riastrad {
    979  1.10  riastrad 	/*
    980  1.10  riastrad 	 * do clock check convert to PICOS
    981  1.10  riastrad 	 * so fb modes get matched the same
    982  1.10  riastrad 	 */
    983  1.10  riastrad 	if (mode1->clock && mode2->clock)
    984  1.10  riastrad 		return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock);
    985  1.10  riastrad 	else
    986  1.10  riastrad 		return mode1->clock == mode2->clock;
    987  1.10  riastrad }
    988  1.10  riastrad 
    989  1.10  riastrad static bool drm_mode_match_flags(const struct drm_display_mode *mode1,
    990  1.10  riastrad 				 const struct drm_display_mode *mode2)
    991  1.10  riastrad {
    992  1.10  riastrad 	return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
    993  1.10  riastrad 		(mode2->flags & ~DRM_MODE_FLAG_3D_MASK);
    994  1.10  riastrad }
    995  1.10  riastrad 
    996  1.10  riastrad static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1,
    997  1.10  riastrad 				    const struct drm_display_mode *mode2)
    998  1.10  riastrad {
    999  1.10  riastrad 	return (mode1->flags & DRM_MODE_FLAG_3D_MASK) ==
   1000  1.10  riastrad 		(mode2->flags & DRM_MODE_FLAG_3D_MASK);
   1001  1.10  riastrad }
   1002  1.10  riastrad 
   1003  1.10  riastrad static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1,
   1004  1.10  riastrad 					const struct drm_display_mode *mode2)
   1005  1.10  riastrad {
   1006  1.10  riastrad 	return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio;
   1007  1.10  riastrad }
   1008  1.10  riastrad 
   1009   1.1  riastrad /**
   1010  1.10  riastrad  * drm_mode_match - test modes for (partial) equality
   1011   1.1  riastrad  * @mode1: first mode
   1012   1.1  riastrad  * @mode2: second mode
   1013  1.10  riastrad  * @match_flags: which parts need to match (DRM_MODE_MATCH_*)
   1014   1.1  riastrad  *
   1015   1.1  riastrad  * Check to see if @mode1 and @mode2 are equivalent.
   1016   1.1  riastrad  *
   1017   1.4  riastrad  * Returns:
   1018  1.10  riastrad  * True if the modes are (partially) equal, false otherwise.
   1019   1.1  riastrad  */
   1020  1.10  riastrad bool drm_mode_match(const struct drm_display_mode *mode1,
   1021  1.10  riastrad 		    const struct drm_display_mode *mode2,
   1022  1.10  riastrad 		    unsigned int match_flags)
   1023   1.1  riastrad {
   1024   1.7  riastrad 	if (!mode1 && !mode2)
   1025   1.7  riastrad 		return true;
   1026   1.7  riastrad 
   1027   1.7  riastrad 	if (!mode1 || !mode2)
   1028   1.7  riastrad 		return false;
   1029   1.7  riastrad 
   1030  1.10  riastrad 	if (match_flags & DRM_MODE_MATCH_TIMINGS &&
   1031  1.10  riastrad 	    !drm_mode_match_timings(mode1, mode2))
   1032  1.10  riastrad 		return false;
   1033  1.10  riastrad 
   1034  1.10  riastrad 	if (match_flags & DRM_MODE_MATCH_CLOCK &&
   1035  1.10  riastrad 	    !drm_mode_match_clock(mode1, mode2))
   1036  1.10  riastrad 		return false;
   1037  1.10  riastrad 
   1038  1.10  riastrad 	if (match_flags & DRM_MODE_MATCH_FLAGS &&
   1039  1.10  riastrad 	    !drm_mode_match_flags(mode1, mode2))
   1040  1.10  riastrad 		return false;
   1041  1.10  riastrad 
   1042  1.10  riastrad 	if (match_flags & DRM_MODE_MATCH_3D_FLAGS &&
   1043  1.10  riastrad 	    !drm_mode_match_3d_flags(mode1, mode2))
   1044   1.1  riastrad 		return false;
   1045   1.1  riastrad 
   1046  1.10  riastrad 	if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO &&
   1047  1.10  riastrad 	    !drm_mode_match_aspect_ratio(mode1, mode2))
   1048   1.4  riastrad 		return false;
   1049   1.4  riastrad 
   1050  1.10  riastrad 	return true;
   1051  1.10  riastrad }
   1052  1.10  riastrad EXPORT_SYMBOL(drm_mode_match);
   1053  1.10  riastrad 
   1054  1.10  riastrad /**
   1055  1.10  riastrad  * drm_mode_equal - test modes for equality
   1056  1.10  riastrad  * @mode1: first mode
   1057  1.10  riastrad  * @mode2: second mode
   1058  1.10  riastrad  *
   1059  1.10  riastrad  * Check to see if @mode1 and @mode2 are equivalent.
   1060  1.10  riastrad  *
   1061  1.10  riastrad  * Returns:
   1062  1.10  riastrad  * True if the modes are equal, false otherwise.
   1063  1.10  riastrad  */
   1064  1.10  riastrad bool drm_mode_equal(const struct drm_display_mode *mode1,
   1065  1.10  riastrad 		    const struct drm_display_mode *mode2)
   1066  1.10  riastrad {
   1067  1.10  riastrad 	return drm_mode_match(mode1, mode2,
   1068  1.10  riastrad 			      DRM_MODE_MATCH_TIMINGS |
   1069  1.10  riastrad 			      DRM_MODE_MATCH_CLOCK |
   1070  1.10  riastrad 			      DRM_MODE_MATCH_FLAGS |
   1071  1.10  riastrad 			      DRM_MODE_MATCH_3D_FLAGS|
   1072  1.10  riastrad 			      DRM_MODE_MATCH_ASPECT_RATIO);
   1073   1.4  riastrad }
   1074   1.4  riastrad EXPORT_SYMBOL(drm_mode_equal);
   1075   1.4  riastrad 
   1076   1.4  riastrad /**
   1077  1.10  riastrad  * drm_mode_equal_no_clocks - test modes for equality
   1078  1.10  riastrad  * @mode1: first mode
   1079  1.10  riastrad  * @mode2: second mode
   1080  1.10  riastrad  *
   1081  1.10  riastrad  * Check to see if @mode1 and @mode2 are equivalent, but
   1082  1.10  riastrad  * don't check the pixel clocks.
   1083  1.10  riastrad  *
   1084  1.10  riastrad  * Returns:
   1085  1.10  riastrad  * True if the modes are equal, false otherwise.
   1086  1.10  riastrad  */
   1087  1.10  riastrad bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1,
   1088  1.10  riastrad 			      const struct drm_display_mode *mode2)
   1089  1.10  riastrad {
   1090  1.10  riastrad 	return drm_mode_match(mode1, mode2,
   1091  1.10  riastrad 			      DRM_MODE_MATCH_TIMINGS |
   1092  1.10  riastrad 			      DRM_MODE_MATCH_FLAGS |
   1093  1.10  riastrad 			      DRM_MODE_MATCH_3D_FLAGS);
   1094  1.10  riastrad }
   1095  1.10  riastrad EXPORT_SYMBOL(drm_mode_equal_no_clocks);
   1096  1.10  riastrad 
   1097  1.10  riastrad /**
   1098   1.4  riastrad  * drm_mode_equal_no_clocks_no_stereo - test modes for equality
   1099   1.4  riastrad  * @mode1: first mode
   1100   1.4  riastrad  * @mode2: second mode
   1101   1.4  riastrad  *
   1102   1.4  riastrad  * Check to see if @mode1 and @mode2 are equivalent, but
   1103   1.4  riastrad  * don't check the pixel clocks nor the stereo layout.
   1104   1.4  riastrad  *
   1105   1.4  riastrad  * Returns:
   1106   1.4  riastrad  * True if the modes are equal, false otherwise.
   1107   1.4  riastrad  */
   1108   1.4  riastrad bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
   1109   1.4  riastrad 					const struct drm_display_mode *mode2)
   1110   1.4  riastrad {
   1111  1.10  riastrad 	return drm_mode_match(mode1, mode2,
   1112  1.10  riastrad 			      DRM_MODE_MATCH_TIMINGS |
   1113  1.10  riastrad 			      DRM_MODE_MATCH_FLAGS);
   1114   1.1  riastrad }
   1115   1.4  riastrad EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
   1116   1.1  riastrad 
   1117  1.10  riastrad static enum drm_mode_status
   1118   1.7  riastrad drm_mode_validate_basic(const struct drm_display_mode *mode)
   1119   1.7  riastrad {
   1120  1.10  riastrad 	if (mode->type & ~DRM_MODE_TYPE_ALL)
   1121  1.10  riastrad 		return MODE_BAD;
   1122  1.10  riastrad 
   1123  1.10  riastrad 	if (mode->flags & ~DRM_MODE_FLAG_ALL)
   1124  1.10  riastrad 		return MODE_BAD;
   1125  1.10  riastrad 
   1126  1.10  riastrad 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
   1127  1.10  riastrad 		return MODE_BAD;
   1128  1.10  riastrad 
   1129   1.7  riastrad 	if (mode->clock == 0)
   1130   1.7  riastrad 		return MODE_CLOCK_LOW;
   1131   1.7  riastrad 
   1132   1.7  riastrad 	if (mode->hdisplay == 0 ||
   1133   1.7  riastrad 	    mode->hsync_start < mode->hdisplay ||
   1134   1.7  riastrad 	    mode->hsync_end < mode->hsync_start ||
   1135   1.7  riastrad 	    mode->htotal < mode->hsync_end)
   1136   1.7  riastrad 		return MODE_H_ILLEGAL;
   1137   1.7  riastrad 
   1138   1.7  riastrad 	if (mode->vdisplay == 0 ||
   1139   1.7  riastrad 	    mode->vsync_start < mode->vdisplay ||
   1140   1.7  riastrad 	    mode->vsync_end < mode->vsync_start ||
   1141   1.7  riastrad 	    mode->vtotal < mode->vsync_end)
   1142   1.7  riastrad 		return MODE_V_ILLEGAL;
   1143   1.7  riastrad 
   1144   1.7  riastrad 	return MODE_OK;
   1145   1.7  riastrad }
   1146  1.10  riastrad 
   1147  1.10  riastrad /**
   1148  1.10  riastrad  * drm_mode_validate_driver - make sure the mode is somewhat sane
   1149  1.10  riastrad  * @dev: drm device
   1150  1.10  riastrad  * @mode: mode to check
   1151  1.10  riastrad  *
   1152  1.10  riastrad  * First do basic validation on the mode, and then allow the driver
   1153  1.10  riastrad  * to check for device/driver specific limitations via the optional
   1154  1.10  riastrad  * &drm_mode_config_helper_funcs.mode_valid hook.
   1155  1.10  riastrad  *
   1156  1.10  riastrad  * Returns:
   1157  1.10  riastrad  * The mode status
   1158  1.10  riastrad  */
   1159  1.10  riastrad enum drm_mode_status
   1160  1.10  riastrad drm_mode_validate_driver(struct drm_device *dev,
   1161  1.10  riastrad 			const struct drm_display_mode *mode)
   1162  1.10  riastrad {
   1163  1.10  riastrad 	enum drm_mode_status status;
   1164  1.10  riastrad 
   1165  1.10  riastrad 	status = drm_mode_validate_basic(mode);
   1166  1.10  riastrad 	if (status != MODE_OK)
   1167  1.10  riastrad 		return status;
   1168  1.10  riastrad 
   1169  1.10  riastrad 	if (dev->mode_config.funcs->mode_valid)
   1170  1.10  riastrad 		return dev->mode_config.funcs->mode_valid(dev, mode);
   1171  1.10  riastrad 	else
   1172  1.10  riastrad 		return MODE_OK;
   1173  1.10  riastrad }
   1174  1.10  riastrad EXPORT_SYMBOL(drm_mode_validate_driver);
   1175   1.7  riastrad 
   1176   1.7  riastrad /**
   1177   1.1  riastrad  * drm_mode_validate_size - make sure modes adhere to size constraints
   1178   1.7  riastrad  * @mode: mode to check
   1179   1.1  riastrad  * @maxX: maximum width
   1180   1.1  riastrad  * @maxY: maximum height
   1181   1.1  riastrad  *
   1182   1.4  riastrad  * This function is a helper which can be used to validate modes against size
   1183   1.4  riastrad  * limitations of the DRM device/connector. If a mode is too big its status
   1184   1.7  riastrad  * member is updated with the appropriate validation failure code. The list
   1185   1.4  riastrad  * itself is not changed.
   1186   1.7  riastrad  *
   1187   1.7  riastrad  * Returns:
   1188   1.7  riastrad  * The mode status
   1189   1.1  riastrad  */
   1190   1.7  riastrad enum drm_mode_status
   1191   1.7  riastrad drm_mode_validate_size(const struct drm_display_mode *mode,
   1192   1.7  riastrad 		       int maxX, int maxY)
   1193   1.1  riastrad {
   1194   1.7  riastrad 	if (maxX > 0 && mode->hdisplay > maxX)
   1195   1.7  riastrad 		return MODE_VIRTUAL_X;
   1196   1.1  riastrad 
   1197   1.9       wiz #if defined(DRM_MAX_RESOLUTION_HORIZONTAL)
   1198   1.9       wiz 	if (mode->hdisplay > DRM_MAX_RESOLUTION_HORIZONTAL)
   1199   1.9       wiz 		return MODE_VIRTUAL_X;
   1200   1.9       wiz #endif
   1201   1.9       wiz 
   1202   1.7  riastrad 	if (maxY > 0 && mode->vdisplay > maxY)
   1203   1.7  riastrad 		return MODE_VIRTUAL_Y;
   1204   1.1  riastrad 
   1205   1.9       wiz #if defined(DRM_MAX_RESOLUTION_VERTICAL)
   1206   1.9       wiz 	if (mode->vdisplay > DRM_MAX_RESOLUTION_VERTICAL)
   1207   1.9       wiz 		return MODE_VIRTUAL_Y;
   1208   1.9       wiz #endif
   1209   1.9       wiz 
   1210   1.7  riastrad 	return MODE_OK;
   1211   1.1  riastrad }
   1212   1.1  riastrad EXPORT_SYMBOL(drm_mode_validate_size);
   1213   1.1  riastrad 
   1214  1.10  riastrad /**
   1215  1.10  riastrad  * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
   1216  1.10  riastrad  * @mode: mode to check
   1217  1.10  riastrad  * @connector: drm connector under action
   1218  1.10  riastrad  *
   1219  1.10  riastrad  * This function is a helper which can be used to filter out any YCBCR420
   1220  1.10  riastrad  * only mode, when the source doesn't support it.
   1221  1.10  riastrad  *
   1222  1.10  riastrad  * Returns:
   1223  1.10  riastrad  * The mode status
   1224  1.10  riastrad  */
   1225  1.10  riastrad enum drm_mode_status
   1226  1.10  riastrad drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
   1227  1.10  riastrad 			   struct drm_connector *connector)
   1228  1.10  riastrad {
   1229  1.10  riastrad 	u8 vic = drm_match_cea_mode(mode);
   1230  1.10  riastrad 	enum drm_mode_status status = MODE_OK;
   1231  1.10  riastrad 	struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
   1232  1.10  riastrad 
   1233  1.10  riastrad 	if (test_bit(vic, hdmi->y420_vdb_modes)) {
   1234  1.10  riastrad 		if (!connector->ycbcr_420_allowed)
   1235  1.10  riastrad 			status = MODE_NO_420;
   1236  1.10  riastrad 	}
   1237  1.10  riastrad 
   1238  1.10  riastrad 	return status;
   1239  1.10  riastrad }
   1240  1.10  riastrad EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
   1241  1.10  riastrad 
   1242   1.7  riastrad #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
   1243   1.7  riastrad 
   1244   1.7  riastrad static const char * const drm_mode_status_names[] = {
   1245   1.7  riastrad 	MODE_STATUS(OK),
   1246   1.7  riastrad 	MODE_STATUS(HSYNC),
   1247   1.7  riastrad 	MODE_STATUS(VSYNC),
   1248   1.7  riastrad 	MODE_STATUS(H_ILLEGAL),
   1249   1.7  riastrad 	MODE_STATUS(V_ILLEGAL),
   1250   1.7  riastrad 	MODE_STATUS(BAD_WIDTH),
   1251   1.7  riastrad 	MODE_STATUS(NOMODE),
   1252   1.7  riastrad 	MODE_STATUS(NO_INTERLACE),
   1253   1.7  riastrad 	MODE_STATUS(NO_DBLESCAN),
   1254   1.7  riastrad 	MODE_STATUS(NO_VSCAN),
   1255   1.7  riastrad 	MODE_STATUS(MEM),
   1256   1.7  riastrad 	MODE_STATUS(VIRTUAL_X),
   1257   1.7  riastrad 	MODE_STATUS(VIRTUAL_Y),
   1258   1.7  riastrad 	MODE_STATUS(MEM_VIRT),
   1259   1.7  riastrad 	MODE_STATUS(NOCLOCK),
   1260   1.7  riastrad 	MODE_STATUS(CLOCK_HIGH),
   1261   1.7  riastrad 	MODE_STATUS(CLOCK_LOW),
   1262   1.7  riastrad 	MODE_STATUS(CLOCK_RANGE),
   1263   1.7  riastrad 	MODE_STATUS(BAD_HVALUE),
   1264   1.7  riastrad 	MODE_STATUS(BAD_VVALUE),
   1265   1.7  riastrad 	MODE_STATUS(BAD_VSCAN),
   1266   1.7  riastrad 	MODE_STATUS(HSYNC_NARROW),
   1267   1.7  riastrad 	MODE_STATUS(HSYNC_WIDE),
   1268   1.7  riastrad 	MODE_STATUS(HBLANK_NARROW),
   1269   1.7  riastrad 	MODE_STATUS(HBLANK_WIDE),
   1270   1.7  riastrad 	MODE_STATUS(VSYNC_NARROW),
   1271   1.7  riastrad 	MODE_STATUS(VSYNC_WIDE),
   1272   1.7  riastrad 	MODE_STATUS(VBLANK_NARROW),
   1273   1.7  riastrad 	MODE_STATUS(VBLANK_WIDE),
   1274   1.7  riastrad 	MODE_STATUS(PANEL),
   1275   1.7  riastrad 	MODE_STATUS(INTERLACE_WIDTH),
   1276   1.7  riastrad 	MODE_STATUS(ONE_WIDTH),
   1277   1.7  riastrad 	MODE_STATUS(ONE_HEIGHT),
   1278   1.7  riastrad 	MODE_STATUS(ONE_SIZE),
   1279   1.7  riastrad 	MODE_STATUS(NO_REDUCED),
   1280   1.7  riastrad 	MODE_STATUS(NO_STEREO),
   1281  1.10  riastrad 	MODE_STATUS(NO_420),
   1282  1.10  riastrad 	MODE_STATUS(STALE),
   1283   1.7  riastrad 	MODE_STATUS(BAD),
   1284   1.7  riastrad 	MODE_STATUS(ERROR),
   1285   1.7  riastrad };
   1286   1.7  riastrad 
   1287   1.7  riastrad #undef MODE_STATUS
   1288   1.7  riastrad 
   1289  1.10  riastrad const char *drm_get_mode_status_name(enum drm_mode_status status)
   1290   1.7  riastrad {
   1291   1.7  riastrad 	int index = status + 3;
   1292   1.7  riastrad 
   1293   1.7  riastrad 	if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
   1294   1.7  riastrad 		return "";
   1295   1.7  riastrad 
   1296   1.7  riastrad 	return drm_mode_status_names[index];
   1297   1.7  riastrad }
   1298   1.7  riastrad 
   1299   1.1  riastrad /**
   1300   1.1  riastrad  * drm_mode_prune_invalid - remove invalid modes from mode list
   1301   1.1  riastrad  * @dev: DRM device
   1302   1.1  riastrad  * @mode_list: list of modes to check
   1303   1.1  riastrad  * @verbose: be verbose about it
   1304   1.1  riastrad  *
   1305   1.4  riastrad  * This helper function can be used to prune a display mode list after
   1306  1.10  riastrad  * validation has been completed. All modes whose status is not MODE_OK will be
   1307   1.4  riastrad  * removed from the list, and if @verbose the status code and mode name is also
   1308   1.4  riastrad  * printed to dmesg.
   1309   1.1  riastrad  */
   1310   1.1  riastrad void drm_mode_prune_invalid(struct drm_device *dev,
   1311   1.1  riastrad 			    struct list_head *mode_list, bool verbose)
   1312   1.1  riastrad {
   1313   1.1  riastrad 	struct drm_display_mode *mode, *t;
   1314   1.1  riastrad 
   1315   1.1  riastrad 	list_for_each_entry_safe(mode, t, mode_list, head) {
   1316   1.1  riastrad 		if (mode->status != MODE_OK) {
   1317   1.1  riastrad 			list_del(&mode->head);
   1318   1.1  riastrad 			if (verbose) {
   1319   1.1  riastrad 				drm_mode_debug_printmodeline(mode);
   1320   1.7  riastrad 				DRM_DEBUG_KMS("Not using %s mode: %s\n",
   1321   1.7  riastrad 					      mode->name,
   1322   1.7  riastrad 					      drm_get_mode_status_name(mode->status));
   1323   1.1  riastrad 			}
   1324   1.1  riastrad 			drm_mode_destroy(dev, mode);
   1325   1.1  riastrad 		}
   1326   1.1  riastrad 	}
   1327   1.1  riastrad }
   1328   1.1  riastrad EXPORT_SYMBOL(drm_mode_prune_invalid);
   1329   1.1  riastrad 
   1330   1.1  riastrad /**
   1331   1.1  riastrad  * drm_mode_compare - compare modes for favorability
   1332   1.1  riastrad  * @priv: unused
   1333   1.1  riastrad  * @lh_a: list_head for first mode
   1334   1.1  riastrad  * @lh_b: list_head for second mode
   1335   1.1  riastrad  *
   1336   1.1  riastrad  * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
   1337   1.1  riastrad  * which is better.
   1338   1.1  riastrad  *
   1339   1.4  riastrad  * Returns:
   1340   1.1  riastrad  * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
   1341   1.1  riastrad  * positive if @lh_b is better than @lh_a.
   1342   1.1  riastrad  */
   1343   1.1  riastrad static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
   1344   1.1  riastrad {
   1345   1.1  riastrad 	struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
   1346   1.1  riastrad 	struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
   1347   1.1  riastrad 	int diff;
   1348   1.1  riastrad 
   1349   1.1  riastrad 	diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
   1350   1.1  riastrad 		((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
   1351   1.1  riastrad 	if (diff)
   1352   1.1  riastrad 		return diff;
   1353   1.1  riastrad 	diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
   1354   1.1  riastrad 	if (diff)
   1355   1.1  riastrad 		return diff;
   1356   1.4  riastrad 
   1357   1.4  riastrad 	diff = b->vrefresh - a->vrefresh;
   1358   1.4  riastrad 	if (diff)
   1359   1.4  riastrad 		return diff;
   1360   1.4  riastrad 
   1361   1.1  riastrad 	diff = b->clock - a->clock;
   1362   1.1  riastrad 	return diff;
   1363   1.1  riastrad }
   1364   1.1  riastrad 
   1365   1.1  riastrad /**
   1366   1.1  riastrad  * drm_mode_sort - sort mode list
   1367   1.4  riastrad  * @mode_list: list of drm_display_mode structures to sort
   1368   1.1  riastrad  *
   1369   1.4  riastrad  * Sort @mode_list by favorability, moving good modes to the head of the list.
   1370   1.1  riastrad  */
   1371   1.1  riastrad void drm_mode_sort(struct list_head *mode_list)
   1372   1.1  riastrad {
   1373   1.1  riastrad 	list_sort(NULL, mode_list, drm_mode_compare);
   1374   1.1  riastrad }
   1375   1.1  riastrad EXPORT_SYMBOL(drm_mode_sort);
   1376   1.1  riastrad 
   1377   1.1  riastrad /**
   1378  1.10  riastrad  * drm_connector_list_update - update the mode list for the connector
   1379   1.1  riastrad  * @connector: the connector to update
   1380   1.1  riastrad  *
   1381   1.1  riastrad  * This moves the modes from the @connector probed_modes list
   1382   1.1  riastrad  * to the actual mode list. It compares the probed mode against the current
   1383   1.4  riastrad  * list and only adds different/new modes.
   1384   1.4  riastrad  *
   1385   1.4  riastrad  * This is just a helper functions doesn't validate any modes itself and also
   1386   1.4  riastrad  * doesn't prune any invalid modes. Callers need to do that themselves.
   1387   1.1  riastrad  */
   1388  1.10  riastrad void drm_connector_list_update(struct drm_connector *connector)
   1389   1.1  riastrad {
   1390   1.1  riastrad 	struct drm_display_mode *pmode, *pt;
   1391   1.1  riastrad 
   1392   1.4  riastrad 	WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
   1393   1.4  riastrad 
   1394  1.10  riastrad 	list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
   1395  1.10  riastrad 		struct drm_display_mode *mode;
   1396  1.10  riastrad 		bool found_it = false;
   1397  1.10  riastrad 
   1398   1.1  riastrad 		/* go through current modes checking for the new probed mode */
   1399   1.1  riastrad 		list_for_each_entry(mode, &connector->modes, head) {
   1400  1.10  riastrad 			if (!drm_mode_equal(pmode, mode))
   1401  1.10  riastrad 				continue;
   1402  1.10  riastrad 
   1403  1.10  riastrad 			found_it = true;
   1404  1.10  riastrad 
   1405  1.10  riastrad 			/*
   1406  1.10  riastrad 			 * If the old matching mode is stale (ie. left over
   1407  1.10  riastrad 			 * from a previous probe) just replace it outright.
   1408  1.10  riastrad 			 * Otherwise just merge the type bits between all
   1409  1.10  riastrad 			 * equal probed modes.
   1410  1.10  riastrad 			 *
   1411  1.10  riastrad 			 * If two probed modes are considered equal, pick the
   1412  1.10  riastrad 			 * actual timings from the one that's marked as
   1413  1.10  riastrad 			 * preferred (in case the match isn't 100%). If
   1414  1.10  riastrad 			 * multiple or zero preferred modes are present, favor
   1415  1.10  riastrad 			 * the mode added to the probed_modes list first.
   1416  1.10  riastrad 			 */
   1417  1.10  riastrad 			if (mode->status == MODE_STALE) {
   1418  1.10  riastrad 				drm_mode_copy(mode, pmode);
   1419  1.10  riastrad 			} else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
   1420  1.10  riastrad 				   (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
   1421  1.10  riastrad 				pmode->type |= mode->type;
   1422  1.10  riastrad 				drm_mode_copy(mode, pmode);
   1423  1.10  riastrad 			} else {
   1424  1.10  riastrad 				mode->type |= pmode->type;
   1425   1.1  riastrad 			}
   1426  1.10  riastrad 
   1427  1.10  riastrad 			list_del(&pmode->head);
   1428  1.10  riastrad 			drm_mode_destroy(connector->dev, pmode);
   1429  1.10  riastrad 			break;
   1430   1.1  riastrad 		}
   1431   1.1  riastrad 
   1432   1.1  riastrad 		if (!found_it) {
   1433   1.1  riastrad 			list_move_tail(&pmode->head, &connector->modes);
   1434   1.1  riastrad 		}
   1435   1.1  riastrad 	}
   1436   1.1  riastrad }
   1437  1.10  riastrad EXPORT_SYMBOL(drm_connector_list_update);
   1438  1.10  riastrad 
   1439  1.10  riastrad static int drm_mode_parse_cmdline_bpp(const char *str, char **end_ptr,
   1440  1.10  riastrad 				      struct drm_cmdline_mode *mode)
   1441  1.10  riastrad {
   1442  1.10  riastrad 	unsigned int bpp;
   1443  1.10  riastrad 
   1444  1.10  riastrad 	if (str[0] != '-')
   1445  1.10  riastrad 		return -EINVAL;
   1446  1.10  riastrad 
   1447  1.10  riastrad 	str++;
   1448  1.10  riastrad 	bpp = simple_strtol(str, end_ptr, 10);
   1449  1.10  riastrad 	if (*end_ptr == str)
   1450  1.10  riastrad 		return -EINVAL;
   1451  1.10  riastrad 
   1452  1.10  riastrad 	mode->bpp = bpp;
   1453  1.10  riastrad 	mode->bpp_specified = true;
   1454  1.10  riastrad 
   1455  1.10  riastrad 	return 0;
   1456  1.10  riastrad }
   1457  1.10  riastrad 
   1458  1.10  riastrad static int drm_mode_parse_cmdline_refresh(const char *str, char **end_ptr,
   1459  1.10  riastrad 					  struct drm_cmdline_mode *mode)
   1460  1.10  riastrad {
   1461  1.10  riastrad 	unsigned int refresh;
   1462  1.10  riastrad 
   1463  1.10  riastrad 	if (str[0] != '@')
   1464  1.10  riastrad 		return -EINVAL;
   1465  1.10  riastrad 
   1466  1.10  riastrad 	str++;
   1467  1.10  riastrad 	refresh = simple_strtol(str, end_ptr, 10);
   1468  1.10  riastrad 	if (*end_ptr == str)
   1469  1.10  riastrad 		return -EINVAL;
   1470  1.10  riastrad 
   1471  1.10  riastrad 	mode->refresh = refresh;
   1472  1.10  riastrad 	mode->refresh_specified = true;
   1473  1.10  riastrad 
   1474  1.10  riastrad 	return 0;
   1475  1.10  riastrad }
   1476  1.10  riastrad 
   1477  1.10  riastrad static int drm_mode_parse_cmdline_extra(const char *str, int length,
   1478  1.10  riastrad 					bool freestanding,
   1479  1.10  riastrad 					const struct drm_connector *connector,
   1480  1.10  riastrad 					struct drm_cmdline_mode *mode)
   1481  1.10  riastrad {
   1482  1.10  riastrad 	int i;
   1483  1.10  riastrad 
   1484  1.10  riastrad 	for (i = 0; i < length; i++) {
   1485  1.10  riastrad 		switch (str[i]) {
   1486  1.10  riastrad 		case 'i':
   1487  1.10  riastrad 			if (freestanding)
   1488  1.10  riastrad 				return -EINVAL;
   1489  1.10  riastrad 
   1490  1.10  riastrad 			mode->interlace = true;
   1491  1.10  riastrad 			break;
   1492  1.10  riastrad 		case 'm':
   1493  1.10  riastrad 			if (freestanding)
   1494  1.10  riastrad 				return -EINVAL;
   1495  1.10  riastrad 
   1496  1.10  riastrad 			mode->margins = true;
   1497  1.10  riastrad 			break;
   1498  1.10  riastrad 		case 'D':
   1499  1.10  riastrad 			if (mode->force != DRM_FORCE_UNSPECIFIED)
   1500  1.10  riastrad 				return -EINVAL;
   1501  1.10  riastrad 
   1502  1.10  riastrad 			if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
   1503  1.10  riastrad 			    (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
   1504  1.10  riastrad 				mode->force = DRM_FORCE_ON;
   1505  1.10  riastrad 			else
   1506  1.10  riastrad 				mode->force = DRM_FORCE_ON_DIGITAL;
   1507  1.10  riastrad 			break;
   1508  1.10  riastrad 		case 'd':
   1509  1.10  riastrad 			if (mode->force != DRM_FORCE_UNSPECIFIED)
   1510  1.10  riastrad 				return -EINVAL;
   1511  1.10  riastrad 
   1512  1.10  riastrad 			mode->force = DRM_FORCE_OFF;
   1513  1.10  riastrad 			break;
   1514  1.10  riastrad 		case 'e':
   1515  1.10  riastrad 			if (mode->force != DRM_FORCE_UNSPECIFIED)
   1516  1.10  riastrad 				return -EINVAL;
   1517  1.10  riastrad 
   1518  1.10  riastrad 			mode->force = DRM_FORCE_ON;
   1519  1.10  riastrad 			break;
   1520  1.10  riastrad 		default:
   1521  1.10  riastrad 			return -EINVAL;
   1522  1.10  riastrad 		}
   1523  1.10  riastrad 	}
   1524  1.10  riastrad 
   1525  1.10  riastrad 	return 0;
   1526  1.10  riastrad }
   1527  1.10  riastrad 
   1528  1.10  riastrad static int drm_mode_parse_cmdline_res_mode(const char *str, unsigned int length,
   1529  1.10  riastrad 					   bool extras,
   1530  1.10  riastrad 					   const struct drm_connector *connector,
   1531  1.10  riastrad 					   struct drm_cmdline_mode *mode)
   1532  1.10  riastrad {
   1533  1.10  riastrad 	const char *str_start = str;
   1534  1.10  riastrad 	bool rb = false, cvt = false;
   1535  1.10  riastrad 	int xres = 0, yres = 0;
   1536  1.10  riastrad 	int remaining, i;
   1537  1.10  riastrad 	char *end_ptr;
   1538  1.10  riastrad 
   1539  1.10  riastrad 	xres = simple_strtol(str, &end_ptr, 10);
   1540  1.10  riastrad 	if (end_ptr == str)
   1541  1.10  riastrad 		return -EINVAL;
   1542  1.10  riastrad 
   1543  1.10  riastrad 	if (end_ptr[0] != 'x')
   1544  1.10  riastrad 		return -EINVAL;
   1545  1.10  riastrad 	end_ptr++;
   1546  1.10  riastrad 
   1547  1.10  riastrad 	str = end_ptr;
   1548  1.10  riastrad 	yres = simple_strtol(str, &end_ptr, 10);
   1549  1.10  riastrad 	if (end_ptr == str)
   1550  1.10  riastrad 		return -EINVAL;
   1551  1.10  riastrad 
   1552  1.10  riastrad 	remaining = length - (end_ptr - str_start);
   1553  1.10  riastrad 	if (remaining < 0)
   1554  1.10  riastrad 		return -EINVAL;
   1555  1.10  riastrad 
   1556  1.10  riastrad 	for (i = 0; i < remaining; i++) {
   1557  1.10  riastrad 		switch (end_ptr[i]) {
   1558  1.10  riastrad 		case 'M':
   1559  1.10  riastrad 			cvt = true;
   1560  1.10  riastrad 			break;
   1561  1.10  riastrad 		case 'R':
   1562  1.10  riastrad 			rb = true;
   1563  1.10  riastrad 			break;
   1564  1.10  riastrad 		default:
   1565  1.10  riastrad 			/*
   1566  1.10  riastrad 			 * Try to pass that to our extras parsing
   1567  1.10  riastrad 			 * function to handle the case where the
   1568  1.10  riastrad 			 * extras are directly after the resolution
   1569  1.10  riastrad 			 */
   1570  1.10  riastrad 			if (extras) {
   1571  1.10  riastrad 				int ret = drm_mode_parse_cmdline_extra(end_ptr + i,
   1572  1.10  riastrad 								       1,
   1573  1.10  riastrad 								       false,
   1574  1.10  riastrad 								       connector,
   1575  1.10  riastrad 								       mode);
   1576  1.10  riastrad 				if (ret)
   1577  1.10  riastrad 					return ret;
   1578  1.10  riastrad 			} else {
   1579  1.10  riastrad 				return -EINVAL;
   1580  1.10  riastrad 			}
   1581  1.10  riastrad 		}
   1582  1.10  riastrad 	}
   1583  1.10  riastrad 
   1584  1.10  riastrad 	mode->xres = xres;
   1585  1.10  riastrad 	mode->yres = yres;
   1586  1.10  riastrad 	mode->cvt = cvt;
   1587  1.10  riastrad 	mode->rb = rb;
   1588  1.10  riastrad 
   1589  1.10  riastrad 	return 0;
   1590  1.10  riastrad }
   1591  1.10  riastrad 
   1592  1.10  riastrad static int drm_mode_parse_cmdline_int(const char *delim, unsigned int *int_ret)
   1593  1.10  riastrad {
   1594  1.10  riastrad 	const char *value;
   1595  1.10  riastrad 	char *endp;
   1596  1.10  riastrad 
   1597  1.10  riastrad 	/*
   1598  1.10  riastrad 	 * delim must point to the '=', otherwise it is a syntax error and
   1599  1.10  riastrad 	 * if delim points to the terminating zero, then delim + 1 wil point
   1600  1.10  riastrad 	 * past the end of the string.
   1601  1.10  riastrad 	 */
   1602  1.10  riastrad 	if (*delim != '=')
   1603  1.10  riastrad 		return -EINVAL;
   1604  1.10  riastrad 
   1605  1.10  riastrad 	value = delim + 1;
   1606  1.10  riastrad 	*int_ret = simple_strtol(value, &endp, 10);
   1607  1.10  riastrad 
   1608  1.10  riastrad 	/* Make sure we have parsed something */
   1609  1.10  riastrad 	if (endp == value)
   1610  1.10  riastrad 		return -EINVAL;
   1611  1.10  riastrad 
   1612  1.10  riastrad 	return 0;
   1613  1.10  riastrad }
   1614  1.10  riastrad 
   1615  1.10  riastrad static int drm_mode_parse_panel_orientation(const char *delim,
   1616  1.10  riastrad 					    struct drm_cmdline_mode *mode)
   1617  1.10  riastrad {
   1618  1.10  riastrad 	const char *value;
   1619  1.10  riastrad 
   1620  1.10  riastrad 	if (*delim != '=')
   1621  1.10  riastrad 		return -EINVAL;
   1622  1.10  riastrad 
   1623  1.10  riastrad 	value = delim + 1;
   1624  1.10  riastrad 	delim = strchr(value, ',');
   1625  1.10  riastrad 	if (!delim)
   1626  1.10  riastrad 		delim = value + strlen(value);
   1627  1.10  riastrad 
   1628  1.10  riastrad 	if (!strncmp(value, "normal", delim - value))
   1629  1.10  riastrad 		mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
   1630  1.10  riastrad 	else if (!strncmp(value, "upside_down", delim - value))
   1631  1.10  riastrad 		mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
   1632  1.10  riastrad 	else if (!strncmp(value, "left_side_up", delim - value))
   1633  1.10  riastrad 		mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
   1634  1.10  riastrad 	else if (!strncmp(value, "right_side_up", delim - value))
   1635  1.10  riastrad 		mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
   1636  1.10  riastrad 	else
   1637  1.10  riastrad 		return -EINVAL;
   1638  1.10  riastrad 
   1639  1.10  riastrad 	return 0;
   1640  1.10  riastrad }
   1641  1.10  riastrad 
   1642  1.10  riastrad static int drm_mode_parse_cmdline_options(const char *str,
   1643  1.10  riastrad 					  bool freestanding,
   1644  1.10  riastrad 					  const struct drm_connector *connector,
   1645  1.10  riastrad 					  struct drm_cmdline_mode *mode)
   1646  1.10  riastrad {
   1647  1.10  riastrad 	unsigned int deg, margin, rotation = 0;
   1648  1.10  riastrad 	const char *delim, *option, *sep;
   1649  1.10  riastrad 
   1650  1.10  riastrad 	option = str;
   1651  1.10  riastrad 	do {
   1652  1.10  riastrad 		delim = strchr(option, '=');
   1653  1.10  riastrad 		if (!delim) {
   1654  1.10  riastrad 			delim = strchr(option, ',');
   1655  1.10  riastrad 
   1656  1.10  riastrad 			if (!delim)
   1657  1.10  riastrad 				delim = option + strlen(option);
   1658  1.10  riastrad 		}
   1659  1.10  riastrad 
   1660  1.10  riastrad 		if (!strncmp(option, "rotate", delim - option)) {
   1661  1.10  riastrad 			if (drm_mode_parse_cmdline_int(delim, &deg))
   1662  1.10  riastrad 				return -EINVAL;
   1663  1.10  riastrad 
   1664  1.10  riastrad 			switch (deg) {
   1665  1.10  riastrad 			case 0:
   1666  1.10  riastrad 				rotation |= DRM_MODE_ROTATE_0;
   1667  1.10  riastrad 				break;
   1668  1.10  riastrad 
   1669  1.10  riastrad 			case 90:
   1670  1.10  riastrad 				rotation |= DRM_MODE_ROTATE_90;
   1671  1.10  riastrad 				break;
   1672  1.10  riastrad 
   1673  1.10  riastrad 			case 180:
   1674  1.10  riastrad 				rotation |= DRM_MODE_ROTATE_180;
   1675  1.10  riastrad 				break;
   1676  1.10  riastrad 
   1677  1.10  riastrad 			case 270:
   1678  1.10  riastrad 				rotation |= DRM_MODE_ROTATE_270;
   1679  1.10  riastrad 				break;
   1680  1.10  riastrad 
   1681  1.10  riastrad 			default:
   1682  1.10  riastrad 				return -EINVAL;
   1683  1.10  riastrad 			}
   1684  1.10  riastrad 		} else if (!strncmp(option, "reflect_x", delim - option)) {
   1685  1.10  riastrad 			rotation |= DRM_MODE_REFLECT_X;
   1686  1.10  riastrad 		} else if (!strncmp(option, "reflect_y", delim - option)) {
   1687  1.10  riastrad 			rotation |= DRM_MODE_REFLECT_Y;
   1688  1.10  riastrad 		} else if (!strncmp(option, "margin_right", delim - option)) {
   1689  1.10  riastrad 			if (drm_mode_parse_cmdline_int(delim, &margin))
   1690  1.10  riastrad 				return -EINVAL;
   1691  1.10  riastrad 
   1692  1.10  riastrad 			mode->tv_margins.right = margin;
   1693  1.10  riastrad 		} else if (!strncmp(option, "margin_left", delim - option)) {
   1694  1.10  riastrad 			if (drm_mode_parse_cmdline_int(delim, &margin))
   1695  1.10  riastrad 				return -EINVAL;
   1696  1.10  riastrad 
   1697  1.10  riastrad 			mode->tv_margins.left = margin;
   1698  1.10  riastrad 		} else if (!strncmp(option, "margin_top", delim - option)) {
   1699  1.10  riastrad 			if (drm_mode_parse_cmdline_int(delim, &margin))
   1700  1.10  riastrad 				return -EINVAL;
   1701  1.10  riastrad 
   1702  1.10  riastrad 			mode->tv_margins.top = margin;
   1703  1.10  riastrad 		} else if (!strncmp(option, "margin_bottom", delim - option)) {
   1704  1.10  riastrad 			if (drm_mode_parse_cmdline_int(delim, &margin))
   1705  1.10  riastrad 				return -EINVAL;
   1706  1.10  riastrad 
   1707  1.10  riastrad 			mode->tv_margins.bottom = margin;
   1708  1.10  riastrad 		} else if (!strncmp(option, "panel_orientation", delim - option)) {
   1709  1.10  riastrad 			if (drm_mode_parse_panel_orientation(delim, mode))
   1710  1.10  riastrad 				return -EINVAL;
   1711  1.10  riastrad 		} else {
   1712  1.10  riastrad 			return -EINVAL;
   1713  1.10  riastrad 		}
   1714  1.10  riastrad 		sep = strchr(delim, ',');
   1715  1.10  riastrad 		option = sep + 1;
   1716  1.10  riastrad 	} while (sep);
   1717  1.10  riastrad 
   1718  1.10  riastrad 	if (rotation && freestanding)
   1719  1.10  riastrad 		return -EINVAL;
   1720  1.10  riastrad 
   1721  1.10  riastrad 	if (!(rotation & DRM_MODE_ROTATE_MASK))
   1722  1.10  riastrad 		rotation |= DRM_MODE_ROTATE_0;
   1723  1.10  riastrad 
   1724  1.10  riastrad 	/* Make sure there is exactly one rotation defined */
   1725  1.10  riastrad 	if (!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK))
   1726  1.10  riastrad 		return -EINVAL;
   1727  1.10  riastrad 
   1728  1.10  riastrad 	mode->rotation_reflection = rotation;
   1729  1.10  riastrad 
   1730  1.10  riastrad 	return 0;
   1731  1.10  riastrad }
   1732  1.10  riastrad 
   1733  1.10  riastrad static const char * const drm_named_modes_whitelist[] = {
   1734  1.10  riastrad 	"NTSC",
   1735  1.10  riastrad 	"PAL",
   1736  1.10  riastrad };
   1737   1.1  riastrad 
   1738   1.1  riastrad /**
   1739   1.4  riastrad  * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
   1740   1.4  riastrad  * @mode_option: optional per connector mode option
   1741   1.4  riastrad  * @connector: connector to parse modeline for
   1742   1.4  riastrad  * @mode: preallocated drm_cmdline_mode structure to fill out
   1743   1.4  riastrad  *
   1744   1.4  riastrad  * This parses @mode_option command line modeline for modes and options to
   1745   1.4  riastrad  * configure the connector. If @mode_option is NULL the default command line
   1746   1.4  riastrad  * modeline in fb_mode_option will be parsed instead.
   1747   1.1  riastrad  *
   1748   1.4  riastrad  * This uses the same parameters as the fb modedb.c, except for an extra
   1749  1.10  riastrad  * force-enable, force-enable-digital and force-disable bit at the end::
   1750   1.1  riastrad  *
   1751   1.1  riastrad  *	<xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
   1752   1.1  riastrad  *
   1753  1.10  riastrad  * Additionals options can be provided following the mode, using a comma to
   1754  1.10  riastrad  * separate each option. Valid options can be found in
   1755  1.10  riastrad  * Documentation/fb/modedb.rst.
   1756  1.10  riastrad  *
   1757   1.4  riastrad  * The intermediate drm_cmdline_mode structure is required to store additional
   1758   1.7  riastrad  * options from the command line modline like the force-enable/disable flag.
   1759   1.4  riastrad  *
   1760   1.4  riastrad  * Returns:
   1761   1.4  riastrad  * True if a valid modeline has been parsed, false otherwise.
   1762   1.1  riastrad  */
   1763   1.1  riastrad bool drm_mode_parse_command_line_for_connector(const char *mode_option,
   1764  1.10  riastrad 					       const struct drm_connector *connector,
   1765   1.1  riastrad 					       struct drm_cmdline_mode *mode)
   1766   1.1  riastrad {
   1767   1.1  riastrad 	const char *name;
   1768  1.10  riastrad 	bool freestanding = false, parse_extras = false;
   1769  1.10  riastrad 	unsigned int bpp_off = 0, refresh_off = 0, options_off = 0;
   1770  1.10  riastrad 	unsigned int mode_end = 0;
   1771  1.10  riastrad 	const char *bpp_ptr = NULL, *refresh_ptr = NULL, *extra_ptr = NULL;
   1772  1.10  riastrad 	const char *options_ptr = NULL;
   1773  1.10  riastrad 	char *bpp_end_ptr = NULL, *refresh_end_ptr = NULL;
   1774  1.10  riastrad 	int i, len, ret;
   1775  1.10  riastrad 
   1776  1.10  riastrad 	memset(mode, 0, sizeof(*mode));
   1777  1.10  riastrad 	mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
   1778   1.1  riastrad 
   1779   1.1  riastrad 	if (!mode_option)
   1780   1.1  riastrad 		return false;
   1781   1.1  riastrad 
   1782   1.1  riastrad 	name = mode_option;
   1783   1.1  riastrad 
   1784  1.10  riastrad 	/* Try to locate the bpp and refresh specifiers, if any */
   1785  1.10  riastrad 	bpp_ptr = strchr(name, '-');
   1786  1.10  riastrad 	if (bpp_ptr)
   1787  1.10  riastrad 		bpp_off = bpp_ptr - name;
   1788  1.10  riastrad 
   1789  1.10  riastrad 	refresh_ptr = strchr(name, '@');
   1790  1.10  riastrad 	if (refresh_ptr)
   1791  1.10  riastrad 		refresh_off = refresh_ptr - name;
   1792  1.10  riastrad 
   1793  1.10  riastrad 	/* Locate the start of named options */
   1794  1.10  riastrad 	options_ptr = strchr(name, ',');
   1795  1.10  riastrad 	if (options_ptr)
   1796  1.10  riastrad 		options_off = options_ptr - name;
   1797  1.10  riastrad 
   1798  1.10  riastrad 	/* Locate the end of the name / resolution, and parse it */
   1799  1.10  riastrad 	if (bpp_ptr) {
   1800  1.10  riastrad 		mode_end = bpp_off;
   1801  1.10  riastrad 	} else if (refresh_ptr) {
   1802  1.10  riastrad 		mode_end = refresh_off;
   1803  1.10  riastrad 	} else if (options_ptr) {
   1804  1.10  riastrad 		mode_end = options_off;
   1805  1.10  riastrad 		parse_extras = true;
   1806  1.10  riastrad 	} else {
   1807  1.10  riastrad 		mode_end = strlen(name);
   1808  1.10  riastrad 		parse_extras = true;
   1809  1.10  riastrad 	}
   1810   1.1  riastrad 
   1811  1.10  riastrad 	/* First check for a named mode */
   1812  1.10  riastrad 	for (i = 0; i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
   1813  1.10  riastrad 		ret = str_has_prefix(name, drm_named_modes_whitelist[i]);
   1814  1.10  riastrad 		if (ret == mode_end) {
   1815  1.10  riastrad 			if (refresh_ptr)
   1816  1.10  riastrad 				return false; /* named + refresh is invalid */
   1817   1.1  riastrad 
   1818  1.10  riastrad 			strcpy(mode->name, drm_named_modes_whitelist[i]);
   1819  1.10  riastrad 			mode->specified = true;
   1820   1.1  riastrad 			break;
   1821   1.1  riastrad 		}
   1822   1.1  riastrad 	}
   1823   1.1  riastrad 
   1824  1.10  riastrad 	/* No named mode? Check for a normal mode argument, e.g. 1024x768 */
   1825  1.10  riastrad 	if (!mode->specified && isdigit(name[0])) {
   1826  1.10  riastrad 		ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
   1827  1.10  riastrad 						      parse_extras,
   1828  1.10  riastrad 						      connector,
   1829  1.10  riastrad 						      mode);
   1830  1.10  riastrad 		if (ret)
   1831  1.10  riastrad 			return false;
   1832  1.10  riastrad 
   1833  1.10  riastrad 		mode->specified = true;
   1834  1.10  riastrad 	}
   1835  1.10  riastrad 
   1836  1.10  riastrad 	/* No mode? Check for freestanding extras and/or options */
   1837  1.10  riastrad 	if (!mode->specified) {
   1838  1.10  riastrad 		unsigned int len = strlen(mode_option);
   1839  1.10  riastrad 
   1840  1.10  riastrad 		if (bpp_ptr || refresh_ptr)
   1841  1.10  riastrad 			return false; /* syntax error */
   1842  1.10  riastrad 
   1843  1.10  riastrad 		if (len == 1 || (len >= 2 && mode_option[1] == ','))
   1844  1.10  riastrad 			extra_ptr = mode_option;
   1845   1.1  riastrad 		else
   1846  1.10  riastrad 			options_ptr = mode_option - 1;
   1847  1.10  riastrad 
   1848  1.10  riastrad 		freestanding = true;
   1849   1.1  riastrad 	}
   1850   1.1  riastrad 
   1851  1.10  riastrad 	if (bpp_ptr) {
   1852  1.10  riastrad 		ret = drm_mode_parse_cmdline_bpp(bpp_ptr, &bpp_end_ptr, mode);
   1853  1.10  riastrad 		if (ret)
   1854  1.10  riastrad 			return false;
   1855  1.10  riastrad 
   1856  1.10  riastrad 		mode->bpp_specified = true;
   1857   1.1  riastrad 	}
   1858   1.1  riastrad 
   1859  1.10  riastrad 	if (refresh_ptr) {
   1860  1.10  riastrad 		ret = drm_mode_parse_cmdline_refresh(refresh_ptr,
   1861  1.10  riastrad 						     &refresh_end_ptr, mode);
   1862  1.10  riastrad 		if (ret)
   1863  1.10  riastrad 			return false;
   1864  1.10  riastrad 
   1865   1.1  riastrad 		mode->refresh_specified = true;
   1866   1.1  riastrad 	}
   1867   1.1  riastrad 
   1868  1.10  riastrad 	/*
   1869  1.10  riastrad 	 * Locate the end of the bpp / refresh, and parse the extras
   1870  1.10  riastrad 	 * if relevant
   1871  1.10  riastrad 	 */
   1872  1.10  riastrad 	if (bpp_ptr && refresh_ptr)
   1873  1.10  riastrad 		extra_ptr = max(bpp_end_ptr, refresh_end_ptr);
   1874  1.10  riastrad 	else if (bpp_ptr)
   1875  1.10  riastrad 		extra_ptr = bpp_end_ptr;
   1876  1.10  riastrad 	else if (refresh_ptr)
   1877  1.10  riastrad 		extra_ptr = refresh_end_ptr;
   1878  1.10  riastrad 
   1879  1.10  riastrad 	if (extra_ptr) {
   1880  1.10  riastrad 		if (options_ptr)
   1881  1.10  riastrad 			len = options_ptr - extra_ptr;
   1882  1.10  riastrad 		else
   1883  1.10  riastrad 			len = strlen(extra_ptr);
   1884  1.10  riastrad 
   1885  1.10  riastrad 		ret = drm_mode_parse_cmdline_extra(extra_ptr, len, freestanding,
   1886  1.10  riastrad 						   connector, mode);
   1887  1.10  riastrad 		if (ret)
   1888  1.10  riastrad 			return false;
   1889  1.10  riastrad 	}
   1890  1.10  riastrad 
   1891  1.10  riastrad 	if (options_ptr) {
   1892  1.10  riastrad 		ret = drm_mode_parse_cmdline_options(options_ptr + 1,
   1893  1.10  riastrad 						     freestanding,
   1894  1.10  riastrad 						     connector, mode);
   1895  1.10  riastrad 		if (ret)
   1896  1.10  riastrad 			return false;
   1897   1.1  riastrad 	}
   1898   1.1  riastrad 
   1899   1.1  riastrad 	return true;
   1900   1.1  riastrad }
   1901   1.1  riastrad EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
   1902   1.1  riastrad 
   1903   1.4  riastrad /**
   1904   1.4  riastrad  * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
   1905   1.4  riastrad  * @dev: DRM device to create the new mode for
   1906   1.4  riastrad  * @cmd: input command line modeline
   1907   1.4  riastrad  *
   1908   1.4  riastrad  * Returns:
   1909   1.4  riastrad  * Pointer to converted mode on success, NULL on error.
   1910   1.4  riastrad  */
   1911   1.1  riastrad struct drm_display_mode *
   1912   1.1  riastrad drm_mode_create_from_cmdline_mode(struct drm_device *dev,
   1913   1.1  riastrad 				  struct drm_cmdline_mode *cmd)
   1914   1.1  riastrad {
   1915   1.1  riastrad 	struct drm_display_mode *mode;
   1916   1.1  riastrad 
   1917   1.1  riastrad 	if (cmd->cvt)
   1918   1.1  riastrad 		mode = drm_cvt_mode(dev,
   1919   1.1  riastrad 				    cmd->xres, cmd->yres,
   1920   1.1  riastrad 				    cmd->refresh_specified ? cmd->refresh : 60,
   1921   1.1  riastrad 				    cmd->rb, cmd->interlace,
   1922   1.1  riastrad 				    cmd->margins);
   1923   1.1  riastrad 	else
   1924   1.1  riastrad 		mode = drm_gtf_mode(dev,
   1925   1.1  riastrad 				    cmd->xres, cmd->yres,
   1926   1.1  riastrad 				    cmd->refresh_specified ? cmd->refresh : 60,
   1927   1.1  riastrad 				    cmd->interlace,
   1928   1.1  riastrad 				    cmd->margins);
   1929   1.1  riastrad 	if (!mode)
   1930   1.1  riastrad 		return NULL;
   1931   1.1  riastrad 
   1932   1.7  riastrad 	mode->type |= DRM_MODE_TYPE_USERDEF;
   1933   1.7  riastrad 	/* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
   1934  1.10  riastrad 	if (cmd->xres == 1366)
   1935  1.10  riastrad 		drm_mode_fixup_1366x768(mode);
   1936   1.1  riastrad 	drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
   1937   1.1  riastrad 	return mode;
   1938   1.1  riastrad }
   1939   1.1  riastrad EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
   1940   1.7  riastrad 
   1941   1.7  riastrad /**
   1942   1.7  riastrad  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
   1943   1.7  riastrad  * @out: drm_mode_modeinfo struct to return to the user
   1944   1.7  riastrad  * @in: drm_display_mode to use
   1945   1.7  riastrad  *
   1946   1.7  riastrad  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
   1947   1.7  riastrad  * the user.
   1948   1.7  riastrad  */
   1949   1.7  riastrad void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
   1950   1.7  riastrad 			       const struct drm_display_mode *in)
   1951   1.7  riastrad {
   1952   1.7  riastrad 	WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
   1953   1.7  riastrad 	     in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
   1954   1.7  riastrad 	     in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
   1955   1.7  riastrad 	     in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
   1956   1.7  riastrad 	     in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
   1957   1.7  riastrad 	     "timing values too large for mode info\n");
   1958   1.7  riastrad 
   1959   1.7  riastrad 	out->clock = in->clock;
   1960   1.7  riastrad 	out->hdisplay = in->hdisplay;
   1961   1.7  riastrad 	out->hsync_start = in->hsync_start;
   1962   1.7  riastrad 	out->hsync_end = in->hsync_end;
   1963   1.7  riastrad 	out->htotal = in->htotal;
   1964   1.7  riastrad 	out->hskew = in->hskew;
   1965   1.7  riastrad 	out->vdisplay = in->vdisplay;
   1966   1.7  riastrad 	out->vsync_start = in->vsync_start;
   1967   1.7  riastrad 	out->vsync_end = in->vsync_end;
   1968   1.7  riastrad 	out->vtotal = in->vtotal;
   1969   1.7  riastrad 	out->vscan = in->vscan;
   1970   1.7  riastrad 	out->vrefresh = in->vrefresh;
   1971   1.7  riastrad 	out->flags = in->flags;
   1972   1.7  riastrad 	out->type = in->type;
   1973  1.10  riastrad 
   1974  1.10  riastrad 	switch (in->picture_aspect_ratio) {
   1975  1.10  riastrad 	case HDMI_PICTURE_ASPECT_4_3:
   1976  1.10  riastrad 		out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
   1977  1.10  riastrad 		break;
   1978  1.10  riastrad 	case HDMI_PICTURE_ASPECT_16_9:
   1979  1.10  riastrad 		out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
   1980  1.10  riastrad 		break;
   1981  1.10  riastrad 	case HDMI_PICTURE_ASPECT_64_27:
   1982  1.10  riastrad 		out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
   1983  1.10  riastrad 		break;
   1984  1.10  riastrad 	case HDMI_PICTURE_ASPECT_256_135:
   1985  1.10  riastrad 		out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
   1986  1.10  riastrad 		break;
   1987  1.10  riastrad 	default:
   1988  1.10  riastrad 		WARN(1, "Invalid aspect ratio (0%x) on mode\n",
   1989  1.10  riastrad 		     in->picture_aspect_ratio);
   1990  1.10  riastrad 		/* fall through */
   1991  1.10  riastrad 	case HDMI_PICTURE_ASPECT_NONE:
   1992  1.10  riastrad 		out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
   1993  1.10  riastrad 		break;
   1994  1.10  riastrad 	}
   1995  1.10  riastrad 
   1996   1.7  riastrad 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
   1997   1.7  riastrad 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
   1998   1.7  riastrad }
   1999   1.7  riastrad 
   2000   1.7  riastrad /**
   2001   1.7  riastrad  * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
   2002  1.10  riastrad  * @dev: drm device
   2003   1.7  riastrad  * @out: drm_display_mode to return to the user
   2004   1.7  riastrad  * @in: drm_mode_modeinfo to use
   2005   1.7  riastrad  *
   2006   1.7  riastrad  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
   2007   1.7  riastrad  * the caller.
   2008   1.7  riastrad  *
   2009   1.7  riastrad  * Returns:
   2010   1.7  riastrad  * Zero on success, negative errno on failure.
   2011   1.7  riastrad  */
   2012  1.10  riastrad int drm_mode_convert_umode(struct drm_device *dev,
   2013  1.10  riastrad 			   struct drm_display_mode *out,
   2014   1.7  riastrad 			   const struct drm_mode_modeinfo *in)
   2015   1.7  riastrad {
   2016  1.10  riastrad 	if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
   2017  1.10  riastrad 		return -ERANGE;
   2018   1.7  riastrad 
   2019   1.7  riastrad 	out->clock = in->clock;
   2020   1.7  riastrad 	out->hdisplay = in->hdisplay;
   2021   1.7  riastrad 	out->hsync_start = in->hsync_start;
   2022   1.7  riastrad 	out->hsync_end = in->hsync_end;
   2023   1.7  riastrad 	out->htotal = in->htotal;
   2024   1.7  riastrad 	out->hskew = in->hskew;
   2025   1.7  riastrad 	out->vdisplay = in->vdisplay;
   2026   1.7  riastrad 	out->vsync_start = in->vsync_start;
   2027   1.7  riastrad 	out->vsync_end = in->vsync_end;
   2028   1.7  riastrad 	out->vtotal = in->vtotal;
   2029   1.7  riastrad 	out->vscan = in->vscan;
   2030   1.7  riastrad 	out->vrefresh = in->vrefresh;
   2031   1.7  riastrad 	out->flags = in->flags;
   2032  1.10  riastrad 	/*
   2033  1.10  riastrad 	 * Old xf86-video-vmware (possibly others too) used to
   2034  1.10  riastrad 	 * leave 'type' unititialized. Just ignore any bits we
   2035  1.10  riastrad 	 * don't like. It's a just hint after all, and more
   2036  1.10  riastrad 	 * useful for the kernel->userspace direction anyway.
   2037  1.10  riastrad 	 */
   2038  1.10  riastrad 	out->type = in->type & DRM_MODE_TYPE_ALL;
   2039   1.7  riastrad 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
   2040   1.7  riastrad 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
   2041   1.7  riastrad 
   2042  1.10  riastrad 	/* Clearing picture aspect ratio bits from out flags,
   2043  1.10  riastrad 	 * as the aspect-ratio information is not stored in
   2044  1.10  riastrad 	 * flags for kernel-mode, but in picture_aspect_ratio.
   2045  1.10  riastrad 	 */
   2046  1.10  riastrad 	out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
   2047  1.10  riastrad 
   2048  1.10  riastrad 	switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
   2049  1.10  riastrad 	case DRM_MODE_FLAG_PIC_AR_4_3:
   2050  1.10  riastrad 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3;
   2051  1.10  riastrad 		break;
   2052  1.10  riastrad 	case DRM_MODE_FLAG_PIC_AR_16_9:
   2053  1.10  riastrad 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9;
   2054  1.10  riastrad 		break;
   2055  1.10  riastrad 	case DRM_MODE_FLAG_PIC_AR_64_27:
   2056  1.10  riastrad 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27;
   2057  1.10  riastrad 		break;
   2058  1.10  riastrad 	case DRM_MODE_FLAG_PIC_AR_256_135:
   2059  1.10  riastrad 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135;
   2060  1.10  riastrad 		break;
   2061  1.10  riastrad 	case DRM_MODE_FLAG_PIC_AR_NONE:
   2062  1.10  riastrad 		out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
   2063  1.10  riastrad 		break;
   2064  1.10  riastrad 	default:
   2065  1.10  riastrad 		return -EINVAL;
   2066  1.10  riastrad 	}
   2067  1.10  riastrad 
   2068  1.10  riastrad 	out->status = drm_mode_validate_driver(dev, out);
   2069   1.7  riastrad 	if (out->status != MODE_OK)
   2070  1.10  riastrad 		return -EINVAL;
   2071   1.7  riastrad 
   2072   1.7  riastrad 	drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
   2073   1.7  riastrad 
   2074  1.10  riastrad 	return 0;
   2075  1.10  riastrad }
   2076  1.10  riastrad 
   2077  1.10  riastrad /**
   2078  1.10  riastrad  * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
   2079  1.10  riastrad  * output format
   2080  1.10  riastrad  *
   2081  1.10  riastrad  * @display: display under action
   2082  1.10  riastrad  * @mode: video mode to be tested.
   2083  1.10  riastrad  *
   2084  1.10  riastrad  * Returns:
   2085  1.10  riastrad  * true if the mode can be supported in YCBCR420 format
   2086  1.10  riastrad  * false if not.
   2087  1.10  riastrad  */
   2088  1.10  riastrad bool drm_mode_is_420_only(const struct drm_display_info *display,
   2089  1.10  riastrad 			  const struct drm_display_mode *mode)
   2090  1.10  riastrad {
   2091  1.10  riastrad 	u8 vic = drm_match_cea_mode(mode);
   2092  1.10  riastrad 
   2093  1.10  riastrad 	return test_bit(vic, display->hdmi.y420_vdb_modes);
   2094  1.10  riastrad }
   2095  1.10  riastrad EXPORT_SYMBOL(drm_mode_is_420_only);
   2096  1.10  riastrad 
   2097  1.10  riastrad /**
   2098  1.10  riastrad  * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
   2099  1.10  riastrad  * output format also (along with RGB/YCBCR444/422)
   2100  1.10  riastrad  *
   2101  1.10  riastrad  * @display: display under action.
   2102  1.10  riastrad  * @mode: video mode to be tested.
   2103  1.10  riastrad  *
   2104  1.10  riastrad  * Returns:
   2105  1.10  riastrad  * true if the mode can be support YCBCR420 format
   2106  1.10  riastrad  * false if not.
   2107  1.10  riastrad  */
   2108  1.10  riastrad bool drm_mode_is_420_also(const struct drm_display_info *display,
   2109  1.10  riastrad 			  const struct drm_display_mode *mode)
   2110  1.10  riastrad {
   2111  1.10  riastrad 	u8 vic = drm_match_cea_mode(mode);
   2112   1.7  riastrad 
   2113  1.10  riastrad 	return test_bit(vic, display->hdmi.y420_cmdb_modes);
   2114  1.10  riastrad }
   2115  1.10  riastrad EXPORT_SYMBOL(drm_mode_is_420_also);
   2116  1.10  riastrad /**
   2117  1.10  riastrad  * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
   2118  1.10  riastrad  * output format
   2119  1.10  riastrad  *
   2120  1.10  riastrad  * @display: display under action.
   2121  1.10  riastrad  * @mode: video mode to be tested.
   2122  1.10  riastrad  *
   2123  1.10  riastrad  * Returns:
   2124  1.10  riastrad  * true if the mode can be supported in YCBCR420 format
   2125  1.10  riastrad  * false if not.
   2126  1.10  riastrad  */
   2127  1.10  riastrad bool drm_mode_is_420(const struct drm_display_info *display,
   2128  1.10  riastrad 		     const struct drm_display_mode *mode)
   2129  1.10  riastrad {
   2130  1.10  riastrad 	return drm_mode_is_420_only(display, mode) ||
   2131  1.10  riastrad 		drm_mode_is_420_also(display, mode);
   2132   1.7  riastrad }
   2133  1.10  riastrad EXPORT_SYMBOL(drm_mode_is_420);
   2134