• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Google, Inc.
3  * Copyright (C) 2012 Intel, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/mm.h>
24 #include <linux/fb.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/ioport.h>
28 #include <linux/platform_device.h>
29 #include <linux/acpi.h>
30 
31 enum {
32 	FB_GET_WIDTH        = 0x00,
33 	FB_GET_HEIGHT       = 0x04,
34 	FB_INT_STATUS       = 0x08,
35 	FB_INT_ENABLE       = 0x0c,
36 	FB_SET_BASE         = 0x10,
37 	FB_SET_ROTATION     = 0x14,
38 	FB_SET_BLANK        = 0x18,
39 	FB_GET_PHYS_WIDTH   = 0x1c,
40 	FB_GET_PHYS_HEIGHT  = 0x20,
41 	FB_GET_FORMAT       = 0x24,
42 
43 	FB_INT_VSYNC             = 1U << 0,
44 	FB_INT_BASE_UPDATE_DONE  = 1U << 1
45 };
46 
47 /* These values *must* match the platform definitions found under
48  * <system/graphics.h>
49  */
50 enum {
51 	HAL_PIXEL_FORMAT_RGBA_8888          = 1,
52 	HAL_PIXEL_FORMAT_RGBX_8888          = 2,
53 	HAL_PIXEL_FORMAT_RGB_888            = 3,
54 	HAL_PIXEL_FORMAT_RGB_565            = 4,
55 	HAL_PIXEL_FORMAT_BGRA_8888          = 5,
56 };
57 
58 struct framebuffer_config {
59 	u8 bytes_per_pixel;
60 	u8 red_offset;
61 	u8 red_length;
62 	u8 green_offset;
63 	u8 green_length;
64 	u8 blue_offset;
65 	u8 blue_length;
66 	u8 transp_offset;
67 	u8 transp_length;
68 };
69 
get_fb_config_from_format(int format)70 static const struct framebuffer_config* get_fb_config_from_format(int format) {
71 	static const struct framebuffer_config fb_configs[] = {
72 		{ 0, 0,  0, 0, 0, 0,  0, 0,  0 }, /* Invalid, assume RGB_565 */
73 		{ 4, 0,  8, 8, 8, 16, 8, 24, 8 }, /* HAL_PIXEL_FORMAT_RGBA_8888 */
74 		{ 4, 0,  8, 8, 8, 16, 8, 0,  0 }, /* HAL_PIXEL_FORMAT_RGBX_8888 */
75 		{ 3, 0,  8, 8, 8, 16, 8, 0,  0 }, /* HAL_PIXEL_FORMAT_RGB_888 */
76 		{ 2, 11, 5, 5, 6, 0,  5, 0,  0 }, /* HAL_PIXEL_FORMAT_RGB_565 */
77 		{ 4, 16, 8, 8, 8, 0,  8, 24, 8 }, /* HAL_PIXEL_FORMAT_BGRA_8888 */
78 	};
79 
80 	if (format > 0 &&
81 		format < sizeof(fb_configs) / sizeof(struct framebuffer_config)) {
82 		return &fb_configs[format];
83 	}
84 
85 	return &fb_configs[HAL_PIXEL_FORMAT_RGB_565]; /* legacy default */
86 }
87 
88 struct goldfish_fb {
89 	void __iomem *reg_base;
90 	int irq;
91 	spinlock_t lock;
92 	wait_queue_head_t wait;
93 	int base_update_count;
94 	int rotation;
95 	struct fb_info fb;
96 	u32 cmap[16];
97 };
98 
goldfish_fb_interrupt(int irq,void * dev_id)99 static irqreturn_t goldfish_fb_interrupt(int irq, void *dev_id)
100 {
101 	unsigned long irq_flags;
102 	struct goldfish_fb *fb = dev_id;
103 	u32 status;
104 
105 	spin_lock_irqsave(&fb->lock, irq_flags);
106 	status = readl(fb->reg_base + FB_INT_STATUS);
107 	if (status & FB_INT_BASE_UPDATE_DONE) {
108 		fb->base_update_count++;
109 		wake_up(&fb->wait);
110 	}
111 	spin_unlock_irqrestore(&fb->lock, irq_flags);
112 	return status ? IRQ_HANDLED : IRQ_NONE;
113 }
114 
convert_bitfield(int val,struct fb_bitfield * bf)115 static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
116 {
117 	unsigned int mask = (1 << bf->length) - 1;
118 
119 	return (val >> (16 - bf->length) & mask) << bf->offset;
120 }
121 
122 static int
goldfish_fb_setcolreg(unsigned int regno,unsigned int red,unsigned int green,unsigned int blue,unsigned int transp,struct fb_info * info)123 goldfish_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
124 		 unsigned int blue, unsigned int transp, struct fb_info *info)
125 {
126 	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
127 
128 	if (regno < 16) {
129 		fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
130 				  convert_bitfield(blue, &fb->fb.var.blue) |
131 				  convert_bitfield(green, &fb->fb.var.green) |
132 				  convert_bitfield(red, &fb->fb.var.red);
133 		return 0;
134 	} else {
135 		return 1;
136 	}
137 }
138 
goldfish_fb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)139 static int goldfish_fb_check_var(struct fb_var_screeninfo *var,
140 							struct fb_info *info)
141 {
142 	if ((var->rotate & 1) != (info->var.rotate & 1)) {
143 		if ((var->xres != info->var.yres) ||
144 				(var->yres != info->var.xres) ||
145 				(var->xres_virtual != info->var.yres) ||
146 				(var->yres_virtual > info->var.xres * 2) ||
147 				(var->yres_virtual < info->var.xres)) {
148 			return -EINVAL;
149 		}
150 	} else {
151 		if ((var->xres != info->var.xres) ||
152 		   (var->yres != info->var.yres) ||
153 		   (var->xres_virtual != info->var.xres) ||
154 		   (var->yres_virtual > info->var.yres * 2) ||
155 		   (var->yres_virtual < info->var.yres)) {
156 			return -EINVAL;
157 		}
158 	}
159 	if ((var->xoffset != info->var.xoffset) ||
160 			(var->bits_per_pixel != info->var.bits_per_pixel) ||
161 			(var->grayscale != info->var.grayscale)) {
162 		return -EINVAL;
163 	}
164 	return 0;
165 }
166 
goldfish_fb_set_par(struct fb_info * info)167 static int goldfish_fb_set_par(struct fb_info *info)
168 {
169 	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
170 	if (fb->rotation != fb->fb.var.rotate) {
171 		info->fix.line_length = info->var.xres *
172 			(fb->fb.var.bits_per_pixel / 8);
173 		fb->rotation = fb->fb.var.rotate;
174 		writel(fb->rotation, fb->reg_base + FB_SET_ROTATION);
175 	}
176 	return 0;
177 }
178 
179 
goldfish_fb_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)180 static int goldfish_fb_pan_display(struct fb_var_screeninfo *var,
181 							struct fb_info *info)
182 {
183 	unsigned long irq_flags;
184 	int base_update_count;
185 	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
186 
187 	spin_lock_irqsave(&fb->lock, irq_flags);
188 	base_update_count = fb->base_update_count;
189 	writel(fb->fb.fix.smem_start +
190 			fb->fb.var.xres * (fb->fb.var.bits_per_pixel / 8) * var->yoffset,
191 			fb->reg_base + FB_SET_BASE);
192 	spin_unlock_irqrestore(&fb->lock, irq_flags);
193 	wait_event_timeout(fb->wait,
194 			fb->base_update_count != base_update_count, HZ / 15);
195 	if (fb->base_update_count == base_update_count)
196 		pr_err("goldfish_fb_pan_display: timeout waiting for base update\n");
197 	return 0;
198 }
199 
goldfish_fb_blank(int blank,struct fb_info * info)200 static int goldfish_fb_blank(int blank, struct fb_info *info)
201 {
202 	struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
203 	switch (blank) {
204 	case FB_BLANK_NORMAL:
205 		writel(1, fb->reg_base + FB_SET_BLANK);
206 		break;
207 	case FB_BLANK_UNBLANK:
208 		writel(0, fb->reg_base + FB_SET_BLANK);
209 		break;
210 	}
211 	return 0;
212 }
213 
214 static struct fb_ops goldfish_fb_ops = {
215 	.owner          = THIS_MODULE,
216 	.fb_check_var   = goldfish_fb_check_var,
217 	.fb_set_par     = goldfish_fb_set_par,
218 	.fb_setcolreg   = goldfish_fb_setcolreg,
219 	.fb_pan_display = goldfish_fb_pan_display,
220 	.fb_blank	= goldfish_fb_blank,
221 	.fb_fillrect    = cfb_fillrect,
222 	.fb_copyarea    = cfb_copyarea,
223 	.fb_imageblit   = cfb_imageblit,
224 };
225 
226 
goldfish_fb_probe(struct platform_device * pdev)227 static int goldfish_fb_probe(struct platform_device *pdev)
228 {
229 	int ret;
230 	struct resource *r;
231 	struct goldfish_fb *fb;
232 	size_t framesize;
233 	u32 width, height, format;
234 	int bytes_per_pixel;
235 	dma_addr_t fbpaddr;
236 	const struct framebuffer_config* fb_config;
237 
238 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
239 	if (fb == NULL) {
240 		ret = -ENOMEM;
241 		goto err_fb_alloc_failed;
242 	}
243 	spin_lock_init(&fb->lock);
244 	init_waitqueue_head(&fb->wait);
245 	platform_set_drvdata(pdev, fb);
246 
247 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
248 	if (r == NULL) {
249 		ret = -ENODEV;
250 		goto err_no_io_base;
251 	}
252 	fb->reg_base = ioremap(r->start, PAGE_SIZE);
253 	if (fb->reg_base == NULL) {
254 		ret = -ENOMEM;
255 		goto err_no_io_base;
256 	}
257 
258 	fb->irq = platform_get_irq(pdev, 0);
259 	if (fb->irq <= 0) {
260 		ret = -ENODEV;
261 		goto err_no_irq;
262 	}
263 
264 	width = readl(fb->reg_base + FB_GET_WIDTH);
265 	height = readl(fb->reg_base + FB_GET_HEIGHT);
266 	format = readl(fb->reg_base + FB_GET_FORMAT);
267 	fb_config = get_fb_config_from_format(format);
268 	if (!fb_config) {
269 		ret = -EINVAL;
270 		goto err_no_irq;
271 	}
272 	bytes_per_pixel = fb_config->bytes_per_pixel;
273 
274 	fb->fb.fbops		= &goldfish_fb_ops;
275 	fb->fb.flags		= FBINFO_FLAG_DEFAULT;
276 	fb->fb.pseudo_palette	= fb->cmap;
277 	fb->fb.fix.type		= FB_TYPE_PACKED_PIXELS;
278 	fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
279 	fb->fb.fix.line_length = width * bytes_per_pixel;
280 	fb->fb.fix.accel	= FB_ACCEL_NONE;
281 	fb->fb.fix.ypanstep = 1;
282 
283 	fb->fb.var.xres		= width;
284 	fb->fb.var.yres		= height;
285 	fb->fb.var.xres_virtual	= width;
286 	fb->fb.var.yres_virtual	= height * 2;
287 	fb->fb.var.bits_per_pixel = bytes_per_pixel * 8;
288 	fb->fb.var.activate	= FB_ACTIVATE_NOW;
289 	fb->fb.var.height	= readl(fb->reg_base + FB_GET_PHYS_HEIGHT);
290 	fb->fb.var.width	= readl(fb->reg_base + FB_GET_PHYS_WIDTH);
291 	fb->fb.var.pixclock	= 0;
292 
293 	fb->fb.var.red.offset = fb_config->red_offset;
294 	fb->fb.var.red.length = fb_config->red_length;
295 	fb->fb.var.green.offset = fb_config->green_offset;
296 	fb->fb.var.green.length = fb_config->green_length;
297 	fb->fb.var.blue.offset = fb_config->blue_offset;
298 	fb->fb.var.blue.length = fb_config->blue_length;
299 	fb->fb.var.transp.offset = fb_config->transp_offset;
300 	fb->fb.var.transp.length = fb_config->transp_length;
301 
302 	framesize = width * height * 2 * bytes_per_pixel;
303 	fb->fb.screen_base = (char __force __iomem *)dma_alloc_coherent(
304 						&pdev->dev, framesize,
305 						&fbpaddr, GFP_KERNEL);
306 	pr_debug("allocating frame buffer %d * %d, got %p\n",
307 					width, height, fb->fb.screen_base);
308 	if (fb->fb.screen_base == NULL) {
309 		ret = -ENOMEM;
310 		goto err_alloc_screen_base_failed;
311 	}
312 	fb->fb.fix.smem_start = fbpaddr;
313 	fb->fb.fix.smem_len = framesize;
314 
315 	ret = fb_set_var(&fb->fb, &fb->fb.var);
316 	if (ret)
317 		goto err_fb_set_var_failed;
318 
319 	ret = request_irq(fb->irq, goldfish_fb_interrupt, IRQF_SHARED,
320 							pdev->name, fb);
321 	if (ret)
322 		goto err_request_irq_failed;
323 
324 	writel(FB_INT_BASE_UPDATE_DONE, fb->reg_base + FB_INT_ENABLE);
325 	goldfish_fb_pan_display(&fb->fb.var, &fb->fb); /* updates base */
326 
327 	ret = register_framebuffer(&fb->fb);
328 	if (ret)
329 		goto err_register_framebuffer_failed;
330 	return 0;
331 
332 err_register_framebuffer_failed:
333 	free_irq(fb->irq, fb);
334 err_request_irq_failed:
335 err_fb_set_var_failed:
336 	dma_free_coherent(&pdev->dev, framesize,
337 				(void *)fb->fb.screen_base,
338 				fb->fb.fix.smem_start);
339 err_alloc_screen_base_failed:
340 err_no_irq:
341 	iounmap(fb->reg_base);
342 err_no_io_base:
343 	kfree(fb);
344 err_fb_alloc_failed:
345 	return ret;
346 }
347 
goldfish_fb_remove(struct platform_device * pdev)348 static int goldfish_fb_remove(struct platform_device *pdev)
349 {
350 	size_t framesize;
351 	struct goldfish_fb *fb = platform_get_drvdata(pdev);
352 
353 	framesize = fb->fb.var.xres_virtual * fb->fb.var.yres_virtual *
354 		(fb->fb.var.bits_per_pixel / 8);
355 	unregister_framebuffer(&fb->fb);
356 	free_irq(fb->irq, fb);
357 
358 	dma_free_coherent(&pdev->dev, framesize, (void *)fb->fb.screen_base,
359 						fb->fb.fix.smem_start);
360 	iounmap(fb->reg_base);
361 	return 0;
362 }
363 
364 static const struct of_device_id goldfish_fb_of_match[] = {
365 	{ .compatible = "generic,goldfish-fb", },
366 	{},
367 };
368 MODULE_DEVICE_TABLE(of, goldfish_fb_of_match);
369 
370 static const struct acpi_device_id goldfish_fb_acpi_match[] = {
371 	{ "GFSH0004", 0 },
372 	{ },
373 };
374 MODULE_DEVICE_TABLE(acpi, goldfish_fb_acpi_match);
375 
376 static struct platform_driver goldfish_fb_driver = {
377 	.probe		= goldfish_fb_probe,
378 	.remove		= goldfish_fb_remove,
379 	.driver = {
380 		.name = "goldfish_fb",
381 		.owner = THIS_MODULE,
382 		.of_match_table = goldfish_fb_of_match,
383 		.acpi_match_table = ACPI_PTR(goldfish_fb_acpi_match),
384 	}
385 };
386 
387 module_platform_driver(goldfish_fb_driver);
388 
389 MODULE_LICENSE("GPL v2");
390