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://www.erd.epson.com/vdc/html/S1D13505.html
10 * Copyright (C) Hewlett-Packard Company. All rights reserved.
12 * Written by Christopher Hoover <ch@hpl.hp.com>
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
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().
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>
51 #include <linux/tty.h>
52 #include <linux/slab.h>
53 #include <linux/delay.h>
55 #include <linux/init.h>
56 #include <linux/ioport.h>
57 #include <linux/platform_device.h>
59 #include <asm/types.h>
61 #include <asm/uaccess.h>
63 #include <video/epson1355.h>
65 struct epson1355_par {
66 unsigned long reg_addr;
69 /* ------------------------------------------------------------------------- */
73 static inline u8 epson1355_read_reg(int index)
75 return ctrl_inb(par.reg_addr + index);
78 static inline void epson1355_write_reg(u8 data, int index)
80 ctrl_outb(data, par.reg_addr + index);
83 #elif defined(CONFIG_ARM)
85 # ifdef CONFIG_ARCH_CEIVA
86 # include <asm/arch/hardware.h>
87 # define EPSON1355FB_BASE_PHYS (CEIVA_PHYS_SED1355)
90 static inline u8 epson1355_read_reg(struct epson1355_par *par, int index)
92 return __raw_readb(par->reg_addr + index);
95 static inline void epson1355_write_reg(struct epson1355_par *par, u8 data, int index)
97 __raw_writeb(data, par->reg_addr + index);
101 # error "no architecture-specific epson1355_{read,write}_reg"
104 #ifndef EPSON1355FB_BASE_PHYS
105 # error "EPSON1355FB_BASE_PHYS is not defined"
108 #define EPSON1355FB_REGS_OFS (0)
109 #define EPSON1355FB_REGS_PHYS (EPSON1355FB_BASE_PHYS + EPSON1355FB_REGS_OFS)
110 #define EPSON1355FB_REGS_LEN (64)
112 #define EPSON1355FB_FB_OFS (0x00200000)
113 #define EPSON1355FB_FB_PHYS (EPSON1355FB_BASE_PHYS + EPSON1355FB_FB_OFS)
114 #define EPSON1355FB_FB_LEN (2 * 1024 * 1024)
116 /* ------------------------------------------------------------------------- */
118 static inline u16 epson1355_read_reg16(struct epson1355_par *par, int index)
120 u8 lo = epson1355_read_reg(par, index);
121 u8 hi = epson1355_read_reg(par, index + 1);
123 return (hi << 8) | lo;
126 static inline void epson1355_write_reg16(struct epson1355_par *par, u16 data, int index)
129 u8 hi = (data >> 8) & 0xff;
131 epson1355_write_reg(par, lo, index);
132 epson1355_write_reg(par, hi, index + 1);
135 static inline u32 epson1355_read_reg20(struct epson1355_par *par, int index)
137 u8 b0 = epson1355_read_reg(par, index);
138 u8 b1 = epson1355_read_reg(par, index + 1);
139 u8 b2 = epson1355_read_reg(par, index + 2);
141 return (b2 & 0x0f) << 16 | (b1 << 8) | b0;
144 static inline void epson1355_write_reg20(struct epson1355_par *par, u32 data, int index)
147 u8 b1 = (data >> 8) & 0xff;
148 u8 b2 = (data >> 16) & 0x0f;
150 epson1355_write_reg(par, b0, index);
151 epson1355_write_reg(par, b1, index + 1);
152 epson1355_write_reg(par, b2, index + 2);
155 /* ------------------------------------------------------------------------- */
157 static void set_lut(struct epson1355_par *par, u8 index, u8 r, u8 g, u8 b)
159 epson1355_write_reg(par, index, REG_LUT_ADDR);
160 epson1355_write_reg(par, r, REG_LUT_DATA);
161 epson1355_write_reg(par, g, REG_LUT_DATA);
162 epson1355_write_reg(par, b, REG_LUT_DATA);
167 * epson1355fb_setcolreg - sets a color register.
168 * @regno: Which register in the CLUT we are programming
169 * @red: The red value which can be up to 16 bits wide
170 * @green: The green value which can be up to 16 bits wide
171 * @blue: The blue value which can be up to 16 bits wide.
172 * @transp: If supported the alpha value which can be up to 16 bits wide.
173 * @info: frame buffer info structure
175 * Returns negative errno on error, or zero on success.
177 static int epson1355fb_setcolreg(unsigned regno, unsigned r, unsigned g,
178 unsigned b, unsigned transp,
179 struct fb_info *info)
181 struct epson1355_par *par = info->par;
183 if (info->var.grayscale)
184 r = g = b = (19595 * r + 38470 * g + 7471 * b) >> 16;
186 switch (info->fix.visual) {
187 case FB_VISUAL_TRUECOLOR:
191 ((u32 *) info->pseudo_palette)[regno] =
192 (r & 0xf800) | (g & 0xfc00) >> 5 | (b & 0xf800) >> 11;
195 case FB_VISUAL_PSEUDOCOLOR:
199 set_lut(par, regno, r >> 8, g >> 8, b >> 8);
208 /* ------------------------------------------------------------------------- */
211 * epson1355fb_pan_display - Pans the display.
212 * @var: frame buffer variable screen structure
213 * @info: frame buffer structure that represents a single frame buffer
215 * Pan (or wrap, depending on the `vmode' field) the display using the
216 * `xoffset' and `yoffset' fields of the `var' structure.
217 * If the values don't fit, return -EINVAL.
219 * Returns negative errno on error, or zero on success.
221 static int epson1355fb_pan_display(struct fb_var_screeninfo *var,
222 struct fb_info *info)
224 struct epson1355_par *par = info->par;
227 if (var->xoffset != 0) /* not yet ... */
230 if (var->yoffset + info->var.yres > info->var.yres_virtual)
233 start = (info->fix.line_length >> 1) * var->yoffset;
235 epson1355_write_reg20(par, start, REG_SCRN1_DISP_START_ADDR0);
240 /* ------------------------------------------------------------------------- */
242 static void lcd_enable(struct epson1355_par *par, int enable)
244 u8 mode = epson1355_read_reg(par, REG_DISPLAY_MODE);
251 epson1355_write_reg(par, mode, REG_DISPLAY_MODE);
254 #if defined(CONFIG_ARCH_CEIVA)
255 static void backlight_enable(int enable)
257 /* ### this should be protected by a spinlock ... */
258 u8 pddr = clps_readb(PDDR);
263 clps_writeb(pddr, PDDR);
266 static void backlight_enable(int enable)
273 * epson1355fb_blank - blanks the display.
274 * @blank_mode: the blank mode we want.
275 * @info: frame buffer structure that represents a single frame buffer
277 * Blank the screen if blank_mode != 0, else unblank. Return 0 if
278 * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
279 * video mode which doesn't support it. Implements VESA suspend
280 * and powerdown modes on hardware that supports disabling hsync/vsync:
281 * blank_mode == 2: suspend vsync
282 * blank_mode == 3: suspend hsync
283 * blank_mode == 4: powerdown
285 * Returns negative errno on error, or zero on success.
288 static int epson1355fb_blank(int blank_mode, struct fb_info *info)
290 struct epson1355_par *par = info->par;
292 switch (blank_mode) {
293 case FB_BLANK_UNBLANKING:
294 case FB_BLANK_NORMAL:
298 case FB_BLANK_VSYNC_SUSPEND:
299 case FB_BLANK_HSYNC_SUSPEND:
302 case FB_BLANK_POWERDOWN:
310 /* let fbcon do a soft blank for us */
311 return (blank_mode == FB_BLANK_NORMAL) ? 1 : 0;
314 /* ------------------------------------------------------------------------- */
317 * We can't use the cfb generic routines, as we have to limit
318 * ourselves to 16-bit or 8-bit loads and stores to this 16-bit
322 static inline void epson1355fb_fb_writel(unsigned long v, unsigned long *a)
332 static inline unsigned long epson1355fb_fb_readl(const unsigned long *a)
334 const u16 *p = (u16 *) a;
336 u16 h = fb_readw(p + 1);
338 return (h << 16) | l;
341 #define FB_READL epson1355fb_fb_readl
342 #define FB_WRITEL epson1355fb_fb_writel
344 /* ------------------------------------------------------------------------- */
346 static inline unsigned long copy_from_user16(void *to, const void *from,
349 u16 *dst = (u16 *) to;
350 u16 *src = (u16 *) from;
352 if (!access_ok(VERIFY_READ, from, n))
357 if (__get_user(v, src))
369 if (__get_user(v, ((u8 *) src)))
377 static inline unsigned long copy_to_user16(void *to, const void *from,
380 u16 *dst = (u16 *) to;
381 u16 *src = (u16 *) from;
383 if (!access_ok(VERIFY_WRITE, to, n))
387 u16 v = fb_readw(src);
389 if (__put_user(v, dst))
397 u8 v = fb_readb(src);
399 if (__put_user(v, ((u8 *) dst)))
407 epson1355fb_read(struct file *file, char *buf, size_t count, loff_t * ppos)
409 struct inode *inode = file->f_dentry->d_inode;
410 int fbidx = iminor(inode);
411 struct fb_info *info = registered_fb[fbidx];
412 unsigned long p = *ppos;
414 /* from fbmem.c except for our own copy_*_user */
415 if (!info || !info->screen_base)
418 if (p >= info->fix.smem_len)
420 if (count >= info->fix.smem_len)
421 count = info->fix.smem_len;
422 if (count + p > info->fix.smem_len)
423 count = info->fix.smem_len - p;
428 base_addr = info->screen_base;
429 count -= copy_to_user16(buf, base_addr + p, count);
438 epson1355fb_write(struct file *file, const char *buf,
439 size_t count, loff_t * ppos)
441 struct inode *inode = file->f_dentry->d_inode;
442 int fbidx = iminor(inode);
443 struct fb_info *info = registered_fb[fbidx];
444 unsigned long p = *ppos;
447 /* from fbmem.c except for our own copy_*_user */
448 if (!info || !info->screen_base)
451 /* from fbmem.c except for our own copy_*_user */
452 if (p > info->fix.smem_len)
454 if (count >= info->fix.smem_len)
455 count = info->fix.smem_len;
457 if (count + p > info->fix.smem_len) {
458 count = info->fix.smem_len - p;
465 base_addr = info->screen_base;
466 count -= copy_from_user16(base_addr + p, buf, count);
475 /* ------------------------------------------------------------------------- */
477 static struct fb_ops epson1355fb_fbops = {
478 .owner = THIS_MODULE,
479 .fb_setcolreg = epson1355fb_setcolreg,
480 .fb_pan_display = epson1355fb_pan_display,
481 .fb_blank = epson1355fb_blank,
482 .fb_fillrect = cfb_fillrect,
483 .fb_copyarea = cfb_copyarea,
484 .fb_imageblit = cfb_imageblit,
485 .fb_read = epson1355fb_read,
486 .fb_write = epson1355fb_write,
487 .fb_cursor = soft_cursor,
490 /* ------------------------------------------------------------------------- */
492 static __init unsigned int get_fb_size(struct fb_info *info)
494 unsigned int size = 2 * 1024 * 1024;
495 char *p = info->screen_base;
497 /* the 512k framebuffer is aliased at start + 0x80000 * n */
499 fb_writeb(0, p + 0x80000);
508 static int epson1355_width_tab[2][4] __initdata =
509 { {4, 8, 16, -1}, {9, 12, 16, -1} };
510 static int epson1355_bpp_tab[8] __initdata = { 1, 2, 4, 8, 15, 16 };
512 static void __init fetch_hw_state(struct fb_info *info, struct epson1355_par *par)
514 struct fb_var_screeninfo *var = &info->var;
515 struct fb_fix_screeninfo *fix = &info->fix;
519 u32 xres_virtual, yres_virtual;
521 int is_color, is_dual, is_tft;
522 int lcd_enabled, crt_enabled;
524 fix->type = FB_TYPE_PACKED_PIXELS;
526 display = epson1355_read_reg(par, REG_DISPLAY_MODE);
527 bpp = epson1355_bpp_tab[(display >> 2) & 7];
531 fix->visual = FB_VISUAL_PSEUDOCOLOR;
532 var->bits_per_pixel = 8;
533 var->red.offset = var->green.offset = var->blue.offset = 0;
534 var->red.length = var->green.length = var->blue.length = 8;
538 fix->visual = FB_VISUAL_TRUECOLOR;
539 var->bits_per_pixel = 16;
540 var->red.offset = 11;
542 var->green.offset = 5;
543 var->green.length = 6;
544 var->blue.offset = 0;
545 var->blue.length = 5;
550 fb_alloc_cmap(&(info->cmap), 256, 0);
552 panel = epson1355_read_reg(par, REG_PANEL_TYPE);
553 is_color = (panel & 0x04) != 0;
554 is_dual = (panel & 0x02) != 0;
555 is_tft = (panel & 0x01) != 0;
556 crt_enabled = (display & 0x02) != 0;
557 lcd_enabled = (display & 0x01) != 0;
558 lcd_bpp = epson1355_width_tab[is_tft][(panel >> 4) & 3];
560 xres = (epson1355_read_reg(par, REG_HORZ_DISP_WIDTH) + 1) * 8;
561 yres = (epson1355_read_reg16(par, REG_VERT_DISP_HEIGHT0) + 1) *
562 ((is_dual && !crt_enabled) ? 2 : 1);
563 offset = epson1355_read_reg16(par, REG_MEM_ADDR_OFFSET0) & 0x7ff;
564 xres_virtual = offset * 16 / bpp;
565 yres_virtual = fix->smem_len / (offset * 2);
569 var->xres_virtual = xres_virtual;
570 var->yres_virtual = yres_virtual;
571 var->xoffset = var->yoffset = 0;
573 fix->line_length = offset * 2;
575 fix->xpanstep = 0; /* no pan yet */
578 fix->accel = FB_ACCEL_NONE;
580 var->grayscale = !is_color;
584 "epson1355fb: xres=%d, yres=%d, "
585 "is_color=%d, is_dual=%d, is_tft=%d\n",
586 xres, yres, is_color, is_dual, is_tft);
588 "epson1355fb: bpp=%d, lcd_bpp=%d, "
589 "crt_enabled=%d, lcd_enabled=%d\n",
590 bpp, lcd_bpp, crt_enabled, lcd_enabled);
595 static void clearfb16(struct fb_info *info)
597 u16 *dst = (u16 *) info->screen_base;
598 unsigned long n = info->fix.smem_len;
609 static void epson1355fb_platform_release(struct device *device)
613 static int epson1355fb_remove(struct device *device)
615 struct fb_info *info = dev_get_drvdata(device);
616 struct epson1355_par *par = info->par;
621 if (par && par->reg_addr)
622 iounmap((void *) par->reg_addr);
626 fb_dealloc_cmap(&info->cmap);
627 if (info->screen_base)
628 iounmap(info->screen_base);
629 framebuffer_release(info);
631 release_mem_region(EPSON1355FB_FB_PHYS, EPSON1355FB_FB_LEN);
632 release_mem_region(EPSON1355FB_REGS_PHYS, EPSON1355FB_REGS_LEN);
636 int __init epson1355fb_probe(struct device *device)
638 struct platform_device *dev = to_platform_device(device);
639 struct epson1355_par *default_par;
640 struct fb_info *info;
644 if (!request_mem_region(EPSON1355FB_REGS_PHYS, EPSON1355FB_REGS_LEN, "S1D13505 registers")) {
645 printk(KERN_ERR "epson1355fb: unable to reserve "
646 "registers at 0x%0x\n", EPSON1355FB_REGS_PHYS);
651 if (!request_mem_region(EPSON1355FB_FB_PHYS, EPSON1355FB_FB_LEN,
652 "S1D13505 framebuffer")) {
653 printk(KERN_ERR "epson1355fb: unable to reserve "
654 "framebuffer at 0x%0x\n", EPSON1355FB_FB_PHYS);
659 info = framebuffer_alloc(sizeof(struct epson1355_par) + sizeof(u32) * 256, &dev->dev);
664 default_par = info->par;
665 default_par->reg_addr = (unsigned long) ioremap(EPSON1355FB_REGS_PHYS, EPSON1355FB_REGS_LEN);
666 if (!default_par->reg_addr) {
667 printk(KERN_ERR "epson1355fb: unable to map registers\n");
671 info->pseudo_palette = (void *)(default_par + 1);
673 info->screen_base = ioremap(EPSON1355FB_FB_PHYS, EPSON1355FB_FB_LEN);
674 if (!info->screen_base) {
675 printk(KERN_ERR "epson1355fb: unable to map framebuffer\n");
680 revision = epson1355_read_reg(default_par, REG_REVISION_CODE);
681 if ((revision >> 2) != 3) {
682 printk(KERN_INFO "epson1355fb: epson1355 not found\n");
687 info->fix.mmio_start = EPSON1355FB_REGS_PHYS;
688 info->fix.mmio_len = EPSON1355FB_REGS_LEN;
689 info->fix.smem_start = EPSON1355FB_FB_PHYS;
690 info->fix.smem_len = get_fb_size(info);
692 printk(KERN_INFO "epson1355fb: regs mapped at 0x%lx, fb %d KiB mapped at 0x%p\n",
693 default_par->reg_addr, info->fix.smem_len / 1024, info->screen_base);
695 strcpy(info->fix.id, "S1D13505");
696 info->par = default_par;
697 info->fbops = &epson1355fb_fbops;
698 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
700 /* we expect the boot loader to have initialized the chip
701 with appropriate parameters from which we can determinte
702 the flavor of lcd panel attached */
703 fetch_hw_state(info, default_par);
705 /* turn this puppy on ... */
708 lcd_enable(default_par, 1);
710 if (register_framebuffer(info) < 0) {
717 dev_set_drvdata(&dev->dev, info);
719 printk(KERN_INFO "fb%d: %s frame buffer device\n",
720 info->node, info->fix.id);
725 epson1355fb_remove(device);
729 static struct device_driver epson1355fb_driver = {
730 .name = "epson1355fb",
731 .bus = &platform_bus_type,
732 .probe = epson1355fb_probe,
733 .remove = epson1355fb_remove,
736 static struct platform_device epson1355fb_device = {
737 .name = "epson1355fb",
740 .release = epson1355fb_platform_release,
744 int __init epson1355fb_init(void)
748 if (fb_get_options("epson1355fb", NULL))
751 ret = driver_register(&epson1355fb_driver);
753 ret = platform_device_register(&epson1355fb_device);
755 driver_unregister(&epson1355fb_driver);
760 module_init(epson1355fb_init);
763 static void __exit epson1355fb_exit(void)
765 platform_device_unregister(&epson1355fb_device);
766 driver_unregister(&epson1355fb_driver);
769 /* ------------------------------------------------------------------------- */
771 module_exit(epson1355fb_exit);
774 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>");
775 MODULE_DESCRIPTION("Framebuffer driver for Epson S1D13505");
776 MODULE_LICENSE("GPL");