2 * The list_sort function is (presumably) licensed under the GPL (see the
3 * top level "COPYING" file for details).
5 * The remainder of this file is:
7 * Copyright © 1997-2003 by The XFree86 Project, Inc.
8 * Copyright © 2007 Dave Airlie
9 * Copyright © 2007-2008 Intel Corporation
10 * Jesse Barnes <jesse.barnes@intel.com>
11 * Copyright 2005-2006 Luc Verhaegen
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
27 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
31 * Except as contained in this notice, the name of the copyright holder(s)
32 * and author(s) shall not be used in advertising or otherwise to promote
33 * the sale, use or other dealings in this Software without prior written
34 * authorization from the copyright holder(s) and author(s).
37 #include <linux/list.h>
42 #define DRM_MODESET_DEBUG "drm_mode"
44 * drm_mode_debug_printmodeline - debug print a mode
46 * @mode: mode to print
51 * Describe @mode using DRM_DEBUG.
53 void drm_mode_debug_printmodeline(struct drm_display_mode *mode)
55 DRM_DEBUG_MODE(DRM_MODESET_DEBUG,
56 "Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
57 mode->base.id, mode->name, mode->vrefresh, mode->clock,
58 mode->hdisplay, mode->hsync_start,
59 mode->hsync_end, mode->htotal,
60 mode->vdisplay, mode->vsync_start,
61 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
63 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
66 * drm_cvt_mode -create a modeline based on CVT algorithm
68 * @hdisplay: hdisplay size
69 * @vdisplay: vdisplay size
70 * @vrefresh : vrefresh rate
71 * @reduced : Whether the GTF calculation is simplified
72 * @interlaced:Whether the interlace is supported
77 * return the modeline based on CVT algorithm
79 * This function is called to generate the modeline based on CVT algorithm
80 * according to the hdisplay, vdisplay, vrefresh.
81 * It is based from the VESA(TM) Coordinated Video Timing Generator by
82 * Graham Loveridge April 9, 2003 available at
83 * http://www.vesa.org/public/CVT/CVTd6r1.xls
85 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
86 * What I have done is to translate it by using integer calculation.
88 #define HV_FACTOR 1000
89 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
90 int vdisplay, int vrefresh,
91 bool reduced, bool interlaced)
93 /* 1) top/bottom margin size (% of height) - default: 1.8, */
94 #define CVT_MARGIN_PERCENTAGE 18
95 /* 2) character cell horizontal granularity (pixels) - default 8 */
96 #define CVT_H_GRANULARITY 8
97 /* 3) Minimum vertical porch (lines) - default 3 */
98 #define CVT_MIN_V_PORCH 3
99 /* 4) Minimum number of vertical back porch lines - default 6 */
100 #define CVT_MIN_V_BPORCH 6
101 /* Pixel Clock step (kHz) */
102 #define CVT_CLOCK_STEP 250
103 struct drm_display_mode *drm_mode;
104 bool margins = false;
105 unsigned int vfieldrate, hperiod;
106 int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
109 /* allocate the drm_display_mode structure. If failure, we will
112 drm_mode = drm_mode_create(dev);
116 /* the CVT default refresh rate is 60Hz */
120 /* the required field fresh rate */
122 vfieldrate = vrefresh * 2;
124 vfieldrate = vrefresh;
126 /* horizontal pixels */
127 hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
129 /* determine the left&right borders */
132 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
133 hmargin -= hmargin % CVT_H_GRANULARITY;
135 /* find the total active pixels */
136 drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
138 /* find the number of lines per field */
140 vdisplay_rnd = vdisplay / 2;
142 vdisplay_rnd = vdisplay;
144 /* find the top & bottom borders */
147 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
149 drm_mode->vdisplay = vdisplay_rnd + 2 * vmargin;
157 /* Determine VSync Width from aspect ratio */
158 if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
160 else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
162 else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
164 else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
166 else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
172 /* simplify the GTF calculation */
173 /* 4) Minimum time of vertical sync + back porch interval (µs)
177 #define CVT_MIN_VSYNC_BP 550
178 /* 3) Nominal HSync width (% of line period) - default 8 */
179 #define CVT_HSYNC_PERCENTAGE 8
180 unsigned int hblank_percentage;
181 int vsyncandback_porch, vback_porch, hblank;
183 /* estimated the horizontal period */
184 tmp1 = HV_FACTOR * 1000000 -
185 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
186 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
188 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
190 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
191 /* 9. Find number of lines in sync + backporch */
192 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
193 vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
195 vsyncandback_porch = tmp1;
196 /* 10. Find number of lines in back porch */
197 vback_porch = vsyncandback_porch - vsync;
198 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
199 vsyncandback_porch + CVT_MIN_V_PORCH;
200 /* 5) Definition of Horizontal blanking time limitation */
201 /* Gradient (%/kHz) - default 600 */
202 #define CVT_M_FACTOR 600
203 /* Offset (%) - default 40 */
204 #define CVT_C_FACTOR 40
205 /* Blanking time scaling factor - default 128 */
206 #define CVT_K_FACTOR 128
207 /* Scaling factor weighting - default 20 */
208 #define CVT_J_FACTOR 20
209 #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
210 #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
212 /* 12. Find ideal blanking duty cycle from formula */
213 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
215 /* 13. Blanking time */
216 if (hblank_percentage < 20 * HV_FACTOR)
217 hblank_percentage = 20 * HV_FACTOR;
218 hblank = drm_mode->hdisplay * hblank_percentage /
219 (100 * HV_FACTOR - hblank_percentage);
220 hblank -= hblank % (2 * CVT_H_GRANULARITY);
221 /* 14. find the total pixes per line */
222 drm_mode->htotal = drm_mode->hdisplay + hblank;
223 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
224 drm_mode->hsync_start = drm_mode->hsync_end -
225 (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
226 drm_mode->hsync_start += CVT_H_GRANULARITY -
227 drm_mode->hsync_start % CVT_H_GRANULARITY;
228 /* fill the Vsync values */
229 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
230 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
232 /* Reduced blanking */
233 /* Minimum vertical blanking interval time (µs)- default 460 */
234 #define CVT_RB_MIN_VBLANK 460
235 /* Fixed number of clocks for horizontal sync */
236 #define CVT_RB_H_SYNC 32
237 /* Fixed number of clocks for horizontal blanking */
238 #define CVT_RB_H_BLANK 160
239 /* Fixed number of lines for vertical front porch - default 3*/
240 #define CVT_RB_VFPORCH 3
243 /* 8. Estimate Horizontal period. */
244 tmp1 = HV_FACTOR * 1000000 -
245 CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
246 tmp2 = vdisplay_rnd + 2 * vmargin;
247 hperiod = tmp1 / (tmp2 * vfieldrate);
248 /* 9. Find number of lines in vertical blanking */
249 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
250 /* 10. Check if vertical blanking is sufficient */
251 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
252 vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
253 /* 11. Find total number of lines in vertical field */
254 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
255 /* 12. Find total number of pixels in a line */
256 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
257 /* Fill in HSync values */
258 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
259 drm_mode->hsync_start = drm_mode->hsync_end = CVT_RB_H_SYNC;
261 /* 15/13. Find pixel clock frequency (kHz for xf86) */
262 drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
263 drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
264 /* 18/16. Find actual vertical frame frequency */
265 /* ignore - just set the mode flag for interlaced */
267 drm_mode->vtotal *= 2;
268 /* Fill the mode line name */
269 drm_mode_set_name(drm_mode);
271 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
272 DRM_MODE_FLAG_NVSYNC);
274 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
275 DRM_MODE_FLAG_NHSYNC);
277 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
281 EXPORT_SYMBOL(drm_cvt_mode);
284 * drm_mode_set_name - set the name on a mode
285 * @mode: name will be set in this mode
290 * Set the name of @mode to a standard format.
292 void drm_mode_set_name(struct drm_display_mode *mode)
294 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d", mode->hdisplay,
297 EXPORT_SYMBOL(drm_mode_set_name);
300 * drm_mode_list_concat - move modes from one list to another
305 * Caller must ensure both lists are locked.
307 * Move all the modes from @head to @new.
309 void drm_mode_list_concat(struct list_head *head, struct list_head *new)
312 struct list_head *entry, *tmp;
314 list_for_each_safe(entry, tmp, head) {
315 list_move_tail(entry, new);
318 EXPORT_SYMBOL(drm_mode_list_concat);
321 * drm_mode_width - get the width of a mode
327 * Return @mode's width (hdisplay) value.
329 * FIXME: is this needed?
334 int drm_mode_width(struct drm_display_mode *mode)
336 return mode->hdisplay;
339 EXPORT_SYMBOL(drm_mode_width);
342 * drm_mode_height - get the height of a mode
348 * Return @mode's height (vdisplay) value.
350 * FIXME: is this needed?
355 int drm_mode_height(struct drm_display_mode *mode)
357 return mode->vdisplay;
359 EXPORT_SYMBOL(drm_mode_height);
362 * drm_mode_vrefresh - get the vrefresh of a mode
368 * Return @mode's vrefresh rate or calculate it if necessary.
370 * FIXME: why is this needed? shouldn't vrefresh be set already?
373 * Vertical refresh rate of @mode x 1000. For precision reasons.
375 int drm_mode_vrefresh(struct drm_display_mode *mode)
378 unsigned int calc_val;
380 if (mode->vrefresh > 0)
381 refresh = mode->vrefresh;
382 else if (mode->htotal > 0 && mode->vtotal > 0) {
383 /* work out vrefresh the value will be x1000 */
384 calc_val = (mode->clock * 1000);
386 calc_val /= mode->htotal;
388 calc_val /= mode->vtotal;
391 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
393 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
396 refresh /= mode->vscan;
400 EXPORT_SYMBOL(drm_mode_vrefresh);
403 * drm_mode_set_crtcinfo - set CRTC modesetting parameters
405 * @adjust_flags: unused? (FIXME)
410 * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
412 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
414 if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
417 p->crtc_hdisplay = p->hdisplay;
418 p->crtc_hsync_start = p->hsync_start;
419 p->crtc_hsync_end = p->hsync_end;
420 p->crtc_htotal = p->htotal;
421 p->crtc_hskew = p->hskew;
422 p->crtc_vdisplay = p->vdisplay;
423 p->crtc_vsync_start = p->vsync_start;
424 p->crtc_vsync_end = p->vsync_end;
425 p->crtc_vtotal = p->vtotal;
427 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
428 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
429 p->crtc_vdisplay /= 2;
430 p->crtc_vsync_start /= 2;
431 p->crtc_vsync_end /= 2;
438 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
439 p->crtc_vdisplay *= 2;
440 p->crtc_vsync_start *= 2;
441 p->crtc_vsync_end *= 2;
446 p->crtc_vdisplay *= p->vscan;
447 p->crtc_vsync_start *= p->vscan;
448 p->crtc_vsync_end *= p->vscan;
449 p->crtc_vtotal *= p->vscan;
452 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
453 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
454 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
455 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
457 p->crtc_hadjusted = false;
458 p->crtc_vadjusted = false;
460 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
464 * drm_mode_duplicate - allocate and duplicate an existing mode
465 * @m: mode to duplicate
470 * Just allocate a new mode, copy the existing mode into it, and return
471 * a pointer to it. Used to create new instances of established modes.
473 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
474 struct drm_display_mode *mode)
476 struct drm_display_mode *nmode;
479 nmode = drm_mode_create(dev);
483 new_id = nmode->base.id;
485 nmode->base.id = new_id;
486 INIT_LIST_HEAD(&nmode->head);
489 EXPORT_SYMBOL(drm_mode_duplicate);
492 * drm_mode_equal - test modes for equality
494 * @mode2: second mode
499 * Check to see if @mode1 and @mode2 are equivalent.
502 * True if the modes are equal, false otherwise.
504 bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
506 /* do clock check convert to PICOS so fb modes get matched
508 if (mode1->clock && mode2->clock) {
509 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
511 } else if (mode1->clock != mode2->clock)
514 if (mode1->hdisplay == mode2->hdisplay &&
515 mode1->hsync_start == mode2->hsync_start &&
516 mode1->hsync_end == mode2->hsync_end &&
517 mode1->htotal == mode2->htotal &&
518 mode1->hskew == mode2->hskew &&
519 mode1->vdisplay == mode2->vdisplay &&
520 mode1->vsync_start == mode2->vsync_start &&
521 mode1->vsync_end == mode2->vsync_end &&
522 mode1->vtotal == mode2->vtotal &&
523 mode1->vscan == mode2->vscan &&
524 mode1->flags == mode2->flags)
529 EXPORT_SYMBOL(drm_mode_equal);
532 * drm_mode_validate_size - make sure modes adhere to size constraints
534 * @mode_list: list of modes to check
535 * @maxX: maximum width
536 * @maxY: maximum height
537 * @maxPitch: max pitch
540 * Caller must hold a lock protecting @mode_list.
542 * The DRM device (@dev) has size and pitch limits. Here we validate the
543 * modes we probed for @dev against those limits and set their status as
546 void drm_mode_validate_size(struct drm_device *dev,
547 struct list_head *mode_list,
548 int maxX, int maxY, int maxPitch)
550 struct drm_display_mode *mode;
552 list_for_each_entry(mode, mode_list, head) {
553 if (maxPitch > 0 && mode->hdisplay > maxPitch)
554 mode->status = MODE_BAD_WIDTH;
556 if (maxX > 0 && mode->hdisplay > maxX)
557 mode->status = MODE_VIRTUAL_X;
559 if (maxY > 0 && mode->vdisplay > maxY)
560 mode->status = MODE_VIRTUAL_Y;
563 EXPORT_SYMBOL(drm_mode_validate_size);
566 * drm_mode_validate_clocks - validate modes against clock limits
568 * @mode_list: list of modes to check
569 * @min: minimum clock rate array
570 * @max: maximum clock rate array
571 * @n_ranges: number of clock ranges (size of arrays)
574 * Caller must hold a lock protecting @mode_list.
576 * Some code may need to check a mode list against the clock limits of the
577 * device in question. This function walks the mode list, testing to make
578 * sure each mode falls within a given range (defined by @min and @max
579 * arrays) and sets @mode->status as needed.
581 void drm_mode_validate_clocks(struct drm_device *dev,
582 struct list_head *mode_list,
583 int *min, int *max, int n_ranges)
585 struct drm_display_mode *mode;
588 list_for_each_entry(mode, mode_list, head) {
590 for (i = 0; i < n_ranges; i++) {
591 if (mode->clock >= min[i] && mode->clock <= max[i]) {
597 mode->status = MODE_CLOCK_RANGE;
600 EXPORT_SYMBOL(drm_mode_validate_clocks);
603 * drm_mode_prune_invalid - remove invalid modes from mode list
605 * @mode_list: list of modes to check
606 * @verbose: be verbose about it
609 * Caller must hold a lock protecting @mode_list.
611 * Once mode list generation is complete, a caller can use this routine to
612 * remove invalid modes from a mode list. If any of the modes have a
613 * status other than %MODE_OK, they are removed from @mode_list and freed.
615 void drm_mode_prune_invalid(struct drm_device *dev,
616 struct list_head *mode_list, bool verbose)
618 struct drm_display_mode *mode, *t;
620 list_for_each_entry_safe(mode, t, mode_list, head) {
621 if (mode->status != MODE_OK) {
622 list_del(&mode->head);
624 drm_mode_debug_printmodeline(mode);
625 DRM_DEBUG_MODE(DRM_MODESET_DEBUG,
626 "Not using %s mode %d\n",
627 mode->name, mode->status);
629 drm_mode_destroy(dev, mode);
633 EXPORT_SYMBOL(drm_mode_prune_invalid);
636 * drm_mode_compare - compare modes for favorability
637 * @lh_a: list_head for first mode
638 * @lh_b: list_head for second mode
643 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
647 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
648 * positive if @lh_b is better than @lh_a.
650 static int drm_mode_compare(struct list_head *lh_a, struct list_head *lh_b)
652 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
653 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
656 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
657 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
660 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
663 diff = b->clock - a->clock;
667 /* FIXME: what we don't have a list sort function? */
668 /* list sort from Mark J Roberts (mjr@znex.org) */
669 void list_sort(struct list_head *head,
670 int (*cmp)(struct list_head *a, struct list_head *b))
672 struct list_head *p, *q, *e, *list, *tail, *oldhead;
673 int insize, nmerges, psize, qsize, i;
687 for (i = 0; i < insize; i++) {
689 q = q->next == oldhead ? NULL : q->next;
695 while (psize > 0 || (qsize > 0 && q)) {
702 } else if (!qsize || !q) {
708 } else if (cmp(p, q) <= 0) {
741 head->prev = list->prev;
742 list->prev->next = head;
747 * drm_mode_sort - sort mode list
748 * @mode_list: list to sort
751 * Caller must hold a lock protecting @mode_list.
753 * Sort @mode_list by favorability, putting good modes first.
755 void drm_mode_sort(struct list_head *mode_list)
757 list_sort(mode_list, drm_mode_compare);
759 EXPORT_SYMBOL(drm_mode_sort);
762 * drm_mode_connector_list_update - update the mode list for the connector
763 * @connector: the connector to update
766 * Caller must hold a lock protecting @mode_list.
768 * This moves the modes from the @connector probed_modes list
769 * to the actual mode list. It compares the probed mode against the current
770 * list and only adds different modes. All modes unverified after this point
771 * will be removed by the prune invalid modes.
773 void drm_mode_connector_list_update(struct drm_connector *connector)
775 struct drm_display_mode *mode;
776 struct drm_display_mode *pmode, *pt;
779 list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
782 /* go through current modes checking for the new probed mode */
783 list_for_each_entry(mode, &connector->modes, head) {
784 if (drm_mode_equal(pmode, mode)) {
786 /* if equal delete the probed mode */
787 mode->status = pmode->status;
788 list_del(&pmode->head);
789 drm_mode_destroy(connector->dev, pmode);
795 list_move_tail(&pmode->head, &connector->modes);
799 EXPORT_SYMBOL(drm_mode_connector_list_update);