xref: /openbmc/linux/drivers/video/fbdev/68328fb.c (revision 2a598d0b)
1 /*
2  *  linux/drivers/video/68328fb.c -- Low level implementation of the
3  *                                   mc68x328 LCD frame buffer device
4  *
5  *	Copyright (C) 2003 Georges Menie
6  *
7  *  This driver assumes an already configured controller (e.g. from config.c)
8  *  Keep the code clean of board specific initialization.
9  *
10  *  This code has not been tested with colors, colormap management functions
11  *  are minimal (no colormap data written to the 68328 registers...)
12  *
13  *  initial version of this driver:
14  *    Copyright (C) 1998,1999 Kenneth Albanowski <kjahds@kjahds.com>,
15  *                            The Silver Hammer Group, Ltd.
16  *
17  *  this version is based on :
18  *
19  *  linux/drivers/video/vfb.c -- Virtual frame buffer device
20  *
21  *      Copyright (C) 2002 James Simmons
22  *
23  *	Copyright (C) 1997 Geert Uytterhoeven
24  *
25  *  This file is subject to the terms and conditions of the GNU General Public
26  *  License. See the file COPYING in the main directory of this archive for
27  *  more details.
28  */
29 
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/string.h>
34 #include <linux/mm.h>
35 #include <linux/vmalloc.h>
36 #include <linux/delay.h>
37 #include <linux/interrupt.h>
38 #include <linux/uaccess.h>
39 #include <linux/fb.h>
40 #include <linux/init.h>
41 
42 #if defined(CONFIG_M68VZ328)
43 #include <asm/MC68VZ328.h>
44 #elif defined(CONFIG_M68EZ328)
45 #include <asm/MC68EZ328.h>
46 #elif defined(CONFIG_M68328)
47 #include <asm/MC68328.h>
48 #else
49 #error wrong architecture for the MC68x328 frame buffer device
50 #endif
51 
52 static u_long videomemory;
53 static u_long videomemorysize;
54 
55 static struct fb_info fb_info;
56 static u32 mc68x328fb_pseudo_palette[16];
57 
58 static struct fb_var_screeninfo mc68x328fb_default __initdata = {
59 	.red =		{ 0, 8, 0 },
60       	.green =	{ 0, 8, 0 },
61       	.blue =		{ 0, 8, 0 },
62       	.activate =	FB_ACTIVATE_TEST,
63       	.height =	-1,
64       	.width =	-1,
65       	.pixclock =	20000,
66       	.left_margin =	64,
67       	.right_margin =	64,
68       	.upper_margin =	32,
69       	.lower_margin =	32,
70       	.hsync_len =	64,
71       	.vsync_len =	2,
72       	.vmode =	FB_VMODE_NONINTERLACED,
73 };
74 
75 static const struct fb_fix_screeninfo mc68x328fb_fix __initconst = {
76 	.id =		"68328fb",
77 	.type =		FB_TYPE_PACKED_PIXELS,
78 	.xpanstep =	1,
79 	.ypanstep =	1,
80 	.ywrapstep =	1,
81 	.accel =	FB_ACCEL_NONE,
82 };
83 
84     /*
85      *  Interface used by the world
86      */
87 static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
88 			 struct fb_info *info);
89 static int mc68x328fb_set_par(struct fb_info *info);
90 static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
91 			 u_int transp, struct fb_info *info);
92 static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
93 			   struct fb_info *info);
94 static int mc68x328fb_mmap(struct fb_info *info, struct vm_area_struct *vma);
95 
96 static const struct fb_ops mc68x328fb_ops = {
97 	.owner		= THIS_MODULE,
98 	.fb_check_var	= mc68x328fb_check_var,
99 	.fb_set_par	= mc68x328fb_set_par,
100 	.fb_setcolreg	= mc68x328fb_setcolreg,
101 	.fb_pan_display	= mc68x328fb_pan_display,
102 	.fb_fillrect	= cfb_fillrect,
103 	.fb_copyarea	= cfb_copyarea,
104 	.fb_imageblit	= cfb_imageblit,
105 	.fb_mmap	= mc68x328fb_mmap,
106 };
107 
108     /*
109      *  Internal routines
110      */
111 
112 static u_long get_line_length(int xres_virtual, int bpp)
113 {
114 	u_long length;
115 
116 	length = xres_virtual * bpp;
117 	length = (length + 31) & ~31;
118 	length >>= 3;
119 	return (length);
120 }
121 
122     /*
123      *  Setting the video mode has been split into two parts.
124      *  First part, xxxfb_check_var, must not write anything
125      *  to hardware, it should only verify and adjust var.
126      *  This means it doesn't alter par but it does use hardware
127      *  data from it to check this var.
128      */
129 
130 static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
131 			 struct fb_info *info)
132 {
133 	u_long line_length;
134 
135 	/*
136 	 *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
137 	 *  as FB_VMODE_SMOOTH_XPAN is only used internally
138 	 */
139 
140 	if (var->vmode & FB_VMODE_CONUPDATE) {
141 		var->vmode |= FB_VMODE_YWRAP;
142 		var->xoffset = info->var.xoffset;
143 		var->yoffset = info->var.yoffset;
144 	}
145 
146 	/*
147 	 *  Some very basic checks
148 	 */
149 	if (!var->xres)
150 		var->xres = 1;
151 	if (!var->yres)
152 		var->yres = 1;
153 	if (var->xres > var->xres_virtual)
154 		var->xres_virtual = var->xres;
155 	if (var->yres > var->yres_virtual)
156 		var->yres_virtual = var->yres;
157 	if (var->bits_per_pixel <= 1)
158 		var->bits_per_pixel = 1;
159 	else if (var->bits_per_pixel <= 8)
160 		var->bits_per_pixel = 8;
161 	else if (var->bits_per_pixel <= 16)
162 		var->bits_per_pixel = 16;
163 	else if (var->bits_per_pixel <= 24)
164 		var->bits_per_pixel = 24;
165 	else if (var->bits_per_pixel <= 32)
166 		var->bits_per_pixel = 32;
167 	else
168 		return -EINVAL;
169 
170 	if (var->xres_virtual < var->xoffset + var->xres)
171 		var->xres_virtual = var->xoffset + var->xres;
172 	if (var->yres_virtual < var->yoffset + var->yres)
173 		var->yres_virtual = var->yoffset + var->yres;
174 
175 	/*
176 	 *  Memory limit
177 	 */
178 	line_length =
179 	    get_line_length(var->xres_virtual, var->bits_per_pixel);
180 	if (line_length * var->yres_virtual > videomemorysize)
181 		return -ENOMEM;
182 
183 	/*
184 	 * Now that we checked it we alter var. The reason being is that the video
185 	 * mode passed in might not work but slight changes to it might make it
186 	 * work. This way we let the user know what is acceptable.
187 	 */
188 	switch (var->bits_per_pixel) {
189 	case 1:
190 		var->red.offset = 0;
191 		var->red.length = 1;
192 		var->green.offset = 0;
193 		var->green.length = 1;
194 		var->blue.offset = 0;
195 		var->blue.length = 1;
196 		var->transp.offset = 0;
197 		var->transp.length = 0;
198 		break;
199 	case 8:
200 		var->red.offset = 0;
201 		var->red.length = 8;
202 		var->green.offset = 0;
203 		var->green.length = 8;
204 		var->blue.offset = 0;
205 		var->blue.length = 8;
206 		var->transp.offset = 0;
207 		var->transp.length = 0;
208 		break;
209 	case 16:		/* RGBA 5551 */
210 		if (var->transp.length) {
211 			var->red.offset = 0;
212 			var->red.length = 5;
213 			var->green.offset = 5;
214 			var->green.length = 5;
215 			var->blue.offset = 10;
216 			var->blue.length = 5;
217 			var->transp.offset = 15;
218 			var->transp.length = 1;
219 		} else {	/* RGB 565 */
220 			var->red.offset = 0;
221 			var->red.length = 5;
222 			var->green.offset = 5;
223 			var->green.length = 6;
224 			var->blue.offset = 11;
225 			var->blue.length = 5;
226 			var->transp.offset = 0;
227 			var->transp.length = 0;
228 		}
229 		break;
230 	case 24:		/* RGB 888 */
231 		var->red.offset = 0;
232 		var->red.length = 8;
233 		var->green.offset = 8;
234 		var->green.length = 8;
235 		var->blue.offset = 16;
236 		var->blue.length = 8;
237 		var->transp.offset = 0;
238 		var->transp.length = 0;
239 		break;
240 	case 32:		/* RGBA 8888 */
241 		var->red.offset = 0;
242 		var->red.length = 8;
243 		var->green.offset = 8;
244 		var->green.length = 8;
245 		var->blue.offset = 16;
246 		var->blue.length = 8;
247 		var->transp.offset = 24;
248 		var->transp.length = 8;
249 		break;
250 	}
251 	var->red.msb_right = 0;
252 	var->green.msb_right = 0;
253 	var->blue.msb_right = 0;
254 	var->transp.msb_right = 0;
255 
256 	return 0;
257 }
258 
259 /* This routine actually sets the video mode. It's in here where we
260  * the hardware state info->par and fix which can be affected by the
261  * change in par. For this driver it doesn't do much.
262  */
263 static int mc68x328fb_set_par(struct fb_info *info)
264 {
265 	info->fix.line_length = get_line_length(info->var.xres_virtual,
266 						info->var.bits_per_pixel);
267 	return 0;
268 }
269 
270     /*
271      *  Set a single color register. The values supplied are already
272      *  rounded down to the hardware's capabilities (according to the
273      *  entries in the var structure). Return != 0 for invalid regno.
274      */
275 
276 static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
277 			 u_int transp, struct fb_info *info)
278 {
279 	if (regno >= 256)	/* no. of hw registers */
280 		return 1;
281 	/*
282 	 * Program hardware... do anything you want with transp
283 	 */
284 
285 	/* grayscale works only partially under directcolor */
286 	if (info->var.grayscale) {
287 		/* grayscale = 0.30*R + 0.59*G + 0.11*B */
288 		red = green = blue =
289 		    (red * 77 + green * 151 + blue * 28) >> 8;
290 	}
291 
292 	/* Directcolor:
293 	 *   var->{color}.offset contains start of bitfield
294 	 *   var->{color}.length contains length of bitfield
295 	 *   {hardwarespecific} contains width of RAMDAC
296 	 *   cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
297 	 *   RAMDAC[X] is programmed to (red, green, blue)
298 	 *
299 	 * Pseudocolor:
300 	 *    uses offset = 0 && length = RAMDAC register width.
301 	 *    var->{color}.offset is 0
302 	 *    var->{color}.length contains width of DAC
303 	 *    cmap is not used
304 	 *    RAMDAC[X] is programmed to (red, green, blue)
305 	 * Truecolor:
306 	 *    does not use DAC. Usually 3 are present.
307 	 *    var->{color}.offset contains start of bitfield
308 	 *    var->{color}.length contains length of bitfield
309 	 *    cmap is programmed to (red << red.offset) | (green << green.offset) |
310 	 *                      (blue << blue.offset) | (transp << transp.offset)
311 	 *    RAMDAC does not exist
312 	 */
313 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
314 	switch (info->fix.visual) {
315 	case FB_VISUAL_TRUECOLOR:
316 	case FB_VISUAL_PSEUDOCOLOR:
317 		red = CNVT_TOHW(red, info->var.red.length);
318 		green = CNVT_TOHW(green, info->var.green.length);
319 		blue = CNVT_TOHW(blue, info->var.blue.length);
320 		transp = CNVT_TOHW(transp, info->var.transp.length);
321 		break;
322 	case FB_VISUAL_DIRECTCOLOR:
323 		red = CNVT_TOHW(red, 8);	/* expect 8 bit DAC */
324 		green = CNVT_TOHW(green, 8);
325 		blue = CNVT_TOHW(blue, 8);
326 		/* hey, there is bug in transp handling... */
327 		transp = CNVT_TOHW(transp, 8);
328 		break;
329 	}
330 #undef CNVT_TOHW
331 	/* Truecolor has hardware independent palette */
332 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
333 		u32 v;
334 
335 		if (regno >= 16)
336 			return 1;
337 
338 		v = (red << info->var.red.offset) |
339 		    (green << info->var.green.offset) |
340 		    (blue << info->var.blue.offset) |
341 		    (transp << info->var.transp.offset);
342 		switch (info->var.bits_per_pixel) {
343 		case 8:
344 			break;
345 		case 16:
346 			((u32 *) (info->pseudo_palette))[regno] = v;
347 			break;
348 		case 24:
349 		case 32:
350 			((u32 *) (info->pseudo_palette))[regno] = v;
351 			break;
352 		}
353 		return 0;
354 	}
355 	return 0;
356 }
357 
358     /*
359      *  Pan or Wrap the Display
360      *
361      *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
362      */
363 
364 static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
365 			   struct fb_info *info)
366 {
367 	if (var->vmode & FB_VMODE_YWRAP) {
368 		if (var->yoffset < 0
369 		    || var->yoffset >= info->var.yres_virtual
370 		    || var->xoffset)
371 			return -EINVAL;
372 	} else {
373 		if (var->xoffset + info->var.xres > info->var.xres_virtual ||
374 		    var->yoffset + info->var.yres > info->var.yres_virtual)
375 			return -EINVAL;
376 	}
377 	info->var.xoffset = var->xoffset;
378 	info->var.yoffset = var->yoffset;
379 	if (var->vmode & FB_VMODE_YWRAP)
380 		info->var.vmode |= FB_VMODE_YWRAP;
381 	else
382 		info->var.vmode &= ~FB_VMODE_YWRAP;
383 	return 0;
384 }
385 
386     /*
387      *  Most drivers don't need their own mmap function
388      */
389 
390 static int mc68x328fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
391 {
392 #ifndef MMU
393 	/* this is uClinux (no MMU) specific code */
394 
395 	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
396 	vma->vm_start = videomemory;
397 
398 	return 0;
399 #else
400 	return -EINVAL;
401 #endif
402 }
403 
404 static int __init mc68x328fb_setup(char *options)
405 {
406 	if (!options || !*options)
407 		return 1;
408 	return 1;
409 }
410 
411     /*
412      *  Initialisation
413      */
414 
415 static int __init mc68x328fb_init(void)
416 {
417 #ifndef MODULE
418 	char *option = NULL;
419 
420 	if (fb_get_options("68328fb", &option))
421 		return -ENODEV;
422 	mc68x328fb_setup(option);
423 #endif
424 	/*
425 	 *  initialize the default mode from the LCD controller registers
426 	 */
427 	mc68x328fb_default.xres = LXMAX;
428 	mc68x328fb_default.yres = LYMAX+1;
429 	mc68x328fb_default.xres_virtual = mc68x328fb_default.xres;
430 	mc68x328fb_default.yres_virtual = mc68x328fb_default.yres;
431 	mc68x328fb_default.bits_per_pixel = 1 + (LPICF & 0x01);
432 	videomemory = LSSA;
433 	videomemorysize = (mc68x328fb_default.xres_virtual+7) / 8 *
434 		mc68x328fb_default.yres_virtual * mc68x328fb_default.bits_per_pixel;
435 
436 	fb_info.screen_base = (void *)videomemory;
437 	fb_info.fbops = &mc68x328fb_ops;
438 	fb_info.var = mc68x328fb_default;
439 	fb_info.fix = mc68x328fb_fix;
440 	fb_info.fix.smem_start = videomemory;
441 	fb_info.fix.smem_len = videomemorysize;
442 	fb_info.fix.line_length =
443 		get_line_length(mc68x328fb_default.xres_virtual, mc68x328fb_default.bits_per_pixel);
444 	fb_info.fix.visual = (mc68x328fb_default.bits_per_pixel) == 1 ?
445 		FB_VISUAL_MONO10 : FB_VISUAL_PSEUDOCOLOR;
446 	if (fb_info.var.bits_per_pixel == 1) {
447 		fb_info.var.red.length = fb_info.var.green.length = fb_info.var.blue.length = 1;
448 		fb_info.var.red.offset = fb_info.var.green.offset = fb_info.var.blue.offset = 0;
449 	}
450 	fb_info.pseudo_palette = &mc68x328fb_pseudo_palette;
451 	fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
452 
453 	if (fb_alloc_cmap(&fb_info.cmap, 256, 0))
454 		return -ENOMEM;
455 
456 	if (register_framebuffer(&fb_info) < 0) {
457 		fb_dealloc_cmap(&fb_info.cmap);
458 		return -EINVAL;
459 	}
460 
461 	fb_info(&fb_info, "%s frame buffer device\n", fb_info.fix.id);
462 	fb_info(&fb_info, "%dx%dx%d at 0x%08lx\n",
463 		mc68x328fb_default.xres_virtual,
464 		mc68x328fb_default.yres_virtual,
465 		1 << mc68x328fb_default.bits_per_pixel, videomemory);
466 
467 	return 0;
468 }
469 
470 module_init(mc68x328fb_init);
471 
472 #ifdef MODULE
473 
474 static void __exit mc68x328fb_cleanup(void)
475 {
476 	unregister_framebuffer(&fb_info);
477 	fb_dealloc_cmap(&fb_info.cmap);
478 }
479 
480 module_exit(mc68x328fb_cleanup);
481 
482 MODULE_LICENSE("GPL");
483 #endif				/* MODULE */
484