4 * Copyright (C) 2005-2010 Texas Instruments.
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.
10 * Leveraged code from the OMAP2 camera driver
11 * Video-for-Linux (Version 2) camera capture driver for
12 * the OMAP24xx camera controller.
14 * Author: Andy Lowe (source@mvista.com)
16 * Copyright (C) 2004 MontaVista Software, Inc.
17 * Copyright (C) 2010 Texas Instruments.
20 * 20-APR-2006 Khasim Modified VRFB based Rotation,
21 * The image data is always read from 0 degree
23 * to the virtual space of desired rotation angle
24 * 4-DEC-2006 Jian Changed to support better memory management
26 * 17-Nov-2008 Hardik Changed driver to use video_ioctl2
28 * 23-Feb-2010 Vaibhav H Modified to use new DSS2 interface
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>
43 #include <media/videobuf-dma-contig.h>
44 #include <media/v4l2-device.h>
45 #include <media/v4l2-ioctl.h>
48 #include <plat/vrfb.h>
49 #include <video/omapdss.h>
51 #include "omap_voutlib.h"
52 #include "omap_voutdef.h"
53 #include "omap_vout_vrfb.h"
55 MODULE_AUTHOR("Texas Instruments");
56 MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
57 MODULE_LICENSE("GPL");
59 /* Driver Configuration macros */
60 #define VOUT_NAME "omap_vout"
62 enum omap_vout_channels {
67 static struct videobuf_queue_ops video_vbq_ops;
68 /* Variables configurable through module params*/
69 static u32 video1_numbuffers = 3;
70 static u32 video2_numbuffers = 3;
71 static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
72 static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
73 static u32 vid1_static_vrfb_alloc;
74 static u32 vid2_static_vrfb_alloc;
77 /* Module parameters */
78 module_param(video1_numbuffers, uint, S_IRUGO);
79 MODULE_PARM_DESC(video1_numbuffers,
80 "Number of buffers to be allocated at init time for Video1 device.");
82 module_param(video2_numbuffers, uint, S_IRUGO);
83 MODULE_PARM_DESC(video2_numbuffers,
84 "Number of buffers to be allocated at init time for Video2 device.");
86 module_param(video1_bufsize, uint, S_IRUGO);
87 MODULE_PARM_DESC(video1_bufsize,
88 "Size of the buffer to be allocated for video1 device");
90 module_param(video2_bufsize, uint, S_IRUGO);
91 MODULE_PARM_DESC(video2_bufsize,
92 "Size of the buffer to be allocated for video2 device");
94 module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
95 MODULE_PARM_DESC(vid1_static_vrfb_alloc,
96 "Static allocation of the VRFB buffer for video1 device");
98 module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
99 MODULE_PARM_DESC(vid2_static_vrfb_alloc,
100 "Static allocation of the VRFB buffer for video2 device");
102 module_param(debug, bool, S_IRUGO);
103 MODULE_PARM_DESC(debug, "Debug level (0-1)");
105 /* list of image formats supported by OMAP2 video pipelines */
106 static const struct v4l2_fmtdesc omap_formats[] = {
108 /* Note: V4L2 defines RGB565 as:
111 * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
113 * We interpret RGB565 as:
116 * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
118 .description = "RGB565, le",
119 .pixelformat = V4L2_PIX_FMT_RGB565,
122 /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
123 * this for RGB24 unpack mode, the last 8 bits are ignored
125 .description = "RGB32, le",
126 .pixelformat = V4L2_PIX_FMT_RGB32,
129 /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
130 * this for RGB24 packed mode
133 .description = "RGB24, le",
134 .pixelformat = V4L2_PIX_FMT_RGB24,
137 .description = "YUYV (YUV 4:2:2), packed",
138 .pixelformat = V4L2_PIX_FMT_YUYV,
141 .description = "UYVY, packed",
142 .pixelformat = V4L2_PIX_FMT_UYVY,
146 #define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
151 static int omap_vout_try_format(struct v4l2_pix_format *pix)
155 pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
156 (u32)VID_MAX_HEIGHT);
157 pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
159 for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
160 if (pix->pixelformat == omap_formats[ifmt].pixelformat)
164 if (ifmt == NUM_OUTPUT_FORMATS)
167 pix->pixelformat = omap_formats[ifmt].pixelformat;
168 pix->field = V4L2_FIELD_ANY;
171 switch (pix->pixelformat) {
172 case V4L2_PIX_FMT_YUYV:
173 case V4L2_PIX_FMT_UYVY:
175 pix->colorspace = V4L2_COLORSPACE_JPEG;
178 case V4L2_PIX_FMT_RGB565:
179 case V4L2_PIX_FMT_RGB565X:
180 pix->colorspace = V4L2_COLORSPACE_SRGB;
183 case V4L2_PIX_FMT_RGB24:
184 pix->colorspace = V4L2_COLORSPACE_SRGB;
187 case V4L2_PIX_FMT_RGB32:
188 case V4L2_PIX_FMT_BGR32:
189 pix->colorspace = V4L2_COLORSPACE_SRGB;
193 pix->bytesperline = pix->width * bpp;
194 pix->sizeimage = pix->bytesperline * pix->height;
200 * omap_vout_uservirt_to_phys: This inline function is used to convert user
201 * space virtual address to physical address.
203 static u32 omap_vout_uservirt_to_phys(u32 virtp)
205 unsigned long physp = 0;
206 struct vm_area_struct *vma;
207 struct mm_struct *mm = current->mm;
209 /* For kernel direct-mapped memory, take the easy way */
210 if (virtp >= PAGE_OFFSET)
211 return virt_to_phys((void *) virtp);
213 down_read(¤t->mm->mmap_sem);
214 vma = find_vma(mm, virtp);
215 if (vma && (vma->vm_flags & VM_IO) && vma->vm_pgoff) {
216 /* this will catch, kernel-allocated, mmaped-to-usermode
218 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
219 up_read(¤t->mm->mmap_sem);
221 /* otherwise, use get_user_pages() for general userland pages */
222 int res, nr_pages = 1;
225 res = get_user_pages(current, current->mm, virtp, nr_pages, 1,
227 up_read(¤t->mm->mmap_sem);
229 if (res == nr_pages) {
230 physp = __pa(page_address(&pages[0]) +
231 (virtp & ~PAGE_MASK));
233 printk(KERN_WARNING VOUT_NAME
234 "get_user_pages failed\n");
243 * Free the V4L2 buffers
245 void omap_vout_free_buffers(struct omap_vout_device *vout)
249 /* Allocate memory for the buffers */
250 numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
251 vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
253 for (i = 0; i < numbuffers; i++) {
254 omap_vout_free_buffer(vout->buf_virt_addr[i],
256 vout->buf_phy_addr[i] = 0;
257 vout->buf_virt_addr[i] = 0;
262 * Convert V4L2 rotation to DSS rotation
263 * V4L2 understand 0, 90, 180, 270.
264 * Convert to 0, 1, 2 and 3 respectively for DSS
266 static int v4l2_rot_to_dss_rot(int v4l2_rotation,
267 enum dss_rotation *rotation, bool mirror)
271 switch (v4l2_rotation) {
273 *rotation = dss_rotation_90_degree;
276 *rotation = dss_rotation_180_degree;
279 *rotation = dss_rotation_270_degree;
282 *rotation = dss_rotation_0_degree;
290 static int omap_vout_calculate_offset(struct omap_vout_device *vout)
292 struct omapvideo_info *ovid;
293 struct v4l2_rect *crop = &vout->crop;
294 struct v4l2_pix_format *pix = &vout->pix;
295 int *cropped_offset = &vout->cropped_offset;
296 int ps = 2, line_length = 0;
298 ovid = &vout->vid_info;
300 if (ovid->rotation_type == VOUT_ROT_VRFB) {
301 omap_vout_calculate_vrfb_offset(vout);
303 vout->line_length = line_length = pix->width;
305 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
306 V4L2_PIX_FMT_UYVY == pix->pixelformat)
308 else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
310 else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
315 *cropped_offset = (line_length * ps) *
316 crop->top + crop->left * ps;
319 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
320 __func__, vout->cropped_offset);
326 * Convert V4L2 pixel format to DSS pixel format
328 static int video_mode_to_dss_mode(struct omap_vout_device *vout)
330 struct omap_overlay *ovl;
331 struct omapvideo_info *ovid;
332 struct v4l2_pix_format *pix = &vout->pix;
333 enum omap_color_mode mode;
335 ovid = &vout->vid_info;
336 ovl = ovid->overlays[0];
338 switch (pix->pixelformat) {
341 case V4L2_PIX_FMT_YUYV:
342 mode = OMAP_DSS_COLOR_YUV2;
344 case V4L2_PIX_FMT_UYVY:
345 mode = OMAP_DSS_COLOR_UYVY;
347 case V4L2_PIX_FMT_RGB565:
348 mode = OMAP_DSS_COLOR_RGB16;
350 case V4L2_PIX_FMT_RGB24:
351 mode = OMAP_DSS_COLOR_RGB24P;
353 case V4L2_PIX_FMT_RGB32:
354 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
355 OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
357 case V4L2_PIX_FMT_BGR32:
358 mode = OMAP_DSS_COLOR_RGBX32;
369 static int omapvid_setup_overlay(struct omap_vout_device *vout,
370 struct omap_overlay *ovl, int posx, int posy, int outw,
374 struct omap_overlay_info info;
375 int cropheight, cropwidth, pixheight, pixwidth;
377 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
378 (outw != vout->pix.width || outh != vout->pix.height)) {
383 vout->dss_mode = video_mode_to_dss_mode(vout);
384 if (vout->dss_mode == -EINVAL) {
389 /* Setup the input plane parameters according to
390 * rotation value selected.
392 if (is_rotation_90_or_270(vout)) {
393 cropheight = vout->crop.width;
394 cropwidth = vout->crop.height;
395 pixheight = vout->pix.width;
396 pixwidth = vout->pix.height;
398 cropheight = vout->crop.height;
399 cropwidth = vout->crop.width;
400 pixheight = vout->pix.height;
401 pixwidth = vout->pix.width;
404 ovl->get_overlay_info(ovl, &info);
406 info.width = cropwidth;
407 info.height = cropheight;
408 info.color_mode = vout->dss_mode;
409 info.mirror = vout->mirror;
412 info.out_width = outw;
413 info.out_height = outh;
414 info.global_alpha = vout->win.global_alpha;
415 if (!is_rotation_enabled(vout)) {
417 info.rotation_type = OMAP_DSS_ROT_DMA;
418 info.screen_width = pixwidth;
420 info.rotation = vout->rotation;
421 info.rotation_type = OMAP_DSS_ROT_VRFB;
422 info.screen_width = 2048;
425 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
426 "%s enable=%d addr=%x width=%d\n height=%d color_mode=%d\n"
427 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
428 "out_height=%d rotation_type=%d screen_width=%d\n",
429 __func__, info.enabled, info.paddr, info.width, info.height,
430 info.color_mode, info.rotation, info.mirror, info.pos_x,
431 info.pos_y, info.out_width, info.out_height, info.rotation_type,
434 ret = ovl->set_overlay_info(ovl, &info);
441 v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
446 * Initialize the overlay structure
448 static int omapvid_init(struct omap_vout_device *vout, u32 addr)
451 struct v4l2_window *win;
452 struct omap_overlay *ovl;
453 int posx, posy, outw, outh, temp;
454 struct omap_video_timings *timing;
455 struct omapvideo_info *ovid = &vout->vid_info;
458 for (i = 0; i < ovid->num_overlays; i++) {
459 ovl = ovid->overlays[i];
460 if (!ovl->manager || !ovl->manager->device)
463 timing = &ovl->manager->device->panel.timings;
466 outh = win->w.height;
467 switch (vout->rotation) {
468 case dss_rotation_90_degree:
469 /* Invert the height and width for 90
470 * and 270 degree rotation
475 posy = (timing->y_res - win->w.width) - win->w.left;
479 case dss_rotation_180_degree:
480 posx = (timing->x_res - win->w.width) - win->w.left;
481 posy = (timing->y_res - win->w.height) - win->w.top;
484 case dss_rotation_270_degree:
489 posx = (timing->x_res - win->w.height) - win->w.top;
498 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
501 goto omapvid_init_err;
506 v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
511 * Apply the changes set the go bit of DSS
513 static int omapvid_apply_changes(struct omap_vout_device *vout)
516 struct omap_overlay *ovl;
517 struct omapvideo_info *ovid = &vout->vid_info;
519 for (i = 0; i < ovid->num_overlays; i++) {
520 ovl = ovid->overlays[i];
521 if (!ovl->manager || !ovl->manager->device)
523 ovl->manager->apply(ovl->manager);
529 static void omap_vout_isr(void *arg, unsigned int irqstatus)
533 struct omap_overlay *ovl;
534 struct timeval timevalue;
535 struct omapvideo_info *ovid;
536 struct omap_dss_device *cur_display;
537 struct omap_vout_device *vout = (struct omap_vout_device *)arg;
539 if (!vout->streaming)
542 ovid = &vout->vid_info;
543 ovl = ovid->overlays[0];
544 /* get the display device attached to the overlay */
545 if (!ovl->manager || !ovl->manager->device)
548 cur_display = ovl->manager->device;
550 spin_lock(&vout->vbq_lock);
551 do_gettimeofday(&timevalue);
553 if (cur_display->type != OMAP_DISPLAY_TYPE_VENC) {
554 switch (cur_display->type) {
555 case OMAP_DISPLAY_TYPE_DPI:
556 if (!(irqstatus & (DISPC_IRQ_VSYNC | DISPC_IRQ_VSYNC2)))
559 case OMAP_DISPLAY_TYPE_HDMI:
560 if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
566 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
567 vout->cur_frm->ts = timevalue;
568 vout->cur_frm->state = VIDEOBUF_DONE;
569 wake_up_interruptible(&vout->cur_frm->done);
570 vout->cur_frm = vout->next_frm;
573 if (list_empty(&vout->dma_queue))
576 vout->next_frm = list_entry(vout->dma_queue.next,
577 struct videobuf_buffer, queue);
578 list_del(&vout->next_frm->queue);
580 vout->next_frm->state = VIDEOBUF_ACTIVE;
582 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
583 + vout->cropped_offset;
585 /* First save the configuration in ovelray structure */
586 ret = omapvid_init(vout, addr);
588 printk(KERN_ERR VOUT_NAME
589 "failed to set overlay info\n");
590 /* Enable the pipeline and set the Go bit */
591 ret = omapvid_apply_changes(vout);
593 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
596 if (vout->first_int) {
600 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
602 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
608 if (fid != vout->field_id) {
610 vout->field_id = fid;
615 if (vout->cur_frm == vout->next_frm)
618 vout->cur_frm->ts = timevalue;
619 vout->cur_frm->state = VIDEOBUF_DONE;
620 wake_up_interruptible(&vout->cur_frm->done);
621 vout->cur_frm = vout->next_frm;
622 } else if (1 == fid) {
623 if (list_empty(&vout->dma_queue) ||
624 (vout->cur_frm != vout->next_frm))
627 vout->next_frm = list_entry(vout->dma_queue.next,
628 struct videobuf_buffer, queue);
629 list_del(&vout->next_frm->queue);
631 vout->next_frm->state = VIDEOBUF_ACTIVE;
632 addr = (unsigned long)
633 vout->queued_buf_addr[vout->next_frm->i] +
634 vout->cropped_offset;
635 /* First save the configuration in ovelray structure */
636 ret = omapvid_init(vout, addr);
638 printk(KERN_ERR VOUT_NAME
639 "failed to set overlay info\n");
640 /* Enable the pipeline and set the Go bit */
641 ret = omapvid_apply_changes(vout);
643 printk(KERN_ERR VOUT_NAME
644 "failed to change mode\n");
650 spin_unlock(&vout->vbq_lock);
654 /* Video buffer call backs */
657 * Buffer setup function is called by videobuf layer when REQBUF ioctl is
658 * called. This is used to setup buffers and return size and count of
659 * buffers allocated. After the call to this buffer, videobuf layer will
660 * setup buffer queue depending on the size and count of buffers
662 static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
665 int startindex = 0, i, j;
666 u32 phy_addr = 0, virt_addr = 0;
667 struct omap_vout_device *vout = q->priv_data;
668 struct omapvideo_info *ovid = &vout->vid_info;
673 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
676 startindex = (vout->vid == OMAP_VIDEO1) ?
677 video1_numbuffers : video2_numbuffers;
678 if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
681 if (ovid->rotation_type == VOUT_ROT_VRFB) {
682 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
686 if (V4L2_MEMORY_MMAP != vout->memory)
689 /* Now allocated the V4L2 buffers */
690 *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
691 startindex = (vout->vid == OMAP_VIDEO1) ?
692 video1_numbuffers : video2_numbuffers;
694 /* Check the size of the buffer */
695 if (*size > vout->buffer_size) {
696 v4l2_err(&vout->vid_dev->v4l2_dev,
697 "buffer allocation mismatch [%u] [%u]\n",
698 *size, vout->buffer_size);
702 for (i = startindex; i < *count; i++) {
703 vout->buffer_size = *size;
705 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
708 if (ovid->rotation_type == VOUT_ROT_NONE) {
711 if (!is_rotation_enabled(vout))
713 /* Free the VRFB buffers if no space for V4L2 buffers */
714 for (j = i; j < *count; j++) {
715 omap_vout_free_buffer(
716 vout->smsshado_virt_addr[j],
717 vout->smsshado_size);
718 vout->smsshado_virt_addr[j] = 0;
719 vout->smsshado_phy_addr[j] = 0;
723 vout->buf_virt_addr[i] = virt_addr;
724 vout->buf_phy_addr[i] = phy_addr;
726 *count = vout->buffer_allocated = i;
732 * Free the V4L2 buffers additionally allocated than default
735 static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
737 int num_buffers = 0, i;
739 num_buffers = (vout->vid == OMAP_VIDEO1) ?
740 video1_numbuffers : video2_numbuffers;
742 for (i = num_buffers; i < vout->buffer_allocated; i++) {
743 if (vout->buf_virt_addr[i])
744 omap_vout_free_buffer(vout->buf_virt_addr[i],
747 vout->buf_virt_addr[i] = 0;
748 vout->buf_phy_addr[i] = 0;
750 vout->buffer_allocated = num_buffers;
754 * This function will be called when VIDIOC_QBUF ioctl is called.
755 * It prepare buffers before give out for the display. This function
756 * converts user space virtual address into physical address if userptr memory
757 * exchange mechanism is used. If rotation is enabled, it copies entire
758 * buffer into VRFB memory space before giving it to the DSS.
760 static int omap_vout_buffer_prepare(struct videobuf_queue *q,
761 struct videobuf_buffer *vb,
762 enum v4l2_field field)
764 struct omap_vout_device *vout = q->priv_data;
765 struct omapvideo_info *ovid = &vout->vid_info;
767 if (VIDEOBUF_NEEDS_INIT == vb->state) {
768 vb->width = vout->pix.width;
769 vb->height = vout->pix.height;
770 vb->size = vb->width * vb->height * vout->bpp;
773 vb->state = VIDEOBUF_PREPARED;
774 /* if user pointer memory mechanism is used, get the physical
775 * address of the buffer
777 if (V4L2_MEMORY_USERPTR == vb->memory) {
780 /* Physical address */
781 vout->queued_buf_addr[vb->i] = (u8 *)
782 omap_vout_uservirt_to_phys(vb->baddr);
787 addr = (unsigned long) vout->buf_virt_addr[vb->i];
788 size = (unsigned long) vb->size;
790 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
791 size, DMA_TO_DEVICE);
792 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
793 v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
795 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
798 if (ovid->rotation_type == VOUT_ROT_VRFB)
799 return omap_vout_prepare_vrfb(vout, vb);
805 * Buffer queue function will be called from the videobuf layer when _QBUF
806 * ioctl is called. It is used to enqueue buffer, which is ready to be
809 static void omap_vout_buffer_queue(struct videobuf_queue *q,
810 struct videobuf_buffer *vb)
812 struct omap_vout_device *vout = q->priv_data;
814 /* Driver is also maintainig a queue. So enqueue buffer in the driver
816 list_add_tail(&vb->queue, &vout->dma_queue);
818 vb->state = VIDEOBUF_QUEUED;
822 * Buffer release function is called from videobuf layer to release buffer
823 * which are already allocated
825 static void omap_vout_buffer_release(struct videobuf_queue *q,
826 struct videobuf_buffer *vb)
828 struct omap_vout_device *vout = q->priv_data;
830 vb->state = VIDEOBUF_NEEDS_INIT;
832 if (V4L2_MEMORY_MMAP != vout->memory)
839 static unsigned int omap_vout_poll(struct file *file,
840 struct poll_table_struct *wait)
842 struct omap_vout_device *vout = file->private_data;
843 struct videobuf_queue *q = &vout->vbq;
845 return videobuf_poll_stream(file, q, wait);
848 static void omap_vout_vm_open(struct vm_area_struct *vma)
850 struct omap_vout_device *vout = vma->vm_private_data;
852 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
853 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
857 static void omap_vout_vm_close(struct vm_area_struct *vma)
859 struct omap_vout_device *vout = vma->vm_private_data;
861 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
862 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
866 static struct vm_operations_struct omap_vout_vm_ops = {
867 .open = omap_vout_vm_open,
868 .close = omap_vout_vm_close,
871 static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
875 unsigned long start = vma->vm_start;
876 unsigned long size = (vma->vm_end - vma->vm_start);
877 struct omap_vout_device *vout = file->private_data;
878 struct videobuf_queue *q = &vout->vbq;
880 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
881 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
882 vma->vm_pgoff, vma->vm_start, vma->vm_end);
884 /* look for the buffer to map */
885 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
886 if (NULL == q->bufs[i])
888 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
890 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
894 if (VIDEO_MAX_FRAME == i) {
895 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
896 "offset invalid [offset=0x%lx]\n",
897 (vma->vm_pgoff << PAGE_SHIFT));
900 /* Check the size of the buffer */
901 if (size > vout->buffer_size) {
902 v4l2_err(&vout->vid_dev->v4l2_dev,
903 "insufficient memory [%lu] [%u]\n",
904 size, vout->buffer_size);
908 q->bufs[i]->baddr = vma->vm_start;
910 vma->vm_flags |= VM_RESERVED;
911 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
912 vma->vm_ops = &omap_vout_vm_ops;
913 vma->vm_private_data = (void *) vout;
914 pos = (void *)vout->buf_virt_addr[i];
915 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
918 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
919 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
926 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
931 static int omap_vout_release(struct file *file)
934 struct videobuf_queue *q;
935 struct omapvideo_info *ovid;
936 struct omap_vout_device *vout = file->private_data;
938 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
939 ovid = &vout->vid_info;
945 /* Disable all the overlay managers connected with this interface */
946 for (i = 0; i < ovid->num_overlays; i++) {
947 struct omap_overlay *ovl = ovid->overlays[i];
948 if (ovl->manager && ovl->manager->device) {
949 struct omap_overlay_info info;
950 ovl->get_overlay_info(ovl, &info);
952 ovl->set_overlay_info(ovl, &info);
955 /* Turn off the pipeline */
956 ret = omapvid_apply_changes(vout);
958 v4l2_warn(&vout->vid_dev->v4l2_dev,
959 "Unable to apply changes\n");
961 /* Free all buffers */
962 omap_vout_free_extra_buffers(vout);
964 /* Free the VRFB buffers only if they are allocated
965 * during reqbufs. Don't free if init time allocated
967 if (ovid->rotation_type == VOUT_ROT_VRFB) {
968 if (!vout->vrfb_static_allocation)
969 omap_vout_free_vrfb_buffers(vout);
971 videobuf_mmap_free(q);
973 /* Even if apply changes fails we should continue
974 freeing allocated memory */
975 if (vout->streaming) {
978 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
979 DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
980 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
983 videobuf_streamoff(q);
984 videobuf_queue_cancel(q);
987 if (vout->mmap_count != 0)
988 vout->mmap_count = 0;
991 file->private_data = NULL;
993 if (vout->buffer_allocated)
994 videobuf_mmap_free(q);
996 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1000 static int omap_vout_open(struct file *file)
1002 struct videobuf_queue *q;
1003 struct omap_vout_device *vout = NULL;
1005 vout = video_drvdata(file);
1006 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1011 /* for now, we only support single open */
1017 file->private_data = vout;
1018 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1021 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1022 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1023 video_vbq_ops.buf_release = omap_vout_buffer_release;
1024 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1025 spin_lock_init(&vout->vbq_lock);
1027 videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1028 &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
1029 sizeof(struct videobuf_buffer), vout, NULL);
1031 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1038 static int vidioc_querycap(struct file *file, void *fh,
1039 struct v4l2_capability *cap)
1041 struct omap_vout_device *vout = fh;
1043 strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1044 strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1045 cap->bus_info[0] = '\0';
1046 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT |
1047 V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
1052 static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1053 struct v4l2_fmtdesc *fmt)
1055 int index = fmt->index;
1057 if (index >= NUM_OUTPUT_FORMATS)
1060 fmt->flags = omap_formats[index].flags;
1061 strlcpy(fmt->description, omap_formats[index].description,
1062 sizeof(fmt->description));
1063 fmt->pixelformat = omap_formats[index].pixelformat;
1068 static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1069 struct v4l2_format *f)
1071 struct omap_vout_device *vout = fh;
1073 f->fmt.pix = vout->pix;
1078 static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1079 struct v4l2_format *f)
1081 struct omap_overlay *ovl;
1082 struct omapvideo_info *ovid;
1083 struct omap_video_timings *timing;
1084 struct omap_vout_device *vout = fh;
1086 ovid = &vout->vid_info;
1087 ovl = ovid->overlays[0];
1089 if (!ovl->manager || !ovl->manager->device)
1091 /* get the display device attached to the overlay */
1092 timing = &ovl->manager->device->panel.timings;
1094 vout->fbuf.fmt.height = timing->y_res;
1095 vout->fbuf.fmt.width = timing->x_res;
1097 omap_vout_try_format(&f->fmt.pix);
1101 static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1102 struct v4l2_format *f)
1105 struct omap_overlay *ovl;
1106 struct omapvideo_info *ovid;
1107 struct omap_video_timings *timing;
1108 struct omap_vout_device *vout = fh;
1110 if (vout->streaming)
1113 mutex_lock(&vout->lock);
1115 ovid = &vout->vid_info;
1116 ovl = ovid->overlays[0];
1118 /* get the display device attached to the overlay */
1119 if (!ovl->manager || !ovl->manager->device) {
1121 goto s_fmt_vid_out_exit;
1123 timing = &ovl->manager->device->panel.timings;
1125 /* We dont support RGB24-packed mode if vrfb rotation
1127 if ((is_rotation_enabled(vout)) &&
1128 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1130 goto s_fmt_vid_out_exit;
1133 /* get the framebuffer parameters */
1135 if (is_rotation_90_or_270(vout)) {
1136 vout->fbuf.fmt.height = timing->x_res;
1137 vout->fbuf.fmt.width = timing->y_res;
1139 vout->fbuf.fmt.height = timing->y_res;
1140 vout->fbuf.fmt.width = timing->x_res;
1143 /* change to samller size is OK */
1145 bpp = omap_vout_try_format(&f->fmt.pix);
1146 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1148 /* try & set the new output format */
1150 vout->pix = f->fmt.pix;
1153 /* If YUYV then vrfb bpp is 2, for others its 1 */
1154 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1155 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1158 /* set default crop and win */
1159 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1161 /* Save the changes in the overlay strcuture */
1162 ret = omapvid_init(vout, 0);
1164 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1165 goto s_fmt_vid_out_exit;
1171 mutex_unlock(&vout->lock);
1175 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1176 struct v4l2_format *f)
1179 struct omap_vout_device *vout = fh;
1180 struct omap_overlay *ovl;
1181 struct omapvideo_info *ovid;
1182 struct v4l2_window *win = &f->fmt.win;
1184 ovid = &vout->vid_info;
1185 ovl = ovid->overlays[0];
1187 ret = omap_vout_try_window(&vout->fbuf, win);
1190 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1191 win->global_alpha = 255;
1193 win->global_alpha = f->fmt.win.global_alpha;
1199 static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1200 struct v4l2_format *f)
1203 struct omap_overlay *ovl;
1204 struct omapvideo_info *ovid;
1205 struct omap_vout_device *vout = fh;
1206 struct v4l2_window *win = &f->fmt.win;
1208 mutex_lock(&vout->lock);
1209 ovid = &vout->vid_info;
1210 ovl = ovid->overlays[0];
1212 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1214 /* Video1 plane does not support global alpha on OMAP3 */
1215 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1216 vout->win.global_alpha = 255;
1218 vout->win.global_alpha = f->fmt.win.global_alpha;
1220 vout->win.chromakey = f->fmt.win.chromakey;
1222 mutex_unlock(&vout->lock);
1226 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
1227 struct v4l2_fmtdesc *fmt)
1229 int index = fmt->index;
1231 if (index >= NUM_OUTPUT_FORMATS)
1234 fmt->flags = omap_formats[index].flags;
1235 strlcpy(fmt->description, omap_formats[index].description,
1236 sizeof(fmt->description));
1237 fmt->pixelformat = omap_formats[index].pixelformat;
1241 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1242 struct v4l2_format *f)
1245 struct omap_overlay *ovl;
1246 struct omapvideo_info *ovid;
1247 struct omap_vout_device *vout = fh;
1248 struct omap_overlay_manager_info info;
1249 struct v4l2_window *win = &f->fmt.win;
1251 ovid = &vout->vid_info;
1252 ovl = ovid->overlays[0];
1254 win->w = vout->win.w;
1255 win->field = vout->win.field;
1256 win->global_alpha = vout->win.global_alpha;
1258 if (ovl->manager && ovl->manager->get_manager_info) {
1259 ovl->manager->get_manager_info(ovl->manager, &info);
1260 key_value = info.trans_key;
1262 win->chromakey = key_value;
1266 static int vidioc_cropcap(struct file *file, void *fh,
1267 struct v4l2_cropcap *cropcap)
1269 struct omap_vout_device *vout = fh;
1270 struct v4l2_pix_format *pix = &vout->pix;
1272 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1275 /* Width and height are always even */
1276 cropcap->bounds.width = pix->width & ~1;
1277 cropcap->bounds.height = pix->height & ~1;
1279 omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1280 cropcap->pixelaspect.numerator = 1;
1281 cropcap->pixelaspect.denominator = 1;
1285 static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1287 struct omap_vout_device *vout = fh;
1289 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1291 crop->c = vout->crop;
1295 static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1298 struct omap_vout_device *vout = fh;
1299 struct omapvideo_info *ovid;
1300 struct omap_overlay *ovl;
1301 struct omap_video_timings *timing;
1303 if (vout->streaming)
1306 mutex_lock(&vout->lock);
1307 ovid = &vout->vid_info;
1308 ovl = ovid->overlays[0];
1310 if (!ovl->manager || !ovl->manager->device) {
1314 /* get the display device attached to the overlay */
1315 timing = &ovl->manager->device->panel.timings;
1317 if (is_rotation_90_or_270(vout)) {
1318 vout->fbuf.fmt.height = timing->x_res;
1319 vout->fbuf.fmt.width = timing->y_res;
1321 vout->fbuf.fmt.height = timing->y_res;
1322 vout->fbuf.fmt.width = timing->x_res;
1325 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1326 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1327 &vout->fbuf, &crop->c);
1330 mutex_unlock(&vout->lock);
1334 static int vidioc_queryctrl(struct file *file, void *fh,
1335 struct v4l2_queryctrl *ctrl)
1340 case V4L2_CID_ROTATE:
1341 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1343 case V4L2_CID_BG_COLOR:
1344 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1346 case V4L2_CID_VFLIP:
1347 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1350 ctrl->name[0] = '\0';
1356 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1359 struct omap_vout_device *vout = fh;
1362 case V4L2_CID_ROTATE:
1363 ctrl->value = vout->control[0].value;
1365 case V4L2_CID_BG_COLOR:
1367 struct omap_overlay_manager_info info;
1368 struct omap_overlay *ovl;
1370 ovl = vout->vid_info.overlays[0];
1371 if (!ovl->manager || !ovl->manager->get_manager_info) {
1376 ovl->manager->get_manager_info(ovl->manager, &info);
1377 ctrl->value = info.default_color;
1380 case V4L2_CID_VFLIP:
1381 ctrl->value = vout->control[2].value;
1389 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1392 struct omap_vout_device *vout = fh;
1395 case V4L2_CID_ROTATE:
1397 struct omapvideo_info *ovid;
1398 int rotation = a->value;
1400 ovid = &vout->vid_info;
1402 mutex_lock(&vout->lock);
1403 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1404 mutex_unlock(&vout->lock);
1409 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1410 mutex_unlock(&vout->lock);
1415 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1417 mutex_unlock(&vout->lock);
1422 vout->control[0].value = rotation;
1423 mutex_unlock(&vout->lock);
1426 case V4L2_CID_BG_COLOR:
1428 struct omap_overlay *ovl;
1429 unsigned int color = a->value;
1430 struct omap_overlay_manager_info info;
1432 ovl = vout->vid_info.overlays[0];
1434 mutex_lock(&vout->lock);
1435 if (!ovl->manager || !ovl->manager->get_manager_info) {
1436 mutex_unlock(&vout->lock);
1441 ovl->manager->get_manager_info(ovl->manager, &info);
1442 info.default_color = color;
1443 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1444 mutex_unlock(&vout->lock);
1449 vout->control[1].value = color;
1450 mutex_unlock(&vout->lock);
1453 case V4L2_CID_VFLIP:
1455 struct omap_overlay *ovl;
1456 struct omapvideo_info *ovid;
1457 unsigned int mirror = a->value;
1459 ovid = &vout->vid_info;
1460 ovl = ovid->overlays[0];
1462 mutex_lock(&vout->lock);
1463 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1464 mutex_unlock(&vout->lock);
1469 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1470 mutex_unlock(&vout->lock);
1474 vout->mirror = mirror;
1475 vout->control[2].value = mirror;
1476 mutex_unlock(&vout->lock);
1485 static int vidioc_reqbufs(struct file *file, void *fh,
1486 struct v4l2_requestbuffers *req)
1489 unsigned int i, num_buffers = 0;
1490 struct omap_vout_device *vout = fh;
1491 struct videobuf_queue *q = &vout->vbq;
1493 if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
1495 /* if memory is not mmp or userptr
1497 if ((V4L2_MEMORY_MMAP != req->memory) &&
1498 (V4L2_MEMORY_USERPTR != req->memory))
1501 mutex_lock(&vout->lock);
1502 /* Cannot be requested when streaming is on */
1503 if (vout->streaming) {
1508 /* If buffers are already allocated free them */
1509 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1510 if (vout->mmap_count) {
1514 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1515 video1_numbuffers : video2_numbuffers;
1516 for (i = num_buffers; i < vout->buffer_allocated; i++) {
1517 omap_vout_free_buffer(vout->buf_virt_addr[i],
1519 vout->buf_virt_addr[i] = 0;
1520 vout->buf_phy_addr[i] = 0;
1522 vout->buffer_allocated = num_buffers;
1523 videobuf_mmap_free(q);
1524 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1525 if (vout->buffer_allocated) {
1526 videobuf_mmap_free(q);
1527 for (i = 0; i < vout->buffer_allocated; i++) {
1531 vout->buffer_allocated = 0;
1535 /*store the memory type in data structure */
1536 vout->memory = req->memory;
1538 INIT_LIST_HEAD(&vout->dma_queue);
1540 /* call videobuf_reqbufs api */
1541 ret = videobuf_reqbufs(q, req);
1545 vout->buffer_allocated = req->count;
1548 mutex_unlock(&vout->lock);
1552 static int vidioc_querybuf(struct file *file, void *fh,
1553 struct v4l2_buffer *b)
1555 struct omap_vout_device *vout = fh;
1557 return videobuf_querybuf(&vout->vbq, b);
1560 static int vidioc_qbuf(struct file *file, void *fh,
1561 struct v4l2_buffer *buffer)
1563 struct omap_vout_device *vout = fh;
1564 struct videobuf_queue *q = &vout->vbq;
1566 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1567 (buffer->index >= vout->buffer_allocated) ||
1568 (q->bufs[buffer->index]->memory != buffer->memory)) {
1571 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1572 if ((buffer->length < vout->pix.sizeimage) ||
1573 (0 == buffer->m.userptr)) {
1578 if ((is_rotation_enabled(vout)) &&
1579 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1580 v4l2_warn(&vout->vid_dev->v4l2_dev,
1581 "DMA Channel not allocated for Rotation\n");
1585 return videobuf_qbuf(q, buffer);
1588 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1590 struct omap_vout_device *vout = fh;
1591 struct videobuf_queue *q = &vout->vbq;
1596 struct videobuf_buffer *vb;
1598 vb = q->bufs[b->index];
1600 if (!vout->streaming)
1603 if (file->f_flags & O_NONBLOCK)
1604 /* Call videobuf_dqbuf for non blocking mode */
1605 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
1607 /* Call videobuf_dqbuf for blocking mode */
1608 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
1610 addr = (unsigned long) vout->buf_phy_addr[vb->i];
1611 size = (unsigned long) vb->size;
1612 dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
1613 size, DMA_TO_DEVICE);
1617 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1620 u32 addr = 0, mask = 0;
1621 struct omap_vout_device *vout = fh;
1622 struct videobuf_queue *q = &vout->vbq;
1623 struct omapvideo_info *ovid = &vout->vid_info;
1625 mutex_lock(&vout->lock);
1627 if (vout->streaming) {
1632 ret = videobuf_streamon(q);
1636 if (list_empty(&vout->dma_queue)) {
1641 /* Get the next frame from the buffer queue */
1642 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1643 struct videobuf_buffer, queue);
1644 /* Remove buffer from the buffer queue */
1645 list_del(&vout->cur_frm->queue);
1646 /* Mark state of the current frame to active */
1647 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1648 /* Initialize field_id and started member */
1651 /* set flag here. Next QBUF will start DMA */
1652 vout->streaming = 1;
1654 vout->first_int = 1;
1656 if (omap_vout_calculate_offset(vout)) {
1660 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1661 + vout->cropped_offset;
1663 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1666 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1668 for (j = 0; j < ovid->num_overlays; j++) {
1669 struct omap_overlay *ovl = ovid->overlays[j];
1671 if (ovl->manager && ovl->manager->device) {
1672 struct omap_overlay_info info;
1673 ovl->get_overlay_info(ovl, &info);
1676 if (ovl->set_overlay_info(ovl, &info)) {
1683 /* First save the configuration in ovelray structure */
1684 ret = omapvid_init(vout, addr);
1686 v4l2_err(&vout->vid_dev->v4l2_dev,
1687 "failed to set overlay info\n");
1688 /* Enable the pipeline and set the Go bit */
1689 ret = omapvid_apply_changes(vout);
1691 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1697 ret = videobuf_streamoff(q);
1699 mutex_unlock(&vout->lock);
1703 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1707 struct omap_vout_device *vout = fh;
1708 struct omapvideo_info *ovid = &vout->vid_info;
1710 if (!vout->streaming)
1713 vout->streaming = 0;
1714 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1717 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1719 for (j = 0; j < ovid->num_overlays; j++) {
1720 struct omap_overlay *ovl = ovid->overlays[j];
1722 if (ovl->manager && ovl->manager->device) {
1723 struct omap_overlay_info info;
1725 ovl->get_overlay_info(ovl, &info);
1727 ret = ovl->set_overlay_info(ovl, &info);
1729 v4l2_err(&vout->vid_dev->v4l2_dev,
1730 "failed to update overlay info in streamoff\n");
1734 /* Turn of the pipeline */
1735 ret = omapvid_apply_changes(vout);
1737 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
1740 INIT_LIST_HEAD(&vout->dma_queue);
1741 ret = videobuf_streamoff(&vout->vbq);
1746 static int vidioc_s_fbuf(struct file *file, void *fh,
1747 struct v4l2_framebuffer *a)
1750 struct omap_overlay *ovl;
1751 struct omapvideo_info *ovid;
1752 struct omap_vout_device *vout = fh;
1753 struct omap_overlay_manager_info info;
1754 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1756 ovid = &vout->vid_info;
1757 ovl = ovid->overlays[0];
1759 /* OMAP DSS doesn't support Source and Destination color
1761 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1762 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1764 /* OMAP DSS Doesn't support the Destination color key
1765 and alpha blending together */
1766 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1767 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1770 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1771 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1772 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
1774 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1776 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1777 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1778 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1780 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
1782 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1783 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1787 if (ovl->manager && ovl->manager->get_manager_info &&
1788 ovl->manager->set_manager_info) {
1790 ovl->manager->get_manager_info(ovl->manager, &info);
1791 info.trans_enabled = enable;
1792 info.trans_key_type = key_type;
1793 info.trans_key = vout->win.chromakey;
1795 if (ovl->manager->set_manager_info(ovl->manager, &info))
1798 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1799 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1802 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1805 if (ovl->manager && ovl->manager->get_manager_info &&
1806 ovl->manager->set_manager_info) {
1807 ovl->manager->get_manager_info(ovl->manager, &info);
1808 /* enable this only if there is no zorder cap */
1809 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1810 info.partial_alpha_enabled = enable;
1811 if (ovl->manager->set_manager_info(ovl->manager, &info))
1818 static int vidioc_g_fbuf(struct file *file, void *fh,
1819 struct v4l2_framebuffer *a)
1821 struct omap_overlay *ovl;
1822 struct omapvideo_info *ovid;
1823 struct omap_vout_device *vout = fh;
1824 struct omap_overlay_manager_info info;
1826 ovid = &vout->vid_info;
1827 ovl = ovid->overlays[0];
1829 /* The video overlay must stay within the framebuffer and can't be
1830 positioned independently. */
1831 a->flags = V4L2_FBUF_FLAG_OVERLAY;
1832 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1833 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1835 if (ovl->manager && ovl->manager->get_manager_info) {
1836 ovl->manager->get_manager_info(ovl->manager, &info);
1837 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1838 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1839 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1840 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1842 if (ovl->manager && ovl->manager->get_manager_info) {
1843 ovl->manager->get_manager_info(ovl->manager, &info);
1844 if (info.partial_alpha_enabled)
1845 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1851 static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1852 .vidioc_querycap = vidioc_querycap,
1853 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1854 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1855 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1856 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1857 .vidioc_queryctrl = vidioc_queryctrl,
1858 .vidioc_g_ctrl = vidioc_g_ctrl,
1859 .vidioc_s_fbuf = vidioc_s_fbuf,
1860 .vidioc_g_fbuf = vidioc_g_fbuf,
1861 .vidioc_s_ctrl = vidioc_s_ctrl,
1862 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1863 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1864 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1865 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1866 .vidioc_cropcap = vidioc_cropcap,
1867 .vidioc_g_crop = vidioc_g_crop,
1868 .vidioc_s_crop = vidioc_s_crop,
1869 .vidioc_reqbufs = vidioc_reqbufs,
1870 .vidioc_querybuf = vidioc_querybuf,
1871 .vidioc_qbuf = vidioc_qbuf,
1872 .vidioc_dqbuf = vidioc_dqbuf,
1873 .vidioc_streamon = vidioc_streamon,
1874 .vidioc_streamoff = vidioc_streamoff,
1877 static const struct v4l2_file_operations omap_vout_fops = {
1878 .owner = THIS_MODULE,
1879 .poll = omap_vout_poll,
1880 .unlocked_ioctl = video_ioctl2,
1881 .mmap = omap_vout_mmap,
1882 .open = omap_vout_open,
1883 .release = omap_vout_release,
1886 /* Init functions used during driver initialization */
1887 /* Initial setup of video_data */
1888 static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1890 struct video_device *vfd;
1891 struct v4l2_pix_format *pix;
1892 struct v4l2_control *control;
1893 struct omap_dss_device *display =
1894 vout->vid_info.overlays[0]->manager->device;
1896 /* set the default pix */
1899 /* Set the default picture of QVGA */
1900 pix->width = QQVGA_WIDTH;
1901 pix->height = QQVGA_HEIGHT;
1903 /* Default pixel format is RGB 5-6-5 */
1904 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1905 pix->field = V4L2_FIELD_ANY;
1906 pix->bytesperline = pix->width * 2;
1907 pix->sizeimage = pix->bytesperline * pix->height;
1909 pix->colorspace = V4L2_COLORSPACE_JPEG;
1911 vout->bpp = RGB565_BPP;
1912 vout->fbuf.fmt.width = display->panel.timings.x_res;
1913 vout->fbuf.fmt.height = display->panel.timings.y_res;
1915 /* Set the data structures for the overlay parameters*/
1916 vout->win.global_alpha = 255;
1917 vout->fbuf.flags = 0;
1918 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1919 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1920 vout->win.chromakey = 0;
1922 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1924 /*Initialize the control variables for
1925 rotation, flipping and background color. */
1926 control = vout->control;
1927 control[0].id = V4L2_CID_ROTATE;
1928 control[0].value = 0;
1931 vout->control[2].id = V4L2_CID_HFLIP;
1932 vout->control[2].value = 0;
1933 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1936 control[1].id = V4L2_CID_BG_COLOR;
1937 control[1].value = 0;
1939 /* initialize the video_device struct */
1940 vfd = vout->vfd = video_device_alloc();
1943 printk(KERN_ERR VOUT_NAME ": could not allocate"
1944 " video device struct\n");
1947 vfd->release = video_device_release;
1948 vfd->ioctl_ops = &vout_ioctl_ops;
1950 strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1952 vfd->fops = &omap_vout_fops;
1953 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
1954 mutex_init(&vout->lock);
1961 /* Setup video buffers */
1962 static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1967 struct omapvideo_info *ovid;
1968 struct omap_vout_device *vout;
1969 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1970 struct omap2video_device *vid_dev =
1971 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1973 vout = vid_dev->vouts[vid_num];
1974 ovid = &vout->vid_info;
1976 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1977 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1978 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1980 for (i = 0; i < numbuffers; i++) {
1981 vout->buf_virt_addr[i] =
1982 omap_vout_alloc_buffer(vout->buffer_size,
1983 (u32 *) &vout->buf_phy_addr[i]);
1984 if (!vout->buf_virt_addr[i]) {
1991 vout->cropped_offset = 0;
1993 if (ovid->rotation_type == VOUT_ROT_VRFB) {
1994 int static_vrfb_allocation = (vid_num == 0) ?
1995 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1996 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1997 static_vrfb_allocation);
2003 for (i = 0; i < numbuffers; i++) {
2004 omap_vout_free_buffer(vout->buf_virt_addr[i],
2006 vout->buf_virt_addr[i] = 0;
2007 vout->buf_phy_addr[i] = 0;
2013 /* Create video out devices */
2014 static int __init omap_vout_create_video_devices(struct platform_device *pdev)
2017 struct omap_vout_device *vout;
2018 struct video_device *vfd = NULL;
2019 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2020 struct omap2video_device *vid_dev = container_of(v4l2_dev,
2021 struct omap2video_device, v4l2_dev);
2023 for (k = 0; k < pdev->num_resources; k++) {
2025 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
2027 dev_err(&pdev->dev, ": could not allocate memory\n");
2032 vid_dev->vouts[k] = vout;
2033 vout->vid_dev = vid_dev;
2034 /* Select video2 if only 1 overlay is controlled by V4L2 */
2035 if (pdev->num_resources == 1)
2036 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2038 /* Else select video1 and video2 one by one. */
2039 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2040 vout->vid_info.num_overlays = 1;
2041 vout->vid_info.id = k + 1;
2043 /* Set VRFB as rotation_type for omap2 and omap3 */
2044 if (cpu_is_omap24xx() || cpu_is_omap34xx())
2045 vout->vid_info.rotation_type = VOUT_ROT_VRFB;
2047 /* Setup the default configuration for the video devices
2049 if (omap_vout_setup_video_data(vout) != 0) {
2054 /* Allocate default number of buffers for the video streaming
2055 * and reserve the VRFB space for rotation
2057 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2062 /* Register the Video device with V4L2
2065 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
2066 dev_err(&pdev->dev, ": Could not register "
2067 "Video for Linux device\n");
2072 video_set_drvdata(vfd, vout);
2074 /* Configure the overlay structure */
2075 ret = omapvid_init(vid_dev->vouts[k], 0);
2080 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2081 omap_vout_release_vrfb(vout);
2082 omap_vout_free_buffers(vout);
2084 video_device_release(vfd);
2090 dev_info(&pdev->dev, ": registered and initialized"
2091 " video device %d\n", vfd->minor);
2092 if (k == (pdev->num_resources - 1))
2098 /* Driver functions */
2099 static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2101 struct video_device *vfd;
2102 struct omapvideo_info *ovid;
2108 ovid = &vout->vid_info;
2110 if (!video_is_registered(vfd)) {
2112 * The device was never registered, so release the
2113 * video_device struct directly.
2115 video_device_release(vfd);
2118 * The unregister function will release the video_device
2119 * struct as well as unregistering it.
2121 video_unregister_device(vfd);
2124 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2125 omap_vout_release_vrfb(vout);
2126 /* Free the VRFB buffer if allocated
2129 if (vout->vrfb_static_allocation)
2130 omap_vout_free_vrfb_buffers(vout);
2132 omap_vout_free_buffers(vout);
2137 static int omap_vout_remove(struct platform_device *pdev)
2140 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2141 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2142 omap2video_device, v4l2_dev);
2144 v4l2_device_unregister(v4l2_dev);
2145 for (k = 0; k < pdev->num_resources; k++)
2146 omap_vout_cleanup_device(vid_dev->vouts[k]);
2148 for (k = 0; k < vid_dev->num_displays; k++) {
2149 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
2150 vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
2152 omap_dss_put_device(vid_dev->displays[k]);
2158 static int __init omap_vout_probe(struct platform_device *pdev)
2161 struct omap_overlay *ovl;
2162 struct omap_dss_device *dssdev = NULL;
2163 struct omap_dss_device *def_display;
2164 struct omap2video_device *vid_dev = NULL;
2166 if (pdev->num_resources == 0) {
2167 dev_err(&pdev->dev, "probed for an unknown device\n");
2171 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2172 if (vid_dev == NULL)
2175 vid_dev->num_displays = 0;
2176 for_each_dss_dev(dssdev) {
2177 omap_dss_get_device(dssdev);
2179 if (!dssdev->driver) {
2180 dev_warn(&pdev->dev, "no driver for display: %s\n",
2182 omap_dss_put_device(dssdev);
2186 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2189 if (vid_dev->num_displays == 0) {
2190 dev_err(&pdev->dev, "no displays\n");
2195 vid_dev->num_overlays = omap_dss_get_num_overlays();
2196 for (i = 0; i < vid_dev->num_overlays; i++)
2197 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2199 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2200 for (i = 0; i < vid_dev->num_managers; i++)
2201 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2203 /* Get the Video1 overlay and video2 overlay.
2204 * Setup the Display attached to that overlays
2206 for (i = 1; i < vid_dev->num_overlays; i++) {
2207 ovl = omap_dss_get_overlay(i);
2208 if (ovl->manager && ovl->manager->device) {
2209 def_display = ovl->manager->device;
2211 dev_warn(&pdev->dev, "cannot find display\n");
2215 struct omap_dss_driver *dssdrv = def_display->driver;
2217 ret = dssdrv->enable(def_display);
2219 /* Here we are not considering a error
2220 * as display may be enabled by frame
2223 dev_warn(&pdev->dev,
2224 "'%s' Display already enabled\n",
2230 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2231 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2236 ret = omap_vout_create_video_devices(pdev);
2240 for (i = 0; i < vid_dev->num_displays; i++) {
2241 struct omap_dss_device *display = vid_dev->displays[i];
2243 if (display->driver->update)
2244 display->driver->update(display, 0, 0,
2245 display->panel.timings.x_res,
2246 display->panel.timings.y_res);
2251 v4l2_device_unregister(&vid_dev->v4l2_dev);
2253 for (i = 1; i < vid_dev->num_overlays; i++) {
2255 ovl = omap_dss_get_overlay(i);
2256 if (ovl->manager && ovl->manager->device)
2257 def_display = ovl->manager->device;
2259 if (def_display && def_display->driver)
2260 def_display->driver->disable(def_display);
2267 static struct platform_driver omap_vout_driver = {
2271 .probe = omap_vout_probe,
2272 .remove = omap_vout_remove,
2275 static int __init omap_vout_init(void)
2277 if (platform_driver_register(&omap_vout_driver) != 0) {
2278 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2284 static void omap_vout_cleanup(void)
2286 platform_driver_unregister(&omap_vout_driver);
2289 late_initcall(omap_vout_init);
2290 module_exit(omap_vout_cleanup);