xref: /linux/drivers/media/test-drivers/vivid/vivid-osd.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-osd.c - osd support for testing overlays.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/font.h>
15 #include <linux/mutex.h>
16 #include <linux/videodev2.h>
17 #include <linux/kthread.h>
18 #include <linux/freezer.h>
19 #include <linux/fb.h>
20 #include <media/videobuf2-vmalloc.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-fh.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-common.h>
27 
28 #include "vivid-core.h"
29 #include "vivid-osd.h"
30 
31 #define MAX_OSD_WIDTH  720
32 #define MAX_OSD_HEIGHT 576
33 
34 /*
35  * Order: white, yellow, cyan, green, magenta, red, blue, black,
36  * and same again with the alpha bit set (if any)
37  */
38 static const u16 rgb555[16] = {
39 	0x7fff, 0x7fe0, 0x03ff, 0x03e0, 0x7c1f, 0x7c00, 0x001f, 0x0000,
40 	0xffff, 0xffe0, 0x83ff, 0x83e0, 0xfc1f, 0xfc00, 0x801f, 0x8000
41 };
42 
43 static const u16 rgb565[16] = {
44 	0xffff, 0xffe0, 0x07ff, 0x07e0, 0xf81f, 0xf800, 0x001f, 0x0000,
45 	0xffff, 0xffe0, 0x07ff, 0x07e0, 0xf81f, 0xf800, 0x001f, 0x0000
46 };
47 
vivid_clear_fb(struct vivid_dev * dev)48 void vivid_clear_fb(struct vivid_dev *dev)
49 {
50 	void *p = dev->video_vbase;
51 	const u16 *rgb = rgb555;
52 	unsigned x, y;
53 
54 	if (dev->fb_defined.green.length == 6)
55 		rgb = rgb565;
56 
57 	for (y = 0; y < dev->display_height; y++) {
58 		u16 *d = p;
59 
60 		for (x = 0; x < dev->display_width; x++)
61 			d[x] = rgb[(y / 16 + x / 16) % 16];
62 		p += dev->display_byte_stride;
63 	}
64 }
65 
66 /* --------------------------------------------------------------------- */
67 
vivid_fb_ioctl(struct fb_info * info,unsigned cmd,unsigned long arg)68 static int vivid_fb_ioctl(struct fb_info *info, unsigned cmd, unsigned long arg)
69 {
70 	struct vivid_dev *dev = (struct vivid_dev *)info->par;
71 
72 	switch (cmd) {
73 	case FBIOGET_VBLANK: {
74 		struct fb_vblank vblank;
75 
76 		memset(&vblank, 0, sizeof(vblank));
77 		vblank.flags = FB_VBLANK_HAVE_COUNT | FB_VBLANK_HAVE_VCOUNT |
78 			FB_VBLANK_HAVE_VSYNC;
79 		vblank.count = 0;
80 		vblank.vcount = 0;
81 		vblank.hcount = 0;
82 		if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
83 			return -EFAULT;
84 		return 0;
85 	}
86 
87 	default:
88 		dprintk(dev, 1, "Unknown ioctl %08x\n", cmd);
89 		return -EINVAL;
90 	}
91 	return 0;
92 }
93 
94 /* Framebuffer device handling */
95 
vivid_fb_set_var(struct vivid_dev * dev,struct fb_var_screeninfo * var)96 static int vivid_fb_set_var(struct vivid_dev *dev, struct fb_var_screeninfo *var)
97 {
98 	dprintk(dev, 1, "vivid_fb_set_var\n");
99 
100 	if (var->bits_per_pixel != 16) {
101 		dprintk(dev, 1, "vivid_fb_set_var - Invalid bpp\n");
102 		return -EINVAL;
103 	}
104 	dev->display_byte_stride = var->xres * dev->bytes_per_pixel;
105 
106 	return 0;
107 }
108 
vivid_fb_get_fix(struct vivid_dev * dev,struct fb_fix_screeninfo * fix)109 static int vivid_fb_get_fix(struct vivid_dev *dev, struct fb_fix_screeninfo *fix)
110 {
111 	dprintk(dev, 1, "vivid_fb_get_fix\n");
112 	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
113 	strscpy(fix->id, "vioverlay fb", sizeof(fix->id));
114 	fix->smem_start = dev->video_pbase;
115 	fix->smem_len = dev->video_buffer_size;
116 	fix->type = FB_TYPE_PACKED_PIXELS;
117 	fix->visual = FB_VISUAL_TRUECOLOR;
118 	fix->xpanstep = 1;
119 	fix->ypanstep = 1;
120 	fix->ywrapstep = 0;
121 	fix->line_length = dev->display_byte_stride;
122 	fix->accel = FB_ACCEL_NONE;
123 	return 0;
124 }
125 
126 /* Check the requested display mode, returning -EINVAL if we can't
127    handle it. */
128 
_vivid_fb_check_var(struct fb_var_screeninfo * var,struct vivid_dev * dev)129 static int _vivid_fb_check_var(struct fb_var_screeninfo *var, struct vivid_dev *dev)
130 {
131 	dprintk(dev, 1, "vivid_fb_check_var\n");
132 
133 	var->bits_per_pixel = 16;
134 	if (var->green.length == 5) {
135 		var->red.offset = 10;
136 		var->red.length = 5;
137 		var->green.offset = 5;
138 		var->green.length = 5;
139 		var->blue.offset = 0;
140 		var->blue.length = 5;
141 		var->transp.offset = 15;
142 		var->transp.length = 1;
143 	} else {
144 		var->red.offset = 11;
145 		var->red.length = 5;
146 		var->green.offset = 5;
147 		var->green.length = 6;
148 		var->blue.offset = 0;
149 		var->blue.length = 5;
150 		var->transp.offset = 0;
151 		var->transp.length = 0;
152 	}
153 	var->xoffset = var->yoffset = 0;
154 	var->left_margin = var->upper_margin = 0;
155 	var->nonstd = 0;
156 
157 	var->vmode &= ~FB_VMODE_MASK;
158 	var->vmode |= FB_VMODE_NONINTERLACED;
159 
160 	/* Dummy values */
161 	var->hsync_len = 24;
162 	var->vsync_len = 2;
163 	var->pixclock = 84316;
164 	var->right_margin = 776;
165 	var->lower_margin = 591;
166 	return 0;
167 }
168 
vivid_fb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)169 static int vivid_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
170 {
171 	struct vivid_dev *dev = (struct vivid_dev *) info->par;
172 
173 	dprintk(dev, 1, "vivid_fb_check_var\n");
174 	return _vivid_fb_check_var(var, dev);
175 }
176 
vivid_fb_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)177 static int vivid_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
178 {
179 	return 0;
180 }
181 
vivid_fb_set_par(struct fb_info * info)182 static int vivid_fb_set_par(struct fb_info *info)
183 {
184 	int rc = 0;
185 	struct vivid_dev *dev = (struct vivid_dev *) info->par;
186 
187 	dprintk(dev, 1, "vivid_fb_set_par\n");
188 
189 	rc = vivid_fb_set_var(dev, &info->var);
190 	vivid_fb_get_fix(dev, &info->fix);
191 	return rc;
192 }
193 
vivid_fb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)194 static int vivid_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
195 				unsigned blue, unsigned transp,
196 				struct fb_info *info)
197 {
198 	u32 color, *palette;
199 
200 	if (regno >= info->cmap.len)
201 		return -EINVAL;
202 
203 	color = ((transp & 0xFF00) << 16) | ((red & 0xFF00) << 8) |
204 		 (green & 0xFF00) | ((blue & 0xFF00) >> 8);
205 	if (regno >= 16)
206 		return -EINVAL;
207 
208 	palette = info->pseudo_palette;
209 	if (info->var.bits_per_pixel == 16) {
210 		switch (info->var.green.length) {
211 		case 6:
212 			color = (red & 0xf800) |
213 				((green & 0xfc00) >> 5) |
214 				((blue & 0xf800) >> 11);
215 			break;
216 		case 5:
217 			color = ((red & 0xf800) >> 1) |
218 				((green & 0xf800) >> 6) |
219 				((blue & 0xf800) >> 11) |
220 				(transp ? 0x8000 : 0);
221 			break;
222 		}
223 	}
224 	palette[regno] = color;
225 	return 0;
226 }
227 
228 /* We don't really support blanking. All this does is enable or
229    disable the OSD. */
vivid_fb_blank(int blank_mode,struct fb_info * info)230 static int vivid_fb_blank(int blank_mode, struct fb_info *info)
231 {
232 	struct vivid_dev *dev = (struct vivid_dev *)info->par;
233 
234 	dprintk(dev, 1, "Set blanking mode : %d\n", blank_mode);
235 	switch (blank_mode) {
236 	case FB_BLANK_UNBLANK:
237 		break;
238 	case FB_BLANK_NORMAL:
239 	case FB_BLANK_HSYNC_SUSPEND:
240 	case FB_BLANK_VSYNC_SUSPEND:
241 	case FB_BLANK_POWERDOWN:
242 		break;
243 	}
244 	return 0;
245 }
246 
247 static const struct fb_ops vivid_fb_ops = {
248 	.owner = THIS_MODULE,
249 	FB_DEFAULT_IOMEM_OPS,
250 	.fb_check_var   = vivid_fb_check_var,
251 	.fb_set_par     = vivid_fb_set_par,
252 	.fb_setcolreg   = vivid_fb_setcolreg,
253 	.fb_cursor      = NULL,
254 	.fb_ioctl       = vivid_fb_ioctl,
255 	.fb_pan_display = vivid_fb_pan_display,
256 	.fb_blank       = vivid_fb_blank,
257 };
258 
259 /* Initialization */
260 
261 
262 /* Setup our initial video mode */
vivid_fb_init_vidmode(struct vivid_dev * dev)263 static int vivid_fb_init_vidmode(struct vivid_dev *dev)
264 {
265 	struct v4l2_rect start_window;
266 
267 	/* Color mode */
268 
269 	dev->bits_per_pixel = 16;
270 	dev->bytes_per_pixel = dev->bits_per_pixel / 8;
271 
272 	start_window.width = MAX_OSD_WIDTH;
273 	start_window.left = 0;
274 
275 	dev->display_byte_stride = start_window.width * dev->bytes_per_pixel;
276 
277 	/* Vertical size & position */
278 
279 	start_window.height = MAX_OSD_HEIGHT;
280 	start_window.top = 0;
281 
282 	dev->display_width = start_window.width;
283 	dev->display_height = start_window.height;
284 
285 	/* Generate a valid fb_var_screeninfo */
286 
287 	dev->fb_defined.xres = dev->display_width;
288 	dev->fb_defined.yres = dev->display_height;
289 	dev->fb_defined.xres_virtual = dev->display_width;
290 	dev->fb_defined.yres_virtual = dev->display_height;
291 	dev->fb_defined.bits_per_pixel = dev->bits_per_pixel;
292 	dev->fb_defined.vmode = FB_VMODE_NONINTERLACED;
293 	dev->fb_defined.left_margin = start_window.left + 1;
294 	dev->fb_defined.upper_margin = start_window.top + 1;
295 	dev->fb_defined.accel_flags = FB_ACCEL_NONE;
296 	dev->fb_defined.nonstd = 0;
297 	/* set default to 1:5:5:5 */
298 	dev->fb_defined.green.length = 5;
299 
300 	/* We've filled in the most data, let the usual mode check
301 	   routine fill in the rest. */
302 	_vivid_fb_check_var(&dev->fb_defined, dev);
303 
304 	/* Generate valid fb_fix_screeninfo */
305 
306 	vivid_fb_get_fix(dev, &dev->fb_fix);
307 
308 	/* Generate valid fb_info */
309 
310 	dev->fb_info.node = -1;
311 	dev->fb_info.par = dev;
312 	dev->fb_info.var = dev->fb_defined;
313 	dev->fb_info.fix = dev->fb_fix;
314 	dev->fb_info.screen_base = (u8 __iomem *)dev->video_vbase;
315 	dev->fb_info.fbops = &vivid_fb_ops;
316 
317 	/* Supply some monitor specs. Bogus values will do for now */
318 	dev->fb_info.monspecs.hfmin = 8000;
319 	dev->fb_info.monspecs.hfmax = 70000;
320 	dev->fb_info.monspecs.vfmin = 10;
321 	dev->fb_info.monspecs.vfmax = 100;
322 
323 	/* Allocate color map */
324 	if (fb_alloc_cmap(&dev->fb_info.cmap, 256, 1)) {
325 		pr_err("abort, unable to alloc cmap\n");
326 		return -ENOMEM;
327 	}
328 
329 	/* Allocate the pseudo palette */
330 	dev->fb_info.pseudo_palette = kmalloc_array(16, sizeof(u32), GFP_KERNEL);
331 
332 	return dev->fb_info.pseudo_palette ? 0 : -ENOMEM;
333 }
334 
335 /* Release any memory we've grabbed */
vivid_fb_release_buffers(struct vivid_dev * dev)336 void vivid_fb_release_buffers(struct vivid_dev *dev)
337 {
338 	if (dev->video_vbase == NULL)
339 		return;
340 
341 	/* Release cmap */
342 	if (dev->fb_info.cmap.len)
343 		fb_dealloc_cmap(&dev->fb_info.cmap);
344 
345 	/* Release pseudo palette */
346 	kfree(dev->fb_info.pseudo_palette);
347 	kfree(dev->video_vbase);
348 }
349 
350 /* Initialize the specified card */
351 
vivid_fb_init(struct vivid_dev * dev)352 int vivid_fb_init(struct vivid_dev *dev)
353 {
354 	int ret;
355 
356 	dev->video_buffer_size = MAX_OSD_HEIGHT * MAX_OSD_WIDTH * 2;
357 	dev->video_vbase = kzalloc(dev->video_buffer_size, GFP_KERNEL);
358 	if (dev->video_vbase == NULL)
359 		return -ENOMEM;
360 	dev->video_pbase = virt_to_phys(dev->video_vbase);
361 
362 	pr_info("Framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
363 			dev->video_pbase, dev->video_vbase,
364 			dev->video_buffer_size / 1024);
365 
366 	/* Set the startup video mode information */
367 	ret = vivid_fb_init_vidmode(dev);
368 	if (ret) {
369 		vivid_fb_release_buffers(dev);
370 		return ret;
371 	}
372 
373 	vivid_clear_fb(dev);
374 
375 	/* Register the framebuffer */
376 	if (register_framebuffer(&dev->fb_info) < 0) {
377 		vivid_fb_release_buffers(dev);
378 		return -EINVAL;
379 	}
380 
381 	/* Set the card to the requested mode */
382 	vivid_fb_set_par(&dev->fb_info);
383 	return 0;
384 
385 }
386