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>
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/delay.h>
53 #include <linux/init.h>
54 #include <linux/ioport.h>
55 #include <linux/platform_device.h>
57 #include <asm/types.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)
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);
88 # error "no architecture-specific epson1355_{read,write}_reg"
91 #ifndef EPSON1355FB_BASE_PHYS
92 # error "EPSON1355FB_BASE_PHYS is not defined"
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)
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)
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:
178 ((u32 *) info->pseudo_palette)[regno] =
179 (r & 0xf800) | (g & 0xfc00) >> 5 | (b & 0xf800) >> 11;
182 case FB_VISUAL_PSEUDOCOLOR:
186 set_lut(par, regno, r >> 8, g >> 8, b >> 8);
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;
214 if (var->xoffset != 0) /* not yet ... */
217 if (var->yoffset + info->var.yres > info->var.yres_virtual)
220 start = (info->fix.line_length >> 1) * var->yoffset;
222 epson1355_write_reg20(par, start, REG_SCRN1_DISP_START_ADDR0);
227 /* ------------------------------------------------------------------------- */
229 static void lcd_enable(struct epson1355_par *par, int enable)
231 u8 mode = epson1355_read_reg(par, REG_DISPLAY_MODE);
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);
250 clps_writeb(pddr, PDDR);
253 static void backlight_enable(int enable)
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:
285 case FB_BLANK_VSYNC_SUSPEND:
286 case FB_BLANK_HSYNC_SUSPEND:
289 case FB_BLANK_POWERDOWN:
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
309 static inline void epson1355fb_fb_writel(unsigned long v, unsigned long *a)
319 static inline unsigned long epson1355fb_fb_readl(const unsigned long *a)
321 const u16 *p = (u16 *) a;
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,
336 u16 *dst = (u16 *) to;
337 u16 *src = (u16 *) from;
339 if (!access_ok(VERIFY_READ, from, n))
344 if (__get_user(v, src))
356 if (__get_user(v, ((u8 *) src)))
364 static inline unsigned long copy_to_user16(void *to, const void *from,
367 u16 *dst = (u16 *) to;
368 u16 *src = (u16 *) from;
370 if (!access_ok(VERIFY_WRITE, to, n))
374 u16 v = fb_readw(src);
376 if (__put_user(v, dst))
384 u8 v = fb_readb(src);
386 if (__put_user(v, ((u8 *) dst)))
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)
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;
408 base_addr = info->screen_base;
409 count -= copy_to_user16(buf, base_addr + p, count);
418 epson1355fb_write(struct fb_info *info, const char *buf,
419 size_t count, loff_t * ppos)
421 unsigned long p = *ppos;
424 /* from fbmem.c except for our own copy_*_user */
425 if (p > info->fix.smem_len)
427 if (count >= info->fix.smem_len)
428 count = info->fix.smem_len;
430 if (count + p > info->fix.smem_len) {
431 count = info->fix.smem_len - p;
438 base_addr = info->screen_base;
439 count -= copy_from_user16(base_addr + p, buf, count);
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 */
471 fb_writeb(0, p + 0x80000);
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;
491 u32 xres_virtual, yres_virtual;
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];
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;
510 fix->visual = FB_VISUAL_TRUECOLOR;
511 var->bits_per_pixel = 16;
512 var->red.offset = 11;
514 var->green.offset = 5;
515 var->green.length = 6;
516 var->blue.offset = 0;
517 var->blue.length = 5;
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);
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 */
550 fix->accel = FB_ACCEL_NONE;
552 var->grayscale = !is_color;
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);
560 "epson1355fb: bpp=%d, lcd_bpp=%d, "
561 "crt_enabled=%d, lcd_enabled=%d\n",
562 bpp, lcd_bpp, crt_enabled, lcd_enabled);
567 static void clearfb16(struct fb_info *info)
569 u16 *dst = (u16 *) info->screen_base;
570 unsigned long n = info->fix.smem_len;
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;
589 if (par && par->reg_addr)
590 iounmap((void *) par->reg_addr);
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);
604 int __devinit epson1355fb_probe(struct platform_device *dev)
606 struct epson1355_par *default_par;
607 struct fb_info *info;
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);
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);
626 info = framebuffer_alloc(sizeof(struct epson1355_par), &dev->dev);
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");
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");
648 revision = epson1355_read_reg(default_par, REG_REVISION_CODE);
649 if ((revision >> 2) != 3) {
650 printk(KERN_INFO "epson1355fb: epson1355 not found\n");
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 ... */
676 lcd_enable(default_par, 1);
678 if (register_framebuffer(info) < 0) {
685 platform_set_drvdata(dev, info);
687 printk(KERN_INFO "fb%d: %s frame buffer device\n",
688 info->node, info->fix.id);
693 epson1355fb_remove(dev);
697 static struct platform_driver epson1355fb_driver = {
698 .probe = epson1355fb_probe,
699 .remove = epson1355fb_remove,
701 .name = "epson1355fb",
705 static struct platform_device *epson1355fb_device;
707 int __init epson1355fb_init(void)
711 if (fb_get_options("epson1355fb", NULL))
714 ret = platform_driver_register(&epson1355fb_driver);
717 epson1355fb_device = platform_device_alloc("epson1355fb", 0);
719 if (epson1355fb_device)
720 ret = platform_device_add(epson1355fb_device);
725 platform_device_put(epson1355fb_device);
726 platform_driver_unregister(&epson1355fb_driver);
733 module_init(epson1355fb_init);
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);
747 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>");
748 MODULE_DESCRIPTION("Framebuffer driver for Epson S1D13505");
749 MODULE_LICENSE("GPL");