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