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