1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
51 int i;
52 unsigned long control0;
53
54 if (!fbi)
55 return -EINVAL;
56
57 if (info->var.bits_per_pixel <= 8) {
58
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
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
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
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 ;
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
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
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
224
225
226
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 return -ENOMEM;
294
295 strcpy(fbi->fb.fix.id, "VT8500 LCD");
296
297 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
298 fbi->fb.fix.xpanstep = 0;
299 fbi->fb.fix.ypanstep = 1;
300 fbi->fb.fix.ywrapstep = 0;
301 fbi->fb.fix.accel = FB_ACCEL_NONE;
302
303 fbi->fb.var.nonstd = 0;
304 fbi->fb.var.activate = FB_ACTIVATE_NOW;
305 fbi->fb.var.height = -1;
306 fbi->fb.var.width = -1;
307 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
308
309 fbi->fb.fbops = &vt8500lcd_ops;
310 fbi->fb.flags = FBINFO_DEFAULT
311 | FBINFO_HWACCEL_COPYAREA
312 | FBINFO_HWACCEL_FILLRECT
313 | FBINFO_HWACCEL_YPAN
314 | FBINFO_VIRTFB
315 | FBINFO_PARTIAL_PAN_OK;
316 fbi->fb.node = -1;
317
318 addr = fbi;
319 addr = addr + sizeof(struct vt8500lcd_info);
320 fbi->fb.pseudo_palette = addr;
321
322 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
323 if (res == NULL) {
324 dev_err(&pdev->dev, "no I/O memory resource defined\n");
325 return -ENODEV;
326 }
327
328 res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
329 if (res == NULL) {
330 dev_err(&pdev->dev, "failed to request I/O memory\n");
331 return -EBUSY;
332 }
333
334 fbi->regbase = ioremap(res->start, resource_size(res));
335 if (fbi->regbase == NULL) {
336 dev_err(&pdev->dev, "failed to map I/O memory\n");
337 ret = -EBUSY;
338 goto failed_free_res;
339 }
340
341 disp_timing = of_get_display_timings(pdev->dev.of_node);
342 if (!disp_timing) {
343 ret = -EINVAL;
344 goto failed_free_io;
345 }
346
347 ret = of_get_fb_videomode(pdev->dev.of_node, &of_mode,
348 OF_USE_NATIVE_MODE);
349 if (ret)
350 goto failed_free_io;
351
352 ret = of_property_read_u32(pdev->dev.of_node, "bits-per-pixel", &bpp);
353 if (ret)
354 goto failed_free_io;
355
356
357 fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8);
358 fb_mem_virt = dma_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
359 GFP_KERNEL);
360 if (!fb_mem_virt) {
361 pr_err("%s: Failed to allocate framebuffer\n", __func__);
362 ret = -ENOMEM;
363 goto failed_free_io;
364 }
365
366 fbi->fb.fix.smem_start = fb_mem_phys;
367 fbi->fb.fix.smem_len = fb_mem_len;
368 fbi->fb.screen_base = fb_mem_virt;
369
370 fbi->palette_size = PAGE_ALIGN(512);
371 fbi->palette_cpu = dma_alloc_coherent(&pdev->dev,
372 fbi->palette_size,
373 &fbi->palette_phys,
374 GFP_KERNEL);
375 if (fbi->palette_cpu == NULL) {
376 dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
377 ret = -ENOMEM;
378 goto failed_free_io;
379 }
380
381 irq = platform_get_irq(pdev, 0);
382 if (irq < 0) {
383 dev_err(&pdev->dev, "no IRQ defined\n");
384 ret = -ENODEV;
385 goto failed_free_palette;
386 }
387
388 ret = request_irq(irq, vt8500lcd_handle_irq, 0, "LCD", fbi);
389 if (ret) {
390 dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
391 ret = -EBUSY;
392 goto failed_free_palette;
393 }
394
395 init_waitqueue_head(&fbi->wait);
396
397 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
398 dev_err(&pdev->dev, "Failed to allocate color map\n");
399 ret = -ENOMEM;
400 goto failed_free_irq;
401 }
402
403 fb_videomode_to_var(&fbi->fb.var, &of_mode);
404
405 fbi->fb.var.xres_virtual = of_mode.xres;
406 fbi->fb.var.yres_virtual = of_mode.yres * 2;
407 fbi->fb.var.bits_per_pixel = bpp;
408
409 ret = vt8500lcd_set_par(&fbi->fb);
410 if (ret) {
411 dev_err(&pdev->dev, "Failed to set parameters\n");
412 goto failed_free_cmap;
413 }
414
415 writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
416 writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
417
418 platform_set_drvdata(pdev, fbi);
419
420 ret = register_framebuffer(&fbi->fb);
421 if (ret < 0) {
422 dev_err(&pdev->dev,
423 "Failed to register framebuffer device: %d\n", ret);
424 goto failed_free_cmap;
425 }
426
427
428
429
430 writel(readl(fbi->regbase) | 1, fbi->regbase);
431
432 return 0;
433
434failed_free_cmap:
435 if (fbi->fb.cmap.len)
436 fb_dealloc_cmap(&fbi->fb.cmap);
437failed_free_irq:
438 free_irq(irq, fbi);
439failed_free_palette:
440 dma_free_coherent(&pdev->dev, fbi->palette_size,
441 fbi->palette_cpu, fbi->palette_phys);
442failed_free_io:
443 iounmap(fbi->regbase);
444failed_free_res:
445 release_mem_region(res->start, resource_size(res));
446 return ret;
447}
448
449static int vt8500lcd_remove(struct platform_device *pdev)
450{
451 struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
452 struct resource *res;
453 int irq;
454
455 unregister_framebuffer(&fbi->fb);
456
457 writel(0, fbi->regbase);
458
459 if (fbi->fb.cmap.len)
460 fb_dealloc_cmap(&fbi->fb.cmap);
461
462 irq = platform_get_irq(pdev, 0);
463 free_irq(irq, fbi);
464
465 dma_free_coherent(&pdev->dev, fbi->palette_size,
466 fbi->palette_cpu, fbi->palette_phys);
467
468 iounmap(fbi->regbase);
469
470 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
471 release_mem_region(res->start, resource_size(res));
472
473 return 0;
474}
475
476static const struct of_device_id via_dt_ids[] = {
477 { .compatible = "via,vt8500-fb", },
478 {}
479};
480
481static struct platform_driver vt8500lcd_driver = {
482 .probe = vt8500lcd_probe,
483 .remove = vt8500lcd_remove,
484 .driver = {
485 .name = "vt8500-lcd",
486 .of_match_table = of_match_ptr(via_dt_ids),
487 },
488};
489
490module_platform_driver(vt8500lcd_driver);
491
492MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
493MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
494MODULE_LICENSE("GPL v2");
495MODULE_DEVICE_TABLE(of, via_dt_ids);
496