1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/video/vt8500lcdfb.c
5 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
7 * Based on skeletonfb.c and pxafb.c
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/wait.h>
24 #include <video/of_display_timing.h>
26 #include "vt8500lcdfb.h"
27 #include "wmt_ge_rops.h"
31 #include <linux/of_fdt.h>
32 #include <linux/memblock.h>
36 #define to_vt8500lcd_info(__info) container_of(__info, \
37 struct vt8500lcd_info, fb)
39 static int vt8500lcd_set_par(struct fb_info
*info
)
41 struct vt8500lcd_info
*fbi
= to_vt8500lcd_info(info
);
42 int reg_bpp
= 5; /* 16bpp */
44 unsigned long control0
;
49 if (info
->var
.bits_per_pixel
<= 8) {
51 info
->var
.red
.offset
= 0;
52 info
->var
.red
.length
= info
->var
.bits_per_pixel
;
53 info
->var
.red
.msb_right
= 0;
55 info
->var
.green
.offset
= 0;
56 info
->var
.green
.length
= info
->var
.bits_per_pixel
;
57 info
->var
.green
.msb_right
= 0;
59 info
->var
.blue
.offset
= 0;
60 info
->var
.blue
.length
= info
->var
.bits_per_pixel
;
61 info
->var
.blue
.msb_right
= 0;
63 info
->var
.transp
.offset
= 0;
64 info
->var
.transp
.length
= 0;
65 info
->var
.transp
.msb_right
= 0;
67 info
->fix
.visual
= FB_VISUAL_PSEUDOCOLOR
;
68 info
->fix
.line_length
= info
->var
.xres_virtual
/
69 (8/info
->var
.bits_per_pixel
);
72 info
->var
.transp
.offset
= 0;
73 info
->var
.transp
.length
= 0;
74 info
->var
.transp
.msb_right
= 0;
76 if (info
->var
.bits_per_pixel
== 16) {
78 info
->var
.red
.offset
= 11;
79 info
->var
.red
.length
= 5;
80 info
->var
.red
.msb_right
= 0;
81 info
->var
.green
.offset
= 5;
82 info
->var
.green
.length
= 6;
83 info
->var
.green
.msb_right
= 0;
84 info
->var
.blue
.offset
= 0;
85 info
->var
.blue
.length
= 5;
86 info
->var
.blue
.msb_right
= 0;
88 /* Equal depths per channel */
89 info
->var
.red
.offset
= info
->var
.bits_per_pixel
91 info
->var
.red
.length
= info
->var
.bits_per_pixel
/ 3;
92 info
->var
.red
.msb_right
= 0;
93 info
->var
.green
.offset
= info
->var
.bits_per_pixel
/ 3;
94 info
->var
.green
.length
= info
->var
.bits_per_pixel
/ 3;
95 info
->var
.green
.msb_right
= 0;
96 info
->var
.blue
.offset
= 0;
97 info
->var
.blue
.length
= info
->var
.bits_per_pixel
/ 3;
98 info
->var
.blue
.msb_right
= 0;
101 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
102 info
->fix
.line_length
= info
->var
.bits_per_pixel
> 16 ?
103 info
->var
.xres_virtual
<< 2 :
104 info
->var
.xres_virtual
<< 1;
107 for (i
= 0; i
< 8; i
++) {
108 if (bpp_values
[i
] == info
->var
.bits_per_pixel
)
112 control0
= readl(fbi
->regbase
) & ~0xf;
113 writel(0, fbi
->regbase
);
114 while (readl(fbi
->regbase
+ 0x38) & 0x10)
116 writel((((info
->var
.hsync_len
- 1) & 0x3f) << 26)
117 | ((info
->var
.left_margin
& 0xff) << 18)
118 | (((info
->var
.xres
- 1) & 0x3ff) << 8)
119 | (info
->var
.right_margin
& 0xff), fbi
->regbase
+ 0x4);
120 writel((((info
->var
.vsync_len
- 1) & 0x3f) << 26)
121 | ((info
->var
.upper_margin
& 0xff) << 18)
122 | (((info
->var
.yres
- 1) & 0x3ff) << 8)
123 | (info
->var
.lower_margin
& 0xff), fbi
->regbase
+ 0x8);
124 writel((((info
->var
.yres
- 1) & 0x400) << 2)
125 | ((info
->var
.xres
- 1) & 0x400), fbi
->regbase
+ 0x10);
126 writel(0x80000000, fbi
->regbase
+ 0x20);
127 writel(control0
| (reg_bpp
<< 1) | 0x100, fbi
->regbase
);
132 static inline u_int
chan_to_field(u_int chan
, struct fb_bitfield
*bf
)
135 chan
>>= 16 - bf
->length
;
136 return chan
<< bf
->offset
;
139 static int vt8500lcd_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
140 unsigned blue
, unsigned transp
,
141 struct fb_info
*info
) {
142 struct vt8500lcd_info
*fbi
= to_vt8500lcd_info(info
);
148 if (info
->var
.grayscale
)
150 (19595 * red
+ 38470 * green
+ 7471 * blue
) >> 16;
152 switch (fbi
->fb
.fix
.visual
) {
153 case FB_VISUAL_TRUECOLOR
:
155 u32
*pal
= fbi
->fb
.pseudo_palette
;
157 val
= chan_to_field(red
, &fbi
->fb
.var
.red
);
158 val
|= chan_to_field(green
, &fbi
->fb
.var
.green
);
159 val
|= chan_to_field(blue
, &fbi
->fb
.var
.blue
);
166 case FB_VISUAL_STATIC_PSEUDOCOLOR
:
167 case FB_VISUAL_PSEUDOCOLOR
:
168 writew((red
& 0xf800)
169 | ((green
>> 5) & 0x7e0)
170 | ((blue
>> 11) & 0x1f),
171 fbi
->palette_cpu
+ sizeof(u16
) * regno
);
178 static int vt8500lcd_ioctl(struct fb_info
*info
, unsigned int cmd
,
182 struct vt8500lcd_info
*fbi
= to_vt8500lcd_info(info
);
184 if (cmd
== FBIO_WAITFORVSYNC
) {
185 /* Unmask End of Frame interrupt */
186 writel(0xffffffff ^ (1 << 3), fbi
->regbase
+ 0x3c);
187 ret
= wait_event_interruptible_timeout(fbi
->wait
,
188 readl(fbi
->regbase
+ 0x38) & (1 << 3), HZ
/ 10);
189 /* Mask back to reduce unwanted interrupt traffic */
190 writel(0xffffffff, fbi
->regbase
+ 0x3c);
200 static int vt8500lcd_pan_display(struct fb_var_screeninfo
*var
,
201 struct fb_info
*info
)
203 unsigned pixlen
= info
->fix
.line_length
/ info
->var
.xres_virtual
;
204 unsigned off
= pixlen
* var
->xoffset
205 + info
->fix
.line_length
* var
->yoffset
;
206 struct vt8500lcd_info
*fbi
= to_vt8500lcd_info(info
);
209 | (((info
->var
.xres_virtual
- info
->var
.xres
) * pixlen
/ 4) << 20)
210 | (off
>> 2), fbi
->regbase
+ 0x20);
216 * Blank the display by setting all palette values to zero. Note,
217 * True Color modes do not really use the palette, so this will not
218 * blank the display in all modes.
220 static int vt8500lcd_blank(int blank
, struct fb_info
*info
)
225 case FB_BLANK_POWERDOWN
:
226 case FB_BLANK_VSYNC_SUSPEND
:
227 case FB_BLANK_HSYNC_SUSPEND
:
228 case FB_BLANK_NORMAL
:
229 if (info
->fix
.visual
== FB_VISUAL_PSEUDOCOLOR
||
230 info
->fix
.visual
== FB_VISUAL_STATIC_PSEUDOCOLOR
)
231 for (i
= 0; i
< 256; i
++)
232 vt8500lcd_setcolreg(i
, 0, 0, 0, 0, info
);
234 case FB_BLANK_UNBLANK
:
235 if (info
->fix
.visual
== FB_VISUAL_PSEUDOCOLOR
||
236 info
->fix
.visual
== FB_VISUAL_STATIC_PSEUDOCOLOR
)
237 fb_set_cmap(&info
->cmap
, info
);
242 static const struct fb_ops vt8500lcd_ops
= {
243 .owner
= THIS_MODULE
,
244 .fb_set_par
= vt8500lcd_set_par
,
245 .fb_setcolreg
= vt8500lcd_setcolreg
,
246 .fb_fillrect
= wmt_ge_fillrect
,
247 .fb_copyarea
= wmt_ge_copyarea
,
248 .fb_imageblit
= sys_imageblit
,
249 .fb_sync
= wmt_ge_sync
,
250 .fb_ioctl
= vt8500lcd_ioctl
,
251 .fb_pan_display
= vt8500lcd_pan_display
,
252 .fb_blank
= vt8500lcd_blank
,
255 static irqreturn_t
vt8500lcd_handle_irq(int irq
, void *dev_id
)
257 struct vt8500lcd_info
*fbi
= dev_id
;
259 if (readl(fbi
->regbase
+ 0x38) & (1 << 3))
260 wake_up_interruptible(&fbi
->wait
);
262 writel(0xffffffff, fbi
->regbase
+ 0x38);
266 static int vt8500lcd_probe(struct platform_device
*pdev
)
268 struct vt8500lcd_info
*fbi
;
269 struct resource
*res
;
270 struct display_timings
*disp_timing
;
274 struct fb_videomode of_mode
;
276 dma_addr_t fb_mem_phys
;
277 unsigned long fb_mem_len
;
283 fbi
= devm_kzalloc(&pdev
->dev
, sizeof(struct vt8500lcd_info
)
284 + sizeof(u32
) * 16, GFP_KERNEL
);
288 strcpy(fbi
->fb
.fix
.id
, "VT8500 LCD");
290 fbi
->fb
.fix
.type
= FB_TYPE_PACKED_PIXELS
;
291 fbi
->fb
.fix
.xpanstep
= 0;
292 fbi
->fb
.fix
.ypanstep
= 1;
293 fbi
->fb
.fix
.ywrapstep
= 0;
294 fbi
->fb
.fix
.accel
= FB_ACCEL_NONE
;
296 fbi
->fb
.var
.nonstd
= 0;
297 fbi
->fb
.var
.activate
= FB_ACTIVATE_NOW
;
298 fbi
->fb
.var
.height
= -1;
299 fbi
->fb
.var
.width
= -1;
300 fbi
->fb
.var
.vmode
= FB_VMODE_NONINTERLACED
;
302 fbi
->fb
.fbops
= &vt8500lcd_ops
;
303 fbi
->fb
.flags
= FBINFO_DEFAULT
304 | FBINFO_HWACCEL_COPYAREA
305 | FBINFO_HWACCEL_FILLRECT
306 | FBINFO_HWACCEL_YPAN
308 | FBINFO_PARTIAL_PAN_OK
;
312 addr
= addr
+ sizeof(struct vt8500lcd_info
);
313 fbi
->fb
.pseudo_palette
= addr
;
315 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
317 dev_err(&pdev
->dev
, "no I/O memory resource defined\n");
321 res
= request_mem_region(res
->start
, resource_size(res
), "vt8500lcd");
323 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
327 fbi
->regbase
= ioremap(res
->start
, resource_size(res
));
328 if (fbi
->regbase
== NULL
) {
329 dev_err(&pdev
->dev
, "failed to map I/O memory\n");
331 goto failed_free_res
;
334 disp_timing
= of_get_display_timings(pdev
->dev
.of_node
);
340 ret
= of_get_fb_videomode(pdev
->dev
.of_node
, &of_mode
,
345 ret
= of_property_read_u32(pdev
->dev
.of_node
, "bits-per-pixel", &bpp
);
349 /* try allocating the framebuffer */
350 fb_mem_len
= of_mode
.xres
* of_mode
.yres
* 2 * (bpp
/ 8);
351 fb_mem_virt
= dma_alloc_coherent(&pdev
->dev
, fb_mem_len
, &fb_mem_phys
,
354 pr_err("%s: Failed to allocate framebuffer\n", __func__
);
359 fbi
->fb
.fix
.smem_start
= fb_mem_phys
;
360 fbi
->fb
.fix
.smem_len
= fb_mem_len
;
361 fbi
->fb
.screen_base
= fb_mem_virt
;
363 fbi
->palette_size
= PAGE_ALIGN(512);
364 fbi
->palette_cpu
= dma_alloc_coherent(&pdev
->dev
,
368 if (fbi
->palette_cpu
== NULL
) {
369 dev_err(&pdev
->dev
, "Failed to allocate palette buffer\n");
374 irq
= platform_get_irq(pdev
, 0);
376 dev_err(&pdev
->dev
, "no IRQ defined\n");
378 goto failed_free_palette
;
381 ret
= request_irq(irq
, vt8500lcd_handle_irq
, 0, "LCD", fbi
);
383 dev_err(&pdev
->dev
, "request_irq failed: %d\n", ret
);
385 goto failed_free_palette
;
388 init_waitqueue_head(&fbi
->wait
);
390 if (fb_alloc_cmap(&fbi
->fb
.cmap
, 256, 0) < 0) {
391 dev_err(&pdev
->dev
, "Failed to allocate color map\n");
393 goto failed_free_irq
;
396 fb_videomode_to_var(&fbi
->fb
.var
, &of_mode
);
398 fbi
->fb
.var
.xres_virtual
= of_mode
.xres
;
399 fbi
->fb
.var
.yres_virtual
= of_mode
.yres
* 2;
400 fbi
->fb
.var
.bits_per_pixel
= bpp
;
402 ret
= vt8500lcd_set_par(&fbi
->fb
);
404 dev_err(&pdev
->dev
, "Failed to set parameters\n");
405 goto failed_free_cmap
;
408 writel(fbi
->fb
.fix
.smem_start
>> 22, fbi
->regbase
+ 0x1c);
409 writel((fbi
->palette_phys
& 0xfffffe00) | 1, fbi
->regbase
+ 0x18);
411 platform_set_drvdata(pdev
, fbi
);
413 ret
= register_framebuffer(&fbi
->fb
);
416 "Failed to register framebuffer device: %d\n", ret
);
417 goto failed_free_cmap
;
421 * Ok, now enable the LCD controller
423 writel(readl(fbi
->regbase
) | 1, fbi
->regbase
);
428 if (fbi
->fb
.cmap
.len
)
429 fb_dealloc_cmap(&fbi
->fb
.cmap
);
433 dma_free_coherent(&pdev
->dev
, fbi
->palette_size
,
434 fbi
->palette_cpu
, fbi
->palette_phys
);
436 iounmap(fbi
->regbase
);
438 release_mem_region(res
->start
, resource_size(res
));
442 static int vt8500lcd_remove(struct platform_device
*pdev
)
444 struct vt8500lcd_info
*fbi
= platform_get_drvdata(pdev
);
445 struct resource
*res
;
448 unregister_framebuffer(&fbi
->fb
);
450 writel(0, fbi
->regbase
);
452 if (fbi
->fb
.cmap
.len
)
453 fb_dealloc_cmap(&fbi
->fb
.cmap
);
455 irq
= platform_get_irq(pdev
, 0);
458 dma_free_coherent(&pdev
->dev
, fbi
->palette_size
,
459 fbi
->palette_cpu
, fbi
->palette_phys
);
461 iounmap(fbi
->regbase
);
463 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
464 release_mem_region(res
->start
, resource_size(res
));
469 static const struct of_device_id via_dt_ids
[] = {
470 { .compatible
= "via,vt8500-fb", },
474 static struct platform_driver vt8500lcd_driver
= {
475 .probe
= vt8500lcd_probe
,
476 .remove
= vt8500lcd_remove
,
478 .name
= "vt8500-lcd",
479 .of_match_table
= of_match_ptr(via_dt_ids
),
483 module_platform_driver(vt8500lcd_driver
);
485 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
486 MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
487 MODULE_LICENSE("GPL v2");
488 MODULE_DEVICE_TABLE(of
, via_dt_ids
);