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