offb: Fix bug in calculating requested vram size
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / epson1355fb.c
bloba268cbf1cbeac90aee2f34406c1eac844ae094f0
1 /*
2 * linux/drivers/video/epson1355fb.c -- Epson S1D13505 frame buffer for 2.5.
4 * Epson Research S1D13505 Embedded RAMDAC LCD/CRT Controller
5 * (previously known as SED1355)
7 * Cf. http://vdc.epson.com/
10 * Copyright (C) Hewlett-Packard Company. All rights reserved.
12 * Written by Christopher Hoover <ch@hpl.hp.com>
14 * Adapted from:
16 * linux/drivers/video/skeletonfb.c
17 * Modified to new api Jan 2001 by James Simmons (jsimmons@infradead.org)
18 * Created 28 Dec 1997 by Geert Uytterhoeven
20 * linux/drivers/video/epson1355fb.c (2.4 driver)
21 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
23 * This file is subject to the terms and conditions of the GNU General Public
24 * License. See the file COPYING in the main directory of this archive for
25 * more details.
28 * Noteworthy Issues
29 * -----------------
31 * This driver is complicated by the fact that this is a 16-bit chip
32 * and, on at least one platform (ceiva), we can only do 16-bit reads
33 * and writes to the framebuffer. We hide this from user space
34 * except in the case of mmap().
37 * To Do
38 * -----
40 * - Test 8-bit pseudocolor mode
41 * - Allow setting bpp, virtual resolution
42 * - Implement horizontal panning
43 * - (maybe) Implement hardware cursor
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/errno.h>
49 #include <linux/string.h>
50 #include <linux/mm.h>
51 #include <linux/delay.h>
52 #include <linux/fb.h>
53 #include <linux/init.h>
54 #include <linux/ioport.h>
55 #include <linux/platform_device.h>
57 #include <asm/types.h>
58 #include <asm/io.h>
59 #include <linux/uaccess.h>
61 #include <video/epson1355.h>
63 struct epson1355_par {
64 unsigned long reg_addr;
65 u32 pseudo_palette[16];
68 /* ------------------------------------------------------------------------- */
70 #if defined(CONFIG_ARM)
72 # ifdef CONFIG_ARCH_CEIVA
73 # include <mach/hardware.h>
74 # define EPSON1355FB_BASE_PHYS (CEIVA_PHYS_SED1355)
75 # endif
77 static inline u8 epson1355_read_reg(struct epson1355_par *par, int index)
79 return __raw_readb(par->reg_addr + index);
82 static inline void epson1355_write_reg(struct epson1355_par *par, u8 data, int index)
84 __raw_writeb(data, par->reg_addr + index);
87 #else
88 # error "no architecture-specific epson1355_{read,write}_reg"
89 #endif
91 #ifndef EPSON1355FB_BASE_PHYS
92 # error "EPSON1355FB_BASE_PHYS is not defined"
93 #endif
95 #define EPSON1355FB_REGS_OFS (0)
96 #define EPSON1355FB_REGS_PHYS (EPSON1355FB_BASE_PHYS + EPSON1355FB_REGS_OFS)
97 #define EPSON1355FB_REGS_LEN (64)
99 #define EPSON1355FB_FB_OFS (0x00200000)
100 #define EPSON1355FB_FB_PHYS (EPSON1355FB_BASE_PHYS + EPSON1355FB_FB_OFS)
101 #define EPSON1355FB_FB_LEN (2 * 1024 * 1024)
103 /* ------------------------------------------------------------------------- */
105 static inline u16 epson1355_read_reg16(struct epson1355_par *par, int index)
107 u8 lo = epson1355_read_reg(par, index);
108 u8 hi = epson1355_read_reg(par, index + 1);
110 return (hi << 8) | lo;
113 static inline void epson1355_write_reg16(struct epson1355_par *par, u16 data, int index)
115 u8 lo = data & 0xff;
116 u8 hi = (data >> 8) & 0xff;
118 epson1355_write_reg(par, lo, index);
119 epson1355_write_reg(par, hi, index + 1);
122 static inline u32 epson1355_read_reg20(struct epson1355_par *par, int index)
124 u8 b0 = epson1355_read_reg(par, index);
125 u8 b1 = epson1355_read_reg(par, index + 1);
126 u8 b2 = epson1355_read_reg(par, index + 2);
128 return (b2 & 0x0f) << 16 | (b1 << 8) | b0;
131 static inline void epson1355_write_reg20(struct epson1355_par *par, u32 data, int index)
133 u8 b0 = data & 0xff;
134 u8 b1 = (data >> 8) & 0xff;
135 u8 b2 = (data >> 16) & 0x0f;
137 epson1355_write_reg(par, b0, index);
138 epson1355_write_reg(par, b1, index + 1);
139 epson1355_write_reg(par, b2, index + 2);
142 /* ------------------------------------------------------------------------- */
144 static void set_lut(struct epson1355_par *par, u8 index, u8 r, u8 g, u8 b)
146 epson1355_write_reg(par, index, REG_LUT_ADDR);
147 epson1355_write_reg(par, r, REG_LUT_DATA);
148 epson1355_write_reg(par, g, REG_LUT_DATA);
149 epson1355_write_reg(par, b, REG_LUT_DATA);
154 * epson1355fb_setcolreg - sets a color register.
155 * @regno: Which register in the CLUT we are programming
156 * @red: The red value which can be up to 16 bits wide
157 * @green: The green value which can be up to 16 bits wide
158 * @blue: The blue value which can be up to 16 bits wide.
159 * @transp: If supported the alpha value which can be up to 16 bits wide.
160 * @info: frame buffer info structure
162 * Returns negative errno on error, or zero on success.
164 static int epson1355fb_setcolreg(unsigned regno, unsigned r, unsigned g,
165 unsigned b, unsigned transp,
166 struct fb_info *info)
168 struct epson1355_par *par = info->par;
170 if (info->var.grayscale)
171 r = g = b = (19595 * r + 38470 * g + 7471 * b) >> 16;
173 switch (info->fix.visual) {
174 case FB_VISUAL_TRUECOLOR:
175 if (regno >= 16)
176 return -EINVAL;
178 ((u32 *) info->pseudo_palette)[regno] =
179 (r & 0xf800) | (g & 0xfc00) >> 5 | (b & 0xf800) >> 11;
181 break;
182 case FB_VISUAL_PSEUDOCOLOR:
183 if (regno >= 256)
184 return -EINVAL;
186 set_lut(par, regno, r >> 8, g >> 8, b >> 8);
188 break;
189 default:
190 return -ENOSYS;
192 return 0;
195 /* ------------------------------------------------------------------------- */
198 * epson1355fb_pan_display - Pans the display.
199 * @var: frame buffer variable screen structure
200 * @info: frame buffer structure that represents a single frame buffer
202 * Pan (or wrap, depending on the `vmode' field) the display using the
203 * `xoffset' and `yoffset' fields of the `var' structure.
204 * If the values don't fit, return -EINVAL.
206 * Returns negative errno on error, or zero on success.
208 static int epson1355fb_pan_display(struct fb_var_screeninfo *var,
209 struct fb_info *info)
211 struct epson1355_par *par = info->par;
212 u32 start;
214 if (var->xoffset != 0) /* not yet ... */
215 return -EINVAL;
217 if (var->yoffset + info->var.yres > info->var.yres_virtual)
218 return -EINVAL;
220 start = (info->fix.line_length >> 1) * var->yoffset;
222 epson1355_write_reg20(par, start, REG_SCRN1_DISP_START_ADDR0);
224 return 0;
227 /* ------------------------------------------------------------------------- */
229 static void lcd_enable(struct epson1355_par *par, int enable)
231 u8 mode = epson1355_read_reg(par, REG_DISPLAY_MODE);
233 if (enable)
234 mode |= 1;
235 else
236 mode &= ~1;
238 epson1355_write_reg(par, mode, REG_DISPLAY_MODE);
241 #if defined(CONFIG_ARCH_CEIVA)
242 static void backlight_enable(int enable)
244 /* ### this should be protected by a spinlock ... */
245 u8 pddr = clps_readb(PDDR);
246 if (enable)
247 pddr |= (1 << 5);
248 else
249 pddr &= ~(1 << 5);
250 clps_writeb(pddr, PDDR);
252 #else
253 static void backlight_enable(int enable)
256 #endif
260 * epson1355fb_blank - blanks the display.
261 * @blank_mode: the blank mode we want.
262 * @info: frame buffer structure that represents a single frame buffer
264 * Blank the screen if blank_mode != 0, else unblank. Return 0 if
265 * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
266 * video mode which doesn't support it. Implements VESA suspend
267 * and powerdown modes on hardware that supports disabling hsync/vsync:
268 * blank_mode == 2: suspend vsync
269 * blank_mode == 3: suspend hsync
270 * blank_mode == 4: powerdown
272 * Returns negative errno on error, or zero on success.
275 static int epson1355fb_blank(int blank_mode, struct fb_info *info)
277 struct epson1355_par *par = info->par;
279 switch (blank_mode) {
280 case FB_BLANK_UNBLANK:
281 case FB_BLANK_NORMAL:
282 lcd_enable(par, 1);
283 backlight_enable(1);
284 break;
285 case FB_BLANK_VSYNC_SUSPEND:
286 case FB_BLANK_HSYNC_SUSPEND:
287 backlight_enable(0);
288 break;
289 case FB_BLANK_POWERDOWN:
290 backlight_enable(0);
291 lcd_enable(par, 0);
292 break;
293 default:
294 return -EINVAL;
297 /* let fbcon do a soft blank for us */
298 return (blank_mode == FB_BLANK_NORMAL) ? 1 : 0;
301 /* ------------------------------------------------------------------------- */
304 * We can't use the cfb generic routines, as we have to limit
305 * ourselves to 16-bit or 8-bit loads and stores to this 16-bit
306 * chip.
309 static inline void epson1355fb_fb_writel(unsigned long v, unsigned long *a)
311 u16 *p = (u16 *) a;
312 u16 l = v & 0xffff;
313 u16 h = v >> 16;
315 fb_writew(l, p);
316 fb_writew(h, p + 1);
319 static inline unsigned long epson1355fb_fb_readl(const unsigned long *a)
321 const u16 *p = (u16 *) a;
322 u16 l = fb_readw(p);
323 u16 h = fb_readw(p + 1);
325 return (h << 16) | l;
328 #define FB_READL epson1355fb_fb_readl
329 #define FB_WRITEL epson1355fb_fb_writel
331 /* ------------------------------------------------------------------------- */
333 static inline unsigned long copy_from_user16(void *to, const void *from,
334 unsigned long n)
336 u16 *dst = (u16 *) to;
337 u16 *src = (u16 *) from;
339 if (!access_ok(VERIFY_READ, from, n))
340 return n;
342 while (n > 1) {
343 u16 v;
344 if (__get_user(v, src))
345 return n;
347 fb_writew(v, dst);
349 src++, dst++;
350 n -= 2;
353 if (n) {
354 u8 v;
356 if (__get_user(v, ((u8 *) src)))
357 return n;
359 fb_writeb(v, dst);
361 return 0;
364 static inline unsigned long copy_to_user16(void *to, const void *from,
365 unsigned long n)
367 u16 *dst = (u16 *) to;
368 u16 *src = (u16 *) from;
370 if (!access_ok(VERIFY_WRITE, to, n))
371 return n;
373 while (n > 1) {
374 u16 v = fb_readw(src);
376 if (__put_user(v, dst))
377 return n;
379 src++, dst++;
380 n -= 2;
383 if (n) {
384 u8 v = fb_readb(src);
386 if (__put_user(v, ((u8 *) dst)))
387 return n;
389 return 0;
393 static ssize_t
394 epson1355fb_read(struct fb_info *info, char *buf, size_t count, loff_t * ppos)
396 unsigned long p = *ppos;
398 if (p >= info->fix.smem_len)
399 return 0;
400 if (count >= info->fix.smem_len)
401 count = info->fix.smem_len;
402 if (count + p > info->fix.smem_len)
403 count = info->fix.smem_len - p;
405 if (count) {
406 char *base_addr;
408 base_addr = info->screen_base;
409 count -= copy_to_user16(buf, base_addr + p, count);
410 if (!count)
411 return -EFAULT;
412 *ppos += count;
414 return count;
417 static ssize_t
418 epson1355fb_write(struct fb_info *info, const char *buf,
419 size_t count, loff_t * ppos)
421 unsigned long p = *ppos;
422 int err;
424 /* from fbmem.c except for our own copy_*_user */
425 if (p > info->fix.smem_len)
426 return -ENOSPC;
427 if (count >= info->fix.smem_len)
428 count = info->fix.smem_len;
429 err = 0;
430 if (count + p > info->fix.smem_len) {
431 count = info->fix.smem_len - p;
432 err = -ENOSPC;
435 if (count) {
436 char *base_addr;
438 base_addr = info->screen_base;
439 count -= copy_from_user16(base_addr + p, buf, count);
440 *ppos += count;
441 err = -EFAULT;
443 if (count)
444 return count;
445 return err;
448 /* ------------------------------------------------------------------------- */
450 static struct fb_ops epson1355fb_fbops = {
451 .owner = THIS_MODULE,
452 .fb_setcolreg = epson1355fb_setcolreg,
453 .fb_pan_display = epson1355fb_pan_display,
454 .fb_blank = epson1355fb_blank,
455 .fb_fillrect = cfb_fillrect,
456 .fb_copyarea = cfb_copyarea,
457 .fb_imageblit = cfb_imageblit,
458 .fb_read = epson1355fb_read,
459 .fb_write = epson1355fb_write,
462 /* ------------------------------------------------------------------------- */
464 static __init unsigned int get_fb_size(struct fb_info *info)
466 unsigned int size = 2 * 1024 * 1024;
467 char *p = info->screen_base;
469 /* the 512k framebuffer is aliased at start + 0x80000 * n */
470 fb_writeb(1, p);
471 fb_writeb(0, p + 0x80000);
472 if (!fb_readb(p))
473 size = 512 * 1024;
475 fb_writeb(0, p);
477 return size;
480 static int epson1355_width_tab[2][4] __initdata =
481 { {4, 8, 16, -1}, {9, 12, 16, -1} };
482 static int epson1355_bpp_tab[8] __initdata = { 1, 2, 4, 8, 15, 16 };
484 static void __init fetch_hw_state(struct fb_info *info, struct epson1355_par *par)
486 struct fb_var_screeninfo *var = &info->var;
487 struct fb_fix_screeninfo *fix = &info->fix;
488 u8 panel, display;
489 u16 offset;
490 u32 xres, yres;
491 u32 xres_virtual, yres_virtual;
492 int bpp, lcd_bpp;
493 int is_color, is_dual, is_tft;
494 int lcd_enabled, crt_enabled;
496 fix->type = FB_TYPE_PACKED_PIXELS;
498 display = epson1355_read_reg(par, REG_DISPLAY_MODE);
499 bpp = epson1355_bpp_tab[(display >> 2) & 7];
501 switch (bpp) {
502 case 8:
503 fix->visual = FB_VISUAL_PSEUDOCOLOR;
504 var->bits_per_pixel = 8;
505 var->red.offset = var->green.offset = var->blue.offset = 0;
506 var->red.length = var->green.length = var->blue.length = 8;
507 break;
508 case 16:
509 /* 5-6-5 RGB */
510 fix->visual = FB_VISUAL_TRUECOLOR;
511 var->bits_per_pixel = 16;
512 var->red.offset = 11;
513 var->red.length = 5;
514 var->green.offset = 5;
515 var->green.length = 6;
516 var->blue.offset = 0;
517 var->blue.length = 5;
518 break;
519 default:
520 BUG();
522 fb_alloc_cmap(&(info->cmap), 256, 0);
524 panel = epson1355_read_reg(par, REG_PANEL_TYPE);
525 is_color = (panel & 0x04) != 0;
526 is_dual = (panel & 0x02) != 0;
527 is_tft = (panel & 0x01) != 0;
528 crt_enabled = (display & 0x02) != 0;
529 lcd_enabled = (display & 0x01) != 0;
530 lcd_bpp = epson1355_width_tab[is_tft][(panel >> 4) & 3];
532 xres = (epson1355_read_reg(par, REG_HORZ_DISP_WIDTH) + 1) * 8;
533 yres = (epson1355_read_reg16(par, REG_VERT_DISP_HEIGHT0) + 1) *
534 ((is_dual && !crt_enabled) ? 2 : 1);
535 offset = epson1355_read_reg16(par, REG_MEM_ADDR_OFFSET0) & 0x7ff;
536 xres_virtual = offset * 16 / bpp;
537 yres_virtual = fix->smem_len / (offset * 2);
539 var->xres = xres;
540 var->yres = yres;
541 var->xres_virtual = xres_virtual;
542 var->yres_virtual = yres_virtual;
543 var->xoffset = var->yoffset = 0;
545 fix->line_length = offset * 2;
547 fix->xpanstep = 0; /* no pan yet */
548 fix->ypanstep = 1;
549 fix->ywrapstep = 0;
550 fix->accel = FB_ACCEL_NONE;
552 var->grayscale = !is_color;
554 #ifdef DEBUG
555 printk(KERN_INFO
556 "epson1355fb: xres=%d, yres=%d, "
557 "is_color=%d, is_dual=%d, is_tft=%d\n",
558 xres, yres, is_color, is_dual, is_tft);
559 printk(KERN_INFO
560 "epson1355fb: bpp=%d, lcd_bpp=%d, "
561 "crt_enabled=%d, lcd_enabled=%d\n",
562 bpp, lcd_bpp, crt_enabled, lcd_enabled);
563 #endif
567 static void clearfb16(struct fb_info *info)
569 u16 *dst = (u16 *) info->screen_base;
570 unsigned long n = info->fix.smem_len;
572 while (n > 1) {
573 fb_writew(0, dst);
574 dst++, n -= 2;
577 if (n)
578 fb_writeb(0, dst);
581 static int epson1355fb_remove(struct platform_device *dev)
583 struct fb_info *info = platform_get_drvdata(dev);
584 struct epson1355_par *par = info->par;
586 backlight_enable(0);
587 if (par) {
588 lcd_enable(par, 0);
589 if (par && par->reg_addr)
590 iounmap((void *) par->reg_addr);
593 if (info) {
594 fb_dealloc_cmap(&info->cmap);
595 if (info->screen_base)
596 iounmap(info->screen_base);
597 framebuffer_release(info);
599 release_mem_region(EPSON1355FB_FB_PHYS, EPSON1355FB_FB_LEN);
600 release_mem_region(EPSON1355FB_REGS_PHYS, EPSON1355FB_REGS_LEN);
601 return 0;
604 int __devinit epson1355fb_probe(struct platform_device *dev)
606 struct epson1355_par *default_par;
607 struct fb_info *info;
608 u8 revision;
609 int rc = 0;
611 if (!request_mem_region(EPSON1355FB_REGS_PHYS, EPSON1355FB_REGS_LEN, "S1D13505 registers")) {
612 printk(KERN_ERR "epson1355fb: unable to reserve "
613 "registers at 0x%0x\n", EPSON1355FB_REGS_PHYS);
614 rc = -EBUSY;
615 goto bail;
618 if (!request_mem_region(EPSON1355FB_FB_PHYS, EPSON1355FB_FB_LEN,
619 "S1D13505 framebuffer")) {
620 printk(KERN_ERR "epson1355fb: unable to reserve "
621 "framebuffer at 0x%0x\n", EPSON1355FB_FB_PHYS);
622 rc = -EBUSY;
623 goto bail;
626 info = framebuffer_alloc(sizeof(struct epson1355_par), &dev->dev);
627 if (!info) {
628 rc = -ENOMEM;
629 goto bail;
632 default_par = info->par;
633 default_par->reg_addr = (unsigned long) ioremap(EPSON1355FB_REGS_PHYS, EPSON1355FB_REGS_LEN);
634 if (!default_par->reg_addr) {
635 printk(KERN_ERR "epson1355fb: unable to map registers\n");
636 rc = -ENOMEM;
637 goto bail;
639 info->pseudo_palette = default_par->pseudo_palette;
641 info->screen_base = ioremap(EPSON1355FB_FB_PHYS, EPSON1355FB_FB_LEN);
642 if (!info->screen_base) {
643 printk(KERN_ERR "epson1355fb: unable to map framebuffer\n");
644 rc = -ENOMEM;
645 goto bail;
648 revision = epson1355_read_reg(default_par, REG_REVISION_CODE);
649 if ((revision >> 2) != 3) {
650 printk(KERN_INFO "epson1355fb: epson1355 not found\n");
651 rc = -ENODEV;
652 goto bail;
655 info->fix.mmio_start = EPSON1355FB_REGS_PHYS;
656 info->fix.mmio_len = EPSON1355FB_REGS_LEN;
657 info->fix.smem_start = EPSON1355FB_FB_PHYS;
658 info->fix.smem_len = get_fb_size(info);
660 printk(KERN_INFO "epson1355fb: regs mapped at 0x%lx, fb %d KiB mapped at 0x%p\n",
661 default_par->reg_addr, info->fix.smem_len / 1024, info->screen_base);
663 strcpy(info->fix.id, "S1D13505");
664 info->par = default_par;
665 info->fbops = &epson1355fb_fbops;
666 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
668 /* we expect the boot loader to have initialized the chip
669 with appropriate parameters from which we can determinte
670 the flavor of lcd panel attached */
671 fetch_hw_state(info, default_par);
673 /* turn this puppy on ... */
674 clearfb16(info);
675 backlight_enable(1);
676 lcd_enable(default_par, 1);
678 if (register_framebuffer(info) < 0) {
679 rc = -EINVAL;
680 goto bail;
683 * Our driver data.
685 platform_set_drvdata(dev, info);
687 printk(KERN_INFO "fb%d: %s frame buffer device\n",
688 info->node, info->fix.id);
690 return 0;
692 bail:
693 epson1355fb_remove(dev);
694 return rc;
697 static struct platform_driver epson1355fb_driver = {
698 .probe = epson1355fb_probe,
699 .remove = epson1355fb_remove,
700 .driver = {
701 .name = "epson1355fb",
705 static struct platform_device *epson1355fb_device;
707 int __init epson1355fb_init(void)
709 int ret = 0;
711 if (fb_get_options("epson1355fb", NULL))
712 return -ENODEV;
714 ret = platform_driver_register(&epson1355fb_driver);
716 if (!ret) {
717 epson1355fb_device = platform_device_alloc("epson1355fb", 0);
719 if (epson1355fb_device)
720 ret = platform_device_add(epson1355fb_device);
721 else
722 ret = -ENOMEM;
724 if (ret) {
725 platform_device_put(epson1355fb_device);
726 platform_driver_unregister(&epson1355fb_driver);
730 return ret;
733 module_init(epson1355fb_init);
735 #ifdef MODULE
736 static void __exit epson1355fb_exit(void)
738 platform_device_unregister(epson1355fb_device);
739 platform_driver_unregister(&epson1355fb_driver);
742 /* ------------------------------------------------------------------------- */
744 module_exit(epson1355fb_exit);
745 #endif
747 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>");
748 MODULE_DESCRIPTION("Framebuffer driver for Epson S1D13505");
749 MODULE_LICENSE("GPL");