blob: 477268a2415fd06d58e47eddc5e157b4940fd8c3 [file] [log] [blame]
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001/*
2 * omap_vout.c
3 *
4 * Copyright (C) 2005-2010 Texas Instruments.
5 *
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
9 *
10 * Leveraged code from the OMAP2 camera driver
11 * Video-for-Linux (Version 2) camera capture driver for
12 * the OMAP24xx camera controller.
13 *
14 * Author: Andy Lowe (source@mvista.com)
15 *
16 * Copyright (C) 2004 MontaVista Software, Inc.
17 * Copyright (C) 2010 Texas Instruments.
18 *
19 * History:
20 * 20-APR-2006 Khasim Modified VRFB based Rotation,
21 * The image data is always read from 0 degree
22 * view and written
23 * to the virtual space of desired rotation angle
24 * 4-DEC-2006 Jian Changed to support better memory management
25 *
26 * 17-Nov-2008 Hardik Changed driver to use video_ioctl2
27 *
28 * 23-Feb-2010 Vaibhav H Modified to use new DSS2 interface
29 *
30 */
31
32#include <linux/init.h>
33#include <linux/module.h>
34#include <linux/vmalloc.h>
35#include <linux/sched.h>
36#include <linux/types.h>
37#include <linux/platform_device.h>
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -030038#include <linux/irq.h>
39#include <linux/videodev2.h>
Amber Jain72915e82011-07-07 06:03:25 -030040#include <linux/dma-mapping.h>
Gary Thomasd1ee8872011-12-01 08:51:09 -030041#include <linux/slab.h>
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -030042
Vaibhav Hiremathdd880dd2010-05-27 08:17:08 -030043#include <media/videobuf-dma-contig.h>
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -030044#include <media/v4l2-device.h>
45#include <media/v4l2-ioctl.h>
46
Tomi Valkeinen6a1c9f62012-10-08 14:52:24 +030047#include <video/omapvrfb.h>
Tomi Valkeinena0b38cc2011-05-11 14:05:07 +030048#include <video/omapdss.h>
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -030049
50#include "omap_voutlib.h"
51#include "omap_voutdef.h"
archit taneja445e2582011-06-14 03:54:47 -030052#include "omap_vout_vrfb.h"
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -030053
54MODULE_AUTHOR("Texas Instruments");
55MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
56MODULE_LICENSE("GPL");
57
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -030058/* Driver Configuration macros */
59#define VOUT_NAME "omap_vout"
60
61enum omap_vout_channels {
62 OMAP_VIDEO1,
63 OMAP_VIDEO2,
64};
65
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -030066static struct videobuf_queue_ops video_vbq_ops;
67/* Variables configurable through module params*/
68static u32 video1_numbuffers = 3;
69static u32 video2_numbuffers = 3;
70static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
71static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103072static bool vid1_static_vrfb_alloc;
73static bool vid2_static_vrfb_alloc;
74static bool debug;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -030075
76/* Module parameters */
77module_param(video1_numbuffers, uint, S_IRUGO);
78MODULE_PARM_DESC(video1_numbuffers,
79 "Number of buffers to be allocated at init time for Video1 device.");
80
81module_param(video2_numbuffers, uint, S_IRUGO);
82MODULE_PARM_DESC(video2_numbuffers,
83 "Number of buffers to be allocated at init time for Video2 device.");
84
85module_param(video1_bufsize, uint, S_IRUGO);
86MODULE_PARM_DESC(video1_bufsize,
87 "Size of the buffer to be allocated for video1 device");
88
89module_param(video2_bufsize, uint, S_IRUGO);
90MODULE_PARM_DESC(video2_bufsize,
91 "Size of the buffer to be allocated for video2 device");
92
93module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
94MODULE_PARM_DESC(vid1_static_vrfb_alloc,
95 "Static allocation of the VRFB buffer for video1 device");
96
97module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
98MODULE_PARM_DESC(vid2_static_vrfb_alloc,
99 "Static allocation of the VRFB buffer for video2 device");
100
101module_param(debug, bool, S_IRUGO);
102MODULE_PARM_DESC(debug, "Debug level (0-1)");
103
104/* list of image formats supported by OMAP2 video pipelines */
Jesper Juhl0d334f72011-07-09 18:22:17 -0300105static const struct v4l2_fmtdesc omap_formats[] = {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300106 {
107 /* Note: V4L2 defines RGB565 as:
108 *
109 * Byte 0 Byte 1
110 * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
111 *
112 * We interpret RGB565 as:
113 *
114 * Byte 0 Byte 1
115 * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
116 */
117 .description = "RGB565, le",
118 .pixelformat = V4L2_PIX_FMT_RGB565,
119 },
120 {
121 /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
122 * this for RGB24 unpack mode, the last 8 bits are ignored
123 * */
124 .description = "RGB32, le",
125 .pixelformat = V4L2_PIX_FMT_RGB32,
126 },
127 {
128 /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
129 * this for RGB24 packed mode
130 *
131 */
132 .description = "RGB24, le",
133 .pixelformat = V4L2_PIX_FMT_RGB24,
134 },
135 {
136 .description = "YUYV (YUV 4:2:2), packed",
137 .pixelformat = V4L2_PIX_FMT_YUYV,
138 },
139 {
140 .description = "UYVY, packed",
141 .pixelformat = V4L2_PIX_FMT_UYVY,
142 },
143};
144
145#define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
146
147/*
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300148 * Try format
149 */
150static int omap_vout_try_format(struct v4l2_pix_format *pix)
151{
152 int ifmt, bpp = 0;
153
154 pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
155 (u32)VID_MAX_HEIGHT);
156 pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
157
158 for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
159 if (pix->pixelformat == omap_formats[ifmt].pixelformat)
160 break;
161 }
162
163 if (ifmt == NUM_OUTPUT_FORMATS)
164 ifmt = 0;
165
166 pix->pixelformat = omap_formats[ifmt].pixelformat;
167 pix->field = V4L2_FIELD_ANY;
168 pix->priv = 0;
169
170 switch (pix->pixelformat) {
171 case V4L2_PIX_FMT_YUYV:
172 case V4L2_PIX_FMT_UYVY:
173 default:
174 pix->colorspace = V4L2_COLORSPACE_JPEG;
175 bpp = YUYV_BPP;
176 break;
177 case V4L2_PIX_FMT_RGB565:
178 case V4L2_PIX_FMT_RGB565X:
179 pix->colorspace = V4L2_COLORSPACE_SRGB;
180 bpp = RGB565_BPP;
181 break;
182 case V4L2_PIX_FMT_RGB24:
183 pix->colorspace = V4L2_COLORSPACE_SRGB;
184 bpp = RGB24_BPP;
185 break;
186 case V4L2_PIX_FMT_RGB32:
187 case V4L2_PIX_FMT_BGR32:
188 pix->colorspace = V4L2_COLORSPACE_SRGB;
189 bpp = RGB32_BPP;
190 break;
191 }
192 pix->bytesperline = pix->width * bpp;
193 pix->sizeimage = pix->bytesperline * pix->height;
194
195 return bpp;
196}
197
198/*
199 * omap_vout_uservirt_to_phys: This inline function is used to convert user
200 * space virtual address to physical address.
201 */
202static u32 omap_vout_uservirt_to_phys(u32 virtp)
203{
204 unsigned long physp = 0;
205 struct vm_area_struct *vma;
206 struct mm_struct *mm = current->mm;
207
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300208 /* For kernel direct-mapped memory, take the easy way */
Al Viro55ee64b2012-12-16 16:04:46 -0300209 if (virtp >= PAGE_OFFSET)
210 return virt_to_phys((void *) virtp);
211
212 down_read(&current->mm->mmap_sem);
213 vma = find_vma(mm, virtp);
214 if (vma && (vma->vm_flags & VM_IO) && vma->vm_pgoff) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300215 /* this will catch, kernel-allocated, mmaped-to-usermode
216 addresses */
217 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
Al Viro55ee64b2012-12-16 16:04:46 -0300218 up_read(&current->mm->mmap_sem);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300219 } else {
220 /* otherwise, use get_user_pages() for general userland pages */
221 int res, nr_pages = 1;
222 struct page *pages;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300223
224 res = get_user_pages(current, current->mm, virtp, nr_pages, 1,
225 0, &pages, NULL);
226 up_read(&current->mm->mmap_sem);
227
228 if (res == nr_pages) {
229 physp = __pa(page_address(&pages[0]) +
230 (virtp & ~PAGE_MASK));
231 } else {
232 printk(KERN_WARNING VOUT_NAME
233 "get_user_pages failed\n");
234 return 0;
235 }
236 }
237
238 return physp;
239}
240
241/*
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300242 * Free the V4L2 buffers
243 */
archit taneja445e2582011-06-14 03:54:47 -0300244void omap_vout_free_buffers(struct omap_vout_device *vout)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300245{
246 int i, numbuffers;
247
248 /* Allocate memory for the buffers */
249 numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
250 vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
251
252 for (i = 0; i < numbuffers; i++) {
253 omap_vout_free_buffer(vout->buf_virt_addr[i],
254 vout->buffer_size);
255 vout->buf_phy_addr[i] = 0;
256 vout->buf_virt_addr[i] = 0;
257 }
258}
259
260/*
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300261 * Convert V4L2 rotation to DSS rotation
262 * V4L2 understand 0, 90, 180, 270.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300263 * Convert to 0, 1, 2 and 3 respectively for DSS
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300264 */
265static int v4l2_rot_to_dss_rot(int v4l2_rotation,
266 enum dss_rotation *rotation, bool mirror)
267{
268 int ret = 0;
269
270 switch (v4l2_rotation) {
271 case 90:
272 *rotation = dss_rotation_90_degree;
273 break;
274 case 180:
275 *rotation = dss_rotation_180_degree;
276 break;
277 case 270:
278 *rotation = dss_rotation_270_degree;
279 break;
280 case 0:
281 *rotation = dss_rotation_0_degree;
282 break;
283 default:
284 ret = -EINVAL;
285 }
286 return ret;
287}
288
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300289static int omap_vout_calculate_offset(struct omap_vout_device *vout)
290{
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300291 struct omapvideo_info *ovid;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300292 struct v4l2_rect *crop = &vout->crop;
293 struct v4l2_pix_format *pix = &vout->pix;
294 int *cropped_offset = &vout->cropped_offset;
archit taneja445e2582011-06-14 03:54:47 -0300295 int ps = 2, line_length = 0;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300296
297 ovid = &vout->vid_info;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300298
archit taneja445e2582011-06-14 03:54:47 -0300299 if (ovid->rotation_type == VOUT_ROT_VRFB) {
300 omap_vout_calculate_vrfb_offset(vout);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300301 } else {
archit taneja445e2582011-06-14 03:54:47 -0300302 vout->line_length = line_length = pix->width;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300303
archit taneja445e2582011-06-14 03:54:47 -0300304 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
305 V4L2_PIX_FMT_UYVY == pix->pixelformat)
306 ps = 2;
307 else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
308 ps = 4;
309 else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
310 ps = 3;
311
312 vout->ps = ps;
313
314 *cropped_offset = (line_length * ps) *
315 crop->top + crop->left * ps;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300316 }
archit taneja445e2582011-06-14 03:54:47 -0300317
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300318 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
archit taneja445e2582011-06-14 03:54:47 -0300319 __func__, vout->cropped_offset);
320
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300321 return 0;
322}
323
324/*
325 * Convert V4L2 pixel format to DSS pixel format
326 */
Vaibhav Hiremath72fcf2a2010-04-11 10:50:23 -0300327static int video_mode_to_dss_mode(struct omap_vout_device *vout)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300328{
329 struct omap_overlay *ovl;
330 struct omapvideo_info *ovid;
331 struct v4l2_pix_format *pix = &vout->pix;
332 enum omap_color_mode mode;
333
334 ovid = &vout->vid_info;
335 ovl = ovid->overlays[0];
336
337 switch (pix->pixelformat) {
338 case 0:
339 break;
340 case V4L2_PIX_FMT_YUYV:
341 mode = OMAP_DSS_COLOR_YUV2;
342 break;
343 case V4L2_PIX_FMT_UYVY:
344 mode = OMAP_DSS_COLOR_UYVY;
345 break;
346 case V4L2_PIX_FMT_RGB565:
347 mode = OMAP_DSS_COLOR_RGB16;
348 break;
349 case V4L2_PIX_FMT_RGB24:
350 mode = OMAP_DSS_COLOR_RGB24P;
351 break;
352 case V4L2_PIX_FMT_RGB32:
353 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
354 OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
355 break;
356 case V4L2_PIX_FMT_BGR32:
357 mode = OMAP_DSS_COLOR_RGBX32;
358 break;
359 default:
360 mode = -EINVAL;
361 }
362 return mode;
363}
364
365/*
366 * Setup the overlay
367 */
archit tanejaa137ac82011-06-14 03:54:45 -0300368static int omapvid_setup_overlay(struct omap_vout_device *vout,
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300369 struct omap_overlay *ovl, int posx, int posy, int outw,
370 int outh, u32 addr)
371{
372 int ret = 0;
373 struct omap_overlay_info info;
374 int cropheight, cropwidth, pixheight, pixwidth;
375
376 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
377 (outw != vout->pix.width || outh != vout->pix.height)) {
378 ret = -EINVAL;
379 goto setup_ovl_err;
380 }
381
382 vout->dss_mode = video_mode_to_dss_mode(vout);
383 if (vout->dss_mode == -EINVAL) {
384 ret = -EINVAL;
385 goto setup_ovl_err;
386 }
387
388 /* Setup the input plane parameters according to
389 * rotation value selected.
390 */
archit tanejab3668882011-06-14 03:54:46 -0300391 if (is_rotation_90_or_270(vout)) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300392 cropheight = vout->crop.width;
393 cropwidth = vout->crop.height;
394 pixheight = vout->pix.width;
395 pixwidth = vout->pix.height;
396 } else {
397 cropheight = vout->crop.height;
398 cropwidth = vout->crop.width;
399 pixheight = vout->pix.height;
400 pixwidth = vout->pix.width;
401 }
402
403 ovl->get_overlay_info(ovl, &info);
404 info.paddr = addr;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300405 info.width = cropwidth;
406 info.height = cropheight;
407 info.color_mode = vout->dss_mode;
408 info.mirror = vout->mirror;
409 info.pos_x = posx;
410 info.pos_y = posy;
411 info.out_width = outw;
412 info.out_height = outh;
413 info.global_alpha = vout->win.global_alpha;
archit tanejab3668882011-06-14 03:54:46 -0300414 if (!is_rotation_enabled(vout)) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300415 info.rotation = 0;
416 info.rotation_type = OMAP_DSS_ROT_DMA;
417 info.screen_width = pixwidth;
418 } else {
419 info.rotation = vout->rotation;
420 info.rotation_type = OMAP_DSS_ROT_VRFB;
421 info.screen_width = 2048;
422 }
423
424 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
425 "%s enable=%d addr=%x width=%d\n height=%d color_mode=%d\n"
426 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
427 "out_height=%d rotation_type=%d screen_width=%d\n",
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +0200428 __func__, ovl->is_enabled(ovl), info.paddr, info.width, info.height,
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300429 info.color_mode, info.rotation, info.mirror, info.pos_x,
430 info.pos_y, info.out_width, info.out_height, info.rotation_type,
431 info.screen_width);
432
433 ret = ovl->set_overlay_info(ovl, &info);
434 if (ret)
435 goto setup_ovl_err;
436
437 return 0;
438
439setup_ovl_err:
440 v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
441 return ret;
442}
443
444/*
445 * Initialize the overlay structure
446 */
archit tanejaa137ac82011-06-14 03:54:45 -0300447static int omapvid_init(struct omap_vout_device *vout, u32 addr)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300448{
449 int ret = 0, i;
450 struct v4l2_window *win;
451 struct omap_overlay *ovl;
452 int posx, posy, outw, outh, temp;
453 struct omap_video_timings *timing;
454 struct omapvideo_info *ovid = &vout->vid_info;
455
456 win = &vout->win;
457 for (i = 0; i < ovid->num_overlays; i++) {
Linus Torvalds5f769452012-10-12 10:21:02 +0900458 struct omap_dss_device *dssdev;
459
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300460 ovl = ovid->overlays[i];
Linus Torvalds5f769452012-10-12 10:21:02 +0900461 dssdev = ovl->get_device(ovl);
462
463 if (!dssdev)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300464 return -EINVAL;
465
Linus Torvalds5f769452012-10-12 10:21:02 +0900466 timing = &dssdev->panel.timings;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300467
468 outw = win->w.width;
469 outh = win->w.height;
470 switch (vout->rotation) {
471 case dss_rotation_90_degree:
472 /* Invert the height and width for 90
473 * and 270 degree rotation
474 */
475 temp = outw;
476 outw = outh;
477 outh = temp;
478 posy = (timing->y_res - win->w.width) - win->w.left;
479 posx = win->w.top;
480 break;
481
482 case dss_rotation_180_degree:
483 posx = (timing->x_res - win->w.width) - win->w.left;
484 posy = (timing->y_res - win->w.height) - win->w.top;
485 break;
486
487 case dss_rotation_270_degree:
488 temp = outw;
489 outw = outh;
490 outh = temp;
491 posy = win->w.left;
492 posx = (timing->x_res - win->w.height) - win->w.top;
493 break;
494
495 default:
496 posx = win->w.left;
497 posy = win->w.top;
498 break;
499 }
500
501 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
502 outw, outh, addr);
503 if (ret)
504 goto omapvid_init_err;
505 }
506 return 0;
507
508omapvid_init_err:
509 v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
510 return ret;
511}
512
513/*
514 * Apply the changes set the go bit of DSS
515 */
archit tanejaa137ac82011-06-14 03:54:45 -0300516static int omapvid_apply_changes(struct omap_vout_device *vout)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300517{
518 int i;
519 struct omap_overlay *ovl;
520 struct omapvideo_info *ovid = &vout->vid_info;
521
522 for (i = 0; i < ovid->num_overlays; i++) {
Linus Torvalds5f769452012-10-12 10:21:02 +0900523 struct omap_dss_device *dssdev;
524
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300525 ovl = ovid->overlays[i];
Linus Torvalds5f769452012-10-12 10:21:02 +0900526 dssdev = ovl->get_device(ovl);
527 if (!dssdev)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300528 return -EINVAL;
529 ovl->manager->apply(ovl->manager);
530 }
531
532 return 0;
533}
534
Archit Taneja27801682011-09-28 10:49:25 -0300535static int omapvid_handle_interlace_display(struct omap_vout_device *vout,
536 unsigned int irqstatus, struct timeval timevalue)
537{
538 u32 fid;
539
540 if (vout->first_int) {
541 vout->first_int = 0;
542 goto err;
543 }
544
545 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
546 fid = 1;
547 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
548 fid = 0;
549 else
550 goto err;
551
552 vout->field_id ^= 1;
553 if (fid != vout->field_id) {
554 if (fid == 0)
555 vout->field_id = fid;
556 } else if (0 == fid) {
557 if (vout->cur_frm == vout->next_frm)
558 goto err;
559
560 vout->cur_frm->ts = timevalue;
561 vout->cur_frm->state = VIDEOBUF_DONE;
562 wake_up_interruptible(&vout->cur_frm->done);
563 vout->cur_frm = vout->next_frm;
564 } else {
565 if (list_empty(&vout->dma_queue) ||
566 (vout->cur_frm != vout->next_frm))
567 goto err;
568 }
569
570 return vout->field_id;
571err:
572 return 0;
573}
574
archit tanejaa137ac82011-06-14 03:54:45 -0300575static void omap_vout_isr(void *arg, unsigned int irqstatus)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300576{
Archit Tanejae144ca62011-09-28 10:49:26 -0300577 int ret, fid, mgr_id;
578 u32 addr, irq;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300579 struct omap_overlay *ovl;
580 struct timeval timevalue;
581 struct omapvideo_info *ovid;
582 struct omap_dss_device *cur_display;
583 struct omap_vout_device *vout = (struct omap_vout_device *)arg;
584
585 if (!vout->streaming)
586 return;
587
588 ovid = &vout->vid_info;
589 ovl = ovid->overlays[0];
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300590
Archit Tanejae144ca62011-09-28 10:49:26 -0300591 mgr_id = ovl->manager->id;
Linus Torvalds5f769452012-10-12 10:21:02 +0900592
593 /* get the display device attached to the overlay */
594 cur_display = ovl->get_device(ovl);
595
596 if (!cur_display)
597 return;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300598
599 spin_lock(&vout->vbq_lock);
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300600 v4l2_get_timestamp(&timevalue);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300601
Archit Taneja27801682011-09-28 10:49:25 -0300602 switch (cur_display->type) {
Archit Taneja881a9642011-09-28 10:49:27 -0300603 case OMAP_DISPLAY_TYPE_DSI:
Archit Taneja27801682011-09-28 10:49:25 -0300604 case OMAP_DISPLAY_TYPE_DPI:
Archit Tanejae144ca62011-09-28 10:49:26 -0300605 if (mgr_id == OMAP_DSS_CHANNEL_LCD)
606 irq = DISPC_IRQ_VSYNC;
607 else if (mgr_id == OMAP_DSS_CHANNEL_LCD2)
608 irq = DISPC_IRQ_VSYNC2;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300609 else
610 goto vout_isr_err;
611
Archit Tanejae144ca62011-09-28 10:49:26 -0300612 if (!(irqstatus & irq))
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300613 goto vout_isr_err;
Archit Taneja27801682011-09-28 10:49:25 -0300614 break;
615 case OMAP_DISPLAY_TYPE_VENC:
616 fid = omapvid_handle_interlace_display(vout, irqstatus,
617 timevalue);
618 if (!fid)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300619 goto vout_isr_err;
Archit Taneja27801682011-09-28 10:49:25 -0300620 break;
621 case OMAP_DISPLAY_TYPE_HDMI:
622 if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300623 goto vout_isr_err;
Archit Taneja27801682011-09-28 10:49:25 -0300624 break;
625 default:
626 goto vout_isr_err;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300627 }
628
Archit Taneja27801682011-09-28 10:49:25 -0300629 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
630 vout->cur_frm->ts = timevalue;
631 vout->cur_frm->state = VIDEOBUF_DONE;
632 wake_up_interruptible(&vout->cur_frm->done);
633 vout->cur_frm = vout->next_frm;
634 }
635
636 vout->first_int = 0;
637 if (list_empty(&vout->dma_queue))
638 goto vout_isr_err;
639
640 vout->next_frm = list_entry(vout->dma_queue.next,
641 struct videobuf_buffer, queue);
642 list_del(&vout->next_frm->queue);
643
644 vout->next_frm->state = VIDEOBUF_ACTIVE;
645
646 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
647 + vout->cropped_offset;
648
649 /* First save the configuration in ovelray structure */
650 ret = omapvid_init(vout, addr);
Federico Fuga15170022012-08-24 11:54:11 -0300651 if (ret) {
Archit Taneja27801682011-09-28 10:49:25 -0300652 printk(KERN_ERR VOUT_NAME
653 "failed to set overlay info\n");
Federico Fuga15170022012-08-24 11:54:11 -0300654 goto vout_isr_err;
655 }
656
Archit Taneja27801682011-09-28 10:49:25 -0300657 /* Enable the pipeline and set the Go bit */
658 ret = omapvid_apply_changes(vout);
659 if (ret)
660 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
661
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300662vout_isr_err:
663 spin_unlock(&vout->vbq_lock);
664}
665
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300666/* Video buffer call backs */
667
668/*
669 * Buffer setup function is called by videobuf layer when REQBUF ioctl is
670 * called. This is used to setup buffers and return size and count of
671 * buffers allocated. After the call to this buffer, videobuf layer will
672 * setup buffer queue depending on the size and count of buffers
673 */
674static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
675 unsigned int *size)
676{
677 int startindex = 0, i, j;
678 u32 phy_addr = 0, virt_addr = 0;
679 struct omap_vout_device *vout = q->priv_data;
archit taneja445e2582011-06-14 03:54:47 -0300680 struct omapvideo_info *ovid = &vout->vid_info;
Archit Tanejad06db7e2011-09-28 10:49:24 -0300681 int vid_max_buf_size;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300682
683 if (!vout)
684 return -EINVAL;
685
Archit Tanejad06db7e2011-09-28 10:49:24 -0300686 vid_max_buf_size = vout->vid == OMAP_VIDEO1 ? video1_bufsize :
687 video2_bufsize;
688
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300689 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
690 return -EINVAL;
691
692 startindex = (vout->vid == OMAP_VIDEO1) ?
693 video1_numbuffers : video2_numbuffers;
694 if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
695 *count = startindex;
696
archit taneja445e2582011-06-14 03:54:47 -0300697 if (ovid->rotation_type == VOUT_ROT_VRFB) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300698 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
699 return -ENOMEM;
archit taneja445e2582011-06-14 03:54:47 -0300700 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300701
702 if (V4L2_MEMORY_MMAP != vout->memory)
703 return 0;
704
705 /* Now allocated the V4L2 buffers */
706 *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
707 startindex = (vout->vid == OMAP_VIDEO1) ?
708 video1_numbuffers : video2_numbuffers;
709
Vaibhav Hiremath383e4f62011-04-14 13:42:34 -0300710 /* Check the size of the buffer */
Archit Tanejad06db7e2011-09-28 10:49:24 -0300711 if (*size > vid_max_buf_size) {
Vaibhav Hiremath383e4f62011-04-14 13:42:34 -0300712 v4l2_err(&vout->vid_dev->v4l2_dev,
713 "buffer allocation mismatch [%u] [%u]\n",
714 *size, vout->buffer_size);
715 return -ENOMEM;
716 }
717
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300718 for (i = startindex; i < *count; i++) {
719 vout->buffer_size = *size;
720
721 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
722 &phy_addr);
723 if (!virt_addr) {
archit taneja445e2582011-06-14 03:54:47 -0300724 if (ovid->rotation_type == VOUT_ROT_NONE) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300725 break;
archit taneja445e2582011-06-14 03:54:47 -0300726 } else {
727 if (!is_rotation_enabled(vout))
728 break;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300729 /* Free the VRFB buffers if no space for V4L2 buffers */
730 for (j = i; j < *count; j++) {
731 omap_vout_free_buffer(
732 vout->smsshado_virt_addr[j],
733 vout->smsshado_size);
734 vout->smsshado_virt_addr[j] = 0;
735 vout->smsshado_phy_addr[j] = 0;
archit taneja445e2582011-06-14 03:54:47 -0300736 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300737 }
738 }
739 vout->buf_virt_addr[i] = virt_addr;
740 vout->buf_phy_addr[i] = phy_addr;
741 }
742 *count = vout->buffer_allocated = i;
743
744 return 0;
745}
746
747/*
748 * Free the V4L2 buffers additionally allocated than default
archit taneja445e2582011-06-14 03:54:47 -0300749 * number of buffers
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300750 */
archit taneja445e2582011-06-14 03:54:47 -0300751static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300752{
753 int num_buffers = 0, i;
754
755 num_buffers = (vout->vid == OMAP_VIDEO1) ?
756 video1_numbuffers : video2_numbuffers;
757
758 for (i = num_buffers; i < vout->buffer_allocated; i++) {
759 if (vout->buf_virt_addr[i])
760 omap_vout_free_buffer(vout->buf_virt_addr[i],
761 vout->buffer_size);
762
763 vout->buf_virt_addr[i] = 0;
764 vout->buf_phy_addr[i] = 0;
765 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300766 vout->buffer_allocated = num_buffers;
767}
768
769/*
770 * This function will be called when VIDIOC_QBUF ioctl is called.
771 * It prepare buffers before give out for the display. This function
772 * converts user space virtual address into physical address if userptr memory
773 * exchange mechanism is used. If rotation is enabled, it copies entire
774 * buffer into VRFB memory space before giving it to the DSS.
775 */
776static int omap_vout_buffer_prepare(struct videobuf_queue *q,
archit taneja445e2582011-06-14 03:54:47 -0300777 struct videobuf_buffer *vb,
778 enum v4l2_field field)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300779{
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300780 struct omap_vout_device *vout = q->priv_data;
archit taneja445e2582011-06-14 03:54:47 -0300781 struct omapvideo_info *ovid = &vout->vid_info;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300782
783 if (VIDEOBUF_NEEDS_INIT == vb->state) {
784 vb->width = vout->pix.width;
785 vb->height = vout->pix.height;
786 vb->size = vb->width * vb->height * vout->bpp;
787 vb->field = field;
788 }
789 vb->state = VIDEOBUF_PREPARED;
790 /* if user pointer memory mechanism is used, get the physical
791 * address of the buffer
792 */
793 if (V4L2_MEMORY_USERPTR == vb->memory) {
794 if (0 == vb->baddr)
795 return -EINVAL;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300796 /* Physical address */
Vaibhav Hiremathdd880dd2010-05-27 08:17:08 -0300797 vout->queued_buf_addr[vb->i] = (u8 *)
798 omap_vout_uservirt_to_phys(vb->baddr);
799 } else {
Amber Jain72915e82011-07-07 06:03:25 -0300800 u32 addr, dma_addr;
801 unsigned long size;
802
803 addr = (unsigned long) vout->buf_virt_addr[vb->i];
804 size = (unsigned long) vb->size;
805
806 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
807 size, DMA_TO_DEVICE);
808 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
809 v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
810
Vaibhav Hiremathdd880dd2010-05-27 08:17:08 -0300811 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300812 }
813
archit taneja445e2582011-06-14 03:54:47 -0300814 if (ovid->rotation_type == VOUT_ROT_VRFB)
815 return omap_vout_prepare_vrfb(vout, vb);
816 else
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300817 return 0;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300818}
819
820/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300821 * Buffer queue function will be called from the videobuf layer when _QBUF
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300822 * ioctl is called. It is used to enqueue buffer, which is ready to be
823 * displayed.
824 */
825static void omap_vout_buffer_queue(struct videobuf_queue *q,
826 struct videobuf_buffer *vb)
827{
828 struct omap_vout_device *vout = q->priv_data;
829
830 /* Driver is also maintainig a queue. So enqueue buffer in the driver
831 * queue */
832 list_add_tail(&vb->queue, &vout->dma_queue);
833
834 vb->state = VIDEOBUF_QUEUED;
835}
836
837/*
838 * Buffer release function is called from videobuf layer to release buffer
839 * which are already allocated
840 */
841static void omap_vout_buffer_release(struct videobuf_queue *q,
842 struct videobuf_buffer *vb)
843{
844 struct omap_vout_device *vout = q->priv_data;
845
846 vb->state = VIDEOBUF_NEEDS_INIT;
847
848 if (V4L2_MEMORY_MMAP != vout->memory)
849 return;
850}
851
852/*
853 * File operations
854 */
Laurent Pinchart94f3f482011-01-20 21:15:42 -0300855static unsigned int omap_vout_poll(struct file *file,
856 struct poll_table_struct *wait)
857{
858 struct omap_vout_device *vout = file->private_data;
859 struct videobuf_queue *q = &vout->vbq;
860
861 return videobuf_poll_stream(file, q, wait);
862}
863
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300864static void omap_vout_vm_open(struct vm_area_struct *vma)
865{
866 struct omap_vout_device *vout = vma->vm_private_data;
867
868 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
869 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
870 vout->mmap_count++;
871}
872
873static void omap_vout_vm_close(struct vm_area_struct *vma)
874{
875 struct omap_vout_device *vout = vma->vm_private_data;
876
877 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
878 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
879 vout->mmap_count--;
880}
881
882static struct vm_operations_struct omap_vout_vm_ops = {
883 .open = omap_vout_vm_open,
884 .close = omap_vout_vm_close,
885};
886
887static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
888{
889 int i;
890 void *pos;
891 unsigned long start = vma->vm_start;
892 unsigned long size = (vma->vm_end - vma->vm_start);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300893 struct omap_vout_device *vout = file->private_data;
894 struct videobuf_queue *q = &vout->vbq;
895
896 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
897 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
898 vma->vm_pgoff, vma->vm_start, vma->vm_end);
899
900 /* look for the buffer to map */
901 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
902 if (NULL == q->bufs[i])
903 continue;
904 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
905 continue;
906 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
907 break;
908 }
909
910 if (VIDEO_MAX_FRAME == i) {
911 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
912 "offset invalid [offset=0x%lx]\n",
913 (vma->vm_pgoff << PAGE_SHIFT));
914 return -EINVAL;
915 }
Vaibhav Hiremath383e4f62011-04-14 13:42:34 -0300916 /* Check the size of the buffer */
917 if (size > vout->buffer_size) {
918 v4l2_err(&vout->vid_dev->v4l2_dev,
919 "insufficient memory [%lu] [%u]\n",
920 size, vout->buffer_size);
921 return -ENOMEM;
922 }
923
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300924 q->bufs[i]->baddr = vma->vm_start;
925
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700926 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300927 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
928 vma->vm_ops = &omap_vout_vm_ops;
929 vma->vm_private_data = (void *) vout;
Vaibhav Hiremathdd880dd2010-05-27 08:17:08 -0300930 pos = (void *)vout->buf_virt_addr[i];
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300931 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
932 while (size > 0) {
933 unsigned long pfn;
934 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
935 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
936 return -EAGAIN;
937 start += PAGE_SIZE;
938 pos += PAGE_SIZE;
939 size -= PAGE_SIZE;
940 }
941 vout->mmap_count++;
942 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
943
944 return 0;
945}
946
947static int omap_vout_release(struct file *file)
948{
949 unsigned int ret, i;
950 struct videobuf_queue *q;
951 struct omapvideo_info *ovid;
952 struct omap_vout_device *vout = file->private_data;
953
954 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
955 ovid = &vout->vid_info;
956
957 if (!vout)
958 return 0;
959
960 q = &vout->vbq;
961 /* Disable all the overlay managers connected with this interface */
962 for (i = 0; i < ovid->num_overlays; i++) {
963 struct omap_overlay *ovl = ovid->overlays[i];
Linus Torvalds5f769452012-10-12 10:21:02 +0900964 struct omap_dss_device *dssdev = ovl->get_device(ovl);
965
966 if (dssdev)
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +0200967 ovl->disable(ovl);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300968 }
969 /* Turn off the pipeline */
970 ret = omapvid_apply_changes(vout);
971 if (ret)
972 v4l2_warn(&vout->vid_dev->v4l2_dev,
973 "Unable to apply changes\n");
974
975 /* Free all buffers */
archit taneja445e2582011-06-14 03:54:47 -0300976 omap_vout_free_extra_buffers(vout);
977
978 /* Free the VRFB buffers only if they are allocated
979 * during reqbufs. Don't free if init time allocated
980 */
981 if (ovid->rotation_type == VOUT_ROT_VRFB) {
982 if (!vout->vrfb_static_allocation)
983 omap_vout_free_vrfb_buffers(vout);
984 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300985 videobuf_mmap_free(q);
986
987 /* Even if apply changes fails we should continue
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400988 freeing allocated memory */
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300989 if (vout->streaming) {
990 u32 mask = 0;
991
992 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
Amber Jain5251dd62011-06-02 02:30:10 -0300993 DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -0300994 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
995 vout->streaming = 0;
996
997 videobuf_streamoff(q);
998 videobuf_queue_cancel(q);
999 }
1000
1001 if (vout->mmap_count != 0)
1002 vout->mmap_count = 0;
1003
1004 vout->opened -= 1;
1005 file->private_data = NULL;
1006
1007 if (vout->buffer_allocated)
1008 videobuf_mmap_free(q);
1009
1010 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1011 return ret;
1012}
1013
1014static int omap_vout_open(struct file *file)
1015{
1016 struct videobuf_queue *q;
1017 struct omap_vout_device *vout = NULL;
1018
1019 vout = video_drvdata(file);
1020 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1021
1022 if (vout == NULL)
1023 return -ENODEV;
1024
1025 /* for now, we only support single open */
1026 if (vout->opened)
1027 return -EBUSY;
1028
1029 vout->opened += 1;
1030
1031 file->private_data = vout;
1032 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1033
1034 q = &vout->vbq;
1035 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1036 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1037 video_vbq_ops.buf_release = omap_vout_buffer_release;
1038 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1039 spin_lock_init(&vout->vbq_lock);
1040
Vaibhav Hiremathdd880dd2010-05-27 08:17:08 -03001041 videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1042 &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
Hans Verkuile3cfd442010-09-30 09:18:52 -03001043 sizeof(struct videobuf_buffer), vout, NULL);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001044
1045 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1046 return 0;
1047}
1048
1049/*
1050 * V4L2 ioctls
1051 */
1052static int vidioc_querycap(struct file *file, void *fh,
1053 struct v4l2_capability *cap)
1054{
1055 struct omap_vout_device *vout = fh;
1056
1057 strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1058 strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1059 cap->bus_info[0] = '\0';
Hans Verkuil047a01f2011-11-22 08:23:19 -03001060 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT |
1061 V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001062
1063 return 0;
1064}
1065
1066static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1067 struct v4l2_fmtdesc *fmt)
1068{
1069 int index = fmt->index;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001070
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001071 if (index >= NUM_OUTPUT_FORMATS)
1072 return -EINVAL;
1073
1074 fmt->flags = omap_formats[index].flags;
1075 strlcpy(fmt->description, omap_formats[index].description,
1076 sizeof(fmt->description));
1077 fmt->pixelformat = omap_formats[index].pixelformat;
1078
1079 return 0;
1080}
1081
1082static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1083 struct v4l2_format *f)
1084{
1085 struct omap_vout_device *vout = fh;
1086
1087 f->fmt.pix = vout->pix;
1088 return 0;
1089
1090}
1091
1092static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1093 struct v4l2_format *f)
1094{
1095 struct omap_overlay *ovl;
1096 struct omapvideo_info *ovid;
1097 struct omap_video_timings *timing;
1098 struct omap_vout_device *vout = fh;
Linus Torvalds5f769452012-10-12 10:21:02 +09001099 struct omap_dss_device *dssdev;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001100
1101 ovid = &vout->vid_info;
1102 ovl = ovid->overlays[0];
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001103 /* get the display device attached to the overlay */
Linus Torvalds5f769452012-10-12 10:21:02 +09001104 dssdev = ovl->get_device(ovl);
1105
1106 if (!dssdev)
1107 return -EINVAL;
1108
1109 timing = &dssdev->panel.timings;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001110
1111 vout->fbuf.fmt.height = timing->y_res;
1112 vout->fbuf.fmt.width = timing->x_res;
1113
1114 omap_vout_try_format(&f->fmt.pix);
1115 return 0;
1116}
1117
1118static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1119 struct v4l2_format *f)
1120{
1121 int ret, bpp;
1122 struct omap_overlay *ovl;
1123 struct omapvideo_info *ovid;
1124 struct omap_video_timings *timing;
1125 struct omap_vout_device *vout = fh;
Linus Torvalds5f769452012-10-12 10:21:02 +09001126 struct omap_dss_device *dssdev;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001127
1128 if (vout->streaming)
1129 return -EBUSY;
1130
1131 mutex_lock(&vout->lock);
1132
1133 ovid = &vout->vid_info;
1134 ovl = ovid->overlays[0];
Linus Torvalds5f769452012-10-12 10:21:02 +09001135 dssdev = ovl->get_device(ovl);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001136
1137 /* get the display device attached to the overlay */
Linus Torvalds5f769452012-10-12 10:21:02 +09001138 if (!dssdev) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001139 ret = -EINVAL;
1140 goto s_fmt_vid_out_exit;
1141 }
Linus Torvalds5f769452012-10-12 10:21:02 +09001142 timing = &dssdev->panel.timings;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001143
1144 /* We dont support RGB24-packed mode if vrfb rotation
1145 * is enabled*/
archit tanejab3668882011-06-14 03:54:46 -03001146 if ((is_rotation_enabled(vout)) &&
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001147 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1148 ret = -EINVAL;
1149 goto s_fmt_vid_out_exit;
1150 }
1151
1152 /* get the framebuffer parameters */
1153
archit tanejab3668882011-06-14 03:54:46 -03001154 if (is_rotation_90_or_270(vout)) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001155 vout->fbuf.fmt.height = timing->x_res;
1156 vout->fbuf.fmt.width = timing->y_res;
1157 } else {
1158 vout->fbuf.fmt.height = timing->y_res;
1159 vout->fbuf.fmt.width = timing->x_res;
1160 }
1161
1162 /* change to samller size is OK */
1163
1164 bpp = omap_vout_try_format(&f->fmt.pix);
1165 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1166
1167 /* try & set the new output format */
1168 vout->bpp = bpp;
1169 vout->pix = f->fmt.pix;
1170 vout->vrfb_bpp = 1;
1171
1172 /* If YUYV then vrfb bpp is 2, for others its 1 */
1173 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1174 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1175 vout->vrfb_bpp = 2;
1176
1177 /* set default crop and win */
1178 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1179
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001180 ret = 0;
1181
1182s_fmt_vid_out_exit:
1183 mutex_unlock(&vout->lock);
1184 return ret;
1185}
1186
1187static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1188 struct v4l2_format *f)
1189{
1190 int ret = 0;
1191 struct omap_vout_device *vout = fh;
Archit Taneja11354dd2011-09-26 11:47:29 +05301192 struct omap_overlay *ovl;
1193 struct omapvideo_info *ovid;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001194 struct v4l2_window *win = &f->fmt.win;
1195
Archit Taneja11354dd2011-09-26 11:47:29 +05301196 ovid = &vout->vid_info;
1197 ovl = ovid->overlays[0];
1198
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001199 ret = omap_vout_try_window(&vout->fbuf, win);
1200
1201 if (!ret) {
Archit Taneja11354dd2011-09-26 11:47:29 +05301202 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001203 win->global_alpha = 255;
1204 else
1205 win->global_alpha = f->fmt.win.global_alpha;
1206 }
1207
1208 return ret;
1209}
1210
1211static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1212 struct v4l2_format *f)
1213{
1214 int ret = 0;
1215 struct omap_overlay *ovl;
1216 struct omapvideo_info *ovid;
1217 struct omap_vout_device *vout = fh;
1218 struct v4l2_window *win = &f->fmt.win;
1219
1220 mutex_lock(&vout->lock);
1221 ovid = &vout->vid_info;
1222 ovl = ovid->overlays[0];
1223
1224 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1225 if (!ret) {
Archit Taneja11354dd2011-09-26 11:47:29 +05301226 /* Video1 plane does not support global alpha on OMAP3 */
1227 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001228 vout->win.global_alpha = 255;
1229 else
1230 vout->win.global_alpha = f->fmt.win.global_alpha;
1231
1232 vout->win.chromakey = f->fmt.win.chromakey;
1233 }
1234 mutex_unlock(&vout->lock);
1235 return ret;
1236}
1237
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001238static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1239 struct v4l2_format *f)
1240{
1241 u32 key_value = 0;
1242 struct omap_overlay *ovl;
1243 struct omapvideo_info *ovid;
1244 struct omap_vout_device *vout = fh;
1245 struct omap_overlay_manager_info info;
1246 struct v4l2_window *win = &f->fmt.win;
1247
1248 ovid = &vout->vid_info;
1249 ovl = ovid->overlays[0];
1250
1251 win->w = vout->win.w;
1252 win->field = vout->win.field;
1253 win->global_alpha = vout->win.global_alpha;
1254
1255 if (ovl->manager && ovl->manager->get_manager_info) {
1256 ovl->manager->get_manager_info(ovl->manager, &info);
1257 key_value = info.trans_key;
1258 }
1259 win->chromakey = key_value;
1260 return 0;
1261}
1262
1263static int vidioc_cropcap(struct file *file, void *fh,
1264 struct v4l2_cropcap *cropcap)
1265{
1266 struct omap_vout_device *vout = fh;
1267 struct v4l2_pix_format *pix = &vout->pix;
1268
1269 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1270 return -EINVAL;
1271
1272 /* Width and height are always even */
1273 cropcap->bounds.width = pix->width & ~1;
1274 cropcap->bounds.height = pix->height & ~1;
1275
1276 omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1277 cropcap->pixelaspect.numerator = 1;
1278 cropcap->pixelaspect.denominator = 1;
1279 return 0;
1280}
1281
1282static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1283{
1284 struct omap_vout_device *vout = fh;
1285
1286 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1287 return -EINVAL;
1288 crop->c = vout->crop;
1289 return 0;
1290}
1291
Hans Verkuil4f996592012-09-05 05:10:48 -03001292static int vidioc_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001293{
1294 int ret = -EINVAL;
1295 struct omap_vout_device *vout = fh;
1296 struct omapvideo_info *ovid;
1297 struct omap_overlay *ovl;
1298 struct omap_video_timings *timing;
Linus Torvalds5f769452012-10-12 10:21:02 +09001299 struct omap_dss_device *dssdev;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001300
1301 if (vout->streaming)
1302 return -EBUSY;
1303
1304 mutex_lock(&vout->lock);
1305 ovid = &vout->vid_info;
1306 ovl = ovid->overlays[0];
Linus Torvalds5f769452012-10-12 10:21:02 +09001307 /* get the display device attached to the overlay */
1308 dssdev = ovl->get_device(ovl);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001309
Linus Torvalds5f769452012-10-12 10:21:02 +09001310 if (!dssdev) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001311 ret = -EINVAL;
1312 goto s_crop_err;
1313 }
Linus Torvalds5f769452012-10-12 10:21:02 +09001314
1315 timing = &dssdev->panel.timings;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001316
archit tanejab3668882011-06-14 03:54:46 -03001317 if (is_rotation_90_or_270(vout)) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001318 vout->fbuf.fmt.height = timing->x_res;
1319 vout->fbuf.fmt.width = timing->y_res;
1320 } else {
1321 vout->fbuf.fmt.height = timing->y_res;
1322 vout->fbuf.fmt.width = timing->x_res;
1323 }
1324
1325 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1326 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1327 &vout->fbuf, &crop->c);
1328
1329s_crop_err:
1330 mutex_unlock(&vout->lock);
1331 return ret;
1332}
1333
1334static int vidioc_queryctrl(struct file *file, void *fh,
1335 struct v4l2_queryctrl *ctrl)
1336{
1337 int ret = 0;
1338
1339 switch (ctrl->id) {
1340 case V4L2_CID_ROTATE:
1341 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1342 break;
1343 case V4L2_CID_BG_COLOR:
1344 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1345 break;
1346 case V4L2_CID_VFLIP:
1347 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1348 break;
1349 default:
1350 ctrl->name[0] = '\0';
1351 ret = -EINVAL;
1352 }
1353 return ret;
1354}
1355
1356static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1357{
1358 int ret = 0;
1359 struct omap_vout_device *vout = fh;
1360
1361 switch (ctrl->id) {
1362 case V4L2_CID_ROTATE:
1363 ctrl->value = vout->control[0].value;
1364 break;
1365 case V4L2_CID_BG_COLOR:
1366 {
1367 struct omap_overlay_manager_info info;
1368 struct omap_overlay *ovl;
1369
1370 ovl = vout->vid_info.overlays[0];
1371 if (!ovl->manager || !ovl->manager->get_manager_info) {
1372 ret = -EINVAL;
1373 break;
1374 }
1375
1376 ovl->manager->get_manager_info(ovl->manager, &info);
1377 ctrl->value = info.default_color;
1378 break;
1379 }
1380 case V4L2_CID_VFLIP:
1381 ctrl->value = vout->control[2].value;
1382 break;
1383 default:
1384 ret = -EINVAL;
1385 }
1386 return ret;
1387}
1388
1389static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1390{
1391 int ret = 0;
1392 struct omap_vout_device *vout = fh;
1393
1394 switch (a->id) {
1395 case V4L2_CID_ROTATE:
1396 {
archit taneja445e2582011-06-14 03:54:47 -03001397 struct omapvideo_info *ovid;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001398 int rotation = a->value;
1399
archit taneja445e2582011-06-14 03:54:47 -03001400 ovid = &vout->vid_info;
1401
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001402 mutex_lock(&vout->lock);
archit taneja445e2582011-06-14 03:54:47 -03001403 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1404 mutex_unlock(&vout->lock);
1405 ret = -ERANGE;
1406 break;
1407 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001408
1409 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1410 mutex_unlock(&vout->lock);
1411 ret = -EINVAL;
1412 break;
1413 }
1414
1415 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1416 vout->mirror)) {
1417 mutex_unlock(&vout->lock);
1418 ret = -EINVAL;
1419 break;
1420 }
1421
1422 vout->control[0].value = rotation;
1423 mutex_unlock(&vout->lock);
1424 break;
1425 }
1426 case V4L2_CID_BG_COLOR:
1427 {
1428 struct omap_overlay *ovl;
1429 unsigned int color = a->value;
1430 struct omap_overlay_manager_info info;
1431
1432 ovl = vout->vid_info.overlays[0];
1433
1434 mutex_lock(&vout->lock);
1435 if (!ovl->manager || !ovl->manager->get_manager_info) {
1436 mutex_unlock(&vout->lock);
1437 ret = -EINVAL;
1438 break;
1439 }
1440
1441 ovl->manager->get_manager_info(ovl->manager, &info);
1442 info.default_color = color;
1443 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1444 mutex_unlock(&vout->lock);
1445 ret = -EINVAL;
1446 break;
1447 }
1448
1449 vout->control[1].value = color;
1450 mutex_unlock(&vout->lock);
1451 break;
1452 }
1453 case V4L2_CID_VFLIP:
1454 {
1455 struct omap_overlay *ovl;
1456 struct omapvideo_info *ovid;
1457 unsigned int mirror = a->value;
1458
1459 ovid = &vout->vid_info;
1460 ovl = ovid->overlays[0];
1461
1462 mutex_lock(&vout->lock);
archit taneja445e2582011-06-14 03:54:47 -03001463 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1464 mutex_unlock(&vout->lock);
1465 ret = -ERANGE;
1466 break;
1467 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001468
1469 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1470 mutex_unlock(&vout->lock);
1471 ret = -EINVAL;
1472 break;
1473 }
1474 vout->mirror = mirror;
1475 vout->control[2].value = mirror;
1476 mutex_unlock(&vout->lock);
1477 break;
1478 }
1479 default:
1480 ret = -EINVAL;
1481 }
1482 return ret;
1483}
1484
1485static int vidioc_reqbufs(struct file *file, void *fh,
1486 struct v4l2_requestbuffers *req)
1487{
1488 int ret = 0;
1489 unsigned int i, num_buffers = 0;
1490 struct omap_vout_device *vout = fh;
1491 struct videobuf_queue *q = &vout->vbq;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001492
1493 if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
1494 return -EINVAL;
1495 /* if memory is not mmp or userptr
1496 return error */
1497 if ((V4L2_MEMORY_MMAP != req->memory) &&
1498 (V4L2_MEMORY_USERPTR != req->memory))
1499 return -EINVAL;
1500
1501 mutex_lock(&vout->lock);
1502 /* Cannot be requested when streaming is on */
1503 if (vout->streaming) {
1504 ret = -EBUSY;
1505 goto reqbuf_err;
1506 }
1507
1508 /* If buffers are already allocated free them */
1509 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1510 if (vout->mmap_count) {
1511 ret = -EBUSY;
1512 goto reqbuf_err;
1513 }
1514 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1515 video1_numbuffers : video2_numbuffers;
1516 for (i = num_buffers; i < vout->buffer_allocated; i++) {
Vaibhav Hiremathdd880dd2010-05-27 08:17:08 -03001517 omap_vout_free_buffer(vout->buf_virt_addr[i],
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001518 vout->buffer_size);
1519 vout->buf_virt_addr[i] = 0;
1520 vout->buf_phy_addr[i] = 0;
1521 }
1522 vout->buffer_allocated = num_buffers;
1523 videobuf_mmap_free(q);
1524 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1525 if (vout->buffer_allocated) {
1526 videobuf_mmap_free(q);
1527 for (i = 0; i < vout->buffer_allocated; i++) {
1528 kfree(q->bufs[i]);
1529 q->bufs[i] = NULL;
1530 }
1531 vout->buffer_allocated = 0;
1532 }
1533 }
1534
1535 /*store the memory type in data structure */
1536 vout->memory = req->memory;
1537
1538 INIT_LIST_HEAD(&vout->dma_queue);
1539
1540 /* call videobuf_reqbufs api */
1541 ret = videobuf_reqbufs(q, req);
1542 if (ret < 0)
1543 goto reqbuf_err;
1544
1545 vout->buffer_allocated = req->count;
Vaibhav Hiremathdd880dd2010-05-27 08:17:08 -03001546
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001547reqbuf_err:
1548 mutex_unlock(&vout->lock);
1549 return ret;
1550}
1551
1552static int vidioc_querybuf(struct file *file, void *fh,
1553 struct v4l2_buffer *b)
1554{
1555 struct omap_vout_device *vout = fh;
1556
1557 return videobuf_querybuf(&vout->vbq, b);
1558}
1559
1560static int vidioc_qbuf(struct file *file, void *fh,
1561 struct v4l2_buffer *buffer)
1562{
1563 struct omap_vout_device *vout = fh;
1564 struct videobuf_queue *q = &vout->vbq;
1565
1566 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1567 (buffer->index >= vout->buffer_allocated) ||
1568 (q->bufs[buffer->index]->memory != buffer->memory)) {
1569 return -EINVAL;
1570 }
1571 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1572 if ((buffer->length < vout->pix.sizeimage) ||
1573 (0 == buffer->m.userptr)) {
1574 return -EINVAL;
1575 }
1576 }
1577
archit tanejab3668882011-06-14 03:54:46 -03001578 if ((is_rotation_enabled(vout)) &&
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001579 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1580 v4l2_warn(&vout->vid_dev->v4l2_dev,
1581 "DMA Channel not allocated for Rotation\n");
1582 return -EINVAL;
1583 }
1584
1585 return videobuf_qbuf(q, buffer);
1586}
1587
1588static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1589{
1590 struct omap_vout_device *vout = fh;
1591 struct videobuf_queue *q = &vout->vbq;
1592
Amber Jain72915e82011-07-07 06:03:25 -03001593 int ret;
1594 u32 addr;
1595 unsigned long size;
1596 struct videobuf_buffer *vb;
1597
1598 vb = q->bufs[b->index];
1599
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001600 if (!vout->streaming)
1601 return -EINVAL;
1602
1603 if (file->f_flags & O_NONBLOCK)
1604 /* Call videobuf_dqbuf for non blocking mode */
Amber Jain72915e82011-07-07 06:03:25 -03001605 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001606 else
1607 /* Call videobuf_dqbuf for blocking mode */
Amber Jain72915e82011-07-07 06:03:25 -03001608 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
1609
1610 addr = (unsigned long) vout->buf_phy_addr[vb->i];
1611 size = (unsigned long) vb->size;
1612 dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
1613 size, DMA_TO_DEVICE);
1614 return ret;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001615}
1616
1617static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1618{
1619 int ret = 0, j;
1620 u32 addr = 0, mask = 0;
1621 struct omap_vout_device *vout = fh;
1622 struct videobuf_queue *q = &vout->vbq;
1623 struct omapvideo_info *ovid = &vout->vid_info;
1624
1625 mutex_lock(&vout->lock);
1626
1627 if (vout->streaming) {
1628 ret = -EBUSY;
1629 goto streamon_err;
1630 }
1631
1632 ret = videobuf_streamon(q);
1633 if (ret)
1634 goto streamon_err;
1635
1636 if (list_empty(&vout->dma_queue)) {
1637 ret = -EIO;
1638 goto streamon_err1;
1639 }
1640
1641 /* Get the next frame from the buffer queue */
1642 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1643 struct videobuf_buffer, queue);
1644 /* Remove buffer from the buffer queue */
1645 list_del(&vout->cur_frm->queue);
1646 /* Mark state of the current frame to active */
1647 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1648 /* Initialize field_id and started member */
1649 vout->field_id = 0;
1650
1651 /* set flag here. Next QBUF will start DMA */
1652 vout->streaming = 1;
1653
1654 vout->first_int = 1;
1655
1656 if (omap_vout_calculate_offset(vout)) {
1657 ret = -EINVAL;
1658 goto streamon_err1;
1659 }
1660 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1661 + vout->cropped_offset;
1662
Amber Jain5251dd62011-06-02 02:30:10 -03001663 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1664 | DISPC_IRQ_VSYNC2;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001665
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001666 /* First save the configuration in ovelray structure */
1667 ret = omapvid_init(vout, addr);
Federico Fuga15170022012-08-24 11:54:11 -03001668 if (ret) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001669 v4l2_err(&vout->vid_dev->v4l2_dev,
1670 "failed to set overlay info\n");
Federico Fuga15170022012-08-24 11:54:11 -03001671 goto streamon_err1;
1672 }
1673
1674 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1675
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001676 /* Enable the pipeline and set the Go bit */
1677 ret = omapvid_apply_changes(vout);
1678 if (ret)
1679 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1680
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +02001681 for (j = 0; j < ovid->num_overlays; j++) {
1682 struct omap_overlay *ovl = ovid->overlays[j];
Linus Torvalds5f769452012-10-12 10:21:02 +09001683 struct omap_dss_device *dssdev = ovl->get_device(ovl);
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +02001684
Linus Torvalds5f769452012-10-12 10:21:02 +09001685 if (dssdev) {
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +02001686 ret = ovl->enable(ovl);
1687 if (ret)
1688 goto streamon_err1;
1689 }
1690 }
1691
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001692 ret = 0;
1693
1694streamon_err1:
1695 if (ret)
1696 ret = videobuf_streamoff(q);
1697streamon_err:
1698 mutex_unlock(&vout->lock);
1699 return ret;
1700}
1701
1702static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1703{
1704 u32 mask = 0;
1705 int ret = 0, j;
1706 struct omap_vout_device *vout = fh;
1707 struct omapvideo_info *ovid = &vout->vid_info;
1708
1709 if (!vout->streaming)
1710 return -EINVAL;
1711
1712 vout->streaming = 0;
Amber Jain5251dd62011-06-02 02:30:10 -03001713 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1714 | DISPC_IRQ_VSYNC2;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001715
1716 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1717
1718 for (j = 0; j < ovid->num_overlays; j++) {
1719 struct omap_overlay *ovl = ovid->overlays[j];
Linus Torvalds5f769452012-10-12 10:21:02 +09001720 struct omap_dss_device *dssdev = ovl->get_device(ovl);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001721
Linus Torvalds5f769452012-10-12 10:21:02 +09001722 if (dssdev)
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +02001723 ovl->disable(ovl);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001724 }
1725
1726 /* Turn of the pipeline */
1727 ret = omapvid_apply_changes(vout);
1728 if (ret)
1729 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
1730 " streamoff\n");
1731
1732 INIT_LIST_HEAD(&vout->dma_queue);
1733 ret = videobuf_streamoff(&vout->vbq);
1734
1735 return ret;
1736}
1737
1738static int vidioc_s_fbuf(struct file *file, void *fh,
Hans Verkuile6eb28c2012-09-04 10:26:45 -03001739 const struct v4l2_framebuffer *a)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001740{
1741 int enable = 0;
1742 struct omap_overlay *ovl;
1743 struct omapvideo_info *ovid;
1744 struct omap_vout_device *vout = fh;
1745 struct omap_overlay_manager_info info;
1746 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1747
1748 ovid = &vout->vid_info;
1749 ovl = ovid->overlays[0];
1750
1751 /* OMAP DSS doesn't support Source and Destination color
1752 key together */
1753 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1754 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1755 return -EINVAL;
1756 /* OMAP DSS Doesn't support the Destination color key
1757 and alpha blending together */
1758 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1759 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1760 return -EINVAL;
1761
1762 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1763 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1764 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
1765 } else
1766 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1767
1768 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1769 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1770 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1771 } else
1772 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
1773
1774 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1775 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1776 enable = 1;
1777 else
1778 enable = 0;
1779 if (ovl->manager && ovl->manager->get_manager_info &&
1780 ovl->manager->set_manager_info) {
1781
1782 ovl->manager->get_manager_info(ovl->manager, &info);
1783 info.trans_enabled = enable;
1784 info.trans_key_type = key_type;
1785 info.trans_key = vout->win.chromakey;
1786
1787 if (ovl->manager->set_manager_info(ovl->manager, &info))
1788 return -EINVAL;
1789 }
1790 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1791 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1792 enable = 1;
1793 } else {
1794 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1795 enable = 0;
1796 }
1797 if (ovl->manager && ovl->manager->get_manager_info &&
1798 ovl->manager->set_manager_info) {
1799 ovl->manager->get_manager_info(ovl->manager, &info);
Archit Taneja11354dd2011-09-26 11:47:29 +05301800 /* enable this only if there is no zorder cap */
1801 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1802 info.partial_alpha_enabled = enable;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001803 if (ovl->manager->set_manager_info(ovl->manager, &info))
1804 return -EINVAL;
1805 }
1806
1807 return 0;
1808}
1809
1810static int vidioc_g_fbuf(struct file *file, void *fh,
1811 struct v4l2_framebuffer *a)
1812{
1813 struct omap_overlay *ovl;
1814 struct omapvideo_info *ovid;
1815 struct omap_vout_device *vout = fh;
1816 struct omap_overlay_manager_info info;
1817
1818 ovid = &vout->vid_info;
1819 ovl = ovid->overlays[0];
1820
Hans Verkuil047a01f2011-11-22 08:23:19 -03001821 /* The video overlay must stay within the framebuffer and can't be
1822 positioned independently. */
1823 a->flags = V4L2_FBUF_FLAG_OVERLAY;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001824 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1825 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1826
1827 if (ovl->manager && ovl->manager->get_manager_info) {
1828 ovl->manager->get_manager_info(ovl->manager, &info);
1829 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1830 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1831 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1832 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1833 }
1834 if (ovl->manager && ovl->manager->get_manager_info) {
1835 ovl->manager->get_manager_info(ovl->manager, &info);
Archit Taneja11354dd2011-09-26 11:47:29 +05301836 if (info.partial_alpha_enabled)
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001837 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1838 }
1839
1840 return 0;
1841}
1842
1843static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1844 .vidioc_querycap = vidioc_querycap,
1845 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1846 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1847 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1848 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1849 .vidioc_queryctrl = vidioc_queryctrl,
1850 .vidioc_g_ctrl = vidioc_g_ctrl,
1851 .vidioc_s_fbuf = vidioc_s_fbuf,
1852 .vidioc_g_fbuf = vidioc_g_fbuf,
1853 .vidioc_s_ctrl = vidioc_s_ctrl,
Laurent Pinchart7b2607c2012-11-13 10:00:22 -03001854 .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_overlay,
1855 .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_overlay,
1856 .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_overlay,
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001857 .vidioc_cropcap = vidioc_cropcap,
1858 .vidioc_g_crop = vidioc_g_crop,
1859 .vidioc_s_crop = vidioc_s_crop,
1860 .vidioc_reqbufs = vidioc_reqbufs,
1861 .vidioc_querybuf = vidioc_querybuf,
1862 .vidioc_qbuf = vidioc_qbuf,
1863 .vidioc_dqbuf = vidioc_dqbuf,
1864 .vidioc_streamon = vidioc_streamon,
1865 .vidioc_streamoff = vidioc_streamoff,
1866};
1867
1868static const struct v4l2_file_operations omap_vout_fops = {
1869 .owner = THIS_MODULE,
Laurent Pinchart94f3f482011-01-20 21:15:42 -03001870 .poll = omap_vout_poll,
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001871 .unlocked_ioctl = video_ioctl2,
1872 .mmap = omap_vout_mmap,
1873 .open = omap_vout_open,
1874 .release = omap_vout_release,
1875};
1876
1877/* Init functions used during driver initialization */
1878/* Initial setup of video_data */
1879static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1880{
1881 struct video_device *vfd;
1882 struct v4l2_pix_format *pix;
1883 struct v4l2_control *control;
Linus Torvalds5f769452012-10-12 10:21:02 +09001884 struct omap_overlay *ovl = vout->vid_info.overlays[0];
1885 struct omap_dss_device *display = ovl->get_device(ovl);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001886
1887 /* set the default pix */
1888 pix = &vout->pix;
1889
1890 /* Set the default picture of QVGA */
1891 pix->width = QQVGA_WIDTH;
1892 pix->height = QQVGA_HEIGHT;
1893
1894 /* Default pixel format is RGB 5-6-5 */
1895 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1896 pix->field = V4L2_FIELD_ANY;
1897 pix->bytesperline = pix->width * 2;
1898 pix->sizeimage = pix->bytesperline * pix->height;
1899 pix->priv = 0;
1900 pix->colorspace = V4L2_COLORSPACE_JPEG;
1901
1902 vout->bpp = RGB565_BPP;
1903 vout->fbuf.fmt.width = display->panel.timings.x_res;
1904 vout->fbuf.fmt.height = display->panel.timings.y_res;
1905
1906 /* Set the data structures for the overlay parameters*/
1907 vout->win.global_alpha = 255;
1908 vout->fbuf.flags = 0;
1909 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1910 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1911 vout->win.chromakey = 0;
1912
1913 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1914
1915 /*Initialize the control variables for
1916 rotation, flipping and background color. */
1917 control = vout->control;
1918 control[0].id = V4L2_CID_ROTATE;
1919 control[0].value = 0;
1920 vout->rotation = 0;
1921 vout->mirror = 0;
1922 vout->control[2].id = V4L2_CID_HFLIP;
1923 vout->control[2].value = 0;
archit taneja445e2582011-06-14 03:54:47 -03001924 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1925 vout->vrfb_bpp = 2;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001926
1927 control[1].id = V4L2_CID_BG_COLOR;
1928 control[1].value = 0;
1929
1930 /* initialize the video_device struct */
1931 vfd = vout->vfd = video_device_alloc();
1932
1933 if (!vfd) {
1934 printk(KERN_ERR VOUT_NAME ": could not allocate"
1935 " video device struct\n");
1936 return -ENOMEM;
1937 }
1938 vfd->release = video_device_release;
1939 vfd->ioctl_ops = &vout_ioctl_ops;
1940
1941 strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1942
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001943 vfd->fops = &omap_vout_fops;
1944 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03001945 vfd->vfl_dir = VFL_DIR_TX;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001946 mutex_init(&vout->lock);
1947
1948 vfd->minor = -1;
1949 return 0;
1950
1951}
1952
1953/* Setup video buffers */
1954static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1955 int vid_num)
1956{
1957 u32 numbuffers;
archit taneja445e2582011-06-14 03:54:47 -03001958 int ret = 0, i;
1959 struct omapvideo_info *ovid;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001960 struct omap_vout_device *vout;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001961 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1962 struct omap2video_device *vid_dev =
1963 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1964
1965 vout = vid_dev->vouts[vid_num];
archit taneja445e2582011-06-14 03:54:47 -03001966 ovid = &vout->vid_info;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001967
1968 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1969 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1970 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1971
1972 for (i = 0; i < numbuffers; i++) {
1973 vout->buf_virt_addr[i] =
1974 omap_vout_alloc_buffer(vout->buffer_size,
1975 (u32 *) &vout->buf_phy_addr[i]);
1976 if (!vout->buf_virt_addr[i]) {
1977 numbuffers = i;
1978 ret = -ENOMEM;
1979 goto free_buffers;
1980 }
1981 }
1982
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001983 vout->cropped_offset = 0;
1984
archit taneja445e2582011-06-14 03:54:47 -03001985 if (ovid->rotation_type == VOUT_ROT_VRFB) {
1986 int static_vrfb_allocation = (vid_num == 0) ?
1987 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1988 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1989 static_vrfb_allocation);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001990 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001991
archit taneja445e2582011-06-14 03:54:47 -03001992 return ret;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03001993
1994free_buffers:
1995 for (i = 0; i < numbuffers; i++) {
1996 omap_vout_free_buffer(vout->buf_virt_addr[i],
1997 vout->buffer_size);
1998 vout->buf_virt_addr[i] = 0;
1999 vout->buf_phy_addr[i] = 0;
2000 }
2001 return ret;
2002
2003}
2004
2005/* Create video out devices */
2006static int __init omap_vout_create_video_devices(struct platform_device *pdev)
2007{
2008 int ret = 0, k;
2009 struct omap_vout_device *vout;
2010 struct video_device *vfd = NULL;
2011 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2012 struct omap2video_device *vid_dev = container_of(v4l2_dev,
2013 struct omap2video_device, v4l2_dev);
2014
2015 for (k = 0; k < pdev->num_resources; k++) {
2016
Julia Lawall2ef17c92010-05-13 16:59:15 -03002017 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002018 if (!vout) {
2019 dev_err(&pdev->dev, ": could not allocate memory\n");
2020 return -ENOMEM;
2021 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002022
2023 vout->vid = k;
2024 vid_dev->vouts[k] = vout;
2025 vout->vid_dev = vid_dev;
2026 /* Select video2 if only 1 overlay is controlled by V4L2 */
2027 if (pdev->num_resources == 1)
2028 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2029 else
2030 /* Else select video1 and video2 one by one. */
2031 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2032 vout->vid_info.num_overlays = 1;
2033 vout->vid_info.id = k + 1;
2034
archit taneja445e2582011-06-14 03:54:47 -03002035 /* Set VRFB as rotation_type for omap2 and omap3 */
Tomi Valkeinen950e2fb2012-11-12 15:17:39 +02002036 if (omap_vout_dss_omap24xx() || omap_vout_dss_omap34xx())
archit taneja445e2582011-06-14 03:54:47 -03002037 vout->vid_info.rotation_type = VOUT_ROT_VRFB;
2038
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002039 /* Setup the default configuration for the video devices
2040 */
2041 if (omap_vout_setup_video_data(vout) != 0) {
2042 ret = -ENOMEM;
2043 goto error;
2044 }
2045
2046 /* Allocate default number of buffers for the video streaming
2047 * and reserve the VRFB space for rotation
2048 */
2049 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2050 ret = -ENOMEM;
2051 goto error1;
2052 }
2053
2054 /* Register the Video device with V4L2
2055 */
2056 vfd = vout->vfd;
Vaibhav Hiremath8f3a3072011-06-16 15:32:07 -03002057 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002058 dev_err(&pdev->dev, ": Could not register "
2059 "Video for Linux device\n");
2060 vfd->minor = -1;
2061 ret = -ENODEV;
2062 goto error2;
2063 }
2064 video_set_drvdata(vfd, vout);
2065
Archit Taneja32d640d2012-03-07 05:01:16 -03002066 dev_info(&pdev->dev, ": registered and initialized"
2067 " video device %d\n", vfd->minor);
2068 if (k == (pdev->num_resources - 1))
2069 return 0;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002070
Archit Taneja32d640d2012-03-07 05:01:16 -03002071 continue;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002072error2:
archit taneja445e2582011-06-14 03:54:47 -03002073 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2074 omap_vout_release_vrfb(vout);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002075 omap_vout_free_buffers(vout);
2076error1:
2077 video_device_release(vfd);
2078error:
2079 kfree(vout);
2080 return ret;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002081 }
2082
2083 return -ENODEV;
2084}
2085/* Driver functions */
2086static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2087{
2088 struct video_device *vfd;
archit taneja445e2582011-06-14 03:54:47 -03002089 struct omapvideo_info *ovid;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002090
2091 if (!vout)
2092 return;
2093
2094 vfd = vout->vfd;
archit taneja445e2582011-06-14 03:54:47 -03002095 ovid = &vout->vid_info;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002096 if (vfd) {
2097 if (!video_is_registered(vfd)) {
2098 /*
2099 * The device was never registered, so release the
2100 * video_device struct directly.
2101 */
2102 video_device_release(vfd);
2103 } else {
2104 /*
2105 * The unregister function will release the video_device
2106 * struct as well as unregistering it.
2107 */
2108 video_unregister_device(vfd);
2109 }
2110 }
archit taneja445e2582011-06-14 03:54:47 -03002111 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2112 omap_vout_release_vrfb(vout);
2113 /* Free the VRFB buffer if allocated
2114 * init time
2115 */
2116 if (vout->vrfb_static_allocation)
2117 omap_vout_free_vrfb_buffers(vout);
2118 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002119 omap_vout_free_buffers(vout);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002120
2121 kfree(vout);
2122}
2123
2124static int omap_vout_remove(struct platform_device *pdev)
2125{
2126 int k;
2127 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2128 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2129 omap2video_device, v4l2_dev);
2130
2131 v4l2_device_unregister(v4l2_dev);
2132 for (k = 0; k < pdev->num_resources; k++)
2133 omap_vout_cleanup_device(vid_dev->vouts[k]);
2134
2135 for (k = 0; k < vid_dev->num_displays; k++) {
2136 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
Vaibhav Hiremath5ba9bb02010-05-27 08:17:07 -03002137 vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002138
2139 omap_dss_put_device(vid_dev->displays[k]);
2140 }
2141 kfree(vid_dev);
2142 return 0;
2143}
2144
2145static int __init omap_vout_probe(struct platform_device *pdev)
2146{
2147 int ret = 0, i;
2148 struct omap_overlay *ovl;
2149 struct omap_dss_device *dssdev = NULL;
2150 struct omap_dss_device *def_display;
2151 struct omap2video_device *vid_dev = NULL;
2152
Tomi Valkeinena9ee9f02012-10-10 10:26:45 +03002153 ret = omapdss_compat_init();
2154 if (ret) {
2155 dev_err(&pdev->dev, "failed to init dss\n");
2156 return ret;
2157 }
2158
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002159 if (pdev->num_resources == 0) {
2160 dev_err(&pdev->dev, "probed for an unknown device\n");
Tomi Valkeinena9ee9f02012-10-10 10:26:45 +03002161 ret = -ENODEV;
2162 goto err_dss_init;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002163 }
2164
2165 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
Tomi Valkeinena9ee9f02012-10-10 10:26:45 +03002166 if (vid_dev == NULL) {
2167 ret = -ENOMEM;
2168 goto err_dss_init;
2169 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002170
2171 vid_dev->num_displays = 0;
2172 for_each_dss_dev(dssdev) {
2173 omap_dss_get_device(dssdev);
Tomi Valkeinen71c7a972011-11-14 04:28:59 -03002174
2175 if (!dssdev->driver) {
2176 dev_warn(&pdev->dev, "no driver for display: %s\n",
2177 dssdev->name);
2178 omap_dss_put_device(dssdev);
2179 continue;
2180 }
2181
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002182 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2183 }
2184
2185 if (vid_dev->num_displays == 0) {
2186 dev_err(&pdev->dev, "no displays\n");
2187 ret = -EINVAL;
2188 goto probe_err0;
2189 }
2190
2191 vid_dev->num_overlays = omap_dss_get_num_overlays();
2192 for (i = 0; i < vid_dev->num_overlays; i++)
2193 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2194
2195 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2196 for (i = 0; i < vid_dev->num_managers; i++)
2197 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2198
2199 /* Get the Video1 overlay and video2 overlay.
2200 * Setup the Display attached to that overlays
2201 */
2202 for (i = 1; i < vid_dev->num_overlays; i++) {
2203 ovl = omap_dss_get_overlay(i);
Linus Torvalds5f769452012-10-12 10:21:02 +09002204 dssdev = ovl->get_device(ovl);
2205
2206 if (dssdev) {
2207 def_display = dssdev;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002208 } else {
2209 dev_warn(&pdev->dev, "cannot find display\n");
2210 def_display = NULL;
2211 }
2212 if (def_display) {
Vaibhav Hiremath5ba9bb02010-05-27 08:17:07 -03002213 struct omap_dss_driver *dssdrv = def_display->driver;
2214
2215 ret = dssdrv->enable(def_display);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002216 if (ret) {
2217 /* Here we are not considering a error
2218 * as display may be enabled by frame
2219 * buffer driver
2220 */
2221 dev_warn(&pdev->dev,
2222 "'%s' Display already enabled\n",
2223 def_display->name);
2224 }
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002225 }
2226 }
2227
2228 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2229 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2230 ret = -ENODEV;
2231 goto probe_err1;
2232 }
2233
2234 ret = omap_vout_create_video_devices(pdev);
2235 if (ret)
2236 goto probe_err2;
2237
2238 for (i = 0; i < vid_dev->num_displays; i++) {
2239 struct omap_dss_device *display = vid_dev->displays[i];
2240
Vaibhav Hiremath5ba9bb02010-05-27 08:17:07 -03002241 if (display->driver->update)
2242 display->driver->update(display, 0, 0,
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002243 display->panel.timings.x_res,
2244 display->panel.timings.y_res);
2245 }
2246 return 0;
2247
2248probe_err2:
2249 v4l2_device_unregister(&vid_dev->v4l2_dev);
2250probe_err1:
2251 for (i = 1; i < vid_dev->num_overlays; i++) {
2252 def_display = NULL;
2253 ovl = omap_dss_get_overlay(i);
Linus Torvalds5f769452012-10-12 10:21:02 +09002254 dssdev = ovl->get_device(ovl);
2255
2256 if (dssdev)
2257 def_display = dssdev;
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002258
Vaibhav Hiremath5ba9bb02010-05-27 08:17:07 -03002259 if (def_display && def_display->driver)
2260 def_display->driver->disable(def_display);
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002261 }
2262probe_err0:
2263 kfree(vid_dev);
Tomi Valkeinena9ee9f02012-10-10 10:26:45 +03002264err_dss_init:
2265 omapdss_compat_uninit();
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002266 return ret;
2267}
2268
2269static struct platform_driver omap_vout_driver = {
2270 .driver = {
2271 .name = VOUT_NAME,
2272 },
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002273 .remove = omap_vout_remove,
2274};
2275
2276static int __init omap_vout_init(void)
2277{
Tomi Valkeinen93596ef2011-11-08 05:47:08 -03002278 if (platform_driver_probe(&omap_vout_driver, omap_vout_probe) != 0) {
Vaibhav Hiremath5c7ab632010-04-11 10:41:49 -03002279 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2280 return -EINVAL;
2281 }
2282 return 0;
2283}
2284
2285static void omap_vout_cleanup(void)
2286{
2287 platform_driver_unregister(&omap_vout_driver);
2288}
2289
2290late_initcall(omap_vout_init);
2291module_exit(omap_vout_cleanup);