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 "vt8500lcdfb.h"
34 #include "wmt_ge_rops.h"
38 #include <linux/of_fdt.h>
39 #include <linux/memblock.h>
43 #define to_vt8500lcd_info(__info) container_of(__info, \
44 struct vt8500lcd_info, fb)
46 static int vt8500lcd_set_par(struct fb_info *info)
48 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
49 int reg_bpp = 5; /* 16bpp */
51 unsigned long control0;
56 if (info->var.bits_per_pixel <= 8) {
58 info->var.red.offset = 0;
59 info->var.red.length = info->var.bits_per_pixel;
60 info->var.red.msb_right = 0;
62 info->var.green.offset = 0;
63 info->var.green.length = info->var.bits_per_pixel;
64 info->var.green.msb_right = 0;
66 info->var.blue.offset = 0;
67 info->var.blue.length = info->var.bits_per_pixel;
68 info->var.blue.msb_right = 0;
70 info->var.transp.offset = 0;
71 info->var.transp.length = 0;
72 info->var.transp.msb_right = 0;
74 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
75 info->fix.line_length = info->var.xres_virtual /
76 (8/info->var.bits_per_pixel);
79 info->var.transp.offset = 0;
80 info->var.transp.length = 0;
81 info->var.transp.msb_right = 0;
83 if (info->var.bits_per_pixel == 16) {
85 info->var.red.offset = 11;
86 info->var.red.length = 5;
87 info->var.red.msb_right = 0;
88 info->var.green.offset = 5;
89 info->var.green.length = 6;
90 info->var.green.msb_right = 0;
91 info->var.blue.offset = 0;
92 info->var.blue.length = 5;
93 info->var.blue.msb_right = 0;
95 /* Equal depths per channel */
96 info->var.red.offset = info->var.bits_per_pixel
98 info->var.red.length = info->var.bits_per_pixel / 3;
99 info->var.red.msb_right = 0;
100 info->var.green.offset = info->var.bits_per_pixel / 3;
101 info->var.green.length = info->var.bits_per_pixel / 3;
102 info->var.green.msb_right = 0;
103 info->var.blue.offset = 0;
104 info->var.blue.length = info->var.bits_per_pixel / 3;
105 info->var.blue.msb_right = 0;
108 info->fix.visual = FB_VISUAL_TRUECOLOR;
109 info->fix.line_length = info->var.bits_per_pixel > 16 ?
110 info->var.xres_virtual << 2 :
111 info->var.xres_virtual << 1;
114 for (i = 0; i < 8; i++) {
115 if (bpp_values[i] == info->var.bits_per_pixel) {
121 control0 = readl(fbi->regbase) & ~0xf;
122 writel(0, fbi->regbase);
123 while (readl(fbi->regbase + 0x38) & 0x10)
125 writel((((info->var.hsync_len - 1) & 0x3f) << 26)
126 | ((info->var.left_margin & 0xff) << 18)
127 | (((info->var.xres - 1) & 0x3ff) << 8)
128 | (info->var.right_margin & 0xff), fbi->regbase + 0x4);
129 writel((((info->var.vsync_len - 1) & 0x3f) << 26)
130 | ((info->var.upper_margin & 0xff) << 18)
131 | (((info->var.yres - 1) & 0x3ff) << 8)
132 | (info->var.lower_margin & 0xff), fbi->regbase + 0x8);
133 writel((((info->var.yres - 1) & 0x400) << 2)
134 | ((info->var.xres - 1) & 0x400), fbi->regbase + 0x10);
135 writel(0x80000000, fbi->regbase + 0x20);
136 writel(control0 | (reg_bpp << 1) | 0x100, fbi->regbase);
141 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
144 chan >>= 16 - bf->length;
145 return chan << bf->offset;
148 static int vt8500lcd_setcolreg(unsigned regno, unsigned red, unsigned green,
149 unsigned blue, unsigned transp,
150 struct fb_info *info) {
151 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
157 if (info->var.grayscale)
159 (19595 * red + 38470 * green + 7471 * blue) >> 16;
161 switch (fbi->fb.fix.visual) {
162 case FB_VISUAL_TRUECOLOR:
164 u32 *pal = fbi->fb.pseudo_palette;
166 val = chan_to_field(red, &fbi->fb.var.red);
167 val |= chan_to_field(green, &fbi->fb.var.green);
168 val |= chan_to_field(blue, &fbi->fb.var.blue);
175 case FB_VISUAL_STATIC_PSEUDOCOLOR:
176 case FB_VISUAL_PSEUDOCOLOR:
177 writew((red & 0xf800)
178 | ((green >> 5) & 0x7e0)
179 | ((blue >> 11) & 0x1f),
180 fbi->palette_cpu + sizeof(u16) * regno);
187 static int vt8500lcd_ioctl(struct fb_info *info, unsigned int cmd,
191 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
193 if (cmd == FBIO_WAITFORVSYNC) {
194 /* Unmask End of Frame interrupt */
195 writel(0xffffffff ^ (1 << 3), fbi->regbase + 0x3c);
196 ret = wait_event_interruptible_timeout(fbi->wait,
197 readl(fbi->regbase + 0x38) & (1 << 3), HZ / 10);
198 /* Mask back to reduce unwanted interrupt traffic */
199 writel(0xffffffff, fbi->regbase + 0x3c);
209 static int vt8500lcd_pan_display(struct fb_var_screeninfo *var,
210 struct fb_info *info)
212 unsigned pixlen = info->fix.line_length / info->var.xres_virtual;
213 unsigned off = pixlen * var->xoffset
214 + info->fix.line_length * var->yoffset;
215 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
218 | (((info->var.xres_virtual - info->var.xres) * pixlen / 4) << 20)
219 | (off >> 2), fbi->regbase + 0x20);
225 * Blank the display by setting all palette values to zero. Note,
226 * True Color modes do not really use the palette, so this will not
227 * blank the display in all modes.
229 static int vt8500lcd_blank(int blank, struct fb_info *info)
234 case FB_BLANK_POWERDOWN:
235 case FB_BLANK_VSYNC_SUSPEND:
236 case FB_BLANK_HSYNC_SUSPEND:
237 case FB_BLANK_NORMAL:
238 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR ||
239 info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
240 for (i = 0; i < 256; i++)
241 vt8500lcd_setcolreg(i, 0, 0, 0, 0, info);
242 case FB_BLANK_UNBLANK:
243 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR ||
244 info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
245 fb_set_cmap(&info->cmap, info);
250 static struct fb_ops vt8500lcd_ops = {
251 .owner = THIS_MODULE,
252 .fb_set_par = vt8500lcd_set_par,
253 .fb_setcolreg = vt8500lcd_setcolreg,
254 .fb_fillrect = wmt_ge_fillrect,
255 .fb_copyarea = wmt_ge_copyarea,
256 .fb_imageblit = sys_imageblit,
257 .fb_sync = wmt_ge_sync,
258 .fb_ioctl = vt8500lcd_ioctl,
259 .fb_pan_display = vt8500lcd_pan_display,
260 .fb_blank = vt8500lcd_blank,
263 static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id)
265 struct vt8500lcd_info *fbi = dev_id;
267 if (readl(fbi->regbase + 0x38) & (1 << 3))
268 wake_up_interruptible(&fbi->wait);
270 writel(0xffffffff, fbi->regbase + 0x38);
274 static int vt8500lcd_probe(struct platform_device *pdev)
276 struct vt8500lcd_info *fbi;
277 struct resource *res;
281 struct fb_videomode of_mode;
282 struct device_node *np;
284 dma_addr_t fb_mem_phys;
285 unsigned long fb_mem_len;
291 fbi = devm_kzalloc(&pdev->dev, sizeof(struct vt8500lcd_info)
292 + sizeof(u32) * 16, GFP_KERNEL);
294 dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
299 strcpy(fbi->fb.fix.id, "VT8500 LCD");
301 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
302 fbi->fb.fix.xpanstep = 0;
303 fbi->fb.fix.ypanstep = 1;
304 fbi->fb.fix.ywrapstep = 0;
305 fbi->fb.fix.accel = FB_ACCEL_NONE;
307 fbi->fb.var.nonstd = 0;
308 fbi->fb.var.activate = FB_ACTIVATE_NOW;
309 fbi->fb.var.height = -1;
310 fbi->fb.var.width = -1;
311 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
313 fbi->fb.fbops = &vt8500lcd_ops;
314 fbi->fb.flags = FBINFO_DEFAULT
315 | FBINFO_HWACCEL_COPYAREA
316 | FBINFO_HWACCEL_FILLRECT
317 | FBINFO_HWACCEL_YPAN
319 | FBINFO_PARTIAL_PAN_OK;
323 addr = addr + sizeof(struct vt8500lcd_info);
324 fbi->fb.pseudo_palette = addr;
326 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
328 dev_err(&pdev->dev, "no I/O memory resource defined\n");
333 res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
335 dev_err(&pdev->dev, "failed to request I/O memory\n");
340 fbi->regbase = ioremap(res->start, resource_size(res));
341 if (fbi->regbase == NULL) {
342 dev_err(&pdev->dev, "failed to map I/O memory\n");
344 goto failed_free_res;
347 np = of_parse_phandle(pdev->dev.of_node, "default-mode", 0);
349 pr_err("%s: No display description in Device Tree\n", __func__);
351 goto failed_free_res;
355 * This code is copied from Sascha Hauer's of_videomode helper
356 * and can be replaced with a call to the helper once mainlined
359 ret |= of_property_read_u32(np, "hactive", &of_mode.xres);
360 ret |= of_property_read_u32(np, "vactive", &of_mode.yres);
361 ret |= of_property_read_u32(np, "hback-porch", &of_mode.left_margin);
362 ret |= of_property_read_u32(np, "hfront-porch", &of_mode.right_margin);
363 ret |= of_property_read_u32(np, "hsync-len", &of_mode.hsync_len);
364 ret |= of_property_read_u32(np, "vback-porch", &of_mode.upper_margin);
365 ret |= of_property_read_u32(np, "vfront-porch", &of_mode.lower_margin);
366 ret |= of_property_read_u32(np, "vsync-len", &of_mode.vsync_len);
367 ret |= of_property_read_u32(np, "bpp", &bpp);
369 pr_err("%s: Unable to read display properties\n", __func__);
370 goto failed_free_res;
372 of_mode.vmode = FB_VMODE_NONINTERLACED;
374 /* try allocating the framebuffer */
375 fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8);
376 fb_mem_virt = dma_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
379 pr_err("%s: Failed to allocate framebuffer\n", __func__);
383 fbi->fb.fix.smem_start = fb_mem_phys;
384 fbi->fb.fix.smem_len = fb_mem_len;
385 fbi->fb.screen_base = fb_mem_virt;
387 fbi->palette_size = PAGE_ALIGN(512);
388 fbi->palette_cpu = dma_alloc_coherent(&pdev->dev,
392 if (fbi->palette_cpu == NULL) {
393 dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
398 irq = platform_get_irq(pdev, 0);
400 dev_err(&pdev->dev, "no IRQ defined\n");
402 goto failed_free_palette;
405 ret = request_irq(irq, vt8500lcd_handle_irq, 0, "LCD", fbi);
407 dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
409 goto failed_free_palette;
412 init_waitqueue_head(&fbi->wait);
414 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
415 dev_err(&pdev->dev, "Failed to allocate color map\n");
417 goto failed_free_irq;
420 fb_videomode_to_var(&fbi->fb.var, &of_mode);
422 fbi->fb.var.xres_virtual = of_mode.xres;
423 fbi->fb.var.yres_virtual = of_mode.yres * 2;
424 fbi->fb.var.bits_per_pixel = bpp;
426 ret = vt8500lcd_set_par(&fbi->fb);
428 dev_err(&pdev->dev, "Failed to set parameters\n");
429 goto failed_free_cmap;
432 writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
433 writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
435 platform_set_drvdata(pdev, fbi);
437 ret = register_framebuffer(&fbi->fb);
440 "Failed to register framebuffer device: %d\n", ret);
441 goto failed_free_cmap;
445 * Ok, now enable the LCD controller
447 writel(readl(fbi->regbase) | 1, fbi->regbase);
452 if (fbi->fb.cmap.len)
453 fb_dealloc_cmap(&fbi->fb.cmap);
457 dma_free_coherent(&pdev->dev, fbi->palette_size,
458 fbi->palette_cpu, fbi->palette_phys);
460 iounmap(fbi->regbase);
462 release_mem_region(res->start, resource_size(res));
464 platform_set_drvdata(pdev, NULL);
470 static int vt8500lcd_remove(struct platform_device *pdev)
472 struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
473 struct resource *res;
476 unregister_framebuffer(&fbi->fb);
478 writel(0, fbi->regbase);
480 if (fbi->fb.cmap.len)
481 fb_dealloc_cmap(&fbi->fb.cmap);
483 irq = platform_get_irq(pdev, 0);
486 dma_free_coherent(&pdev->dev, fbi->palette_size,
487 fbi->palette_cpu, fbi->palette_phys);
489 iounmap(fbi->regbase);
491 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
492 release_mem_region(res->start, resource_size(res));
499 static const struct of_device_id via_dt_ids[] = {
500 { .compatible = "via,vt8500-fb", },
504 static struct platform_driver vt8500lcd_driver = {
505 .probe = vt8500lcd_probe,
506 .remove = vt8500lcd_remove,
508 .owner = THIS_MODULE,
509 .name = "vt8500-lcd",
510 .of_match_table = of_match_ptr(via_dt_ids),
514 module_platform_driver(vt8500lcd_driver);
516 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
517 MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
518 MODULE_LICENSE("GPL v2");
519 MODULE_DEVICE_TABLE(of, via_dt_ids);