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