video: vt8500: Adjust contrast in wm8505 framebuffer driver.
[deliverable/linux.git] / drivers / video / vt8500lcdfb.c
CommitLineData
d6ff7d0f
AC
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/module.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/string.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
24#include <linux/delay.h>
25#include <linux/fb.h>
26#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/dma-mapping.h>
30#include <linux/platform_device.h>
31#include <linux/wait.h>
32
d6ff7d0f
AC
33#include "vt8500lcdfb.h"
34#include "wmt_ge_rops.h"
35
e7b99537
TP
36#ifdef CONFIG_OF
37#include <linux/of.h>
38#include <linux/of_fdt.h>
39#include <linux/memblock.h>
40#endif
41
42
d6ff7d0f
AC
43#define to_vt8500lcd_info(__info) container_of(__info, \
44 struct vt8500lcd_info, fb)
45
46static int vt8500lcd_set_par(struct fb_info *info)
47{
48 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
49 int reg_bpp = 5; /* 16bpp */
50 int i;
51 unsigned long control0;
52
53 if (!fbi)
54 return -EINVAL;
55
56 if (info->var.bits_per_pixel <= 8) {
57 /* palettized */
58 info->var.red.offset = 0;
59 info->var.red.length = info->var.bits_per_pixel;
60 info->var.red.msb_right = 0;
61
62 info->var.green.offset = 0;
63 info->var.green.length = info->var.bits_per_pixel;
64 info->var.green.msb_right = 0;
65
66 info->var.blue.offset = 0;
67 info->var.blue.length = info->var.bits_per_pixel;
68 info->var.blue.msb_right = 0;
69
70 info->var.transp.offset = 0;
71 info->var.transp.length = 0;
72 info->var.transp.msb_right = 0;
73
74 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
75 info->fix.line_length = info->var.xres_virtual /
76 (8/info->var.bits_per_pixel);
77 } else {
78 /* non-palettized */
79 info->var.transp.offset = 0;
80 info->var.transp.length = 0;
81 info->var.transp.msb_right = 0;
82
83 if (info->var.bits_per_pixel == 16) {
84 /* RGB565 */
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;
94 } else {
95 /* Equal depths per channel */
96 info->var.red.offset = info->var.bits_per_pixel
97 * 2 / 3;
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;
106 }
107
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;
112 }
113
114 for (i = 0; i < 8; i++) {
115 if (bpp_values[i] == info->var.bits_per_pixel) {
116 reg_bpp = i;
117 continue;
118 }
119 }
120
121 control0 = readl(fbi->regbase) & ~0xf;
122 writel(0, fbi->regbase);
123 while (readl(fbi->regbase + 0x38) & 0x10)
124 /* wait */;
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);
137
138 return 0;
139}
140
141static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
142{
143 chan &= 0xffff;
144 chan >>= 16 - bf->length;
145 return chan << bf->offset;
146}
147
148static 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);
152 int ret = 1;
153 unsigned int val;
154 if (regno >= 256)
155 return -EINVAL;
156
157 if (info->var.grayscale)
158 red = green = blue =
159 (19595 * red + 38470 * green + 7471 * blue) >> 16;
160
161 switch (fbi->fb.fix.visual) {
162 case FB_VISUAL_TRUECOLOR:
163 if (regno < 16) {
164 u32 *pal = fbi->fb.pseudo_palette;
165
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);
169
170 pal[regno] = val;
171 ret = 0;
172 }
173 break;
174
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);
181 break;
182 }
183
184 return ret;
185}
186
187static int vt8500lcd_ioctl(struct fb_info *info, unsigned int cmd,
188 unsigned long arg)
189{
190 int ret = 0;
191 struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
192
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);
200 if (ret < 0)
201 return ret;
202 if (ret == 0)
203 return -ETIMEDOUT;
204 }
205
206 return ret;
207}
208
209static int vt8500lcd_pan_display(struct fb_var_screeninfo *var,
210 struct fb_info *info)
211{
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);
216
217 writel((1 << 31)
8f0246d6
LP
218 | (((info->var.xres_virtual - info->var.xres) * pixlen / 4) << 20)
219 | (off >> 2), fbi->regbase + 0x20);
d6ff7d0f
AC
220 return 0;
221}
222
e41f1a98
AC
223/*
224 * vt8500lcd_blank():
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.
228 */
229static int vt8500lcd_blank(int blank, struct fb_info *info)
230{
231 int i;
232
233 switch (blank) {
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);
246 }
247 return 0;
248}
249
d6ff7d0f
AC
250static 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,
e41f1a98 260 .fb_blank = vt8500lcd_blank,
d6ff7d0f
AC
261};
262
263static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id)
264{
265 struct vt8500lcd_info *fbi = dev_id;
266
267 if (readl(fbi->regbase + 0x38) & (1 << 3))
268 wake_up_interruptible(&fbi->wait);
269
270 writel(0xffffffff, fbi->regbase + 0x38);
271 return IRQ_HANDLED;
272}
273
48c68c4f 274static int vt8500lcd_probe(struct platform_device *pdev)
d6ff7d0f
AC
275{
276 struct vt8500lcd_info *fbi;
277 struct resource *res;
d6ff7d0f
AC
278 void *addr;
279 int irq, ret;
280
e7b99537
TP
281 struct fb_videomode of_mode;
282 struct device_node *np;
283 u32 bpp;
284 dma_addr_t fb_mem_phys;
285 unsigned long fb_mem_len;
286 void *fb_mem_virt;
287
d6ff7d0f
AC
288 ret = -ENOMEM;
289 fbi = NULL;
290
e7b99537
TP
291 fbi = devm_kzalloc(&pdev->dev, sizeof(struct vt8500lcd_info)
292 + sizeof(u32) * 16, GFP_KERNEL);
d6ff7d0f
AC
293 if (!fbi) {
294 dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
295 ret = -ENOMEM;
296 goto failed;
297 }
298
299 strcpy(fbi->fb.fix.id, "VT8500 LCD");
300
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;
306
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;
312
313 fbi->fb.fbops = &vt8500lcd_ops;
314 fbi->fb.flags = FBINFO_DEFAULT
315 | FBINFO_HWACCEL_COPYAREA
316 | FBINFO_HWACCEL_FILLRECT
317 | FBINFO_HWACCEL_YPAN
318 | FBINFO_VIRTFB
319 | FBINFO_PARTIAL_PAN_OK;
320 fbi->fb.node = -1;
321
322 addr = fbi;
323 addr = addr + sizeof(struct vt8500lcd_info);
324 fbi->fb.pseudo_palette = addr;
325
326 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
327 if (res == NULL) {
328 dev_err(&pdev->dev, "no I/O memory resource defined\n");
329 ret = -ENODEV;
330 goto failed_fbi;
331 }
332
333 res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
334 if (res == NULL) {
335 dev_err(&pdev->dev, "failed to request I/O memory\n");
336 ret = -EBUSY;
337 goto failed_fbi;
338 }
339
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");
343 ret = -EBUSY;
344 goto failed_free_res;
345 }
346
e7b99537
TP
347 np = of_parse_phandle(pdev->dev.of_node, "default-mode", 0);
348 if (!np) {
349 pr_err("%s: No display description in Device Tree\n", __func__);
350 ret = -EINVAL;
351 goto failed_free_res;
352 }
353
354 /*
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
357 */
358 ret = 0;
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);
368 if (ret) {
369 pr_err("%s: Unable to read display properties\n", __func__);
370 goto failed_free_res;
371 }
372 of_mode.vmode = FB_VMODE_NONINTERLACED;
373
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,
377 GFP_KERNEL);
378 if (!fb_mem_virt) {
379 pr_err("%s: Failed to allocate framebuffer\n", __func__);
380 return -ENOMEM;
381 };
382
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;
d6ff7d0f
AC
386
387 fbi->palette_size = PAGE_ALIGN(512);
388 fbi->palette_cpu = dma_alloc_coherent(&pdev->dev,
389 fbi->palette_size,
390 &fbi->palette_phys,
391 GFP_KERNEL);
392 if (fbi->palette_cpu == NULL) {
393 dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
394 ret = -ENOMEM;
395 goto failed_free_io;
396 }
397
398 irq = platform_get_irq(pdev, 0);
399 if (irq < 0) {
400 dev_err(&pdev->dev, "no IRQ defined\n");
401 ret = -ENODEV;
402 goto failed_free_palette;
403 }
404
f8798ccb 405 ret = request_irq(irq, vt8500lcd_handle_irq, 0, "LCD", fbi);
d6ff7d0f
AC
406 if (ret) {
407 dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
408 ret = -EBUSY;
409 goto failed_free_palette;
410 }
411
412 init_waitqueue_head(&fbi->wait);
413
414 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
415 dev_err(&pdev->dev, "Failed to allocate color map\n");
416 ret = -ENOMEM;
417 goto failed_free_irq;
418 }
419
e7b99537
TP
420 fb_videomode_to_var(&fbi->fb.var, &of_mode);
421
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;
d6ff7d0f
AC
425
426 ret = vt8500lcd_set_par(&fbi->fb);
427 if (ret) {
428 dev_err(&pdev->dev, "Failed to set parameters\n");
429 goto failed_free_cmap;
430 }
431
432 writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
433 writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
434
435 platform_set_drvdata(pdev, fbi);
436
437 ret = register_framebuffer(&fbi->fb);
438 if (ret < 0) {
439 dev_err(&pdev->dev,
440 "Failed to register framebuffer device: %d\n", ret);
441 goto failed_free_cmap;
442 }
443
444 /*
445 * Ok, now enable the LCD controller
446 */
447 writel(readl(fbi->regbase) | 1, fbi->regbase);
448
449 return 0;
450
451failed_free_cmap:
452 if (fbi->fb.cmap.len)
453 fb_dealloc_cmap(&fbi->fb.cmap);
454failed_free_irq:
455 free_irq(irq, fbi);
456failed_free_palette:
457 dma_free_coherent(&pdev->dev, fbi->palette_size,
458 fbi->palette_cpu, fbi->palette_phys);
459failed_free_io:
460 iounmap(fbi->regbase);
461failed_free_res:
462 release_mem_region(res->start, resource_size(res));
463failed_fbi:
464 platform_set_drvdata(pdev, NULL);
465 kfree(fbi);
466failed:
467 return ret;
468}
469
48c68c4f 470static int vt8500lcd_remove(struct platform_device *pdev)
d6ff7d0f
AC
471{
472 struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
473 struct resource *res;
474 int irq;
475
476 unregister_framebuffer(&fbi->fb);
477
478 writel(0, fbi->regbase);
479
480 if (fbi->fb.cmap.len)
481 fb_dealloc_cmap(&fbi->fb.cmap);
482
483 irq = platform_get_irq(pdev, 0);
484 free_irq(irq, fbi);
485
486 dma_free_coherent(&pdev->dev, fbi->palette_size,
487 fbi->palette_cpu, fbi->palette_phys);
488
489 iounmap(fbi->regbase);
490
491 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
492 release_mem_region(res->start, resource_size(res));
493
494 kfree(fbi);
495
496 return 0;
497}
498
e7b99537
TP
499static const struct of_device_id via_dt_ids[] = {
500 { .compatible = "via,vt8500-fb", },
501 {}
502};
503
d6ff7d0f
AC
504static struct platform_driver vt8500lcd_driver = {
505 .probe = vt8500lcd_probe,
48c68c4f 506 .remove = vt8500lcd_remove,
d6ff7d0f
AC
507 .driver = {
508 .owner = THIS_MODULE,
509 .name = "vt8500-lcd",
e7b99537 510 .of_match_table = of_match_ptr(via_dt_ids),
d6ff7d0f
AC
511 },
512};
513
4277f2c4 514module_platform_driver(vt8500lcd_driver);
d6ff7d0f
AC
515
516MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
517MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
e7b99537
TP
518MODULE_LICENSE("GPL v2");
519MODULE_DEVICE_TABLE(of, via_dt_ids);
This page took 0.352645 seconds and 5 git commands to generate.