Pull bugzilla-7570 into release branch
[linux-2.6] / drivers / video / hitfb.c
1 /*
2  * linux/drivers/video/hitfb.c -- Hitachi LCD frame buffer device
3  *
4  * (C) 1999 Mihai Spatar
5  * (C) 2000 YAEGASHI Takeshi
6  * (C) 2003, 2004 Paul Mundt
7  * (C) 2003, 2004, 2006 Andriy Skulysh
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/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/mm.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/fb.h>
24
25 #include <asm/machvec.h>
26 #include <asm/uaccess.h>
27 #include <asm/pgtable.h>
28 #include <asm/io.h>
29 #include <asm/hd64461.h>
30 #include <asm/cpu/dac.h>
31
32 #define WIDTH 640
33
34 static struct fb_var_screeninfo hitfb_var __initdata = {
35         .activate       = FB_ACTIVATE_NOW,
36         .height         = -1,
37         .width          = -1,
38         .vmode          = FB_VMODE_NONINTERLACED,
39 };
40
41 static struct fb_fix_screeninfo hitfb_fix __initdata = {
42         .id             = "Hitachi HD64461",
43         .type           = FB_TYPE_PACKED_PIXELS,
44         .accel          = FB_ACCEL_NONE,
45 };
46
47 static u32 pseudo_palette[16];
48 static struct fb_info fb_info;
49
50 static inline void hitfb_accel_wait(void)
51 {
52         while (fb_readw(HD64461_GRCFGR) & HD64461_GRCFGR_ACCSTATUS) ;
53 }
54
55 static inline void hitfb_accel_start(int truecolor)
56 {
57         if (truecolor) {
58                 fb_writew(6, HD64461_GRCFGR);
59         } else {
60                 fb_writew(7, HD64461_GRCFGR);
61         }
62 }
63
64 static inline void hitfb_accel_set_dest(int truecolor, u16 dx, u16 dy,
65                                         u16 width, u16 height)
66 {
67         u32 saddr = WIDTH * dy + dx;
68         if (truecolor)
69                 saddr <<= 1;
70
71         fb_writew(width-1, HD64461_BBTDWR);
72         fb_writew(height-1, HD64461_BBTDHR);
73
74         fb_writew(saddr & 0xffff, HD64461_BBTDSARL);
75         fb_writew(saddr >> 16, HD64461_BBTDSARH);
76
77 }
78
79 static inline void hitfb_accel_bitblt(int truecolor, u16 sx, u16 sy, u16 dx,
80                                       u16 dy, u16 width, u16 height, u16 rop,
81                                       u32 mask_addr)
82 {
83         u32 saddr, daddr;
84         u32 maddr = 0;
85
86         height--;
87         width--;
88         fb_writew(rop, HD64461_BBTROPR);
89         if ((sy < dy) || ((sy == dy) && (sx <= dx))) {
90                 saddr = WIDTH * (sy + height) + sx + width;
91                 daddr = WIDTH * (dy + height) + dx + width;
92                 if (mask_addr) {
93                         if (truecolor)
94                                 maddr = ((width >> 3) + 1) * (height + 1) - 1;
95                         else
96                                 maddr =
97                                     (((width >> 4) + 1) * (height + 1) - 1) * 2;
98
99                         fb_writew((1 << 5) | 1, HD64461_BBTMDR);
100                 } else
101                         fb_writew(1, HD64461_BBTMDR);
102         } else {
103                 saddr = WIDTH * sy + sx;
104                 daddr = WIDTH * dy + dx;
105                 if (mask_addr) {
106                         fb_writew((1 << 5), HD64461_BBTMDR);
107                 } else {
108                         fb_writew(0, HD64461_BBTMDR);
109                 }
110         }
111         if (truecolor) {
112                 saddr <<= 1;
113                 daddr <<= 1;
114         }
115         fb_writew(width, HD64461_BBTDWR);
116         fb_writew(height, HD64461_BBTDHR);
117         fb_writew(saddr & 0xffff, HD64461_BBTSSARL);
118         fb_writew(saddr >> 16, HD64461_BBTSSARH);
119         fb_writew(daddr & 0xffff, HD64461_BBTDSARL);
120         fb_writew(daddr >> 16, HD64461_BBTDSARH);
121         if (mask_addr) {
122                 maddr += mask_addr;
123                 fb_writew(maddr & 0xffff, HD64461_BBTMARL);
124                 fb_writew(maddr >> 16, HD64461_BBTMARH);
125         }
126         hitfb_accel_start(truecolor);
127 }
128
129 static void hitfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
130 {
131         if (rect->rop != ROP_COPY)
132                 cfb_fillrect(p, rect);
133         else {
134                 hitfb_accel_wait();
135                 fb_writew(0x00f0, HD64461_BBTROPR);
136                 fb_writew(16, HD64461_BBTMDR);
137
138                 if (p->var.bits_per_pixel == 16) {
139                         fb_writew(((u32 *) (p->pseudo_palette))[rect->color],
140                                   HD64461_GRSCR);
141                         hitfb_accel_set_dest(1, rect->dx, rect->dy, rect->width,
142                                              rect->height);
143                         hitfb_accel_start(1);
144                 } else {
145                         fb_writew(rect->color, HD64461_GRSCR);
146                         hitfb_accel_set_dest(0, rect->dx, rect->dy, rect->width,
147                                              rect->height);
148                         hitfb_accel_start(0);
149                 }
150         }
151 }
152
153 static void hitfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
154 {
155         hitfb_accel_wait();
156         hitfb_accel_bitblt(p->var.bits_per_pixel == 16, area->sx, area->sy,
157                            area->dx, area->dy, area->width, area->height,
158                            0x00cc, 0);
159 }
160
161 static int hitfb_pan_display(struct fb_var_screeninfo *var,
162                              struct fb_info *info)
163 {
164         int xoffset = var->xoffset;
165         int yoffset = var->yoffset;
166
167         if (xoffset != 0)
168                 return -EINVAL;
169
170         fb_writew((yoffset*info->fix.line_length)>>10, HD64461_LCDCBAR);
171
172         return 0;
173 }
174
175 int hitfb_blank(int blank_mode, struct fb_info *info)
176 {
177         unsigned short v;
178
179         if (blank_mode) {
180                 v = fb_readw(HD64461_LDR1);
181                 v &= ~HD64461_LDR1_DON;
182                 fb_writew(v, HD64461_LDR1);
183
184                 v = fb_readw(HD64461_LCDCCR);
185                 v |= HD64461_LCDCCR_MOFF;
186                 fb_writew(v, HD64461_LCDCCR);
187
188                 v = fb_readw(HD64461_STBCR);
189                 v |= HD64461_STBCR_SLCDST;
190                 fb_writew(v, HD64461_STBCR);
191         } else {
192                 v = fb_readw(HD64461_STBCR);
193                 v &= ~HD64461_STBCR_SLCDST;
194                 fb_writew(v, HD64461_STBCR);
195
196                 v = fb_readw(HD64461_LCDCCR);
197                 v &= ~(HD64461_LCDCCR_MOFF | HD64461_LCDCCR_STREQ);
198                 fb_writew(v, HD64461_LCDCCR);
199
200                 do {
201                     v = fb_readw(HD64461_LCDCCR);
202                 } while(v&HD64461_LCDCCR_STBACK);
203
204                 v = fb_readw(HD64461_LDR1);
205                 v |= HD64461_LDR1_DON;
206                 fb_writew(v, HD64461_LDR1);
207         }
208         return 0;
209 }
210
211 static int hitfb_setcolreg(unsigned regno, unsigned red, unsigned green,
212                            unsigned blue, unsigned transp, struct fb_info *info)
213 {
214         if (regno >= 256)
215                 return 1;
216
217         switch (info->var.bits_per_pixel) {
218         case 8:
219                 fb_writew(regno << 8, HD64461_CPTWAR);
220                 fb_writew(red >> 10, HD64461_CPTWDR);
221                 fb_writew(green >> 10, HD64461_CPTWDR);
222                 fb_writew(blue >> 10, HD64461_CPTWDR);
223                 break;
224         case 16:
225                 if (regno >= 16)
226                         return 1;
227                 ((u32 *) (info->pseudo_palette))[regno] =
228                     ((red & 0xf800)) |
229                     ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
230                 break;
231         }
232         return 0;
233 }
234
235 static int hitfb_sync(struct fb_info *info)
236 {
237         hitfb_accel_wait();
238
239         return 0;
240 }
241
242 static int hitfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
243 {
244         int maxy;
245
246         var->xres = info->var.xres;
247         var->xres_virtual = info->var.xres;
248         var->yres = info->var.yres;
249
250         if ((var->bits_per_pixel != 8) && (var->bits_per_pixel != 16))
251                 var->bits_per_pixel = info->var.bits_per_pixel;
252
253         if (var->yres_virtual < var->yres)
254                 var->yres_virtual = var->yres;
255
256         maxy = info->fix.smem_len / var->xres;
257
258         if (var->bits_per_pixel == 16)
259                 maxy /= 2;
260
261         if (var->yres_virtual > maxy)
262                 var->yres_virtual = maxy;
263
264         var->xoffset = 0;
265         var->yoffset = 0;
266
267         switch (var->bits_per_pixel) {
268         case 8:
269                 var->red.offset = 0;
270                 var->red.length = 8;
271                 var->green.offset = 0;
272                 var->green.length = 8;
273                 var->blue.offset = 0;
274                 var->blue.length = 8;
275                 var->transp.offset = 0;
276                 var->transp.length = 0;
277                 break;
278         case 16:                /* RGB 565 */
279                 var->red.offset = 11;
280                 var->red.length = 5;
281                 var->green.offset = 5;
282                 var->green.length = 6;
283                 var->blue.offset = 0;
284                 var->blue.length = 5;
285                 var->transp.offset = 0;
286                 var->transp.length = 0;
287                 break;
288         }
289
290         return 0;
291 }
292
293 static int hitfb_set_par(struct fb_info *info)
294 {
295         unsigned short ldr3;
296
297         switch (info->var.bits_per_pixel) {
298         case 8:
299                 info->fix.line_length = info->var.xres;
300                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
301                 info->fix.ypanstep = 16;
302                 break;
303         case 16:
304                 info->fix.line_length = info->var.xres*2;
305                 info->fix.visual = FB_VISUAL_TRUECOLOR;
306                 info->fix.ypanstep = 8;
307                 break;
308         }
309
310         fb_writew(info->fix.line_length, HD64461_LCDCLOR);
311         ldr3 = fb_readw(HD64461_LDR3);
312         ldr3 &= ~15;
313         ldr3 |= (info->var.bits_per_pixel == 8) ? 4 : 8;
314         fb_writew(ldr3, HD64461_LDR3);
315         return 0;
316 }
317
318 static struct fb_ops hitfb_ops = {
319         .owner          = THIS_MODULE,
320         .fb_check_var   = hitfb_check_var,
321         .fb_set_par             = hitfb_set_par,
322         .fb_setcolreg   = hitfb_setcolreg,
323         .fb_blank       = hitfb_blank,
324         .fb_sync        = hitfb_sync,
325         .fb_pan_display = hitfb_pan_display,
326         .fb_fillrect    = hitfb_fillrect,
327         .fb_copyarea    = hitfb_copyarea,
328         .fb_imageblit   = cfb_imageblit,
329 };
330
331 static int __init hitfb_probe(struct platform_device *dev)
332 {
333         unsigned short lcdclor, ldr3, ldvndr;
334
335         if (fb_get_options("hitfb", NULL))
336                 return -ENODEV;
337
338         hitfb_fix.mmio_start = CONFIG_HD64461_IOBASE+0x1000;
339         hitfb_fix.mmio_len = 0x1000;
340         hitfb_fix.smem_start = CONFIG_HD64461_IOBASE + 0x02000000;
341         hitfb_fix.smem_len = 512 * 1024;
342
343         lcdclor = fb_readw(HD64461_LCDCLOR);
344         ldvndr = fb_readw(HD64461_LDVNDR);
345         ldr3 = fb_readw(HD64461_LDR3);
346
347         switch (ldr3 & 15) {
348         default:
349         case 4:
350                 hitfb_var.bits_per_pixel = 8;
351                 hitfb_var.xres = lcdclor;
352                 break;
353         case 8:
354                 hitfb_var.bits_per_pixel = 16;
355                 hitfb_var.xres = lcdclor / 2;
356                 break;
357         }
358         hitfb_fix.line_length = lcdclor;
359         hitfb_fix.visual = (hitfb_var.bits_per_pixel == 8) ?
360             FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
361         hitfb_var.yres = ldvndr + 1;
362         hitfb_var.xres_virtual = hitfb_var.xres;
363         hitfb_var.yres_virtual = hitfb_fix.smem_len / lcdclor;
364         switch (hitfb_var.bits_per_pixel) {
365         case 8:
366                 hitfb_var.red.offset = 0;
367                 hitfb_var.red.length = 8;
368                 hitfb_var.green.offset = 0;
369                 hitfb_var.green.length = 8;
370                 hitfb_var.blue.offset = 0;
371                 hitfb_var.blue.length = 8;
372                 hitfb_var.transp.offset = 0;
373                 hitfb_var.transp.length = 0;
374                 break;
375         case 16:                /* RGB 565 */
376                 hitfb_var.red.offset = 11;
377                 hitfb_var.red.length = 5;
378                 hitfb_var.green.offset = 5;
379                 hitfb_var.green.length = 6;
380                 hitfb_var.blue.offset = 0;
381                 hitfb_var.blue.length = 5;
382                 hitfb_var.transp.offset = 0;
383                 hitfb_var.transp.length = 0;
384                 break;
385         }
386
387         fb_info.fbops = &hitfb_ops;
388         fb_info.var = hitfb_var;
389         fb_info.fix = hitfb_fix;
390         fb_info.pseudo_palette = pseudo_palette;
391         fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
392                 FBINFO_HWACCEL_FILLRECT | FBINFO_HWACCEL_COPYAREA;
393
394         fb_info.screen_base = (void *)hitfb_fix.smem_start;
395
396         fb_alloc_cmap(&fb_info.cmap, 256, 0);
397
398         if (register_framebuffer(&fb_info) < 0)
399                 return -EINVAL;
400
401         printk(KERN_INFO "fb%d: %s frame buffer device\n",
402                fb_info.node, fb_info.fix.id);
403         return 0;
404 }
405
406 static int __devexit hitfb_remove(struct platform_device *dev)
407 {
408         return unregister_framebuffer(&fb_info);
409 }
410
411 #ifdef CONFIG_PM
412 static int hitfb_suspend(struct platform_device *dev, pm_message_t state)
413 {
414         u16 v;
415
416         hitfb_blank(1,0);
417         v = fb_readw(HD64461_STBCR);
418         v |= HD64461_STBCR_SLCKE_IST;
419         fb_writew(v, HD64461_STBCR);
420
421         return 0;
422 }
423
424 static int hitfb_resume(struct platform_device *dev)
425 {
426         u16 v;
427
428         v = fb_readw(HD64461_STBCR);
429         v &= ~HD64461_STBCR_SLCKE_OST;
430         msleep(100);
431         v = fb_readw(HD64461_STBCR);
432         v &= ~HD64461_STBCR_SLCKE_IST;
433         fb_writew(v, HD64461_STBCR);
434         hitfb_blank(0,0);
435
436         return 0;
437 }
438 #endif
439
440 static struct platform_driver hitfb_driver = {
441         .probe          = hitfb_probe,
442         .remove         = __devexit_p(hitfb_remove),
443 #ifdef CONFIG_PM
444         .suspend        = hitfb_suspend,
445         .resume         = hitfb_resume,
446 #endif
447         .driver         = {
448                 .name   = "hitfb",
449         },
450 };
451
452 static struct platform_device hitfb_device = {
453         .name   = "hitfb",
454         .id     = -1,
455 };
456
457 static int __init hitfb_init(void)
458 {
459         int ret;
460
461         ret = platform_driver_register(&hitfb_driver);
462         if (!ret) {
463                 ret = platform_device_register(&hitfb_device);
464                 if (ret)
465                         platform_driver_unregister(&hitfb_driver);
466         }
467         return ret;
468 }
469
470
471 static void __exit hitfb_exit(void)
472 {
473         platform_device_unregister(&hitfb_device);
474         platform_driver_unregister(&hitfb_driver);
475 }
476
477 module_init(hitfb_init);
478 module_exit(hitfb_exit);
479
480 MODULE_LICENSE("GPL");