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