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