mm: don't include asm/pgtable.h if linux/mm.h is already included
[platform/kernel/linux-starfive.git] / drivers / video / fbdev / fb-puv3.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Frame Buffer Driver for PKUnity-v3 Unigfx
4  * Code specific to PKUnity SoC and UniCore ISA
5  *
6  *      Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
7  *      Copyright (C) 2001-2010 Guan Xuetao
8  */
9
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
15 #include <linux/fb.h>
16 #include <linux/init.h>
17 #include <linux/console.h>
18 #include <linux/mm.h>
19
20 #include <linux/sizes.h>
21 #include <mach/hardware.h>
22
23 /* Platform_data reserved for unifb registers. */
24 #define UNIFB_REGS_NUM          10
25 /* RAM reserved for the frame buffer. */
26 #define UNIFB_MEMSIZE           (SZ_4M)         /* 4 MB for 1024*768*32b */
27
28 /*
29  * cause UNIGFX don not have EDID
30  * all the modes are organized as follow
31  */
32 static const struct fb_videomode unifb_modes[] = {
33         /* 0 640x480-60 VESA */
34         { "640x480@60",  60,  640, 480,  25175000,  48, 16, 34, 10,  96, 1,
35           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
36         /* 1 640x480-75 VESA */
37         { "640x480@75",  75,  640, 480,  31500000, 120, 16, 18,  1,  64, 1,
38           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
39         /* 2 800x600-60 VESA */
40         { "800x600@60",  60,  800, 600,  40000000,  88, 40, 26,  1, 128, 1,
41           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
42         /* 3 800x600-75 VESA */
43         { "800x600@75",  75,  800, 600,  49500000, 160, 16, 23,  1,  80, 1,
44           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
45         /* 4 1024x768-60 VESA */
46         { "1024x768@60", 60, 1024, 768,  65000000, 160, 24, 34,  3, 136, 1,
47           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
48         /* 5 1024x768-75 VESA */
49         { "1024x768@75", 75, 1024, 768,  78750000, 176, 16, 30,  1,  96, 1,
50           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
51         /* 6 1280x960-60 VESA */
52         { "1280x960@60", 60, 1280, 960, 108000000, 312, 96, 38,  1, 112, 1,
53           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
54         /* 7 1440x900-60 VESA */
55         { "1440x900@60", 60, 1440, 900, 106500000, 232, 80, 30,  3, 152, 1,
56           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
57         /* 8 FIXME 9 1024x600-60 VESA UNTESTED */
58         { "1024x600@60", 60, 1024, 600,  50650000, 160, 24, 26,  1, 136, 1,
59           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
60         /* 9 FIXME 10 1024x600-75 VESA UNTESTED */
61         { "1024x600@75", 75, 1024, 600,  61500000, 176, 16, 23,  1,  96, 1,
62           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
63         /* 10 FIXME 11 1366x768-60 VESA UNTESTED */
64         { "1366x768@60", 60, 1366, 768,  85500000, 256, 58, 18,  1,  112, 3,
65           0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
66 };
67
68 static const struct fb_var_screeninfo unifb_default = {
69         .xres =         640,
70         .yres =         480,
71         .xres_virtual = 640,
72         .yres_virtual = 480,
73         .bits_per_pixel = 16,
74         .red =          { 11, 5, 0 },
75         .green =        { 5,  6, 0 },
76         .blue =         { 0,  5, 0 },
77         .activate =     FB_ACTIVATE_NOW,
78         .height =       -1,
79         .width =        -1,
80         .pixclock =     25175000,
81         .left_margin =  48,
82         .right_margin = 16,
83         .upper_margin = 33,
84         .lower_margin = 10,
85         .hsync_len =    96,
86         .vsync_len =    2,
87         .vmode =        FB_VMODE_NONINTERLACED,
88 };
89
90 static struct fb_fix_screeninfo unifb_fix = {
91         .id =           "UNIGFX FB",
92         .type =         FB_TYPE_PACKED_PIXELS,
93         .visual =       FB_VISUAL_TRUECOLOR,
94         .xpanstep =     1,
95         .ypanstep =     1,
96         .ywrapstep =    1,
97         .accel =        FB_ACCEL_NONE,
98 };
99
100 static void unifb_sync(struct fb_info *info)
101 {
102         /* TODO: may, this can be replaced by interrupt */
103         int cnt;
104
105         for (cnt = 0; cnt < 0x10000000; cnt++) {
106                 if (readl(UGE_COMMAND) & 0x1000000)
107                         return;
108         }
109
110         if (cnt > 0x8000000)
111                 dev_warn(info->device, "Warning: UniGFX GE time out ...\n");
112 }
113
114 static void unifb_prim_fillrect(struct fb_info *info,
115                                 const struct fb_fillrect *region)
116 {
117         int awidth = region->width;
118         int aheight = region->height;
119         int m_iBpp = info->var.bits_per_pixel;
120         int screen_width = info->var.xres;
121         int src_sel = 1;        /* from fg_color */
122         int pat_sel = 1;
123         int src_x0 = 0;
124         int dst_x0 = region->dx;
125         int src_y0 = 0;
126         int dst_y0 = region->dy;
127         int rop_alpha_sel = 0;
128         int rop_alpha_code = 0xCC;
129         int x_dir = 1;
130         int y_dir = 1;
131         int alpha_r = 0;
132         int alpha_sel = 0;
133         int dst_pitch = screen_width * (m_iBpp / 8);
134         int dst_offset = dst_y0 * dst_pitch + dst_x0 * (m_iBpp / 8);
135         int src_pitch = screen_width * (m_iBpp / 8);
136         int src_offset = src_y0 * src_pitch + src_x0 * (m_iBpp / 8);
137         unsigned int command = 0;
138         int clip_region = 0;
139         int clip_en = 0;
140         int tp_en = 0;
141         int fg_color = 0;
142         int bottom = info->var.yres - 1;
143         int right = info->var.xres - 1;
144         int top = 0;
145
146         bottom = (bottom << 16) | right;
147         command = (rop_alpha_sel << 26) | (pat_sel << 18) | (src_sel << 16)
148                 | (x_dir << 20) | (y_dir << 21) | (command << 24)
149                 | (clip_region << 23) | (clip_en << 22) | (tp_en << 27);
150         src_pitch = (dst_pitch << 16) | src_pitch;
151         awidth = awidth | (aheight << 16);
152         alpha_r = ((rop_alpha_code & 0xff) << 8) | (alpha_r & 0xff)
153                 | (alpha_sel << 16);
154         src_x0 = (src_x0 & 0x1fff) | ((src_y0 & 0x1fff) << 16);
155         dst_x0 = (dst_x0 & 0x1fff) | ((dst_y0 & 0x1fff) << 16);
156         fg_color = region->color;
157
158         unifb_sync(info);
159
160         writel(((u32 *)(info->pseudo_palette))[fg_color], UGE_FCOLOR);
161         writel(0, UGE_BCOLOR);
162         writel(src_pitch, UGE_PITCH);
163         writel(src_offset, UGE_SRCSTART);
164         writel(dst_offset, UGE_DSTSTART);
165         writel(awidth, UGE_WIDHEIGHT);
166         writel(top, UGE_CLIP0);
167         writel(bottom, UGE_CLIP1);
168         writel(alpha_r, UGE_ROPALPHA);
169         writel(src_x0, UGE_SRCXY);
170         writel(dst_x0, UGE_DSTXY);
171         writel(command, UGE_COMMAND);
172 }
173
174 static void unifb_fillrect(struct fb_info *info,
175                 const struct fb_fillrect *region)
176 {
177         struct fb_fillrect modded;
178         int vxres, vyres;
179
180         if (info->flags & FBINFO_HWACCEL_DISABLED) {
181                 sys_fillrect(info, region);
182                 return;
183         }
184
185         vxres = info->var.xres_virtual;
186         vyres = info->var.yres_virtual;
187
188         memcpy(&modded, region, sizeof(struct fb_fillrect));
189
190         if (!modded.width || !modded.height ||
191             modded.dx >= vxres || modded.dy >= vyres)
192                 return;
193
194         if (modded.dx + modded.width > vxres)
195                 modded.width = vxres - modded.dx;
196         if (modded.dy + modded.height > vyres)
197                 modded.height = vyres - modded.dy;
198
199         unifb_prim_fillrect(info, &modded);
200 }
201
202 static void unifb_prim_copyarea(struct fb_info *info,
203                                 const struct fb_copyarea *area)
204 {
205         int awidth = area->width;
206         int aheight = area->height;
207         int m_iBpp = info->var.bits_per_pixel;
208         int screen_width = info->var.xres;
209         int src_sel = 2;        /* from mem */
210         int pat_sel = 0;
211         int src_x0 = area->sx;
212         int dst_x0 = area->dx;
213         int src_y0 = area->sy;
214         int dst_y0 = area->dy;
215
216         int rop_alpha_sel = 0;
217         int rop_alpha_code = 0xCC;
218         int x_dir = 1;
219         int y_dir = 1;
220
221         int alpha_r = 0;
222         int alpha_sel = 0;
223         int dst_pitch = screen_width * (m_iBpp / 8);
224         int dst_offset = dst_y0 * dst_pitch + dst_x0 * (m_iBpp / 8);
225         int src_pitch = screen_width * (m_iBpp / 8);
226         int src_offset = src_y0 * src_pitch + src_x0 * (m_iBpp / 8);
227         unsigned int command = 0;
228         int clip_region = 0;
229         int clip_en = 1;
230         int tp_en = 0;
231         int top = 0;
232         int bottom = info->var.yres;
233         int right = info->var.xres;
234         int fg_color = 0;
235         int bg_color = 0;
236
237         if (src_x0 < 0)
238                 src_x0 = 0;
239         if (src_y0 < 0)
240                 src_y0 = 0;
241
242         if (src_y0 - dst_y0 > 0) {
243                 y_dir = 1;
244         } else {
245                 y_dir = 0;
246                 src_offset = (src_y0 + aheight) * src_pitch +
247                                 src_x0 * (m_iBpp / 8);
248                 dst_offset = (dst_y0 + aheight) * dst_pitch +
249                                 dst_x0 * (m_iBpp / 8);
250                 src_y0 += aheight;
251                 dst_y0 += aheight;
252         }
253
254         command = (rop_alpha_sel << 26) | (pat_sel << 18) | (src_sel << 16) |
255                 (x_dir << 20) | (y_dir << 21) | (command << 24) |
256                 (clip_region << 23) | (clip_en << 22) | (tp_en << 27);
257         src_pitch = (dst_pitch << 16) | src_pitch;
258         awidth = awidth | (aheight << 16);
259         alpha_r = ((rop_alpha_code & 0xff) << 8) | (alpha_r & 0xff) |
260                 (alpha_sel << 16);
261         src_x0 = (src_x0 & 0x1fff) | ((src_y0 & 0x1fff) << 16);
262         dst_x0 = (dst_x0 & 0x1fff) | ((dst_y0 & 0x1fff) << 16);
263         bottom = (bottom << 16) | right;
264
265         unifb_sync(info);
266
267         writel(src_pitch, UGE_PITCH);
268         writel(src_offset, UGE_SRCSTART);
269         writel(dst_offset, UGE_DSTSTART);
270         writel(awidth, UGE_WIDHEIGHT);
271         writel(top, UGE_CLIP0);
272         writel(bottom, UGE_CLIP1);
273         writel(bg_color, UGE_BCOLOR);
274         writel(fg_color, UGE_FCOLOR);
275         writel(alpha_r, UGE_ROPALPHA);
276         writel(src_x0, UGE_SRCXY);
277         writel(dst_x0, UGE_DSTXY);
278         writel(command, UGE_COMMAND);
279 }
280
281 static void unifb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
282 {
283         struct fb_copyarea modded;
284         u32 vxres, vyres;
285         modded.sx = area->sx;
286         modded.sy = area->sy;
287         modded.dx = area->dx;
288         modded.dy = area->dy;
289         modded.width = area->width;
290         modded.height = area->height;
291
292         if (info->flags & FBINFO_HWACCEL_DISABLED) {
293                 sys_copyarea(info, area);
294                 return;
295         }
296
297         vxres = info->var.xres_virtual;
298         vyres = info->var.yres_virtual;
299
300         if (!modded.width || !modded.height ||
301             modded.sx >= vxres || modded.sy >= vyres ||
302             modded.dx >= vxres || modded.dy >= vyres)
303                 return;
304
305         if (modded.sx + modded.width > vxres)
306                 modded.width = vxres - modded.sx;
307         if (modded.dx + modded.width > vxres)
308                 modded.width = vxres - modded.dx;
309         if (modded.sy + modded.height > vyres)
310                 modded.height = vyres - modded.sy;
311         if (modded.dy + modded.height > vyres)
312                 modded.height = vyres - modded.dy;
313
314         unifb_prim_copyarea(info, &modded);
315 }
316
317 static void unifb_imageblit(struct fb_info *info, const struct fb_image *image)
318 {
319         sys_imageblit(info, image);
320 }
321
322 static u_long get_line_length(int xres_virtual, int bpp)
323 {
324         u_long length;
325
326         length = xres_virtual * bpp;
327         length = (length + 31) & ~31;
328         length >>= 3;
329         return length;
330 }
331
332 /*
333  *  Setting the video mode has been split into two parts.
334  *  First part, xxxfb_check_var, must not write anything
335  *  to hardware, it should only verify and adjust var.
336  *  This means it doesn't alter par but it does use hardware
337  *  data from it to check this var.
338  */
339 static int unifb_check_var(struct fb_var_screeninfo *var,
340                          struct fb_info *info)
341 {
342         u_long line_length;
343
344         /*
345          *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
346          *  as FB_VMODE_SMOOTH_XPAN is only used internally
347          */
348
349         if (var->vmode & FB_VMODE_CONUPDATE) {
350                 var->vmode |= FB_VMODE_YWRAP;
351                 var->xoffset = info->var.xoffset;
352                 var->yoffset = info->var.yoffset;
353         }
354
355         /*
356          *  Some very basic checks
357          */
358         if (!var->xres)
359                 var->xres = 1;
360         if (!var->yres)
361                 var->yres = 1;
362         if (var->xres > var->xres_virtual)
363                 var->xres_virtual = var->xres;
364         if (var->yres > var->yres_virtual)
365                 var->yres_virtual = var->yres;
366         if (var->bits_per_pixel <= 1)
367                 var->bits_per_pixel = 1;
368         else if (var->bits_per_pixel <= 8)
369                 var->bits_per_pixel = 8;
370         else if (var->bits_per_pixel <= 16)
371                 var->bits_per_pixel = 16;
372         else if (var->bits_per_pixel <= 24)
373                 var->bits_per_pixel = 24;
374         else if (var->bits_per_pixel <= 32)
375                 var->bits_per_pixel = 32;
376         else
377                 return -EINVAL;
378
379         if (var->xres_virtual < var->xoffset + var->xres)
380                 var->xres_virtual = var->xoffset + var->xres;
381         if (var->yres_virtual < var->yoffset + var->yres)
382                 var->yres_virtual = var->yoffset + var->yres;
383
384         /*
385          *  Memory limit
386          */
387         line_length =
388             get_line_length(var->xres_virtual, var->bits_per_pixel);
389         if (line_length * var->yres_virtual > UNIFB_MEMSIZE)
390                 return -ENOMEM;
391
392         /*
393          * Now that we checked it we alter var. The reason being is that the
394          * video mode passed in might not work but slight changes to it might
395          * make it work. This way we let the user know what is acceptable.
396          */
397         switch (var->bits_per_pixel) {
398         case 1:
399         case 8:
400                 var->red.offset = 0;
401                 var->red.length = 8;
402                 var->green.offset = 0;
403                 var->green.length = 8;
404                 var->blue.offset = 0;
405                 var->blue.length = 8;
406                 var->transp.offset = 0;
407                 var->transp.length = 0;
408                 break;
409         case 16:                /* RGBA 5551 */
410                 if (var->transp.length) {
411                         var->red.offset = 0;
412                         var->red.length = 5;
413                         var->green.offset = 5;
414                         var->green.length = 5;
415                         var->blue.offset = 10;
416                         var->blue.length = 5;
417                         var->transp.offset = 15;
418                         var->transp.length = 1;
419                 } else {        /* RGB 565 */
420                         var->red.offset = 11;
421                         var->red.length = 5;
422                         var->green.offset = 5;
423                         var->green.length = 6;
424                         var->blue.offset = 0;
425                         var->blue.length = 5;
426                         var->transp.offset = 0;
427                         var->transp.length = 0;
428                 }
429                 break;
430         case 24:                /* RGB 888 */
431                 var->red.offset = 0;
432                 var->red.length = 8;
433                 var->green.offset = 8;
434                 var->green.length = 8;
435                 var->blue.offset = 16;
436                 var->blue.length = 8;
437                 var->transp.offset = 0;
438                 var->transp.length = 0;
439                 break;
440         case 32:                /* RGBA 8888 */
441                 var->red.offset = 16;
442                 var->red.length = 8;
443                 var->green.offset = 8;
444                 var->green.length = 8;
445                 var->blue.offset = 0;
446                 var->blue.length = 8;
447                 var->transp.offset = 24;
448                 var->transp.length = 8;
449                 break;
450         }
451         var->red.msb_right = 0;
452         var->green.msb_right = 0;
453         var->blue.msb_right = 0;
454         var->transp.msb_right = 0;
455
456         return 0;
457 }
458
459 /*
460  * This routine actually sets the video mode. It's in here where we
461  * the hardware state info->par and fix which can be affected by the
462  * change in par. For this driver it doesn't do much.
463  */
464 static int unifb_set_par(struct fb_info *info)
465 {
466         int hTotal, vTotal, hSyncStart, hSyncEnd, vSyncStart, vSyncEnd;
467         int format;
468
469 #ifdef CONFIG_PUV3_PM
470         struct clk *clk_vga;
471         u32 pixclk = 0;
472         int i;
473
474         for (i = 0; i <= 10; i++) {
475                 if    (info->var.xres         == unifb_modes[i].xres
476                     && info->var.yres         == unifb_modes[i].yres
477                     && info->var.upper_margin == unifb_modes[i].upper_margin
478                     && info->var.lower_margin == unifb_modes[i].lower_margin
479                     && info->var.left_margin  == unifb_modes[i].left_margin
480                     && info->var.right_margin == unifb_modes[i].right_margin
481                     && info->var.hsync_len    == unifb_modes[i].hsync_len
482                     && info->var.vsync_len    == unifb_modes[i].vsync_len) {
483                         pixclk = unifb_modes[i].pixclock;
484                         break;
485                 }
486         }
487
488         /* set clock rate */
489         clk_vga = clk_get(info->device, "VGA_CLK");
490         if (clk_vga == ERR_PTR(-ENOENT))
491                 return -ENOENT;
492
493         if (pixclk != 0) {
494                 if (clk_set_rate(clk_vga, pixclk)) { /* set clock failed */
495                         info->fix = unifb_fix;
496                         info->var = unifb_default;
497                         if (clk_set_rate(clk_vga, unifb_default.pixclock))
498                                 return -EINVAL;
499                 }
500         }
501 #endif
502
503         info->fix.line_length = get_line_length(info->var.xres_virtual,
504                                                 info->var.bits_per_pixel);
505
506         hSyncStart = info->var.xres + info->var.right_margin;
507         hSyncEnd = hSyncStart + info->var.hsync_len;
508         hTotal = hSyncEnd + info->var.left_margin;
509
510         vSyncStart = info->var.yres + info->var.lower_margin;
511         vSyncEnd = vSyncStart + info->var.vsync_len;
512         vTotal = vSyncEnd + info->var.upper_margin;
513
514         switch (info->var.bits_per_pixel) {
515         case 8:
516                 format = UDE_CFG_DST8;
517                 break;
518         case 16:
519                 format = UDE_CFG_DST16;
520                 break;
521         case 24:
522                 format = UDE_CFG_DST24;
523                 break;
524         case 32:
525                 format = UDE_CFG_DST32;
526                 break;
527         default:
528                 return -EINVAL;
529         }
530
531         writel(info->fix.smem_start, UDE_FSA);
532         writel(info->var.yres, UDE_LS);
533         writel(get_line_length(info->var.xres,
534                         info->var.bits_per_pixel) >> 3, UDE_PS);
535                         /* >> 3 for hardware required. */
536         writel((hTotal << 16) | (info->var.xres), UDE_HAT);
537         writel(((hTotal - 1) << 16) | (info->var.xres - 1), UDE_HBT);
538         writel(((hSyncEnd - 1) << 16) | (hSyncStart - 1), UDE_HST);
539         writel((vTotal << 16) | (info->var.yres), UDE_VAT);
540         writel(((vTotal - 1) << 16) | (info->var.yres - 1), UDE_VBT);
541         writel(((vSyncEnd - 1) << 16) | (vSyncStart - 1), UDE_VST);
542         writel(UDE_CFG_GDEN_ENABLE | UDE_CFG_TIMEUP_ENABLE
543                         | format | 0xC0000001, UDE_CFG);
544
545         return 0;
546 }
547
548 /*
549  *  Set a single color register. The values supplied are already
550  *  rounded down to the hardware's capabilities (according to the
551  *  entries in the var structure). Return != 0 for invalid regno.
552  */
553 static int unifb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
554                          u_int transp, struct fb_info *info)
555 {
556         if (regno >= 256)       /* no. of hw registers */
557                 return 1;
558
559         /* grayscale works only partially under directcolor */
560         if (info->var.grayscale) {
561                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
562                 red = green = blue =
563                     (red * 77 + green * 151 + blue * 28) >> 8;
564         }
565
566 #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
567         switch (info->fix.visual) {
568         case FB_VISUAL_TRUECOLOR:
569         case FB_VISUAL_PSEUDOCOLOR:
570                 red = CNVT_TOHW(red, info->var.red.length);
571                 green = CNVT_TOHW(green, info->var.green.length);
572                 blue = CNVT_TOHW(blue, info->var.blue.length);
573                 transp = CNVT_TOHW(transp, info->var.transp.length);
574                 break;
575         case FB_VISUAL_DIRECTCOLOR:
576                 red = CNVT_TOHW(red, 8);        /* expect 8 bit DAC */
577                 green = CNVT_TOHW(green, 8);
578                 blue = CNVT_TOHW(blue, 8);
579                 /* hey, there is bug in transp handling... */
580                 transp = CNVT_TOHW(transp, 8);
581                 break;
582         }
583 #undef CNVT_TOHW
584         /* Truecolor has hardware independent palette */
585         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
586                 u32 v;
587
588                 if (regno >= 16)
589                         return 1;
590
591                 v = (red << info->var.red.offset) |
592                     (green << info->var.green.offset) |
593                     (blue << info->var.blue.offset) |
594                     (transp << info->var.transp.offset);
595                 switch (info->var.bits_per_pixel) {
596                 case 8:
597                         break;
598                 case 16:
599                 case 24:
600                 case 32:
601                         ((u32 *) (info->pseudo_palette))[regno] = v;
602                         break;
603                 default:
604                         return 1;
605                 }
606                 return 0;
607         }
608         return 0;
609 }
610
611 /*
612  *  Pan or Wrap the Display
613  *
614  *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
615  */
616 static int unifb_pan_display(struct fb_var_screeninfo *var,
617                            struct fb_info *info)
618 {
619         if (var->vmode & FB_VMODE_YWRAP) {
620                 if (var->yoffset < 0
621                     || var->yoffset >= info->var.yres_virtual
622                     || var->xoffset)
623                         return -EINVAL;
624         } else {
625                 if (var->xoffset + info->var.xres > info->var.xres_virtual ||
626                     var->yoffset + info->var.yres > info->var.yres_virtual)
627                         return -EINVAL;
628         }
629         info->var.xoffset = var->xoffset;
630         info->var.yoffset = var->yoffset;
631         if (var->vmode & FB_VMODE_YWRAP)
632                 info->var.vmode |= FB_VMODE_YWRAP;
633         else
634                 info->var.vmode &= ~FB_VMODE_YWRAP;
635         return 0;
636 }
637
638 int unifb_mmap(struct fb_info *info,
639                     struct vm_area_struct *vma)
640 {
641         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
642
643         return vm_iomap_memory(vma, info->fix.smem_start, info->fix.smem_len);
644 }
645
646 static const struct fb_ops unifb_ops = {
647         .fb_read        = fb_sys_read,
648         .fb_write       = fb_sys_write,
649         .fb_check_var   = unifb_check_var,
650         .fb_set_par     = unifb_set_par,
651         .fb_setcolreg   = unifb_setcolreg,
652         .fb_pan_display = unifb_pan_display,
653         .fb_fillrect    = unifb_fillrect,
654         .fb_copyarea    = unifb_copyarea,
655         .fb_imageblit   = unifb_imageblit,
656         .fb_mmap        = unifb_mmap,
657 };
658
659 /*
660  *  Initialisation
661  */
662 static int unifb_probe(struct platform_device *dev)
663 {
664         struct fb_info *info;
665         u32 unifb_regs[UNIFB_REGS_NUM];
666         int retval = -ENOMEM;
667         struct resource *iomem;
668         void *videomemory;
669
670         videomemory = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP,
671                                 get_order(UNIFB_MEMSIZE));
672         if (!videomemory)
673                 goto err;
674
675         memset(videomemory, 0, UNIFB_MEMSIZE);
676
677         unifb_fix.smem_start = virt_to_phys(videomemory);
678         unifb_fix.smem_len = UNIFB_MEMSIZE;
679
680         iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
681         unifb_fix.mmio_start = iomem->start;
682
683         info = framebuffer_alloc(sizeof(u32)*256, &dev->dev);
684         if (!info)
685                 goto err;
686
687         info->screen_base = (char __iomem *)videomemory;
688         info->fbops = &unifb_ops;
689
690         retval = fb_find_mode(&info->var, info, NULL,
691                               unifb_modes, 10, &unifb_modes[0], 16);
692
693         if (!retval || (retval == 4))
694                 info->var = unifb_default;
695
696         info->fix = unifb_fix;
697         info->pseudo_palette = info->par;
698         info->par = NULL;
699         info->flags = FBINFO_FLAG_DEFAULT;
700 #ifdef FB_ACCEL_PUV3_UNIGFX
701         info->fix.accel = FB_ACCEL_PUV3_UNIGFX;
702 #endif
703
704         retval = fb_alloc_cmap(&info->cmap, 256, 0);
705         if (retval < 0)
706                 goto err1;
707
708         retval = register_framebuffer(info);
709         if (retval < 0)
710                 goto err2;
711         platform_set_drvdata(dev, info);
712         platform_device_add_data(dev, unifb_regs, sizeof(u32) * UNIFB_REGS_NUM);
713
714         fb_info(info, "Virtual frame buffer device, using %dM of video memory\n",
715                 UNIFB_MEMSIZE >> 20);
716         return 0;
717 err2:
718         fb_dealloc_cmap(&info->cmap);
719 err1:
720         framebuffer_release(info);
721 err:
722         return retval;
723 }
724
725 static int unifb_remove(struct platform_device *dev)
726 {
727         struct fb_info *info = platform_get_drvdata(dev);
728
729         if (info) {
730                 unregister_framebuffer(info);
731                 fb_dealloc_cmap(&info->cmap);
732                 framebuffer_release(info);
733         }
734         return 0;
735 }
736
737 #ifdef CONFIG_PM
738 static int unifb_resume(struct platform_device *dev)
739 {
740         int rc = 0;
741         u32 *unifb_regs = dev->dev.platform_data;
742
743         if (dev->dev.power.power_state.event == PM_EVENT_ON)
744                 return 0;
745
746         console_lock();
747
748         if (dev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
749                 writel(unifb_regs[0], UDE_FSA);
750                 writel(unifb_regs[1], UDE_LS);
751                 writel(unifb_regs[2], UDE_PS);
752                 writel(unifb_regs[3], UDE_HAT);
753                 writel(unifb_regs[4], UDE_HBT);
754                 writel(unifb_regs[5], UDE_HST);
755                 writel(unifb_regs[6], UDE_VAT);
756                 writel(unifb_regs[7], UDE_VBT);
757                 writel(unifb_regs[8], UDE_VST);
758                 writel(unifb_regs[9], UDE_CFG);
759         }
760         dev->dev.power.power_state = PMSG_ON;
761
762         console_unlock();
763
764         return rc;
765 }
766
767 static int unifb_suspend(struct platform_device *dev, pm_message_t mesg)
768 {
769         u32 *unifb_regs = dev->dev.platform_data;
770
771         unifb_regs[0] = readl(UDE_FSA);
772         unifb_regs[1] = readl(UDE_LS);
773         unifb_regs[2] = readl(UDE_PS);
774         unifb_regs[3] = readl(UDE_HAT);
775         unifb_regs[4] = readl(UDE_HBT);
776         unifb_regs[5] = readl(UDE_HST);
777         unifb_regs[6] = readl(UDE_VAT);
778         unifb_regs[7] = readl(UDE_VBT);
779         unifb_regs[8] = readl(UDE_VST);
780         unifb_regs[9] = readl(UDE_CFG);
781
782         if (mesg.event == dev->dev.power.power_state.event)
783                 return 0;
784
785         switch (mesg.event) {
786         case PM_EVENT_FREEZE:           /* about to take snapshot */
787         case PM_EVENT_PRETHAW:          /* before restoring snapshot */
788                 goto done;
789         }
790
791         console_lock();
792
793         /* do nothing... */
794
795         console_unlock();
796
797 done:
798         dev->dev.power.power_state = mesg;
799
800         return 0;
801 }
802 #else
803 #define unifb_resume    NULL
804 #define unifb_suspend   NULL
805 #endif
806
807 static struct platform_driver unifb_driver = {
808         .probe   = unifb_probe,
809         .remove  = unifb_remove,
810         .resume  = unifb_resume,
811         .suspend = unifb_suspend,
812         .driver  = {
813                 .name   = "PKUnity-v3-UNIGFX",
814         },
815 };
816
817 static int __init unifb_init(void)
818 {
819 #ifndef MODULE
820         if (fb_get_options("unifb", NULL))
821                 return -ENODEV;
822 #endif
823
824         return platform_driver_register(&unifb_driver);
825 }
826
827 module_init(unifb_init);
828
829 static void __exit unifb_exit(void)
830 {
831         platform_driver_unregister(&unifb_driver);
832 }
833
834 module_exit(unifb_exit);
835
836 MODULE_LICENSE("GPL v2");