0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/delay.h>
0011 #include <linux/dma-mapping.h>
0012 #include <linux/errno.h>
0013 #include <linux/fb.h>
0014 #include <linux/init.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/io.h>
0017 #include <linux/kernel.h>
0018 #include <linux/mm.h>
0019 #include <linux/module.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/slab.h>
0022 #include <linux/string.h>
0023 #include <linux/wait.h>
0024 #include <video/of_display_timing.h>
0025
0026 #include "vt8500lcdfb.h"
0027 #include "wmt_ge_rops.h"
0028
0029 #ifdef CONFIG_OF
0030 #include <linux/of.h>
0031 #include <linux/of_fdt.h>
0032 #include <linux/memblock.h>
0033 #endif
0034
0035
0036 #define to_vt8500lcd_info(__info) container_of(__info, \
0037 struct vt8500lcd_info, fb)
0038
0039 static int vt8500lcd_set_par(struct fb_info *info)
0040 {
0041 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
0042 int reg_bpp = 5;
0043 int i;
0044 unsigned long control0;
0045
0046 if (!fbi)
0047 return -EINVAL;
0048
0049 if (info->var.bits_per_pixel <= 8) {
0050
0051 info->var.red.offset = 0;
0052 info->var.red.length = info->var.bits_per_pixel;
0053 info->var.red.msb_right = 0;
0054
0055 info->var.green.offset = 0;
0056 info->var.green.length = info->var.bits_per_pixel;
0057 info->var.green.msb_right = 0;
0058
0059 info->var.blue.offset = 0;
0060 info->var.blue.length = info->var.bits_per_pixel;
0061 info->var.blue.msb_right = 0;
0062
0063 info->var.transp.offset = 0;
0064 info->var.transp.length = 0;
0065 info->var.transp.msb_right = 0;
0066
0067 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
0068 info->fix.line_length = info->var.xres_virtual /
0069 (8/info->var.bits_per_pixel);
0070 } else {
0071
0072 info->var.transp.offset = 0;
0073 info->var.transp.length = 0;
0074 info->var.transp.msb_right = 0;
0075
0076 if (info->var.bits_per_pixel == 16) {
0077
0078 info->var.red.offset = 11;
0079 info->var.red.length = 5;
0080 info->var.red.msb_right = 0;
0081 info->var.green.offset = 5;
0082 info->var.green.length = 6;
0083 info->var.green.msb_right = 0;
0084 info->var.blue.offset = 0;
0085 info->var.blue.length = 5;
0086 info->var.blue.msb_right = 0;
0087 } else {
0088
0089 info->var.red.offset = info->var.bits_per_pixel
0090 * 2 / 3;
0091 info->var.red.length = info->var.bits_per_pixel / 3;
0092 info->var.red.msb_right = 0;
0093 info->var.green.offset = info->var.bits_per_pixel / 3;
0094 info->var.green.length = info->var.bits_per_pixel / 3;
0095 info->var.green.msb_right = 0;
0096 info->var.blue.offset = 0;
0097 info->var.blue.length = info->var.bits_per_pixel / 3;
0098 info->var.blue.msb_right = 0;
0099 }
0100
0101 info->fix.visual = FB_VISUAL_TRUECOLOR;
0102 info->fix.line_length = info->var.bits_per_pixel > 16 ?
0103 info->var.xres_virtual << 2 :
0104 info->var.xres_virtual << 1;
0105 }
0106
0107 for (i = 0; i < 8; i++) {
0108 if (bpp_values[i] == info->var.bits_per_pixel)
0109 reg_bpp = i;
0110 }
0111
0112 control0 = readl(fbi->regbase) & ~0xf;
0113 writel(0, fbi->regbase);
0114 while (readl(fbi->regbase + 0x38) & 0x10)
0115 ;
0116 writel((((info->var.hsync_len - 1) & 0x3f) << 26)
0117 | ((info->var.left_margin & 0xff) << 18)
0118 | (((info->var.xres - 1) & 0x3ff) << 8)
0119 | (info->var.right_margin & 0xff), fbi->regbase + 0x4);
0120 writel((((info->var.vsync_len - 1) & 0x3f) << 26)
0121 | ((info->var.upper_margin & 0xff) << 18)
0122 | (((info->var.yres - 1) & 0x3ff) << 8)
0123 | (info->var.lower_margin & 0xff), fbi->regbase + 0x8);
0124 writel((((info->var.yres - 1) & 0x400) << 2)
0125 | ((info->var.xres - 1) & 0x400), fbi->regbase + 0x10);
0126 writel(0x80000000, fbi->regbase + 0x20);
0127 writel(control0 | (reg_bpp << 1) | 0x100, fbi->regbase);
0128
0129 return 0;
0130 }
0131
0132 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
0133 {
0134 chan &= 0xffff;
0135 chan >>= 16 - bf->length;
0136 return chan << bf->offset;
0137 }
0138
0139 static int vt8500lcd_setcolreg(unsigned regno, unsigned red, unsigned green,
0140 unsigned blue, unsigned transp,
0141 struct fb_info *info) {
0142 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
0143 int ret = 1;
0144 unsigned int val;
0145 if (regno >= 256)
0146 return -EINVAL;
0147
0148 if (info->var.grayscale)
0149 red = green = blue =
0150 (19595 * red + 38470 * green + 7471 * blue) >> 16;
0151
0152 switch (fbi->fb.fix.visual) {
0153 case FB_VISUAL_TRUECOLOR:
0154 if (regno < 16) {
0155 u32 *pal = fbi->fb.pseudo_palette;
0156
0157 val = chan_to_field(red, &fbi->fb.var.red);
0158 val |= chan_to_field(green, &fbi->fb.var.green);
0159 val |= chan_to_field(blue, &fbi->fb.var.blue);
0160
0161 pal[regno] = val;
0162 ret = 0;
0163 }
0164 break;
0165
0166 case FB_VISUAL_STATIC_PSEUDOCOLOR:
0167 case FB_VISUAL_PSEUDOCOLOR:
0168 writew((red & 0xf800)
0169 | ((green >> 5) & 0x7e0)
0170 | ((blue >> 11) & 0x1f),
0171 fbi->palette_cpu + sizeof(u16) * regno);
0172 break;
0173 }
0174
0175 return ret;
0176 }
0177
0178 static int vt8500lcd_ioctl(struct fb_info *info, unsigned int cmd,
0179 unsigned long arg)
0180 {
0181 int ret = 0;
0182 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
0183
0184 if (cmd == FBIO_WAITFORVSYNC) {
0185
0186 writel(0xffffffff ^ (1 << 3), fbi->regbase + 0x3c);
0187 ret = wait_event_interruptible_timeout(fbi->wait,
0188 readl(fbi->regbase + 0x38) & (1 << 3), HZ / 10);
0189
0190 writel(0xffffffff, fbi->regbase + 0x3c);
0191 if (ret < 0)
0192 return ret;
0193 if (ret == 0)
0194 return -ETIMEDOUT;
0195 }
0196
0197 return ret;
0198 }
0199
0200 static int vt8500lcd_pan_display(struct fb_var_screeninfo *var,
0201 struct fb_info *info)
0202 {
0203 unsigned pixlen = info->fix.line_length / info->var.xres_virtual;
0204 unsigned off = pixlen * var->xoffset
0205 + info->fix.line_length * var->yoffset;
0206 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
0207
0208 writel((1 << 31)
0209 | (((info->var.xres_virtual - info->var.xres) * pixlen / 4) << 20)
0210 | (off >> 2), fbi->regbase + 0x20);
0211 return 0;
0212 }
0213
0214
0215
0216
0217
0218
0219
0220 static int vt8500lcd_blank(int blank, struct fb_info *info)
0221 {
0222 int i;
0223
0224 switch (blank) {
0225 case FB_BLANK_POWERDOWN:
0226 case FB_BLANK_VSYNC_SUSPEND:
0227 case FB_BLANK_HSYNC_SUSPEND:
0228 case FB_BLANK_NORMAL:
0229 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR ||
0230 info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
0231 for (i = 0; i < 256; i++)
0232 vt8500lcd_setcolreg(i, 0, 0, 0, 0, info);
0233 fallthrough;
0234 case FB_BLANK_UNBLANK:
0235 if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR ||
0236 info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
0237 fb_set_cmap(&info->cmap, info);
0238 }
0239 return 0;
0240 }
0241
0242 static const struct fb_ops vt8500lcd_ops = {
0243 .owner = THIS_MODULE,
0244 .fb_set_par = vt8500lcd_set_par,
0245 .fb_setcolreg = vt8500lcd_setcolreg,
0246 .fb_fillrect = wmt_ge_fillrect,
0247 .fb_copyarea = wmt_ge_copyarea,
0248 .fb_imageblit = sys_imageblit,
0249 .fb_sync = wmt_ge_sync,
0250 .fb_ioctl = vt8500lcd_ioctl,
0251 .fb_pan_display = vt8500lcd_pan_display,
0252 .fb_blank = vt8500lcd_blank,
0253 };
0254
0255 static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id)
0256 {
0257 struct vt8500lcd_info *fbi = dev_id;
0258
0259 if (readl(fbi->regbase + 0x38) & (1 << 3))
0260 wake_up_interruptible(&fbi->wait);
0261
0262 writel(0xffffffff, fbi->regbase + 0x38);
0263 return IRQ_HANDLED;
0264 }
0265
0266 static int vt8500lcd_probe(struct platform_device *pdev)
0267 {
0268 struct vt8500lcd_info *fbi;
0269 struct resource *res;
0270 struct display_timings *disp_timing;
0271 void *addr;
0272 int irq, ret;
0273
0274 struct fb_videomode of_mode;
0275 u32 bpp;
0276 dma_addr_t fb_mem_phys;
0277 unsigned long fb_mem_len;
0278 void *fb_mem_virt;
0279
0280 ret = -ENOMEM;
0281 fbi = NULL;
0282
0283 fbi = devm_kzalloc(&pdev->dev, sizeof(struct vt8500lcd_info)
0284 + sizeof(u32) * 16, GFP_KERNEL);
0285 if (!fbi)
0286 return -ENOMEM;
0287
0288 strcpy(fbi->fb.fix.id, "VT8500 LCD");
0289
0290 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
0291 fbi->fb.fix.xpanstep = 0;
0292 fbi->fb.fix.ypanstep = 1;
0293 fbi->fb.fix.ywrapstep = 0;
0294 fbi->fb.fix.accel = FB_ACCEL_NONE;
0295
0296 fbi->fb.var.nonstd = 0;
0297 fbi->fb.var.activate = FB_ACTIVATE_NOW;
0298 fbi->fb.var.height = -1;
0299 fbi->fb.var.width = -1;
0300 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
0301
0302 fbi->fb.fbops = &vt8500lcd_ops;
0303 fbi->fb.flags = FBINFO_DEFAULT
0304 | FBINFO_HWACCEL_COPYAREA
0305 | FBINFO_HWACCEL_FILLRECT
0306 | FBINFO_HWACCEL_YPAN
0307 | FBINFO_VIRTFB
0308 | FBINFO_PARTIAL_PAN_OK;
0309 fbi->fb.node = -1;
0310
0311 addr = fbi;
0312 addr = addr + sizeof(struct vt8500lcd_info);
0313 fbi->fb.pseudo_palette = addr;
0314
0315 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0316 if (res == NULL) {
0317 dev_err(&pdev->dev, "no I/O memory resource defined\n");
0318 return -ENODEV;
0319 }
0320
0321 res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
0322 if (res == NULL) {
0323 dev_err(&pdev->dev, "failed to request I/O memory\n");
0324 return -EBUSY;
0325 }
0326
0327 fbi->regbase = ioremap(res->start, resource_size(res));
0328 if (fbi->regbase == NULL) {
0329 dev_err(&pdev->dev, "failed to map I/O memory\n");
0330 ret = -EBUSY;
0331 goto failed_free_res;
0332 }
0333
0334 disp_timing = of_get_display_timings(pdev->dev.of_node);
0335 if (!disp_timing) {
0336 ret = -EINVAL;
0337 goto failed_free_io;
0338 }
0339
0340 ret = of_get_fb_videomode(pdev->dev.of_node, &of_mode,
0341 OF_USE_NATIVE_MODE);
0342 if (ret)
0343 goto failed_free_io;
0344
0345 ret = of_property_read_u32(pdev->dev.of_node, "bits-per-pixel", &bpp);
0346 if (ret)
0347 goto failed_free_io;
0348
0349
0350 fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8);
0351 fb_mem_virt = dma_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
0352 GFP_KERNEL);
0353 if (!fb_mem_virt) {
0354 pr_err("%s: Failed to allocate framebuffer\n", __func__);
0355 ret = -ENOMEM;
0356 goto failed_free_io;
0357 }
0358
0359 fbi->fb.fix.smem_start = fb_mem_phys;
0360 fbi->fb.fix.smem_len = fb_mem_len;
0361 fbi->fb.screen_base = fb_mem_virt;
0362
0363 fbi->palette_size = PAGE_ALIGN(512);
0364 fbi->palette_cpu = dma_alloc_coherent(&pdev->dev,
0365 fbi->palette_size,
0366 &fbi->palette_phys,
0367 GFP_KERNEL);
0368 if (fbi->palette_cpu == NULL) {
0369 dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
0370 ret = -ENOMEM;
0371 goto failed_free_io;
0372 }
0373
0374 irq = platform_get_irq(pdev, 0);
0375 if (irq < 0) {
0376 dev_err(&pdev->dev, "no IRQ defined\n");
0377 ret = -ENODEV;
0378 goto failed_free_palette;
0379 }
0380
0381 ret = request_irq(irq, vt8500lcd_handle_irq, 0, "LCD", fbi);
0382 if (ret) {
0383 dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
0384 ret = -EBUSY;
0385 goto failed_free_palette;
0386 }
0387
0388 init_waitqueue_head(&fbi->wait);
0389
0390 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
0391 dev_err(&pdev->dev, "Failed to allocate color map\n");
0392 ret = -ENOMEM;
0393 goto failed_free_irq;
0394 }
0395
0396 fb_videomode_to_var(&fbi->fb.var, &of_mode);
0397
0398 fbi->fb.var.xres_virtual = of_mode.xres;
0399 fbi->fb.var.yres_virtual = of_mode.yres * 2;
0400 fbi->fb.var.bits_per_pixel = bpp;
0401
0402 ret = vt8500lcd_set_par(&fbi->fb);
0403 if (ret) {
0404 dev_err(&pdev->dev, "Failed to set parameters\n");
0405 goto failed_free_cmap;
0406 }
0407
0408 writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
0409 writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
0410
0411 platform_set_drvdata(pdev, fbi);
0412
0413 ret = register_framebuffer(&fbi->fb);
0414 if (ret < 0) {
0415 dev_err(&pdev->dev,
0416 "Failed to register framebuffer device: %d\n", ret);
0417 goto failed_free_cmap;
0418 }
0419
0420
0421
0422
0423 writel(readl(fbi->regbase) | 1, fbi->regbase);
0424
0425 return 0;
0426
0427 failed_free_cmap:
0428 if (fbi->fb.cmap.len)
0429 fb_dealloc_cmap(&fbi->fb.cmap);
0430 failed_free_irq:
0431 free_irq(irq, fbi);
0432 failed_free_palette:
0433 dma_free_coherent(&pdev->dev, fbi->palette_size,
0434 fbi->palette_cpu, fbi->palette_phys);
0435 failed_free_io:
0436 iounmap(fbi->regbase);
0437 failed_free_res:
0438 release_mem_region(res->start, resource_size(res));
0439 return ret;
0440 }
0441
0442 static int vt8500lcd_remove(struct platform_device *pdev)
0443 {
0444 struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
0445 struct resource *res;
0446 int irq;
0447
0448 unregister_framebuffer(&fbi->fb);
0449
0450 writel(0, fbi->regbase);
0451
0452 if (fbi->fb.cmap.len)
0453 fb_dealloc_cmap(&fbi->fb.cmap);
0454
0455 irq = platform_get_irq(pdev, 0);
0456 free_irq(irq, fbi);
0457
0458 dma_free_coherent(&pdev->dev, fbi->palette_size,
0459 fbi->palette_cpu, fbi->palette_phys);
0460
0461 iounmap(fbi->regbase);
0462
0463 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0464 release_mem_region(res->start, resource_size(res));
0465
0466 return 0;
0467 }
0468
0469 static const struct of_device_id via_dt_ids[] = {
0470 { .compatible = "via,vt8500-fb", },
0471 {}
0472 };
0473
0474 static struct platform_driver vt8500lcd_driver = {
0475 .probe = vt8500lcd_probe,
0476 .remove = vt8500lcd_remove,
0477 .driver = {
0478 .name = "vt8500-lcd",
0479 .of_match_table = of_match_ptr(via_dt_ids),
0480 },
0481 };
0482
0483 module_platform_driver(vt8500lcd_driver);
0484
0485 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
0486 MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
0487 MODULE_LICENSE("GPL v2");
0488 MODULE_DEVICE_TABLE(of, via_dt_ids);