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