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