1 /*
2  *  linux/drivers/video/macmodes.c -- Standard MacOS video modes
3  *
4  *	Copyright (C) 1998 Geert Uytterhoeven
5  *
6  *      2000 - Removal of OpenFirmware dependencies by:
7  *      - Ani Joshi
8  *      - Brad Douglas <brad@neruo.com>
9  *
10  *	2001 - Documented with DocBook
11  *	- Brad Douglas <brad@neruo.com>
12  *
13  *  This file is subject to the terms and conditions of the GNU General Public
14  *  License. See the file COPYING in the main directory of this archive for
15  *  more details.
16  */
17 
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/fb.h>
21 #include <linux/string.h>
22 
23 #ifdef CONFIG_FB_COMPAT_XPMAC
24 #include <asm/vc_ioctl.h>
25 #endif
26 
27 #include <video/fbcon.h>
28 #include <video/macmodes.h>
29 
30 
31     /*
32      *  MacOS video mode definitions
33      *
34      *  Order IS important! If you change these, don't forget to update
35      *  mac_modes[] below!
36      */
37 
38 #define DEFAULT_MODEDB_INDEX	0
39 
40 static const struct fb_videomode mac_modedb[] = {
41     {
42 	/* 640x480, 60 Hz, Non-Interlaced (25.175 MHz dotclock) */
43 	"mac5", 60, 640, 480, 39722, 32, 32, 33, 10, 96, 2,
44 	0, FB_VMODE_NONINTERLACED
45     }, {
46 	/* 640x480, 67Hz, Non-Interlaced (30.0 MHz dotclock) */
47 	"mac6", 67, 640, 480, 33334, 80, 80, 39, 3, 64, 3,
48 	0, FB_VMODE_NONINTERLACED
49     }, {
50 	/* 800x600, 56 Hz, Non-Interlaced (36.00 MHz dotclock) */
51 	"mac9", 56, 800, 600, 27778, 112, 40, 22, 1, 72, 2,
52 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
53     }, {
54 	/* 800x600, 60 Hz, Non-Interlaced (40.00 MHz dotclock) */
55 	"mac10", 60, 800, 600, 25000, 72, 56, 23, 1, 128, 4,
56 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
57     }, {
58 	/* 800x600, 72 Hz, Non-Interlaced (50.00 MHz dotclock) */
59 	"mac11", 72, 800, 600, 20000, 48, 72, 23, 37, 120, 6,
60 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
61     }, {
62 	/* 800x600, 75 Hz, Non-Interlaced (49.50 MHz dotclock) */
63 	"mac12", 75, 800, 600, 20203, 144, 32, 21, 1, 80, 3,
64 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
65     }, {
66 	/* 832x624, 75Hz, Non-Interlaced (57.6 MHz dotclock) */
67 	"mac13", 75, 832, 624, 17362, 208, 48, 39, 1, 64, 3,
68 	0, FB_VMODE_NONINTERLACED
69     }, {
70 	/* 1024x768, 60 Hz, Non-Interlaced (65.00 MHz dotclock) */
71 	"mac14", 60, 1024, 768, 15385, 144, 40, 29, 3, 136, 6,
72 	0, FB_VMODE_NONINTERLACED
73     }, {
74 	/* 1024x768, 72 Hz, Non-Interlaced (75.00 MHz dotclock) */
75 	"mac15", 72, 1024, 768, 13334, 128, 40, 29, 3, 136, 6,
76 	0, FB_VMODE_NONINTERLACED
77     }, {
78 	/* 1024x768, 75 Hz, Non-Interlaced (78.75 MHz dotclock) */
79 	"mac16", 75, 1024, 768, 12699, 176, 16, 28, 1, 96, 3,
80 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
81     }, {
82 	/* 1024x768, 75 Hz, Non-Interlaced (78.75 MHz dotclock) */
83 	"mac17", 75, 1024, 768, 12699, 160, 32, 28, 1, 96, 3,
84 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
85     }, {
86 	/* 1152x870, 75 Hz, Non-Interlaced (100.0 MHz dotclock) */
87 	"mac18", 75, 1152, 870, 10000, 128, 48, 39, 3, 128, 3,
88 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
89     }, {
90 	/* 1280x960, 75 Hz, Non-Interlaced (126.00 MHz dotclock) */
91 	"mac19", 75, 1280, 960, 7937, 224, 32, 36, 1, 144, 3,
92 	0, FB_VMODE_NONINTERLACED
93     }, {
94 	/* 1280x1024, 75 Hz, Non-Interlaced (135.00 MHz dotclock) */
95 	"mac20", 75, 1280, 1024, 7408, 232, 64, 38, 1, 112, 3,
96 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
97     }, {
98 	/* 1152x768, 60 Hz, Titanium PowerBook */
99 	"mac21", 60, 1152, 768, 15386, 158, 26, 29, 3, 136, 6,
100 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
101     }, {
102 	/* 1600x1024, 60 Hz, Non-Interlaced (112.27 MHz dotclock) */
103 	"mac22", 60, 1600, 1024, 8908, 88, 104, 1, 10, 16, 1,
104 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
105     }
106 
107 #if 0
108     /* Anyone who has timings for these? */
109     {
110 	/* VMODE_512_384_60I: 512x384, 60Hz, Interlaced (NTSC) */
111 	"mac1", 60, 512, 384, pixclock, left, right, upper, lower, hslen, vslen,
112 	sync, FB_VMODE_INTERLACED
113     }, {
114 	/* VMODE_512_384_60: 512x384, 60Hz, Non-Interlaced */
115     	"mac2", 60, 512, 384, pixclock, left, right, upper, lower, hslen, vslen,
116 	sync, FB_VMODE_NONINTERLACED
117     }, {
118 	/* VMODE_640_480_50I: 640x480, 50Hz, Interlaced (PAL) */
119 	"mac3", 50, 640, 480, pixclock, left, right, upper, lower, hslen, vslen,
120 	sync, FB_VMODE_INTERLACED
121     }, {
122 	/* VMODE_640_480_60I: 640x480, 60Hz, Interlaced (NTSC) */
123 	"mac4", 60, 640, 480, pixclock, left, right, upper, lower, hslen, vslen,
124 	sync, FB_VMODE_INTERLACED
125     }, {
126 	/* VMODE_640_870_75P: 640x870, 75Hz (portrait), Non-Interlaced */
127 	"mac7", 75, 640, 870, pixclock, left, right, upper, lower, hslen, vslen,
128 	sync, FB_VMODE_NONINTERLACED
129     }, {
130 	/* VMODE_768_576_50I: 768x576, 50Hz (PAL full frame), Interlaced */
131 	"mac8", 50, 768, 576, pixclock, left, right, upper, lower, hslen, vslen,
132 	sync, FB_VMODE_INTERLACED
133     },
134 #endif
135 };
136 
137 
138     /*
139      *  Mapping between MacOS video mode numbers and video mode definitions
140      *
141      *  These MUST be ordered in
142      *    - increasing resolution
143      *    - decreasing refresh rate
144      */
145 
146 static const struct mode_map {
147     int vmode;
148     const struct fb_videomode *mode;
149 } mac_modes[] = {
150     /* 640x480 */
151     { VMODE_640_480_67, &mac_modedb[1] },
152     { VMODE_640_480_60, &mac_modedb[0] },
153     /* 800x600 */
154     { VMODE_800_600_75, &mac_modedb[5] },
155     { VMODE_800_600_72, &mac_modedb[4] },
156     { VMODE_800_600_60, &mac_modedb[3] },
157     { VMODE_800_600_56, &mac_modedb[2] },
158     /* 832x624 */
159     { VMODE_832_624_75, &mac_modedb[6] },
160     /* 1024x768 */
161     { VMODE_1024_768_75, &mac_modedb[10] },
162     { VMODE_1024_768_75V, &mac_modedb[9] },
163     { VMODE_1024_768_70, &mac_modedb[8] },
164     { VMODE_1024_768_60, &mac_modedb[7] },
165     /* 1152x768 */
166     { VMODE_1152_768_60, &mac_modedb[14] },
167     /* 1152x870 */
168     { VMODE_1152_870_75, &mac_modedb[11] },
169     /* 1280x960 */
170     { VMODE_1280_960_75, &mac_modedb[12] },
171     /* 1280x1024 */
172     { VMODE_1280_1024_75, &mac_modedb[13] },
173     /* 1600x1024 */
174     { VMODE_1600_1024_60, &mac_modedb[15] },
175     { -1, NULL }
176 };
177 
178 
179     /*
180      *  Mapping between monitor sense values and MacOS video mode numbers
181      */
182 
183 static const struct monitor_map {
184     int sense;
185     int vmode;
186 } mac_monitors[] = {
187     { 0x000, VMODE_1280_1024_75 },	/* 21" RGB */
188     { 0x114, VMODE_640_870_75P },	/* Portrait Monochrome */
189     { 0x221, VMODE_512_384_60 },	/* 12" RGB*/
190     { 0x331, VMODE_1280_1024_75 },	/* 21" RGB (Radius) */
191     { 0x334, VMODE_1280_1024_75 },	/* 21" mono (Radius) */
192     { 0x335, VMODE_1280_1024_75 },	/* 21" mono */
193     { 0x40A, VMODE_640_480_60I },	/* NTSC */
194     { 0x51E, VMODE_640_870_75P },	/* Portrait RGB */
195     { 0x603, VMODE_832_624_75 },	/* 12"-16" multiscan */
196     { 0x60b, VMODE_1024_768_70 },	/* 13"-19" multiscan */
197     { 0x623, VMODE_1152_870_75 },	/* 13"-21" multiscan */
198     { 0x62b, VMODE_640_480_67 },	/* 13"/14" RGB */
199     { 0x700, VMODE_640_480_50I },	/* PAL */
200     { 0x714, VMODE_640_480_60I },	/* NTSC */
201     { 0x717, VMODE_800_600_75 },	/* VGA */
202     { 0x72d, VMODE_832_624_75 },	/* 16" RGB (Goldfish) */
203     { 0x730, VMODE_768_576_50I },	/* PAL (Alternate) */
204     { 0x73a, VMODE_1152_870_75 },	/* 3rd party 19" */
205     { 0x73f, VMODE_640_480_67 },	/* no sense lines connected at all */
206     { 0xBEEF, VMODE_1600_1024_60 },	/* 22" Apple Cinema Display */
207     { -1,    VMODE_640_480_60 },	/* catch-all, must be last */
208 };
209 
210 #ifdef CONFIG_FB_COMPAT_XPMAC
211 struct fb_info *console_fb_info = NULL;
212 struct vc_mode display_info;
213 
214 static u16 palette_red[16];
215 static u16 palette_green[16];
216 static u16 palette_blue[16];
217 static struct fb_cmap palette_cmap = {
218     0, 16, palette_red, palette_green, palette_blue, NULL
219 };
220 
221 
222 /**
223  *	console_getmode - get current mode
224  *	@mode: virtual console mode structure
225  *
226  *	Populates @mode with the current mode held in the global
227  *	display_info structure.
228  *
229  *	Note, this function is only for XPMAC compatibility.
230  *
231  *	Returns zero.
232  */
233 
console_getmode(struct vc_mode * mode)234 int console_getmode(struct vc_mode *mode)
235 {
236     *mode = display_info;
237     return 0;
238 }
239 
240 
241 /**
242  *	console_setmode - sets current console mode
243  *	@mode: virtual console mode structure
244  *	@doit: boolean, 0 test mode, 1 test and activate mode
245  *
246  *	Sets @mode for all virtual consoles if @doit is non-zero,
247  *	otherwise, test a mode for validity.
248  *
249  *	Note, this function is only for XPMAC compatibility.
250  *
251  *	Returns negative errno on error, or zero for success.
252  *
253  */
254 
console_setmode(struct vc_mode * mode,int doit)255 int console_setmode(struct vc_mode *mode, int doit)
256 {
257     struct fb_var_screeninfo var;
258     int cmode, err;
259 
260     if (!console_fb_info)
261         return -EOPNOTSUPP;
262 
263     switch(mode->depth) {
264         case 0:
265         case 8:
266             cmode = CMODE_8;
267             break;
268         case 16:
269             cmode = CMODE_16;
270             break;
271         case 24:
272         case 32:
273             cmode = CMODE_32;
274             break;
275         default:
276             return -EINVAL;
277     }
278 
279     if ((err = mac_vmode_to_var(mode->mode, cmode, &var)))
280         return err;
281 
282     var.activate = FB_ACTIVATE_TEST;
283     err = console_fb_info->fbops->fb_set_var(&var, fg_console,
284                                              console_fb_info);
285     if (err || !doit)
286         return err;
287     else {
288         int unit;
289 
290         var.activate = FB_ACTIVATE_NOW;
291         for (unit = 0; unit < MAX_NR_CONSOLES; unit++)
292             if (fb_display[unit].conp &&
293                 (GET_FB_IDX(console_fb_info->node) == con2fb_map[unit]))
294                 console_fb_info->fbops->fb_set_var(&var, unit,
295                                                    console_fb_info);
296     }
297 
298     return 0;
299 }
300 
301 
302 /**
303  *	console_setcmap - sets palette color map for console
304  *	@n_entries: number of entries in the palette (max 16)
305  *	@red: value for red component of palette
306  *	@green: value for green component of palette
307  *	@blue: value for blue component of palette
308  *
309  *	Sets global palette_cmap structure and activates the palette
310  *	on the current console.
311  *
312  *	Note, this function is only for XPMAC compatibility.
313  *
314  *	Returns negative errno on error, or zero for success.
315  *
316  */
317 
console_setcmap(int n_entries,unsigned char * red,unsigned char * green,unsigned char * blue)318 int console_setcmap(int n_entries, unsigned char *red, unsigned char *green,
319                     unsigned char *blue)
320 {
321     int i, j, n = 0, err;
322 
323     if (!console_fb_info)
324         return -EOPNOTSUPP;
325 
326     for (i = 0; i < n_entries; i += n) {
327         n = n_entries - i;
328         if (n > 16)
329             n = 16;
330         palette_cmap.start = i;
331         palette_cmap.len = n;
332 
333         for (j = 0; j < n; j++) {
334             palette_cmap.red[j] = (red[i+j] << 8) | red[i+j];
335             palette_cmap.green[j] = (green[i+j] << 8) | green[i+j];
336             palette_cmap.blue[j] = (blue[i+j] << 8) | blue[i+j];
337         }
338         err = console_fb_info->fbops->fb_set_cmap(&palette_cmap, 1,
339                                                   fg_console,
340                                                   console_fb_info);
341         if (err)
342             return err;
343     }
344 
345     return 0;
346 }
347 
348 
349 /**
350  *	console_powermode - sets monitor power mode
351  *	@mode: power state to set
352  *
353  *	Sets power state as dictated by @mode.
354  *
355  *	Note that this function is only for XPMAC compatibility and
356  *	doesn't do much.
357  *
358  *	Returns 0 for %VC_POWERMODE_INQUIRY, -EINVAL for VESA power
359  *	settings, or -ENIXIO on failure.
360  *
361  */
362 
console_powermode(int mode)363 int console_powermode(int mode)
364 {
365     if (mode == VC_POWERMODE_INQUIRY)
366         return 0;
367     if (mode < VESA_NO_BLANKING || mode > VESA_POWERDOWN)
368         return -EINVAL;
369     /* Not Supported */
370     return -ENXIO;
371 }
372 #endif /* CONFIG_FB_COMPAT_XPMAC */
373 
374 
375 /**
376  *	mac_vmode_to_var - converts vmode/cmode pair to var structure
377  *	@vmode: MacOS video mode
378  *	@cmode: MacOS color mode
379  *	@var: frame buffer video mode structure
380  *
381  *	Converts a MacOS vmode/cmode pair to a frame buffer video
382  *	mode structure.
383  *
384  *	Returns negative errno on error, or zero for success.
385  *
386  */
387 
mac_vmode_to_var(int vmode,int cmode,struct fb_var_screeninfo * var)388 int mac_vmode_to_var(int vmode, int cmode, struct fb_var_screeninfo *var)
389 {
390     const struct fb_videomode *mode = NULL;
391     const struct mode_map *map;
392 
393     for (map = mac_modes; map->vmode != -1; map++)
394 	if (map->vmode == vmode) {
395 	    mode = map->mode;
396 	    break;
397 	}
398     if (!mode)
399 	return -EINVAL;
400 
401     memset(var, 0, sizeof(struct fb_var_screeninfo));
402     switch (cmode) {
403 	case CMODE_8:
404 	    var->bits_per_pixel = 8;
405 	    var->red.offset = 0;
406 	    var->red.length = 8;
407 	    var->green.offset = 0;
408 	    var->green.length = 8;
409 	    var->blue.offset = 0;
410 	    var->blue.length = 8;
411 	    break;
412 
413 	case CMODE_16:
414 	    var->bits_per_pixel = 16;
415 	    var->red.offset = 10;
416 	    var->red.length = 5;
417 	    var->green.offset = 5;
418 	    var->green.length = 5;
419 	    var->blue.offset = 0;
420 	    var->blue.length = 5;
421 	    break;
422 
423 	case CMODE_32:
424 	    var->bits_per_pixel = 32;
425 	    var->red.offset = 16;
426 	    var->red.length = 8;
427 	    var->green.offset = 8;
428 	    var->green.length = 8;
429 	    var->blue.offset = 0;
430 	    var->blue.length = 8;
431 	    var->transp.offset = 24;
432 	    var->transp.length = 8;
433 	    break;
434 
435 	default:
436 	    return -EINVAL;
437     }
438     var->xres = mode->xres;
439     var->yres = mode->yres;
440     var->xres_virtual = mode->xres;
441     var->yres_virtual = mode->yres;
442     var->height = -1;
443     var->width = -1;
444     var->pixclock = mode->pixclock;
445     var->left_margin = mode->left_margin;
446     var->right_margin = mode->right_margin;
447     var->upper_margin = mode->upper_margin;
448     var->lower_margin = mode->lower_margin;
449     var->hsync_len = mode->hsync_len;
450     var->vsync_len = mode->vsync_len;
451     var->sync = mode->sync;
452     var->vmode = mode->vmode;
453     return 0;
454 }
455 
456 
457 /**
458  *	mac_var_to_vmode - convert var structure to MacOS vmode/cmode pair
459  *	@var: frame buffer video mode structure
460  *	@vmode: MacOS video mode
461  *	@cmode: MacOS color mode
462  *
463  *	Converts a frame buffer video mode structure to a MacOS
464  *	vmode/cmode pair.
465  *
466  *	Returns negative errno on error, or zero for success.
467  *
468  */
469 
mac_var_to_vmode(const struct fb_var_screeninfo * var,int * vmode,int * cmode)470 int mac_var_to_vmode(const struct fb_var_screeninfo *var, int *vmode,
471 		     int *cmode)
472 {
473     const struct fb_videomode *mode = NULL;
474     const struct mode_map *map;
475 
476     if (var->bits_per_pixel <= 8)
477 	*cmode = CMODE_8;
478     else if (var->bits_per_pixel <= 16)
479 	*cmode = CMODE_16;
480     else if (var->bits_per_pixel <= 32)
481 	*cmode = CMODE_32;
482     else
483 	return -EINVAL;
484 
485     for (map = mac_modes; map->vmode != -1; map++) {
486 	mode = map->mode;
487 	if (var->xres > mode->xres || var->yres > mode->yres)
488 	    continue;
489 	if (var->xres_virtual > mode->xres || var->yres_virtual > mode->yres)
490 	    continue;
491 	if (var->pixclock > mode->pixclock)
492 	    continue;
493 	if ((var->vmode & FB_VMODE_MASK) != mode->vmode)
494 	    continue;
495 	*vmode = map->vmode;
496 	return 0;
497     }
498     return -EINVAL;
499 }
500 
501 
502 /**
503  *	mac_map_monitor_sense - Convert monitor sense to vmode
504  *	@sense: Macintosh monitor sense number
505  *
506  *	Converts a Macintosh monitor sense number to a MacOS
507  *	vmode number.
508  *
509  *	Returns MacOS vmode video mode number.
510  *
511  */
512 
mac_map_monitor_sense(int sense)513 int mac_map_monitor_sense(int sense)
514 {
515     const struct monitor_map *map;
516 
517     for (map = mac_monitors; map->sense != -1; map++)
518 	if (map->sense == sense)
519 	    break;
520     return map->vmode;
521 }
522 
523 
524 /**
525  *	mac_find_mode - find a video mode
526  *	@var: frame buffer user defined part of display
527  *	@info: frame buffer info structure
528  *	@mode_option: video mode name (see mac_modedb[])
529  *	@default_bpp: default color depth in bits per pixel
530  *
531  *	Finds a suitable video mode.  Tries to set mode specified
532  *	by @mode_option.  If the name of the wanted mode begins with
533  *	'mac', the Mac video mode database will be used, otherwise it
534  *	will fall back to the standard video mode database.
535  *
536  *	Note: Function marked as __init and can only be used during
537  *	system boot.
538  *
539  *	Returns error code from fb_find_mode (see fb_find_mode
540  *	function).
541  *
542  */
543 
mac_find_mode(struct fb_var_screeninfo * var,struct fb_info * info,const char * mode_option,unsigned int default_bpp)544 int __init mac_find_mode(struct fb_var_screeninfo *var, struct fb_info *info,
545 			 const char *mode_option, unsigned int default_bpp)
546 {
547     const struct fb_videomode *db = NULL;
548     unsigned int dbsize = 0;
549 
550     if (mode_option && !strncmp(mode_option, "mac", 3)) {
551 	db = mac_modedb;
552 	dbsize = sizeof(mac_modedb)/sizeof(*mac_modedb);
553     }
554     return fb_find_mode(var, info, mode_option, db, dbsize,
555 			&mac_modedb[DEFAULT_MODEDB_INDEX], default_bpp);
556 }
557