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