1*cd9ad85fSchristos /* $NetBSD: pickmode.c,v 1.4 2011/04/09 20:53:39 christos Exp $ */
2d3a30408Smacallan
3d3a30408Smacallan /*-
4d3a30408Smacallan * Copyright (c) 2006 The NetBSD Foundation
5d3a30408Smacallan * All rights reserved.
6d3a30408Smacallan *
7d3a30408Smacallan * this code was contributed to The NetBSD Foundation by Michael Lorenz
8d3a30408Smacallan *
9d3a30408Smacallan * Redistribution and use in source and binary forms, with or without
10d3a30408Smacallan * modification, are permitted provided that the following conditions
11d3a30408Smacallan * are met:
12d3a30408Smacallan * 1. Redistributions of source code must retain the above copyright
13d3a30408Smacallan * notice, this list of conditions and the following disclaimer.
14d3a30408Smacallan * 2. Redistributions in binary form must reproduce the above copyright
15d3a30408Smacallan * notice, this list of conditions and the following disclaimer in the
16d3a30408Smacallan * documentation and/or other materials provided with the distribution.
17d3a30408Smacallan *
18d3a30408Smacallan * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS
19d3a30408Smacallan * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20d3a30408Smacallan * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21d3a30408Smacallan * ARE DISCLAIMED. IN NO EVENT SHALL THE NETBSD FOUNDATION BE LIABLE
22d3a30408Smacallan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23d3a30408Smacallan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24d3a30408Smacallan * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25d3a30408Smacallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26d3a30408Smacallan * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27d3a30408Smacallan * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28d3a30408Smacallan * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29d3a30408Smacallan */
30d3a30408Smacallan
31d3a30408Smacallan #include <sys/cdefs.h>
32*cd9ad85fSchristos __KERNEL_RCSID(0, "$NetBSD: pickmode.c,v 1.4 2011/04/09 20:53:39 christos Exp $");
33d3a30408Smacallan
34d3a30408Smacallan #include <sys/param.h>
35d3a30408Smacallan #include <dev/videomode/videomode.h>
36d3a30408Smacallan #include "opt_videomode.h"
37d3a30408Smacallan
38d3a30408Smacallan #ifdef PICKMODE_DEBUG
39d3a30408Smacallan #define DPRINTF printf
40d3a30408Smacallan #else
41d3a30408Smacallan #define DPRINTF while (0) printf
42d3a30408Smacallan #endif
43d3a30408Smacallan
44d3a30408Smacallan const struct videomode *
pick_mode_by_dotclock(int width,int height,int dotclock)45d3a30408Smacallan pick_mode_by_dotclock(int width, int height, int dotclock)
46d3a30408Smacallan {
47d3a30408Smacallan const struct videomode *this, *best = NULL;
48d3a30408Smacallan int i;
49d3a30408Smacallan
50d3a30408Smacallan DPRINTF("%s: looking for %d x %d at up to %d kHz\n", __func__, width,
51d3a30408Smacallan height, dotclock);
52d3a30408Smacallan for (i = 0; i < videomode_count; i++) {
53d3a30408Smacallan this = &videomode_list[i];
54d3a30408Smacallan if ((this->hdisplay != width) || (this->vdisplay != height) ||
55d3a30408Smacallan (this->dot_clock > dotclock))
56d3a30408Smacallan continue;
57d3a30408Smacallan if (best != NULL) {
58d3a30408Smacallan if (this->dot_clock > best->dot_clock)
59d3a30408Smacallan best = this;
60d3a30408Smacallan } else
61d3a30408Smacallan best = this;
62d3a30408Smacallan }
63d3a30408Smacallan if (best != NULL)
64d3a30408Smacallan DPRINTF("found %s\n", best->name);
65d3a30408Smacallan
66d3a30408Smacallan return best;
67d3a30408Smacallan }
68d3a30408Smacallan
69d3a30408Smacallan const struct videomode *
pick_mode_by_ref(int width,int height,int refresh)70d3a30408Smacallan pick_mode_by_ref(int width, int height, int refresh)
71d3a30408Smacallan {
72d3a30408Smacallan const struct videomode *this, *best = NULL;
73d3a30408Smacallan int mref, closest = 1000, i, diff;
74d3a30408Smacallan
75d3a30408Smacallan DPRINTF("%s: looking for %d x %d at up to %d Hz\n", __func__, width,
76d3a30408Smacallan height, refresh);
77d3a30408Smacallan for (i = 0; i < videomode_count; i++) {
78d3a30408Smacallan
79d3a30408Smacallan this = &videomode_list[i];
80d3a30408Smacallan mref = this->dot_clock * 1000 / (this->htotal * this->vtotal);
81d3a30408Smacallan diff = abs(mref - refresh);
82d199f85dSmacallan if ((this->hdisplay != width) || (this->vdisplay != height))
83d3a30408Smacallan continue;
84d199f85dSmacallan DPRINTF("%s in %d hz, diff %d\n", this->name, mref, diff);
85d3a30408Smacallan if (best != NULL) {
86d3a30408Smacallan if (diff < closest) {
87d3a30408Smacallan best = this;
88d3a30408Smacallan closest = diff;
89d3a30408Smacallan }
90d199f85dSmacallan } else {
91d3a30408Smacallan best = this;
92d199f85dSmacallan closest = diff;
93d199f85dSmacallan }
94d3a30408Smacallan }
95d3a30408Smacallan if (best != NULL)
96d199f85dSmacallan DPRINTF("found %s %d\n", best->name, best->dot_clock);
97d3a30408Smacallan
98d3a30408Smacallan return best;
99d3a30408Smacallan }
1001271f8ecSjdc
1011271f8ecSjdc static inline void
swap_modes(struct videomode * left,struct videomode * right)1021271f8ecSjdc swap_modes(struct videomode *left, struct videomode *right)
1031271f8ecSjdc {
1041271f8ecSjdc struct videomode temp;
1051271f8ecSjdc
106*cd9ad85fSchristos temp = *left;
107*cd9ad85fSchristos *left = *right;
108*cd9ad85fSchristos *right = temp;
1091271f8ecSjdc }
1101271f8ecSjdc
1111271f8ecSjdc /*
1121271f8ecSjdc * Sort modes by refresh rate, aspect ratio (*), then resolution.
1131271f8ecSjdc * Preferred mode or largest mode is first in the list and other modes
1141271f8ecSjdc * are sorted on closest match to that mode.
1151271f8ecSjdc * (*) Note that the aspect ratio calculation treats "close" aspect ratios
1161271f8ecSjdc * (within 12.5%) as the same for this purpose.
1171271f8ecSjdc */
1181271f8ecSjdc #define DIVIDE(x, y) (((x) + ((y) / 2)) / (y))
1191271f8ecSjdc void
sort_modes(struct videomode * modes,struct videomode ** preferred,int nmodes)1201271f8ecSjdc sort_modes(struct videomode *modes, struct videomode **preferred, int nmodes)
1211271f8ecSjdc {
1221271f8ecSjdc int aspect, refresh, hbest, vbest, abest, atemp, rbest, rtemp;
1231271f8ecSjdc int i, j;
1241271f8ecSjdc struct videomode *mtemp = NULL;
1251271f8ecSjdc
1261271f8ecSjdc if (nmodes < 2)
1271271f8ecSjdc return;
1281271f8ecSjdc
1291271f8ecSjdc if (*preferred != NULL) {
1301271f8ecSjdc /* Put the preferred mode first in the list */
1311271f8ecSjdc aspect = (*preferred)->hdisplay * 100 / (*preferred)->vdisplay;
1321271f8ecSjdc refresh = DIVIDE(DIVIDE((*preferred)->dot_clock * 1000,
1331271f8ecSjdc (*preferred)->htotal), (*preferred)->vtotal);
134*cd9ad85fSchristos if (*preferred != modes) {
135*cd9ad85fSchristos swap_modes(*preferred, modes);
1361271f8ecSjdc *preferred = modes;
1371271f8ecSjdc }
1381271f8ecSjdc } else {
1391271f8ecSjdc /*
1401271f8ecSjdc * Find the largest horizontal and vertical mode and put that
1411271f8ecSjdc * first in the list. Preferred refresh rate is taken from
1421271f8ecSjdc * the first mode of this size.
1431271f8ecSjdc */
1441271f8ecSjdc hbest = 0;
1451271f8ecSjdc vbest = 0;
1461271f8ecSjdc for (i = 0; i < nmodes; i++) {
1471271f8ecSjdc if (modes[i].hdisplay > hbest) {
1481271f8ecSjdc hbest = modes[i].hdisplay;
1491271f8ecSjdc vbest = modes[i].vdisplay;
1501271f8ecSjdc mtemp = &modes[i];
1511271f8ecSjdc } else if (modes[i].hdisplay == hbest &&
1521271f8ecSjdc modes[i].vdisplay > vbest) {
1531271f8ecSjdc vbest = modes[i].vdisplay;
1541271f8ecSjdc mtemp = &modes[i];
1551271f8ecSjdc }
1561271f8ecSjdc }
1571271f8ecSjdc aspect = mtemp->hdisplay * 100 / mtemp->vdisplay;
1581271f8ecSjdc refresh = DIVIDE(DIVIDE(mtemp->dot_clock * 1000,
1591271f8ecSjdc mtemp->htotal), mtemp->vtotal);
1601271f8ecSjdc if (mtemp != modes)
1611271f8ecSjdc swap_modes(mtemp, modes);
1621271f8ecSjdc }
1631271f8ecSjdc
1641271f8ecSjdc /* Sort other modes by refresh rate, aspect ratio, then resolution */
1651271f8ecSjdc for (j = 1; j < nmodes - 1; j++) {
1661271f8ecSjdc rbest = 1000;
1671271f8ecSjdc abest = 1000;
1681271f8ecSjdc hbest = 0;
1691271f8ecSjdc vbest = 0;
1701271f8ecSjdc for (i = j; i < nmodes; i++) {
1711271f8ecSjdc rtemp = abs(refresh -
1721271f8ecSjdc DIVIDE(DIVIDE(modes[i].dot_clock * 1000,
1731271f8ecSjdc modes[i].htotal), modes[i].vtotal));
1741271f8ecSjdc atemp = (modes[i].hdisplay * 100 / modes[i].vdisplay);
1751271f8ecSjdc if (rtemp < rbest) {
1761271f8ecSjdc rbest = rtemp;
1771271f8ecSjdc mtemp = &modes[i];
1781271f8ecSjdc }
1791271f8ecSjdc if (rtemp == rbest) {
1801271f8ecSjdc /* Treat "close" aspect ratios as identical */
1811271f8ecSjdc if (abs(abest - atemp) > (abest / 8) &&
182*cd9ad85fSchristos abs(aspect - atemp) < abs(aspect - abest)) {
1831271f8ecSjdc abest = atemp;
1841271f8ecSjdc mtemp = &modes[i];
1851271f8ecSjdc }
1861271f8ecSjdc if (atemp == abest ||
1871271f8ecSjdc abs(abest - atemp) <= (abest / 8)) {
1881271f8ecSjdc if (modes[i].hdisplay > hbest) {
1891271f8ecSjdc hbest = modes[i].hdisplay;
1901271f8ecSjdc mtemp = &modes[i];
1911271f8ecSjdc }
1921271f8ecSjdc if (modes[i].hdisplay == hbest &&
1931271f8ecSjdc modes[i].vdisplay > vbest) {
1941271f8ecSjdc vbest = modes[i].vdisplay;
1951271f8ecSjdc mtemp = &modes[i];
1961271f8ecSjdc }
1971271f8ecSjdc }
1981271f8ecSjdc }
1991271f8ecSjdc }
2001271f8ecSjdc if (mtemp != &modes[j])
2011271f8ecSjdc swap_modes(mtemp, &modes[j]);
2021271f8ecSjdc }
2031271f8ecSjdc }
204