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