[media] OMAP_VOUT: Fix VSYNC IRQ handling in omap_vout_isr
[linux-2.6.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__, info.enabled, 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 int omapvid_handle_interlace_display(struct omap_vout_device *vout,
527                 unsigned int irqstatus, struct timeval timevalue)
528 {
529         u32 fid;
530
531         if (vout->first_int) {
532                 vout->first_int = 0;
533                 goto err;
534         }
535
536         if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
537                 fid = 1;
538         else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
539                 fid = 0;
540         else
541                 goto err;
542
543         vout->field_id ^= 1;
544         if (fid != vout->field_id) {
545                 if (fid == 0)
546                         vout->field_id = fid;
547         } else if (0 == fid) {
548                 if (vout->cur_frm == vout->next_frm)
549                         goto err;
550
551                 vout->cur_frm->ts = timevalue;
552                 vout->cur_frm->state = VIDEOBUF_DONE;
553                 wake_up_interruptible(&vout->cur_frm->done);
554                 vout->cur_frm = vout->next_frm;
555         } else {
556                 if (list_empty(&vout->dma_queue) ||
557                                 (vout->cur_frm != vout->next_frm))
558                         goto err;
559         }
560
561         return vout->field_id;
562 err:
563         return 0;
564 }
565
566 static void omap_vout_isr(void *arg, unsigned int irqstatus)
567 {
568         int ret, fid, mgr_id;
569         u32 addr, irq;
570         struct omap_overlay *ovl;
571         struct timeval timevalue;
572         struct omapvideo_info *ovid;
573         struct omap_dss_device *cur_display;
574         struct omap_vout_device *vout = (struct omap_vout_device *)arg;
575
576         if (!vout->streaming)
577                 return;
578
579         ovid = &vout->vid_info;
580         ovl = ovid->overlays[0];
581         /* get the display device attached to the overlay */
582         if (!ovl->manager || !ovl->manager->device)
583                 return;
584
585         mgr_id = ovl->manager->id;
586         cur_display = ovl->manager->device;
587
588         spin_lock(&vout->vbq_lock);
589         do_gettimeofday(&timevalue);
590
591         switch (cur_display->type) {
592         case OMAP_DISPLAY_TYPE_DPI:
593                 if (mgr_id == OMAP_DSS_CHANNEL_LCD)
594                         irq = DISPC_IRQ_VSYNC;
595                 else if (mgr_id == OMAP_DSS_CHANNEL_LCD2)
596                         irq = DISPC_IRQ_VSYNC2;
597                 else
598                         goto vout_isr_err;
599
600                 if (!(irqstatus & irq))
601                         goto vout_isr_err;
602                 break;
603         case OMAP_DISPLAY_TYPE_VENC:
604                 fid = omapvid_handle_interlace_display(vout, irqstatus,
605                                 timevalue);
606                 if (!fid)
607                         goto vout_isr_err;
608                 break;
609         case OMAP_DISPLAY_TYPE_HDMI:
610                 if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
611                         goto vout_isr_err;
612                 break;
613         default:
614                 goto vout_isr_err;
615         }
616
617         if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
618                 vout->cur_frm->ts = timevalue;
619                 vout->cur_frm->state = VIDEOBUF_DONE;
620                 wake_up_interruptible(&vout->cur_frm->done);
621                 vout->cur_frm = vout->next_frm;
622         }
623
624         vout->first_int = 0;
625         if (list_empty(&vout->dma_queue))
626                 goto vout_isr_err;
627
628         vout->next_frm = list_entry(vout->dma_queue.next,
629                         struct videobuf_buffer, queue);
630         list_del(&vout->next_frm->queue);
631
632         vout->next_frm->state = VIDEOBUF_ACTIVE;
633
634         addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
635                 + vout->cropped_offset;
636
637         /* First save the configuration in ovelray structure */
638         ret = omapvid_init(vout, addr);
639         if (ret)
640                 printk(KERN_ERR VOUT_NAME
641                         "failed to set overlay info\n");
642         /* Enable the pipeline and set the Go bit */
643         ret = omapvid_apply_changes(vout);
644         if (ret)
645                 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
646
647 vout_isr_err:
648         spin_unlock(&vout->vbq_lock);
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         int vid_max_buf_size;
667
668         if (!vout)
669                 return -EINVAL;
670
671         vid_max_buf_size = vout->vid == OMAP_VIDEO1 ? video1_bufsize :
672                 video2_bufsize;
673
674         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
675                 return -EINVAL;
676
677         startindex = (vout->vid == OMAP_VIDEO1) ?
678                 video1_numbuffers : video2_numbuffers;
679         if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
680                 *count = startindex;
681
682         if (ovid->rotation_type == VOUT_ROT_VRFB) {
683                 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
684                         return -ENOMEM;
685         }
686
687         if (V4L2_MEMORY_MMAP != vout->memory)
688                 return 0;
689
690         /* Now allocated the V4L2 buffers */
691         *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
692         startindex = (vout->vid == OMAP_VIDEO1) ?
693                 video1_numbuffers : video2_numbuffers;
694
695         /* Check the size of the buffer */
696         if (*size > vid_max_buf_size) {
697                 v4l2_err(&vout->vid_dev->v4l2_dev,
698                                 "buffer allocation mismatch [%u] [%u]\n",
699                                 *size, vout->buffer_size);
700                 return -ENOMEM;
701         }
702
703         for (i = startindex; i < *count; i++) {
704                 vout->buffer_size = *size;
705
706                 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
707                                 &phy_addr);
708                 if (!virt_addr) {
709                         if (ovid->rotation_type == VOUT_ROT_NONE) {
710                                 break;
711                         } else {
712                                 if (!is_rotation_enabled(vout))
713                                         break;
714                         /* Free the VRFB buffers if no space for V4L2 buffers */
715                         for (j = i; j < *count; j++) {
716                                 omap_vout_free_buffer(
717                                                 vout->smsshado_virt_addr[j],
718                                                 vout->smsshado_size);
719                                 vout->smsshado_virt_addr[j] = 0;
720                                 vout->smsshado_phy_addr[j] = 0;
721                                 }
722                         }
723                 }
724                 vout->buf_virt_addr[i] = virt_addr;
725                 vout->buf_phy_addr[i] = phy_addr;
726         }
727         *count = vout->buffer_allocated = i;
728
729         return 0;
730 }
731
732 /*
733  * Free the V4L2 buffers additionally allocated than default
734  * number of buffers
735  */
736 static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
737 {
738         int num_buffers = 0, i;
739
740         num_buffers = (vout->vid == OMAP_VIDEO1) ?
741                 video1_numbuffers : video2_numbuffers;
742
743         for (i = num_buffers; i < vout->buffer_allocated; i++) {
744                 if (vout->buf_virt_addr[i])
745                         omap_vout_free_buffer(vout->buf_virt_addr[i],
746                                         vout->buffer_size);
747
748                 vout->buf_virt_addr[i] = 0;
749                 vout->buf_phy_addr[i] = 0;
750         }
751         vout->buffer_allocated = num_buffers;
752 }
753
754 /*
755  * This function will be called when VIDIOC_QBUF ioctl is called.
756  * It prepare buffers before give out for the display. This function
757  * converts user space virtual address into physical address if userptr memory
758  * exchange mechanism is used. If rotation is enabled, it copies entire
759  * buffer into VRFB memory space before giving it to the DSS.
760  */
761 static int omap_vout_buffer_prepare(struct videobuf_queue *q,
762                         struct videobuf_buffer *vb,
763                         enum v4l2_field field)
764 {
765         struct omap_vout_device *vout = q->priv_data;
766         struct omapvideo_info *ovid = &vout->vid_info;
767
768         if (VIDEOBUF_NEEDS_INIT == vb->state) {
769                 vb->width = vout->pix.width;
770                 vb->height = vout->pix.height;
771                 vb->size = vb->width * vb->height * vout->bpp;
772                 vb->field = field;
773         }
774         vb->state = VIDEOBUF_PREPARED;
775         /* if user pointer memory mechanism is used, get the physical
776          * address of the buffer
777          */
778         if (V4L2_MEMORY_USERPTR == vb->memory) {
779                 if (0 == vb->baddr)
780                         return -EINVAL;
781                 /* Physical address */
782                 vout->queued_buf_addr[vb->i] = (u8 *)
783                         omap_vout_uservirt_to_phys(vb->baddr);
784         } else {
785                 u32 addr, dma_addr;
786                 unsigned long size;
787
788                 addr = (unsigned long) vout->buf_virt_addr[vb->i];
789                 size = (unsigned long) vb->size;
790
791                 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
792                                 size, DMA_TO_DEVICE);
793                 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
794                         v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
795
796                 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
797         }
798
799         if (ovid->rotation_type == VOUT_ROT_VRFB)
800                 return omap_vout_prepare_vrfb(vout, vb);
801         else
802                 return 0;
803 }
804
805 /*
806  * Buffer queue function will be called from the videobuf layer when _QBUF
807  * ioctl is called. It is used to enqueue buffer, which is ready to be
808  * displayed.
809  */
810 static void omap_vout_buffer_queue(struct videobuf_queue *q,
811                           struct videobuf_buffer *vb)
812 {
813         struct omap_vout_device *vout = q->priv_data;
814
815         /* Driver is also maintainig a queue. So enqueue buffer in the driver
816          * queue */
817         list_add_tail(&vb->queue, &vout->dma_queue);
818
819         vb->state = VIDEOBUF_QUEUED;
820 }
821
822 /*
823  * Buffer release function is called from videobuf layer to release buffer
824  * which are already allocated
825  */
826 static void omap_vout_buffer_release(struct videobuf_queue *q,
827                             struct videobuf_buffer *vb)
828 {
829         struct omap_vout_device *vout = q->priv_data;
830
831         vb->state = VIDEOBUF_NEEDS_INIT;
832
833         if (V4L2_MEMORY_MMAP != vout->memory)
834                 return;
835 }
836
837 /*
838  *  File operations
839  */
840 static unsigned int omap_vout_poll(struct file *file,
841                                    struct poll_table_struct *wait)
842 {
843         struct omap_vout_device *vout = file->private_data;
844         struct videobuf_queue *q = &vout->vbq;
845
846         return videobuf_poll_stream(file, q, wait);
847 }
848
849 static void omap_vout_vm_open(struct vm_area_struct *vma)
850 {
851         struct omap_vout_device *vout = vma->vm_private_data;
852
853         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
854                 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
855         vout->mmap_count++;
856 }
857
858 static void omap_vout_vm_close(struct vm_area_struct *vma)
859 {
860         struct omap_vout_device *vout = vma->vm_private_data;
861
862         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
863                 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
864         vout->mmap_count--;
865 }
866
867 static struct vm_operations_struct omap_vout_vm_ops = {
868         .open   = omap_vout_vm_open,
869         .close  = omap_vout_vm_close,
870 };
871
872 static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
873 {
874         int i;
875         void *pos;
876         unsigned long start = vma->vm_start;
877         unsigned long size = (vma->vm_end - vma->vm_start);
878         struct omap_vout_device *vout = file->private_data;
879         struct videobuf_queue *q = &vout->vbq;
880
881         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
882                         " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
883                         vma->vm_pgoff, vma->vm_start, vma->vm_end);
884
885         /* look for the buffer to map */
886         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
887                 if (NULL == q->bufs[i])
888                         continue;
889                 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
890                         continue;
891                 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
892                         break;
893         }
894
895         if (VIDEO_MAX_FRAME == i) {
896                 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
897                                 "offset invalid [offset=0x%lx]\n",
898                                 (vma->vm_pgoff << PAGE_SHIFT));
899                 return -EINVAL;
900         }
901         /* Check the size of the buffer */
902         if (size > vout->buffer_size) {
903                 v4l2_err(&vout->vid_dev->v4l2_dev,
904                                 "insufficient memory [%lu] [%u]\n",
905                                 size, vout->buffer_size);
906                 return -ENOMEM;
907         }
908
909         q->bufs[i]->baddr = vma->vm_start;
910
911         vma->vm_flags |= VM_RESERVED;
912         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
913         vma->vm_ops = &omap_vout_vm_ops;
914         vma->vm_private_data = (void *) vout;
915         pos = (void *)vout->buf_virt_addr[i];
916         vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
917         while (size > 0) {
918                 unsigned long pfn;
919                 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
920                 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
921                         return -EAGAIN;
922                 start += PAGE_SIZE;
923                 pos += PAGE_SIZE;
924                 size -= PAGE_SIZE;
925         }
926         vout->mmap_count++;
927         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
928
929         return 0;
930 }
931
932 static int omap_vout_release(struct file *file)
933 {
934         unsigned int ret, i;
935         struct videobuf_queue *q;
936         struct omapvideo_info *ovid;
937         struct omap_vout_device *vout = file->private_data;
938
939         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
940         ovid = &vout->vid_info;
941
942         if (!vout)
943                 return 0;
944
945         q = &vout->vbq;
946         /* Disable all the overlay managers connected with this interface */
947         for (i = 0; i < ovid->num_overlays; i++) {
948                 struct omap_overlay *ovl = ovid->overlays[i];
949                 if (ovl->manager && ovl->manager->device) {
950                         struct omap_overlay_info info;
951                         ovl->get_overlay_info(ovl, &info);
952                         info.enabled = 0;
953                         ovl->set_overlay_info(ovl, &info);
954                 }
955         }
956         /* Turn off the pipeline */
957         ret = omapvid_apply_changes(vout);
958         if (ret)
959                 v4l2_warn(&vout->vid_dev->v4l2_dev,
960                                 "Unable to apply changes\n");
961
962         /* Free all buffers */
963         omap_vout_free_extra_buffers(vout);
964
965         /* Free the VRFB buffers only if they are allocated
966          * during reqbufs.  Don't free if init time allocated
967          */
968         if (ovid->rotation_type == VOUT_ROT_VRFB) {
969                 if (!vout->vrfb_static_allocation)
970                         omap_vout_free_vrfb_buffers(vout);
971         }
972         videobuf_mmap_free(q);
973
974         /* Even if apply changes fails we should continue
975            freeing allocated memory */
976         if (vout->streaming) {
977                 u32 mask = 0;
978
979                 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
980                         DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
981                 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
982                 vout->streaming = 0;
983
984                 videobuf_streamoff(q);
985                 videobuf_queue_cancel(q);
986         }
987
988         if (vout->mmap_count != 0)
989                 vout->mmap_count = 0;
990
991         vout->opened -= 1;
992         file->private_data = NULL;
993
994         if (vout->buffer_allocated)
995                 videobuf_mmap_free(q);
996
997         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
998         return ret;
999 }
1000
1001 static int omap_vout_open(struct file *file)
1002 {
1003         struct videobuf_queue *q;
1004         struct omap_vout_device *vout = NULL;
1005
1006         vout = video_drvdata(file);
1007         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1008
1009         if (vout == NULL)
1010                 return -ENODEV;
1011
1012         /* for now, we only support single open */
1013         if (vout->opened)
1014                 return -EBUSY;
1015
1016         vout->opened += 1;
1017
1018         file->private_data = vout;
1019         vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1020
1021         q = &vout->vbq;
1022         video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1023         video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1024         video_vbq_ops.buf_release = omap_vout_buffer_release;
1025         video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1026         spin_lock_init(&vout->vbq_lock);
1027
1028         videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1029                         &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
1030                         sizeof(struct videobuf_buffer), vout, NULL);
1031
1032         v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1033         return 0;
1034 }
1035
1036 /*
1037  * V4L2 ioctls
1038  */
1039 static int vidioc_querycap(struct file *file, void *fh,
1040                 struct v4l2_capability *cap)
1041 {
1042         struct omap_vout_device *vout = fh;
1043
1044         strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1045         strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1046         cap->bus_info[0] = '\0';
1047         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
1048
1049         return 0;
1050 }
1051
1052 static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1053                         struct v4l2_fmtdesc *fmt)
1054 {
1055         int index = fmt->index;
1056
1057         if (index >= NUM_OUTPUT_FORMATS)
1058                 return -EINVAL;
1059
1060         fmt->flags = omap_formats[index].flags;
1061         strlcpy(fmt->description, omap_formats[index].description,
1062                         sizeof(fmt->description));
1063         fmt->pixelformat = omap_formats[index].pixelformat;
1064
1065         return 0;
1066 }
1067
1068 static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1069                         struct v4l2_format *f)
1070 {
1071         struct omap_vout_device *vout = fh;
1072
1073         f->fmt.pix = vout->pix;
1074         return 0;
1075
1076 }
1077
1078 static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1079                         struct v4l2_format *f)
1080 {
1081         struct omap_overlay *ovl;
1082         struct omapvideo_info *ovid;
1083         struct omap_video_timings *timing;
1084         struct omap_vout_device *vout = fh;
1085
1086         ovid = &vout->vid_info;
1087         ovl = ovid->overlays[0];
1088
1089         if (!ovl->manager || !ovl->manager->device)
1090                 return -EINVAL;
1091         /* get the display device attached to the overlay */
1092         timing = &ovl->manager->device->panel.timings;
1093
1094         vout->fbuf.fmt.height = timing->y_res;
1095         vout->fbuf.fmt.width = timing->x_res;
1096
1097         omap_vout_try_format(&f->fmt.pix);
1098         return 0;
1099 }
1100
1101 static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1102                         struct v4l2_format *f)
1103 {
1104         int ret, bpp;
1105         struct omap_overlay *ovl;
1106         struct omapvideo_info *ovid;
1107         struct omap_video_timings *timing;
1108         struct omap_vout_device *vout = fh;
1109
1110         if (vout->streaming)
1111                 return -EBUSY;
1112
1113         mutex_lock(&vout->lock);
1114
1115         ovid = &vout->vid_info;
1116         ovl = ovid->overlays[0];
1117
1118         /* get the display device attached to the overlay */
1119         if (!ovl->manager || !ovl->manager->device) {
1120                 ret = -EINVAL;
1121                 goto s_fmt_vid_out_exit;
1122         }
1123         timing = &ovl->manager->device->panel.timings;
1124
1125         /* We dont support RGB24-packed mode if vrfb rotation
1126          * is enabled*/
1127         if ((is_rotation_enabled(vout)) &&
1128                         f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1129                 ret = -EINVAL;
1130                 goto s_fmt_vid_out_exit;
1131         }
1132
1133         /* get the framebuffer parameters */
1134
1135         if (is_rotation_90_or_270(vout)) {
1136                 vout->fbuf.fmt.height = timing->x_res;
1137                 vout->fbuf.fmt.width = timing->y_res;
1138         } else {
1139                 vout->fbuf.fmt.height = timing->y_res;
1140                 vout->fbuf.fmt.width = timing->x_res;
1141         }
1142
1143         /* change to samller size is OK */
1144
1145         bpp = omap_vout_try_format(&f->fmt.pix);
1146         f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1147
1148         /* try & set the new output format */
1149         vout->bpp = bpp;
1150         vout->pix = f->fmt.pix;
1151         vout->vrfb_bpp = 1;
1152
1153         /* If YUYV then vrfb bpp is 2, for  others its 1 */
1154         if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1155                         V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1156                 vout->vrfb_bpp = 2;
1157
1158         /* set default crop and win */
1159         omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1160
1161         /* Save the changes in the overlay strcuture */
1162         ret = omapvid_init(vout, 0);
1163         if (ret) {
1164                 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1165                 goto s_fmt_vid_out_exit;
1166         }
1167
1168         ret = 0;
1169
1170 s_fmt_vid_out_exit:
1171         mutex_unlock(&vout->lock);
1172         return ret;
1173 }
1174
1175 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1176                         struct v4l2_format *f)
1177 {
1178         int ret = 0;
1179         struct omap_vout_device *vout = fh;
1180         struct omap_overlay *ovl;
1181         struct omapvideo_info *ovid;
1182         struct v4l2_window *win = &f->fmt.win;
1183
1184         ovid = &vout->vid_info;
1185         ovl = ovid->overlays[0];
1186
1187         ret = omap_vout_try_window(&vout->fbuf, win);
1188
1189         if (!ret) {
1190                 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1191                         win->global_alpha = 255;
1192                 else
1193                         win->global_alpha = f->fmt.win.global_alpha;
1194         }
1195
1196         return ret;
1197 }
1198
1199 static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1200                         struct v4l2_format *f)
1201 {
1202         int ret = 0;
1203         struct omap_overlay *ovl;
1204         struct omapvideo_info *ovid;
1205         struct omap_vout_device *vout = fh;
1206         struct v4l2_window *win = &f->fmt.win;
1207
1208         mutex_lock(&vout->lock);
1209         ovid = &vout->vid_info;
1210         ovl = ovid->overlays[0];
1211
1212         ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1213         if (!ret) {
1214                 /* Video1 plane does not support global alpha on OMAP3 */
1215                 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1216                         vout->win.global_alpha = 255;
1217                 else
1218                         vout->win.global_alpha = f->fmt.win.global_alpha;
1219
1220                 vout->win.chromakey = f->fmt.win.chromakey;
1221         }
1222         mutex_unlock(&vout->lock);
1223         return ret;
1224 }
1225
1226 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
1227                         struct v4l2_fmtdesc *fmt)
1228 {
1229         int index = fmt->index;
1230
1231         if (index >= NUM_OUTPUT_FORMATS)
1232                 return -EINVAL;
1233
1234         fmt->flags = omap_formats[index].flags;
1235         strlcpy(fmt->description, omap_formats[index].description,
1236                         sizeof(fmt->description));
1237         fmt->pixelformat = omap_formats[index].pixelformat;
1238         return 0;
1239 }
1240
1241 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1242                         struct v4l2_format *f)
1243 {
1244         u32 key_value =  0;
1245         struct omap_overlay *ovl;
1246         struct omapvideo_info *ovid;
1247         struct omap_vout_device *vout = fh;
1248         struct omap_overlay_manager_info info;
1249         struct v4l2_window *win = &f->fmt.win;
1250
1251         ovid = &vout->vid_info;
1252         ovl = ovid->overlays[0];
1253
1254         win->w = vout->win.w;
1255         win->field = vout->win.field;
1256         win->global_alpha = vout->win.global_alpha;
1257
1258         if (ovl->manager && ovl->manager->get_manager_info) {
1259                 ovl->manager->get_manager_info(ovl->manager, &info);
1260                 key_value = info.trans_key;
1261         }
1262         win->chromakey = key_value;
1263         return 0;
1264 }
1265
1266 static int vidioc_cropcap(struct file *file, void *fh,
1267                 struct v4l2_cropcap *cropcap)
1268 {
1269         struct omap_vout_device *vout = fh;
1270         struct v4l2_pix_format *pix = &vout->pix;
1271
1272         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1273                 return -EINVAL;
1274
1275         /* Width and height are always even */
1276         cropcap->bounds.width = pix->width & ~1;
1277         cropcap->bounds.height = pix->height & ~1;
1278
1279         omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1280         cropcap->pixelaspect.numerator = 1;
1281         cropcap->pixelaspect.denominator = 1;
1282         return 0;
1283 }
1284
1285 static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1286 {
1287         struct omap_vout_device *vout = fh;
1288
1289         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1290                 return -EINVAL;
1291         crop->c = vout->crop;
1292         return 0;
1293 }
1294
1295 static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1296 {
1297         int ret = -EINVAL;
1298         struct omap_vout_device *vout = fh;
1299         struct omapvideo_info *ovid;
1300         struct omap_overlay *ovl;
1301         struct omap_video_timings *timing;
1302
1303         if (vout->streaming)
1304                 return -EBUSY;
1305
1306         mutex_lock(&vout->lock);
1307         ovid = &vout->vid_info;
1308         ovl = ovid->overlays[0];
1309
1310         if (!ovl->manager || !ovl->manager->device) {
1311                 ret = -EINVAL;
1312                 goto s_crop_err;
1313         }
1314         /* get the display device attached to the overlay */
1315         timing = &ovl->manager->device->panel.timings;
1316
1317         if (is_rotation_90_or_270(vout)) {
1318                 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
1329 s_crop_err:
1330         mutex_unlock(&vout->lock);
1331         return ret;
1332 }
1333
1334 static 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
1356 static 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
1389 static 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         {
1397                 struct omapvideo_info *ovid;
1398                 int rotation = a->value;
1399
1400                 ovid = &vout->vid_info;
1401
1402                 mutex_lock(&vout->lock);
1403                 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1404                         mutex_unlock(&vout->lock);
1405                         ret = -ERANGE;
1406                         break;
1407                 }
1408
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);
1463                 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1464                         mutex_unlock(&vout->lock);
1465                         ret = -ERANGE;
1466                         break;
1467                 }
1468
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
1485 static 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;
1492
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++) {
1517                         omap_vout_free_buffer(vout->buf_virt_addr[i],
1518                                         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;
1546
1547 reqbuf_err:
1548         mutex_unlock(&vout->lock);
1549         return ret;
1550 }
1551
1552 static 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
1560 static 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
1578         if ((is_rotation_enabled(vout)) &&
1579                         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
1588 static 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
1593         int ret;
1594         u32 addr;
1595         unsigned long size;
1596         struct videobuf_buffer *vb;
1597
1598         vb = q->bufs[b->index];
1599
1600         if (!vout->streaming)
1601                 return -EINVAL;
1602
1603         if (file->f_flags & O_NONBLOCK)
1604                 /* Call videobuf_dqbuf for non blocking mode */
1605                 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
1606         else
1607                 /* Call videobuf_dqbuf for  blocking mode */
1608                 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;
1615 }
1616
1617 static 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
1663         mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1664                 | DISPC_IRQ_VSYNC2;
1665
1666         omap_dispc_register_isr(omap_vout_isr, vout, mask);
1667
1668         for (j = 0; j < ovid->num_overlays; j++) {
1669                 struct omap_overlay *ovl = ovid->overlays[j];
1670
1671                 if (ovl->manager && ovl->manager->device) {
1672                         struct omap_overlay_info info;
1673                         ovl->get_overlay_info(ovl, &info);
1674                         info.enabled = 1;
1675                         info.paddr = addr;
1676                         if (ovl->set_overlay_info(ovl, &info)) {
1677                                 ret = -EINVAL;
1678                                 goto streamon_err1;
1679                         }
1680                 }
1681         }
1682
1683         /* First save the configuration in ovelray structure */
1684         ret = omapvid_init(vout, addr);
1685         if (ret)
1686                 v4l2_err(&vout->vid_dev->v4l2_dev,
1687                                 "failed to set overlay info\n");
1688         /* Enable the pipeline and set the Go bit */
1689         ret = omapvid_apply_changes(vout);
1690         if (ret)
1691                 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1692
1693         ret = 0;
1694
1695 streamon_err1:
1696         if (ret)
1697                 ret = videobuf_streamoff(q);
1698 streamon_err:
1699         mutex_unlock(&vout->lock);
1700         return ret;
1701 }
1702
1703 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1704 {
1705         u32 mask = 0;
1706         int ret = 0, j;
1707         struct omap_vout_device *vout = fh;
1708         struct omapvideo_info *ovid = &vout->vid_info;
1709
1710         if (!vout->streaming)
1711                 return -EINVAL;
1712
1713         vout->streaming = 0;
1714         mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1715                 | DISPC_IRQ_VSYNC2;
1716
1717         omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1718
1719         for (j = 0; j < ovid->num_overlays; j++) {
1720                 struct omap_overlay *ovl = ovid->overlays[j];
1721
1722                 if (ovl->manager && ovl->manager->device) {
1723                         struct omap_overlay_info info;
1724
1725                         ovl->get_overlay_info(ovl, &info);
1726                         info.enabled = 0;
1727                         ret = ovl->set_overlay_info(ovl, &info);
1728                         if (ret)
1729                                 v4l2_err(&vout->vid_dev->v4l2_dev,
1730                                 "failed to update overlay info in streamoff\n");
1731                 }
1732         }
1733
1734         /* Turn of the pipeline */
1735         ret = omapvid_apply_changes(vout);
1736         if (ret)
1737                 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
1738                                 " streamoff\n");
1739
1740         INIT_LIST_HEAD(&vout->dma_queue);
1741         ret = videobuf_streamoff(&vout->vbq);
1742
1743         return ret;
1744 }
1745
1746 static int vidioc_s_fbuf(struct file *file, void *fh,
1747                                 struct v4l2_framebuffer *a)
1748 {
1749         int enable = 0;
1750         struct omap_overlay *ovl;
1751         struct omapvideo_info *ovid;
1752         struct omap_vout_device *vout = fh;
1753         struct omap_overlay_manager_info info;
1754         enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1755
1756         ovid = &vout->vid_info;
1757         ovl = ovid->overlays[0];
1758
1759         /* OMAP DSS doesn't support Source and Destination color
1760            key together */
1761         if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1762                         (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1763                 return -EINVAL;
1764         /* OMAP DSS Doesn't support the Destination color key
1765            and alpha blending together */
1766         if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1767                         (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1768                 return -EINVAL;
1769
1770         if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1771                 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1772                 key_type =  OMAP_DSS_COLOR_KEY_VID_SRC;
1773         } else
1774                 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1775
1776         if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1777                 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1778                 key_type =  OMAP_DSS_COLOR_KEY_GFX_DST;
1779         } else
1780                 vout->fbuf.flags &=  ~V4L2_FBUF_FLAG_CHROMAKEY;
1781
1782         if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1783                                 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1784                 enable = 1;
1785         else
1786                 enable = 0;
1787         if (ovl->manager && ovl->manager->get_manager_info &&
1788                         ovl->manager->set_manager_info) {
1789
1790                 ovl->manager->get_manager_info(ovl->manager, &info);
1791                 info.trans_enabled = enable;
1792                 info.trans_key_type = key_type;
1793                 info.trans_key = vout->win.chromakey;
1794
1795                 if (ovl->manager->set_manager_info(ovl->manager, &info))
1796                         return -EINVAL;
1797         }
1798         if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1799                 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1800                 enable = 1;
1801         } else {
1802                 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1803                 enable = 0;
1804         }
1805         if (ovl->manager && ovl->manager->get_manager_info &&
1806                         ovl->manager->set_manager_info) {
1807                 ovl->manager->get_manager_info(ovl->manager, &info);
1808                 /* enable this only if there is no zorder cap */
1809                 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1810                         info.partial_alpha_enabled = enable;
1811                 if (ovl->manager->set_manager_info(ovl->manager, &info))
1812                         return -EINVAL;
1813         }
1814
1815         return 0;
1816 }
1817
1818 static int vidioc_g_fbuf(struct file *file, void *fh,
1819                 struct v4l2_framebuffer *a)
1820 {
1821         struct omap_overlay *ovl;
1822         struct omapvideo_info *ovid;
1823         struct omap_vout_device *vout = fh;
1824         struct omap_overlay_manager_info info;
1825
1826         ovid = &vout->vid_info;
1827         ovl = ovid->overlays[0];
1828
1829         a->flags = 0x0;
1830         a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1831                 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1832
1833         if (ovl->manager && ovl->manager->get_manager_info) {
1834                 ovl->manager->get_manager_info(ovl->manager, &info);
1835                 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1836                         a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1837                 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1838                         a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1839         }
1840         if (ovl->manager && ovl->manager->get_manager_info) {
1841                 ovl->manager->get_manager_info(ovl->manager, &info);
1842                 if (info.partial_alpha_enabled)
1843                         a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1844         }
1845
1846         return 0;
1847 }
1848
1849 static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1850         .vidioc_querycap                        = vidioc_querycap,
1851         .vidioc_enum_fmt_vid_out                = vidioc_enum_fmt_vid_out,
1852         .vidioc_g_fmt_vid_out                   = vidioc_g_fmt_vid_out,
1853         .vidioc_try_fmt_vid_out                 = vidioc_try_fmt_vid_out,
1854         .vidioc_s_fmt_vid_out                   = vidioc_s_fmt_vid_out,
1855         .vidioc_queryctrl                       = vidioc_queryctrl,
1856         .vidioc_g_ctrl                          = vidioc_g_ctrl,
1857         .vidioc_s_fbuf                          = vidioc_s_fbuf,
1858         .vidioc_g_fbuf                          = vidioc_g_fbuf,
1859         .vidioc_s_ctrl                          = vidioc_s_ctrl,
1860         .vidioc_try_fmt_vid_overlay             = vidioc_try_fmt_vid_overlay,
1861         .vidioc_s_fmt_vid_overlay               = vidioc_s_fmt_vid_overlay,
1862         .vidioc_enum_fmt_vid_overlay            = vidioc_enum_fmt_vid_overlay,
1863         .vidioc_g_fmt_vid_overlay               = vidioc_g_fmt_vid_overlay,
1864         .vidioc_cropcap                         = vidioc_cropcap,
1865         .vidioc_g_crop                          = vidioc_g_crop,
1866         .vidioc_s_crop                          = vidioc_s_crop,
1867         .vidioc_reqbufs                         = vidioc_reqbufs,
1868         .vidioc_querybuf                        = vidioc_querybuf,
1869         .vidioc_qbuf                            = vidioc_qbuf,
1870         .vidioc_dqbuf                           = vidioc_dqbuf,
1871         .vidioc_streamon                        = vidioc_streamon,
1872         .vidioc_streamoff                       = vidioc_streamoff,
1873 };
1874
1875 static const struct v4l2_file_operations omap_vout_fops = {
1876         .owner          = THIS_MODULE,
1877         .poll           = omap_vout_poll,
1878         .unlocked_ioctl = video_ioctl2,
1879         .mmap           = omap_vout_mmap,
1880         .open           = omap_vout_open,
1881         .release        = omap_vout_release,
1882 };
1883
1884 /* Init functions used during driver initialization */
1885 /* Initial setup of video_data */
1886 static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1887 {
1888         struct video_device *vfd;
1889         struct v4l2_pix_format *pix;
1890         struct v4l2_control *control;
1891         struct omap_dss_device *display =
1892                 vout->vid_info.overlays[0]->manager->device;
1893
1894         /* set the default pix */
1895         pix = &vout->pix;
1896
1897         /* Set the default picture of QVGA  */
1898         pix->width = QQVGA_WIDTH;
1899         pix->height = QQVGA_HEIGHT;
1900
1901         /* Default pixel format is RGB 5-6-5 */
1902         pix->pixelformat = V4L2_PIX_FMT_RGB565;
1903         pix->field = V4L2_FIELD_ANY;
1904         pix->bytesperline = pix->width * 2;
1905         pix->sizeimage = pix->bytesperline * pix->height;
1906         pix->priv = 0;
1907         pix->colorspace = V4L2_COLORSPACE_JPEG;
1908
1909         vout->bpp = RGB565_BPP;
1910         vout->fbuf.fmt.width  =  display->panel.timings.x_res;
1911         vout->fbuf.fmt.height =  display->panel.timings.y_res;
1912
1913         /* Set the data structures for the overlay parameters*/
1914         vout->win.global_alpha = 255;
1915         vout->fbuf.flags = 0;
1916         vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1917                 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1918         vout->win.chromakey = 0;
1919
1920         omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1921
1922         /*Initialize the control variables for
1923           rotation, flipping and background color. */
1924         control = vout->control;
1925         control[0].id = V4L2_CID_ROTATE;
1926         control[0].value = 0;
1927         vout->rotation = 0;
1928         vout->mirror = 0;
1929         vout->control[2].id = V4L2_CID_HFLIP;
1930         vout->control[2].value = 0;
1931         if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1932                 vout->vrfb_bpp = 2;
1933
1934         control[1].id = V4L2_CID_BG_COLOR;
1935         control[1].value = 0;
1936
1937         /* initialize the video_device struct */
1938         vfd = vout->vfd = video_device_alloc();
1939
1940         if (!vfd) {
1941                 printk(KERN_ERR VOUT_NAME ": could not allocate"
1942                                 " video device struct\n");
1943                 return -ENOMEM;
1944         }
1945         vfd->release = video_device_release;
1946         vfd->ioctl_ops = &vout_ioctl_ops;
1947
1948         strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1949
1950         vfd->fops = &omap_vout_fops;
1951         vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
1952         mutex_init(&vout->lock);
1953
1954         vfd->minor = -1;
1955         return 0;
1956
1957 }
1958
1959 /* Setup video buffers */
1960 static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1961                 int vid_num)
1962 {
1963         u32 numbuffers;
1964         int ret = 0, i;
1965         struct omapvideo_info *ovid;
1966         struct omap_vout_device *vout;
1967         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1968         struct omap2video_device *vid_dev =
1969                 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1970
1971         vout = vid_dev->vouts[vid_num];
1972         ovid = &vout->vid_info;
1973
1974         numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1975         vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1976         dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1977
1978         for (i = 0; i < numbuffers; i++) {
1979                 vout->buf_virt_addr[i] =
1980                         omap_vout_alloc_buffer(vout->buffer_size,
1981                                         (u32 *) &vout->buf_phy_addr[i]);
1982                 if (!vout->buf_virt_addr[i]) {
1983                         numbuffers = i;
1984                         ret = -ENOMEM;
1985                         goto free_buffers;
1986                 }
1987         }
1988
1989         vout->cropped_offset = 0;
1990
1991         if (ovid->rotation_type == VOUT_ROT_VRFB) {
1992                 int static_vrfb_allocation = (vid_num == 0) ?
1993                         vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1994                 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1995                                 static_vrfb_allocation);
1996         }
1997
1998         return ret;
1999
2000 free_buffers:
2001         for (i = 0; i < numbuffers; i++) {
2002                 omap_vout_free_buffer(vout->buf_virt_addr[i],
2003                                                 vout->buffer_size);
2004                 vout->buf_virt_addr[i] = 0;
2005                 vout->buf_phy_addr[i] = 0;
2006         }
2007         return ret;
2008
2009 }
2010
2011 /* Create video out devices */
2012 static int __init omap_vout_create_video_devices(struct platform_device *pdev)
2013 {
2014         int ret = 0, k;
2015         struct omap_vout_device *vout;
2016         struct video_device *vfd = NULL;
2017         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2018         struct omap2video_device *vid_dev = container_of(v4l2_dev,
2019                         struct omap2video_device, v4l2_dev);
2020
2021         for (k = 0; k < pdev->num_resources; k++) {
2022
2023                 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
2024                 if (!vout) {
2025                         dev_err(&pdev->dev, ": could not allocate memory\n");
2026                         return -ENOMEM;
2027                 }
2028
2029                 vout->vid = k;
2030                 vid_dev->vouts[k] = vout;
2031                 vout->vid_dev = vid_dev;
2032                 /* Select video2 if only 1 overlay is controlled by V4L2 */
2033                 if (pdev->num_resources == 1)
2034                         vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2035                 else
2036                         /* Else select video1 and video2 one by one. */
2037                         vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2038                 vout->vid_info.num_overlays = 1;
2039                 vout->vid_info.id = k + 1;
2040
2041                 /* Set VRFB as rotation_type for omap2 and omap3 */
2042                 if (cpu_is_omap24xx() || cpu_is_omap34xx())
2043                         vout->vid_info.rotation_type = VOUT_ROT_VRFB;
2044
2045                 /* Setup the default configuration for the video devices
2046                  */
2047                 if (omap_vout_setup_video_data(vout) != 0) {
2048                         ret = -ENOMEM;
2049                         goto error;
2050                 }
2051
2052                 /* Allocate default number of buffers for the video streaming
2053                  * and reserve the VRFB space for rotation
2054                  */
2055                 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2056                         ret = -ENOMEM;
2057                         goto error1;
2058                 }
2059
2060                 /* Register the Video device with V4L2
2061                  */
2062                 vfd = vout->vfd;
2063                 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
2064                         dev_err(&pdev->dev, ": Could not register "
2065                                         "Video for Linux device\n");
2066                         vfd->minor = -1;
2067                         ret = -ENODEV;
2068                         goto error2;
2069                 }
2070                 video_set_drvdata(vfd, vout);
2071
2072                 /* Configure the overlay structure */
2073                 ret = omapvid_init(vid_dev->vouts[k], 0);
2074                 if (!ret)
2075                         goto success;
2076
2077 error2:
2078                 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2079                         omap_vout_release_vrfb(vout);
2080                 omap_vout_free_buffers(vout);
2081 error1:
2082                 video_device_release(vfd);
2083 error:
2084                 kfree(vout);
2085                 return ret;
2086
2087 success:
2088                 dev_info(&pdev->dev, ": registered and initialized"
2089                                 " video device %d\n", vfd->minor);
2090                 if (k == (pdev->num_resources - 1))
2091                         return 0;
2092         }
2093
2094         return -ENODEV;
2095 }
2096 /* Driver functions */
2097 static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2098 {
2099         struct video_device *vfd;
2100         struct omapvideo_info *ovid;
2101
2102         if (!vout)
2103                 return;
2104
2105         vfd = vout->vfd;
2106         ovid = &vout->vid_info;
2107         if (vfd) {
2108                 if (!video_is_registered(vfd)) {
2109                         /*
2110                          * The device was never registered, so release the
2111                          * video_device struct directly.
2112                          */
2113                         video_device_release(vfd);
2114                 } else {
2115                         /*
2116                          * The unregister function will release the video_device
2117                          * struct as well as unregistering it.
2118                          */
2119                         video_unregister_device(vfd);
2120                 }
2121         }
2122         if (ovid->rotation_type == VOUT_ROT_VRFB) {
2123                 omap_vout_release_vrfb(vout);
2124                 /* Free the VRFB buffer if allocated
2125                  * init time
2126                  */
2127                 if (vout->vrfb_static_allocation)
2128                         omap_vout_free_vrfb_buffers(vout);
2129         }
2130         omap_vout_free_buffers(vout);
2131
2132         kfree(vout);
2133 }
2134
2135 static int omap_vout_remove(struct platform_device *pdev)
2136 {
2137         int k;
2138         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2139         struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2140                         omap2video_device, v4l2_dev);
2141
2142         v4l2_device_unregister(v4l2_dev);
2143         for (k = 0; k < pdev->num_resources; k++)
2144                 omap_vout_cleanup_device(vid_dev->vouts[k]);
2145
2146         for (k = 0; k < vid_dev->num_displays; k++) {
2147                 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
2148                         vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
2149
2150                 omap_dss_put_device(vid_dev->displays[k]);
2151         }
2152         kfree(vid_dev);
2153         return 0;
2154 }
2155
2156 static int __init omap_vout_probe(struct platform_device *pdev)
2157 {
2158         int ret = 0, i;
2159         struct omap_overlay *ovl;
2160         struct omap_dss_device *dssdev = NULL;
2161         struct omap_dss_device *def_display;
2162         struct omap2video_device *vid_dev = NULL;
2163
2164         if (pdev->num_resources == 0) {
2165                 dev_err(&pdev->dev, "probed for an unknown device\n");
2166                 return -ENODEV;
2167         }
2168
2169         vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2170         if (vid_dev == NULL)
2171                 return -ENOMEM;
2172
2173         vid_dev->num_displays = 0;
2174         for_each_dss_dev(dssdev) {
2175                 omap_dss_get_device(dssdev);
2176                 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2177         }
2178
2179         if (vid_dev->num_displays == 0) {
2180                 dev_err(&pdev->dev, "no displays\n");
2181                 ret = -EINVAL;
2182                 goto probe_err0;
2183         }
2184
2185         vid_dev->num_overlays = omap_dss_get_num_overlays();
2186         for (i = 0; i < vid_dev->num_overlays; i++)
2187                 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2188
2189         vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2190         for (i = 0; i < vid_dev->num_managers; i++)
2191                 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2192
2193         /* Get the Video1 overlay and video2 overlay.
2194          * Setup the Display attached to that overlays
2195          */
2196         for (i = 1; i < vid_dev->num_overlays; i++) {
2197                 ovl = omap_dss_get_overlay(i);
2198                 if (ovl->manager && ovl->manager->device) {
2199                         def_display = ovl->manager->device;
2200                 } else {
2201                         dev_warn(&pdev->dev, "cannot find display\n");
2202                         def_display = NULL;
2203                 }
2204                 if (def_display) {
2205                         struct omap_dss_driver *dssdrv = def_display->driver;
2206
2207                         ret = dssdrv->enable(def_display);
2208                         if (ret) {
2209                                 /* Here we are not considering a error
2210                                  *  as display may be enabled by frame
2211                                  *  buffer driver
2212                                  */
2213                                 dev_warn(&pdev->dev,
2214                                         "'%s' Display already enabled\n",
2215                                         def_display->name);
2216                         }
2217                 }
2218         }
2219
2220         if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2221                 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2222                 ret = -ENODEV;
2223                 goto probe_err1;
2224         }
2225
2226         ret = omap_vout_create_video_devices(pdev);
2227         if (ret)
2228                 goto probe_err2;
2229
2230         for (i = 0; i < vid_dev->num_displays; i++) {
2231                 struct omap_dss_device *display = vid_dev->displays[i];
2232
2233                 if (display->driver->update)
2234                         display->driver->update(display, 0, 0,
2235                                         display->panel.timings.x_res,
2236                                         display->panel.timings.y_res);
2237         }
2238         return 0;
2239
2240 probe_err2:
2241         v4l2_device_unregister(&vid_dev->v4l2_dev);
2242 probe_err1:
2243         for (i = 1; i < vid_dev->num_overlays; i++) {
2244                 def_display = NULL;
2245                 ovl = omap_dss_get_overlay(i);
2246                 if (ovl->manager && ovl->manager->device)
2247                         def_display = ovl->manager->device;
2248
2249                 if (def_display && def_display->driver)
2250                         def_display->driver->disable(def_display);
2251         }
2252 probe_err0:
2253         kfree(vid_dev);
2254         return ret;
2255 }
2256
2257 static struct platform_driver omap_vout_driver = {
2258         .driver = {
2259                 .name = VOUT_NAME,
2260         },
2261         .probe = omap_vout_probe,
2262         .remove = omap_vout_remove,
2263 };
2264
2265 static int __init omap_vout_init(void)
2266 {
2267         if (platform_driver_register(&omap_vout_driver) != 0) {
2268                 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2269                 return -EINVAL;
2270         }
2271         return 0;
2272 }
2273
2274 static void omap_vout_cleanup(void)
2275 {
2276         platform_driver_unregister(&omap_vout_driver);
2277 }
2278
2279 late_initcall(omap_vout_init);
2280 module_exit(omap_vout_cleanup);