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