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
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
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).
33 #include <linux/list.h>
34 #include <linux/list_sort.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <video/of_videomode.h>
39 #include <video/videomode.h>
41 #include "drm_crtc_internal.h"
44 * drm_mode_debug_printmodeline - print a mode to dmesg
45 * @mode: mode to print
47 * Describe @mode using DRM_DEBUG.
49 void drm_mode_debug_printmodeline(const struct drm_display_mode
*mode
)
51 DRM_DEBUG_KMS("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d "
53 mode
->base
.id
, mode
->name
, mode
->vrefresh
, mode
->clock
,
54 mode
->hdisplay
, mode
->hsync_start
,
55 mode
->hsync_end
, mode
->htotal
,
56 mode
->vdisplay
, mode
->vsync_start
,
57 mode
->vsync_end
, mode
->vtotal
, mode
->type
, mode
->flags
);
59 EXPORT_SYMBOL(drm_mode_debug_printmodeline
);
62 * drm_mode_create - create a new display mode
65 * Create a new drm_display_mode, give it an ID, and return it.
68 * Pointer to new mode on success, NULL on error.
70 struct drm_display_mode
*drm_mode_create(struct drm_device
*dev
)
72 struct drm_display_mode
*nmode
;
74 nmode
= kzalloc(sizeof(struct drm_display_mode
), GFP_KERNEL
);
78 if (drm_mode_object_get(dev
, &nmode
->base
, DRM_MODE_OBJECT_MODE
)) {
85 EXPORT_SYMBOL(drm_mode_create
);
88 * drm_mode_destroy - remove a mode
90 * @mode: mode to remove
92 * Free @mode's unique identifier, then free it.
94 void drm_mode_destroy(struct drm_device
*dev
, struct drm_display_mode
*mode
)
99 drm_mode_object_put(dev
, &mode
->base
);
103 EXPORT_SYMBOL(drm_mode_destroy
);
106 * drm_mode_probed_add - add a mode to a connector's probed mode list
107 * @connector: connector the new mode
110 * Add @mode to @connector's mode list for later use.
112 void drm_mode_probed_add(struct drm_connector
*connector
,
113 struct drm_display_mode
*mode
)
115 WARN_ON(!mutex_is_locked(&connector
->dev
->mode_config
.mutex
));
117 list_add_tail(&mode
->head
, &connector
->probed_modes
);
119 EXPORT_SYMBOL(drm_mode_probed_add
);
122 * drm_cvt_mode -create a modeline based on CVT algorithm
124 * @hdisplay: hdisplay size
125 * @vdisplay: vdisplay size
126 * @vrefresh : vrefresh rate
127 * @reduced : Whether the GTF calculation is simplified
128 * @interlaced:Whether the interlace is supported
129 * @margins: whether to add margins or not
131 * return the modeline based on CVT algorithm
133 * This function is called to generate the modeline based on CVT algorithm
134 * according to the hdisplay, vdisplay, vrefresh.
135 * It is based from the VESA(TM) Coordinated Video Timing Generator by
136 * Graham Loveridge April 9, 2003 available at
137 * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
139 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
140 * What I have done is to translate it by using integer calculation.
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
)
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
;
162 /* allocate the drm_display_mode structure. If failure, we will
165 drm_mode
= drm_mode_create(dev
);
169 /* the CVT default refresh rate is 60Hz */
173 /* the required field fresh rate */
175 vfieldrate
= vrefresh
* 2;
177 vfieldrate
= vrefresh
;
179 /* horizontal pixels */
180 hdisplay_rnd
= hdisplay
- (hdisplay
% CVT_H_GRANULARITY
);
182 /* determine the left&right borders */
185 hmargin
= hdisplay_rnd
* CVT_MARGIN_PERCENTAGE
/ 1000;
186 hmargin
-= hmargin
% CVT_H_GRANULARITY
;
188 /* find the total active pixels */
189 drm_mode
->hdisplay
= hdisplay_rnd
+ 2 * hmargin
;
191 /* find the number of lines per field */
193 vdisplay_rnd
= vdisplay
/ 2;
195 vdisplay_rnd
= vdisplay
;
197 /* find the top & bottom borders */
200 vmargin
= vdisplay_rnd
* CVT_MARGIN_PERCENTAGE
/ 1000;
202 drm_mode
->vdisplay
= vdisplay
+ 2 * vmargin
;
210 /* Determine VSync Width from aspect ratio */
211 if (!(vdisplay
% 3) && ((vdisplay
* 4 / 3) == hdisplay
))
213 else if (!(vdisplay
% 9) && ((vdisplay
* 16 / 9) == hdisplay
))
215 else if (!(vdisplay
% 10) && ((vdisplay
* 16 / 10) == hdisplay
))
217 else if (!(vdisplay
% 4) && ((vdisplay
* 5 / 4) == hdisplay
))
219 else if (!(vdisplay
% 9) && ((vdisplay
* 15 / 9) == hdisplay
))
225 /* simplify the GTF calculation */
226 /* 4) Minimum time of vertical sync + back porch interval (µs)
230 #define CVT_MIN_VSYNC_BP 550
231 /* 3) Nominal HSync width (% of line period) - default 8 */
232 #define CVT_HSYNC_PERCENTAGE 8
233 unsigned int hblank_percentage
;
234 int vsyncandback_porch
, vback_porch
, hblank
;
236 /* estimated the horizontal period */
237 tmp1
= HV_FACTOR
* 1000000 -
238 CVT_MIN_VSYNC_BP
* HV_FACTOR
* vfieldrate
;
239 tmp2
= (vdisplay_rnd
+ 2 * vmargin
+ CVT_MIN_V_PORCH
) * 2 +
241 hperiod
= tmp1
* 2 / (tmp2
* vfieldrate
);
243 tmp1
= CVT_MIN_VSYNC_BP
* HV_FACTOR
/ hperiod
+ 1;
244 /* 9. Find number of lines in sync + backporch */
245 if (tmp1
< (vsync
+ CVT_MIN_V_PORCH
))
246 vsyncandback_porch
= vsync
+ CVT_MIN_V_PORCH
;
248 vsyncandback_porch
= tmp1
;
249 /* 10. Find number of lines in back porch */
250 vback_porch
= vsyncandback_porch
- vsync
;
251 drm_mode
->vtotal
= vdisplay_rnd
+ 2 * vmargin
+
252 vsyncandback_porch
+ CVT_MIN_V_PORCH
;
253 /* 5) Definition of Horizontal blanking time limitation */
254 /* Gradient (%/kHz) - default 600 */
255 #define CVT_M_FACTOR 600
256 /* Offset (%) - default 40 */
257 #define CVT_C_FACTOR 40
258 /* Blanking time scaling factor - default 128 */
259 #define CVT_K_FACTOR 128
260 /* Scaling factor weighting - default 20 */
261 #define CVT_J_FACTOR 20
262 #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
263 #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
265 /* 12. Find ideal blanking duty cycle from formula */
266 hblank_percentage
= CVT_C_PRIME
* HV_FACTOR
- CVT_M_PRIME
*
268 /* 13. Blanking time */
269 if (hblank_percentage
< 20 * HV_FACTOR
)
270 hblank_percentage
= 20 * HV_FACTOR
;
271 hblank
= drm_mode
->hdisplay
* hblank_percentage
/
272 (100 * HV_FACTOR
- hblank_percentage
);
273 hblank
-= hblank
% (2 * CVT_H_GRANULARITY
);
274 /* 14. find the total pixes per line */
275 drm_mode
->htotal
= drm_mode
->hdisplay
+ hblank
;
276 drm_mode
->hsync_end
= drm_mode
->hdisplay
+ hblank
/ 2;
277 drm_mode
->hsync_start
= drm_mode
->hsync_end
-
278 (drm_mode
->htotal
* CVT_HSYNC_PERCENTAGE
) / 100;
279 drm_mode
->hsync_start
+= CVT_H_GRANULARITY
-
280 drm_mode
->hsync_start
% CVT_H_GRANULARITY
;
281 /* fill the Vsync values */
282 drm_mode
->vsync_start
= drm_mode
->vdisplay
+ CVT_MIN_V_PORCH
;
283 drm_mode
->vsync_end
= drm_mode
->vsync_start
+ vsync
;
285 /* Reduced blanking */
286 /* Minimum vertical blanking interval time (µs)- default 460 */
287 #define CVT_RB_MIN_VBLANK 460
288 /* Fixed number of clocks for horizontal sync */
289 #define CVT_RB_H_SYNC 32
290 /* Fixed number of clocks for horizontal blanking */
291 #define CVT_RB_H_BLANK 160
292 /* Fixed number of lines for vertical front porch - default 3*/
293 #define CVT_RB_VFPORCH 3
296 /* 8. Estimate Horizontal period. */
297 tmp1
= HV_FACTOR
* 1000000 -
298 CVT_RB_MIN_VBLANK
* HV_FACTOR
* vfieldrate
;
299 tmp2
= vdisplay_rnd
+ 2 * vmargin
;
300 hperiod
= tmp1
/ (tmp2
* vfieldrate
);
301 /* 9. Find number of lines in vertical blanking */
302 vbilines
= CVT_RB_MIN_VBLANK
* HV_FACTOR
/ hperiod
+ 1;
303 /* 10. Check if vertical blanking is sufficient */
304 if (vbilines
< (CVT_RB_VFPORCH
+ vsync
+ CVT_MIN_V_BPORCH
))
305 vbilines
= CVT_RB_VFPORCH
+ vsync
+ CVT_MIN_V_BPORCH
;
306 /* 11. Find total number of lines in vertical field */
307 drm_mode
->vtotal
= vdisplay_rnd
+ 2 * vmargin
+ vbilines
;
308 /* 12. Find total number of pixels in a line */
309 drm_mode
->htotal
= drm_mode
->hdisplay
+ CVT_RB_H_BLANK
;
310 /* Fill in HSync values */
311 drm_mode
->hsync_end
= drm_mode
->hdisplay
+ CVT_RB_H_BLANK
/ 2;
312 drm_mode
->hsync_start
= drm_mode
->hsync_end
- CVT_RB_H_SYNC
;
313 /* Fill in VSync values */
314 drm_mode
->vsync_start
= drm_mode
->vdisplay
+ CVT_RB_VFPORCH
;
315 drm_mode
->vsync_end
= drm_mode
->vsync_start
+ vsync
;
317 /* 15/13. Find pixel clock frequency (kHz for xf86) */
318 drm_mode
->clock
= drm_mode
->htotal
* HV_FACTOR
* 1000 / hperiod
;
319 drm_mode
->clock
-= drm_mode
->clock
% CVT_CLOCK_STEP
;
320 /* 18/16. Find actual vertical frame frequency */
321 /* ignore - just set the mode flag for interlaced */
323 drm_mode
->vtotal
*= 2;
324 drm_mode
->flags
|= DRM_MODE_FLAG_INTERLACE
;
326 /* Fill the mode line name */
327 drm_mode_set_name(drm_mode
);
329 drm_mode
->flags
|= (DRM_MODE_FLAG_PHSYNC
|
330 DRM_MODE_FLAG_NVSYNC
);
332 drm_mode
->flags
|= (DRM_MODE_FLAG_PVSYNC
|
333 DRM_MODE_FLAG_NHSYNC
);
337 EXPORT_SYMBOL(drm_cvt_mode
);
340 * drm_gtf_mode_complex - create the modeline based on full GTF algorithm
343 * @hdisplay :hdisplay size
344 * @vdisplay :vdisplay size
345 * @vrefresh :vrefresh rate.
346 * @interlaced :whether the interlace is supported
347 * @margins :desired margin size
348 * @GTF_M: extended GTF formula parameters
349 * @GTF_2C: extended GTF formula parameters
350 * @GTF_K: extended GTF formula parameters
351 * @GTF_2J: extended GTF formula parameters
353 * return the modeline based on full GTF algorithm.
355 * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
356 * in here multiplied by two. For a C of 40, pass in 80.
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, */
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 */
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
374 /* C' and M' are part of the Blanking Duty Cycle computation */
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)
377 struct drm_display_mode
*drm_mode
;
378 unsigned int hdisplay_rnd
, vdisplay_rnd
, vfieldrate_rqd
;
379 int top_margin
, bottom_margin
;
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
;
391 drm_mode
= drm_mode_create(dev
);
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
400 hdisplay_rnd
= (hdisplay
+ GTF_CELL_GRAN
/ 2) / GTF_CELL_GRAN
;
401 hdisplay_rnd
= hdisplay_rnd
* GTF_CELL_GRAN
;
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.
408 vdisplay_rnd
= vdisplay
/ 2;
410 vdisplay_rnd
= vdisplay
;
412 /* 3. Find the frame rate required: */
414 vfieldrate_rqd
= vrefresh
* 2;
416 vfieldrate_rqd
= vrefresh
;
418 /* 4. Find number of lines in Top margin: */
421 top_margin
= (vdisplay_rnd
* GTF_MARGIN_PERCENTAGE
+ 500) /
423 /* 5. Find number of lines in bottom margin: */
424 bottom_margin
= top_margin
;
426 /* 6. If interlace is required, then set variable interlace: */
432 /* 7. Estimate the Horizontal frequency */
434 tmp1
= (1000000 - MIN_VSYNC_PLUS_BP
* vfieldrate_rqd
) / 500;
435 tmp2
= (vdisplay_rnd
+ 2 * top_margin
+ GTF_MIN_V_PORCH
) *
437 hfreq_est
= (tmp2
* 1000 * vfieldrate_rqd
) / tmp1
;
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
);
454 /* 13. Find the actual Vertical field frequency: */
455 vfield_rate
= hfreq_est
/ vtotal_lines
;
456 /* 14. Find the Vertical frame frequency: */
458 vframe_rate
= vfield_rate
/ 2;
460 vframe_rate
= vfield_rate
;
461 /* 15. Find number of pixels in left margin: */
463 left_margin
= (hdisplay_rnd
* GTF_MARGIN_PERCENTAGE
+ 500) /
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
;
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
;
508 drm_mode
->clock
= pixel_freq
;
511 drm_mode
->vtotal
*= 2;
512 drm_mode
->flags
|= DRM_MODE_FLAG_INTERLACE
;
515 drm_mode_set_name(drm_mode
);
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
;
519 drm_mode
->flags
= DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_NVSYNC
;
523 EXPORT_SYMBOL(drm_gtf_mode_complex
);
526 * drm_gtf_mode - create the modeline based on GTF algorithm
529 * @hdisplay :hdisplay size
530 * @vdisplay :vdisplay size
531 * @vrefresh :vrefresh rate.
532 * @interlaced :whether the interlace is supported
533 * @margins :whether the margin is supported
535 * return the modeline based on GTF algorithm
537 * This function is to create the modeline based on the GTF algorithm.
538 * Generalized Timing Formula is derived from:
539 * GTF Spreadsheet by Andy Morrish (1/5/97)
540 * available at http://www.vesa.org
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
547 * Standard GTF parameters:
553 struct drm_display_mode
*
554 drm_gtf_mode(struct drm_device
*dev
, int hdisplay
, int vdisplay
, int vrefresh
,
555 bool interlaced
, int margins
)
557 return drm_gtf_mode_complex(dev
, hdisplay
, vdisplay
, vrefresh
,
559 600, 40 * 2, 128, 20 * 2);
561 EXPORT_SYMBOL(drm_gtf_mode
);
563 #ifdef CONFIG_VIDEOMODE_HELPERS
564 int drm_display_mode_from_videomode(const struct videomode
*vm
,
565 struct drm_display_mode
*dmode
)
567 dmode
->hdisplay
= vm
->hactive
;
568 dmode
->hsync_start
= dmode
->hdisplay
+ vm
->hfront_porch
;
569 dmode
->hsync_end
= dmode
->hsync_start
+ vm
->hsync_len
;
570 dmode
->htotal
= dmode
->hsync_end
+ vm
->hback_porch
;
572 dmode
->vdisplay
= vm
->vactive
;
573 dmode
->vsync_start
= dmode
->vdisplay
+ vm
->vfront_porch
;
574 dmode
->vsync_end
= dmode
->vsync_start
+ vm
->vsync_len
;
575 dmode
->vtotal
= dmode
->vsync_end
+ vm
->vback_porch
;
577 dmode
->clock
= vm
->pixelclock
/ 1000;
580 if (vm
->flags
& DISPLAY_FLAGS_HSYNC_HIGH
)
581 dmode
->flags
|= DRM_MODE_FLAG_PHSYNC
;
582 else if (vm
->flags
& DISPLAY_FLAGS_HSYNC_LOW
)
583 dmode
->flags
|= DRM_MODE_FLAG_NHSYNC
;
584 if (vm
->flags
& DISPLAY_FLAGS_VSYNC_HIGH
)
585 dmode
->flags
|= DRM_MODE_FLAG_PVSYNC
;
586 else if (vm
->flags
& DISPLAY_FLAGS_VSYNC_LOW
)
587 dmode
->flags
|= DRM_MODE_FLAG_NVSYNC
;
588 if (vm
->flags
& DISPLAY_FLAGS_INTERLACED
)
589 dmode
->flags
|= DRM_MODE_FLAG_INTERLACE
;
590 if (vm
->flags
& DISPLAY_FLAGS_DOUBLESCAN
)
591 dmode
->flags
|= DRM_MODE_FLAG_DBLSCAN
;
592 if (vm
->flags
& DISPLAY_FLAGS_DOUBLECLK
)
593 dmode
->flags
|= DRM_MODE_FLAG_DBLCLK
;
594 drm_mode_set_name(dmode
);
598 EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode
);
602 * of_get_drm_display_mode - get a drm_display_mode from devicetree
603 * @np: device_node with the timing specification
604 * @dmode: will be set to the return value
605 * @index: index into the list of display timings in devicetree
607 * This function is expensive and should only be used, if only one mode is to be
608 * read from DT. To get multiple modes start with of_get_display_timings and
609 * work with that instead.
611 int of_get_drm_display_mode(struct device_node
*np
,
612 struct drm_display_mode
*dmode
, int index
)
617 ret
= of_get_videomode(np
, &vm
, index
);
621 drm_display_mode_from_videomode(&vm
, dmode
);
623 pr_debug("%s: got %dx%d display mode from %s\n",
624 of_node_full_name(np
), vm
.hactive
, vm
.vactive
, np
->name
);
625 drm_mode_debug_printmodeline(dmode
);
629 EXPORT_SYMBOL_GPL(of_get_drm_display_mode
);
630 #endif /* CONFIG_OF */
631 #endif /* CONFIG_VIDEOMODE_HELPERS */
634 * drm_mode_set_name - set the name on a mode
635 * @mode: name will be set in this mode
637 * Set the name of @mode to a standard format.
639 void drm_mode_set_name(struct drm_display_mode
*mode
)
641 bool interlaced
= !!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
);
643 snprintf(mode
->name
, DRM_DISPLAY_MODE_LEN
, "%dx%d%s",
644 mode
->hdisplay
, mode
->vdisplay
,
645 interlaced
? "i" : "");
647 EXPORT_SYMBOL(drm_mode_set_name
);
650 * drm_mode_width - get the width of a mode
653 * Return @mode's width (hdisplay) value.
655 * FIXME: is this needed?
660 int drm_mode_width(const struct drm_display_mode
*mode
)
662 return mode
->hdisplay
;
665 EXPORT_SYMBOL(drm_mode_width
);
668 * drm_mode_height - get the height of a mode
671 * Return @mode's height (vdisplay) value.
673 * FIXME: is this needed?
678 int drm_mode_height(const struct drm_display_mode
*mode
)
680 return mode
->vdisplay
;
682 EXPORT_SYMBOL(drm_mode_height
);
684 /** drm_mode_hsync - get the hsync of a mode
687 * Return @modes's hsync rate in kHz, rounded to the nearest int.
689 int drm_mode_hsync(const struct drm_display_mode
*mode
)
691 unsigned int calc_val
;
696 if (mode
->htotal
< 0)
699 calc_val
= (mode
->clock
* 1000) / mode
->htotal
; /* hsync in Hz */
700 calc_val
+= 500; /* round to 1000Hz */
701 calc_val
/= 1000; /* truncate to kHz */
705 EXPORT_SYMBOL(drm_mode_hsync
);
708 * drm_mode_vrefresh - get the vrefresh of a mode
711 * Return @mode's vrefresh rate in Hz or calculate it if necessary.
713 * FIXME: why is this needed? shouldn't vrefresh be set already?
716 * Vertical refresh rate. It will be the result of actual value plus 0.5.
717 * If it is 70.288, it will return 70Hz.
718 * If it is 59.6, it will return 60Hz.
720 int drm_mode_vrefresh(const struct drm_display_mode
*mode
)
723 unsigned int calc_val
;
725 if (mode
->vrefresh
> 0)
726 refresh
= mode
->vrefresh
;
727 else if (mode
->htotal
> 0 && mode
->vtotal
> 0) {
729 vtotal
= mode
->vtotal
;
730 /* work out vrefresh the value will be x1000 */
731 calc_val
= (mode
->clock
* 1000);
732 calc_val
/= mode
->htotal
;
733 refresh
= (calc_val
+ vtotal
/ 2) / vtotal
;
735 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
737 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
740 refresh
/= mode
->vscan
;
744 EXPORT_SYMBOL(drm_mode_vrefresh
);
747 * drm_mode_set_crtcinfo - set CRTC modesetting parameters
749 * @adjust_flags: a combination of adjustment flags
751 * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
753 * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
755 * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
756 * buffers containing two eyes (only adjust the timings when needed, eg. for
757 * "frame packing" or "side by side full").
759 void drm_mode_set_crtcinfo(struct drm_display_mode
*p
, int adjust_flags
)
761 if ((p
== NULL
) || ((p
->type
& DRM_MODE_TYPE_CRTC_C
) == DRM_MODE_TYPE_BUILTIN
))
764 p
->crtc_clock
= p
->clock
;
765 p
->crtc_hdisplay
= p
->hdisplay
;
766 p
->crtc_hsync_start
= p
->hsync_start
;
767 p
->crtc_hsync_end
= p
->hsync_end
;
768 p
->crtc_htotal
= p
->htotal
;
769 p
->crtc_hskew
= p
->hskew
;
770 p
->crtc_vdisplay
= p
->vdisplay
;
771 p
->crtc_vsync_start
= p
->vsync_start
;
772 p
->crtc_vsync_end
= p
->vsync_end
;
773 p
->crtc_vtotal
= p
->vtotal
;
775 if (p
->flags
& DRM_MODE_FLAG_INTERLACE
) {
776 if (adjust_flags
& CRTC_INTERLACE_HALVE_V
) {
777 p
->crtc_vdisplay
/= 2;
778 p
->crtc_vsync_start
/= 2;
779 p
->crtc_vsync_end
/= 2;
784 if (p
->flags
& DRM_MODE_FLAG_DBLSCAN
) {
785 p
->crtc_vdisplay
*= 2;
786 p
->crtc_vsync_start
*= 2;
787 p
->crtc_vsync_end
*= 2;
792 p
->crtc_vdisplay
*= p
->vscan
;
793 p
->crtc_vsync_start
*= p
->vscan
;
794 p
->crtc_vsync_end
*= p
->vscan
;
795 p
->crtc_vtotal
*= p
->vscan
;
798 if (adjust_flags
& CRTC_STEREO_DOUBLE
) {
799 unsigned int layout
= p
->flags
& DRM_MODE_FLAG_3D_MASK
;
802 case DRM_MODE_FLAG_3D_FRAME_PACKING
:
804 p
->crtc_vdisplay
+= p
->crtc_vtotal
;
805 p
->crtc_vsync_start
+= p
->crtc_vtotal
;
806 p
->crtc_vsync_end
+= p
->crtc_vtotal
;
807 p
->crtc_vtotal
+= p
->crtc_vtotal
;
812 p
->crtc_vblank_start
= min(p
->crtc_vsync_start
, p
->crtc_vdisplay
);
813 p
->crtc_vblank_end
= max(p
->crtc_vsync_end
, p
->crtc_vtotal
);
814 p
->crtc_hblank_start
= min(p
->crtc_hsync_start
, p
->crtc_hdisplay
);
815 p
->crtc_hblank_end
= max(p
->crtc_hsync_end
, p
->crtc_htotal
);
817 EXPORT_SYMBOL(drm_mode_set_crtcinfo
);
821 * drm_mode_copy - copy the mode
822 * @dst: mode to overwrite
825 * Copy an existing mode into another mode, preserving the object id and
826 * list head of the destination mode.
828 void drm_mode_copy(struct drm_display_mode
*dst
, const struct drm_display_mode
*src
)
830 int id
= dst
->base
.id
;
831 struct list_head head
= dst
->head
;
837 EXPORT_SYMBOL(drm_mode_copy
);
840 * drm_mode_duplicate - allocate and duplicate an existing mode
841 * @dev: drm_device to allocate the duplicated mode for
842 * @mode: mode to duplicate
844 * Just allocate a new mode, copy the existing mode into it, and return
845 * a pointer to it. Used to create new instances of established modes.
847 struct drm_display_mode
*drm_mode_duplicate(struct drm_device
*dev
,
848 const struct drm_display_mode
*mode
)
850 struct drm_display_mode
*nmode
;
852 nmode
= drm_mode_create(dev
);
856 drm_mode_copy(nmode
, mode
);
860 EXPORT_SYMBOL(drm_mode_duplicate
);
863 * drm_mode_equal - test modes for equality
865 * @mode2: second mode
867 * Check to see if @mode1 and @mode2 are equivalent.
870 * True if the modes are equal, false otherwise.
872 bool drm_mode_equal(const struct drm_display_mode
*mode1
, const struct drm_display_mode
*mode2
)
874 /* do clock check convert to PICOS so fb modes get matched
876 if (mode1
->clock
&& mode2
->clock
) {
877 if (KHZ2PICOS(mode1
->clock
) != KHZ2PICOS(mode2
->clock
))
879 } else if (mode1
->clock
!= mode2
->clock
)
882 if ((mode1
->flags
& DRM_MODE_FLAG_3D_MASK
) !=
883 (mode2
->flags
& DRM_MODE_FLAG_3D_MASK
))
886 return drm_mode_equal_no_clocks_no_stereo(mode1
, mode2
);
888 EXPORT_SYMBOL(drm_mode_equal
);
891 * drm_mode_equal_no_clocks_no_stereo - test modes for equality
893 * @mode2: second mode
895 * Check to see if @mode1 and @mode2 are equivalent, but
896 * don't check the pixel clocks nor the stereo layout.
899 * True if the modes are equal, false otherwise.
901 bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode
*mode1
,
902 const struct drm_display_mode
*mode2
)
904 if (mode1
->hdisplay
== mode2
->hdisplay
&&
905 mode1
->hsync_start
== mode2
->hsync_start
&&
906 mode1
->hsync_end
== mode2
->hsync_end
&&
907 mode1
->htotal
== mode2
->htotal
&&
908 mode1
->hskew
== mode2
->hskew
&&
909 mode1
->vdisplay
== mode2
->vdisplay
&&
910 mode1
->vsync_start
== mode2
->vsync_start
&&
911 mode1
->vsync_end
== mode2
->vsync_end
&&
912 mode1
->vtotal
== mode2
->vtotal
&&
913 mode1
->vscan
== mode2
->vscan
&&
914 (mode1
->flags
& ~DRM_MODE_FLAG_3D_MASK
) ==
915 (mode2
->flags
& ~DRM_MODE_FLAG_3D_MASK
))
920 EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo
);
923 * drm_mode_validate_size - make sure modes adhere to size constraints
925 * @mode_list: list of modes to check
926 * @maxX: maximum width
927 * @maxY: maximum height
928 * @maxPitch: max pitch
930 * The DRM device (@dev) has size and pitch limits. Here we validate the
931 * modes we probed for @dev against those limits and set their status as
934 void drm_mode_validate_size(struct drm_device
*dev
,
935 struct list_head
*mode_list
,
936 int maxX
, int maxY
, int maxPitch
)
938 struct drm_display_mode
*mode
;
940 list_for_each_entry(mode
, mode_list
, head
) {
941 if (maxPitch
> 0 && mode
->hdisplay
> maxPitch
)
942 mode
->status
= MODE_BAD_WIDTH
;
944 if (maxX
> 0 && mode
->hdisplay
> maxX
)
945 mode
->status
= MODE_VIRTUAL_X
;
947 if (maxY
> 0 && mode
->vdisplay
> maxY
)
948 mode
->status
= MODE_VIRTUAL_Y
;
951 EXPORT_SYMBOL(drm_mode_validate_size
);
954 * drm_mode_prune_invalid - remove invalid modes from mode list
956 * @mode_list: list of modes to check
957 * @verbose: be verbose about it
959 * Once mode list generation is complete, a caller can use this routine to
960 * remove invalid modes from a mode list. If any of the modes have a
961 * status other than %MODE_OK, they are removed from @mode_list and freed.
963 void drm_mode_prune_invalid(struct drm_device
*dev
,
964 struct list_head
*mode_list
, bool verbose
)
966 struct drm_display_mode
*mode
, *t
;
968 list_for_each_entry_safe(mode
, t
, mode_list
, head
) {
969 if (mode
->status
!= MODE_OK
) {
970 list_del(&mode
->head
);
972 drm_mode_debug_printmodeline(mode
);
973 DRM_DEBUG_KMS("Not using %s mode %d\n",
974 mode
->name
, mode
->status
);
976 drm_mode_destroy(dev
, mode
);
980 EXPORT_SYMBOL(drm_mode_prune_invalid
);
983 * drm_mode_compare - compare modes for favorability
985 * @lh_a: list_head for first mode
986 * @lh_b: list_head for second mode
988 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
992 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
993 * positive if @lh_b is better than @lh_a.
995 static int drm_mode_compare(void *priv
, struct list_head
*lh_a
, struct list_head
*lh_b
)
997 struct drm_display_mode
*a
= list_entry(lh_a
, struct drm_display_mode
, head
);
998 struct drm_display_mode
*b
= list_entry(lh_b
, struct drm_display_mode
, head
);
1001 diff
= ((b
->type
& DRM_MODE_TYPE_PREFERRED
) != 0) -
1002 ((a
->type
& DRM_MODE_TYPE_PREFERRED
) != 0);
1005 diff
= b
->hdisplay
* b
->vdisplay
- a
->hdisplay
* a
->vdisplay
;
1009 diff
= b
->vrefresh
- a
->vrefresh
;
1013 diff
= b
->clock
- a
->clock
;
1018 * drm_mode_sort - sort mode list
1019 * @mode_list: list to sort
1021 * Sort @mode_list by favorability, putting good modes first.
1023 void drm_mode_sort(struct list_head
*mode_list
)
1025 list_sort(NULL
, mode_list
, drm_mode_compare
);
1027 EXPORT_SYMBOL(drm_mode_sort
);
1030 * drm_mode_connector_list_update - update the mode list for the connector
1031 * @connector: the connector to update
1033 * This moves the modes from the @connector probed_modes list
1034 * to the actual mode list. It compares the probed mode against the current
1035 * list and only adds different modes. All modes unverified after this point
1036 * will be removed by the prune invalid modes.
1038 void drm_mode_connector_list_update(struct drm_connector
*connector
)
1040 struct drm_display_mode
*mode
;
1041 struct drm_display_mode
*pmode
, *pt
;
1044 WARN_ON(!mutex_is_locked(&connector
->dev
->mode_config
.mutex
));
1046 list_for_each_entry_safe(pmode
, pt
, &connector
->probed_modes
,
1049 /* go through current modes checking for the new probed mode */
1050 list_for_each_entry(mode
, &connector
->modes
, head
) {
1051 if (drm_mode_equal(pmode
, mode
)) {
1053 /* if equal delete the probed mode */
1054 mode
->status
= pmode
->status
;
1055 /* Merge type bits together */
1056 mode
->type
|= pmode
->type
;
1057 list_del(&pmode
->head
);
1058 drm_mode_destroy(connector
->dev
, pmode
);
1064 list_move_tail(&pmode
->head
, &connector
->modes
);
1068 EXPORT_SYMBOL(drm_mode_connector_list_update
);
1071 * drm_mode_parse_command_line_for_connector - parse command line for connector
1072 * @mode_option: per connector mode option
1073 * @connector: connector to parse line for
1074 * @mode: preallocated mode structure to fill out
1076 * This parses the connector specific then generic command lines for
1077 * modes and options to configure the connector.
1079 * This uses the same parameters as the fb modedb.c, except for extra
1080 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
1082 * enable/enable Digital/disable bit at the end
1084 bool drm_mode_parse_command_line_for_connector(const char *mode_option
,
1085 struct drm_connector
*connector
,
1086 struct drm_cmdline_mode
*mode
)
1089 unsigned int namelen
;
1090 bool res_specified
= false, bpp_specified
= false, refresh_specified
= false;
1091 unsigned int xres
= 0, yres
= 0, bpp
= 32, refresh
= 0;
1092 bool yres_specified
= false, cvt
= false, rb
= false;
1093 bool interlace
= false, margins
= false, was_digit
= false;
1095 enum drm_connector_force force
= DRM_FORCE_UNSPECIFIED
;
1099 mode_option
= fb_mode_option
;
1103 mode
->specified
= false;
1108 namelen
= strlen(name
);
1109 for (i
= namelen
-1; i
>= 0; i
--) {
1112 if (!refresh_specified
&& !bpp_specified
&&
1113 !yres_specified
&& !cvt
&& !rb
&& was_digit
) {
1114 refresh
= simple_strtol(&name
[i
+1], NULL
, 10);
1115 refresh_specified
= true;
1121 if (!bpp_specified
&& !yres_specified
&& !cvt
&&
1123 bpp
= simple_strtol(&name
[i
+1], NULL
, 10);
1124 bpp_specified
= true;
1130 if (!yres_specified
&& was_digit
) {
1131 yres
= simple_strtol(&name
[i
+1], NULL
, 10);
1132 yres_specified
= true;
1141 if (yres_specified
|| cvt
|| was_digit
)
1146 if (yres_specified
|| cvt
|| rb
|| was_digit
)
1151 if (cvt
|| yres_specified
|| was_digit
)
1156 if (cvt
|| yres_specified
|| was_digit
)
1161 if (yres_specified
|| bpp_specified
|| refresh_specified
||
1162 was_digit
|| (force
!= DRM_FORCE_UNSPECIFIED
))
1165 force
= DRM_FORCE_ON
;
1168 if (yres_specified
|| bpp_specified
|| refresh_specified
||
1169 was_digit
|| (force
!= DRM_FORCE_UNSPECIFIED
))
1172 if ((connector
->connector_type
!= DRM_MODE_CONNECTOR_DVII
) &&
1173 (connector
->connector_type
!= DRM_MODE_CONNECTOR_HDMIB
))
1174 force
= DRM_FORCE_ON
;
1176 force
= DRM_FORCE_ON_DIGITAL
;
1179 if (yres_specified
|| bpp_specified
|| refresh_specified
||
1180 was_digit
|| (force
!= DRM_FORCE_UNSPECIFIED
))
1183 force
= DRM_FORCE_OFF
;
1190 if (i
< 0 && yres_specified
) {
1192 xres
= simple_strtol(name
, &ch
, 10);
1193 if ((ch
!= NULL
) && (*ch
== 'x'))
1194 res_specified
= true;
1197 } else if (!yres_specified
&& was_digit
) {
1198 /* catch mode that begins with digits but has no 'x' */
1204 "parse error at position %i in video mode '%s'\n",
1206 mode
->specified
= false;
1210 if (res_specified
) {
1211 mode
->specified
= true;
1216 if (refresh_specified
) {
1217 mode
->refresh_specified
= true;
1218 mode
->refresh
= refresh
;
1221 if (bpp_specified
) {
1222 mode
->bpp_specified
= true;
1227 mode
->interlace
= interlace
;
1228 mode
->margins
= margins
;
1229 mode
->force
= force
;
1233 EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector
);
1235 struct drm_display_mode
*
1236 drm_mode_create_from_cmdline_mode(struct drm_device
*dev
,
1237 struct drm_cmdline_mode
*cmd
)
1239 struct drm_display_mode
*mode
;
1242 mode
= drm_cvt_mode(dev
,
1243 cmd
->xres
, cmd
->yres
,
1244 cmd
->refresh_specified
? cmd
->refresh
: 60,
1245 cmd
->rb
, cmd
->interlace
,
1248 mode
= drm_gtf_mode(dev
,
1249 cmd
->xres
, cmd
->yres
,
1250 cmd
->refresh_specified
? cmd
->refresh
: 60,
1256 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
1259 EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode
);