2 * linux/drivers/video/vt8500lcdfb.c
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
6 * Based on skeletonfb.c and pxafb.c
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/platform_device.h>
31 #include <linux/wait.h>
33 #include <mach/vt8500fb.h>
35 #include "vt8500lcdfb.h"
36 #include "wmt_ge_rops.h"
38 #define to_vt8500lcd_info(__info) container_of(__info, \
39 struct vt8500lcd_info, fb)
41 static int vt8500lcd_set_par(struct fb_info *info)
43 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
44 int reg_bpp = 5; /* 16bpp */
46 unsigned long control0;
51 if (info->var.bits_per_pixel <= 8) {
53 info->var.red.offset = 0;
54 info->var.red.length = info->var.bits_per_pixel;
55 info->var.red.msb_right = 0;
57 info->var.green.offset = 0;
58 info->var.green.length = info->var.bits_per_pixel;
59 info->var.green.msb_right = 0;
61 info->var.blue.offset = 0;
62 info->var.blue.length = info->var.bits_per_pixel;
63 info->var.blue.msb_right = 0;
65 info->var.transp.offset = 0;
66 info->var.transp.length = 0;
67 info->var.transp.msb_right = 0;
69 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
70 info->fix.line_length = info->var.xres_virtual /
71 (8/info->var.bits_per_pixel);
74 info->var.transp.offset = 0;
75 info->var.transp.length = 0;
76 info->var.transp.msb_right = 0;
78 if (info->var.bits_per_pixel == 16) {
80 info->var.red.offset = 11;
81 info->var.red.length = 5;
82 info->var.red.msb_right = 0;
83 info->var.green.offset = 5;
84 info->var.green.length = 6;
85 info->var.green.msb_right = 0;
86 info->var.blue.offset = 0;
87 info->var.blue.length = 5;
88 info->var.blue.msb_right = 0;
90 /* Equal depths per channel */
91 info->var.red.offset = info->var.bits_per_pixel
93 info->var.red.length = info->var.bits_per_pixel / 3;
94 info->var.red.msb_right = 0;
95 info->var.green.offset = info->var.bits_per_pixel / 3;
96 info->var.green.length = info->var.bits_per_pixel / 3;
97 info->var.green.msb_right = 0;
98 info->var.blue.offset = 0;
99 info->var.blue.length = info->var.bits_per_pixel / 3;
100 info->var.blue.msb_right = 0;
103 info->fix.visual = FB_VISUAL_TRUECOLOR;
104 info->fix.line_length = info->var.bits_per_pixel > 16 ?
105 info->var.xres_virtual << 2 :
106 info->var.xres_virtual << 1;
109 for (i = 0; i < 8; i++) {
110 if (bpp_values[i] == info->var.bits_per_pixel) {
116 control0 = readl(fbi->regbase) & ~0xf;
117 writel(0, fbi->regbase);
118 while (readl(fbi->regbase + 0x38) & 0x10)
120 writel((((info->var.hsync_len - 1) & 0x3f) << 26)
121 | ((info->var.left_margin & 0xff) << 18)
122 | (((info->var.xres - 1) & 0x3ff) << 8)
123 | (info->var.right_margin & 0xff), fbi->regbase + 0x4);
124 writel((((info->var.vsync_len - 1) & 0x3f) << 26)
125 | ((info->var.upper_margin & 0xff) << 18)
126 | (((info->var.yres - 1) & 0x3ff) << 8)
127 | (info->var.lower_margin & 0xff), fbi->regbase + 0x8);
128 writel((((info->var.yres - 1) & 0x400) << 2)
129 | ((info->var.xres - 1) & 0x400), fbi->regbase + 0x10);
130 writel(0x80000000, fbi->regbase + 0x20);
131 writel(control0 | (reg_bpp << 1) | 0x100, fbi->regbase);
136 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
139 chan >>= 16 - bf->length;
140 return chan << bf->offset;
143 static int vt8500lcd_setcolreg(unsigned regno, unsigned red, unsigned green,
144 unsigned blue, unsigned transp,
145 struct fb_info *info) {
146 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
152 if (info->var.grayscale)
154 (19595 * red + 38470 * green + 7471 * blue) >> 16;
156 switch (fbi->fb.fix.visual) {
157 case FB_VISUAL_TRUECOLOR:
159 u32 *pal = fbi->fb.pseudo_palette;
161 val = chan_to_field(red, &fbi->fb.var.red);
162 val |= chan_to_field(green, &fbi->fb.var.green);
163 val |= chan_to_field(blue, &fbi->fb.var.blue);
170 case FB_VISUAL_STATIC_PSEUDOCOLOR:
171 case FB_VISUAL_PSEUDOCOLOR:
172 writew((red & 0xf800)
173 | ((green >> 5) & 0x7e0)
174 | ((blue >> 11) & 0x1f),
175 fbi->palette_cpu + sizeof(u16) * regno);
182 static int vt8500lcd_ioctl(struct fb_info *info, unsigned int cmd,
186 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
188 if (cmd == FBIO_WAITFORVSYNC) {
189 /* Unmask End of Frame interrupt */
190 writel(0xffffffff ^ (1 << 3), fbi->regbase + 0x3c);
191 ret = wait_event_interruptible_timeout(fbi->wait,
192 readl(fbi->regbase + 0x38) & (1 << 3), HZ / 10);
193 /* Mask back to reduce unwanted interrupt traffic */
194 writel(0xffffffff, fbi->regbase + 0x3c);
204 static int vt8500lcd_pan_display(struct fb_var_screeninfo *var,
205 struct fb_info *info)
207 unsigned pixlen = info->fix.line_length / info->var.xres_virtual;
208 unsigned off = pixlen * var->xoffset
209 + info->fix.line_length * var->yoffset;
210 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
213 | (((var->xres_virtual - var->xres) * pixlen / 4) << 20)
214 | (off >> 2), fbi->regbase + 0x20);
218 static struct fb_ops vt8500lcd_ops = {
219 .owner = THIS_MODULE,
220 .fb_set_par = vt8500lcd_set_par,
221 .fb_setcolreg = vt8500lcd_setcolreg,
222 .fb_fillrect = wmt_ge_fillrect,
223 .fb_copyarea = wmt_ge_copyarea,
224 .fb_imageblit = sys_imageblit,
225 .fb_sync = wmt_ge_sync,
226 .fb_ioctl = vt8500lcd_ioctl,
227 .fb_pan_display = vt8500lcd_pan_display,
230 static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id)
232 struct vt8500lcd_info *fbi = dev_id;
234 if (readl(fbi->regbase + 0x38) & (1 << 3))
235 wake_up_interruptible(&fbi->wait);
237 writel(0xffffffff, fbi->regbase + 0x38);
241 static int __devinit vt8500lcd_probe(struct platform_device *pdev)
243 struct vt8500lcd_info *fbi;
244 struct resource *res;
245 struct vt8500fb_platform_data *pdata = pdev->dev.platform_data;
252 fbi = kzalloc(sizeof(struct vt8500lcd_info) + sizeof(u32) * 16,
255 dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
260 strcpy(fbi->fb.fix.id, "VT8500 LCD");
262 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
263 fbi->fb.fix.xpanstep = 0;
264 fbi->fb.fix.ypanstep = 1;
265 fbi->fb.fix.ywrapstep = 0;
266 fbi->fb.fix.accel = FB_ACCEL_NONE;
268 fbi->fb.var.nonstd = 0;
269 fbi->fb.var.activate = FB_ACTIVATE_NOW;
270 fbi->fb.var.height = -1;
271 fbi->fb.var.width = -1;
272 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
274 fbi->fb.fbops = &vt8500lcd_ops;
275 fbi->fb.flags = FBINFO_DEFAULT
276 | FBINFO_HWACCEL_COPYAREA
277 | FBINFO_HWACCEL_FILLRECT
278 | FBINFO_HWACCEL_YPAN
280 | FBINFO_PARTIAL_PAN_OK;
284 addr = addr + sizeof(struct vt8500lcd_info);
285 fbi->fb.pseudo_palette = addr;
287 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289 dev_err(&pdev->dev, "no I/O memory resource defined\n");
294 res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
296 dev_err(&pdev->dev, "failed to request I/O memory\n");
301 fbi->regbase = ioremap(res->start, resource_size(res));
302 if (fbi->regbase == NULL) {
303 dev_err(&pdev->dev, "failed to map I/O memory\n");
305 goto failed_free_res;
308 fbi->fb.fix.smem_start = pdata->video_mem_phys;
309 fbi->fb.fix.smem_len = pdata->video_mem_len;
310 fbi->fb.screen_base = pdata->video_mem_virt;
312 fbi->palette_size = PAGE_ALIGN(512);
313 fbi->palette_cpu = dma_alloc_coherent(&pdev->dev,
317 if (fbi->palette_cpu == NULL) {
318 dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
323 irq = platform_get_irq(pdev, 0);
325 dev_err(&pdev->dev, "no IRQ defined\n");
327 goto failed_free_palette;
330 ret = request_irq(irq, vt8500lcd_handle_irq, IRQF_DISABLED, "LCD", fbi);
332 dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
334 goto failed_free_palette;
337 init_waitqueue_head(&fbi->wait);
339 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
340 dev_err(&pdev->dev, "Failed to allocate color map\n");
342 goto failed_free_irq;
345 fb_videomode_to_var(&fbi->fb.var, &pdata->mode);
346 fbi->fb.var.bits_per_pixel = pdata->bpp;
347 fbi->fb.var.xres_virtual = pdata->xres_virtual;
348 fbi->fb.var.yres_virtual = pdata->yres_virtual;
350 ret = vt8500lcd_set_par(&fbi->fb);
352 dev_err(&pdev->dev, "Failed to set parameters\n");
353 goto failed_free_cmap;
356 writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
357 writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
359 platform_set_drvdata(pdev, fbi);
361 ret = register_framebuffer(&fbi->fb);
364 "Failed to register framebuffer device: %d\n", ret);
365 goto failed_free_cmap;
369 * Ok, now enable the LCD controller
371 writel(readl(fbi->regbase) | 1, fbi->regbase);
376 if (fbi->fb.cmap.len)
377 fb_dealloc_cmap(&fbi->fb.cmap);
381 dma_free_coherent(&pdev->dev, fbi->palette_size,
382 fbi->palette_cpu, fbi->palette_phys);
384 iounmap(fbi->regbase);
386 release_mem_region(res->start, resource_size(res));
388 platform_set_drvdata(pdev, NULL);
394 static int __devexit vt8500lcd_remove(struct platform_device *pdev)
396 struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
397 struct resource *res;
400 unregister_framebuffer(&fbi->fb);
402 writel(0, fbi->regbase);
404 if (fbi->fb.cmap.len)
405 fb_dealloc_cmap(&fbi->fb.cmap);
407 irq = platform_get_irq(pdev, 0);
410 dma_free_coherent(&pdev->dev, fbi->palette_size,
411 fbi->palette_cpu, fbi->palette_phys);
413 iounmap(fbi->regbase);
415 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
416 release_mem_region(res->start, resource_size(res));
423 static struct platform_driver vt8500lcd_driver = {
424 .probe = vt8500lcd_probe,
425 .remove = __devexit_p(vt8500lcd_remove),
427 .owner = THIS_MODULE,
428 .name = "vt8500-lcd",
432 static int __init vt8500lcd_init(void)
434 return platform_driver_register(&vt8500lcd_driver);
437 static void __exit vt8500lcd_exit(void)
439 platform_driver_unregister(&vt8500lcd_driver);
442 module_init(vt8500lcd_init);
443 module_exit(vt8500lcd_exit);
445 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
446 MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
447 MODULE_LICENSE("GPL");