blob: 73c83a8de2d37a68b35553f7dcc8e18cb5fde890 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Andriy Skulysh048839d2006-09-27 13:47:22 +09007 * (C) 2003, 2004, 2006 Andriy Skulysh
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/string.h>
18#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/init.h>
Andriy Skulysh048839d2006-09-27 13:47:22 +090022#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Andriy Skulysh048839d2006-09-27 13:47:22 +090029#include <asm/hd64461.h>
Paul Mundt193f3c22008-07-30 02:16:12 +090030#include <cpu/dac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define WIDTH 640
33
34static 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
41static struct fb_fix_screeninfo hitfb_fix __initdata = {
42 .id = "Hitachi HD64461",
43 .type = FB_TYPE_PACKED_PIXELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .accel = FB_ACCEL_NONE,
45};
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static inline void hitfb_accel_wait(void)
48{
49 while (fb_readw(HD64461_GRCFGR) & HD64461_GRCFGR_ACCSTATUS) ;
50}
51
52static inline void hitfb_accel_start(int truecolor)
53{
54 if (truecolor) {
55 fb_writew(6, HD64461_GRCFGR);
56 } else {
57 fb_writew(7, HD64461_GRCFGR);
58 }
59}
60
61static inline void hitfb_accel_set_dest(int truecolor, u16 dx, u16 dy,
62 u16 width, u16 height)
63{
64 u32 saddr = WIDTH * dy + dx;
65 if (truecolor)
66 saddr <<= 1;
67
Andriy Skulysh048839d2006-09-27 13:47:22 +090068 fb_writew(width-1, HD64461_BBTDWR);
69 fb_writew(height-1, HD64461_BBTDHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 fb_writew(saddr & 0xffff, HD64461_BBTDSARL);
72 fb_writew(saddr >> 16, HD64461_BBTDSARH);
73
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static inline void hitfb_accel_bitblt(int truecolor, u16 sx, u16 sy, u16 dx,
77 u16 dy, u16 width, u16 height, u16 rop,
78 u32 mask_addr)
79{
80 u32 saddr, daddr;
81 u32 maddr = 0;
82
Andriy Skulysh048839d2006-09-27 13:47:22 +090083 height--;
84 width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 fb_writew(rop, HD64461_BBTROPR);
86 if ((sy < dy) || ((sy == dy) && (sx <= dx))) {
87 saddr = WIDTH * (sy + height) + sx + width;
88 daddr = WIDTH * (dy + height) + dx + width;
89 if (mask_addr) {
90 if (truecolor)
91 maddr = ((width >> 3) + 1) * (height + 1) - 1;
92 else
93 maddr =
94 (((width >> 4) + 1) * (height + 1) - 1) * 2;
95
96 fb_writew((1 << 5) | 1, HD64461_BBTMDR);
97 } else
98 fb_writew(1, HD64461_BBTMDR);
99 } else {
100 saddr = WIDTH * sy + sx;
101 daddr = WIDTH * dy + dx;
102 if (mask_addr) {
103 fb_writew((1 << 5), HD64461_BBTMDR);
104 } else {
105 fb_writew(0, HD64461_BBTMDR);
106 }
107 }
108 if (truecolor) {
109 saddr <<= 1;
110 daddr <<= 1;
111 }
112 fb_writew(width, HD64461_BBTDWR);
113 fb_writew(height, HD64461_BBTDHR);
114 fb_writew(saddr & 0xffff, HD64461_BBTSSARL);
115 fb_writew(saddr >> 16, HD64461_BBTSSARH);
116 fb_writew(daddr & 0xffff, HD64461_BBTDSARL);
117 fb_writew(daddr >> 16, HD64461_BBTDSARH);
118 if (mask_addr) {
119 maddr += mask_addr;
120 fb_writew(maddr & 0xffff, HD64461_BBTMARL);
121 fb_writew(maddr >> 16, HD64461_BBTMARH);
122 }
123 hitfb_accel_start(truecolor);
124}
125
126static void hitfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
127{
128 if (rect->rop != ROP_COPY)
129 cfb_fillrect(p, rect);
130 else {
Andriy Skulysh048839d2006-09-27 13:47:22 +0900131 hitfb_accel_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 fb_writew(0x00f0, HD64461_BBTROPR);
133 fb_writew(16, HD64461_BBTMDR);
134
135 if (p->var.bits_per_pixel == 16) {
136 fb_writew(((u32 *) (p->pseudo_palette))[rect->color],
137 HD64461_GRSCR);
138 hitfb_accel_set_dest(1, rect->dx, rect->dy, rect->width,
139 rect->height);
140 hitfb_accel_start(1);
141 } else {
142 fb_writew(rect->color, HD64461_GRSCR);
143 hitfb_accel_set_dest(0, rect->dx, rect->dy, rect->width,
144 rect->height);
145 hitfb_accel_start(0);
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148}
149
150static void hitfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
151{
Andriy Skulysh048839d2006-09-27 13:47:22 +0900152 hitfb_accel_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 hitfb_accel_bitblt(p->var.bits_per_pixel == 16, area->sx, area->sy,
154 area->dx, area->dy, area->width, area->height,
155 0x00cc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static int hitfb_pan_display(struct fb_var_screeninfo *var,
159 struct fb_info *info)
160{
161 int xoffset = var->xoffset;
162 int yoffset = var->yoffset;
163
164 if (xoffset != 0)
165 return -EINVAL;
166
Andriy Skulysh048839d2006-09-27 13:47:22 +0900167 fb_writew((yoffset*info->fix.line_length)>>10, HD64461_LCDCBAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return 0;
170}
171
172int hitfb_blank(int blank_mode, struct fb_info *info)
173{
174 unsigned short v;
175
176 if (blank_mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 v = fb_readw(HD64461_LDR1);
178 v &= ~HD64461_LDR1_DON;
179 fb_writew(v, HD64461_LDR1);
180
181 v = fb_readw(HD64461_LCDCCR);
182 v |= HD64461_LCDCCR_MOFF;
183 fb_writew(v, HD64461_LCDCCR);
184
185 v = fb_readw(HD64461_STBCR);
186 v |= HD64461_STBCR_SLCDST;
187 fb_writew(v, HD64461_STBCR);
188 } else {
189 v = fb_readw(HD64461_STBCR);
190 v &= ~HD64461_STBCR_SLCDST;
191 fb_writew(v, HD64461_STBCR);
Andriy Skulysh4bcac202006-09-27 13:07:38 +0900192
Andriy Skulysh048839d2006-09-27 13:47:22 +0900193 v = fb_readw(HD64461_LCDCCR);
194 v &= ~(HD64461_LCDCCR_MOFF | HD64461_LCDCCR_STREQ);
195 fb_writew(v, HD64461_LCDCCR);
196
197 do {
198 v = fb_readw(HD64461_LCDCCR);
199 } while(v&HD64461_LCDCCR_STBACK);
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 v = fb_readw(HD64461_LDR1);
202 v |= HD64461_LDR1_DON;
203 fb_writew(v, HD64461_LDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 return 0;
206}
207
208static int hitfb_setcolreg(unsigned regno, unsigned red, unsigned green,
209 unsigned blue, unsigned transp, struct fb_info *info)
210{
Andriy Skulysh048839d2006-09-27 13:47:22 +0900211 if (regno >= 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 1;
213
214 switch (info->var.bits_per_pixel) {
215 case 8:
216 fb_writew(regno << 8, HD64461_CPTWAR);
217 fb_writew(red >> 10, HD64461_CPTWDR);
218 fb_writew(green >> 10, HD64461_CPTWDR);
219 fb_writew(blue >> 10, HD64461_CPTWDR);
220 break;
221 case 16:
Andriy Skulysh048839d2006-09-27 13:47:22 +0900222 if (regno >= 16)
223 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 ((u32 *) (info->pseudo_palette))[regno] =
225 ((red & 0xf800)) |
226 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
227 break;
228 }
229 return 0;
230}
231
Andriy Skulysh048839d2006-09-27 13:47:22 +0900232static int hitfb_sync(struct fb_info *info)
233{
234 hitfb_accel_wait();
235
236 return 0;
237}
238
239static int hitfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
240{
241 int maxy;
242
243 var->xres = info->var.xres;
244 var->xres_virtual = info->var.xres;
245 var->yres = info->var.yres;
246
247 if ((var->bits_per_pixel != 8) && (var->bits_per_pixel != 16))
248 var->bits_per_pixel = info->var.bits_per_pixel;
249
250 if (var->yres_virtual < var->yres)
251 var->yres_virtual = var->yres;
252
253 maxy = info->fix.smem_len / var->xres;
254
255 if (var->bits_per_pixel == 16)
256 maxy /= 2;
257
258 if (var->yres_virtual > maxy)
259 var->yres_virtual = maxy;
260
261 var->xoffset = 0;
262 var->yoffset = 0;
263
264 switch (var->bits_per_pixel) {
265 case 8:
266 var->red.offset = 0;
267 var->red.length = 8;
268 var->green.offset = 0;
269 var->green.length = 8;
270 var->blue.offset = 0;
271 var->blue.length = 8;
272 var->transp.offset = 0;
273 var->transp.length = 0;
274 break;
275 case 16: /* RGB 565 */
276 var->red.offset = 11;
277 var->red.length = 5;
278 var->green.offset = 5;
279 var->green.length = 6;
280 var->blue.offset = 0;
281 var->blue.length = 5;
282 var->transp.offset = 0;
283 var->transp.length = 0;
284 break;
285 }
286
287 return 0;
288}
289
290static int hitfb_set_par(struct fb_info *info)
291{
292 unsigned short ldr3;
293
294 switch (info->var.bits_per_pixel) {
295 case 8:
296 info->fix.line_length = info->var.xres;
297 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
298 info->fix.ypanstep = 16;
299 break;
300 case 16:
301 info->fix.line_length = info->var.xres*2;
302 info->fix.visual = FB_VISUAL_TRUECOLOR;
303 info->fix.ypanstep = 8;
304 break;
305 }
306
307 fb_writew(info->fix.line_length, HD64461_LCDCLOR);
308 ldr3 = fb_readw(HD64461_LDR3);
309 ldr3 &= ~15;
310 ldr3 |= (info->var.bits_per_pixel == 8) ? 4 : 8;
311 fb_writew(ldr3, HD64461_LDR3);
312 return 0;
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static struct fb_ops hitfb_ops = {
316 .owner = THIS_MODULE,
Andriy Skulysh048839d2006-09-27 13:47:22 +0900317 .fb_check_var = hitfb_check_var,
318 .fb_set_par = hitfb_set_par,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 .fb_setcolreg = hitfb_setcolreg,
320 .fb_blank = hitfb_blank,
Andriy Skulysh048839d2006-09-27 13:47:22 +0900321 .fb_sync = hitfb_sync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 .fb_pan_display = hitfb_pan_display,
323 .fb_fillrect = hitfb_fillrect,
324 .fb_copyarea = hitfb_copyarea,
325 .fb_imageblit = cfb_imageblit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
Andriy Skulysh048839d2006-09-27 13:47:22 +0900328static int __init hitfb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned short lcdclor, ldr3, ldvndr;
Paul Mundt684dcd02009-07-07 11:44:56 +0900331 struct fb_info *info;
332 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (fb_get_options("hitfb", NULL))
335 return -ENODEV;
336
Paul Mundt62669e62009-05-20 11:27:13 +0900337 hitfb_fix.mmio_start = HD64461_IO_OFFSET(0x1000);
Andriy Skulysh048839d2006-09-27 13:47:22 +0900338 hitfb_fix.mmio_len = 0x1000;
Paul Mundt62669e62009-05-20 11:27:13 +0900339 hitfb_fix.smem_start = HD64461_IO_OFFSET(0x02000000);
Andriy Skulysh048839d2006-09-27 13:47:22 +0900340 hitfb_fix.smem_len = 512 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 lcdclor = fb_readw(HD64461_LCDCLOR);
343 ldvndr = fb_readw(HD64461_LDVNDR);
344 ldr3 = fb_readw(HD64461_LDR3);
345
346 switch (ldr3 & 15) {
347 default:
348 case 4:
349 hitfb_var.bits_per_pixel = 8;
350 hitfb_var.xres = lcdclor;
351 break;
352 case 8:
353 hitfb_var.bits_per_pixel = 16;
354 hitfb_var.xres = lcdclor / 2;
355 break;
356 }
357 hitfb_fix.line_length = lcdclor;
358 hitfb_fix.visual = (hitfb_var.bits_per_pixel == 8) ?
359 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
360 hitfb_var.yres = ldvndr + 1;
361 hitfb_var.xres_virtual = hitfb_var.xres;
362 hitfb_var.yres_virtual = hitfb_fix.smem_len / lcdclor;
363 switch (hitfb_var.bits_per_pixel) {
364 case 8:
365 hitfb_var.red.offset = 0;
366 hitfb_var.red.length = 8;
367 hitfb_var.green.offset = 0;
368 hitfb_var.green.length = 8;
369 hitfb_var.blue.offset = 0;
370 hitfb_var.blue.length = 8;
371 hitfb_var.transp.offset = 0;
372 hitfb_var.transp.length = 0;
373 break;
374 case 16: /* RGB 565 */
375 hitfb_var.red.offset = 11;
376 hitfb_var.red.length = 5;
377 hitfb_var.green.offset = 5;
378 hitfb_var.green.length = 6;
379 hitfb_var.blue.offset = 0;
380 hitfb_var.blue.length = 5;
381 hitfb_var.transp.offset = 0;
382 hitfb_var.transp.length = 0;
383 break;
384 }
385
Paul Mundt684dcd02009-07-07 11:44:56 +0900386 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
387 if (unlikely(!info))
388 return -ENOMEM;
389
390 info->fbops = &hitfb_ops;
391 info->var = hitfb_var;
392 info->fix = hitfb_fix;
393 info->pseudo_palette = info->par;
394 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
Andriy Skulysh048839d2006-09-27 13:47:22 +0900395 FBINFO_HWACCEL_FILLRECT | FBINFO_HWACCEL_COPYAREA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Paul Mundt684dcd02009-07-07 11:44:56 +0900397 info->screen_base = (void *)hitfb_fix.smem_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Paul Mundt684dcd02009-07-07 11:44:56 +0900399 ret = fb_alloc_cmap(&info->cmap, 256, 0);
400 if (unlikely(ret < 0))
401 goto err_fb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Paul Mundt684dcd02009-07-07 11:44:56 +0900403 ret = register_framebuffer(info);
404 if (unlikely(ret < 0))
405 goto err;
406
407 platform_set_drvdata(dev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 printk(KERN_INFO "fb%d: %s frame buffer device\n",
Paul Mundt684dcd02009-07-07 11:44:56 +0900410 info->node, info->fix.id);
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
Paul Mundt684dcd02009-07-07 11:44:56 +0900413
414err:
415 fb_dealloc_cmap(&info->cmap);
416err_fb:
417 framebuffer_release(info);
418 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Paul Mundt0f8afa72008-03-06 13:56:33 +0900421static int __exit hitfb_remove(struct platform_device *dev)
Andriy Skulysh048839d2006-09-27 13:47:22 +0900422{
Paul Mundt684dcd02009-07-07 11:44:56 +0900423 struct fb_info *info = platform_get_drvdata(dev);
424
425 unregister_framebuffer(info);
426 fb_dealloc_cmap(&info->cmap);
427 framebuffer_release(info);
428
429 return 0;
Andriy Skulysh048839d2006-09-27 13:47:22 +0900430}
431
Paul Mundt2dbc8a22009-07-07 11:50:57 +0900432static int hitfb_suspend(struct device *dev)
Andriy Skulysh048839d2006-09-27 13:47:22 +0900433{
434 u16 v;
435
436 hitfb_blank(1,0);
437 v = fb_readw(HD64461_STBCR);
438 v |= HD64461_STBCR_SLCKE_IST;
439 fb_writew(v, HD64461_STBCR);
440
441 return 0;
442}
443
Paul Mundt2dbc8a22009-07-07 11:50:57 +0900444static int hitfb_resume(struct device *dev)
Andriy Skulysh048839d2006-09-27 13:47:22 +0900445{
446 u16 v;
447
448 v = fb_readw(HD64461_STBCR);
449 v &= ~HD64461_STBCR_SLCKE_OST;
450 msleep(100);
451 v = fb_readw(HD64461_STBCR);
452 v &= ~HD64461_STBCR_SLCKE_IST;
453 fb_writew(v, HD64461_STBCR);
454 hitfb_blank(0,0);
455
456 return 0;
457}
Paul Mundt2dbc8a22009-07-07 11:50:57 +0900458
Alexey Dobriyan47145212009-12-14 18:00:08 -0800459static const struct dev_pm_ops hitfb_dev_pm_ops = {
Paul Mundt2dbc8a22009-07-07 11:50:57 +0900460 .suspend = hitfb_suspend,
461 .resume = hitfb_resume,
462};
Andriy Skulysh048839d2006-09-27 13:47:22 +0900463
464static struct platform_driver hitfb_driver = {
465 .probe = hitfb_probe,
Paul Mundt0f8afa72008-03-06 13:56:33 +0900466 .remove = __exit_p(hitfb_remove),
Andriy Skulysh048839d2006-09-27 13:47:22 +0900467 .driver = {
468 .name = "hitfb",
Paul Mundt2dbc8a22009-07-07 11:50:57 +0900469 .owner = THIS_MODULE,
470 .pm = &hitfb_dev_pm_ops,
Andriy Skulysh048839d2006-09-27 13:47:22 +0900471 },
472};
473
474static struct platform_device hitfb_device = {
475 .name = "hitfb",
476 .id = -1,
477};
478
479static int __init hitfb_init(void)
480{
481 int ret;
482
483 ret = platform_driver_register(&hitfb_driver);
484 if (!ret) {
485 ret = platform_device_register(&hitfb_device);
486 if (ret)
487 platform_driver_unregister(&hitfb_driver);
488 }
489 return ret;
490}
491
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static void __exit hitfb_exit(void)
494{
Andriy Skulysh048839d2006-09-27 13:47:22 +0900495 platform_device_unregister(&hitfb_device);
496 platform_driver_unregister(&hitfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499module_init(hitfb_init);
500module_exit(hitfb_exit);
501
502MODULE_LICENSE("GPL");