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