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