xref: /linux/drivers/video/fbdev/goldfishfb.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f7018c21STomi Valkeinen /*
3f7018c21STomi Valkeinen  * Copyright (C) 2007 Google, Inc.
4f7018c21STomi Valkeinen  * Copyright (C) 2012 Intel, Inc.
5f7018c21STomi Valkeinen  */
6f7018c21STomi Valkeinen 
7f7018c21STomi Valkeinen #include <linux/module.h>
8f7018c21STomi Valkeinen #include <linux/kernel.h>
9f7018c21STomi Valkeinen #include <linux/dma-mapping.h>
10f7018c21STomi Valkeinen #include <linux/errno.h>
11f7018c21STomi Valkeinen #include <linux/string.h>
12f7018c21STomi Valkeinen #include <linux/slab.h>
13f7018c21STomi Valkeinen #include <linux/delay.h>
14f7018c21STomi Valkeinen #include <linux/mm.h>
15f7018c21STomi Valkeinen #include <linux/fb.h>
16f7018c21STomi Valkeinen #include <linux/init.h>
17f7018c21STomi Valkeinen #include <linux/interrupt.h>
18f7018c21STomi Valkeinen #include <linux/ioport.h>
19f7018c21STomi Valkeinen #include <linux/platform_device.h>
201ef4e117SYu Ning #include <linux/acpi.h>
21f7018c21STomi Valkeinen 
22f7018c21STomi Valkeinen enum {
23f7018c21STomi Valkeinen 	FB_GET_WIDTH        = 0x00,
24f7018c21STomi Valkeinen 	FB_GET_HEIGHT       = 0x04,
25f7018c21STomi Valkeinen 	FB_INT_STATUS       = 0x08,
26f7018c21STomi Valkeinen 	FB_INT_ENABLE       = 0x0c,
27f7018c21STomi Valkeinen 	FB_SET_BASE         = 0x10,
28f7018c21STomi Valkeinen 	FB_SET_ROTATION     = 0x14,
29f7018c21STomi Valkeinen 	FB_SET_BLANK        = 0x18,
30f7018c21STomi Valkeinen 	FB_GET_PHYS_WIDTH   = 0x1c,
31f7018c21STomi Valkeinen 	FB_GET_PHYS_HEIGHT  = 0x20,
32f7018c21STomi Valkeinen 
33f7018c21STomi Valkeinen 	FB_INT_VSYNC             = 1U << 0,
34f7018c21STomi Valkeinen 	FB_INT_BASE_UPDATE_DONE  = 1U << 1
35f7018c21STomi Valkeinen };
36f7018c21STomi Valkeinen 
37f7018c21STomi Valkeinen struct goldfish_fb {
38f7018c21STomi Valkeinen 	void __iomem *reg_base;
39f7018c21STomi Valkeinen 	int irq;
40f7018c21STomi Valkeinen 	spinlock_t lock;
41f7018c21STomi Valkeinen 	wait_queue_head_t wait;
42f7018c21STomi Valkeinen 	int base_update_count;
43f7018c21STomi Valkeinen 	int rotation;
44f7018c21STomi Valkeinen 	struct fb_info fb;
45f7018c21STomi Valkeinen 	u32 cmap[16];
46f7018c21STomi Valkeinen };
47f7018c21STomi Valkeinen 
goldfish_fb_interrupt(int irq,void * dev_id)48f7018c21STomi Valkeinen static irqreturn_t goldfish_fb_interrupt(int irq, void *dev_id)
49f7018c21STomi Valkeinen {
50f7018c21STomi Valkeinen 	unsigned long irq_flags;
51f7018c21STomi Valkeinen 	struct goldfish_fb *fb = dev_id;
52f7018c21STomi Valkeinen 	u32 status;
53f7018c21STomi Valkeinen 
54f7018c21STomi Valkeinen 	spin_lock_irqsave(&fb->lock, irq_flags);
55f7018c21STomi Valkeinen 	status = readl(fb->reg_base + FB_INT_STATUS);
56f7018c21STomi Valkeinen 	if (status & FB_INT_BASE_UPDATE_DONE) {
57f7018c21STomi Valkeinen 		fb->base_update_count++;
58f7018c21STomi Valkeinen 		wake_up(&fb->wait);
59f7018c21STomi Valkeinen 	}
60f7018c21STomi Valkeinen 	spin_unlock_irqrestore(&fb->lock, irq_flags);
61f7018c21STomi Valkeinen 	return status ? IRQ_HANDLED : IRQ_NONE;
62f7018c21STomi Valkeinen }
63f7018c21STomi Valkeinen 
convert_bitfield(int val,struct fb_bitfield * bf)64f7018c21STomi Valkeinen static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
65f7018c21STomi Valkeinen {
66f7018c21STomi Valkeinen 	unsigned int mask = (1 << bf->length) - 1;
67f7018c21STomi Valkeinen 
68f7018c21STomi Valkeinen 	return (val >> (16 - bf->length) & mask) << bf->offset;
69f7018c21STomi Valkeinen }
70f7018c21STomi Valkeinen 
71f7018c21STomi Valkeinen static int
goldfish_fb_setcolreg(unsigned int regno,unsigned int red,unsigned int green,unsigned int blue,unsigned int transp,struct fb_info * info)72f7018c21STomi Valkeinen goldfish_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
73f7018c21STomi Valkeinen 		 unsigned int blue, unsigned int transp, struct fb_info *info)
74f7018c21STomi Valkeinen {
75f7018c21STomi Valkeinen 	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
76f7018c21STomi Valkeinen 
77f7018c21STomi Valkeinen 	if (regno < 16) {
78f7018c21STomi Valkeinen 		fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
79f7018c21STomi Valkeinen 				  convert_bitfield(blue, &fb->fb.var.blue) |
80f7018c21STomi Valkeinen 				  convert_bitfield(green, &fb->fb.var.green) |
81f7018c21STomi Valkeinen 				  convert_bitfield(red, &fb->fb.var.red);
82f7018c21STomi Valkeinen 		return 0;
83f7018c21STomi Valkeinen 	} else {
84f7018c21STomi Valkeinen 		return 1;
85f7018c21STomi Valkeinen 	}
86f7018c21STomi Valkeinen }
87f7018c21STomi Valkeinen 
goldfish_fb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)88f7018c21STomi Valkeinen static int goldfish_fb_check_var(struct fb_var_screeninfo *var,
89f7018c21STomi Valkeinen 							struct fb_info *info)
90f7018c21STomi Valkeinen {
91f7018c21STomi Valkeinen 	if ((var->rotate & 1) != (info->var.rotate & 1)) {
92f7018c21STomi Valkeinen 		if ((var->xres != info->var.yres) ||
93f7018c21STomi Valkeinen 				(var->yres != info->var.xres) ||
94f7018c21STomi Valkeinen 				(var->xres_virtual != info->var.yres) ||
95f7018c21STomi Valkeinen 				(var->yres_virtual > info->var.xres * 2) ||
96f7018c21STomi Valkeinen 				(var->yres_virtual < info->var.xres)) {
97f7018c21STomi Valkeinen 			return -EINVAL;
98f7018c21STomi Valkeinen 		}
99f7018c21STomi Valkeinen 	} else {
100f7018c21STomi Valkeinen 		if ((var->xres != info->var.xres) ||
101f7018c21STomi Valkeinen 		   (var->yres != info->var.yres) ||
102f7018c21STomi Valkeinen 		   (var->xres_virtual != info->var.xres) ||
103f7018c21STomi Valkeinen 		   (var->yres_virtual > info->var.yres * 2) ||
104f7018c21STomi Valkeinen 		   (var->yres_virtual < info->var.yres)) {
105f7018c21STomi Valkeinen 			return -EINVAL;
106f7018c21STomi Valkeinen 		}
107f7018c21STomi Valkeinen 	}
108f7018c21STomi Valkeinen 	if ((var->xoffset != info->var.xoffset) ||
109f7018c21STomi Valkeinen 			(var->bits_per_pixel != info->var.bits_per_pixel) ||
110f7018c21STomi Valkeinen 			(var->grayscale != info->var.grayscale)) {
111f7018c21STomi Valkeinen 		return -EINVAL;
112f7018c21STomi Valkeinen 	}
113f7018c21STomi Valkeinen 	return 0;
114f7018c21STomi Valkeinen }
115f7018c21STomi Valkeinen 
goldfish_fb_set_par(struct fb_info * info)116f7018c21STomi Valkeinen static int goldfish_fb_set_par(struct fb_info *info)
117f7018c21STomi Valkeinen {
118f7018c21STomi Valkeinen 	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
11938e8f5c8SRoman Kiryanov 
120f7018c21STomi Valkeinen 	if (fb->rotation != fb->fb.var.rotate) {
121f7018c21STomi Valkeinen 		info->fix.line_length = info->var.xres * 2;
122f7018c21STomi Valkeinen 		fb->rotation = fb->fb.var.rotate;
123f7018c21STomi Valkeinen 		writel(fb->rotation, fb->reg_base + FB_SET_ROTATION);
124f7018c21STomi Valkeinen 	}
125f7018c21STomi Valkeinen 	return 0;
126f7018c21STomi Valkeinen }
127f7018c21STomi Valkeinen 
128f7018c21STomi Valkeinen 
goldfish_fb_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)129f7018c21STomi Valkeinen static int goldfish_fb_pan_display(struct fb_var_screeninfo *var,
130f7018c21STomi Valkeinen 							struct fb_info *info)
131f7018c21STomi Valkeinen {
132f7018c21STomi Valkeinen 	unsigned long irq_flags;
133f7018c21STomi Valkeinen 	int base_update_count;
134f7018c21STomi Valkeinen 	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
135f7018c21STomi Valkeinen 
136f7018c21STomi Valkeinen 	spin_lock_irqsave(&fb->lock, irq_flags);
137f7018c21STomi Valkeinen 	base_update_count = fb->base_update_count;
138f7018c21STomi Valkeinen 	writel(fb->fb.fix.smem_start + fb->fb.var.xres * 2 * var->yoffset,
139f7018c21STomi Valkeinen 						fb->reg_base + FB_SET_BASE);
140f7018c21STomi Valkeinen 	spin_unlock_irqrestore(&fb->lock, irq_flags);
141f7018c21STomi Valkeinen 	wait_event_timeout(fb->wait,
142f7018c21STomi Valkeinen 			fb->base_update_count != base_update_count, HZ / 15);
143f7018c21STomi Valkeinen 	if (fb->base_update_count == base_update_count)
14438e8f5c8SRoman Kiryanov 		pr_err("%s: timeout waiting for base update\n", __func__);
145f7018c21STomi Valkeinen 	return 0;
146f7018c21STomi Valkeinen }
147f7018c21STomi Valkeinen 
goldfish_fb_blank(int blank,struct fb_info * info)148f7018c21STomi Valkeinen static int goldfish_fb_blank(int blank, struct fb_info *info)
149f7018c21STomi Valkeinen {
150f7018c21STomi Valkeinen 	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
15138e8f5c8SRoman Kiryanov 
152f7018c21STomi Valkeinen 	switch (blank) {
153f7018c21STomi Valkeinen 	case FB_BLANK_NORMAL:
154f7018c21STomi Valkeinen 		writel(1, fb->reg_base + FB_SET_BLANK);
155f7018c21STomi Valkeinen 		break;
156f7018c21STomi Valkeinen 	case FB_BLANK_UNBLANK:
157f7018c21STomi Valkeinen 		writel(0, fb->reg_base + FB_SET_BLANK);
158f7018c21STomi Valkeinen 		break;
159f7018c21STomi Valkeinen 	}
160f7018c21STomi Valkeinen 	return 0;
161f7018c21STomi Valkeinen }
162f7018c21STomi Valkeinen 
1638a48ac33SJani Nikula static const struct fb_ops goldfish_fb_ops = {
164f7018c21STomi Valkeinen 	.owner          = THIS_MODULE,
16566813970SThomas Zimmermann 	FB_DEFAULT_IOMEM_OPS,
166f7018c21STomi Valkeinen 	.fb_check_var   = goldfish_fb_check_var,
167f7018c21STomi Valkeinen 	.fb_set_par     = goldfish_fb_set_par,
168f7018c21STomi Valkeinen 	.fb_setcolreg   = goldfish_fb_setcolreg,
169f7018c21STomi Valkeinen 	.fb_pan_display = goldfish_fb_pan_display,
170f7018c21STomi Valkeinen 	.fb_blank	= goldfish_fb_blank,
171f7018c21STomi Valkeinen };
172f7018c21STomi Valkeinen 
173f7018c21STomi Valkeinen 
goldfish_fb_probe(struct platform_device * pdev)174f7018c21STomi Valkeinen static int goldfish_fb_probe(struct platform_device *pdev)
175f7018c21STomi Valkeinen {
176f7018c21STomi Valkeinen 	int ret;
177f7018c21STomi Valkeinen 	struct resource *r;
178f7018c21STomi Valkeinen 	struct goldfish_fb *fb;
179f7018c21STomi Valkeinen 	size_t framesize;
180f7018c21STomi Valkeinen 	u32 width, height;
181f7018c21STomi Valkeinen 	dma_addr_t fbpaddr;
182f7018c21STomi Valkeinen 
183f7018c21STomi Valkeinen 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
184f7018c21STomi Valkeinen 	if (fb == NULL) {
185f7018c21STomi Valkeinen 		ret = -ENOMEM;
186f7018c21STomi Valkeinen 		goto err_fb_alloc_failed;
187f7018c21STomi Valkeinen 	}
188f7018c21STomi Valkeinen 	spin_lock_init(&fb->lock);
189f7018c21STomi Valkeinen 	init_waitqueue_head(&fb->wait);
190f7018c21STomi Valkeinen 	platform_set_drvdata(pdev, fb);
191f7018c21STomi Valkeinen 
192f7018c21STomi Valkeinen 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193f7018c21STomi Valkeinen 	if (r == NULL) {
194f7018c21STomi Valkeinen 		ret = -ENODEV;
195f7018c21STomi Valkeinen 		goto err_no_io_base;
196f7018c21STomi Valkeinen 	}
197f7018c21STomi Valkeinen 	fb->reg_base = ioremap(r->start, PAGE_SIZE);
198f7018c21STomi Valkeinen 	if (fb->reg_base == NULL) {
199f7018c21STomi Valkeinen 		ret = -ENOMEM;
200f7018c21STomi Valkeinen 		goto err_no_io_base;
201f7018c21STomi Valkeinen 	}
202f7018c21STomi Valkeinen 
203f7018c21STomi Valkeinen 	fb->irq = platform_get_irq(pdev, 0);
2040650d509SZhu Wang 	if (fb->irq < 0) {
2050650d509SZhu Wang 		ret = fb->irq;
206f7018c21STomi Valkeinen 		goto err_no_irq;
207f7018c21STomi Valkeinen 	}
208f7018c21STomi Valkeinen 
209f7018c21STomi Valkeinen 	width = readl(fb->reg_base + FB_GET_WIDTH);
210f7018c21STomi Valkeinen 	height = readl(fb->reg_base + FB_GET_HEIGHT);
211f7018c21STomi Valkeinen 
212f7018c21STomi Valkeinen 	fb->fb.fbops		= &goldfish_fb_ops;
213f7018c21STomi Valkeinen 	fb->fb.pseudo_palette	= fb->cmap;
214f7018c21STomi Valkeinen 	fb->fb.fix.type		= FB_TYPE_PACKED_PIXELS;
215f7018c21STomi Valkeinen 	fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
216f7018c21STomi Valkeinen 	fb->fb.fix.line_length = width * 2;
217f7018c21STomi Valkeinen 	fb->fb.fix.accel	= FB_ACCEL_NONE;
218f7018c21STomi Valkeinen 	fb->fb.fix.ypanstep = 1;
219f7018c21STomi Valkeinen 
220f7018c21STomi Valkeinen 	fb->fb.var.xres		= width;
221f7018c21STomi Valkeinen 	fb->fb.var.yres		= height;
222f7018c21STomi Valkeinen 	fb->fb.var.xres_virtual	= width;
223f7018c21STomi Valkeinen 	fb->fb.var.yres_virtual	= height * 2;
224f7018c21STomi Valkeinen 	fb->fb.var.bits_per_pixel = 16;
225f7018c21STomi Valkeinen 	fb->fb.var.activate	= FB_ACTIVATE_NOW;
226f7018c21STomi Valkeinen 	fb->fb.var.height	= readl(fb->reg_base + FB_GET_PHYS_HEIGHT);
227f7018c21STomi Valkeinen 	fb->fb.var.width	= readl(fb->reg_base + FB_GET_PHYS_WIDTH);
228ace6033eSChristoffer Dall 	fb->fb.var.pixclock	= 0;
229f7018c21STomi Valkeinen 
230f7018c21STomi Valkeinen 	fb->fb.var.red.offset = 11;
231f7018c21STomi Valkeinen 	fb->fb.var.red.length = 5;
232f7018c21STomi Valkeinen 	fb->fb.var.green.offset = 5;
233f7018c21STomi Valkeinen 	fb->fb.var.green.length = 6;
234f7018c21STomi Valkeinen 	fb->fb.var.blue.offset = 0;
235f7018c21STomi Valkeinen 	fb->fb.var.blue.length = 5;
236f7018c21STomi Valkeinen 
237f7018c21STomi Valkeinen 	framesize = width * height * 2 * 2;
238f7018c21STomi Valkeinen 	fb->fb.screen_base = (char __force __iomem *)dma_alloc_coherent(
239f7018c21STomi Valkeinen 						&pdev->dev, framesize,
240f7018c21STomi Valkeinen 						&fbpaddr, GFP_KERNEL);
241f7018c21STomi Valkeinen 	pr_debug("allocating frame buffer %d * %d, got %p\n",
242f7018c21STomi Valkeinen 					width, height, fb->fb.screen_base);
243f7018c21STomi Valkeinen 	if (fb->fb.screen_base == NULL) {
244f7018c21STomi Valkeinen 		ret = -ENOMEM;
245f7018c21STomi Valkeinen 		goto err_alloc_screen_base_failed;
246f7018c21STomi Valkeinen 	}
247f7018c21STomi Valkeinen 	fb->fb.fix.smem_start = fbpaddr;
248f7018c21STomi Valkeinen 	fb->fb.fix.smem_len = framesize;
249f7018c21STomi Valkeinen 
250f7018c21STomi Valkeinen 	ret = fb_set_var(&fb->fb, &fb->fb.var);
251f7018c21STomi Valkeinen 	if (ret)
252f7018c21STomi Valkeinen 		goto err_fb_set_var_failed;
253f7018c21STomi Valkeinen 
254f7018c21STomi Valkeinen 	ret = request_irq(fb->irq, goldfish_fb_interrupt, IRQF_SHARED,
255f7018c21STomi Valkeinen 							pdev->name, fb);
256f7018c21STomi Valkeinen 	if (ret)
257f7018c21STomi Valkeinen 		goto err_request_irq_failed;
258f7018c21STomi Valkeinen 
259f7018c21STomi Valkeinen 	writel(FB_INT_BASE_UPDATE_DONE, fb->reg_base + FB_INT_ENABLE);
260f7018c21STomi Valkeinen 	goldfish_fb_pan_display(&fb->fb.var, &fb->fb); /* updates base */
261f7018c21STomi Valkeinen 
262f7018c21STomi Valkeinen 	ret = register_framebuffer(&fb->fb);
263f7018c21STomi Valkeinen 	if (ret)
264f7018c21STomi Valkeinen 		goto err_register_framebuffer_failed;
265f7018c21STomi Valkeinen 	return 0;
266f7018c21STomi Valkeinen 
267f7018c21STomi Valkeinen err_register_framebuffer_failed:
268f7018c21STomi Valkeinen 	free_irq(fb->irq, fb);
269f7018c21STomi Valkeinen err_request_irq_failed:
270f7018c21STomi Valkeinen err_fb_set_var_failed:
271f7018c21STomi Valkeinen 	dma_free_coherent(&pdev->dev, framesize,
272f7018c21STomi Valkeinen 				(void *)fb->fb.screen_base,
273f7018c21STomi Valkeinen 				fb->fb.fix.smem_start);
274f7018c21STomi Valkeinen err_alloc_screen_base_failed:
275f7018c21STomi Valkeinen err_no_irq:
276f7018c21STomi Valkeinen 	iounmap(fb->reg_base);
277f7018c21STomi Valkeinen err_no_io_base:
278f7018c21STomi Valkeinen 	kfree(fb);
279f7018c21STomi Valkeinen err_fb_alloc_failed:
280f7018c21STomi Valkeinen 	return ret;
281f7018c21STomi Valkeinen }
282f7018c21STomi Valkeinen 
goldfish_fb_remove(struct platform_device * pdev)283ecab1e9aSUwe Kleine-König static void goldfish_fb_remove(struct platform_device *pdev)
284f7018c21STomi Valkeinen {
285f7018c21STomi Valkeinen 	size_t framesize;
286f7018c21STomi Valkeinen 	struct goldfish_fb *fb = platform_get_drvdata(pdev);
287f7018c21STomi Valkeinen 
288f7018c21STomi Valkeinen 	framesize = fb->fb.var.xres_virtual * fb->fb.var.yres_virtual * 2;
289f7018c21STomi Valkeinen 	unregister_framebuffer(&fb->fb);
290f7018c21STomi Valkeinen 	free_irq(fb->irq, fb);
291f7018c21STomi Valkeinen 
292f7018c21STomi Valkeinen 	dma_free_coherent(&pdev->dev, framesize, (void *)fb->fb.screen_base,
293f7018c21STomi Valkeinen 						fb->fb.fix.smem_start);
294f7018c21STomi Valkeinen 	iounmap(fb->reg_base);
2955958fde7SAnton Vasilyev 	kfree(fb);
296f7018c21STomi Valkeinen }
297f7018c21STomi Valkeinen 
29831ff6b2aSAleksandar Markovic static const struct of_device_id goldfish_fb_of_match[] = {
29931ff6b2aSAleksandar Markovic 	{ .compatible = "google,goldfish-fb", },
30031ff6b2aSAleksandar Markovic 	{},
30131ff6b2aSAleksandar Markovic };
30231ff6b2aSAleksandar Markovic MODULE_DEVICE_TABLE(of, goldfish_fb_of_match);
303f7018c21STomi Valkeinen 
30454f57264SSam Ravnborg #ifdef CONFIG_ACPI
3051ef4e117SYu Ning static const struct acpi_device_id goldfish_fb_acpi_match[] = {
3061ef4e117SYu Ning 	{ "GFSH0004", 0 },
3071ef4e117SYu Ning 	{ },
3081ef4e117SYu Ning };
3091ef4e117SYu Ning MODULE_DEVICE_TABLE(acpi, goldfish_fb_acpi_match);
31054f57264SSam Ravnborg #endif
3111ef4e117SYu Ning 
312f7018c21STomi Valkeinen static struct platform_driver goldfish_fb_driver = {
313f7018c21STomi Valkeinen 	.probe		= goldfish_fb_probe,
314ecab1e9aSUwe Kleine-König 	.remove_new	= goldfish_fb_remove,
315f7018c21STomi Valkeinen 	.driver = {
31631ff6b2aSAleksandar Markovic 		.name = "goldfish_fb",
31731ff6b2aSAleksandar Markovic 		.of_match_table = goldfish_fb_of_match,
3181ef4e117SYu Ning 		.acpi_match_table = ACPI_PTR(goldfish_fb_acpi_match),
319f7018c21STomi Valkeinen 	}
320f7018c21STomi Valkeinen };
321f7018c21STomi Valkeinen 
322f7018c21STomi Valkeinen module_platform_driver(goldfish_fb_driver);
323f7018c21STomi Valkeinen 
324*e4d52c0eSJeff Johnson MODULE_DESCRIPTION("Goldfish Virtual Platform Framebuffer driver");
325f7018c21STomi Valkeinen MODULE_LICENSE("GPL v2");
326