blob: de450c1fb86926f39190f052d7644d56f60ccc5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/modedb.c -- Standard video mode database management
3 *
4 * Copyright (C) 1999 Geert Uytterhoeven
5 *
6 * 2001 - Documented with DocBook
7 * - Brad Douglas <brad@neruo.com>
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive for
11 * more details.
12 */
13
14#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fb.h>
Andy Shevchenko2cb96f82009-12-15 16:46:24 -080017#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#undef DEBUG
20
21#define name_matches(v, s, l) \
22 ((v).name && !strncmp((s), (v).name, (l)) && strlen((v).name) == (l))
23#define res_matches(v, x, y) \
24 ((v).xres == (x) && (v).yres == (y))
25
26#ifdef DEBUG
Harvey Harrison5ae12172008-04-28 02:15:47 -070027#define DPRINTK(fmt, args...) printk("modedb %s: " fmt, __func__ , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#else
29#define DPRINTK(fmt, args...)
30#endif
31
Geert Uytterhoeven9a054fb2007-10-16 01:29:51 -070032const char *fb_mode_option;
Geoff Levand659179b2008-06-05 22:46:44 -070033EXPORT_SYMBOL_GPL(fb_mode_option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 /*
36 * Standard video mode definitions (taken from XFree86)
37 */
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static const struct fb_videomode modedb[] = {
40 {
41 /* 640x400 @ 70 Hz, 31.5 kHz hsync */
42 NULL, 70, 640, 400, 39721, 40, 24, 39, 9, 96, 2,
43 0, FB_VMODE_NONINTERLACED
44 }, {
45 /* 640x480 @ 60 Hz, 31.5 kHz hsync */
46 NULL, 60, 640, 480, 39721, 40, 24, 32, 11, 96, 2,
47 0, FB_VMODE_NONINTERLACED
48 }, {
49 /* 800x600 @ 56 Hz, 35.15 kHz hsync */
50 NULL, 56, 800, 600, 27777, 128, 24, 22, 1, 72, 2,
51 0, FB_VMODE_NONINTERLACED
52 }, {
53 /* 1024x768 @ 87 Hz interlaced, 35.5 kHz hsync */
54 NULL, 87, 1024, 768, 22271, 56, 24, 33, 8, 160, 8,
55 0, FB_VMODE_INTERLACED
56 }, {
57 /* 640x400 @ 85 Hz, 37.86 kHz hsync */
58 NULL, 85, 640, 400, 31746, 96, 32, 41, 1, 64, 3,
59 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
60 }, {
61 /* 640x480 @ 72 Hz, 36.5 kHz hsync */
62 NULL, 72, 640, 480, 31746, 144, 40, 30, 8, 40, 3,
63 0, FB_VMODE_NONINTERLACED
64 }, {
65 /* 640x480 @ 75 Hz, 37.50 kHz hsync */
66 NULL, 75, 640, 480, 31746, 120, 16, 16, 1, 64, 3,
67 0, FB_VMODE_NONINTERLACED
68 }, {
69 /* 800x600 @ 60 Hz, 37.8 kHz hsync */
70 NULL, 60, 800, 600, 25000, 88, 40, 23, 1, 128, 4,
71 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
72 }, {
73 /* 640x480 @ 85 Hz, 43.27 kHz hsync */
74 NULL, 85, 640, 480, 27777, 80, 56, 25, 1, 56, 3,
75 0, FB_VMODE_NONINTERLACED
76 }, {
77 /* 1152x864 @ 89 Hz interlaced, 44 kHz hsync */
Antonino A. Daplas9d775e172007-10-16 01:28:47 -070078 NULL, 89, 1152, 864, 15384, 96, 16, 110, 1, 216, 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 0, FB_VMODE_INTERLACED
80 }, {
81 /* 800x600 @ 72 Hz, 48.0 kHz hsync */
82 NULL, 72, 800, 600, 20000, 64, 56, 23, 37, 120, 6,
83 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
84 }, {
85 /* 1024x768 @ 60 Hz, 48.4 kHz hsync */
86 NULL, 60, 1024, 768, 15384, 168, 8, 29, 3, 144, 6,
87 0, FB_VMODE_NONINTERLACED
88 }, {
89 /* 640x480 @ 100 Hz, 53.01 kHz hsync */
90 NULL, 100, 640, 480, 21834, 96, 32, 36, 8, 96, 6,
91 0, FB_VMODE_NONINTERLACED
92 }, {
93 /* 1152x864 @ 60 Hz, 53.5 kHz hsync */
94 NULL, 60, 1152, 864, 11123, 208, 64, 16, 4, 256, 8,
95 0, FB_VMODE_NONINTERLACED
96 }, {
97 /* 800x600 @ 85 Hz, 55.84 kHz hsync */
98 NULL, 85, 800, 600, 16460, 160, 64, 36, 16, 64, 5,
99 0, FB_VMODE_NONINTERLACED
100 }, {
101 /* 1024x768 @ 70 Hz, 56.5 kHz hsync */
102 NULL, 70, 1024, 768, 13333, 144, 24, 29, 3, 136, 6,
103 0, FB_VMODE_NONINTERLACED
104 }, {
105 /* 1280x1024 @ 87 Hz interlaced, 51 kHz hsync */
106 NULL, 87, 1280, 1024, 12500, 56, 16, 128, 1, 216, 12,
107 0, FB_VMODE_INTERLACED
108 }, {
109 /* 800x600 @ 100 Hz, 64.02 kHz hsync */
110 NULL, 100, 800, 600, 14357, 160, 64, 30, 4, 64, 6,
111 0, FB_VMODE_NONINTERLACED
112 }, {
113 /* 1024x768 @ 76 Hz, 62.5 kHz hsync */
114 NULL, 76, 1024, 768, 11764, 208, 8, 36, 16, 120, 3,
115 0, FB_VMODE_NONINTERLACED
116 }, {
117 /* 1152x864 @ 70 Hz, 62.4 kHz hsync */
118 NULL, 70, 1152, 864, 10869, 106, 56, 20, 1, 160, 10,
119 0, FB_VMODE_NONINTERLACED
120 }, {
121 /* 1280x1024 @ 61 Hz, 64.2 kHz hsync */
122 NULL, 61, 1280, 1024, 9090, 200, 48, 26, 1, 184, 3,
123 0, FB_VMODE_NONINTERLACED
124 }, {
125 /* 1400x1050 @ 60Hz, 63.9 kHz hsync */
Antonino A. Daplas9d775e172007-10-16 01:28:47 -0700126 NULL, 60, 1400, 1050, 9259, 136, 40, 13, 1, 112, 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 0, FB_VMODE_NONINTERLACED
128 }, {
129 /* 1400x1050 @ 75,107 Hz, 82,392 kHz +hsync +vsync*/
Antonino A. Daplas9d775e172007-10-16 01:28:47 -0700130 NULL, 75, 1400, 1050, 7190, 120, 56, 23, 10, 112, 13,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
132 }, {
133 /* 1400x1050 @ 60 Hz, ? kHz +hsync +vsync*/
134 NULL, 60, 1400, 1050, 9259, 128, 40, 12, 0, 112, 3,
135 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
136 }, {
137 /* 1024x768 @ 85 Hz, 70.24 kHz hsync */
138 NULL, 85, 1024, 768, 10111, 192, 32, 34, 14, 160, 6,
139 0, FB_VMODE_NONINTERLACED
140 }, {
141 /* 1152x864 @ 78 Hz, 70.8 kHz hsync */
142 NULL, 78, 1152, 864, 9090, 228, 88, 32, 0, 84, 12,
143 0, FB_VMODE_NONINTERLACED
144 }, {
145 /* 1280x1024 @ 70 Hz, 74.59 kHz hsync */
146 NULL, 70, 1280, 1024, 7905, 224, 32, 28, 8, 160, 8,
147 0, FB_VMODE_NONINTERLACED
148 }, {
149 /* 1600x1200 @ 60Hz, 75.00 kHz hsync */
150 NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
151 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
152 }, {
153 /* 1152x864 @ 84 Hz, 76.0 kHz hsync */
154 NULL, 84, 1152, 864, 7407, 184, 312, 32, 0, 128, 12,
155 0, FB_VMODE_NONINTERLACED
156 }, {
157 /* 1280x1024 @ 74 Hz, 78.85 kHz hsync */
158 NULL, 74, 1280, 1024, 7407, 256, 32, 34, 3, 144, 3,
159 0, FB_VMODE_NONINTERLACED
160 }, {
161 /* 1024x768 @ 100Hz, 80.21 kHz hsync */
162 NULL, 100, 1024, 768, 8658, 192, 32, 21, 3, 192, 10,
163 0, FB_VMODE_NONINTERLACED
164 }, {
165 /* 1280x1024 @ 76 Hz, 81.13 kHz hsync */
166 NULL, 76, 1280, 1024, 7407, 248, 32, 34, 3, 104, 3,
167 0, FB_VMODE_NONINTERLACED
168 }, {
169 /* 1600x1200 @ 70 Hz, 87.50 kHz hsync */
170 NULL, 70, 1600, 1200, 5291, 304, 64, 46, 1, 192, 3,
171 0, FB_VMODE_NONINTERLACED
172 }, {
173 /* 1152x864 @ 100 Hz, 89.62 kHz hsync */
174 NULL, 100, 1152, 864, 7264, 224, 32, 17, 2, 128, 19,
175 0, FB_VMODE_NONINTERLACED
176 }, {
177 /* 1280x1024 @ 85 Hz, 91.15 kHz hsync */
178 NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
179 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
180 }, {
181 /* 1600x1200 @ 75 Hz, 93.75 kHz hsync */
182 NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
183 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
184 }, {
Olaf Heringb0c87972006-03-27 01:17:38 -0800185 /* 1680x1050 @ 60 Hz, 65.191 kHz hsync */
186 NULL, 60, 1680, 1050, 6848, 280, 104, 30, 3, 176, 6,
187 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
188 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* 1600x1200 @ 85 Hz, 105.77 kHz hsync */
190 NULL, 85, 1600, 1200, 4545, 272, 16, 37, 4, 192, 3,
191 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
192 }, {
193 /* 1280x1024 @ 100 Hz, 107.16 kHz hsync */
194 NULL, 100, 1280, 1024, 5502, 256, 32, 26, 7, 128, 15,
195 0, FB_VMODE_NONINTERLACED
196 }, {
197 /* 1800x1440 @ 64Hz, 96.15 kHz hsync */
198 NULL, 64, 1800, 1440, 4347, 304, 96, 46, 1, 192, 3,
199 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
200 }, {
201 /* 1800x1440 @ 70Hz, 104.52 kHz hsync */
202 NULL, 70, 1800, 1440, 4000, 304, 96, 46, 1, 192, 3,
203 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
204 }, {
205 /* 512x384 @ 78 Hz, 31.50 kHz hsync */
206 NULL, 78, 512, 384, 49603, 48, 16, 16, 1, 64, 3,
207 0, FB_VMODE_NONINTERLACED
208 }, {
209 /* 512x384 @ 85 Hz, 34.38 kHz hsync */
210 NULL, 85, 512, 384, 45454, 48, 16, 16, 1, 64, 3,
211 0, FB_VMODE_NONINTERLACED
212 }, {
213 /* 320x200 @ 70 Hz, 31.5 kHz hsync, 8:5 aspect ratio */
214 NULL, 70, 320, 200, 79440, 16, 16, 20, 4, 48, 1,
215 0, FB_VMODE_DOUBLE
216 }, {
217 /* 320x240 @ 60 Hz, 31.5 kHz hsync, 4:3 aspect ratio */
218 NULL, 60, 320, 240, 79440, 16, 16, 16, 5, 48, 1,
219 0, FB_VMODE_DOUBLE
220 }, {
221 /* 320x240 @ 72 Hz, 36.5 kHz hsync */
222 NULL, 72, 320, 240, 63492, 16, 16, 16, 4, 48, 2,
223 0, FB_VMODE_DOUBLE
224 }, {
225 /* 400x300 @ 56 Hz, 35.2 kHz hsync, 4:3 aspect ratio */
226 NULL, 56, 400, 300, 55555, 64, 16, 10, 1, 32, 1,
227 0, FB_VMODE_DOUBLE
228 }, {
229 /* 400x300 @ 60 Hz, 37.8 kHz hsync */
230 NULL, 60, 400, 300, 50000, 48, 16, 11, 1, 64, 2,
231 0, FB_VMODE_DOUBLE
232 }, {
233 /* 400x300 @ 72 Hz, 48.0 kHz hsync */
234 NULL, 72, 400, 300, 40000, 32, 24, 11, 19, 64, 3,
235 0, FB_VMODE_DOUBLE
236 }, {
237 /* 480x300 @ 56 Hz, 35.2 kHz hsync, 8:5 aspect ratio */
238 NULL, 56, 480, 300, 46176, 80, 16, 10, 1, 40, 1,
239 0, FB_VMODE_DOUBLE
240 }, {
241 /* 480x300 @ 60 Hz, 37.8 kHz hsync */
242 NULL, 60, 480, 300, 41858, 56, 16, 11, 1, 80, 2,
243 0, FB_VMODE_DOUBLE
244 }, {
245 /* 480x300 @ 63 Hz, 39.6 kHz hsync */
246 NULL, 63, 480, 300, 40000, 56, 16, 11, 1, 80, 2,
247 0, FB_VMODE_DOUBLE
248 }, {
249 /* 480x300 @ 72 Hz, 48.0 kHz hsync */
250 NULL, 72, 480, 300, 33386, 40, 24, 11, 19, 80, 3,
251 0, FB_VMODE_DOUBLE
Antonino Daplasdb6778d2005-08-08 14:22:43 +0800252 }, {
253 /* 1920x1200 @ 60 Hz, 74.5 Khz hsync */
254 NULL, 60, 1920, 1200, 5177, 128, 336, 1, 38, 208, 3,
255 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
256 FB_VMODE_NONINTERLACED
Michael Hanselmann1dfcdfa2005-11-08 21:39:06 -0800257 }, {
258 /* 1152x768, 60 Hz, PowerBook G4 Titanium I and II */
Antonino A. Daplas9d775e172007-10-16 01:28:47 -0700259 NULL, 60, 1152, 768, 14047, 158, 26, 29, 3, 136, 6,
Michael Hanselmann1dfcdfa2005-11-08 21:39:06 -0800260 FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
Dennis Munsief71689e2006-06-26 00:26:39 -0700261 }, {
262 /* 1366x768, 60 Hz, 47.403 kHz hsync, WXGA 16:9 aspect ratio */
263 NULL, 60, 1366, 768, 13806, 120, 10, 14, 3, 32, 5,
264 0, FB_VMODE_NONINTERLACED
Alex545c4422008-01-17 15:21:18 -0800265 }, {
266 /* 1280x800, 60 Hz, 47.403 kHz hsync, WXGA 16:10 aspect ratio */
267 NULL, 60, 1280, 800, 12048, 200, 64, 24, 1, 136, 3,
268 0, FB_VMODE_NONINTERLACED
Paul Menzel27b7f2e2009-06-16 15:34:20 -0700269 }, {
270 /* 720x576i @ 50 Hz, 15.625 kHz hsync (PAL RGB) */
271 NULL, 50, 720, 576, 74074, 64, 16, 39, 5, 64, 5,
272 0, FB_VMODE_INTERLACED
273 }, {
274 /* 800x520i @ 50 Hz, 15.625 kHz hsync (PAL RGB) */
275 NULL, 50, 800, 520, 58823, 144, 64, 72, 28, 80, 5,
276 0, FB_VMODE_INTERLACED
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 },
278};
279
280#ifdef CONFIG_FB_MODE_HELPERS
281const struct fb_videomode vesa_modes[] = {
282 /* 0 640x350-85 VESA */
283 { NULL, 85, 640, 350, 31746, 96, 32, 60, 32, 64, 3,
284 FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA},
285 /* 1 640x400-85 VESA */
286 { NULL, 85, 640, 400, 31746, 96, 32, 41, 01, 64, 3,
287 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
288 /* 2 720x400-85 VESA */
289 { NULL, 85, 721, 400, 28169, 108, 36, 42, 01, 72, 3,
290 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
291 /* 3 640x480-60 VESA */
292 { NULL, 60, 640, 480, 39682, 48, 16, 33, 10, 96, 2,
293 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
294 /* 4 640x480-72 VESA */
295 { NULL, 72, 640, 480, 31746, 128, 24, 29, 9, 40, 2,
296 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
297 /* 5 640x480-75 VESA */
298 { NULL, 75, 640, 480, 31746, 120, 16, 16, 01, 64, 3,
299 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
300 /* 6 640x480-85 VESA */
301 { NULL, 85, 640, 480, 27777, 80, 56, 25, 01, 56, 3,
302 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
303 /* 7 800x600-56 VESA */
304 { NULL, 56, 800, 600, 27777, 128, 24, 22, 01, 72, 2,
305 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
306 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
307 /* 8 800x600-60 VESA */
308 { NULL, 60, 800, 600, 25000, 88, 40, 23, 01, 128, 4,
309 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
310 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
311 /* 9 800x600-72 VESA */
312 { NULL, 72, 800, 600, 20000, 64, 56, 23, 37, 120, 6,
313 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
314 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
315 /* 10 800x600-75 VESA */
316 { NULL, 75, 800, 600, 20202, 160, 16, 21, 01, 80, 3,
317 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
318 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
319 /* 11 800x600-85 VESA */
320 { NULL, 85, 800, 600, 17761, 152, 32, 27, 01, 64, 3,
321 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
322 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
323 /* 12 1024x768i-43 VESA */
Antonino A. Daplas9d775e172007-10-16 01:28:47 -0700324 { NULL, 43, 1024, 768, 22271, 56, 8, 41, 0, 176, 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
326 FB_VMODE_INTERLACED, FB_MODE_IS_VESA },
327 /* 13 1024x768-60 VESA */
328 { NULL, 60, 1024, 768, 15384, 160, 24, 29, 3, 136, 6,
329 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
330 /* 14 1024x768-70 VESA */
331 { NULL, 70, 1024, 768, 13333, 144, 24, 29, 3, 136, 6,
332 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
333 /* 15 1024x768-75 VESA */
334 { NULL, 75, 1024, 768, 12690, 176, 16, 28, 1, 96, 3,
335 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
336 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
337 /* 16 1024x768-85 VESA */
338 { NULL, 85, 1024, 768, 10582, 208, 48, 36, 1, 96, 3,
339 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
340 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
341 /* 17 1152x864-75 VESA */
Jiri Moravec3efbb982009-01-06 14:42:29 -0800342 { NULL, 75, 1152, 864, 9259, 256, 64, 32, 1, 128, 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
344 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
345 /* 18 1280x960-60 VESA */
346 { NULL, 60, 1280, 960, 9259, 312, 96, 36, 1, 112, 3,
347 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
348 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
349 /* 19 1280x960-85 VESA */
350 { NULL, 85, 1280, 960, 6734, 224, 64, 47, 1, 160, 3,
351 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
352 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
353 /* 20 1280x1024-60 VESA */
354 { NULL, 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
355 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
356 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
357 /* 21 1280x1024-75 VESA */
358 { NULL, 75, 1280, 1024, 7407, 248, 16, 38, 1, 144, 3,
359 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
360 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
361 /* 22 1280x1024-85 VESA */
362 { NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
363 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
364 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
365 /* 23 1600x1200-60 VESA */
366 { NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
367 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
368 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
369 /* 24 1600x1200-65 VESA */
370 { NULL, 65, 1600, 1200, 5698, 304, 64, 46, 1, 192, 3,
371 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
372 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
373 /* 25 1600x1200-70 VESA */
374 { NULL, 70, 1600, 1200, 5291, 304, 64, 46, 1, 192, 3,
375 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
376 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
377 /* 26 1600x1200-75 VESA */
378 { NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
379 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
380 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
381 /* 27 1600x1200-85 VESA */
382 { NULL, 85, 1600, 1200, 4357, 304, 64, 46, 1, 192, 3,
383 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
384 FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
385 /* 28 1792x1344-60 VESA */
386 { NULL, 60, 1792, 1344, 4882, 328, 128, 46, 1, 200, 3,
387 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
388 /* 29 1792x1344-75 VESA */
389 { NULL, 75, 1792, 1344, 3831, 352, 96, 69, 1, 216, 3,
390 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
391 /* 30 1856x1392-60 VESA */
392 { NULL, 60, 1856, 1392, 4580, 352, 96, 43, 1, 224, 3,
393 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
394 /* 31 1856x1392-75 VESA */
395 { NULL, 75, 1856, 1392, 3472, 352, 128, 104, 1, 224, 3,
396 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
397 /* 32 1920x1440-60 VESA */
398 { NULL, 60, 1920, 1440, 4273, 344, 128, 56, 1, 200, 3,
399 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
400 /* 33 1920x1440-75 VESA */
Antonino A. Daplas9d775e172007-10-16 01:28:47 -0700401 { NULL, 75, 1920, 1440, 3367, 352, 144, 56, 1, 224, 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
403};
404EXPORT_SYMBOL(vesa_modes);
405#endif /* CONFIG_FB_MODE_HELPERS */
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/**
408 * fb_try_mode - test a video mode
409 * @var: frame buffer user defined part of display
410 * @info: frame buffer info structure
411 * @mode: frame buffer video mode structure
412 * @bpp: color depth in bits per pixel
413 *
414 * Tries a video mode to test it's validity for device @info.
415 *
416 * Returns 1 on success.
417 *
418 */
419
420static int fb_try_mode(struct fb_var_screeninfo *var, struct fb_info *info,
421 const struct fb_videomode *mode, unsigned int bpp)
422{
423 int err = 0;
424
425 DPRINTK("Trying mode %s %dx%d-%d@%d\n", mode->name ? mode->name : "noname",
426 mode->xres, mode->yres, bpp, mode->refresh);
427 var->xres = mode->xres;
428 var->yres = mode->yres;
429 var->xres_virtual = mode->xres;
430 var->yres_virtual = mode->yres;
431 var->xoffset = 0;
432 var->yoffset = 0;
433 var->bits_per_pixel = bpp;
434 var->activate |= FB_ACTIVATE_TEST;
435 var->pixclock = mode->pixclock;
436 var->left_margin = mode->left_margin;
437 var->right_margin = mode->right_margin;
438 var->upper_margin = mode->upper_margin;
439 var->lower_margin = mode->lower_margin;
440 var->hsync_len = mode->hsync_len;
441 var->vsync_len = mode->vsync_len;
442 var->sync = mode->sync;
443 var->vmode = mode->vmode;
444 if (info->fbops->fb_check_var)
445 err = info->fbops->fb_check_var(var, info);
446 var->activate &= ~FB_ACTIVATE_TEST;
447 return err;
448}
449
450/**
451 * fb_find_mode - finds a valid video mode
452 * @var: frame buffer user defined part of display
453 * @info: frame buffer info structure
454 * @mode_option: string video mode to find
455 * @db: video mode database
456 * @dbsize: size of @db
457 * @default_mode: default video mode to fall back to
458 * @default_bpp: default color depth in bits per pixel
459 *
460 * Finds a suitable video mode, starting with the specified mode
461 * in @mode_option with fallback to @default_mode. If
462 * @default_mode fails, all modes in the video mode database will
463 * be tried.
464 *
465 * Valid mode specifiers for @mode_option:
466 *
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -0700467 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m] or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * <name>[-<bpp>][@<refresh>]
469 *
470 * with <xres>, <yres>, <bpp> and <refresh> decimal numbers and
471 * <name> a string.
472 *
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -0700473 * If 'M' is present after yres (and before refresh/bpp if present),
474 * the function will compute the timings using VESA(tm) Coordinated
475 * Video Timings (CVT). If 'R' is present after 'M', will compute with
476 * reduced blanking (for flatpanels). If 'i' is present, compute
477 * interlaced mode. If 'm' is present, add margins equal to 1.8%
478 * of xres rounded down to 8 pixels, and 1.8% of yres. The char
479 * 'i' and 'm' must be after 'M' and 'R'. Example:
480 *
481 * 1024x768MR-8@60m - Reduced blank with margins at 60Hz.
482 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * NOTE: The passed struct @var is _not_ cleared! This allows you
484 * to supply values for e.g. the grayscale and accel_flags fields.
485 *
486 * Returns zero for failure, 1 if using specified @mode_option,
487 * 2 if using specified @mode_option with an ignored refresh rate,
488 * 3 if default mode is used, 4 if fall back to any valid mode.
489 *
490 */
491
492int fb_find_mode(struct fb_var_screeninfo *var,
493 struct fb_info *info, const char *mode_option,
494 const struct fb_videomode *db, unsigned int dbsize,
495 const struct fb_videomode *default_mode,
496 unsigned int default_bpp)
497{
498 int i;
499
500 /* Set up defaults */
501 if (!db) {
502 db = modedb;
Tobias Klauserd1ae4182006-03-27 01:17:39 -0800503 dbsize = ARRAY_SIZE(modedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Jordan Crousec25623f2006-12-08 02:40:21 -0800505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!default_mode)
Jordan Crousec25623f2006-12-08 02:40:21 -0800507 default_mode = &db[0];
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!default_bpp)
510 default_bpp = 8;
511
512 /* Did the user specify a video mode? */
Geert Uytterhoevenc40eea92007-10-18 03:04:36 -0700513 if (!mode_option)
514 mode_option = fb_mode_option;
515 if (mode_option) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 const char *name = mode_option;
517 unsigned int namelen = strlen(name);
518 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
519 unsigned int xres = 0, yres = 0, bpp = default_bpp, refresh = 0;
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -0700520 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
Michal Januszewski6b745b62008-04-28 02:14:48 -0700521 u32 best, diff, tdiff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 for (i = namelen-1; i >= 0; i--) {
524 switch (name[i]) {
525 case '@':
526 namelen = i;
527 if (!refresh_specified && !bpp_specified &&
528 !yres_specified) {
Andy Shevchenko2cb96f82009-12-15 16:46:24 -0800529 refresh = simple_strtol(&name[i+1], NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 refresh_specified = 1;
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -0700531 if (cvt || rb)
532 cvt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else
534 goto done;
535 break;
536 case '-':
537 namelen = i;
538 if (!bpp_specified && !yres_specified) {
Andy Shevchenko2cb96f82009-12-15 16:46:24 -0800539 bpp = simple_strtol(&name[i+1], NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 bpp_specified = 1;
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -0700541 if (cvt || rb)
542 cvt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } else
544 goto done;
545 break;
546 case 'x':
547 if (!yres_specified) {
Andy Shevchenko2cb96f82009-12-15 16:46:24 -0800548 yres = simple_strtol(&name[i+1], NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 yres_specified = 1;
550 } else
551 goto done;
552 break;
Robert P. J. Day88b229c2007-05-08 00:38:27 -0700553 case '0' ... '9':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -0700555 case 'M':
556 if (!yres_specified)
557 cvt = 1;
558 break;
559 case 'R':
560 if (!cvt)
561 rb = 1;
562 break;
563 case 'm':
564 if (!cvt)
565 margins = 1;
566 break;
567 case 'i':
568 if (!cvt)
569 interlace = 1;
570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 default:
572 goto done;
573 }
574 }
575 if (i < 0 && yres_specified) {
Andy Shevchenko2cb96f82009-12-15 16:46:24 -0800576 xres = simple_strtol(name, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 res_specified = 1;
578 }
579done:
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -0700580 if (cvt) {
581 struct fb_videomode cvt_mode;
582 int ret;
583
584 DPRINTK("CVT mode %dx%d@%dHz%s%s%s\n", xres, yres,
585 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
586 "", (margins) ? " with margins" : "", (interlace) ?
587 " interlaced" : "");
588
Krzysztof Helte2d4eca2008-06-05 22:45:56 -0700589 memset(&cvt_mode, 0, sizeof(cvt_mode));
Antonino A. Daplas96fe6a22005-09-09 13:09:58 -0700590 cvt_mode.xres = xres;
591 cvt_mode.yres = yres;
592 cvt_mode.refresh = (refresh) ? refresh : 60;
593
594 if (interlace)
595 cvt_mode.vmode |= FB_VMODE_INTERLACED;
596 else
597 cvt_mode.vmode &= ~FB_VMODE_INTERLACED;
598
599 ret = fb_find_mode_cvt(&cvt_mode, margins, rb);
600
601 if (!ret && !fb_try_mode(var, info, &cvt_mode, bpp)) {
602 DPRINTK("modedb CVT: CVT mode ok\n");
603 return 1;
604 }
605
606 DPRINTK("CVT mode invalid, getting mode from database\n");
607 }
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 DPRINTK("Trying specified video mode%s %ix%i\n",
610 refresh_specified ? "" : " (ignoring refresh rate)", xres, yres);
611
Michal Januszewski1c5dd172007-10-16 01:29:19 -0700612 if (!refresh_specified) {
613 /*
614 * If the caller has provided a custom mode database and a
615 * valid monspecs structure, we look for the mode with the
616 * highest refresh rate. Otherwise we play it safe it and
617 * try to find a mode with a refresh rate closest to the
618 * standard 60 Hz.
619 */
620 if (db != modedb &&
621 info->monspecs.vfmin && info->monspecs.vfmax &&
622 info->monspecs.hfmin && info->monspecs.hfmax &&
623 info->monspecs.dclkmax) {
624 refresh = 1000;
625 } else {
626 refresh = 60;
627 }
628 }
629
630 diff = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 best = -1;
632 for (i = 0; i < dbsize; i++) {
Michal Januszewski1c5dd172007-10-16 01:29:19 -0700633 if ((name_matches(db[i], name, namelen) ||
634 (res_specified && res_matches(db[i], xres, yres))) &&
635 !fb_try_mode(var, info, &db[i], bpp)) {
636 if (refresh_specified && db[i].refresh == refresh) {
637 return 1;
638 } else {
639 if (abs(db[i].refresh - refresh) < diff) {
640 diff = abs(db[i].refresh - refresh);
641 best = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 }
644 }
645 }
646 if (best != -1) {
647 fb_try_mode(var, info, &db[best], bpp);
Michal Januszewski1c5dd172007-10-16 01:29:19 -0700648 return (refresh_specified) ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
Michal Januszewski6b745b62008-04-28 02:14:48 -0700651 diff = 2 * (xres + yres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 best = -1;
653 DPRINTK("Trying best-fit modes\n");
654 for (i = 0; i < dbsize; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 DPRINTK("Trying %ix%i\n", db[i].xres, db[i].yres);
656 if (!fb_try_mode(var, info, &db[i], bpp)) {
Michal Januszewski6b745b62008-04-28 02:14:48 -0700657 tdiff = abs(db[i].xres - xres) +
658 abs(db[i].yres - yres);
659
660 /*
661 * Penalize modes with resolutions smaller
662 * than requested.
663 */
664 if (xres > db[i].xres || yres > db[i].yres)
665 tdiff += xres + yres;
666
667 if (diff > tdiff) {
668 diff = tdiff;
669 best = i;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 if (best != -1) {
674 fb_try_mode(var, info, &db[best], bpp);
675 return 5;
676 }
677 }
678
679 DPRINTK("Trying default video mode\n");
680 if (!fb_try_mode(var, info, default_mode, default_bpp))
681 return 3;
682
683 DPRINTK("Trying all modes\n");
684 for (i = 0; i < dbsize; i++)
685 if (!fb_try_mode(var, info, &db[i], default_bpp))
686 return 4;
687
688 DPRINTK("No valid mode found\n");
689 return 0;
690}
691
692/**
693 * fb_var_to_videomode - convert fb_var_screeninfo to fb_videomode
694 * @mode: pointer to struct fb_videomode
695 * @var: pointer to struct fb_var_screeninfo
696 */
697void fb_var_to_videomode(struct fb_videomode *mode,
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800698 const struct fb_var_screeninfo *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 u32 pixclock, hfreq, htotal, vtotal;
701
702 mode->name = NULL;
703 mode->xres = var->xres;
704 mode->yres = var->yres;
705 mode->pixclock = var->pixclock;
706 mode->hsync_len = var->hsync_len;
707 mode->vsync_len = var->vsync_len;
708 mode->left_margin = var->left_margin;
709 mode->right_margin = var->right_margin;
710 mode->upper_margin = var->upper_margin;
711 mode->lower_margin = var->lower_margin;
712 mode->sync = var->sync;
713 mode->vmode = var->vmode & FB_VMODE_MASK;
714 mode->flag = FB_MODE_IS_FROM_VAR;
Michal Januszewski8fb65672005-11-07 01:00:47 -0800715 mode->refresh = 0;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (!var->pixclock)
718 return;
719
720 pixclock = PICOS2KHZ(var->pixclock) * 1000;
721
722 htotal = var->xres + var->right_margin + var->hsync_len +
723 var->left_margin;
724 vtotal = var->yres + var->lower_margin + var->vsync_len +
725 var->upper_margin;
726
727 if (var->vmode & FB_VMODE_INTERLACED)
728 vtotal /= 2;
729 if (var->vmode & FB_VMODE_DOUBLE)
730 vtotal *= 2;
731
732 hfreq = pixclock/htotal;
733 mode->refresh = hfreq/vtotal;
734}
735
736/**
737 * fb_videomode_to_var - convert fb_videomode to fb_var_screeninfo
738 * @var: pointer to struct fb_var_screeninfo
739 * @mode: pointer to struct fb_videomode
740 */
741void fb_videomode_to_var(struct fb_var_screeninfo *var,
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800742 const struct fb_videomode *mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 var->xres = mode->xres;
745 var->yres = mode->yres;
Geert Uytterhoevendcfe2662007-02-12 00:55:21 -0800746 var->xres_virtual = mode->xres;
747 var->yres_virtual = mode->yres;
748 var->xoffset = 0;
749 var->yoffset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 var->pixclock = mode->pixclock;
751 var->left_margin = mode->left_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 var->right_margin = mode->right_margin;
753 var->upper_margin = mode->upper_margin;
754 var->lower_margin = mode->lower_margin;
Geert Uytterhoevendcfe2662007-02-12 00:55:21 -0800755 var->hsync_len = mode->hsync_len;
756 var->vsync_len = mode->vsync_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 var->sync = mode->sync;
758 var->vmode = mode->vmode & FB_VMODE_MASK;
759}
760
761/**
762 * fb_mode_is_equal - compare 2 videomodes
763 * @mode1: first videomode
764 * @mode2: second videomode
765 *
766 * RETURNS:
767 * 1 if equal, 0 if not
768 */
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800769int fb_mode_is_equal(const struct fb_videomode *mode1,
770 const struct fb_videomode *mode2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 return (mode1->xres == mode2->xres &&
773 mode1->yres == mode2->yres &&
774 mode1->pixclock == mode2->pixclock &&
775 mode1->hsync_len == mode2->hsync_len &&
776 mode1->vsync_len == mode2->vsync_len &&
777 mode1->left_margin == mode2->left_margin &&
778 mode1->right_margin == mode2->right_margin &&
779 mode1->upper_margin == mode2->upper_margin &&
780 mode1->lower_margin == mode2->lower_margin &&
781 mode1->sync == mode2->sync &&
782 mode1->vmode == mode2->vmode);
783}
784
785/**
786 * fb_find_best_mode - find best matching videomode
787 * @var: pointer to struct fb_var_screeninfo
788 * @head: pointer to struct list_head of modelist
789 *
790 * RETURNS:
791 * struct fb_videomode, NULL if none found
792 *
793 * IMPORTANT:
794 * This function assumes that all modelist entries in
795 * info->modelist are valid.
796 *
797 * NOTES:
798 * Finds best matching videomode which has an equal or greater dimension than
799 * var->xres and var->yres. If more than 1 videomode is found, will return
800 * the videomode with the highest refresh rate
801 */
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800802const struct fb_videomode *fb_find_best_mode(const struct fb_var_screeninfo *var,
803 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct list_head *pos;
806 struct fb_modelist *modelist;
807 struct fb_videomode *mode, *best = NULL;
808 u32 diff = -1;
809
810 list_for_each(pos, head) {
811 u32 d;
812
813 modelist = list_entry(pos, struct fb_modelist, list);
814 mode = &modelist->mode;
815
816 if (mode->xres >= var->xres && mode->yres >= var->yres) {
817 d = (mode->xres - var->xres) +
818 (mode->yres - var->yres);
819 if (diff > d) {
820 diff = d;
821 best = mode;
Antonino A. Daplas17cc62812006-06-26 00:26:44 -0700822 } else if (diff == d && best &&
823 mode->refresh > best->refresh)
824 best = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826 }
827 return best;
828}
829
830/**
Michal Januszewski8fb65672005-11-07 01:00:47 -0800831 * fb_find_nearest_mode - find closest videomode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 *
Michal Januszewski8fb65672005-11-07 01:00:47 -0800833 * @mode: pointer to struct fb_videomode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * @head: pointer to modelist
835 *
836 * Finds best matching videomode, smaller or greater in dimension.
837 * If more than 1 videomode is found, will return the videomode with
Michal Januszewski8fb65672005-11-07 01:00:47 -0800838 * the closest refresh rate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800840const struct fb_videomode *fb_find_nearest_mode(const struct fb_videomode *mode,
841 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct list_head *pos;
844 struct fb_modelist *modelist;
Michal Januszewski8fb65672005-11-07 01:00:47 -0800845 struct fb_videomode *cmode, *best = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 u32 diff = -1, diff_refresh = -1;
847
848 list_for_each(pos, head) {
849 u32 d;
850
851 modelist = list_entry(pos, struct fb_modelist, list);
Michal Januszewski8fb65672005-11-07 01:00:47 -0800852 cmode = &modelist->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Michal Januszewski8fb65672005-11-07 01:00:47 -0800854 d = abs(cmode->xres - mode->xres) +
855 abs(cmode->yres - mode->yres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (diff > d) {
857 diff = d;
Andrew Kepharte504b842010-12-13 09:46:34 -0600858 diff_refresh = abs(cmode->refresh - mode->refresh);
Michal Januszewski8fb65672005-11-07 01:00:47 -0800859 best = cmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 } else if (diff == d) {
Michal Januszewski8fb65672005-11-07 01:00:47 -0800861 d = abs(cmode->refresh - mode->refresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (diff_refresh > d) {
863 diff_refresh = d;
Michal Januszewski8fb65672005-11-07 01:00:47 -0800864 best = cmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 }
867 }
868
869 return best;
870}
871
872/**
873 * fb_match_mode - find a videomode which exactly matches the timings in var
874 * @var: pointer to struct fb_var_screeninfo
875 * @head: pointer to struct list_head of modelist
876 *
877 * RETURNS:
878 * struct fb_videomode, NULL if none found
879 */
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800880const struct fb_videomode *fb_match_mode(const struct fb_var_screeninfo *var,
881 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 struct list_head *pos;
884 struct fb_modelist *modelist;
885 struct fb_videomode *m, mode;
886
887 fb_var_to_videomode(&mode, var);
888 list_for_each(pos, head) {
889 modelist = list_entry(pos, struct fb_modelist, list);
890 m = &modelist->mode;
891 if (fb_mode_is_equal(m, &mode))
892 return m;
893 }
894 return NULL;
895}
896
897/**
Ben Hutchingsa8b19252010-03-10 15:21:49 -0800898 * fb_add_videomode - adds videomode entry to modelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * @mode: videomode to add
900 * @head: struct list_head of modelist
901 *
902 * NOTES:
903 * Will only add unmatched mode entries
904 */
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800905int fb_add_videomode(const struct fb_videomode *mode, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct list_head *pos;
908 struct fb_modelist *modelist;
909 struct fb_videomode *m;
910 int found = 0;
911
912 list_for_each(pos, head) {
913 modelist = list_entry(pos, struct fb_modelist, list);
914 m = &modelist->mode;
915 if (fb_mode_is_equal(m, mode)) {
916 found = 1;
917 break;
918 }
919 }
920 if (!found) {
921 modelist = kmalloc(sizeof(struct fb_modelist),
922 GFP_KERNEL);
923
924 if (!modelist)
925 return -ENOMEM;
926 modelist->mode = *mode;
927 list_add(&modelist->list, head);
928 }
929 return 0;
930}
931
932/**
Ben Hutchingsa8b19252010-03-10 15:21:49 -0800933 * fb_delete_videomode - removed videomode entry from modelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * @mode: videomode to remove
935 * @head: struct list_head of modelist
936 *
937 * NOTES:
938 * Will remove all matching mode entries
939 */
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800940void fb_delete_videomode(const struct fb_videomode *mode,
941 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct list_head *pos, *n;
944 struct fb_modelist *modelist;
945 struct fb_videomode *m;
946
947 list_for_each_safe(pos, n, head) {
948 modelist = list_entry(pos, struct fb_modelist, list);
949 m = &modelist->mode;
950 if (fb_mode_is_equal(m, mode)) {
951 list_del(pos);
952 kfree(pos);
953 }
954 }
955}
956
957/**
Ben Hutchingsa8b19252010-03-10 15:21:49 -0800958 * fb_destroy_modelist - destroy modelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * @head: struct list_head of modelist
960 */
961void fb_destroy_modelist(struct list_head *head)
962{
963 struct list_head *pos, *n;
964
965 list_for_each_safe(pos, n, head) {
966 list_del(pos);
967 kfree(pos);
968 }
969}
Michal Januszewski9953d232007-10-16 01:28:23 -0700970EXPORT_SYMBOL_GPL(fb_destroy_modelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972/**
Ben Hutchingsa8b19252010-03-10 15:21:49 -0800973 * fb_videomode_to_modelist - convert mode array to mode list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 * @modedb: array of struct fb_videomode
975 * @num: number of entries in array
976 * @head: struct list_head of modelist
977 */
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800978void fb_videomode_to_modelist(const struct fb_videomode *modedb, int num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 struct list_head *head)
980{
981 int i;
982
983 INIT_LIST_HEAD(head);
984
985 for (i = 0; i < num; i++) {
986 if (fb_add_videomode(&modedb[i], head))
987 return;
988 }
989}
990
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800991const struct fb_videomode *fb_find_best_display(const struct fb_monspecs *specs,
992 struct list_head *head)
Antonino A. Daplas003cfc02005-11-07 01:00:54 -0800993{
994 struct list_head *pos;
995 struct fb_modelist *modelist;
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800996 const struct fb_videomode *m, *m1 = NULL, *md = NULL, *best = NULL;
Antonino A. Daplas003cfc02005-11-07 01:00:54 -0800997 int first = 0;
998
999 if (!head->prev || !head->next || list_empty(head))
1000 goto finished;
1001
1002 /* get the first detailed mode and the very first mode */
1003 list_for_each(pos, head) {
1004 modelist = list_entry(pos, struct fb_modelist, list);
1005 m = &modelist->mode;
1006
1007 if (!first) {
1008 m1 = m;
1009 first = 1;
1010 }
1011
1012 if (m->flag & FB_MODE_IS_FIRST) {
1013 md = m;
1014 break;
1015 }
1016 }
1017
1018 /* first detailed timing is preferred */
1019 if (specs->misc & FB_MISC_1ST_DETAIL) {
1020 best = md;
1021 goto finished;
1022 }
1023
1024 /* find best mode based on display width and height */
1025 if (specs->max_x && specs->max_y) {
1026 struct fb_var_screeninfo var;
1027
1028 memset(&var, 0, sizeof(struct fb_var_screeninfo));
1029 var.xres = (specs->max_x * 7200)/254;
1030 var.yres = (specs->max_y * 7200)/254;
1031 m = fb_find_best_mode(&var, head);
1032 if (m) {
1033 best = m;
1034 goto finished;
1035 }
1036 }
1037
1038 /* use first detailed mode */
1039 if (md) {
1040 best = md;
1041 goto finished;
1042 }
1043
1044 /* last resort, use the very first mode */
1045 best = m1;
1046finished:
1047 return best;
1048}
1049EXPORT_SYMBOL(fb_find_best_display);
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051EXPORT_SYMBOL(fb_videomode_to_var);
1052EXPORT_SYMBOL(fb_var_to_videomode);
1053EXPORT_SYMBOL(fb_mode_is_equal);
1054EXPORT_SYMBOL(fb_add_videomode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055EXPORT_SYMBOL(fb_match_mode);
1056EXPORT_SYMBOL(fb_find_best_mode);
1057EXPORT_SYMBOL(fb_find_nearest_mode);
1058EXPORT_SYMBOL(fb_videomode_to_modelist);
1059EXPORT_SYMBOL(fb_find_mode);