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