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