Merge branch 'for-florian' of git://gitorious.org/linux-omap-dss2/linux into fbdev...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / video / omap / omap_vout.c
1 /*
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>
38 #include <linux/irq.h>
39 #include <linux/videodev2.h>
40 #include <linux/dma-mapping.h>
41
42 #include <media/videobuf-dma-contig.h>
43 #include <media/v4l2-device.h>
44 #include <media/v4l2-ioctl.h>
45
46 #include <plat/dma.h>
47 #include <plat/vrfb.h>
48 #include <video/omapdss.h>
49
50 #include "omap_voutlib.h"
51 #include "omap_voutdef.h"
52 #include "omap_vout_vrfb.h"
53
54 MODULE_AUTHOR("Texas Instruments");
55 MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
56 MODULE_LICENSE("GPL");
57
58 /* Driver Configuration macros */
59 #define VOUT_NAME               "omap_vout"
60
61 enum omap_vout_channels {
62         OMAP_VIDEO1,
63         OMAP_VIDEO2,
64 };
65
66 static struct videobuf_queue_ops video_vbq_ops;
67 /* Variables configurable through module params*/
68 static u32 video1_numbuffers = 3;
69 static u32 video2_numbuffers = 3;
70 static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
71 static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
72 static u32 vid1_static_vrfb_alloc;
73 static u32 vid2_static_vrfb_alloc;
74 static int debug;
75
76 /* Module parameters */
77 module_param(video1_numbuffers, uint, S_IRUGO);
78 MODULE_PARM_DESC(video1_numbuffers,
79         "Number of buffers to be allocated at init time for Video1 device.");
80
81 module_param(video2_numbuffers, uint, S_IRUGO);
82 MODULE_PARM_DESC(video2_numbuffers,
83         "Number of buffers to be allocated at init time for Video2 device.");
84
85 module_param(video1_bufsize, uint, S_IRUGO);
86 MODULE_PARM_DESC(video1_bufsize,
87         "Size of the buffer to be allocated for video1 device");
88
89 module_param(video2_bufsize, uint, S_IRUGO);
90 MODULE_PARM_DESC(video2_bufsize,
91         "Size of the buffer to be allocated for video2 device");
92
93 module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
94 MODULE_PARM_DESC(vid1_static_vrfb_alloc,
95         "Static allocation of the VRFB buffer for video1 device");
96
97 module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
98 MODULE_PARM_DESC(vid2_static_vrfb_alloc,
99         "Static allocation of the VRFB buffer for video2 device");
100
101 module_param(debug, bool, S_IRUGO);
102 MODULE_PARM_DESC(debug, "Debug level (0-1)");
103
104 /* list of image formats supported by OMAP2 video pipelines */
105 static const struct v4l2_fmtdesc omap_formats[] = {
106         {
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 /*
148  * Try format
149  */
150 static 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  */
202 static 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
208         vma = find_vma(mm, virtp);
209         /* For kernel direct-mapped memory, take the easy way */
210         if (virtp >= PAGE_OFFSET) {
211                 physp = virt_to_phys((void *) virtp);
212         } else if (vma && (vma->vm_flags & VM_IO) && vma->vm_pgoff) {
213                 /* this will catch, kernel-allocated, mmaped-to-usermode
214                    addresses */
215                 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
216         } else {
217                 /* otherwise, use get_user_pages() for general userland pages */
218                 int res, nr_pages = 1;
219                 struct page *pages;
220                 down_read(&current->mm->mmap_sem);
221
222                 res = get_user_pages(current, current->mm, virtp, nr_pages, 1,
223                                 0, &pages, NULL);
224                 up_read(&current->mm->mmap_sem);
225
226                 if (res == nr_pages) {
227                         physp =  __pa(page_address(&pages[0]) +
228                                         (virtp & ~PAGE_MASK));
229                 } else {
230                         printk(KERN_WARNING VOUT_NAME
231                                         "get_user_pages failed\n");
232                         return 0;
233                 }
234         }
235
236         return physp;
237 }
238
239 /*
240  * Free the V4L2 buffers
241  */
242 void omap_vout_free_buffers(struct omap_vout_device *vout)
243 {
244         int i, numbuffers;
245
246         /* Allocate memory for the buffers */
247         numbuffers = (vout->vid) ?  video2_numbuffers : video1_numbuffers;
248         vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
249
250         for (i = 0; i < numbuffers; i++) {
251                 omap_vout_free_buffer(vout->buf_virt_addr[i],
252                                 vout->buffer_size);
253                 vout->buf_phy_addr[i] = 0;
254                 vout->buf_virt_addr[i] = 0;
255         }
256 }
257
258 /*
259  * Convert V4L2 rotation to DSS rotation
260  *      V4L2 understand 0, 90, 180, 270.
261  *      Convert to 0, 1, 2 and 3 respectively for DSS
262  */
263 static int v4l2_rot_to_dss_rot(int v4l2_rotation,
264                         enum dss_rotation *rotation, bool mirror)
265 {
266         int ret = 0;
267
268         switch (v4l2_rotation) {
269         case 90:
270                 *rotation = dss_rotation_90_degree;
271                 break;
272         case 180:
273                 *rotation = dss_rotation_180_degree;
274                 break;
275         case 270:
276                 *rotation = dss_rotation_270_degree;
277                 break;
278         case 0:
279                 *rotation = dss_rotation_0_degree;
280                 break;
281         default:
282                 ret = -EINVAL;
283         }
284         return ret;
285 }
286
287 static int omap_vout_calculate_offset(struct omap_vout_device *vout)
288 {
289         struct omapvideo_info *ovid;
290         struct v4l2_rect *crop = &vout->crop;
291         struct v4l2_pix_format *pix = &vout->pix;
292         int *cropped_offset = &vout->cropped_offset;
293         int ps = 2, line_length = 0;
294
295         ovid = &vout->vid_info;
296
297         if (ovid->rotation_type == VOUT_ROT_VRFB) {
298                 omap_vout_calculate_vrfb_offset(vout);
299         } else {
300                 vout->line_length = line_length = pix->width;
301
302                 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
303                         V4L2_PIX_FMT_UYVY == pix->pixelformat)
304                         ps = 2;
305                 else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
306                         ps = 4;
307                 else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
308                         ps = 3;
309
310                 vout->ps = ps;
311
312                 *cropped_offset = (line_length * ps) *
313                         crop->top + crop->left * ps;
314         }
315
316         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
317                         __func__, vout->cropped_offset);
318
319         return 0;
320 }
321
322 /*
323  * Convert V4L2 pixel format to DSS pixel format
324  */
325 static int video_mode_to_dss_mode(struct omap_vout_device *vout)
326 {
327         struct omap_overlay *ovl;
328         struct omapvideo_info *ovid;
329         struct v4l2_pix_format *pix = &vout->pix;
330         enum omap_color_mode mode;
331
332         ovid = &vout->vid_info;
333         ovl = ovid->overlays[0];
334
335         switch (pix->pixelformat) {
336         case 0:
337                 break;
338         case V4L2_PIX_FMT_YUYV:
339                 mode = OMAP_DSS_COLOR_YUV2;
340                 break;
341         case V4L2_PIX_FMT_UYVY:
342                 mode = OMAP_DSS_COLOR_UYVY;
343                 break;
344         case V4L2_PIX_FMT_RGB565:
345                 mode = OMAP_DSS_COLOR_RGB16;
346                 break;
347         case V4L2_PIX_FMT_RGB24:
348                 mode = OMAP_DSS_COLOR_RGB24P;
349                 break;
350         case V4L2_PIX_FMT_RGB32:
351                 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
352                         OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
353                 break;
354         case V4L2_PIX_FMT_BGR32:
355                 mode = OMAP_DSS_COLOR_RGBX32;
356                 break;
357         default:
358                 mode = -EINVAL;
359         }
360         return mode;
361 }
362
363 /*
364  * Setup the overlay
365  */
366 static int omapvid_setup_overlay(struct omap_vout_device *vout,
367                 struct omap_overlay *ovl, int posx, int posy, int outw,
368                 int outh, u32 addr)
369 {
370         int ret = 0;
371         struct omap_overlay_info info;
372         int cropheight, cropwidth, pixheight, pixwidth;
373
374         if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
375                         (outw != vout->pix.width || outh != vout->pix.height)) {
376                 ret = -EINVAL;
377                 goto setup_ovl_err;
378         }
379
380         vout->dss_mode = video_mode_to_dss_mode(vout);
381         if (vout->dss_mode == -EINVAL) {
382                 ret = -EINVAL;
383                 goto setup_ovl_err;
384         }
385
386         /* Setup the input plane parameters according to
387          * rotation value selected.
388          */
389         if (is_rotation_90_or_270(vout)) {
390                 cropheight = vout->crop.width;
391                 cropwidth = vout->crop.height;
392                 pixheight = vout->pix.width;
393                 pixwidth = vout->pix.height;
394         } else {
395                 cropheight = vout->crop.height;
396                 cropwidth = vout->crop.width;
397                 pixheight = vout->pix.height;
398                 pixwidth = vout->pix.width;
399         }
400
401         ovl->get_overlay_info(ovl, &info);
402         info.paddr = addr;
403         info.width = cropwidth;
404         info.height = cropheight;
405         info.color_mode = vout->dss_mode;
406         info.mirror = vout->mirror;
407         info.pos_x = posx;
408         info.pos_y = posy;
409         info.out_width = outw;
410         info.out_height = outh;
411         info.global_alpha = vout->win.global_alpha;
412         if (!is_rotation_enabled(vout)) {
413                 info.rotation = 0;
414                 info.rotation_type = OMAP_DSS_ROT_DMA;
415                 info.screen_width = pixwidth;
416         } else {
417                 info.rotation = vout->rotation;
418                 info.rotation_type = OMAP_DSS_ROT_VRFB;
419                 info.screen_width = 2048;
420         }
421
422         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
423                 "%s enable=%d addr=%x width=%d\n height=%d color_mode=%d\n"
424                 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
425                 "out_height=%d rotation_type=%d screen_width=%d\n",
426                 __func__, ovl->is_enabled(ovl), info.paddr, info.width, info.height,
427                 info.color_mode, info.rotation, info.mirror, info.pos_x,
428                 info.pos_y, info.out_width, info.out_height, info.rotation_type,
429                 info.screen_width);
430
431         ret = ovl->set_overlay_info(ovl, &info);
432         if (ret)
433                 goto setup_ovl_err;
434
435         return 0;
436
437 setup_ovl_err:
438         v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
439         return ret;
440 }
441
442 /*
443  * Initialize the overlay structure
444  */
445 static int omapvid_init(struct omap_vout_device *vout, u32 addr)
446 {
447         int ret = 0, i;
448         struct v4l2_window *win;
449         struct omap_overlay *ovl;
450         int posx, posy, outw, outh, temp;
451         struct omap_video_timings *timing;
452         struct omapvideo_info *ovid = &vout->vid_info;
453
454         win = &vout->win;
455         for (i = 0; i < ovid->num_overlays; i++) {
456                 ovl = ovid->overlays[i];
457                 if (!ovl->manager || !ovl->manager->device)
458                         return -EINVAL;
459
460                 timing = &ovl->manager->device->panel.timings;
461
462                 outw = win->w.width;
463                 outh = win->w.height;
464                 switch (vout->rotation) {
465                 case dss_rotation_90_degree:
466                         /* Invert the height and width for 90
467                          * and 270 degree rotation
468                          */
469                         temp = outw;
470                         outw = outh;
471                         outh = temp;
472                         posy = (timing->y_res - win->w.width) - win->w.left;
473                         posx = win->w.top;
474                         break;
475
476                 case dss_rotation_180_degree:
477                         posx = (timing->x_res - win->w.width) - win->w.left;
478                         posy = (timing->y_res - win->w.height) - win->w.top;
479                         break;
480
481                 case dss_rotation_270_degree:
482                         temp = outw;
483                         outw = outh;
484                         outh = temp;
485                         posy = win->w.left;
486                         posx = (timing->x_res - win->w.height) - win->w.top;
487                         break;
488
489                 default:
490                         posx = win->w.left;
491                         posy = win->w.top;
492                         break;
493                 }
494
495                 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
496                                 outw, outh, addr);
497                 if (ret)
498                         goto omapvid_init_err;
499         }
500         return 0;
501
502 omapvid_init_err:
503         v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
504         return ret;
505 }
506
507 /*
508  * Apply the changes set the go bit of DSS
509  */
510 static int omapvid_apply_changes(struct omap_vout_device *vout)
511 {
512         int i;
513         struct omap_overlay *ovl;
514         struct omapvideo_info *ovid = &vout->vid_info;
515
516         for (i = 0; i < ovid->num_overlays; i++) {
517                 ovl = ovid->overlays[i];
518                 if (!ovl->manager || !ovl->manager->device)
519                         return -EINVAL;
520                 ovl->manager->apply(ovl->manager);
521         }
522
523         return 0;
524 }
525
526 static void omap_vout_isr(void *arg, unsigned int irqstatus)
527 {
528         int ret;
529         u32 addr, fid;
530         struct omap_overlay *ovl;
531         struct timeval timevalue;
532         struct omapvideo_info *ovid;
533         struct omap_dss_device *cur_display;
534         struct omap_vout_device *vout = (struct omap_vout_device *)arg;
535
536         if (!vout->streaming)
537                 return;
538
539         ovid = &vout->vid_info;
540         ovl = ovid->overlays[0];
541         /* get the display device attached to the overlay */
542         if (!ovl->manager || !ovl->manager->device)
543                 return;
544
545         cur_display = ovl->manager->device;
546
547         spin_lock(&vout->vbq_lock);
548         do_gettimeofday(&timevalue);
549
550         if (cur_display->type != OMAP_DISPLAY_TYPE_VENC) {
551                 switch (cur_display->type) {
552                 case OMAP_DISPLAY_TYPE_DPI:
553                         if (!(irqstatus & (DISPC_IRQ_VSYNC | DISPC_IRQ_VSYNC2)))
554                                 goto vout_isr_err;
555                         break;
556                 case OMAP_DISPLAY_TYPE_HDMI:
557                         if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
558                                 goto vout_isr_err;
559                         break;
560                 default:
561                         goto vout_isr_err;
562                 }
563                 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
564                         vout->cur_frm->ts = timevalue;
565                         vout->cur_frm->state = VIDEOBUF_DONE;
566                         wake_up_interruptible(&vout->cur_frm->done);
567                         vout->cur_frm = vout->next_frm;
568                 }
569                 vout->first_int = 0;
570                 if (list_empty(&vout->dma_queue))
571                         goto vout_isr_err;
572
573                 vout->next_frm = list_entry(vout->dma_queue.next,
574                                 struct videobuf_buffer, queue);
575                 list_del(&vout->next_frm->queue);
576
577                 vout->next_frm->state = VIDEOBUF_ACTIVE;
578
579                 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
580                         + vout->cropped_offset;
581
582                 /* First save the configuration in ovelray structure */
583                 ret = omapvid_init(vout, addr);
584                 if (ret)
585                         printk(KERN_ERR VOUT_NAME
586                                 "failed to set overlay info\n");
587                 /* Enable the pipeline and set the Go bit */
588                 ret = omapvid_apply_changes(vout);
589                 if (ret)
590                         printk(KERN_ERR VOUT_NAME "failed to change mode\n");
591         } else {
592
593                 if (vout->first_int) {
594                         vout->first_int = 0;
595                         goto vout_isr_err;
596                 }
597                 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
598                         fid = 1;
599                 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
600                         fid = 0;
601                 else
602                         goto vout_isr_err;
603
604                 vout->field_id ^= 1;
605                 if (fid != vout->field_id) {
606                         if (0 == fid)
607                                 vout->field_id = fid;
608
609                         goto vout_isr_err;
610                 }
611                 if (0 == fid) {
612                         if (vout->cur_frm == vout->next_frm)
613                                 goto vout_isr_err;
614
615                         vout->cur_frm->ts = timevalue;
616                         vout->cur_frm->state = VIDEOBUF_DONE;
617                         wake_up_interruptible(&vout->cur_frm->done);
618                         vout->cur_frm = vout->next_frm;
619                 } else if (1 == fid) {
620                         if (list_empty(&vout->dma_queue) ||
621                                         (vout->cur_frm != vout->next_frm))
622                                 goto vout_isr_err;
623
624                         vout->next_frm = list_entry(vout->dma_queue.next,
625                                         struct videobuf_buffer, queue);
626                         list_del(&vout->next_frm->queue);
627
628                         vout->next_frm->state = VIDEOBUF_ACTIVE;
629                         addr = (unsigned long)
630                                 vout->queued_buf_addr[vout->next_frm->i] +
631                                 vout->cropped_offset;
632                         /* First save the configuration in ovelray structure */
633                         ret = omapvid_init(vout, addr);
634                         if (ret)
635                                 printk(KERN_ERR VOUT_NAME
636                                                 "failed to set overlay info\n");
637                         /* Enable the pipeline and set the Go bit */
638                         ret = omapvid_apply_changes(vout);
639                         if (ret)
640                                 printk(KERN_ERR VOUT_NAME
641                                                 "failed to change mode\n");
642                 }
643
644         }
645
646 vout_isr_err:
647         spin_unlock(&vout->vbq_lock);
648 }
649
650
651 /* Video buffer call backs */
652
653 /*
654  * Buffer setup function is called by videobuf layer when REQBUF ioctl is
655  * called. This is used to setup buffers and return size and count of
656  * buffers allocated. After the call to this buffer, videobuf layer will
657  * setup buffer queue depending on the size and count of buffers
658  */
659 static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
660                           unsigned int *size)
661 {
662         int startindex = 0, i, j;
663         u32 phy_addr = 0, virt_addr = 0;
664         struct omap_vout_device *vout = q->priv_data;
665         struct omapvideo_info *ovid = &vout->vid_info;
666
667         if (!vout)
668                 return -EINVAL;
669
670         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
671                 return -EINVAL;
672
673         startindex = (vout->vid == OMAP_VIDEO1) ?
674                 video1_numbuffers : video2_numbuffers;
675         if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
676                 *count = startindex;
677
678         if (ovid->rotation_type == VOUT_ROT_VRFB) {
679                 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
680                         return -ENOMEM;
681         }
682
683         if (V4L2_MEMORY_MMAP != vout->memory)
684                 return 0;
685
686         /* Now allocated the V4L2 buffers */
687         *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
688         startindex = (vout->vid == OMAP_VIDEO1) ?
689                 video1_numbuffers : video2_numbuffers;
690
691         /* Check the size of the buffer */
692         if (*size > vout->buffer_size) {
693                 v4l2_err(&vout->vid_dev->v4l2_dev,
694                                 "buffer allocation mismatch [%u] [%u]\n",
695                                 *size, vout->buffer_size);
696                 return -ENOMEM;
697         }
698
699         for (i = startindex; i < *count; i++) {
700                 vout->buffer_size = *size;
701
702                 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
703                                 &phy_addr);
704                 if (!virt_addr) {
705                         if (ovid->rotation_type == VOUT_ROT_NONE) {
706                                 break;
707                         } else {
708                                 if (!is_rotation_enabled(vout))
709                                         break;
710                         /* Free the VRFB buffers if no space for V4L2 buffers */
711                         for (j = i; j < *count; j++) {
712                                 omap_vout_free_buffer(
713                                                 vout->smsshado_virt_addr[j],
714                                                 vout->smsshado_size);
715                                 vout->smsshado_virt_addr[j] = 0;
716                                 vout->smsshado_phy_addr[j] = 0;
717                                 }
718                         }
719                 }
720                 vout->buf_virt_addr[i] = virt_addr;
721                 vout->buf_phy_addr[i] = phy_addr;
722         }
723         *count = vout->buffer_allocated = i;
724
725         return 0;
726 }
727
728 /*
729  * Free the V4L2 buffers additionally allocated than default
730  * number of buffers
731  */
732 static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
733 {
734         int num_buffers = 0, i;
735
736         num_buffers = (vout->vid == OMAP_VIDEO1) ?
737                 video1_numbuffers : video2_numbuffers;
738
739         for (i = num_buffers; i < vout->buffer_allocated; i++) {
740                 if (vout->buf_virt_addr[i])
741                         omap_vout_free_buffer(vout->buf_virt_addr[i],
742                                         vout->buffer_size);
743
744                 vout->buf_virt_addr[i] = 0;
745                 vout->buf_phy_addr[i] = 0;
746         }
747         vout->buffer_allocated = num_buffers;
748 }
749
750 /*
751  * This function will be called when VIDIOC_QBUF ioctl is called.
752  * It prepare buffers before give out for the display. This function
753  * converts user space virtual address into physical address if userptr memory
754  * exchange mechanism is used. If rotation is enabled, it copies entire
755  * buffer into VRFB memory space before giving it to the DSS.
756  */
757 static int omap_vout_buffer_prepare(struct videobuf_queue *q,
758                         struct videobuf_buffer *vb,
759                         enum v4l2_field field)
760 {
761         struct omap_vout_device *vout = q->priv_data;
762         struct omapvideo_info *ovid = &vout->vid_info;
763
764         if (VIDEOBUF_NEEDS_INIT == vb->state) {
765                 vb->width = vout->pix.width;
766                 vb->height = vout->pix.height;
767                 vb->size = vb->width * vb->height * vout->bpp;
768                 vb->field = field;
769         }
770         vb->state = VIDEOBUF_PREPARED;
771         /* if user pointer memory mechanism is used, get the physical
772          * address of the buffer
773          */
774         if (V4L2_MEMORY_USERPTR == vb->memory) {
775                 if (0 == vb->baddr)
776                         return -EINVAL;
777                 /* Physical address */
778                 vout->queued_buf_addr[vb->i] = (u8 *)
779                         omap_vout_uservirt_to_phys(vb->baddr);
780         } else {
781                 u32 addr, dma_addr;
782                 unsigned long size;
783
784                 addr = (unsigned long) vout->buf_virt_addr[vb->i];
785                 size = (unsigned long) vb->size;
786
787                 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
788                                 size, DMA_TO_DEVICE);
789                 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
790                         v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
791
792                 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
793         }
794
795         if (ovid->rotation_type == VOUT_ROT_VRFB)
796                 return omap_vout_prepare_vrfb(vout, vb);
797         else
798                 return 0;
799 }
800
801 /*
802  * Buffer queue function will be called from the videobuf layer when _QBUF
803  * ioctl is called. It is used to enqueue buffer, which is ready to be
804  * displayed.
805  */
806 static void omap_vout_buffer_queue(struct videobuf_queue *q,
807                           struct videobuf_buffer *vb)
808 {
809         struct omap_vout_device *vout = q->priv_data;
810
811         /* Driver is also maintainig a queue. So enqueue buffer in the driver
812          * queue */
813         list_add_tail(&vb->queue, &vout->dma_queue);
814
815         vb->state = VIDEOBUF_QUEUED;
816 }
817
818 /*
819  * Buffer release function is called from videobuf layer to release buffer
820  * which are already allocated
821  */
822 static void omap_vout_buffer_release(struct videobuf_queue *q,
823                             struct videobuf_buffer *vb)
824 {
825         struct omap_vout_device *vout = q->priv_data;
826
827         vb->state = VIDEOBUF_NEEDS_INIT;
828
829         if (V4L2_MEMORY_MMAP != vout->memory)
830                 return;
831 }
832
833 /*
834  *  File operations
835  */
836 static unsigned int omap_vout_poll(struct file *file,
837                                    struct poll_table_struct *wait)
838 {
839         struct omap_vout_device *vout = file->private_data;
840         struct videobuf_queue *q = &vout->vbq;
841
842         return videobuf_poll_stream(file, q, wait);
843 }
844
845 static void omap_vout_vm_open(struct vm_area_struct *vma)
846 {
847         struct omap_vout_device *vout = vma->vm_private_data;
848
849         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
850                 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
851         vout->mmap_count++;
852 }
853
854 static void omap_vout_vm_close(struct vm_area_struct *vma)
855 {
856         struct omap_vout_device *vout = vma->vm_private_data;
857
858         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
859                 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
860         vout->mmap_count--;
861 }
862
863 static struct vm_operations_struct omap_vout_vm_ops = {
864         .open   = omap_vout_vm_open,
865         .close  = omap_vout_vm_close,
866 };
867
868 static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
869 {
870         int i;
871         void *pos;
872         unsigned long start = vma->vm_start;
873         unsigned long size = (vma->vm_end - vma->vm_start);
874         struct omap_vout_device *vout = file->private_data;
875         struct videobuf_queue *q = &vout->vbq;
876
877         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
878                         " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
879                         vma->vm_pgoff, vma->vm_start, vma->vm_end);
880
881         /* look for the buffer to map */
882         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
883                 if (NULL == q->bufs[i])
884                         continue;
885                 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
886                         continue;
887                 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
888                         break;
889         }
890
891         if (VIDEO_MAX_FRAME == i) {
892                 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
893                                 "offset invalid [offset=0x%lx]\n",
894                                 (vma->vm_pgoff << PAGE_SHIFT));
895                 return -EINVAL;
896         }
897         /* Check the size of the buffer */
898         if (size > vout->buffer_size) {
899                 v4l2_err(&vout->vid_dev->v4l2_dev,
900                                 "insufficient memory [%lu] [%u]\n",
901                                 size, vout->buffer_size);
902                 return -ENOMEM;
903         }
904
905         q->bufs[i]->baddr = vma->vm_start;
906
907         vma->vm_flags |= VM_RESERVED;
908         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
909         vma->vm_ops = &omap_vout_vm_ops;
910         vma->vm_private_data = (void *) vout;
911         pos = (void *)vout->buf_virt_addr[i];
912         vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
913         while (size > 0) {
914                 unsigned long pfn;
915                 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
916                 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
917                         return -EAGAIN;
918                 start += PAGE_SIZE;
919                 pos += PAGE_SIZE;
920                 size -= PAGE_SIZE;
921         }
922         vout->mmap_count++;
923         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
924
925         return 0;
926 }
927
928 static int omap_vout_release(struct file *file)
929 {
930         unsigned int ret, i;
931         struct videobuf_queue *q;
932         struct omapvideo_info *ovid;
933         struct omap_vout_device *vout = file->private_data;
934
935         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
936         ovid = &vout->vid_info;
937
938         if (!vout)
939                 return 0;
940
941         q = &vout->vbq;
942         /* Disable all the overlay managers connected with this interface */
943         for (i = 0; i < ovid->num_overlays; i++) {
944                 struct omap_overlay *ovl = ovid->overlays[i];
945                 if (ovl->manager && ovl->manager->device)
946                         ovl->disable(ovl);
947         }
948         /* Turn off the pipeline */
949         ret = omapvid_apply_changes(vout);
950         if (ret)
951                 v4l2_warn(&vout->vid_dev->v4l2_dev,
952                                 "Unable to apply changes\n");
953
954         /* Free all buffers */
955         omap_vout_free_extra_buffers(vout);
956
957         /* Free the VRFB buffers only if they are allocated
958          * during reqbufs.  Don't free if init time allocated
959          */
960         if (ovid->rotation_type == VOUT_ROT_VRFB) {
961                 if (!vout->vrfb_static_allocation)
962                         omap_vout_free_vrfb_buffers(vout);
963         }
964         videobuf_mmap_free(q);
965
966         /* Even if apply changes fails we should continue
967            freeing allocated memory */
968         if (vout->streaming) {
969                 u32 mask = 0;
970
971                 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
972                         DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
973                 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
974                 vout->streaming = 0;
975
976                 videobuf_streamoff(q);
977                 videobuf_queue_cancel(q);
978         }
979
980         if (vout->mmap_count != 0)
981                 vout->mmap_count = 0;
982
983         vout->opened -= 1;
984         file->private_data = NULL;
985
986         if (vout->buffer_allocated)
987                 videobuf_mmap_free(q);
988
989         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
990         return ret;
991 }
992
993 static int omap_vout_open(struct file *file)
994 {
995         struct videobuf_queue *q;
996         struct omap_vout_device *vout = NULL;
997
998         vout = video_drvdata(file);
999         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1000
1001         if (vout == NULL)
1002                 return -ENODEV;
1003
1004         /* for now, we only support single open */
1005         if (vout->opened)
1006                 return -EBUSY;
1007
1008         vout->opened += 1;
1009
1010         file->private_data = vout;
1011         vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1012
1013         q = &vout->vbq;
1014         video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1015         video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1016         video_vbq_ops.buf_release = omap_vout_buffer_release;
1017         video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1018         spin_lock_init(&vout->vbq_lock);
1019
1020         videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1021                         &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
1022                         sizeof(struct videobuf_buffer), vout, NULL);
1023
1024         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1025         return 0;
1026 }
1027
1028 /*
1029  * V4L2 ioctls
1030  */
1031 static int vidioc_querycap(struct file *file, void *fh,
1032                 struct v4l2_capability *cap)
1033 {
1034         struct omap_vout_device *vout = fh;
1035
1036         strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1037         strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1038         cap->bus_info[0] = '\0';
1039         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
1040
1041         return 0;
1042 }
1043
1044 static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1045                         struct v4l2_fmtdesc *fmt)
1046 {
1047         int index = fmt->index;
1048
1049         if (index >= NUM_OUTPUT_FORMATS)
1050                 return -EINVAL;
1051
1052         fmt->flags = omap_formats[index].flags;
1053         strlcpy(fmt->description, omap_formats[index].description,
1054                         sizeof(fmt->description));
1055         fmt->pixelformat = omap_formats[index].pixelformat;
1056
1057         return 0;
1058 }
1059
1060 static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1061                         struct v4l2_format *f)
1062 {
1063         struct omap_vout_device *vout = fh;
1064
1065         f->fmt.pix = vout->pix;
1066         return 0;
1067
1068 }
1069
1070 static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1071                         struct v4l2_format *f)
1072 {
1073         struct omap_overlay *ovl;
1074         struct omapvideo_info *ovid;
1075         struct omap_video_timings *timing;
1076         struct omap_vout_device *vout = fh;
1077
1078         ovid = &vout->vid_info;
1079         ovl = ovid->overlays[0];
1080
1081         if (!ovl->manager || !ovl->manager->device)
1082                 return -EINVAL;
1083         /* get the display device attached to the overlay */
1084         timing = &ovl->manager->device->panel.timings;
1085
1086         vout->fbuf.fmt.height = timing->y_res;
1087         vout->fbuf.fmt.width = timing->x_res;
1088
1089         omap_vout_try_format(&f->fmt.pix);
1090         return 0;
1091 }
1092
1093 static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1094                         struct v4l2_format *f)
1095 {
1096         int ret, bpp;
1097         struct omap_overlay *ovl;
1098         struct omapvideo_info *ovid;
1099         struct omap_video_timings *timing;
1100         struct omap_vout_device *vout = fh;
1101
1102         if (vout->streaming)
1103                 return -EBUSY;
1104
1105         mutex_lock(&vout->lock);
1106
1107         ovid = &vout->vid_info;
1108         ovl = ovid->overlays[0];
1109
1110         /* get the display device attached to the overlay */
1111         if (!ovl->manager || !ovl->manager->device) {
1112                 ret = -EINVAL;
1113                 goto s_fmt_vid_out_exit;
1114         }
1115         timing = &ovl->manager->device->panel.timings;
1116
1117         /* We dont support RGB24-packed mode if vrfb rotation
1118          * is enabled*/
1119         if ((is_rotation_enabled(vout)) &&
1120                         f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1121                 ret = -EINVAL;
1122                 goto s_fmt_vid_out_exit;
1123         }
1124
1125         /* get the framebuffer parameters */
1126
1127         if (is_rotation_90_or_270(vout)) {
1128                 vout->fbuf.fmt.height = timing->x_res;
1129                 vout->fbuf.fmt.width = timing->y_res;
1130         } else {
1131                 vout->fbuf.fmt.height = timing->y_res;
1132                 vout->fbuf.fmt.width = timing->x_res;
1133         }
1134
1135         /* change to samller size is OK */
1136
1137         bpp = omap_vout_try_format(&f->fmt.pix);
1138         f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1139
1140         /* try & set the new output format */
1141         vout->bpp = bpp;
1142         vout->pix = f->fmt.pix;
1143         vout->vrfb_bpp = 1;
1144
1145         /* If YUYV then vrfb bpp is 2, for  others its 1 */
1146         if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1147                         V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1148                 vout->vrfb_bpp = 2;
1149
1150         /* set default crop and win */
1151         omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1152
1153         /* Save the changes in the overlay strcuture */
1154         ret = omapvid_init(vout, 0);
1155         if (ret) {
1156                 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1157                 goto s_fmt_vid_out_exit;
1158         }
1159
1160         ret = 0;
1161
1162 s_fmt_vid_out_exit:
1163         mutex_unlock(&vout->lock);
1164         return ret;
1165 }
1166
1167 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1168                         struct v4l2_format *f)
1169 {
1170         int ret = 0;
1171         struct omap_vout_device *vout = fh;
1172         struct omap_overlay *ovl;
1173         struct omapvideo_info *ovid;
1174         struct v4l2_window *win = &f->fmt.win;
1175
1176         ovid = &vout->vid_info;
1177         ovl = ovid->overlays[0];
1178
1179         ret = omap_vout_try_window(&vout->fbuf, win);
1180
1181         if (!ret) {
1182                 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1183                         win->global_alpha = 255;
1184                 else
1185                         win->global_alpha = f->fmt.win.global_alpha;
1186         }
1187
1188         return ret;
1189 }
1190
1191 static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1192                         struct v4l2_format *f)
1193 {
1194         int ret = 0;
1195         struct omap_overlay *ovl;
1196         struct omapvideo_info *ovid;
1197         struct omap_vout_device *vout = fh;
1198         struct v4l2_window *win = &f->fmt.win;
1199
1200         mutex_lock(&vout->lock);
1201         ovid = &vout->vid_info;
1202         ovl = ovid->overlays[0];
1203
1204         ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1205         if (!ret) {
1206                 /* Video1 plane does not support global alpha on OMAP3 */
1207                 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1208                         vout->win.global_alpha = 255;
1209                 else
1210                         vout->win.global_alpha = f->fmt.win.global_alpha;
1211
1212                 vout->win.chromakey = f->fmt.win.chromakey;
1213         }
1214         mutex_unlock(&vout->lock);
1215         return ret;
1216 }
1217
1218 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
1219                         struct v4l2_fmtdesc *fmt)
1220 {
1221         int index = fmt->index;
1222
1223         if (index >= NUM_OUTPUT_FORMATS)
1224                 return -EINVAL;
1225
1226         fmt->flags = omap_formats[index].flags;
1227         strlcpy(fmt->description, omap_formats[index].description,
1228                         sizeof(fmt->description));
1229         fmt->pixelformat = omap_formats[index].pixelformat;
1230         return 0;
1231 }
1232
1233 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1234                         struct v4l2_format *f)
1235 {
1236         u32 key_value =  0;
1237         struct omap_overlay *ovl;
1238         struct omapvideo_info *ovid;
1239         struct omap_vout_device *vout = fh;
1240         struct omap_overlay_manager_info info;
1241         struct v4l2_window *win = &f->fmt.win;
1242
1243         ovid = &vout->vid_info;
1244         ovl = ovid->overlays[0];
1245
1246         win->w = vout->win.w;
1247         win->field = vout->win.field;
1248         win->global_alpha = vout->win.global_alpha;
1249
1250         if (ovl->manager && ovl->manager->get_manager_info) {
1251                 ovl->manager->get_manager_info(ovl->manager, &info);
1252                 key_value = info.trans_key;
1253         }
1254         win->chromakey = key_value;
1255         return 0;
1256 }
1257
1258 static int vidioc_cropcap(struct file *file, void *fh,
1259                 struct v4l2_cropcap *cropcap)
1260 {
1261         struct omap_vout_device *vout = fh;
1262         struct v4l2_pix_format *pix = &vout->pix;
1263
1264         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1265                 return -EINVAL;
1266
1267         /* Width and height are always even */
1268         cropcap->bounds.width = pix->width & ~1;
1269         cropcap->bounds.height = pix->height & ~1;
1270
1271         omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1272         cropcap->pixelaspect.numerator = 1;
1273         cropcap->pixelaspect.denominator = 1;
1274         return 0;
1275 }
1276
1277 static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1278 {
1279         struct omap_vout_device *vout = fh;
1280
1281         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1282                 return -EINVAL;
1283         crop->c = vout->crop;
1284         return 0;
1285 }
1286
1287 static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1288 {
1289         int ret = -EINVAL;
1290         struct omap_vout_device *vout = fh;
1291         struct omapvideo_info *ovid;
1292         struct omap_overlay *ovl;
1293         struct omap_video_timings *timing;
1294
1295         if (vout->streaming)
1296                 return -EBUSY;
1297
1298         mutex_lock(&vout->lock);
1299         ovid = &vout->vid_info;
1300         ovl = ovid->overlays[0];
1301
1302         if (!ovl->manager || !ovl->manager->device) {
1303                 ret = -EINVAL;
1304                 goto s_crop_err;
1305         }
1306         /* get the display device attached to the overlay */
1307         timing = &ovl->manager->device->panel.timings;
1308
1309         if (is_rotation_90_or_270(vout)) {
1310                 vout->fbuf.fmt.height = timing->x_res;
1311                 vout->fbuf.fmt.width = timing->y_res;
1312         } else {
1313                 vout->fbuf.fmt.height = timing->y_res;
1314                 vout->fbuf.fmt.width = timing->x_res;
1315         }
1316
1317         if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1318                 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1319                                 &vout->fbuf, &crop->c);
1320
1321 s_crop_err:
1322         mutex_unlock(&vout->lock);
1323         return ret;
1324 }
1325
1326 static int vidioc_queryctrl(struct file *file, void *fh,
1327                 struct v4l2_queryctrl *ctrl)
1328 {
1329         int ret = 0;
1330
1331         switch (ctrl->id) {
1332         case V4L2_CID_ROTATE:
1333                 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1334                 break;
1335         case V4L2_CID_BG_COLOR:
1336                 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1337                 break;
1338         case V4L2_CID_VFLIP:
1339                 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1340                 break;
1341         default:
1342                 ctrl->name[0] = '\0';
1343                 ret = -EINVAL;
1344         }
1345         return ret;
1346 }
1347
1348 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1349 {
1350         int ret = 0;
1351         struct omap_vout_device *vout = fh;
1352
1353         switch (ctrl->id) {
1354         case V4L2_CID_ROTATE:
1355                 ctrl->value = vout->control[0].value;
1356                 break;
1357         case V4L2_CID_BG_COLOR:
1358         {
1359                 struct omap_overlay_manager_info info;
1360                 struct omap_overlay *ovl;
1361
1362                 ovl = vout->vid_info.overlays[0];
1363                 if (!ovl->manager || !ovl->manager->get_manager_info) {
1364                         ret = -EINVAL;
1365                         break;
1366                 }
1367
1368                 ovl->manager->get_manager_info(ovl->manager, &info);
1369                 ctrl->value = info.default_color;
1370                 break;
1371         }
1372         case V4L2_CID_VFLIP:
1373                 ctrl->value = vout->control[2].value;
1374                 break;
1375         default:
1376                 ret = -EINVAL;
1377         }
1378         return ret;
1379 }
1380
1381 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1382 {
1383         int ret = 0;
1384         struct omap_vout_device *vout = fh;
1385
1386         switch (a->id) {
1387         case V4L2_CID_ROTATE:
1388         {
1389                 struct omapvideo_info *ovid;
1390                 int rotation = a->value;
1391
1392                 ovid = &vout->vid_info;
1393
1394                 mutex_lock(&vout->lock);
1395                 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1396                         mutex_unlock(&vout->lock);
1397                         ret = -ERANGE;
1398                         break;
1399                 }
1400
1401                 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1402                         mutex_unlock(&vout->lock);
1403                         ret = -EINVAL;
1404                         break;
1405                 }
1406
1407                 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1408                                                         vout->mirror)) {
1409                         mutex_unlock(&vout->lock);
1410                         ret = -EINVAL;
1411                         break;
1412                 }
1413
1414                 vout->control[0].value = rotation;
1415                 mutex_unlock(&vout->lock);
1416                 break;
1417         }
1418         case V4L2_CID_BG_COLOR:
1419         {
1420                 struct omap_overlay *ovl;
1421                 unsigned int  color = a->value;
1422                 struct omap_overlay_manager_info info;
1423
1424                 ovl = vout->vid_info.overlays[0];
1425
1426                 mutex_lock(&vout->lock);
1427                 if (!ovl->manager || !ovl->manager->get_manager_info) {
1428                         mutex_unlock(&vout->lock);
1429                         ret = -EINVAL;
1430                         break;
1431                 }
1432
1433                 ovl->manager->get_manager_info(ovl->manager, &info);
1434                 info.default_color = color;
1435                 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1436                         mutex_unlock(&vout->lock);
1437                         ret = -EINVAL;
1438                         break;
1439                 }
1440
1441                 vout->control[1].value = color;
1442                 mutex_unlock(&vout->lock);
1443                 break;
1444         }
1445         case V4L2_CID_VFLIP:
1446         {
1447                 struct omap_overlay *ovl;
1448                 struct omapvideo_info *ovid;
1449                 unsigned int  mirror = a->value;
1450
1451                 ovid = &vout->vid_info;
1452                 ovl = ovid->overlays[0];
1453
1454                 mutex_lock(&vout->lock);
1455                 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1456                         mutex_unlock(&vout->lock);
1457                         ret = -ERANGE;
1458                         break;
1459                 }
1460
1461                 if (mirror  && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1462                         mutex_unlock(&vout->lock);
1463                         ret = -EINVAL;
1464                         break;
1465                 }
1466                 vout->mirror = mirror;
1467                 vout->control[2].value = mirror;
1468                 mutex_unlock(&vout->lock);
1469                 break;
1470         }
1471         default:
1472                 ret = -EINVAL;
1473         }
1474         return ret;
1475 }
1476
1477 static int vidioc_reqbufs(struct file *file, void *fh,
1478                         struct v4l2_requestbuffers *req)
1479 {
1480         int ret = 0;
1481         unsigned int i, num_buffers = 0;
1482         struct omap_vout_device *vout = fh;
1483         struct videobuf_queue *q = &vout->vbq;
1484
1485         if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
1486                 return -EINVAL;
1487         /* if memory is not mmp or userptr
1488            return error */
1489         if ((V4L2_MEMORY_MMAP != req->memory) &&
1490                         (V4L2_MEMORY_USERPTR != req->memory))
1491                 return -EINVAL;
1492
1493         mutex_lock(&vout->lock);
1494         /* Cannot be requested when streaming is on */
1495         if (vout->streaming) {
1496                 ret = -EBUSY;
1497                 goto reqbuf_err;
1498         }
1499
1500         /* If buffers are already allocated free them */
1501         if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1502                 if (vout->mmap_count) {
1503                         ret = -EBUSY;
1504                         goto reqbuf_err;
1505                 }
1506                 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1507                         video1_numbuffers : video2_numbuffers;
1508                 for (i = num_buffers; i < vout->buffer_allocated; i++) {
1509                         omap_vout_free_buffer(vout->buf_virt_addr[i],
1510                                         vout->buffer_size);
1511                         vout->buf_virt_addr[i] = 0;
1512                         vout->buf_phy_addr[i] = 0;
1513                 }
1514                 vout->buffer_allocated = num_buffers;
1515                 videobuf_mmap_free(q);
1516         } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1517                 if (vout->buffer_allocated) {
1518                         videobuf_mmap_free(q);
1519                         for (i = 0; i < vout->buffer_allocated; i++) {
1520                                 kfree(q->bufs[i]);
1521                                 q->bufs[i] = NULL;
1522                         }
1523                         vout->buffer_allocated = 0;
1524                 }
1525         }
1526
1527         /*store the memory type in data structure */
1528         vout->memory = req->memory;
1529
1530         INIT_LIST_HEAD(&vout->dma_queue);
1531
1532         /* call videobuf_reqbufs api */
1533         ret = videobuf_reqbufs(q, req);
1534         if (ret < 0)
1535                 goto reqbuf_err;
1536
1537         vout->buffer_allocated = req->count;
1538
1539 reqbuf_err:
1540         mutex_unlock(&vout->lock);
1541         return ret;
1542 }
1543
1544 static int vidioc_querybuf(struct file *file, void *fh,
1545                         struct v4l2_buffer *b)
1546 {
1547         struct omap_vout_device *vout = fh;
1548
1549         return videobuf_querybuf(&vout->vbq, b);
1550 }
1551
1552 static int vidioc_qbuf(struct file *file, void *fh,
1553                         struct v4l2_buffer *buffer)
1554 {
1555         struct omap_vout_device *vout = fh;
1556         struct videobuf_queue *q = &vout->vbq;
1557
1558         if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1559                         (buffer->index >= vout->buffer_allocated) ||
1560                         (q->bufs[buffer->index]->memory != buffer->memory)) {
1561                 return -EINVAL;
1562         }
1563         if (V4L2_MEMORY_USERPTR == buffer->memory) {
1564                 if ((buffer->length < vout->pix.sizeimage) ||
1565                                 (0 == buffer->m.userptr)) {
1566                         return -EINVAL;
1567                 }
1568         }
1569
1570         if ((is_rotation_enabled(vout)) &&
1571                         vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1572                 v4l2_warn(&vout->vid_dev->v4l2_dev,
1573                                 "DMA Channel not allocated for Rotation\n");
1574                 return -EINVAL;
1575         }
1576
1577         return videobuf_qbuf(q, buffer);
1578 }
1579
1580 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1581 {
1582         struct omap_vout_device *vout = fh;
1583         struct videobuf_queue *q = &vout->vbq;
1584
1585         int ret;
1586         u32 addr;
1587         unsigned long size;
1588         struct videobuf_buffer *vb;
1589
1590         vb = q->bufs[b->index];
1591
1592         if (!vout->streaming)
1593                 return -EINVAL;
1594
1595         if (file->f_flags & O_NONBLOCK)
1596                 /* Call videobuf_dqbuf for non blocking mode */
1597                 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
1598         else
1599                 /* Call videobuf_dqbuf for  blocking mode */
1600                 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
1601
1602         addr = (unsigned long) vout->buf_phy_addr[vb->i];
1603         size = (unsigned long) vb->size;
1604         dma_unmap_single(vout->vid_dev->v4l2_dev.dev,  addr,
1605                                 size, DMA_TO_DEVICE);
1606         return ret;
1607 }
1608
1609 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1610 {
1611         int ret = 0, j;
1612         u32 addr = 0, mask = 0;
1613         struct omap_vout_device *vout = fh;
1614         struct videobuf_queue *q = &vout->vbq;
1615         struct omapvideo_info *ovid = &vout->vid_info;
1616
1617         mutex_lock(&vout->lock);
1618
1619         if (vout->streaming) {
1620                 ret = -EBUSY;
1621                 goto streamon_err;
1622         }
1623
1624         ret = videobuf_streamon(q);
1625         if (ret)
1626                 goto streamon_err;
1627
1628         if (list_empty(&vout->dma_queue)) {
1629                 ret = -EIO;
1630                 goto streamon_err1;
1631         }
1632
1633         /* Get the next frame from the buffer queue */
1634         vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1635                         struct videobuf_buffer, queue);
1636         /* Remove buffer from the buffer queue */
1637         list_del(&vout->cur_frm->queue);
1638         /* Mark state of the current frame to active */
1639         vout->cur_frm->state = VIDEOBUF_ACTIVE;
1640         /* Initialize field_id and started member */
1641         vout->field_id = 0;
1642
1643         /* set flag here. Next QBUF will start DMA */
1644         vout->streaming = 1;
1645
1646         vout->first_int = 1;
1647
1648         if (omap_vout_calculate_offset(vout)) {
1649                 ret = -EINVAL;
1650                 goto streamon_err1;
1651         }
1652         addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1653                 + vout->cropped_offset;
1654
1655         mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1656                 | DISPC_IRQ_VSYNC2;
1657
1658         omap_dispc_register_isr(omap_vout_isr, vout, mask);
1659
1660         for (j = 0; j < ovid->num_overlays; j++) {
1661                 struct omap_overlay *ovl = ovid->overlays[j];
1662
1663                 if (ovl->manager && ovl->manager->device) {
1664                         struct omap_overlay_info info;
1665                         ovl->get_overlay_info(ovl, &info);
1666                         info.paddr = addr;
1667                         if (ovl->set_overlay_info(ovl, &info)) {
1668                                 ret = -EINVAL;
1669                                 goto streamon_err1;
1670                         }
1671                 }
1672         }
1673
1674         /* First save the configuration in ovelray structure */
1675         ret = omapvid_init(vout, addr);
1676         if (ret)
1677                 v4l2_err(&vout->vid_dev->v4l2_dev,
1678                                 "failed to set overlay info\n");
1679         /* Enable the pipeline and set the Go bit */
1680         ret = omapvid_apply_changes(vout);
1681         if (ret)
1682                 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1683
1684         for (j = 0; j < ovid->num_overlays; j++) {
1685                 struct omap_overlay *ovl = ovid->overlays[j];
1686
1687                 if (ovl->manager && ovl->manager->device) {
1688                         ret = ovl->enable(ovl);
1689                         if (ret)
1690                                 goto streamon_err1;
1691                 }
1692         }
1693
1694         ret = 0;
1695
1696 streamon_err1:
1697         if (ret)
1698                 ret = videobuf_streamoff(q);
1699 streamon_err:
1700         mutex_unlock(&vout->lock);
1701         return ret;
1702 }
1703
1704 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1705 {
1706         u32 mask = 0;
1707         int ret = 0, j;
1708         struct omap_vout_device *vout = fh;
1709         struct omapvideo_info *ovid = &vout->vid_info;
1710
1711         if (!vout->streaming)
1712                 return -EINVAL;
1713
1714         vout->streaming = 0;
1715         mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1716                 | DISPC_IRQ_VSYNC2;
1717
1718         omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1719
1720         for (j = 0; j < ovid->num_overlays; j++) {
1721                 struct omap_overlay *ovl = ovid->overlays[j];
1722
1723                 if (ovl->manager && ovl->manager->device)
1724                         ovl->disable(ovl);
1725         }
1726
1727         /* Turn of the pipeline */
1728         ret = omapvid_apply_changes(vout);
1729         if (ret)
1730                 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
1731                                 " streamoff\n");
1732
1733         INIT_LIST_HEAD(&vout->dma_queue);
1734         ret = videobuf_streamoff(&vout->vbq);
1735
1736         return ret;
1737 }
1738
1739 static int vidioc_s_fbuf(struct file *file, void *fh,
1740                                 struct v4l2_framebuffer *a)
1741 {
1742         int enable = 0;
1743         struct omap_overlay *ovl;
1744         struct omapvideo_info *ovid;
1745         struct omap_vout_device *vout = fh;
1746         struct omap_overlay_manager_info info;
1747         enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1748
1749         ovid = &vout->vid_info;
1750         ovl = ovid->overlays[0];
1751
1752         /* OMAP DSS doesn't support Source and Destination color
1753            key together */
1754         if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1755                         (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1756                 return -EINVAL;
1757         /* OMAP DSS Doesn't support the Destination color key
1758            and alpha blending together */
1759         if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1760                         (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1761                 return -EINVAL;
1762
1763         if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1764                 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1765                 key_type =  OMAP_DSS_COLOR_KEY_VID_SRC;
1766         } else
1767                 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1768
1769         if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1770                 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1771                 key_type =  OMAP_DSS_COLOR_KEY_GFX_DST;
1772         } else
1773                 vout->fbuf.flags &=  ~V4L2_FBUF_FLAG_CHROMAKEY;
1774
1775         if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1776                                 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1777                 enable = 1;
1778         else
1779                 enable = 0;
1780         if (ovl->manager && ovl->manager->get_manager_info &&
1781                         ovl->manager->set_manager_info) {
1782
1783                 ovl->manager->get_manager_info(ovl->manager, &info);
1784                 info.trans_enabled = enable;
1785                 info.trans_key_type = key_type;
1786                 info.trans_key = vout->win.chromakey;
1787
1788                 if (ovl->manager->set_manager_info(ovl->manager, &info))
1789                         return -EINVAL;
1790         }
1791         if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1792                 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1793                 enable = 1;
1794         } else {
1795                 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1796                 enable = 0;
1797         }
1798         if (ovl->manager && ovl->manager->get_manager_info &&
1799                         ovl->manager->set_manager_info) {
1800                 ovl->manager->get_manager_info(ovl->manager, &info);
1801                 /* enable this only if there is no zorder cap */
1802                 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1803                         info.partial_alpha_enabled = enable;
1804                 if (ovl->manager->set_manager_info(ovl->manager, &info))
1805                         return -EINVAL;
1806         }
1807
1808         return 0;
1809 }
1810
1811 static int vidioc_g_fbuf(struct file *file, void *fh,
1812                 struct v4l2_framebuffer *a)
1813 {
1814         struct omap_overlay *ovl;
1815         struct omapvideo_info *ovid;
1816         struct omap_vout_device *vout = fh;
1817         struct omap_overlay_manager_info info;
1818
1819         ovid = &vout->vid_info;
1820         ovl = ovid->overlays[0];
1821
1822         a->flags = 0x0;
1823         a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1824                 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1825
1826         if (ovl->manager && ovl->manager->get_manager_info) {
1827                 ovl->manager->get_manager_info(ovl->manager, &info);
1828                 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1829                         a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1830                 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1831                         a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1832         }
1833         if (ovl->manager && ovl->manager->get_manager_info) {
1834                 ovl->manager->get_manager_info(ovl->manager, &info);
1835                 if (info.partial_alpha_enabled)
1836                         a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1837         }
1838
1839         return 0;
1840 }
1841
1842 static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1843         .vidioc_querycap                        = vidioc_querycap,
1844         .vidioc_enum_fmt_vid_out                = vidioc_enum_fmt_vid_out,
1845         .vidioc_g_fmt_vid_out                   = vidioc_g_fmt_vid_out,
1846         .vidioc_try_fmt_vid_out                 = vidioc_try_fmt_vid_out,
1847         .vidioc_s_fmt_vid_out                   = vidioc_s_fmt_vid_out,
1848         .vidioc_queryctrl                       = vidioc_queryctrl,
1849         .vidioc_g_ctrl                          = vidioc_g_ctrl,
1850         .vidioc_s_fbuf                          = vidioc_s_fbuf,
1851         .vidioc_g_fbuf                          = vidioc_g_fbuf,
1852         .vidioc_s_ctrl                          = vidioc_s_ctrl,
1853         .vidioc_try_fmt_vid_overlay             = vidioc_try_fmt_vid_overlay,
1854         .vidioc_s_fmt_vid_overlay               = vidioc_s_fmt_vid_overlay,
1855         .vidioc_enum_fmt_vid_overlay            = vidioc_enum_fmt_vid_overlay,
1856         .vidioc_g_fmt_vid_overlay               = vidioc_g_fmt_vid_overlay,
1857         .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
1868 static const struct v4l2_file_operations omap_vout_fops = {
1869         .owner          = THIS_MODULE,
1870         .poll           = omap_vout_poll,
1871         .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 */
1879 static 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;
1884         struct omap_dss_device *display =
1885                 vout->vid_info.overlays[0]->manager->device;
1886
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;
1924         if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1925                 vout->vrfb_bpp = 2;
1926
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
1943         vfd->fops = &omap_vout_fops;
1944         vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
1945         mutex_init(&vout->lock);
1946
1947         vfd->minor = -1;
1948         return 0;
1949
1950 }
1951
1952 /* Setup video buffers */
1953 static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1954                 int vid_num)
1955 {
1956         u32 numbuffers;
1957         int ret = 0, i;
1958         struct omapvideo_info *ovid;
1959         struct omap_vout_device *vout;
1960         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1961         struct omap2video_device *vid_dev =
1962                 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1963
1964         vout = vid_dev->vouts[vid_num];
1965         ovid = &vout->vid_info;
1966
1967         numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1968         vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1969         dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1970
1971         for (i = 0; i < numbuffers; i++) {
1972                 vout->buf_virt_addr[i] =
1973                         omap_vout_alloc_buffer(vout->buffer_size,
1974                                         (u32 *) &vout->buf_phy_addr[i]);
1975                 if (!vout->buf_virt_addr[i]) {
1976                         numbuffers = i;
1977                         ret = -ENOMEM;
1978                         goto free_buffers;
1979                 }
1980         }
1981
1982         vout->cropped_offset = 0;
1983
1984         if (ovid->rotation_type == VOUT_ROT_VRFB) {
1985                 int static_vrfb_allocation = (vid_num == 0) ?
1986                         vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1987                 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1988                                 static_vrfb_allocation);
1989         }
1990
1991         return ret;
1992
1993 free_buffers:
1994         for (i = 0; i < numbuffers; i++) {
1995                 omap_vout_free_buffer(vout->buf_virt_addr[i],
1996                                                 vout->buffer_size);
1997                 vout->buf_virt_addr[i] = 0;
1998                 vout->buf_phy_addr[i] = 0;
1999         }
2000         return ret;
2001
2002 }
2003
2004 /* Create video out devices */
2005 static int __init omap_vout_create_video_devices(struct platform_device *pdev)
2006 {
2007         int ret = 0, k;
2008         struct omap_vout_device *vout;
2009         struct video_device *vfd = NULL;
2010         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2011         struct omap2video_device *vid_dev = container_of(v4l2_dev,
2012                         struct omap2video_device, v4l2_dev);
2013
2014         for (k = 0; k < pdev->num_resources; k++) {
2015
2016                 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
2017                 if (!vout) {
2018                         dev_err(&pdev->dev, ": could not allocate memory\n");
2019                         return -ENOMEM;
2020                 }
2021
2022                 vout->vid = k;
2023                 vid_dev->vouts[k] = vout;
2024                 vout->vid_dev = vid_dev;
2025                 /* Select video2 if only 1 overlay is controlled by V4L2 */
2026                 if (pdev->num_resources == 1)
2027                         vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2028                 else
2029                         /* Else select video1 and video2 one by one. */
2030                         vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2031                 vout->vid_info.num_overlays = 1;
2032                 vout->vid_info.id = k + 1;
2033
2034                 /* Set VRFB as rotation_type for omap2 and omap3 */
2035                 if (cpu_is_omap24xx() || cpu_is_omap34xx())
2036                         vout->vid_info.rotation_type = VOUT_ROT_VRFB;
2037
2038                 /* Setup the default configuration for the video devices
2039                  */
2040                 if (omap_vout_setup_video_data(vout) != 0) {
2041                         ret = -ENOMEM;
2042                         goto error;
2043                 }
2044
2045                 /* Allocate default number of buffers for the video streaming
2046                  * and reserve the VRFB space for rotation
2047                  */
2048                 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2049                         ret = -ENOMEM;
2050                         goto error1;
2051                 }
2052
2053                 /* Register the Video device with V4L2
2054                  */
2055                 vfd = vout->vfd;
2056                 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
2057                         dev_err(&pdev->dev, ": Could not register "
2058                                         "Video for Linux device\n");
2059                         vfd->minor = -1;
2060                         ret = -ENODEV;
2061                         goto error2;
2062                 }
2063                 video_set_drvdata(vfd, vout);
2064
2065                 /* Configure the overlay structure */
2066                 ret = omapvid_init(vid_dev->vouts[k], 0);
2067                 if (!ret)
2068                         goto success;
2069
2070 error2:
2071                 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2072                         omap_vout_release_vrfb(vout);
2073                 omap_vout_free_buffers(vout);
2074 error1:
2075                 video_device_release(vfd);
2076 error:
2077                 kfree(vout);
2078                 return ret;
2079
2080 success:
2081                 dev_info(&pdev->dev, ": registered and initialized"
2082                                 " video device %d\n", vfd->minor);
2083                 if (k == (pdev->num_resources - 1))
2084                         return 0;
2085         }
2086
2087         return -ENODEV;
2088 }
2089 /* Driver functions */
2090 static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2091 {
2092         struct video_device *vfd;
2093         struct omapvideo_info *ovid;
2094
2095         if (!vout)
2096                 return;
2097
2098         vfd = vout->vfd;
2099         ovid = &vout->vid_info;
2100         if (vfd) {
2101                 if (!video_is_registered(vfd)) {
2102                         /*
2103                          * The device was never registered, so release the
2104                          * video_device struct directly.
2105                          */
2106                         video_device_release(vfd);
2107                 } else {
2108                         /*
2109                          * The unregister function will release the video_device
2110                          * struct as well as unregistering it.
2111                          */
2112                         video_unregister_device(vfd);
2113                 }
2114         }
2115         if (ovid->rotation_type == VOUT_ROT_VRFB) {
2116                 omap_vout_release_vrfb(vout);
2117                 /* Free the VRFB buffer if allocated
2118                  * init time
2119                  */
2120                 if (vout->vrfb_static_allocation)
2121                         omap_vout_free_vrfb_buffers(vout);
2122         }
2123         omap_vout_free_buffers(vout);
2124
2125         kfree(vout);
2126 }
2127
2128 static int omap_vout_remove(struct platform_device *pdev)
2129 {
2130         int k;
2131         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2132         struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2133                         omap2video_device, v4l2_dev);
2134
2135         v4l2_device_unregister(v4l2_dev);
2136         for (k = 0; k < pdev->num_resources; k++)
2137                 omap_vout_cleanup_device(vid_dev->vouts[k]);
2138
2139         for (k = 0; k < vid_dev->num_displays; k++) {
2140                 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
2141                         vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
2142
2143                 omap_dss_put_device(vid_dev->displays[k]);
2144         }
2145         kfree(vid_dev);
2146         return 0;
2147 }
2148
2149 static int __init omap_vout_probe(struct platform_device *pdev)
2150 {
2151         int ret = 0, i;
2152         struct omap_overlay *ovl;
2153         struct omap_dss_device *dssdev = NULL;
2154         struct omap_dss_device *def_display;
2155         struct omap2video_device *vid_dev = NULL;
2156
2157         if (pdev->num_resources == 0) {
2158                 dev_err(&pdev->dev, "probed for an unknown device\n");
2159                 return -ENODEV;
2160         }
2161
2162         vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2163         if (vid_dev == NULL)
2164                 return -ENOMEM;
2165
2166         vid_dev->num_displays = 0;
2167         for_each_dss_dev(dssdev) {
2168                 omap_dss_get_device(dssdev);
2169                 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2170         }
2171
2172         if (vid_dev->num_displays == 0) {
2173                 dev_err(&pdev->dev, "no displays\n");
2174                 ret = -EINVAL;
2175                 goto probe_err0;
2176         }
2177
2178         vid_dev->num_overlays = omap_dss_get_num_overlays();
2179         for (i = 0; i < vid_dev->num_overlays; i++)
2180                 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2181
2182         vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2183         for (i = 0; i < vid_dev->num_managers; i++)
2184                 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2185
2186         /* Get the Video1 overlay and video2 overlay.
2187          * Setup the Display attached to that overlays
2188          */
2189         for (i = 1; i < vid_dev->num_overlays; i++) {
2190                 ovl = omap_dss_get_overlay(i);
2191                 if (ovl->manager && ovl->manager->device) {
2192                         def_display = ovl->manager->device;
2193                 } else {
2194                         dev_warn(&pdev->dev, "cannot find display\n");
2195                         def_display = NULL;
2196                 }
2197                 if (def_display) {
2198                         struct omap_dss_driver *dssdrv = def_display->driver;
2199
2200                         ret = dssdrv->enable(def_display);
2201                         if (ret) {
2202                                 /* Here we are not considering a error
2203                                  *  as display may be enabled by frame
2204                                  *  buffer driver
2205                                  */
2206                                 dev_warn(&pdev->dev,
2207                                         "'%s' Display already enabled\n",
2208                                         def_display->name);
2209                         }
2210                 }
2211         }
2212
2213         if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2214                 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2215                 ret = -ENODEV;
2216                 goto probe_err1;
2217         }
2218
2219         ret = omap_vout_create_video_devices(pdev);
2220         if (ret)
2221                 goto probe_err2;
2222
2223         for (i = 0; i < vid_dev->num_displays; i++) {
2224                 struct omap_dss_device *display = vid_dev->displays[i];
2225
2226                 if (display->driver->update)
2227                         display->driver->update(display, 0, 0,
2228                                         display->panel.timings.x_res,
2229                                         display->panel.timings.y_res);
2230         }
2231         return 0;
2232
2233 probe_err2:
2234         v4l2_device_unregister(&vid_dev->v4l2_dev);
2235 probe_err1:
2236         for (i = 1; i < vid_dev->num_overlays; i++) {
2237                 def_display = NULL;
2238                 ovl = omap_dss_get_overlay(i);
2239                 if (ovl->manager && ovl->manager->device)
2240                         def_display = ovl->manager->device;
2241
2242                 if (def_display && def_display->driver)
2243                         def_display->driver->disable(def_display);
2244         }
2245 probe_err0:
2246         kfree(vid_dev);
2247         return ret;
2248 }
2249
2250 static struct platform_driver omap_vout_driver = {
2251         .driver = {
2252                 .name = VOUT_NAME,
2253         },
2254         .probe = omap_vout_probe,
2255         .remove = omap_vout_remove,
2256 };
2257
2258 static int __init omap_vout_init(void)
2259 {
2260         if (platform_driver_register(&omap_vout_driver) != 0) {
2261                 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2262                 return -EINVAL;
2263         }
2264         return 0;
2265 }
2266
2267 static void omap_vout_cleanup(void)
2268 {
2269         platform_driver_unregister(&omap_vout_driver);
2270 }
2271
2272 late_initcall(omap_vout_init);
2273 module_exit(omap_vout_cleanup);