2 * Copyright © 1997-2003 by The XFree86 Project, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name of the copyright holder(s)
23 * and author(s) shall not be used in advertising or otherwise to promote
24 * the sale, use or other dealings in this Software without prior written
25 * authorization from the copyright holder(s) and author(s).
28 * Copyright © 2007 Dave Airlie
31 #include <linux/list.h>
37 * drm_mode_debug_printmodeline - debug print a mode
39 * @mode: mode to print
44 * Describe @mode using DRM_DEBUG.
46 void drm_mode_debug_printmodeline(struct drm_display_mode *mode)
48 DRM_DEBUG("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
49 mode->base.id, mode->name, mode->vrefresh, mode->clock,
50 mode->hdisplay, mode->hsync_start,
51 mode->hsync_end, mode->htotal,
52 mode->vdisplay, mode->vsync_start,
53 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
55 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
58 * drm_mode_set_name - set the name on a mode
59 * @mode: name will be set in this mode
64 * Set the name of @mode to a standard format.
66 void drm_mode_set_name(struct drm_display_mode *mode)
68 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d", mode->hdisplay,
71 EXPORT_SYMBOL(drm_mode_set_name);
74 * drm_mode_list_concat - move modes from one list to another
79 * Caller must ensure both lists are locked.
81 * Move all the modes from @head to @new.
83 void drm_mode_list_concat(struct list_head *head, struct list_head *new)
86 struct list_head *entry, *tmp;
88 list_for_each_safe(entry, tmp, head) {
89 list_move_tail(entry, new);
92 EXPORT_SYMBOL(drm_mode_list_concat);
95 * drm_mode_width - get the width of a mode
101 * Return @mode's width (hdisplay) value.
103 * FIXME: is this needed?
108 int drm_mode_width(struct drm_display_mode *mode)
110 return mode->hdisplay;
113 EXPORT_SYMBOL(drm_mode_width);
116 * drm_mode_height - get the height of a mode
122 * Return @mode's height (vdisplay) value.
124 * FIXME: is this needed?
129 int drm_mode_height(struct drm_display_mode *mode)
131 return mode->vdisplay;
133 EXPORT_SYMBOL(drm_mode_height);
136 * drm_mode_vrefresh - get the vrefresh of a mode
142 * Return @mode's vrefresh rate or calculate it if necessary.
144 * FIXME: why is this needed? shouldn't vrefresh be set already?
147 * Vertical refresh rate of @mode x 1000. For precision reasons.
149 int drm_mode_vrefresh(struct drm_display_mode *mode)
152 unsigned int calc_val;
154 if (mode->vrefresh > 0)
155 refresh = mode->vrefresh;
156 else if (mode->htotal > 0 && mode->vtotal > 0) {
157 /* work out vrefresh the value will be x1000 */
158 calc_val = (mode->clock * 1000);
160 calc_val /= mode->htotal;
162 calc_val /= mode->vtotal;
165 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
167 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
170 refresh /= mode->vscan;
174 EXPORT_SYMBOL(drm_mode_vrefresh);
177 * drm_mode_set_crtcinfo - set CRTC modesetting parameters
179 * @adjust_flags: unused? (FIXME)
184 * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
186 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
188 if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
191 p->crtc_hdisplay = p->hdisplay;
192 p->crtc_hsync_start = p->hsync_start;
193 p->crtc_hsync_end = p->hsync_end;
194 p->crtc_htotal = p->htotal;
195 p->crtc_hskew = p->hskew;
196 p->crtc_vdisplay = p->vdisplay;
197 p->crtc_vsync_start = p->vsync_start;
198 p->crtc_vsync_end = p->vsync_end;
199 p->crtc_vtotal = p->vtotal;
201 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
202 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
203 p->crtc_vdisplay /= 2;
204 p->crtc_vsync_start /= 2;
205 p->crtc_vsync_end /= 2;
212 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
213 p->crtc_vdisplay *= 2;
214 p->crtc_vsync_start *= 2;
215 p->crtc_vsync_end *= 2;
220 p->crtc_vdisplay *= p->vscan;
221 p->crtc_vsync_start *= p->vscan;
222 p->crtc_vsync_end *= p->vscan;
223 p->crtc_vtotal *= p->vscan;
226 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
227 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
228 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
229 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
231 p->crtc_hadjusted = false;
232 p->crtc_vadjusted = false;
234 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
238 * drm_mode_duplicate - allocate and duplicate an existing mode
239 * @m: mode to duplicate
244 * Just allocate a new mode, copy the existing mode into it, and return
245 * a pointer to it. Used to create new instances of established modes.
247 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
248 struct drm_display_mode *mode)
250 struct drm_display_mode *nmode;
253 nmode = drm_mode_create(dev);
257 new_id = nmode->base.id;
259 nmode->base.id = new_id;
260 INIT_LIST_HEAD(&nmode->head);
263 EXPORT_SYMBOL(drm_mode_duplicate);
266 * drm_mode_equal - test modes for equality
268 * @mode2: second mode
273 * Check to see if @mode1 and @mode2 are equivalent.
276 * True if the modes are equal, false otherwise.
278 bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
280 /* do clock check convert to PICOS so fb modes get matched
282 if (mode1->clock && mode2->clock) {
283 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
285 } else if (mode1->clock != mode2->clock)
288 if (mode1->hdisplay == mode2->hdisplay &&
289 mode1->hsync_start == mode2->hsync_start &&
290 mode1->hsync_end == mode2->hsync_end &&
291 mode1->htotal == mode2->htotal &&
292 mode1->hskew == mode2->hskew &&
293 mode1->vdisplay == mode2->vdisplay &&
294 mode1->vsync_start == mode2->vsync_start &&
295 mode1->vsync_end == mode2->vsync_end &&
296 mode1->vtotal == mode2->vtotal &&
297 mode1->vscan == mode2->vscan &&
298 mode1->flags == mode2->flags)
303 EXPORT_SYMBOL(drm_mode_equal);
306 * drm_mode_validate_size - make sure modes adhere to size constraints
308 * @mode_list: list of modes to check
309 * @maxX: maximum width
310 * @maxY: maximum height
311 * @maxPitch: max pitch
314 * Caller must hold a lock protecting @mode_list.
316 * The DRM device (@dev) has size and pitch limits. Here we validate the
317 * modes we probed for @dev against those limits and set their status as
320 void drm_mode_validate_size(struct drm_device *dev,
321 struct list_head *mode_list,
322 int maxX, int maxY, int maxPitch)
324 struct drm_display_mode *mode;
326 list_for_each_entry(mode, mode_list, head) {
327 if (maxPitch > 0 && mode->hdisplay > maxPitch)
328 mode->status = MODE_BAD_WIDTH;
330 if (maxX > 0 && mode->hdisplay > maxX)
331 mode->status = MODE_VIRTUAL_X;
333 if (maxY > 0 && mode->vdisplay > maxY)
334 mode->status = MODE_VIRTUAL_Y;
337 EXPORT_SYMBOL(drm_mode_validate_size);
340 * drm_mode_validate_clocks - validate modes against clock limits
342 * @mode_list: list of modes to check
343 * @min: minimum clock rate array
344 * @max: maximum clock rate array
345 * @n_ranges: number of clock ranges (size of arrays)
348 * Caller must hold a lock protecting @mode_list.
350 * Some code may need to check a mode list against the clock limits of the
351 * device in question. This function walks the mode list, testing to make
352 * sure each mode falls within a given range (defined by @min and @max
353 * arrays) and sets @mode->status as needed.
355 void drm_mode_validate_clocks(struct drm_device *dev,
356 struct list_head *mode_list,
357 int *min, int *max, int n_ranges)
359 struct drm_display_mode *mode;
362 list_for_each_entry(mode, mode_list, head) {
364 for (i = 0; i < n_ranges; i++) {
365 if (mode->clock >= min[i] && mode->clock <= max[i]) {
371 mode->status = MODE_CLOCK_RANGE;
374 EXPORT_SYMBOL(drm_mode_validate_clocks);
377 * drm_mode_prune_invalid - remove invalid modes from mode list
379 * @mode_list: list of modes to check
380 * @verbose: be verbose about it
383 * Caller must hold a lock protecting @mode_list.
385 * Once mode list generation is complete, a caller can use this routine to
386 * remove invalid modes from a mode list. If any of the modes have a
387 * status other than %MODE_OK, they are removed from @mode_list and freed.
389 void drm_mode_prune_invalid(struct drm_device *dev,
390 struct list_head *mode_list, bool verbose)
392 struct drm_display_mode *mode, *t;
394 list_for_each_entry_safe(mode, t, mode_list, head) {
395 if (mode->status != MODE_OK) {
396 list_del(&mode->head);
398 drm_mode_debug_printmodeline(mode);
399 DRM_DEBUG("Not using %s mode %d\n", mode->name, mode->status);
405 EXPORT_SYMBOL(drm_mode_prune_invalid);
408 * drm_mode_compare - compare modes for favorability
409 * @lh_a: list_head for first mode
410 * @lh_b: list_head for second mode
415 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
419 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
420 * positive if @lh_b is better than @lh_a.
422 static int drm_mode_compare(struct list_head *lh_a, struct list_head *lh_b)
424 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
425 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
428 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
429 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
432 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
435 diff = b->clock - a->clock;
439 /* FIXME: what we don't have a list sort function? */
440 /* list sort from Mark J Roberts (mjr@znex.org) */
441 void list_sort(struct list_head *head, int (*cmp)(struct list_head *a, struct list_head *b))
443 struct list_head *p, *q, *e, *list, *tail, *oldhead;
444 int insize, nmerges, psize, qsize, i;
458 for (i = 0; i < insize; i++) {
460 q = q->next == oldhead ? NULL : q->next;
466 while (psize > 0 || (qsize > 0 && q)) {
473 } else if (!qsize || !q) {
479 } else if (cmp(p, q) <= 0) {
512 head->prev = list->prev;
513 list->prev->next = head;
518 * drm_mode_sort - sort mode list
519 * @mode_list: list to sort
522 * Caller must hold a lock protecting @mode_list.
524 * Sort @mode_list by favorability, putting good modes first.
526 void drm_mode_sort(struct list_head *mode_list)
528 list_sort(mode_list, drm_mode_compare);
530 EXPORT_SYMBOL(drm_mode_sort);
533 * drm_mode_connector_list_update - update the mode list for the connector
534 * @connector: the connector to update
537 * Caller must hold a lock protecting @mode_list.
539 * This moves the modes from the @connector probed_modes list
540 * to the actual mode list. It compares the probed mode against the current
541 * list and only adds different modes. All modes unverified after this point
542 * will be removed by the prune invalid modes.
544 void drm_mode_connector_list_update(struct drm_connector *connector)
546 struct drm_display_mode *mode;
547 struct drm_display_mode *pmode, *pt;
549 list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
552 /* go through current modes checking for the new probed mode */
553 list_for_each_entry(mode, &connector->modes, head) {
554 if (drm_mode_equal(pmode, mode)) {
556 /* if equal delete the probed mode */
557 mode->status = pmode->status;
558 list_del(&pmode->head);
565 list_move_tail(&pmode->head, &connector->modes);
569 EXPORT_SYMBOL(drm_mode_connector_list_update);