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