1*07615495SHans Petter Selasky /* $NetBSD: pickmode.c,v 1.3 2011/04/09 18:22:31 jdc Exp $ */
2*07615495SHans Petter Selasky
3*07615495SHans Petter Selasky /*-
4*07615495SHans Petter Selasky * Copyright (c) 2006 The NetBSD Foundation
5*07615495SHans Petter Selasky * All rights reserved.
6*07615495SHans Petter Selasky *
7*07615495SHans Petter Selasky * this code was contributed to The NetBSD Foundation by Michael Lorenz
8*07615495SHans Petter Selasky *
9*07615495SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
10*07615495SHans Petter Selasky * modification, are permitted provided that the following conditions
11*07615495SHans Petter Selasky * are met:
12*07615495SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
13*07615495SHans Petter Selasky * notice, this list of conditions and the following disclaimer.
14*07615495SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
15*07615495SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
16*07615495SHans Petter Selasky * documentation and/or other materials provided with the distribution.
17*07615495SHans Petter Selasky *
18*07615495SHans Petter Selasky * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS
19*07615495SHans Petter Selasky * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20*07615495SHans Petter Selasky * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*07615495SHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL THE NETBSD FOUNDATION BE LIABLE
22*07615495SHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*07615495SHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24*07615495SHans Petter Selasky * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*07615495SHans Petter Selasky * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26*07615495SHans Petter Selasky * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27*07615495SHans Petter Selasky * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*07615495SHans Petter Selasky * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*07615495SHans Petter Selasky */
30*07615495SHans Petter Selasky
31*07615495SHans Petter Selasky #include <sys/param.h>
32*07615495SHans Petter Selasky #include <sys/libkern.h>
33*07615495SHans Petter Selasky #include <dev/videomode/videomode.h>
34*07615495SHans Petter Selasky #include "opt_videomode.h"
35*07615495SHans Petter Selasky
36*07615495SHans Petter Selasky #ifdef PICKMODE_DEBUG
37*07615495SHans Petter Selasky #define DPRINTF printf
38*07615495SHans Petter Selasky #else
39*07615495SHans Petter Selasky #define DPRINTF while (0) printf
40*07615495SHans Petter Selasky #endif
41*07615495SHans Petter Selasky
42*07615495SHans Petter Selasky const struct videomode *
pick_mode_by_dotclock(int width,int height,int dotclock)43*07615495SHans Petter Selasky pick_mode_by_dotclock(int width, int height, int dotclock)
44*07615495SHans Petter Selasky {
45*07615495SHans Petter Selasky const struct videomode *this, *best = NULL;
46*07615495SHans Petter Selasky int i;
47*07615495SHans Petter Selasky
48*07615495SHans Petter Selasky DPRINTF("%s: looking for %d x %d at up to %d kHz\n", __func__, width,
49*07615495SHans Petter Selasky height, dotclock);
50*07615495SHans Petter Selasky for (i = 0; i < videomode_count; i++) {
51*07615495SHans Petter Selasky this = &videomode_list[i];
52*07615495SHans Petter Selasky if ((this->hdisplay != width) || (this->vdisplay != height) ||
53*07615495SHans Petter Selasky (this->dot_clock > dotclock))
54*07615495SHans Petter Selasky continue;
55*07615495SHans Petter Selasky if (best != NULL) {
56*07615495SHans Petter Selasky if (this->dot_clock > best->dot_clock)
57*07615495SHans Petter Selasky best = this;
58*07615495SHans Petter Selasky } else
59*07615495SHans Petter Selasky best = this;
60*07615495SHans Petter Selasky }
61*07615495SHans Petter Selasky if (best != NULL)
62*07615495SHans Petter Selasky DPRINTF("found %s\n", best->name);
63*07615495SHans Petter Selasky
64*07615495SHans Petter Selasky return best;
65*07615495SHans Petter Selasky }
66*07615495SHans Petter Selasky
67*07615495SHans Petter Selasky const struct videomode *
pick_mode_by_ref(int width,int height,int refresh)68*07615495SHans Petter Selasky pick_mode_by_ref(int width, int height, int refresh)
69*07615495SHans Petter Selasky {
70*07615495SHans Petter Selasky const struct videomode *this, *best = NULL;
71*07615495SHans Petter Selasky int mref, closest = 1000, i, diff;
72*07615495SHans Petter Selasky
73*07615495SHans Petter Selasky DPRINTF("%s: looking for %d x %d at up to %d Hz\n", __func__, width,
74*07615495SHans Petter Selasky height, refresh);
75*07615495SHans Petter Selasky for (i = 0; i < videomode_count; i++) {
76*07615495SHans Petter Selasky this = &videomode_list[i];
77*07615495SHans Petter Selasky mref = this->dot_clock * 1000 / (this->htotal * this->vtotal);
78*07615495SHans Petter Selasky diff = abs(mref - refresh);
79*07615495SHans Petter Selasky if ((this->hdisplay != width) || (this->vdisplay != height))
80*07615495SHans Petter Selasky continue;
81*07615495SHans Petter Selasky DPRINTF("%s in %d hz, diff %d\n", this->name, mref, diff);
82*07615495SHans Petter Selasky if (best != NULL) {
83*07615495SHans Petter Selasky if (diff < closest) {
84*07615495SHans Petter Selasky best = this;
85*07615495SHans Petter Selasky closest = diff;
86*07615495SHans Petter Selasky }
87*07615495SHans Petter Selasky } else {
88*07615495SHans Petter Selasky best = this;
89*07615495SHans Petter Selasky closest = diff;
90*07615495SHans Petter Selasky }
91*07615495SHans Petter Selasky }
92*07615495SHans Petter Selasky if (best != NULL)
93*07615495SHans Petter Selasky DPRINTF("found %s %d\n", best->name, best->dot_clock);
94*07615495SHans Petter Selasky
95*07615495SHans Petter Selasky return best;
96*07615495SHans Petter Selasky }
97*07615495SHans Petter Selasky
98*07615495SHans Petter Selasky static inline void
swap_modes(struct videomode * left,struct videomode * right)99*07615495SHans Petter Selasky swap_modes(struct videomode *left, struct videomode *right)
100*07615495SHans Petter Selasky {
101*07615495SHans Petter Selasky struct videomode temp;
102*07615495SHans Petter Selasky
103*07615495SHans Petter Selasky temp = *left;
104*07615495SHans Petter Selasky *left = *right;
105*07615495SHans Petter Selasky *right = temp;
106*07615495SHans Petter Selasky }
107*07615495SHans Petter Selasky
108*07615495SHans Petter Selasky /*
109*07615495SHans Petter Selasky * Sort modes by refresh rate, aspect ratio (*), then resolution.
110*07615495SHans Petter Selasky * Preferred mode or largest mode is first in the list and other modes
111*07615495SHans Petter Selasky * are sorted on closest match to that mode.
112*07615495SHans Petter Selasky * (*) Note that the aspect ratio calculation treats "close" aspect ratios
113*07615495SHans Petter Selasky * (within 12.5%) as the same for this purpose.
114*07615495SHans Petter Selasky */
115*07615495SHans Petter Selasky #define DIVIDE(x, y) (((x) + ((y) / 2)) / (y))
116*07615495SHans Petter Selasky void
sort_modes(struct videomode * modes,struct videomode ** preferred,int nmodes)117*07615495SHans Petter Selasky sort_modes(struct videomode *modes, struct videomode **preferred, int nmodes)
118*07615495SHans Petter Selasky {
119*07615495SHans Petter Selasky int aspect, refresh, hbest, vbest, abest, atemp, rbest, rtemp;
120*07615495SHans Petter Selasky int i, j;
121*07615495SHans Petter Selasky struct videomode *mtemp = NULL;
122*07615495SHans Petter Selasky
123*07615495SHans Petter Selasky if (nmodes < 2)
124*07615495SHans Petter Selasky return;
125*07615495SHans Petter Selasky
126*07615495SHans Petter Selasky if (*preferred != NULL) {
127*07615495SHans Petter Selasky /* Put the preferred mode first in the list */
128*07615495SHans Petter Selasky aspect = (*preferred)->hdisplay * 100 / (*preferred)->vdisplay;
129*07615495SHans Petter Selasky refresh = DIVIDE(DIVIDE((*preferred)->dot_clock * 1000,
130*07615495SHans Petter Selasky (*preferred)->htotal), (*preferred)->vtotal);
131*07615495SHans Petter Selasky if (*preferred != modes) {
132*07615495SHans Petter Selasky swap_modes(*preferred, modes);
133*07615495SHans Petter Selasky *preferred = modes;
134*07615495SHans Petter Selasky }
135*07615495SHans Petter Selasky } else {
136*07615495SHans Petter Selasky /*
137*07615495SHans Petter Selasky * Find the largest horizontal and vertical mode and put that
138*07615495SHans Petter Selasky * first in the list. Preferred refresh rate is taken from
139*07615495SHans Petter Selasky * the first mode of this size.
140*07615495SHans Petter Selasky */
141*07615495SHans Petter Selasky hbest = 0;
142*07615495SHans Petter Selasky vbest = 0;
143*07615495SHans Petter Selasky for (i = 0; i < nmodes; i++) {
144*07615495SHans Petter Selasky if (modes[i].hdisplay > hbest) {
145*07615495SHans Petter Selasky hbest = modes[i].hdisplay;
146*07615495SHans Petter Selasky vbest = modes[i].vdisplay;
147*07615495SHans Petter Selasky mtemp = &modes[i];
148*07615495SHans Petter Selasky } else if (modes[i].hdisplay == hbest &&
149*07615495SHans Petter Selasky modes[i].vdisplay > vbest) {
150*07615495SHans Petter Selasky vbest = modes[i].vdisplay;
151*07615495SHans Petter Selasky mtemp = &modes[i];
152*07615495SHans Petter Selasky }
153*07615495SHans Petter Selasky }
154*07615495SHans Petter Selasky aspect = mtemp->hdisplay * 100 / mtemp->vdisplay;
155*07615495SHans Petter Selasky refresh = DIVIDE(DIVIDE(mtemp->dot_clock * 1000,
156*07615495SHans Petter Selasky mtemp->htotal), mtemp->vtotal);
157*07615495SHans Petter Selasky if (mtemp != modes)
158*07615495SHans Petter Selasky swap_modes(mtemp, modes);
159*07615495SHans Petter Selasky }
160*07615495SHans Petter Selasky
161*07615495SHans Petter Selasky /* Sort other modes by refresh rate, aspect ratio, then resolution */
162*07615495SHans Petter Selasky for (j = 1; j < nmodes - 1; j++) {
163*07615495SHans Petter Selasky rbest = 1000;
164*07615495SHans Petter Selasky abest = 1000;
165*07615495SHans Petter Selasky hbest = 0;
166*07615495SHans Petter Selasky vbest = 0;
167*07615495SHans Petter Selasky for (i = j; i < nmodes; i++) {
168*07615495SHans Petter Selasky rtemp = abs(refresh -
169*07615495SHans Petter Selasky DIVIDE(DIVIDE(modes[i].dot_clock * 1000,
170*07615495SHans Petter Selasky modes[i].htotal), modes[i].vtotal));
171*07615495SHans Petter Selasky atemp = (modes[i].hdisplay * 100 / modes[i].vdisplay);
172*07615495SHans Petter Selasky if (rtemp < rbest) {
173*07615495SHans Petter Selasky rbest = rtemp;
174*07615495SHans Petter Selasky mtemp = &modes[i];
175*07615495SHans Petter Selasky }
176*07615495SHans Petter Selasky if (rtemp == rbest) {
177*07615495SHans Petter Selasky /* Treat "close" aspect ratios as identical */
178*07615495SHans Petter Selasky if (abs(abest - atemp) > (abest / 8) &&
179*07615495SHans Petter Selasky abs(aspect - atemp) < abs(aspect - abest)) {
180*07615495SHans Petter Selasky abest = atemp;
181*07615495SHans Petter Selasky mtemp = &modes[i];
182*07615495SHans Petter Selasky }
183*07615495SHans Petter Selasky if (atemp == abest ||
184*07615495SHans Petter Selasky abs(abest - atemp) <= (abest / 8)) {
185*07615495SHans Petter Selasky if (modes[i].hdisplay > hbest) {
186*07615495SHans Petter Selasky hbest = modes[i].hdisplay;
187*07615495SHans Petter Selasky mtemp = &modes[i];
188*07615495SHans Petter Selasky }
189*07615495SHans Petter Selasky if (modes[i].hdisplay == hbest &&
190*07615495SHans Petter Selasky modes[i].vdisplay > vbest) {
191*07615495SHans Petter Selasky vbest = modes[i].vdisplay;
192*07615495SHans Petter Selasky mtemp = &modes[i];
193*07615495SHans Petter Selasky }
194*07615495SHans Petter Selasky }
195*07615495SHans Petter Selasky }
196*07615495SHans Petter Selasky }
197*07615495SHans Petter Selasky if (mtemp != &modes[j])
198*07615495SHans Petter Selasky swap_modes(mtemp, &modes[j]);
199*07615495SHans Petter Selasky }
200*07615495SHans Petter Selasky }
201