xref: /linux/drivers/video/fbdev/core/modedb.c (revision 260f6f4fda93c8485c8037865c941b42b9cba5d2)
1 /*
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/export.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/fb.h>
18 #include <linux/kernel.h>
19 
20 #undef DEBUG
21 
22 #define name_matches(v, s, l) \
23     ((v).name && !strncmp((s), (v).name, (l)) && strlen((v).name) == (l))
24 #define res_matches(v, x, y) \
25     ((v).xres == (x) && (v).yres == (y))
26 
27 #ifdef DEBUG
28 #define DPRINTK(fmt, args...)	printk("modedb %s: " fmt, __func__ , ## args)
29 #else
30 #define DPRINTK(fmt, args...)
31 #endif
32 
33 /*
34  *  Standard video mode definitions (taken from XFree86)
35  */
36 
37 static const struct fb_videomode modedb[] = {
38 
39 	/* 640x400 @ 70 Hz, 31.5 kHz hsync */
40 	{ NULL, 70, 640, 400, 39721, 40, 24, 39, 9, 96, 2, 0,
41 		FB_VMODE_NONINTERLACED },
42 
43 	/* 640x480 @ 60 Hz, 31.5 kHz hsync */
44 	{ NULL, 60, 640, 480, 39721, 40, 24, 32, 11, 96, 2,	0,
45 		FB_VMODE_NONINTERLACED },
46 
47 	/* 800x600 @ 56 Hz, 35.15 kHz hsync */
48 	{ NULL, 56, 800, 600, 27777, 128, 24, 22, 1, 72, 2,	0,
49 		FB_VMODE_NONINTERLACED },
50 
51 	/* 1024x768 @ 87 Hz interlaced, 35.5 kHz hsync */
52 	{ NULL, 87, 1024, 768, 22271, 56, 24, 33, 8, 160, 8, 0,
53 		FB_VMODE_INTERLACED },
54 
55 	/* 640x400 @ 85 Hz, 37.86 kHz hsync */
56 	{ NULL, 85, 640, 400, 31746, 96, 32, 41, 1, 64, 3,
57 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED },
58 
59 	/* 640x480 @ 72 Hz, 36.5 kHz hsync */
60 	{ NULL, 72, 640, 480, 31746, 144, 40, 30, 8, 40, 3, 0,
61 		FB_VMODE_NONINTERLACED },
62 
63 	/* 640x480 @ 75 Hz, 37.50 kHz hsync */
64 	{ NULL, 75, 640, 480, 31746, 120, 16, 16, 1, 64, 3,	0,
65 		FB_VMODE_NONINTERLACED },
66 
67 	/* 800x600 @ 60 Hz, 37.8 kHz hsync */
68 	{ NULL, 60, 800, 600, 25000, 88, 40, 23, 1, 128, 4,
69 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
70 		FB_VMODE_NONINTERLACED },
71 
72 	/* 640x480 @ 85 Hz, 43.27 kHz hsync */
73 	{ NULL, 85, 640, 480, 27777, 80, 56, 25, 1, 56, 3, 0,
74 		FB_VMODE_NONINTERLACED },
75 
76 	/* 1152x864 @ 89 Hz interlaced, 44 kHz hsync */
77 	{ NULL, 89, 1152, 864, 15384, 96, 16, 110, 1, 216, 10, 0,
78 		FB_VMODE_INTERLACED },
79 	/* 800x600 @ 72 Hz, 48.0 kHz hsync */
80 	{ NULL, 72, 800, 600, 20000, 64, 56, 23, 37, 120, 6,
81 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
82 		FB_VMODE_NONINTERLACED },
83 
84 	/* 1024x768 @ 60 Hz, 48.4 kHz hsync */
85 	{ NULL, 60, 1024, 768, 15384, 168, 8, 29, 3, 144, 6, 0,
86 		FB_VMODE_NONINTERLACED },
87 
88 	/* 640x480 @ 100 Hz, 53.01 kHz hsync */
89 	{ NULL, 100, 640, 480, 21834, 96, 32, 36, 8, 96, 6,	0,
90 		FB_VMODE_NONINTERLACED },
91 
92 	/* 1152x864 @ 60 Hz, 53.5 kHz hsync */
93 	{ NULL, 60, 1152, 864, 11123, 208, 64, 16, 4, 256, 8, 0,
94 		FB_VMODE_NONINTERLACED },
95 
96 	/* 800x600 @ 85 Hz, 55.84 kHz hsync */
97 	{ NULL, 85, 800, 600, 16460, 160, 64, 36, 16, 64, 5, 0,
98 		FB_VMODE_NONINTERLACED },
99 
100 	/* 1024x768 @ 70 Hz, 56.5 kHz hsync */
101 	{ NULL, 70, 1024, 768, 13333, 144, 24, 29, 3, 136, 6, 0,
102 		FB_VMODE_NONINTERLACED },
103 
104 	/* 1280x1024 @ 87 Hz interlaced, 51 kHz hsync */
105 	{ NULL, 87, 1280, 1024, 12500, 56, 16, 128, 1, 216, 12,	0,
106 		FB_VMODE_INTERLACED },
107 
108 	/* 800x600 @ 100 Hz, 64.02 kHz hsync */
109 	{ NULL, 100, 800, 600, 14357, 160, 64, 30, 4, 64, 6, 0,
110 		FB_VMODE_NONINTERLACED },
111 
112 	/* 1024x768 @ 76 Hz, 62.5 kHz hsync */
113 	{ NULL, 76, 1024, 768, 11764, 208, 8, 36, 16, 120, 3, 0,
114 		FB_VMODE_NONINTERLACED },
115 
116 	/* 1152x864 @ 70 Hz, 62.4 kHz hsync */
117 	{ NULL, 70, 1152, 864, 10869, 106, 56, 20, 1, 160, 10, 0,
118 		FB_VMODE_NONINTERLACED },
119 
120 	/* 1280x1024 @ 61 Hz, 64.2 kHz hsync */
121 	{ NULL, 61, 1280, 1024, 9090, 200, 48, 26, 1, 184, 3, 0,
122 		FB_VMODE_NONINTERLACED },
123 
124 	/* 1400x1050 @ 60Hz, 63.9 kHz hsync */
125 	{ NULL, 60, 1400, 1050, 9259, 136, 40, 13, 1, 112, 3, 0,
126 		FB_VMODE_NONINTERLACED },
127 
128 	/* 1400x1050 @ 75,107 Hz, 82,392 kHz +hsync +vsync*/
129 	{ NULL, 75, 1400, 1050, 7190, 120, 56, 23, 10, 112, 13,
130 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
131 		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,
136 		FB_VMODE_NONINTERLACED },
137 
138 	/* 1024x768 @ 85 Hz, 70.24 kHz hsync */
139 	{ NULL, 85, 1024, 768, 10111, 192, 32, 34, 14, 160, 6, 0,
140 		FB_VMODE_NONINTERLACED },
141 
142 	/* 1152x864 @ 78 Hz, 70.8 kHz hsync */
143 	{ NULL, 78, 1152, 864, 9090, 228, 88, 32, 0, 84, 12, 0,
144 		FB_VMODE_NONINTERLACED },
145 
146 	/* 1280x1024 @ 70 Hz, 74.59 kHz hsync */
147 	{ NULL, 70, 1280, 1024, 7905, 224, 32, 28, 8, 160, 8, 0,
148 		FB_VMODE_NONINTERLACED },
149 
150 	/* 1600x1200 @ 60Hz, 75.00 kHz hsync */
151 	{ NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
152 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
153 		FB_VMODE_NONINTERLACED },
154 
155 	/* 1152x864 @ 84 Hz, 76.0 kHz hsync */
156 	{ NULL, 84, 1152, 864, 7407, 184, 312, 32, 0, 128, 12, 0,
157 		FB_VMODE_NONINTERLACED },
158 
159 	/* 1280x1024 @ 74 Hz, 78.85 kHz hsync */
160 	{ NULL, 74, 1280, 1024, 7407, 256, 32, 34, 3, 144, 3, 0,
161 		FB_VMODE_NONINTERLACED },
162 
163 	/* 1024x768 @ 100Hz, 80.21 kHz hsync */
164 	{ NULL, 100, 1024, 768, 8658, 192, 32, 21, 3, 192, 10, 0,
165 		FB_VMODE_NONINTERLACED },
166 
167 	/* 1280x1024 @ 76 Hz, 81.13 kHz hsync */
168 	{ NULL, 76, 1280, 1024, 7407, 248, 32, 34, 3, 104, 3, 0,
169 		FB_VMODE_NONINTERLACED },
170 
171 	/* 1600x1200 @ 70 Hz, 87.50 kHz hsync */
172 	{ NULL, 70, 1600, 1200, 5291, 304, 64, 46, 1, 192, 3, 0,
173 		FB_VMODE_NONINTERLACED },
174 
175 	/* 1152x864 @ 100 Hz, 89.62 kHz hsync */
176 	{ NULL, 100, 1152, 864, 7264, 224, 32, 17, 2, 128, 19, 0,
177 		FB_VMODE_NONINTERLACED },
178 
179 	/* 1280x1024 @ 85 Hz, 91.15 kHz hsync */
180 	{ NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
181 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
182 		FB_VMODE_NONINTERLACED },
183 
184 	/* 1600x1200 @ 75 Hz, 93.75 kHz hsync */
185 	{ NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
186 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
187 		FB_VMODE_NONINTERLACED },
188 
189 	/* 1680x1050 @ 60 Hz, 65.191 kHz hsync */
190 	{ NULL, 60, 1680, 1050, 6848, 280, 104, 30, 3, 176, 6,
191 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
192 		FB_VMODE_NONINTERLACED },
193 
194 	/* 1600x1200 @ 85 Hz, 105.77 kHz hsync */
195 	{ NULL, 85, 1600, 1200, 4545, 272, 16, 37, 4, 192, 3,
196 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
197 		FB_VMODE_NONINTERLACED },
198 
199 	/* 1280x1024 @ 100 Hz, 107.16 kHz hsync */
200 	{ NULL, 100, 1280, 1024, 5502, 256, 32, 26, 7, 128, 15, 0,
201 		FB_VMODE_NONINTERLACED },
202 
203 	/* 1800x1440 @ 64Hz, 96.15 kHz hsync  */
204 	{ NULL, 64, 1800, 1440, 4347, 304, 96, 46, 1, 192, 3,
205 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
206 		FB_VMODE_NONINTERLACED },
207 
208 	/* 1800x1440 @ 70Hz, 104.52 kHz hsync  */
209 	{ NULL, 70, 1800, 1440, 4000, 304, 96, 46, 1, 192, 3,
210 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
211 		FB_VMODE_NONINTERLACED },
212 
213 	/* 512x384 @ 78 Hz, 31.50 kHz hsync */
214 	{ NULL, 78, 512, 384, 49603, 48, 16, 16, 1, 64, 3, 0,
215 		FB_VMODE_NONINTERLACED },
216 
217 	/* 512x384 @ 85 Hz, 34.38 kHz hsync */
218 	{ NULL, 85, 512, 384, 45454, 48, 16, 16, 1, 64, 3, 0,
219 		FB_VMODE_NONINTERLACED },
220 
221 	/* 320x200 @ 70 Hz, 31.5 kHz hsync, 8:5 aspect ratio */
222 	{ NULL, 70, 320, 200, 79440, 16, 16, 20, 4, 48, 1, 0,
223 		FB_VMODE_DOUBLE },
224 
225 	/* 320x240 @ 60 Hz, 31.5 kHz hsync, 4:3 aspect ratio */
226 	{ NULL, 60, 320, 240, 79440, 16, 16, 16, 5, 48, 1, 0,
227 		FB_VMODE_DOUBLE },
228 
229 	/* 320x240 @ 72 Hz, 36.5 kHz hsync */
230 	{ NULL, 72, 320, 240, 63492, 16, 16, 16, 4, 48, 2, 0,
231 		FB_VMODE_DOUBLE },
232 
233 	/* 400x300 @ 56 Hz, 35.2 kHz hsync, 4:3 aspect ratio */
234 	{ NULL, 56, 400, 300, 55555, 64, 16, 10, 1, 32, 1, 0,
235 		FB_VMODE_DOUBLE },
236 
237 	/* 400x300 @ 60 Hz, 37.8 kHz hsync */
238 	{ NULL, 60, 400, 300, 50000, 48, 16, 11, 1, 64, 2, 0,
239 		FB_VMODE_DOUBLE },
240 
241 	/* 400x300 @ 72 Hz, 48.0 kHz hsync */
242 	{ NULL, 72, 400, 300, 40000, 32, 24, 11, 19, 64, 3,	0,
243 		FB_VMODE_DOUBLE },
244 
245 	/* 480x300 @ 56 Hz, 35.2 kHz hsync, 8:5 aspect ratio */
246 	{ NULL, 56, 480, 300, 46176, 80, 16, 10, 1, 40, 1, 0,
247 		FB_VMODE_DOUBLE },
248 
249 	/* 480x300 @ 60 Hz, 37.8 kHz hsync */
250 	{ NULL, 60, 480, 300, 41858, 56, 16, 11, 1, 80, 2, 0,
251 		FB_VMODE_DOUBLE },
252 
253 	/* 480x300 @ 63 Hz, 39.6 kHz hsync */
254 	{ NULL, 63, 480, 300, 40000, 56, 16, 11, 1, 80, 2, 0,
255 		FB_VMODE_DOUBLE },
256 
257 	/* 480x300 @ 72 Hz, 48.0 kHz hsync */
258 	{ NULL, 72, 480, 300, 33386, 40, 24, 11, 19, 80, 3, 0,
259 		FB_VMODE_DOUBLE },
260 
261 	/* 1920x1080 @ 60 Hz, 67.3 kHz hsync */
262 	{ NULL, 60, 1920, 1080, 6734, 148, 88, 36, 4, 44, 5, 0,
263 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
264 		FB_VMODE_NONINTERLACED },
265 
266 	/* 1920x1200 @ 60 Hz, 74.5 Khz hsync */
267 	{ NULL, 60, 1920, 1200, 5177, 128, 336, 1, 38, 208, 3,
268 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
269 		FB_VMODE_NONINTERLACED },
270 
271 	/* 1152x768, 60 Hz, PowerBook G4 Titanium I and II */
272 	{ NULL, 60, 1152, 768, 14047, 158, 26, 29, 3, 136, 6,
273 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
274 		FB_VMODE_NONINTERLACED },
275 
276 	/* 1366x768, 60 Hz, 47.403 kHz hsync, WXGA 16:9 aspect ratio */
277 	{ NULL, 60, 1366, 768, 13806, 120, 10, 14, 3, 32, 5, 0,
278 		FB_VMODE_NONINTERLACED },
279 
280 	/* 1280x800, 60 Hz, 47.403 kHz hsync, WXGA 16:10 aspect ratio */
281 	{ NULL, 60, 1280, 800, 12048, 200, 64, 24, 1, 136, 3, 0,
282 		FB_VMODE_NONINTERLACED },
283 
284 	/* 720x576i @ 50 Hz, 15.625 kHz hsync (PAL RGB) */
285 	{ NULL, 50, 720, 576, 74074, 64, 16, 39, 5, 64, 5, 0,
286 		FB_VMODE_INTERLACED },
287 
288 	/* 800x520i @ 50 Hz, 15.625 kHz hsync (PAL RGB) */
289 	{ NULL, 50, 800, 520, 58823, 144, 64, 72, 28, 80, 5, 0,
290 		FB_VMODE_INTERLACED },
291 
292 	/* 864x480 @ 60 Hz, 35.15 kHz hsync */
293 	{ NULL, 60, 864, 480, 27777, 1, 1, 1, 1, 0, 0,
294 		0, FB_VMODE_NONINTERLACED },
295 };
296 
297 #ifdef CONFIG_FB_MODE_HELPERS
298 const struct fb_videomode vesa_modes[] = {
299 	/* 0 640x350-85 VESA */
300 	{ NULL, 85, 640, 350, 31746,  96, 32, 60, 32, 64, 3,
301 	  FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA},
302 	/* 1 640x400-85 VESA */
303 	{ NULL, 85, 640, 400, 31746,  96, 32, 41, 01, 64, 3,
304 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
305 	/* 2 720x400-85 VESA */
306 	{ NULL, 85, 721, 400, 28169, 108, 36, 42, 01, 72, 3,
307 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
308 	/* 3 640x480-60 VESA */
309 	{ NULL, 60, 640, 480, 39682,  48, 16, 33, 10, 96, 2,
310 	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
311 	/* 4 640x480-72 VESA */
312 	{ NULL, 72, 640, 480, 31746, 128, 24, 29, 9, 40, 2,
313 	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
314 	/* 5 640x480-75 VESA */
315 	{ NULL, 75, 640, 480, 31746, 120, 16, 16, 01, 64, 3,
316 	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
317 	/* 6 640x480-85 VESA */
318 	{ NULL, 85, 640, 480, 27777, 80, 56, 25, 01, 56, 3,
319 	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
320 	/* 7 800x600-56 VESA */
321 	{ NULL, 56, 800, 600, 27777, 128, 24, 22, 01, 72, 2,
322 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
323 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
324 	/* 8 800x600-60 VESA */
325 	{ NULL, 60, 800, 600, 25000, 88, 40, 23, 01, 128, 4,
326 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
327 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
328 	/* 9 800x600-72 VESA */
329 	{ NULL, 72, 800, 600, 20000, 64, 56, 23, 37, 120, 6,
330 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
331 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
332 	/* 10 800x600-75 VESA */
333 	{ NULL, 75, 800, 600, 20202, 160, 16, 21, 01, 80, 3,
334 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
335 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
336 	/* 11 800x600-85 VESA */
337 	{ NULL, 85, 800, 600, 17761, 152, 32, 27, 01, 64, 3,
338 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
339 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
340         /* 12 1024x768i-43 VESA */
341 	{ NULL, 43, 1024, 768, 22271, 56, 8, 41, 0, 176, 8,
342 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
343 	  FB_VMODE_INTERLACED, FB_MODE_IS_VESA },
344 	/* 13 1024x768-60 VESA */
345 	{ NULL, 60, 1024, 768, 15384, 160, 24, 29, 3, 136, 6,
346 	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
347 	/* 14 1024x768-70 VESA */
348 	{ NULL, 70, 1024, 768, 13333, 144, 24, 29, 3, 136, 6,
349 	  0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
350 	/* 15 1024x768-75 VESA */
351 	{ NULL, 75, 1024, 768, 12690, 176, 16, 28, 1, 96, 3,
352 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
353 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
354 	/* 16 1024x768-85 VESA */
355 	{ NULL, 85, 1024, 768, 10582, 208, 48, 36, 1, 96, 3,
356 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
357 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
358 	/* 17 1152x864-75 VESA */
359 	{ NULL, 75, 1152, 864, 9259, 256, 64, 32, 1, 128, 3,
360 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
361 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
362 	/* 18 1280x960-60 VESA */
363 	{ NULL, 60, 1280, 960, 9259, 312, 96, 36, 1, 112, 3,
364 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
365 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
366 	/* 19 1280x960-85 VESA */
367 	{ NULL, 85, 1280, 960, 6734, 224, 64, 47, 1, 160, 3,
368 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
369 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
370 	/* 20 1280x1024-60 VESA */
371 	{ NULL, 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
372 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
373 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
374 	/* 21 1280x1024-75 VESA */
375 	{ NULL, 75, 1280, 1024, 7407, 248, 16, 38, 1, 144, 3,
376 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
377 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
378 	/* 22 1280x1024-85 VESA */
379 	{ NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
380 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
381 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
382 	/* 23 1600x1200-60 VESA */
383 	{ NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
384 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
385 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
386 	/* 24 1600x1200-65 VESA */
387 	{ NULL, 65, 1600, 1200, 5698, 304,  64, 46, 1, 192, 3,
388 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
389 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
390 	/* 25 1600x1200-70 VESA */
391 	{ NULL, 70, 1600, 1200, 5291, 304, 64, 46, 1, 192, 3,
392 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
393 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
394 	/* 26 1600x1200-75 VESA */
395 	{ NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
396 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
397 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
398 	/* 27 1600x1200-85 VESA */
399 	{ NULL, 85, 1600, 1200, 4357, 304, 64, 46, 1, 192, 3,
400 	  FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
401 	  FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
402 	/* 28 1792x1344-60 VESA */
403 	{ NULL, 60, 1792, 1344, 4882, 328, 128, 46, 1, 200, 3,
404 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
405 	/* 29 1792x1344-75 VESA */
406 	{ NULL, 75, 1792, 1344, 3831, 352, 96, 69, 1, 216, 3,
407 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
408 	/* 30 1856x1392-60 VESA */
409 	{ NULL, 60, 1856, 1392, 4580, 352, 96, 43, 1, 224, 3,
410 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
411 	/* 31 1856x1392-75 VESA */
412 	{ NULL, 75, 1856, 1392, 3472, 352, 128, 104, 1, 224, 3,
413 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
414 	/* 32 1920x1440-60 VESA */
415 	{ NULL, 60, 1920, 1440, 4273, 344, 128, 56, 1, 200, 3,
416 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
417 	/* 33 1920x1440-75 VESA */
418 	{ NULL, 75, 1920, 1440, 3367, 352, 144, 56, 1, 224, 3,
419 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
420 	/* 34 1920x1200-60 RB VESA */
421 	{ NULL, 60, 1920, 1200, 6493, 80, 48, 26, 3, 32, 6,
422 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
423 	/* 35 1920x1200-60 VESA */
424 	{ NULL, 60, 1920, 1200, 5174, 336, 136, 36, 3, 200, 6,
425 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
426 	/* 36 1920x1200-75 VESA */
427 	{ NULL, 75, 1920, 1200, 4077, 344, 136, 46, 3, 208, 6,
428 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
429 	/* 37 1920x1200-85 VESA */
430 	{ NULL, 85, 1920, 1200, 3555, 352, 144, 53, 3, 208, 6,
431 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
432 	/* 38 2560x1600-60 RB VESA */
433 	{ NULL, 60, 2560, 1600, 3724, 80, 48, 37, 3, 32, 6,
434 	  FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
435 	/* 39 2560x1600-60 VESA */
436 	{ NULL, 60, 2560, 1600, 2869, 472, 192, 49, 3, 280, 6,
437 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
438 	/* 40 2560x1600-75 VESA */
439 	{ NULL, 75, 2560, 1600, 2256, 488, 208, 63, 3, 280, 6,
440 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
441 	/* 41 2560x1600-85 VESA */
442 	{ NULL, 85, 2560, 1600, 1979, 488, 208, 73, 3, 280, 6,
443 	  FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
444 	/* 42 2560x1600-120 RB VESA */
445 	{ NULL, 120, 2560, 1600, 1809, 80, 48, 85, 3, 32, 6,
446 	  FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
447 };
448 EXPORT_SYMBOL(vesa_modes);
449 
450 const struct dmt_videomode dmt_modes[DMT_SIZE] = {
451 	{ 0x01, 0x0000, 0x000000, &vesa_modes[0] },
452 	{ 0x02, 0x3119, 0x000000, &vesa_modes[1] },
453 	{ 0x03, 0x0000, 0x000000, &vesa_modes[2] },
454 	{ 0x04, 0x3140, 0x000000, &vesa_modes[3] },
455 	{ 0x05, 0x314c, 0x000000, &vesa_modes[4] },
456 	{ 0x06, 0x314f, 0x000000, &vesa_modes[5] },
457 	{ 0x07, 0x3159, 0x000000, &vesa_modes[6] },
458 	{ 0x08, 0x0000, 0x000000, &vesa_modes[7] },
459 	{ 0x09, 0x4540, 0x000000, &vesa_modes[8] },
460 	{ 0x0a, 0x454c, 0x000000, &vesa_modes[9] },
461 	{ 0x0b, 0x454f, 0x000000, &vesa_modes[10] },
462 	{ 0x0c, 0x4559, 0x000000, &vesa_modes[11] },
463 	{ 0x0d, 0x0000, 0x000000, NULL },
464 	{ 0x0e, 0x0000, 0x000000, NULL },
465 	{ 0x0f, 0x0000, 0x000000, &vesa_modes[12] },
466 	{ 0x10, 0x6140, 0x000000, &vesa_modes[13] },
467 	{ 0x11, 0x614a, 0x000000, &vesa_modes[14] },
468 	{ 0x12, 0x614f, 0x000000, &vesa_modes[15] },
469 	{ 0x13, 0x6159, 0x000000, &vesa_modes[16] },
470 	{ 0x14, 0x0000, 0x000000, NULL },
471 	{ 0x15, 0x714f, 0x000000, &vesa_modes[17] },
472 	{ 0x16, 0x0000, 0x7f1c21, NULL },
473 	{ 0x17, 0x0000, 0x7f1c28, NULL },
474 	{ 0x18, 0x0000, 0x7f1c44, NULL },
475 	{ 0x19, 0x0000, 0x7f1c62, NULL },
476 	{ 0x1a, 0x0000, 0x000000, NULL },
477 	{ 0x1b, 0x0000, 0x8f1821, NULL },
478 	{ 0x1c, 0x8100, 0x8f1828, NULL },
479 	{ 0x1d, 0x810f, 0x8f1844, NULL },
480 	{ 0x1e, 0x8119, 0x8f1862, NULL },
481 	{ 0x1f, 0x0000, 0x000000, NULL },
482 	{ 0x20, 0x8140, 0x000000, &vesa_modes[18] },
483 	{ 0x21, 0x8159, 0x000000, &vesa_modes[19] },
484 	{ 0x22, 0x0000, 0x000000, NULL },
485 	{ 0x23, 0x8180, 0x000000, &vesa_modes[20] },
486 	{ 0x24, 0x818f, 0x000000, &vesa_modes[21] },
487 	{ 0x25, 0x8199, 0x000000, &vesa_modes[22] },
488 	{ 0x26, 0x0000, 0x000000, NULL },
489 	{ 0x27, 0x0000, 0x000000, NULL },
490 	{ 0x28, 0x0000, 0x000000, NULL },
491 	{ 0x29, 0x0000, 0x0c2021, NULL },
492 	{ 0x2a, 0x9040, 0x0c2028, NULL },
493 	{ 0x2b, 0x904f, 0x0c2044, NULL },
494 	{ 0x2c, 0x9059, 0x0c2062, NULL },
495 	{ 0x2d, 0x0000, 0x000000, NULL },
496 	{ 0x2e, 0x9500, 0xc11821, NULL },
497 	{ 0x2f, 0x9500, 0xc11828, NULL },
498 	{ 0x30, 0x950f, 0xc11844, NULL },
499 	{ 0x31, 0x9519, 0xc11868, NULL },
500 	{ 0x32, 0x0000, 0x000000, NULL },
501 	{ 0x33, 0xa940, 0x000000, &vesa_modes[23] },
502 	{ 0x34, 0xa945, 0x000000, &vesa_modes[24] },
503 	{ 0x35, 0xa94a, 0x000000, &vesa_modes[25] },
504 	{ 0x36, 0xa94f, 0x000000, &vesa_modes[26] },
505 	{ 0x37, 0xa959, 0x000000, &vesa_modes[27] },
506 	{ 0x38, 0x0000, 0x000000, NULL },
507 	{ 0x39, 0x0000, 0x0c2821, NULL },
508 	{ 0x3a, 0xb300, 0x0c2828, NULL },
509 	{ 0x3b, 0xb30f, 0x0c2844, NULL },
510 	{ 0x3c, 0xb319, 0x0c2868, NULL },
511 	{ 0x3d, 0x0000, 0x000000, NULL },
512 	{ 0x3e, 0xc140, 0x000000, &vesa_modes[28] },
513 	{ 0x3f, 0xc14f, 0x000000, &vesa_modes[29] },
514 	{ 0x40, 0x0000, 0x000000, NULL},
515 	{ 0x41, 0xc940, 0x000000, &vesa_modes[30] },
516 	{ 0x42, 0xc94f, 0x000000, &vesa_modes[31] },
517 	{ 0x43, 0x0000, 0x000000, NULL },
518 	{ 0x44, 0x0000, 0x572821, &vesa_modes[34] },
519 	{ 0x45, 0xd100, 0x572828, &vesa_modes[35] },
520 	{ 0x46, 0xd10f, 0x572844, &vesa_modes[36] },
521 	{ 0x47, 0xd119, 0x572862, &vesa_modes[37] },
522 	{ 0x48, 0x0000, 0x000000, NULL },
523 	{ 0x49, 0xd140, 0x000000, &vesa_modes[32] },
524 	{ 0x4a, 0xd14f, 0x000000, &vesa_modes[33] },
525 	{ 0x4b, 0x0000, 0x000000, NULL },
526 	{ 0x4c, 0x0000, 0x1f3821, &vesa_modes[38] },
527 	{ 0x4d, 0x0000, 0x1f3828, &vesa_modes[39] },
528 	{ 0x4e, 0x0000, 0x1f3844, &vesa_modes[40] },
529 	{ 0x4f, 0x0000, 0x1f3862, &vesa_modes[41] },
530 	{ 0x50, 0x0000, 0x000000, &vesa_modes[42] },
531 };
532 EXPORT_SYMBOL(dmt_modes);
533 #endif /* CONFIG_FB_MODE_HELPERS */
534 
535 /**
536  *	fb_try_mode - test a video mode
537  *	@var: frame buffer user defined part of display
538  *	@info: frame buffer info structure
539  *	@mode: frame buffer video mode structure
540  *	@bpp: color depth in bits per pixel
541  *
542  *	Tries a video mode to test it's validity for device @info.
543  *
544  *	Returns 1 on success.
545  *
546  */
547 
fb_try_mode(struct fb_var_screeninfo * var,struct fb_info * info,const struct fb_videomode * mode,unsigned int bpp)548 static int fb_try_mode(struct fb_var_screeninfo *var, struct fb_info *info,
549 		       const struct fb_videomode *mode, unsigned int bpp)
550 {
551 	int err = 0;
552 
553 	DPRINTK("Trying mode %s %dx%d-%d@%d\n",
554 		mode->name ? mode->name : "noname",
555 		mode->xres, mode->yres, bpp, mode->refresh);
556 	var->xres = mode->xres;
557 	var->yres = mode->yres;
558 	var->xres_virtual = mode->xres;
559 	var->yres_virtual = mode->yres;
560 	var->xoffset = 0;
561 	var->yoffset = 0;
562 	var->bits_per_pixel = bpp;
563 	var->activate |= FB_ACTIVATE_TEST;
564 	var->pixclock = mode->pixclock;
565 	var->left_margin = mode->left_margin;
566 	var->right_margin = mode->right_margin;
567 	var->upper_margin = mode->upper_margin;
568 	var->lower_margin = mode->lower_margin;
569 	var->hsync_len = mode->hsync_len;
570 	var->vsync_len = mode->vsync_len;
571 	var->sync = mode->sync;
572 	var->vmode = mode->vmode;
573 	if (info->fbops->fb_check_var)
574 		err = info->fbops->fb_check_var(var, info);
575 	var->activate &= ~FB_ACTIVATE_TEST;
576 	return err;
577 }
578 
579 /**
580  * fb_find_mode - finds a valid video mode
581  * @var: frame buffer user defined part of display
582  * @info: frame buffer info structure
583  * @mode_option: string video mode to find
584  * @db: video mode database
585  * @dbsize: size of @db
586  * @default_mode: default video mode to fall back to
587  * @default_bpp: default color depth in bits per pixel
588  *
589  * Finds a suitable video mode, starting with the specified mode
590  * in @mode_option with fallback to @default_mode.  If
591  * @default_mode fails, all modes in the video mode database will
592  * be tried.
593  *
594  * Valid mode specifiers for @mode_option::
595  *
596  *     <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][p][m]
597  *
598  * or ::
599  *
600  *     <name>[-<bpp>][@<refresh>]
601  *
602  * with <xres>, <yres>, <bpp> and <refresh> decimal numbers and
603  * <name> a string.
604  *
605  * If 'M' is present after yres (and before refresh/bpp if present),
606  * the function will compute the timings using VESA(tm) Coordinated
607  * Video Timings (CVT).  If 'R' is present after 'M', will compute with
608  * reduced blanking (for flatpanels).  If 'i' or 'p' are present, compute
609  * interlaced or progressive mode.  If 'm' is present, add margins equal
610  * to 1.8% of xres rounded down to 8 pixels, and 1.8% of yres. The char
611  * 'i', 'p' and 'm' must be after 'M' and 'R'. Example::
612  *
613  *     1024x768MR-8@60m - Reduced blank with margins at 60Hz.
614  *
615  * NOTE: The passed struct @var is _not_ cleared!  This allows you
616  * to supply values for e.g. the grayscale and accel_flags fields.
617  *
618  * Returns zero for failure, 1 if using specified @mode_option,
619  * 2 if using specified @mode_option with an ignored refresh rate,
620  * 3 if default mode is used, 4 if fall back to any valid mode.
621  */
622 
fb_find_mode(struct fb_var_screeninfo * var,struct fb_info * info,const char * mode_option,const struct fb_videomode * db,unsigned int dbsize,const struct fb_videomode * default_mode,unsigned int default_bpp)623 int fb_find_mode(struct fb_var_screeninfo *var,
624 		 struct fb_info *info, const char *mode_option,
625 		 const struct fb_videomode *db, unsigned int dbsize,
626 		 const struct fb_videomode *default_mode,
627 		 unsigned int default_bpp)
628 {
629 	char *mode_option_buf = NULL;
630 	int i;
631 
632 	/* Set up defaults */
633 	if (!db) {
634 		db = modedb;
635 		dbsize = ARRAY_SIZE(modedb);
636 	}
637 
638 	if (!default_mode)
639 		default_mode = &db[0];
640 
641 	if (!default_bpp)
642 		default_bpp = 8;
643 
644 	/* Did the user specify a video mode? */
645 	if (!mode_option) {
646 		fb_get_options(NULL, &mode_option_buf);
647 		mode_option = mode_option_buf;
648 	}
649 	if (mode_option) {
650 		const char *name = mode_option;
651 		unsigned int namelen = strlen(name);
652 		int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
653 		unsigned int xres = 0, yres = 0, bpp = default_bpp, refresh = 0;
654 		int yres_specified = 0, cvt = 0, rb = 0;
655 		int interlace_specified = 0, interlace = 0;
656 		int margins = 0;
657 		u32 best, diff, tdiff;
658 
659 		for (i = namelen-1; i >= 0; i--) {
660 			switch (name[i]) {
661 			case '@':
662 				namelen = i;
663 				if (!refresh_specified && !bpp_specified &&
664 				    !yres_specified) {
665 					refresh = simple_strtol(&name[i+1], NULL,
666 								10);
667 					refresh_specified = 1;
668 					if (cvt || rb)
669 						cvt = 0;
670 				} else
671 					goto done;
672 				break;
673 			case '-':
674 				namelen = i;
675 				if (!bpp_specified && !yres_specified) {
676 					bpp = simple_strtol(&name[i+1], NULL,
677 							    10);
678 					bpp_specified = 1;
679 					if (cvt || rb)
680 						cvt = 0;
681 				} else
682 					goto done;
683 				break;
684 			case 'x':
685 				if (!yres_specified) {
686 					yres = simple_strtol(&name[i+1], NULL,
687 							     10);
688 					yres_specified = 1;
689 				} else
690 					goto done;
691 				break;
692 			case '0' ... '9':
693 				break;
694 			case 'M':
695 				if (!yres_specified)
696 					cvt = 1;
697 				break;
698 			case 'R':
699 				if (!cvt)
700 					rb = 1;
701 				break;
702 			case 'm':
703 				if (!cvt)
704 					margins = 1;
705 				break;
706 			case 'p':
707 				if (!cvt) {
708 					interlace = 0;
709 					interlace_specified = 1;
710 				}
711 				break;
712 			case 'i':
713 				if (!cvt) {
714 					interlace = 1;
715 					interlace_specified = 1;
716 				}
717 				break;
718 			default:
719 				goto done;
720 			}
721 		}
722 		if (i < 0 && yres_specified) {
723 			xres = simple_strtol(name, NULL, 10);
724 			res_specified = 1;
725 		}
726 done:
727 		kfree(mode_option_buf);
728 		if (cvt) {
729 			struct fb_videomode cvt_mode;
730 			int ret;
731 
732 			DPRINTK("CVT mode %dx%d@%dHz%s%s%s\n", xres, yres,
733 				(refresh) ? refresh : 60,
734 				(rb) ? " reduced blanking" : "",
735 				(margins) ? " with margins" : "",
736 				(interlace) ? " interlaced" : "");
737 
738 			memset(&cvt_mode, 0, sizeof(cvt_mode));
739 			cvt_mode.xres = xres;
740 			cvt_mode.yres = yres;
741 			cvt_mode.refresh = (refresh) ? refresh : 60;
742 
743 			if (interlace)
744 				cvt_mode.vmode |= FB_VMODE_INTERLACED;
745 			else
746 				cvt_mode.vmode &= ~FB_VMODE_INTERLACED;
747 
748 			ret = fb_find_mode_cvt(&cvt_mode, margins, rb);
749 
750 			if (!ret && !fb_try_mode(var, info, &cvt_mode, bpp)) {
751 				DPRINTK("modedb CVT: CVT mode ok\n");
752 				return 1;
753 			}
754 
755 			DPRINTK("CVT mode invalid, getting mode from database\n");
756 		}
757 
758 		DPRINTK("Trying specified video mode%s %ix%i\n",
759 			refresh_specified ? "" : " (ignoring refresh rate)",
760 			xres, yres);
761 
762 		if (!refresh_specified) {
763 			/*
764 			 * If the caller has provided a custom mode database and
765 			 * a valid monspecs structure, we look for the mode with
766 			 * the highest refresh rate.  Otherwise we play it safe
767 			 * it and try to find a mode with a refresh rate closest
768 			 * to the standard 60 Hz.
769 			 */
770 			if (db != modedb &&
771 			    info->monspecs.vfmin && info->monspecs.vfmax &&
772 			    info->monspecs.hfmin && info->monspecs.hfmax &&
773 			    info->monspecs.dclkmax) {
774 				refresh = 1000;
775 			} else {
776 				refresh = 60;
777 			}
778 		}
779 
780 		diff = -1;
781 		best = -1;
782 		for (i = 0; i < dbsize; i++) {
783 			if ((name_matches(db[i], name, namelen) ||
784 			     (res_specified && res_matches(db[i], xres, yres))) &&
785 			    !fb_try_mode(var, info, &db[i], bpp)) {
786 				const int db_interlace = (db[i].vmode &
787 					FB_VMODE_INTERLACED ? 1 : 0);
788 				int score = abs(db[i].refresh - refresh);
789 
790 				if (interlace_specified)
791 					score += abs(db_interlace - interlace);
792 
793 				if (!interlace_specified ||
794 				    db_interlace == interlace)
795 					if (refresh_specified &&
796 					    db[i].refresh == refresh)
797 						return 1;
798 
799 				if (score < diff) {
800 					diff = score;
801 					best = i;
802 				}
803 			}
804 		}
805 		if (best != -1) {
806 			fb_try_mode(var, info, &db[best], bpp);
807 			return (refresh_specified) ? 2 : 1;
808 		}
809 
810 		diff = 2 * (xres + yres);
811 		best = -1;
812 		DPRINTK("Trying best-fit modes\n");
813 		for (i = 0; i < dbsize; i++) {
814 			DPRINTK("Trying %ix%i\n", db[i].xres, db[i].yres);
815 			if (!fb_try_mode(var, info, &db[i], bpp)) {
816 				tdiff = abs(db[i].xres - xres) +
817 					abs(db[i].yres - yres);
818 
819 				/*
820 				 * Penalize modes with resolutions smaller
821 				 * than requested.
822 				 */
823 				if (xres > db[i].xres || yres > db[i].yres)
824 					tdiff += xres + yres;
825 
826 				if (diff > tdiff) {
827 					diff = tdiff;
828 					best = i;
829 				}
830 			}
831 		}
832 		if (best != -1) {
833 			fb_try_mode(var, info, &db[best], bpp);
834 			return 5;
835 		}
836 	}
837 
838 	DPRINTK("Trying default video mode\n");
839 	if (!fb_try_mode(var, info, default_mode, default_bpp))
840 		return 3;
841 
842 	DPRINTK("Trying all modes\n");
843 	for (i = 0; i < dbsize; i++)
844 		if (!fb_try_mode(var, info, &db[i], default_bpp))
845 			return 4;
846 
847 	DPRINTK("No valid mode found\n");
848 	return 0;
849 }
850 
851 /**
852  * fb_var_to_videomode - convert fb_var_screeninfo to fb_videomode
853  * @mode: pointer to struct fb_videomode
854  * @var: pointer to struct fb_var_screeninfo
855  */
fb_var_to_videomode(struct fb_videomode * mode,const struct fb_var_screeninfo * var)856 void fb_var_to_videomode(struct fb_videomode *mode,
857 			 const struct fb_var_screeninfo *var)
858 {
859 	u32 pixclock, hfreq, htotal, vtotal;
860 
861 	mode->name = NULL;
862 	mode->xres = var->xres;
863 	mode->yres = var->yres;
864 	mode->pixclock = var->pixclock;
865 	mode->hsync_len = var->hsync_len;
866 	mode->vsync_len = var->vsync_len;
867 	mode->left_margin = var->left_margin;
868 	mode->right_margin = var->right_margin;
869 	mode->upper_margin = var->upper_margin;
870 	mode->lower_margin = var->lower_margin;
871 	mode->sync = var->sync;
872 	mode->vmode = var->vmode & FB_VMODE_MASK;
873 	mode->flag = FB_MODE_IS_FROM_VAR;
874 	mode->refresh = 0;
875 
876 	if (!var->pixclock)
877 		return;
878 
879 	pixclock = PICOS2KHZ(var->pixclock) * 1000;
880 
881 	htotal = var->xres + var->right_margin + var->hsync_len +
882 		var->left_margin;
883 	vtotal = var->yres + var->lower_margin + var->vsync_len +
884 		var->upper_margin;
885 
886 	if (var->vmode & FB_VMODE_INTERLACED)
887 		vtotal /= 2;
888 	if (var->vmode & FB_VMODE_DOUBLE)
889 		vtotal *= 2;
890 
891 	if (!htotal || !vtotal)
892 		return;
893 
894 	hfreq = pixclock/htotal;
895 	mode->refresh = hfreq/vtotal;
896 }
897 
898 /**
899  * fb_videomode_to_var - convert fb_videomode to fb_var_screeninfo
900  * @var: pointer to struct fb_var_screeninfo
901  * @mode: pointer to struct fb_videomode
902  */
fb_videomode_to_var(struct fb_var_screeninfo * var,const struct fb_videomode * mode)903 void fb_videomode_to_var(struct fb_var_screeninfo *var,
904 			 const struct fb_videomode *mode)
905 {
906 	var->xres = mode->xres;
907 	var->yres = mode->yres;
908 	var->xres_virtual = mode->xres;
909 	var->yres_virtual = mode->yres;
910 	var->xoffset = 0;
911 	var->yoffset = 0;
912 	var->pixclock = mode->pixclock;
913 	var->left_margin = mode->left_margin;
914 	var->right_margin = mode->right_margin;
915 	var->upper_margin = mode->upper_margin;
916 	var->lower_margin = mode->lower_margin;
917 	var->hsync_len = mode->hsync_len;
918 	var->vsync_len = mode->vsync_len;
919 	var->sync = mode->sync;
920 	var->vmode = mode->vmode & FB_VMODE_MASK;
921 }
922 
923 /**
924  * fb_mode_is_equal - compare 2 videomodes
925  * @mode1: first videomode
926  * @mode2: second videomode
927  *
928  * RETURNS:
929  * 1 if equal, 0 if not
930  */
fb_mode_is_equal(const struct fb_videomode * mode1,const struct fb_videomode * mode2)931 int fb_mode_is_equal(const struct fb_videomode *mode1,
932 		     const struct fb_videomode *mode2)
933 {
934 	return (mode1->xres         == mode2->xres &&
935 		mode1->yres         == mode2->yres &&
936 		mode1->pixclock     == mode2->pixclock &&
937 		mode1->hsync_len    == mode2->hsync_len &&
938 		mode1->vsync_len    == mode2->vsync_len &&
939 		mode1->left_margin  == mode2->left_margin &&
940 		mode1->right_margin == mode2->right_margin &&
941 		mode1->upper_margin == mode2->upper_margin &&
942 		mode1->lower_margin == mode2->lower_margin &&
943 		mode1->sync         == mode2->sync &&
944 		mode1->vmode        == mode2->vmode);
945 }
946 
947 /**
948  * fb_find_best_mode - find best matching videomode
949  * @var: pointer to struct fb_var_screeninfo
950  * @head: pointer to struct list_head of modelist
951  *
952  * RETURNS:
953  * struct fb_videomode, NULL if none found
954  *
955  * IMPORTANT:
956  * This function assumes that all modelist entries in
957  * info->modelist are valid.
958  *
959  * NOTES:
960  * Finds best matching videomode which has an equal or greater dimension than
961  * var->xres and var->yres.  If more than 1 videomode is found, will return
962  * the videomode with the highest refresh rate
963  */
fb_find_best_mode(const struct fb_var_screeninfo * var,struct list_head * head)964 const struct fb_videomode *fb_find_best_mode(const struct fb_var_screeninfo *var,
965 					     struct list_head *head)
966 {
967 	struct fb_modelist *modelist;
968 	struct fb_videomode *mode, *best = NULL;
969 	u32 diff = -1;
970 
971 	list_for_each_entry(modelist, head, list) {
972 		u32 d;
973 		mode = &modelist->mode;
974 
975 		if (mode->xres >= var->xres && mode->yres >= var->yres) {
976 			d = (mode->xres - var->xres) +
977 				(mode->yres - var->yres);
978 			if (diff > d) {
979 				diff = d;
980 				best = mode;
981 			} else if (diff == d && best &&
982 				   mode->refresh > best->refresh)
983 				best = mode;
984 		}
985 	}
986 	return best;
987 }
988 
989 /**
990  * fb_find_nearest_mode - find closest videomode
991  *
992  * @mode: pointer to struct fb_videomode
993  * @head: pointer to modelist
994  *
995  * Finds best matching videomode, smaller or greater in dimension.
996  * If more than 1 videomode is found, will return the videomode with
997  * the closest refresh rate.
998  */
fb_find_nearest_mode(const struct fb_videomode * mode,struct list_head * head)999 const struct fb_videomode *fb_find_nearest_mode(const struct fb_videomode *mode,
1000 					        struct list_head *head)
1001 {
1002 	struct fb_modelist *modelist;
1003 	struct fb_videomode *cmode, *best = NULL;
1004 	u32 diff = -1, diff_refresh = -1;
1005 
1006 	list_for_each_entry(modelist, head, list) {
1007 		u32 d;
1008 		cmode = &modelist->mode;
1009 
1010 		d = abs(cmode->xres - mode->xres) +
1011 			abs(cmode->yres - mode->yres);
1012 		if (diff > d) {
1013 			diff = d;
1014 			diff_refresh = abs(cmode->refresh - mode->refresh);
1015 			best = cmode;
1016 		} else if (diff == d) {
1017 			d = abs(cmode->refresh - mode->refresh);
1018 			if (diff_refresh > d) {
1019 				diff_refresh = d;
1020 				best = cmode;
1021 			}
1022 		}
1023 	}
1024 
1025 	return best;
1026 }
1027 
1028 /**
1029  * fb_match_mode - find a videomode which exactly matches the timings in var
1030  * @var: pointer to struct fb_var_screeninfo
1031  * @head: pointer to struct list_head of modelist
1032  *
1033  * RETURNS:
1034  * struct fb_videomode, NULL if none found
1035  */
fb_match_mode(const struct fb_var_screeninfo * var,struct list_head * head)1036 const struct fb_videomode *fb_match_mode(const struct fb_var_screeninfo *var,
1037 					 struct list_head *head)
1038 {
1039 	struct fb_modelist *modelist;
1040 	struct fb_videomode *m, mode;
1041 
1042 	fb_var_to_videomode(&mode, var);
1043 	list_for_each_entry(modelist, head, list) {
1044 		m = &modelist->mode;
1045 		if (fb_mode_is_equal(m, &mode))
1046 			return m;
1047 	}
1048 	return NULL;
1049 }
1050 
1051 /**
1052  * fb_add_videomode - adds videomode entry to modelist
1053  * @mode: videomode to add
1054  * @head: struct list_head of modelist
1055  *
1056  * NOTES:
1057  * Will only add unmatched mode entries
1058  */
fb_add_videomode(const struct fb_videomode * mode,struct list_head * head)1059 int fb_add_videomode(const struct fb_videomode *mode, struct list_head *head)
1060 {
1061 	struct fb_modelist *modelist;
1062 	struct fb_videomode *m;
1063 	int found = 0;
1064 
1065 	list_for_each_entry(modelist, head, list) {
1066 		m = &modelist->mode;
1067 		if (fb_mode_is_equal(m, mode)) {
1068 			found = 1;
1069 			break;
1070 		}
1071 	}
1072 	if (!found) {
1073 		modelist = kmalloc(sizeof(struct fb_modelist),
1074 						  GFP_KERNEL);
1075 
1076 		if (!modelist)
1077 			return -ENOMEM;
1078 		modelist->mode = *mode;
1079 		list_add(&modelist->list, head);
1080 	}
1081 	return 0;
1082 }
1083 
1084 /**
1085  * fb_delete_videomode - removed videomode entry from modelist
1086  * @mode: videomode to remove
1087  * @head: struct list_head of modelist
1088  *
1089  * NOTES:
1090  * Will remove all matching mode entries
1091  */
fb_delete_videomode(const struct fb_videomode * mode,struct list_head * head)1092 void fb_delete_videomode(const struct fb_videomode *mode,
1093 			 struct list_head *head)
1094 {
1095 	struct list_head *pos, *n;
1096 	struct fb_modelist *modelist;
1097 	struct fb_videomode *m;
1098 
1099 	list_for_each_safe(pos, n, head) {
1100 		modelist = list_entry(pos, struct fb_modelist, list);
1101 		m = &modelist->mode;
1102 		if (fb_mode_is_equal(m, mode)) {
1103 			list_del(pos);
1104 			kfree(pos);
1105 		}
1106 	}
1107 }
1108 
1109 /**
1110  * fb_destroy_modelist - destroy modelist
1111  * @head: struct list_head of modelist
1112  */
fb_destroy_modelist(struct list_head * head)1113 void fb_destroy_modelist(struct list_head *head)
1114 {
1115 	struct list_head *pos, *n;
1116 
1117 	list_for_each_safe(pos, n, head) {
1118 		list_del(pos);
1119 		kfree(pos);
1120 	}
1121 }
1122 EXPORT_SYMBOL_GPL(fb_destroy_modelist);
1123 
1124 /**
1125  * fb_videomode_to_modelist - convert mode array to mode list
1126  * @modedb: array of struct fb_videomode
1127  * @num: number of entries in array
1128  * @head: struct list_head of modelist
1129  */
fb_videomode_to_modelist(const struct fb_videomode * modedb,int num,struct list_head * head)1130 void fb_videomode_to_modelist(const struct fb_videomode *modedb, int num,
1131 			      struct list_head *head)
1132 {
1133 	int i;
1134 
1135 	INIT_LIST_HEAD(head);
1136 
1137 	for (i = 0; i < num; i++) {
1138 		if (fb_add_videomode(&modedb[i], head))
1139 			return;
1140 	}
1141 }
1142 
fb_find_best_display(const struct fb_monspecs * specs,struct list_head * head)1143 const struct fb_videomode *fb_find_best_display(const struct fb_monspecs *specs,
1144 					        struct list_head *head)
1145 {
1146 	struct fb_modelist *modelist;
1147 	const struct fb_videomode *m, *m1 = NULL, *md = NULL, *best = NULL;
1148 	int first = 0;
1149 
1150 	if (!head->prev || !head->next || list_empty(head))
1151 		goto finished;
1152 
1153 	/* get the first detailed mode and the very first mode */
1154 	list_for_each_entry(modelist, head, list) {
1155 		m = &modelist->mode;
1156 
1157 		if (!first) {
1158 			m1 = m;
1159 			first = 1;
1160 		}
1161 
1162 		if (m->flag & FB_MODE_IS_FIRST) {
1163  			md = m;
1164 			break;
1165 		}
1166 	}
1167 
1168 	/* first detailed timing is preferred */
1169 	if (specs->misc & FB_MISC_1ST_DETAIL) {
1170 		best = md;
1171 		goto finished;
1172 	}
1173 
1174 	/* find best mode based on display width and height */
1175 	if (specs->max_x && specs->max_y) {
1176 		struct fb_var_screeninfo var;
1177 
1178 		memset(&var, 0, sizeof(struct fb_var_screeninfo));
1179 		var.xres = (specs->max_x * 7200)/254;
1180 		var.yres = (specs->max_y * 7200)/254;
1181 		m = fb_find_best_mode(&var, head);
1182 		if (m) {
1183 			best = m;
1184 			goto finished;
1185 		}
1186 	}
1187 
1188 	/* use first detailed mode */
1189 	if (md) {
1190 		best = md;
1191 		goto finished;
1192 	}
1193 
1194 	/* last resort, use the very first mode */
1195 	best = m1;
1196 finished:
1197 	return best;
1198 }
1199 EXPORT_SYMBOL(fb_find_best_display);
1200 
1201 EXPORT_SYMBOL(fb_videomode_to_var);
1202 EXPORT_SYMBOL(fb_var_to_videomode);
1203 EXPORT_SYMBOL(fb_mode_is_equal);
1204 EXPORT_SYMBOL(fb_add_videomode);
1205 EXPORT_SYMBOL(fb_match_mode);
1206 EXPORT_SYMBOL(fb_find_best_mode);
1207 EXPORT_SYMBOL(fb_find_nearest_mode);
1208 EXPORT_SYMBOL(fb_videomode_to_modelist);
1209 EXPORT_SYMBOL(fb_find_mode);
1210 EXPORT_SYMBOL(fb_find_mode_cvt);
1211