1/* 2 * linux/drivers/video/vt8500lcdfb.c 3 * 4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com> 5 * 6 * Based on skeletonfb.c and pxafb.c 7 * 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. 11 * 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. 16 */ 17 18#include <linux/delay.h> 19#include <linux/dma-mapping.h> 20#include <linux/errno.h> 21#include <linux/fb.h> 22#include <linux/init.h> 23#include <linux/interrupt.h> 24#include <linux/io.h> 25#include <linux/kernel.h> 26#include <linux/mm.h> 27#include <linux/module.h> 28#include <linux/platform_device.h> 29#include <linux/slab.h> 30#include <linux/string.h> 31#include <linux/wait.h> 32#include <video/of_display_timing.h> 33 34#include "vt8500lcdfb.h" 35#include "wmt_ge_rops.h" 36 37#ifdef CONFIG_OF 38#include <linux/of.h> 39#include <linux/of_fdt.h> 40#include <linux/memblock.h> 41#endif 42 43 44#define to_vt8500lcd_info(__info) container_of(__info, \ 45 struct vt8500lcd_info, fb) 46 47static int vt8500lcd_set_par(struct fb_info *info) 48{ 49 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info); 50 int reg_bpp = 5; /* 16bpp */ 51 int i; 52 unsigned long control0; 53 54 if (!fbi) 55 return -EINVAL; 56 57 if (info->var.bits_per_pixel <= 8) { 58 /* palettized */ 59 info->var.red.offset = 0; 60 info->var.red.length = info->var.bits_per_pixel; 61 info->var.red.msb_right = 0; 62 63 info->var.green.offset = 0; 64 info->var.green.length = info->var.bits_per_pixel; 65 info->var.green.msb_right = 0; 66 67 info->var.blue.offset = 0; 68 info->var.blue.length = info->var.bits_per_pixel; 69 info->var.blue.msb_right = 0; 70 71 info->var.transp.offset = 0; 72 info->var.transp.length = 0; 73 info->var.transp.msb_right = 0; 74 75 info->fix.visual = FB_VISUAL_PSEUDOCOLOR; 76 info->fix.line_length = info->var.xres_virtual / 77 (8/info->var.bits_per_pixel); 78 } else { 79 /* non-palettized */ 80 info->var.transp.offset = 0; 81 info->var.transp.length = 0; 82 info->var.transp.msb_right = 0; 83 84 if (info->var.bits_per_pixel == 16) { 85 /* RGB565 */ 86 info->var.red.offset = 11; 87 info->var.red.length = 5; 88 info->var.red.msb_right = 0; 89 info->var.green.offset = 5; 90 info->var.green.length = 6; 91 info->var.green.msb_right = 0; 92 info->var.blue.offset = 0; 93 info->var.blue.length = 5; 94 info->var.blue.msb_right = 0; 95 } else { 96 /* Equal depths per channel */ 97 info->var.red.offset = info->var.bits_per_pixel 98 * 2 / 3; 99 info->var.red.length = info->var.bits_per_pixel / 3; 100 info->var.red.msb_right = 0; 101 info->var.green.offset = info->var.bits_per_pixel / 3; 102 info->var.green.length = info->var.bits_per_pixel / 3; 103 info->var.green.msb_right = 0; 104 info->var.blue.offset = 0; 105 info->var.blue.length = info->var.bits_per_pixel / 3; 106 info->var.blue.msb_right = 0; 107 } 108 109 info->fix.visual = FB_VISUAL_TRUECOLOR; 110 info->fix.line_length = info->var.bits_per_pixel > 16 ? 111 info->var.xres_virtual << 2 : 112 info->var.xres_virtual << 1; 113 } 114 115 for (i = 0; i < 8; i++) { 116 if (bpp_values[i] == info->var.bits_per_pixel) 117 reg_bpp = i; 118 } 119 120 control0 = readl(fbi->regbase) & ~0xf; 121 writel(0, fbi->regbase); 122 while (readl(fbi->regbase + 0x38) & 0x10) 123 /* wait */; 124 writel((((info->var.hsync_len - 1) & 0x3f) << 26) 125 | ((info->var.left_margin & 0xff) << 18) 126 | (((info->var.xres - 1) & 0x3ff) << 8) 127 | (info->var.right_margin & 0xff), fbi->regbase + 0x4); 128 writel((((info->var.vsync_len - 1) & 0x3f) << 26) 129 | ((info->var.upper_margin & 0xff) << 18) 130 | (((info->var.yres - 1) & 0x3ff) << 8) 131 | (info->var.lower_margin & 0xff), fbi->regbase + 0x8); 132 writel((((info->var.yres - 1) & 0x400) << 2) 133 | ((info->var.xres - 1) & 0x400), fbi->regbase + 0x10); 134 writel(0x80000000, fbi->regbase + 0x20); 135 writel(control0 | (reg_bpp << 1) | 0x100, fbi->regbase); 136 137 return 0; 138} 139 140static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf) 141{ 142 chan &= 0xffff; 143 chan >>= 16 - bf->length; 144 return chan << bf->offset; 145} 146 147static int vt8500lcd_setcolreg(unsigned regno, unsigned red, unsigned green, 148 unsigned blue, unsigned transp, 149 struct fb_info *info) { 150 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info); 151 int ret = 1; 152 unsigned int val; 153 if (regno >= 256) 154 return -EINVAL; 155 156 if (info->var.grayscale) 157 red = green = blue = 158 (19595 * red + 38470 * green + 7471 * blue) >> 16; 159 160 switch (fbi->fb.fix.visual) { 161 case FB_VISUAL_TRUECOLOR: 162 if (regno < 16) { 163 u32 *pal = fbi->fb.pseudo_palette; 164 165 val = chan_to_field(red, &fbi->fb.var.red); 166 val |= chan_to_field(green, &fbi->fb.var.green); 167 val |= chan_to_field(blue, &fbi->fb.var.blue); 168 169 pal[regno] = val; 170 ret = 0; 171 } 172 break; 173 174 case FB_VISUAL_STATIC_PSEUDOCOLOR: 175 case FB_VISUAL_PSEUDOCOLOR: 176 writew((red & 0xf800) 177 | ((green >> 5) & 0x7e0) 178 | ((blue >> 11) & 0x1f), 179 fbi->palette_cpu + sizeof(u16) * regno); 180 break; 181 } 182 183 return ret; 184} 185 186static int vt8500lcd_ioctl(struct fb_info *info, unsigned int cmd, 187 unsigned long arg) 188{ 189 int ret = 0; 190 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info); 191 192 if (cmd == FBIO_WAITFORVSYNC) { 193 /* Unmask End of Frame interrupt */ 194 writel(0xffffffff ^ (1 << 3), fbi->regbase + 0x3c); 195 ret = wait_event_interruptible_timeout(fbi->wait, 196 readl(fbi->regbase + 0x38) & (1 << 3), HZ / 10); 197 /* Mask back to reduce unwanted interrupt traffic */ 198 writel(0xffffffff, fbi->regbase + 0x3c); 199 if (ret < 0) 200 return ret; 201 if (ret == 0) 202 return -ETIMEDOUT; 203 } 204 205 return ret; 206} 207 208static int vt8500lcd_pan_display(struct fb_var_screeninfo *var, 209 struct fb_info *info) 210{ 211 unsigned pixlen = info->fix.line_length / info->var.xres_virtual; 212 unsigned off = pixlen * var->xoffset 213 + info->fix.line_length * var->yoffset; 214 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info); 215 216 writel((1 << 31) 217 | (((info->var.xres_virtual - info->var.xres) * pixlen / 4) << 20) 218 | (off >> 2), fbi->regbase + 0x20); 219 return 0; 220} 221 222/* 223 * vt8500lcd_blank(): 224 * Blank the display by setting all palette values to zero. Note, 225 * True Color modes do not really use the palette, so this will not 226 * blank the display in all modes. 227 */ 228static int vt8500lcd_blank(int blank, struct fb_info *info) 229{ 230 int i; 231 232 switch (blank) { 233 case FB_BLANK_POWERDOWN: 234 case FB_BLANK_VSYNC_SUSPEND: 235 case FB_BLANK_HSYNC_SUSPEND: 236 case FB_BLANK_NORMAL: 237 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR || 238 info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR) 239 for (i = 0; i < 256; i++) 240 vt8500lcd_setcolreg(i, 0, 0, 0, 0, info); 241 case FB_BLANK_UNBLANK: 242 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR || 243 info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR) 244 fb_set_cmap(&info->cmap, info); 245 } 246 return 0; 247} 248 249static struct fb_ops vt8500lcd_ops = { 250 .owner = THIS_MODULE, 251 .fb_set_par = vt8500lcd_set_par, 252 .fb_setcolreg = vt8500lcd_setcolreg, 253 .fb_fillrect = wmt_ge_fillrect, 254 .fb_copyarea = wmt_ge_copyarea, 255 .fb_imageblit = sys_imageblit, 256 .fb_sync = wmt_ge_sync, 257 .fb_ioctl = vt8500lcd_ioctl, 258 .fb_pan_display = vt8500lcd_pan_display, 259 .fb_blank = vt8500lcd_blank, 260}; 261 262static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id) 263{ 264 struct vt8500lcd_info *fbi = dev_id; 265 266 if (readl(fbi->regbase + 0x38) & (1 << 3)) 267 wake_up_interruptible(&fbi->wait); 268 269 writel(0xffffffff, fbi->regbase + 0x38); 270 return IRQ_HANDLED; 271} 272 273static int vt8500lcd_probe(struct platform_device *pdev) 274{ 275 struct vt8500lcd_info *fbi; 276 struct resource *res; 277 struct display_timings *disp_timing; 278 void *addr; 279 int irq, ret; 280 281 struct fb_videomode of_mode; 282 u32 bpp; 283 dma_addr_t fb_mem_phys; 284 unsigned long fb_mem_len; 285 void *fb_mem_virt; 286 287 ret = -ENOMEM; 288 fbi = NULL; 289 290 fbi = devm_kzalloc(&pdev->dev, sizeof(struct vt8500lcd_info) 291 + sizeof(u32) * 16, GFP_KERNEL); 292 if (!fbi) { 293 dev_err(&pdev->dev, "Failed to initialize framebuffer device\n"); 294 return -ENOMEM; 295 } 296 297 strcpy(fbi->fb.fix.id, "VT8500 LCD"); 298 299 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS; 300 fbi->fb.fix.xpanstep = 0; 301 fbi->fb.fix.ypanstep = 1; 302 fbi->fb.fix.ywrapstep = 0; 303 fbi->fb.fix.accel = FB_ACCEL_NONE; 304 305 fbi->fb.var.nonstd = 0; 306 fbi->fb.var.activate = FB_ACTIVATE_NOW; 307 fbi->fb.var.height = -1; 308 fbi->fb.var.width = -1; 309 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED; 310 311 fbi->fb.fbops = &vt8500lcd_ops; 312 fbi->fb.flags = FBINFO_DEFAULT 313 | FBINFO_HWACCEL_COPYAREA 314 | FBINFO_HWACCEL_FILLRECT 315 | FBINFO_HWACCEL_YPAN 316 | FBINFO_VIRTFB 317 | FBINFO_PARTIAL_PAN_OK; 318 fbi->fb.node = -1; 319 320 addr = fbi; 321 addr = addr + sizeof(struct vt8500lcd_info); 322 fbi->fb.pseudo_palette = addr; 323 324 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 325 if (res == NULL) { 326 dev_err(&pdev->dev, "no I/O memory resource defined\n"); 327 return -ENODEV; 328 } 329 330 res = request_mem_region(res->start, resource_size(res), "vt8500lcd"); 331 if (res == NULL) { 332 dev_err(&pdev->dev, "failed to request I/O memory\n"); 333 return -EBUSY; 334 } 335 336 fbi->regbase = ioremap(res->start, resource_size(res)); 337 if (fbi->regbase == NULL) { 338 dev_err(&pdev->dev, "failed to map I/O memory\n"); 339 ret = -EBUSY; 340 goto failed_free_res; 341 } 342 343 disp_timing = of_get_display_timings(pdev->dev.of_node); 344 if (!disp_timing) { 345 ret = -EINVAL; 346 goto failed_free_io; 347 } 348 349 ret = of_get_fb_videomode(pdev->dev.of_node, &of_mode, 350 OF_USE_NATIVE_MODE); 351 if (ret) 352 goto failed_free_io; 353 354 ret = of_property_read_u32(pdev->dev.of_node, "bits-per-pixel", &bpp); 355 if (ret) 356 goto failed_free_io; 357 358 /* try allocating the framebuffer */ 359 fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8); 360 fb_mem_virt = dma_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys, 361 GFP_KERNEL); 362 if (!fb_mem_virt) { 363 pr_err("%s: Failed to allocate framebuffer\n", __func__); 364 ret = -ENOMEM; 365 goto failed_free_io; 366 } 367 368 fbi->fb.fix.smem_start = fb_mem_phys; 369 fbi->fb.fix.smem_len = fb_mem_len; 370 fbi->fb.screen_base = fb_mem_virt; 371 372 fbi->palette_size = PAGE_ALIGN(512); 373 fbi->palette_cpu = dma_alloc_coherent(&pdev->dev, 374 fbi->palette_size, 375 &fbi->palette_phys, 376 GFP_KERNEL); 377 if (fbi->palette_cpu == NULL) { 378 dev_err(&pdev->dev, "Failed to allocate palette buffer\n"); 379 ret = -ENOMEM; 380 goto failed_free_io; 381 } 382 383 irq = platform_get_irq(pdev, 0); 384 if (irq < 0) { 385 dev_err(&pdev->dev, "no IRQ defined\n"); 386 ret = -ENODEV; 387 goto failed_free_palette; 388 } 389 390 ret = request_irq(irq, vt8500lcd_handle_irq, 0, "LCD", fbi); 391 if (ret) { 392 dev_err(&pdev->dev, "request_irq failed: %d\n", ret); 393 ret = -EBUSY; 394 goto failed_free_palette; 395 } 396 397 init_waitqueue_head(&fbi->wait); 398 399 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) { 400 dev_err(&pdev->dev, "Failed to allocate color map\n"); 401 ret = -ENOMEM; 402 goto failed_free_irq; 403 } 404 405 fb_videomode_to_var(&fbi->fb.var, &of_mode); 406 407 fbi->fb.var.xres_virtual = of_mode.xres; 408 fbi->fb.var.yres_virtual = of_mode.yres * 2; 409 fbi->fb.var.bits_per_pixel = bpp; 410 411 ret = vt8500lcd_set_par(&fbi->fb); 412 if (ret) { 413 dev_err(&pdev->dev, "Failed to set parameters\n"); 414 goto failed_free_cmap; 415 } 416 417 writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c); 418 writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18); 419 420 platform_set_drvdata(pdev, fbi); 421 422 ret = register_framebuffer(&fbi->fb); 423 if (ret < 0) { 424 dev_err(&pdev->dev, 425 "Failed to register framebuffer device: %d\n", ret); 426 goto failed_free_cmap; 427 } 428 429 /* 430 * Ok, now enable the LCD controller 431 */ 432 writel(readl(fbi->regbase) | 1, fbi->regbase); 433 434 return 0; 435 436failed_free_cmap: 437 if (fbi->fb.cmap.len) 438 fb_dealloc_cmap(&fbi->fb.cmap); 439failed_free_irq: 440 free_irq(irq, fbi); 441failed_free_palette: 442 dma_free_coherent(&pdev->dev, fbi->palette_size, 443 fbi->palette_cpu, fbi->palette_phys); 444failed_free_io: 445 iounmap(fbi->regbase); 446failed_free_res: 447 release_mem_region(res->start, resource_size(res)); 448 return ret; 449} 450 451static int vt8500lcd_remove(struct platform_device *pdev) 452{ 453 struct vt8500lcd_info *fbi = platform_get_drvdata(pdev); 454 struct resource *res; 455 int irq; 456 457 unregister_framebuffer(&fbi->fb); 458 459 writel(0, fbi->regbase); 460 461 if (fbi->fb.cmap.len) 462 fb_dealloc_cmap(&fbi->fb.cmap); 463 464 irq = platform_get_irq(pdev, 0); 465 free_irq(irq, fbi); 466 467 dma_free_coherent(&pdev->dev, fbi->palette_size, 468 fbi->palette_cpu, fbi->palette_phys); 469 470 iounmap(fbi->regbase); 471 472 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 473 release_mem_region(res->start, resource_size(res)); 474 475 return 0; 476} 477 478static const struct of_device_id via_dt_ids[] = { 479 { .compatible = "via,vt8500-fb", }, 480 {} 481}; 482 483static struct platform_driver vt8500lcd_driver = { 484 .probe = vt8500lcd_probe, 485 .remove = vt8500lcd_remove, 486 .driver = { 487 .name = "vt8500-lcd", 488 .of_match_table = of_match_ptr(via_dt_ids), 489 }, 490}; 491 492module_platform_driver(vt8500lcd_driver); 493 494MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>"); 495MODULE_DESCRIPTION("LCD controller driver for VIA VT8500"); 496MODULE_LICENSE("GPL v2"); 497MODULE_DEVICE_TABLE(of, via_dt_ids); 498