xref: /openbmc/linux/drivers/video/fbdev/kyro/fbdev.c (revision 5c816641)
1 /*
2  *  linux/drivers/video/kyro/fbdev.c
3  *
4  *  Copyright (C) 2002 STMicroelectronics
5  *  Copyright (C) 2003, 2004 Paul Mundt
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file COPYING in the main directory of this archive
9  * for more details.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/delay.h>
19 #include <linux/fb.h>
20 #include <linux/ioctl.h>
21 #include <linux/init.h>
22 #include <linux/pci.h>
23 #include <asm/io.h>
24 #include <linux/uaccess.h>
25 
26 #include <video/kyro.h>
27 
28 #include "STG4000Reg.h"
29 #include "STG4000Interface.h"
30 
31 /*
32  * PCI Definitions
33  */
34 #define PCI_VENDOR_ID_ST	0x104a
35 #define PCI_DEVICE_ID_STG4000	0x0010
36 
37 #define KHZ2PICOS(a) (1000000000UL/(a))
38 
39 /****************************************************************************/
40 static struct fb_fix_screeninfo kyro_fix = {
41 	.id		= "ST Kyro",
42 	.type		= FB_TYPE_PACKED_PIXELS,
43 	.visual		= FB_VISUAL_TRUECOLOR,
44 	.accel		= FB_ACCEL_NONE,
45 };
46 
47 static const struct fb_var_screeninfo kyro_var = {
48 	/* 640x480, 16bpp @ 60 Hz */
49 	.xres		= 640,
50 	.yres		= 480,
51 	.xres_virtual	= 640,
52 	.yres_virtual	= 480,
53 	.bits_per_pixel	= 16,
54 	.red		= { 11, 5, 0 },
55 	.green		= {  5, 6, 0 },
56 	.blue		= {  0, 5, 0 },
57 	.activate	= FB_ACTIVATE_NOW,
58 	.height		= -1,
59 	.width		= -1,
60 	.pixclock	= KHZ2PICOS(25175),
61 	.left_margin	= 48,
62 	.right_margin	= 16,
63 	.upper_margin	= 33,
64 	.lower_margin	= 10,
65 	.hsync_len	= 96,
66 	.vsync_len	= 2,
67 	.vmode		= FB_VMODE_NONINTERLACED,
68 };
69 
70 typedef struct {
71 	STG4000REG __iomem *pSTGReg;	/* Virtual address of PCI register region */
72 	u32 ulNextFreeVidMem;	/* Offset from start of vid mem to next free region */
73 	u32 ulOverlayOffset;	/* Offset from start of vid mem to overlay */
74 	u32 ulOverlayStride;	/* Interleaved YUV and 422 mode Y stride */
75 	u32 ulOverlayUVStride;	/* 422 mode U & V stride */
76 } device_info_t;
77 
78 /* global graphics card info structure (one per card) */
79 static device_info_t deviceInfo;
80 
81 static char *mode_option = NULL;
82 static int nopan = 0;
83 static int nowrap = 1;
84 static int nomtrr = 0;
85 
86 /* PCI driver prototypes */
87 static int kyrofb_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
88 static void kyrofb_remove(struct pci_dev *pdev);
89 
90 static struct fb_videomode kyro_modedb[] = {
91 	{
92 		/* 640x350 @ 85Hz */
93 		NULL, 85, 640, 350, KHZ2PICOS(31500),
94 		96, 32, 60, 32, 64, 3,
95 		FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED
96 	}, {
97 		/* 640x400 @ 85Hz */
98 		NULL, 85, 640, 400, KHZ2PICOS(31500),
99 		96, 32, 41, 1, 64, 3,
100 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
101 	}, {
102 		/* 720x400 @ 85Hz */
103 		NULL, 85, 720, 400, KHZ2PICOS(35500),
104 		108, 36, 42, 1, 72, 3,
105 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
106 	}, {
107 		/* 640x480 @ 60Hz */
108 		NULL, 60, 640, 480, KHZ2PICOS(25175),
109 		48, 16, 33, 10, 96, 2,
110 		0, FB_VMODE_NONINTERLACED
111 	}, {
112 		/* 640x480 @ 72Hz */
113 		NULL, 72, 640, 480, KHZ2PICOS(31500),
114 		128, 24, 28, 9, 40, 3,
115 		0, FB_VMODE_NONINTERLACED
116 	}, {
117 		/* 640x480 @ 75Hz */
118 		NULL, 75, 640, 480, KHZ2PICOS(31500),
119 		120, 16, 16, 1, 64, 3,
120 		0, FB_VMODE_NONINTERLACED
121 	}, {
122 		/* 640x480 @ 85Hz */
123 		NULL, 85, 640, 480, KHZ2PICOS(36000),
124 		80, 56, 25, 1, 56, 3,
125 		0, FB_VMODE_NONINTERLACED
126 	}, {
127 		/* 800x600 @ 56Hz */
128 		NULL, 56, 800, 600, KHZ2PICOS(36000),
129 		128, 24, 22, 1, 72, 2,
130 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
131 	}, {
132 		/* 800x600 @ 60Hz */
133 		NULL, 60, 800, 600, KHZ2PICOS(40000),
134 		88, 40, 23, 1, 128, 4,
135 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
136 	}, {
137 		/* 800x600 @ 72Hz */
138 		NULL, 72, 800, 600, KHZ2PICOS(50000),
139 		64, 56, 23, 37, 120, 6,
140 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
141 	}, {
142 		/* 800x600 @ 75Hz */
143 		NULL, 75, 800, 600, KHZ2PICOS(49500),
144 		160, 16, 21, 1, 80, 3,
145 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
146 	}, {
147 		/* 800x600 @ 85Hz */
148 		NULL, 85, 800, 600, KHZ2PICOS(56250),
149 		152, 32, 27, 1, 64, 3,
150 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
151 	}, {
152 		/* 1024x768 @ 60Hz */
153 		NULL, 60, 1024, 768, KHZ2PICOS(65000),
154 		160, 24, 29, 3, 136, 6,
155 		0, FB_VMODE_NONINTERLACED
156 	}, {
157 		/* 1024x768 @ 70Hz */
158 		NULL, 70, 1024, 768, KHZ2PICOS(75000),
159 		144, 24, 29, 3, 136, 6,
160 		0, FB_VMODE_NONINTERLACED
161 	}, {
162 		/* 1024x768 @ 75Hz */
163 		NULL, 75, 1024, 768, KHZ2PICOS(78750),
164 		176, 16, 28, 1, 96, 3,
165 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
166 	}, {
167 		/* 1024x768 @ 85Hz */
168 		NULL, 85, 1024, 768, KHZ2PICOS(94500),
169 		208, 48, 36, 1, 96, 3,
170 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
171 	}, {
172 		/* 1152x864 @ 75Hz */
173 		NULL, 75, 1152, 864, KHZ2PICOS(108000),
174 		256, 64, 32, 1, 128, 3,
175 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
176 	}, {
177 		/* 1280x960 @ 60Hz */
178 		NULL, 60, 1280, 960, KHZ2PICOS(108000),
179 		312, 96, 36, 1, 112, 3,
180 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
181 	}, {
182 		/* 1280x960 @ 85Hz */
183 		NULL, 85, 1280, 960, KHZ2PICOS(148500),
184 		224, 64, 47, 1, 160, 3,
185 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
186 	}, {
187 		/* 1280x1024 @ 60Hz */
188 		NULL, 60, 1280, 1024, KHZ2PICOS(108000),
189 		248, 48, 38, 1, 112, 3,
190 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
191 	}, {
192 		/* 1280x1024 @ 75Hz */
193 		NULL, 75, 1280, 1024, KHZ2PICOS(135000),
194 		248, 16, 38, 1, 144, 3,
195 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
196 	}, {
197 		/* 1280x1024 @ 85Hz */
198 		NULL, 85, 1280, 1024, KHZ2PICOS(157500),
199 		224, 64, 44, 1, 160, 3,
200 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
201 	}, {
202 		/* 1600x1200 @ 60Hz */
203 		NULL, 60, 1600, 1200, KHZ2PICOS(162000),
204 		304, 64, 46, 1, 192, 3,
205 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
206 	}, {
207 		/* 1600x1200 @ 65Hz */
208 		NULL, 65, 1600, 1200, KHZ2PICOS(175500),
209 		304, 64, 46, 1, 192, 3,
210 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
211 	}, {
212 		/* 1600x1200 @ 70Hz */
213 		NULL, 70, 1600, 1200, KHZ2PICOS(189000),
214 		304, 64, 46, 1, 192, 3,
215 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
216 	}, {
217 		/* 1600x1200 @ 75Hz */
218 		NULL, 75, 1600, 1200, KHZ2PICOS(202500),
219 		304, 64, 46, 1, 192, 3,
220 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
221 	}, {
222 		/* 1600x1200 @ 85Hz */
223 		NULL, 85, 1600, 1200, KHZ2PICOS(229500),
224 		304, 64, 46, 1, 192, 3,
225 		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
226 	}, {
227 		/* 1792x1344 @ 60Hz */
228 		NULL, 60, 1792, 1344, KHZ2PICOS(204750),
229 		328, 128, 46, 1, 200, 3,
230 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
231 	}, {
232 		/* 1792x1344 @ 75Hz */
233 		NULL, 75, 1792, 1344, KHZ2PICOS(261000),
234 		352, 96, 69, 1, 216, 3,
235 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
236 	}, {
237 		/* 1856x1392 @ 60Hz */
238 		NULL, 60, 1856, 1392, KHZ2PICOS(218250),
239 		352, 96, 43, 1, 224, 3,
240 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
241 	}, {
242 		/* 1856x1392 @ 75Hz */
243 		NULL, 75, 1856, 1392, KHZ2PICOS(288000),
244 		352, 128, 104, 1, 224, 3,
245 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
246 	}, {
247 		/* 1920x1440 @ 60Hz */
248 		NULL, 60, 1920, 1440, KHZ2PICOS(234000),
249 		344, 128, 56, 1, 208, 3,
250 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
251 	}, {
252 		/* 1920x1440 @ 75Hz */
253 		NULL, 75, 1920, 1440, KHZ2PICOS(297000),
254 		352, 144, 56, 1, 224, 3,
255 		FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
256 	},
257 };
258 #define NUM_TOTAL_MODES	ARRAY_SIZE(kyro_modedb)
259 
260 /*
261  * This needs to be kept ordered corresponding to kyro_modedb.
262  */
263 enum {
264 	VMODE_640_350_85,
265 	VMODE_640_400_85,
266 	VMODE_720_400_85,
267 	VMODE_640_480_60,
268 	VMODE_640_480_72,
269 	VMODE_640_480_75,
270 	VMODE_640_480_85,
271 	VMODE_800_600_56,
272 	VMODE_800_600_60,
273 	VMODE_800_600_72,
274 	VMODE_800_600_75,
275 	VMODE_800_600_85,
276 	VMODE_1024_768_60,
277 	VMODE_1024_768_70,
278 	VMODE_1024_768_75,
279 	VMODE_1024_768_85,
280 	VMODE_1152_864_75,
281 	VMODE_1280_960_60,
282 	VMODE_1280_960_85,
283 	VMODE_1280_1024_60,
284 	VMODE_1280_1024_75,
285 	VMODE_1280_1024_85,
286 	VMODE_1600_1200_60,
287 	VMODE_1600_1200_65,
288 	VMODE_1600_1200_70,
289 	VMODE_1600_1200_75,
290 	VMODE_1600_1200_85,
291 	VMODE_1792_1344_60,
292 	VMODE_1792_1344_75,
293 	VMODE_1856_1392_60,
294 	VMODE_1856_1392_75,
295 	VMODE_1920_1440_60,
296 	VMODE_1920_1440_75,
297 };
298 
299 /* Accessors */
300 static int kyro_dev_video_mode_set(struct fb_info *info)
301 {
302 	struct kyrofb_info *par = info->par;
303 
304 	/* Turn off display */
305 	StopVTG(deviceInfo.pSTGReg);
306 	DisableRamdacOutput(deviceInfo.pSTGReg);
307 
308 	/* Bring us out of VGA and into Hi-Res mode, if not already. */
309 	DisableVGA(deviceInfo.pSTGReg);
310 
311 	if (InitialiseRamdac(deviceInfo.pSTGReg,
312 			     info->var.bits_per_pixel,
313 			     info->var.xres, info->var.yres,
314 			     par->HSP, par->VSP, &par->PIXCLK) < 0)
315 		return -EINVAL;
316 
317 	SetupVTG(deviceInfo.pSTGReg, par);
318 
319 	ResetOverlayRegisters(deviceInfo.pSTGReg);
320 
321 	/* Turn on display in new mode */
322 	EnableRamdacOutput(deviceInfo.pSTGReg);
323 	StartVTG(deviceInfo.pSTGReg);
324 
325 	deviceInfo.ulNextFreeVidMem = info->var.xres * info->var.yres *
326 				      info->var.bits_per_pixel;
327 	deviceInfo.ulOverlayOffset = 0;
328 
329 	return 0;
330 }
331 
332 static int kyro_dev_overlay_create(u32 ulWidth,
333 				   u32 ulHeight, int bLinear)
334 {
335 	u32 offset;
336 	u32 stride, uvStride;
337 
338 	if (deviceInfo.ulOverlayOffset != 0)
339 		/*
340 		 * Can only create one overlay without resetting the card or
341 		 * changing display mode
342 		 */
343 		return -EINVAL;
344 
345 	ResetOverlayRegisters(deviceInfo.pSTGReg);
346 
347 	/* Overlays are addressed in multiples of 16bytes or 32bytes, so make
348 	 * sure the start offset is on an appropriate boundary.
349 	 */
350 	offset = deviceInfo.ulNextFreeVidMem;
351 	if ((offset & 0x1f) != 0) {
352 		offset = (offset + 32L) & 0xffffffE0L;
353 	}
354 
355 	if (CreateOverlaySurface(deviceInfo.pSTGReg, ulWidth, ulHeight,
356 				 bLinear, offset, &stride, &uvStride) < 0)
357 		return -EINVAL;
358 
359 	deviceInfo.ulOverlayOffset = offset;
360 	deviceInfo.ulOverlayStride = stride;
361 	deviceInfo.ulOverlayUVStride = uvStride;
362 	deviceInfo.ulNextFreeVidMem = offset + (ulHeight * stride) + (ulHeight * 2 * uvStride);
363 
364 	SetOverlayBlendMode(deviceInfo.pSTGReg, GLOBAL_ALPHA, 0xf, 0x0);
365 
366 	return 0;
367 }
368 
369 static int kyro_dev_overlay_viewport_set(u32 x, u32 y, u32 ulWidth, u32 ulHeight)
370 {
371 	if (deviceInfo.ulOverlayOffset == 0)
372 		/* probably haven't called CreateOverlay yet */
373 		return -EINVAL;
374 
375 	if (ulWidth == 0 || ulWidth == 0xffffffff ||
376 	    ulHeight == 0 || ulHeight == 0xffffffff ||
377 	    (x < 2 && ulWidth + 2 == 0))
378 		return -EINVAL;
379 
380 	/* Stop Ramdac Output */
381 	DisableRamdacOutput(deviceInfo.pSTGReg);
382 
383 	SetOverlayViewPort(deviceInfo.pSTGReg,
384 			   x, y, x + ulWidth - 1, y + ulHeight - 1);
385 
386 	EnableOverlayPlane(deviceInfo.pSTGReg);
387 	/* Start Ramdac Output */
388 	EnableRamdacOutput(deviceInfo.pSTGReg);
389 
390 	return 0;
391 }
392 
393 static inline unsigned long get_line_length(int x, int bpp)
394 {
395 	return (unsigned long)((((x*bpp)+31)&~31) >> 3);
396 }
397 
398 static int kyrofb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
399 {
400 	struct kyrofb_info *par = info->par;
401 
402 	if (!var->pixclock)
403 		return -EINVAL;
404 
405 	if (var->bits_per_pixel != 16 && var->bits_per_pixel != 32) {
406 		printk(KERN_WARNING "kyrofb: depth not supported: %u\n", var->bits_per_pixel);
407 		return -EINVAL;
408 	}
409 
410 	switch (var->bits_per_pixel) {
411 	case 16:
412 		var->red.offset = 11;
413 		var->red.length = 5;
414 		var->green.offset = 5;
415 		var->green.length = 6;
416 		var->blue.length = 5;
417 		break;
418 	case 32:
419 		var->transp.offset = 24;
420 		var->red.offset = 16;
421 		var->green.offset = 8;
422 		var->blue.offset = 0;
423 
424 		var->red.length = 8;
425 		var->green.length = 8;
426 		var->blue.length = 8;
427 		var->transp.length = 8;
428 		break;
429 	}
430 
431 	/* Height/Width of picture in mm */
432 	var->height = var->width = -1;
433 
434 	/* Timing information. All values are in picoseconds */
435 
436 	/* par->PIXCLK is in 100Hz units. Convert to picoseconds -
437 	 * ensuring we do not exceed 32 bit precision
438 	 */
439 	/*
440 	 * XXX: Enabling this really screws over the pixclock value when we
441 	 * read it back with fbset. As such, leaving this commented out appears
442 	 * to do the right thing (at least for now) .. bearing in mind that we
443 	 * have infact already done the KHZ2PICOS conversion in both the modedb
444 	 * and kyro_var. -- PFM.
445 	 */
446 //	var->pixclock = 1000000000 / (par->PIXCLK / 10);
447 
448 	/* the header file claims we should use picoseconds
449 	 * - nobody else does though, the all use pixels and lines
450 	 * of h and v sizes. Both options here.
451 	 */
452 
453 	/*
454 	 * If we're being called by __fb_try_mode(), then we don't want to
455 	 * override any of the var settings that we've already parsed
456 	 * from our modedb. -- PFM.
457 	 */
458 	if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST)
459 		return 0;
460 
461 	var->left_margin = par->HBP;
462 	var->hsync_len = par->HST;
463 	var->right_margin = par->HFP;
464 
465 	var->upper_margin = par->VBP;
466 	var->vsync_len = par->VST;
467 	var->lower_margin = par->VFP;
468 
469 	if (par->HSP == 1)
470 		var->sync |= FB_SYNC_HOR_HIGH_ACT;
471 	if (par->VSP == 1)
472 		var->sync |= FB_SYNC_VERT_HIGH_ACT;
473 
474 	return 0;
475 }
476 
477 static int kyrofb_set_par(struct fb_info *info)
478 {
479 	struct kyrofb_info *par = info->par;
480 	unsigned long lineclock;
481 	unsigned long frameclock;
482 
483 	/* Actual resolution */
484 	par->XRES = info->var.xres;
485 	par->YRES = info->var.yres;
486 
487 	/* pixel depth */
488 	par->PIXDEPTH = info->var.bits_per_pixel;
489 
490 	/* Refresh rate */
491 	/* time for a line in ns */
492 	lineclock = (info->var.pixclock * (info->var.xres +
493 				    info->var.right_margin +
494 				    info->var.hsync_len +
495 				    info->var.left_margin)) / 1000;
496 
497 
498 	/* time for a frame in ns (precision in 32bpp) */
499 	frameclock = lineclock * (info->var.yres +
500 				  info->var.lower_margin +
501 				  info->var.vsync_len +
502 				  info->var.upper_margin);
503 
504 	/* Calculate refresh rate and horrizontal clocks */
505 	par->VFREQ = (1000000000 + (frameclock / 2)) / frameclock;
506 	par->HCLK = (1000000000 + (lineclock / 2)) / lineclock;
507 	par->PIXCLK = ((1000000000 + (info->var.pixclock / 2))
508 					/ info->var.pixclock) * 10;
509 
510 	/* calculate horizontal timings */
511 	par->HFP = info->var.right_margin;
512 	par->HST = info->var.hsync_len;
513 	par->HBP = info->var.left_margin;
514 	par->HTot = par->XRES + par->HBP + par->HST + par->HFP;
515 
516 	/* calculate vertical timings */
517 	par->VFP = info->var.lower_margin;
518 	par->VST = info->var.vsync_len;
519 	par->VBP = info->var.upper_margin;
520 	par->VTot = par->YRES + par->VBP + par->VST + par->VFP;
521 
522 	par->HSP = (info->var.sync & FB_SYNC_HOR_HIGH_ACT) ? 1 : 0;
523 	par->VSP = (info->var.sync & FB_SYNC_VERT_HIGH_ACT) ? 1 : 0;
524 
525 	kyro_dev_video_mode_set(info);
526 
527 	/* length of a line in bytes    */
528 	info->fix.line_length = get_line_length(par->XRES, par->PIXDEPTH);
529 	info->fix.visual = FB_VISUAL_TRUECOLOR;
530 
531 	return 0;
532 }
533 
534 static int kyrofb_setcolreg(u_int regno, u_int red, u_int green,
535 			    u_int blue, u_int transp, struct fb_info *info)
536 {
537 	struct kyrofb_info *par = info->par;
538 
539 	if (regno > 255)
540 		return 1;	/* Invalid register */
541 
542 	if (regno < 16) {
543 		switch (info->var.bits_per_pixel) {
544 		case 16:
545 			par->palette[regno] =
546 			     (red   & 0xf800) |
547 			    ((green & 0xfc00) >> 5) |
548 			    ((blue  & 0xf800) >> 11);
549 			break;
550 		case 32:
551 			red >>= 8; green >>= 8; blue >>= 8; transp >>= 8;
552 			par->palette[regno] =
553 			    (transp << 24) | (red << 16) | (green << 8) | blue;
554 			break;
555 		}
556 	}
557 
558 	return 0;
559 }
560 
561 #ifndef MODULE
562 static int __init kyrofb_setup(char *options)
563 {
564 	char *this_opt;
565 
566 	if (!options || !*options)
567 		return 0;
568 
569 	while ((this_opt = strsep(&options, ","))) {
570 		if (!*this_opt)
571 			continue;
572 		if (strcmp(this_opt, "nopan") == 0) {
573 			nopan = 1;
574 		} else if (strcmp(this_opt, "nowrap") == 0) {
575 			nowrap = 1;
576 		} else if (strcmp(this_opt, "nomtrr") == 0) {
577 			nomtrr = 1;
578 		} else {
579 			mode_option = this_opt;
580 		}
581 	}
582 
583 	return 0;
584 }
585 #endif
586 
587 static int kyrofb_ioctl(struct fb_info *info,
588 			unsigned int cmd, unsigned long arg)
589 {
590 	overlay_create ol_create;
591 	overlay_viewport_set ol_viewport_set;
592 	void __user *argp = (void __user *)arg;
593 
594 	switch (cmd) {
595 	case KYRO_IOCTL_OVERLAY_CREATE:
596 		if (copy_from_user(&ol_create, argp, sizeof(overlay_create)))
597 			return -EFAULT;
598 
599 		if (kyro_dev_overlay_create(ol_create.ulWidth,
600 					    ol_create.ulHeight, 0) < 0) {
601 			printk(KERN_ERR "Kyro FB: failed to create overlay surface.\n");
602 
603 			return -EINVAL;
604 		}
605 		break;
606 	case KYRO_IOCTL_OVERLAY_VIEWPORT_SET:
607 		if (copy_from_user(&ol_viewport_set, argp,
608 			       sizeof(overlay_viewport_set)))
609 			return -EFAULT;
610 
611 		if (kyro_dev_overlay_viewport_set(ol_viewport_set.xOrgin,
612 						  ol_viewport_set.yOrgin,
613 						  ol_viewport_set.xSize,
614 						  ol_viewport_set.ySize) != 0)
615 		{
616 			printk(KERN_ERR "Kyro FB: failed to create overlay viewport.\n");
617 			return -EINVAL;
618 		}
619 		break;
620 	case KYRO_IOCTL_SET_VIDEO_MODE:
621 		{
622 			printk(KERN_ERR "Kyro FB: KYRO_IOCTL_SET_VIDEO_MODE is"
623 				"obsolete, use the appropriate fb_ioctl()"
624 				"command instead.\n");
625 			return -EINVAL;
626 		}
627 	case KYRO_IOCTL_UVSTRIDE:
628 		if (copy_to_user(argp, &deviceInfo.ulOverlayUVStride, sizeof(deviceInfo.ulOverlayUVStride)))
629 			return -EFAULT;
630 		break;
631 	case KYRO_IOCTL_STRIDE:
632 		if (copy_to_user(argp, &deviceInfo.ulOverlayStride, sizeof(deviceInfo.ulOverlayStride)))
633 			return -EFAULT;
634 		break;
635 	case KYRO_IOCTL_OVERLAY_OFFSET:
636 		if (copy_to_user(argp, &deviceInfo.ulOverlayOffset, sizeof(deviceInfo.ulOverlayOffset)))
637 			return -EFAULT;
638 		break;
639 	}
640 
641 	return 0;
642 }
643 
644 static const struct pci_device_id kyrofb_pci_tbl[] = {
645 	{ PCI_VENDOR_ID_ST, PCI_DEVICE_ID_STG4000,
646 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
647 	{ 0, }
648 };
649 
650 MODULE_DEVICE_TABLE(pci, kyrofb_pci_tbl);
651 
652 static struct pci_driver kyrofb_pci_driver = {
653 	.name		= "kyrofb",
654 	.id_table	= kyrofb_pci_tbl,
655 	.probe		= kyrofb_probe,
656 	.remove		= kyrofb_remove,
657 };
658 
659 static const struct fb_ops kyrofb_ops = {
660 	.owner		= THIS_MODULE,
661 	.fb_check_var	= kyrofb_check_var,
662 	.fb_set_par	= kyrofb_set_par,
663 	.fb_setcolreg	= kyrofb_setcolreg,
664 	.fb_ioctl	= kyrofb_ioctl,
665 	.fb_fillrect	= cfb_fillrect,
666 	.fb_copyarea	= cfb_copyarea,
667 	.fb_imageblit	= cfb_imageblit,
668 };
669 
670 static int kyrofb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
671 {
672 	struct fb_info *info;
673 	struct kyrofb_info *currentpar;
674 	unsigned long size;
675 	int err;
676 
677 	if ((err = pci_enable_device(pdev))) {
678 		printk(KERN_WARNING "kyrofb: Can't enable pdev: %d\n", err);
679 		return err;
680 	}
681 
682 	info = framebuffer_alloc(sizeof(struct kyrofb_info), &pdev->dev);
683 	if (!info)
684 		return -ENOMEM;
685 
686 	currentpar = info->par;
687 
688 	kyro_fix.smem_start = pci_resource_start(pdev, 0);
689 	kyro_fix.smem_len   = pci_resource_len(pdev, 0);
690 	kyro_fix.mmio_start = pci_resource_start(pdev, 1);
691 	kyro_fix.mmio_len   = pci_resource_len(pdev, 1);
692 
693 	currentpar->regbase = deviceInfo.pSTGReg =
694 		ioremap(kyro_fix.mmio_start, kyro_fix.mmio_len);
695 	if (!currentpar->regbase)
696 		goto out_free_fb;
697 
698 	info->screen_base = pci_ioremap_wc_bar(pdev, 0);
699 	if (!info->screen_base)
700 		goto out_unmap_regs;
701 
702 	if (!nomtrr)
703 		currentpar->wc_cookie = arch_phys_wc_add(kyro_fix.smem_start,
704 							 kyro_fix.smem_len);
705 
706 	kyro_fix.ypanstep	= nopan ? 0 : 1;
707 	kyro_fix.ywrapstep	= nowrap ? 0 : 1;
708 
709 	info->fbops		= &kyrofb_ops;
710 	info->fix		= kyro_fix;
711 	info->pseudo_palette	= currentpar->palette;
712 	info->flags		= FBINFO_DEFAULT;
713 
714 	SetCoreClockPLL(deviceInfo.pSTGReg, pdev);
715 
716 	deviceInfo.ulNextFreeVidMem = 0;
717 	deviceInfo.ulOverlayOffset = 0;
718 
719 	/* This should give a reasonable default video mode */
720 	if (!fb_find_mode(&info->var, info, mode_option, kyro_modedb,
721 			  NUM_TOTAL_MODES, &kyro_modedb[VMODE_1024_768_75], 32))
722 		info->var = kyro_var;
723 
724 	fb_alloc_cmap(&info->cmap, 256, 0);
725 
726 	kyrofb_set_par(info);
727 	kyrofb_check_var(&info->var, info);
728 
729 	size = get_line_length(info->var.xres_virtual,
730 			       info->var.bits_per_pixel);
731 	size *= info->var.yres_virtual;
732 
733 	fb_memset(info->screen_base, 0, size);
734 
735 	if (register_framebuffer(info) < 0)
736 		goto out_unmap;
737 
738 	fb_info(info, "%s frame buffer device, at %dx%d@%d using %ldk/%ldk of VRAM\n",
739 		info->fix.id,
740 		info->var.xres, info->var.yres, info->var.bits_per_pixel,
741 		size >> 10, (unsigned long)info->fix.smem_len >> 10);
742 
743 	pci_set_drvdata(pdev, info);
744 
745 	return 0;
746 
747 out_unmap:
748 	iounmap(info->screen_base);
749 out_unmap_regs:
750 	iounmap(currentpar->regbase);
751 out_free_fb:
752 	framebuffer_release(info);
753 
754 	return -EINVAL;
755 }
756 
757 static void kyrofb_remove(struct pci_dev *pdev)
758 {
759 	struct fb_info *info = pci_get_drvdata(pdev);
760 	struct kyrofb_info *par = info->par;
761 
762 	/* Reset the board */
763 	StopVTG(deviceInfo.pSTGReg);
764 	DisableRamdacOutput(deviceInfo.pSTGReg);
765 
766 	/* Sync up the PLL */
767 	SetCoreClockPLL(deviceInfo.pSTGReg, pdev);
768 
769 	deviceInfo.ulNextFreeVidMem = 0;
770 	deviceInfo.ulOverlayOffset = 0;
771 
772 	iounmap(info->screen_base);
773 	iounmap(par->regbase);
774 
775 	arch_phys_wc_del(par->wc_cookie);
776 
777 	unregister_framebuffer(info);
778 	framebuffer_release(info);
779 }
780 
781 static int __init kyrofb_init(void)
782 {
783 #ifndef MODULE
784 	char *option = NULL;
785 
786 	if (fb_get_options("kyrofb", &option))
787 		return -ENODEV;
788 	kyrofb_setup(option);
789 #endif
790 	return pci_register_driver(&kyrofb_pci_driver);
791 }
792 
793 static void __exit kyrofb_exit(void)
794 {
795 	pci_unregister_driver(&kyrofb_pci_driver);
796 }
797 
798 module_init(kyrofb_init);
799 
800 #ifdef MODULE
801 module_exit(kyrofb_exit);
802 #endif
803 
804 MODULE_AUTHOR("STMicroelectronics; Paul Mundt <lethal@linux-sh.org>");
805 MODULE_LICENSE("GPL");
806