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