1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/video/vt8500lcdfb.c
4  *
5  *  Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
6  *
7  * Based on skeletonfb.c and pxafb.c
8  */
9 
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/errno.h>
13 #include <linux/fb.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.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>
25 
26 #include "vt8500lcdfb.h"
27 #include "wmt_ge_rops.h"
28 
29 #ifdef CONFIG_OF
30 #include <linux/of.h>
31 #include <linux/of_fdt.h>
32 #include <linux/memblock.h>
33 #endif
34 
35 
36 #define to_vt8500lcd_info(__info) container_of(__info, \
37 						struct vt8500lcd_info, fb)
38 
vt8500lcd_set_par(struct fb_info * info)39 static int vt8500lcd_set_par(struct fb_info *info)
40 {
41 	struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
42 	int reg_bpp = 5; /* 16bpp */
43 	int i;
44 	unsigned long control0;
45 
46 	if (!fbi)
47 		return -EINVAL;
48 
49 	if (info->var.bits_per_pixel <= 8) {
50 		/* palettized */
51 		info->var.red.offset    = 0;
52 		info->var.red.length    = info->var.bits_per_pixel;
53 		info->var.red.msb_right = 0;
54 
55 		info->var.green.offset  = 0;
56 		info->var.green.length  = info->var.bits_per_pixel;
57 		info->var.green.msb_right = 0;
58 
59 		info->var.blue.offset   = 0;
60 		info->var.blue.length   = info->var.bits_per_pixel;
61 		info->var.blue.msb_right = 0;
62 
63 		info->var.transp.offset = 0;
64 		info->var.transp.length = 0;
65 		info->var.transp.msb_right = 0;
66 
67 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
68 		info->fix.line_length = info->var.xres_virtual /
69 						(8/info->var.bits_per_pixel);
70 	} else {
71 		/* non-palettized */
72 		info->var.transp.offset = 0;
73 		info->var.transp.length = 0;
74 		info->var.transp.msb_right = 0;
75 
76 		if (info->var.bits_per_pixel == 16) {
77 			/* RGB565 */
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;
87 		} else {
88 			/* Equal depths per channel */
89 			info->var.red.offset = info->var.bits_per_pixel
90 							* 2 / 3;
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;
99 		}
100 
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;
105 	}
106 
107 	for (i = 0; i < 8; i++) {
108 		if (bpp_values[i] == info->var.bits_per_pixel)
109 			reg_bpp = i;
110 	}
111 
112 	control0 = readl(fbi->regbase) & ~0xf;
113 	writel(0, fbi->regbase);
114 	while (readl(fbi->regbase + 0x38) & 0x10)
115 		/* wait */;
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);
128 
129 	return 0;
130 }
131 
chan_to_field(u_int chan,struct fb_bitfield * bf)132 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
133 {
134 	chan &= 0xffff;
135 	chan >>= 16 - bf->length;
136 	return chan << bf->offset;
137 }
138 
vt8500lcd_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)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);
143 	int ret = 1;
144 	unsigned int val;
145 	if (regno >= 256)
146 		return -EINVAL;
147 
148 	if (info->var.grayscale)
149 		red = green = blue =
150 			(19595 * red + 38470 * green + 7471 * blue) >> 16;
151 
152 	switch (fbi->fb.fix.visual) {
153 	case FB_VISUAL_TRUECOLOR:
154 		if (regno < 16) {
155 			u32 *pal = fbi->fb.pseudo_palette;
156 
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);
160 
161 			pal[regno] = val;
162 			ret = 0;
163 		}
164 		break;
165 
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);
172 		break;
173 	}
174 
175 	return ret;
176 }
177 
vt8500lcd_ioctl(struct fb_info * info,unsigned int cmd,unsigned long arg)178 static int vt8500lcd_ioctl(struct fb_info *info, unsigned int cmd,
179 			 unsigned long arg)
180 {
181 	int ret = 0;
182 	struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
183 
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);
191 		if (ret < 0)
192 			return ret;
193 		if (ret == 0)
194 			return -ETIMEDOUT;
195 	}
196 
197 	return ret;
198 }
199 
vt8500lcd_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)200 static int vt8500lcd_pan_display(struct fb_var_screeninfo *var,
201 				struct fb_info *info)
202 {
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);
207 
208 	writel((1 << 31)
209 	     | (((info->var.xres_virtual - info->var.xres) * pixlen / 4) << 20)
210 	     | (off >> 2), fbi->regbase + 0x20);
211 	return 0;
212 }
213 
214 /*
215  * vt8500lcd_blank():
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.
219  */
vt8500lcd_blank(int blank,struct fb_info * info)220 static int vt8500lcd_blank(int blank, struct fb_info *info)
221 {
222 	int i;
223 
224 	switch (blank) {
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);
233 		fallthrough;
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);
238 	}
239 	return 0;
240 }
241 
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,
253 };
254 
vt8500lcd_handle_irq(int irq,void * dev_id)255 static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id)
256 {
257 	struct vt8500lcd_info *fbi = dev_id;
258 
259 	if (readl(fbi->regbase + 0x38) & (1 << 3))
260 		wake_up_interruptible(&fbi->wait);
261 
262 	writel(0xffffffff, fbi->regbase + 0x38);
263 	return IRQ_HANDLED;
264 }
265 
vt8500lcd_probe(struct platform_device * pdev)266 static int vt8500lcd_probe(struct platform_device *pdev)
267 {
268 	struct vt8500lcd_info *fbi;
269 	struct resource *res;
270 	struct display_timings *disp_timing;
271 	void *addr;
272 	int irq, ret;
273 
274 	struct fb_videomode	of_mode;
275 	u32			bpp;
276 	dma_addr_t fb_mem_phys;
277 	unsigned long fb_mem_len;
278 	void *fb_mem_virt;
279 
280 	ret = -ENOMEM;
281 	fbi = NULL;
282 
283 	fbi = devm_kzalloc(&pdev->dev, sizeof(struct vt8500lcd_info)
284 			+ sizeof(u32) * 16, GFP_KERNEL);
285 	if (!fbi)
286 		return -ENOMEM;
287 
288 	strcpy(fbi->fb.fix.id, "VT8500 LCD");
289 
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;
295 
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;
301 
302 	fbi->fb.fbops		= &vt8500lcd_ops;
303 	fbi->fb.flags		= FBINFO_HWACCEL_COPYAREA
304 				| FBINFO_HWACCEL_FILLRECT
305 				| FBINFO_HWACCEL_YPAN
306 				| FBINFO_VIRTFB
307 				| FBINFO_PARTIAL_PAN_OK;
308 	fbi->fb.node		= -1;
309 
310 	addr = fbi;
311 	addr = addr + sizeof(struct vt8500lcd_info);
312 	fbi->fb.pseudo_palette	= addr;
313 
314 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
315 	if (res == NULL) {
316 		dev_err(&pdev->dev, "no I/O memory resource defined\n");
317 		return -ENODEV;
318 	}
319 
320 	res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
321 	if (res == NULL) {
322 		dev_err(&pdev->dev, "failed to request I/O memory\n");
323 		return -EBUSY;
324 	}
325 
326 	fbi->regbase = ioremap(res->start, resource_size(res));
327 	if (fbi->regbase == NULL) {
328 		dev_err(&pdev->dev, "failed to map I/O memory\n");
329 		ret = -EBUSY;
330 		goto failed_free_res;
331 	}
332 
333 	disp_timing = of_get_display_timings(pdev->dev.of_node);
334 	if (!disp_timing) {
335 		ret = -EINVAL;
336 		goto failed_free_io;
337 	}
338 
339 	ret = of_get_fb_videomode(pdev->dev.of_node, &of_mode,
340 							OF_USE_NATIVE_MODE);
341 	if (ret)
342 		goto failed_free_io;
343 
344 	ret = of_property_read_u32(pdev->dev.of_node, "bits-per-pixel", &bpp);
345 	if (ret)
346 		goto failed_free_io;
347 
348 	/* try allocating the framebuffer */
349 	fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8);
350 	fb_mem_virt = dma_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
351 				GFP_KERNEL);
352 	if (!fb_mem_virt) {
353 		pr_err("%s: Failed to allocate framebuffer\n", __func__);
354 		ret = -ENOMEM;
355 		goto failed_free_io;
356 	}
357 
358 	fbi->fb.fix.smem_start	= fb_mem_phys;
359 	fbi->fb.fix.smem_len	= fb_mem_len;
360 	fbi->fb.screen_base	= fb_mem_virt;
361 
362 	fbi->palette_size	= PAGE_ALIGN(512);
363 	fbi->palette_cpu	= dma_alloc_coherent(&pdev->dev,
364 						     fbi->palette_size,
365 						     &fbi->palette_phys,
366 						     GFP_KERNEL);
367 	if (fbi->palette_cpu == NULL) {
368 		dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
369 		ret = -ENOMEM;
370 		goto failed_free_io;
371 	}
372 
373 	irq = platform_get_irq(pdev, 0);
374 	if (irq < 0) {
375 		dev_err(&pdev->dev, "no IRQ defined\n");
376 		ret = -ENODEV;
377 		goto failed_free_palette;
378 	}
379 
380 	ret = request_irq(irq, vt8500lcd_handle_irq, 0, "LCD", fbi);
381 	if (ret) {
382 		dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
383 		ret = -EBUSY;
384 		goto failed_free_palette;
385 	}
386 
387 	init_waitqueue_head(&fbi->wait);
388 
389 	if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
390 		dev_err(&pdev->dev, "Failed to allocate color map\n");
391 		ret = -ENOMEM;
392 		goto failed_free_irq;
393 	}
394 
395 	fb_videomode_to_var(&fbi->fb.var, &of_mode);
396 
397 	fbi->fb.var.xres_virtual	= of_mode.xres;
398 	fbi->fb.var.yres_virtual	= of_mode.yres * 2;
399 	fbi->fb.var.bits_per_pixel	= bpp;
400 
401 	ret = vt8500lcd_set_par(&fbi->fb);
402 	if (ret) {
403 		dev_err(&pdev->dev, "Failed to set parameters\n");
404 		goto failed_free_cmap;
405 	}
406 
407 	writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
408 	writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
409 
410 	platform_set_drvdata(pdev, fbi);
411 
412 	ret = register_framebuffer(&fbi->fb);
413 	if (ret < 0) {
414 		dev_err(&pdev->dev,
415 			"Failed to register framebuffer device: %d\n", ret);
416 		goto failed_free_cmap;
417 	}
418 
419 	/*
420 	 * Ok, now enable the LCD controller
421 	 */
422 	writel(readl(fbi->regbase) | 1, fbi->regbase);
423 
424 	return 0;
425 
426 failed_free_cmap:
427 	if (fbi->fb.cmap.len)
428 		fb_dealloc_cmap(&fbi->fb.cmap);
429 failed_free_irq:
430 	free_irq(irq, fbi);
431 failed_free_palette:
432 	dma_free_coherent(&pdev->dev, fbi->palette_size,
433 			  fbi->palette_cpu, fbi->palette_phys);
434 failed_free_io:
435 	iounmap(fbi->regbase);
436 failed_free_res:
437 	release_mem_region(res->start, resource_size(res));
438 	return ret;
439 }
440 
vt8500lcd_remove(struct platform_device * pdev)441 static void vt8500lcd_remove(struct platform_device *pdev)
442 {
443 	struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
444 	struct resource *res;
445 	int irq;
446 
447 	unregister_framebuffer(&fbi->fb);
448 
449 	writel(0, fbi->regbase);
450 
451 	if (fbi->fb.cmap.len)
452 		fb_dealloc_cmap(&fbi->fb.cmap);
453 
454 	irq = platform_get_irq(pdev, 0);
455 	free_irq(irq, fbi);
456 
457 	dma_free_coherent(&pdev->dev, fbi->palette_size,
458 			  fbi->palette_cpu, fbi->palette_phys);
459 
460 	iounmap(fbi->regbase);
461 
462 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
463 	release_mem_region(res->start, resource_size(res));
464 }
465 
466 static const struct of_device_id via_dt_ids[] = {
467 	{ .compatible = "via,vt8500-fb", },
468 	{}
469 };
470 
471 static struct platform_driver vt8500lcd_driver = {
472 	.probe		= vt8500lcd_probe,
473 	.remove_new	= vt8500lcd_remove,
474 	.driver		= {
475 		.name	= "vt8500-lcd",
476 		.of_match_table = of_match_ptr(via_dt_ids),
477 	},
478 };
479 
480 module_platform_driver(vt8500lcd_driver);
481 
482 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
483 MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
484 MODULE_LICENSE("GPL v2");
485 MODULE_DEVICE_TABLE(of, via_dt_ids);
486