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>
42 #include <media/videobuf-dma-contig.h>
43 #include <media/v4l2-device.h>
44 #include <media/v4l2-ioctl.h>
47 #include <plat/vrfb.h>
48 #include <video/omapdss.h>
50 #include "omap_voutlib.h"
51 #include "omap_voutdef.h"
52 #include "omap_vout_vrfb.h"
54 MODULE_AUTHOR("Texas Instruments");
55 MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
56 MODULE_LICENSE("GPL");
58 /* Driver Configuration macros */
59 #define VOUT_NAME "omap_vout"
61 enum omap_vout_channels {
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 u32 vid1_static_vrfb_alloc;
73 static u32 vid2_static_vrfb_alloc;
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.");
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.");
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");
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");
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");
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");
101 module_param(debug, bool, S_IRUGO);
102 MODULE_PARM_DESC(debug, "Debug level (0-1)");
104 /* list of image formats supported by OMAP2 video pipelines */
105 static const struct v4l2_fmtdesc omap_formats[] = {
107 /* Note: V4L2 defines RGB565 as:
110 * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
112 * We interpret RGB565 as:
115 * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
117 .description = "RGB565, le",
118 .pixelformat = V4L2_PIX_FMT_RGB565,
121 /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
122 * this for RGB24 unpack mode, the last 8 bits are ignored
124 .description = "RGB32, le",
125 .pixelformat = V4L2_PIX_FMT_RGB32,
128 /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
129 * this for RGB24 packed mode
132 .description = "RGB24, le",
133 .pixelformat = V4L2_PIX_FMT_RGB24,
136 .description = "YUYV (YUV 4:2:2), packed",
137 .pixelformat = V4L2_PIX_FMT_YUYV,
140 .description = "UYVY, packed",
141 .pixelformat = V4L2_PIX_FMT_UYVY,
145 #define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
150 static int omap_vout_try_format(struct v4l2_pix_format *pix)
154 pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
155 (u32)VID_MAX_HEIGHT);
156 pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
158 for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
159 if (pix->pixelformat == omap_formats[ifmt].pixelformat)
163 if (ifmt == NUM_OUTPUT_FORMATS)
166 pix->pixelformat = omap_formats[ifmt].pixelformat;
167 pix->field = V4L2_FIELD_ANY;
170 switch (pix->pixelformat) {
171 case V4L2_PIX_FMT_YUYV:
172 case V4L2_PIX_FMT_UYVY:
174 pix->colorspace = V4L2_COLORSPACE_JPEG;
177 case V4L2_PIX_FMT_RGB565:
178 case V4L2_PIX_FMT_RGB565X:
179 pix->colorspace = V4L2_COLORSPACE_SRGB;
182 case V4L2_PIX_FMT_RGB24:
183 pix->colorspace = V4L2_COLORSPACE_SRGB;
186 case V4L2_PIX_FMT_RGB32:
187 case V4L2_PIX_FMT_BGR32:
188 pix->colorspace = V4L2_COLORSPACE_SRGB;
192 pix->bytesperline = pix->width * bpp;
193 pix->sizeimage = pix->bytesperline * pix->height;
199 * omap_vout_uservirt_to_phys: This inline function is used to convert user
200 * space virtual address to physical address.
202 static u32 omap_vout_uservirt_to_phys(u32 virtp)
204 unsigned long physp = 0;
205 struct vm_area_struct *vma;
206 struct mm_struct *mm = current->mm;
208 vma = find_vma(mm, virtp);
209 /* For kernel direct-mapped memory, take the easy way */
210 if (virtp >= PAGE_OFFSET) {
211 physp = virt_to_phys((void *) virtp);
212 } else if (vma && (vma->vm_flags & VM_IO) && vma->vm_pgoff) {
213 /* this will catch, kernel-allocated, mmaped-to-usermode
215 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
217 /* otherwise, use get_user_pages() for general userland pages */
218 int res, nr_pages = 1;
220 down_read(¤t->mm->mmap_sem);
222 res = get_user_pages(current, current->mm, virtp, nr_pages, 1,
224 up_read(¤t->mm->mmap_sem);
226 if (res == nr_pages) {
227 physp = __pa(page_address(&pages[0]) +
228 (virtp & ~PAGE_MASK));
230 printk(KERN_WARNING VOUT_NAME
231 "get_user_pages failed\n");
240 * Free the V4L2 buffers
242 void omap_vout_free_buffers(struct omap_vout_device *vout)
246 /* Allocate memory for the buffers */
247 numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
248 vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
250 for (i = 0; i < numbuffers; i++) {
251 omap_vout_free_buffer(vout->buf_virt_addr[i],
253 vout->buf_phy_addr[i] = 0;
254 vout->buf_virt_addr[i] = 0;
259 * Convert V4L2 rotation to DSS rotation
260 * V4L2 understand 0, 90, 180, 270.
261 * Convert to 0, 1, 2 and 3 respectively for DSS
263 static int v4l2_rot_to_dss_rot(int v4l2_rotation,
264 enum dss_rotation *rotation, bool mirror)
268 switch (v4l2_rotation) {
270 *rotation = dss_rotation_90_degree;
273 *rotation = dss_rotation_180_degree;
276 *rotation = dss_rotation_270_degree;
279 *rotation = dss_rotation_0_degree;
287 static int omap_vout_calculate_offset(struct omap_vout_device *vout)
289 struct omapvideo_info *ovid;
290 struct v4l2_rect *crop = &vout->crop;
291 struct v4l2_pix_format *pix = &vout->pix;
292 int *cropped_offset = &vout->cropped_offset;
293 int ps = 2, line_length = 0;
295 ovid = &vout->vid_info;
297 if (ovid->rotation_type == VOUT_ROT_VRFB) {
298 omap_vout_calculate_vrfb_offset(vout);
300 vout->line_length = line_length = pix->width;
302 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
303 V4L2_PIX_FMT_UYVY == pix->pixelformat)
305 else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
307 else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
312 *cropped_offset = (line_length * ps) *
313 crop->top + crop->left * ps;
316 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
317 __func__, vout->cropped_offset);
323 * Convert V4L2 pixel format to DSS pixel format
325 static int video_mode_to_dss_mode(struct omap_vout_device *vout)
327 struct omap_overlay *ovl;
328 struct omapvideo_info *ovid;
329 struct v4l2_pix_format *pix = &vout->pix;
330 enum omap_color_mode mode;
332 ovid = &vout->vid_info;
333 ovl = ovid->overlays[0];
335 switch (pix->pixelformat) {
338 case V4L2_PIX_FMT_YUYV:
339 mode = OMAP_DSS_COLOR_YUV2;
341 case V4L2_PIX_FMT_UYVY:
342 mode = OMAP_DSS_COLOR_UYVY;
344 case V4L2_PIX_FMT_RGB565:
345 mode = OMAP_DSS_COLOR_RGB16;
347 case V4L2_PIX_FMT_RGB24:
348 mode = OMAP_DSS_COLOR_RGB24P;
350 case V4L2_PIX_FMT_RGB32:
351 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
352 OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
354 case V4L2_PIX_FMT_BGR32:
355 mode = OMAP_DSS_COLOR_RGBX32;
366 static int omapvid_setup_overlay(struct omap_vout_device *vout,
367 struct omap_overlay *ovl, int posx, int posy, int outw,
371 struct omap_overlay_info info;
372 int cropheight, cropwidth, pixheight, pixwidth;
374 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
375 (outw != vout->pix.width || outh != vout->pix.height)) {
380 vout->dss_mode = video_mode_to_dss_mode(vout);
381 if (vout->dss_mode == -EINVAL) {
386 /* Setup the input plane parameters according to
387 * rotation value selected.
389 if (is_rotation_90_or_270(vout)) {
390 cropheight = vout->crop.width;
391 cropwidth = vout->crop.height;
392 pixheight = vout->pix.width;
393 pixwidth = vout->pix.height;
395 cropheight = vout->crop.height;
396 cropwidth = vout->crop.width;
397 pixheight = vout->pix.height;
398 pixwidth = vout->pix.width;
401 ovl->get_overlay_info(ovl, &info);
403 info.width = cropwidth;
404 info.height = cropheight;
405 info.color_mode = vout->dss_mode;
406 info.mirror = vout->mirror;
409 info.out_width = outw;
410 info.out_height = outh;
411 info.global_alpha = vout->win.global_alpha;
412 if (!is_rotation_enabled(vout)) {
414 info.rotation_type = OMAP_DSS_ROT_DMA;
415 info.screen_width = pixwidth;
417 info.rotation = vout->rotation;
418 info.rotation_type = OMAP_DSS_ROT_VRFB;
419 info.screen_width = 2048;
422 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
423 "%s enable=%d addr=%x width=%d\n height=%d color_mode=%d\n"
424 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
425 "out_height=%d rotation_type=%d screen_width=%d\n",
426 __func__, info.enabled, info.paddr, info.width, info.height,
427 info.color_mode, info.rotation, info.mirror, info.pos_x,
428 info.pos_y, info.out_width, info.out_height, info.rotation_type,
431 ret = ovl->set_overlay_info(ovl, &info);
438 v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
443 * Initialize the overlay structure
445 static int omapvid_init(struct omap_vout_device *vout, u32 addr)
448 struct v4l2_window *win;
449 struct omap_overlay *ovl;
450 int posx, posy, outw, outh, temp;
451 struct omap_video_timings *timing;
452 struct omapvideo_info *ovid = &vout->vid_info;
455 for (i = 0; i < ovid->num_overlays; i++) {
456 ovl = ovid->overlays[i];
457 if (!ovl->manager || !ovl->manager->device)
460 timing = &ovl->manager->device->panel.timings;
463 outh = win->w.height;
464 switch (vout->rotation) {
465 case dss_rotation_90_degree:
466 /* Invert the height and width for 90
467 * and 270 degree rotation
472 posy = (timing->y_res - win->w.width) - win->w.left;
476 case dss_rotation_180_degree:
477 posx = (timing->x_res - win->w.width) - win->w.left;
478 posy = (timing->y_res - win->w.height) - win->w.top;
481 case dss_rotation_270_degree:
486 posx = (timing->x_res - win->w.height) - win->w.top;
495 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
498 goto omapvid_init_err;
503 v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
508 * Apply the changes set the go bit of DSS
510 static int omapvid_apply_changes(struct omap_vout_device *vout)
513 struct omap_overlay *ovl;
514 struct omapvideo_info *ovid = &vout->vid_info;
516 for (i = 0; i < ovid->num_overlays; i++) {
517 ovl = ovid->overlays[i];
518 if (!ovl->manager || !ovl->manager->device)
520 ovl->manager->apply(ovl->manager);
526 static void omap_vout_isr(void *arg, unsigned int irqstatus)
530 struct omap_overlay *ovl;
531 struct timeval timevalue;
532 struct omapvideo_info *ovid;
533 struct omap_dss_device *cur_display;
534 struct omap_vout_device *vout = (struct omap_vout_device *)arg;
536 if (!vout->streaming)
539 ovid = &vout->vid_info;
540 ovl = ovid->overlays[0];
541 /* get the display device attached to the overlay */
542 if (!ovl->manager || !ovl->manager->device)
545 cur_display = ovl->manager->device;
547 spin_lock(&vout->vbq_lock);
548 do_gettimeofday(&timevalue);
550 if (cur_display->type != OMAP_DISPLAY_TYPE_VENC) {
551 switch (cur_display->type) {
552 case OMAP_DISPLAY_TYPE_DPI:
553 if (!(irqstatus & (DISPC_IRQ_VSYNC | DISPC_IRQ_VSYNC2)))
556 case OMAP_DISPLAY_TYPE_HDMI:
557 if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
563 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
564 vout->cur_frm->ts = timevalue;
565 vout->cur_frm->state = VIDEOBUF_DONE;
566 wake_up_interruptible(&vout->cur_frm->done);
567 vout->cur_frm = vout->next_frm;
570 if (list_empty(&vout->dma_queue))
573 vout->next_frm = list_entry(vout->dma_queue.next,
574 struct videobuf_buffer, queue);
575 list_del(&vout->next_frm->queue);
577 vout->next_frm->state = VIDEOBUF_ACTIVE;
579 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
580 + vout->cropped_offset;
582 /* First save the configuration in ovelray structure */
583 ret = omapvid_init(vout, addr);
585 printk(KERN_ERR VOUT_NAME
586 "failed to set overlay info\n");
587 /* Enable the pipeline and set the Go bit */
588 ret = omapvid_apply_changes(vout);
590 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
593 if (vout->first_int) {
597 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
599 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
605 if (fid != vout->field_id) {
607 vout->field_id = fid;
612 if (vout->cur_frm == vout->next_frm)
615 vout->cur_frm->ts = timevalue;
616 vout->cur_frm->state = VIDEOBUF_DONE;
617 wake_up_interruptible(&vout->cur_frm->done);
618 vout->cur_frm = vout->next_frm;
619 } else if (1 == fid) {
620 if (list_empty(&vout->dma_queue) ||
621 (vout->cur_frm != vout->next_frm))
624 vout->next_frm = list_entry(vout->dma_queue.next,
625 struct videobuf_buffer, queue);
626 list_del(&vout->next_frm->queue);
628 vout->next_frm->state = VIDEOBUF_ACTIVE;
629 addr = (unsigned long)
630 vout->queued_buf_addr[vout->next_frm->i] +
631 vout->cropped_offset;
632 /* First save the configuration in ovelray structure */
633 ret = omapvid_init(vout, addr);
635 printk(KERN_ERR VOUT_NAME
636 "failed to set overlay info\n");
637 /* Enable the pipeline and set the Go bit */
638 ret = omapvid_apply_changes(vout);
640 printk(KERN_ERR VOUT_NAME
641 "failed to change mode\n");
647 spin_unlock(&vout->vbq_lock);
651 /* Video buffer call backs */
654 * Buffer setup function is called by videobuf layer when REQBUF ioctl is
655 * called. This is used to setup buffers and return size and count of
656 * buffers allocated. After the call to this buffer, videobuf layer will
657 * setup buffer queue depending on the size and count of buffers
659 static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
662 int startindex = 0, i, j;
663 u32 phy_addr = 0, virt_addr = 0;
664 struct omap_vout_device *vout = q->priv_data;
665 struct omapvideo_info *ovid = &vout->vid_info;
670 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
673 startindex = (vout->vid == OMAP_VIDEO1) ?
674 video1_numbuffers : video2_numbuffers;
675 if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
678 if (ovid->rotation_type == VOUT_ROT_VRFB) {
679 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
683 if (V4L2_MEMORY_MMAP != vout->memory)
686 /* Now allocated the V4L2 buffers */
687 *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
688 startindex = (vout->vid == OMAP_VIDEO1) ?
689 video1_numbuffers : video2_numbuffers;
691 /* Check the size of the buffer */
692 if (*size > vout->buffer_size) {
693 v4l2_err(&vout->vid_dev->v4l2_dev,
694 "buffer allocation mismatch [%u] [%u]\n",
695 *size, vout->buffer_size);
699 for (i = startindex; i < *count; i++) {
700 vout->buffer_size = *size;
702 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
705 if (ovid->rotation_type == VOUT_ROT_NONE) {
708 if (!is_rotation_enabled(vout))
710 /* Free the VRFB buffers if no space for V4L2 buffers */
711 for (j = i; j < *count; j++) {
712 omap_vout_free_buffer(
713 vout->smsshado_virt_addr[j],
714 vout->smsshado_size);
715 vout->smsshado_virt_addr[j] = 0;
716 vout->smsshado_phy_addr[j] = 0;
720 vout->buf_virt_addr[i] = virt_addr;
721 vout->buf_phy_addr[i] = phy_addr;
723 *count = vout->buffer_allocated = i;
729 * Free the V4L2 buffers additionally allocated than default
732 static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
734 int num_buffers = 0, i;
736 num_buffers = (vout->vid == OMAP_VIDEO1) ?
737 video1_numbuffers : video2_numbuffers;
739 for (i = num_buffers; i < vout->buffer_allocated; i++) {
740 if (vout->buf_virt_addr[i])
741 omap_vout_free_buffer(vout->buf_virt_addr[i],
744 vout->buf_virt_addr[i] = 0;
745 vout->buf_phy_addr[i] = 0;
747 vout->buffer_allocated = num_buffers;
751 * This function will be called when VIDIOC_QBUF ioctl is called.
752 * It prepare buffers before give out for the display. This function
753 * converts user space virtual address into physical address if userptr memory
754 * exchange mechanism is used. If rotation is enabled, it copies entire
755 * buffer into VRFB memory space before giving it to the DSS.
757 static int omap_vout_buffer_prepare(struct videobuf_queue *q,
758 struct videobuf_buffer *vb,
759 enum v4l2_field field)
761 struct omap_vout_device *vout = q->priv_data;
762 struct omapvideo_info *ovid = &vout->vid_info;
764 if (VIDEOBUF_NEEDS_INIT == vb->state) {
765 vb->width = vout->pix.width;
766 vb->height = vout->pix.height;
767 vb->size = vb->width * vb->height * vout->bpp;
770 vb->state = VIDEOBUF_PREPARED;
771 /* if user pointer memory mechanism is used, get the physical
772 * address of the buffer
774 if (V4L2_MEMORY_USERPTR == vb->memory) {
777 /* Physical address */
778 vout->queued_buf_addr[vb->i] = (u8 *)
779 omap_vout_uservirt_to_phys(vb->baddr);
784 addr = (unsigned long) vout->buf_virt_addr[vb->i];
785 size = (unsigned long) vb->size;
787 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
788 size, DMA_TO_DEVICE);
789 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
790 v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
792 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
795 if (ovid->rotation_type == VOUT_ROT_VRFB)
796 return omap_vout_prepare_vrfb(vout, vb);
802 * Buffer queue function will be called from the videobuf layer when _QBUF
803 * ioctl is called. It is used to enqueue buffer, which is ready to be
806 static void omap_vout_buffer_queue(struct videobuf_queue *q,
807 struct videobuf_buffer *vb)
809 struct omap_vout_device *vout = q->priv_data;
811 /* Driver is also maintainig a queue. So enqueue buffer in the driver
813 list_add_tail(&vb->queue, &vout->dma_queue);
815 vb->state = VIDEOBUF_QUEUED;
819 * Buffer release function is called from videobuf layer to release buffer
820 * which are already allocated
822 static void omap_vout_buffer_release(struct videobuf_queue *q,
823 struct videobuf_buffer *vb)
825 struct omap_vout_device *vout = q->priv_data;
827 vb->state = VIDEOBUF_NEEDS_INIT;
829 if (V4L2_MEMORY_MMAP != vout->memory)
836 static void omap_vout_vm_open(struct vm_area_struct *vma)
838 struct omap_vout_device *vout = vma->vm_private_data;
840 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
841 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
845 static void omap_vout_vm_close(struct vm_area_struct *vma)
847 struct omap_vout_device *vout = vma->vm_private_data;
849 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
850 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
854 static struct vm_operations_struct omap_vout_vm_ops = {
855 .open = omap_vout_vm_open,
856 .close = omap_vout_vm_close,
859 static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
863 unsigned long start = vma->vm_start;
864 unsigned long size = (vma->vm_end - vma->vm_start);
865 struct omap_vout_device *vout = file->private_data;
866 struct videobuf_queue *q = &vout->vbq;
868 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
869 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
870 vma->vm_pgoff, vma->vm_start, vma->vm_end);
872 /* look for the buffer to map */
873 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
874 if (NULL == q->bufs[i])
876 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
878 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
882 if (VIDEO_MAX_FRAME == i) {
883 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
884 "offset invalid [offset=0x%lx]\n",
885 (vma->vm_pgoff << PAGE_SHIFT));
888 /* Check the size of the buffer */
889 if (size > vout->buffer_size) {
890 v4l2_err(&vout->vid_dev->v4l2_dev,
891 "insufficient memory [%lu] [%u]\n",
892 size, vout->buffer_size);
896 q->bufs[i]->baddr = vma->vm_start;
898 vma->vm_flags |= VM_RESERVED;
899 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
900 vma->vm_ops = &omap_vout_vm_ops;
901 vma->vm_private_data = (void *) vout;
902 pos = (void *)vout->buf_virt_addr[i];
903 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
906 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
907 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
914 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
919 static int omap_vout_release(struct file *file)
922 struct videobuf_queue *q;
923 struct omapvideo_info *ovid;
924 struct omap_vout_device *vout = file->private_data;
926 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
927 ovid = &vout->vid_info;
933 /* Disable all the overlay managers connected with this interface */
934 for (i = 0; i < ovid->num_overlays; i++) {
935 struct omap_overlay *ovl = ovid->overlays[i];
936 if (ovl->manager && ovl->manager->device) {
937 struct omap_overlay_info info;
938 ovl->get_overlay_info(ovl, &info);
940 ovl->set_overlay_info(ovl, &info);
943 /* Turn off the pipeline */
944 ret = omapvid_apply_changes(vout);
946 v4l2_warn(&vout->vid_dev->v4l2_dev,
947 "Unable to apply changes\n");
949 /* Free all buffers */
950 omap_vout_free_extra_buffers(vout);
952 /* Free the VRFB buffers only if they are allocated
953 * during reqbufs. Don't free if init time allocated
955 if (ovid->rotation_type == VOUT_ROT_VRFB) {
956 if (!vout->vrfb_static_allocation)
957 omap_vout_free_vrfb_buffers(vout);
959 videobuf_mmap_free(q);
961 /* Even if apply changes fails we should continue
962 freeing allocated memory */
963 if (vout->streaming) {
966 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
967 DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
968 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
971 videobuf_streamoff(q);
972 videobuf_queue_cancel(q);
975 if (vout->mmap_count != 0)
976 vout->mmap_count = 0;
979 file->private_data = NULL;
981 if (vout->buffer_allocated)
982 videobuf_mmap_free(q);
984 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
988 static int omap_vout_open(struct file *file)
990 struct videobuf_queue *q;
991 struct omap_vout_device *vout = NULL;
993 vout = video_drvdata(file);
994 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
999 /* for now, we only support single open */
1005 file->private_data = vout;
1006 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1009 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1010 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1011 video_vbq_ops.buf_release = omap_vout_buffer_release;
1012 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1013 spin_lock_init(&vout->vbq_lock);
1015 videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1016 &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
1017 sizeof(struct videobuf_buffer), vout, NULL);
1019 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1026 static int vidioc_querycap(struct file *file, void *fh,
1027 struct v4l2_capability *cap)
1029 struct omap_vout_device *vout = fh;
1031 strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1032 strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1033 cap->bus_info[0] = '\0';
1034 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
1039 static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1040 struct v4l2_fmtdesc *fmt)
1042 int index = fmt->index;
1044 if (index >= NUM_OUTPUT_FORMATS)
1047 fmt->flags = omap_formats[index].flags;
1048 strlcpy(fmt->description, omap_formats[index].description,
1049 sizeof(fmt->description));
1050 fmt->pixelformat = omap_formats[index].pixelformat;
1055 static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1056 struct v4l2_format *f)
1058 struct omap_vout_device *vout = fh;
1060 f->fmt.pix = vout->pix;
1065 static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1066 struct v4l2_format *f)
1068 struct omap_overlay *ovl;
1069 struct omapvideo_info *ovid;
1070 struct omap_video_timings *timing;
1071 struct omap_vout_device *vout = fh;
1073 ovid = &vout->vid_info;
1074 ovl = ovid->overlays[0];
1076 if (!ovl->manager || !ovl->manager->device)
1078 /* get the display device attached to the overlay */
1079 timing = &ovl->manager->device->panel.timings;
1081 vout->fbuf.fmt.height = timing->y_res;
1082 vout->fbuf.fmt.width = timing->x_res;
1084 omap_vout_try_format(&f->fmt.pix);
1088 static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1089 struct v4l2_format *f)
1092 struct omap_overlay *ovl;
1093 struct omapvideo_info *ovid;
1094 struct omap_video_timings *timing;
1095 struct omap_vout_device *vout = fh;
1097 if (vout->streaming)
1100 mutex_lock(&vout->lock);
1102 ovid = &vout->vid_info;
1103 ovl = ovid->overlays[0];
1105 /* get the display device attached to the overlay */
1106 if (!ovl->manager || !ovl->manager->device) {
1108 goto s_fmt_vid_out_exit;
1110 timing = &ovl->manager->device->panel.timings;
1112 /* We dont support RGB24-packed mode if vrfb rotation
1114 if ((is_rotation_enabled(vout)) &&
1115 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1117 goto s_fmt_vid_out_exit;
1120 /* get the framebuffer parameters */
1122 if (is_rotation_90_or_270(vout)) {
1123 vout->fbuf.fmt.height = timing->x_res;
1124 vout->fbuf.fmt.width = timing->y_res;
1126 vout->fbuf.fmt.height = timing->y_res;
1127 vout->fbuf.fmt.width = timing->x_res;
1130 /* change to samller size is OK */
1132 bpp = omap_vout_try_format(&f->fmt.pix);
1133 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1135 /* try & set the new output format */
1137 vout->pix = f->fmt.pix;
1140 /* If YUYV then vrfb bpp is 2, for others its 1 */
1141 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1142 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1145 /* set default crop and win */
1146 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1148 /* Save the changes in the overlay strcuture */
1149 ret = omapvid_init(vout, 0);
1151 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1152 goto s_fmt_vid_out_exit;
1158 mutex_unlock(&vout->lock);
1162 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1163 struct v4l2_format *f)
1166 struct omap_vout_device *vout = fh;
1167 struct omap_overlay *ovl;
1168 struct omapvideo_info *ovid;
1169 struct v4l2_window *win = &f->fmt.win;
1171 ovid = &vout->vid_info;
1172 ovl = ovid->overlays[0];
1174 ret = omap_vout_try_window(&vout->fbuf, win);
1177 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1178 win->global_alpha = 255;
1180 win->global_alpha = f->fmt.win.global_alpha;
1186 static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1187 struct v4l2_format *f)
1190 struct omap_overlay *ovl;
1191 struct omapvideo_info *ovid;
1192 struct omap_vout_device *vout = fh;
1193 struct v4l2_window *win = &f->fmt.win;
1195 mutex_lock(&vout->lock);
1196 ovid = &vout->vid_info;
1197 ovl = ovid->overlays[0];
1199 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1201 /* Video1 plane does not support global alpha on OMAP3 */
1202 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1203 vout->win.global_alpha = 255;
1205 vout->win.global_alpha = f->fmt.win.global_alpha;
1207 vout->win.chromakey = f->fmt.win.chromakey;
1209 mutex_unlock(&vout->lock);
1213 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
1214 struct v4l2_fmtdesc *fmt)
1216 int index = fmt->index;
1218 if (index >= NUM_OUTPUT_FORMATS)
1221 fmt->flags = omap_formats[index].flags;
1222 strlcpy(fmt->description, omap_formats[index].description,
1223 sizeof(fmt->description));
1224 fmt->pixelformat = omap_formats[index].pixelformat;
1228 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1229 struct v4l2_format *f)
1232 struct omap_overlay *ovl;
1233 struct omapvideo_info *ovid;
1234 struct omap_vout_device *vout = fh;
1235 struct omap_overlay_manager_info info;
1236 struct v4l2_window *win = &f->fmt.win;
1238 ovid = &vout->vid_info;
1239 ovl = ovid->overlays[0];
1241 win->w = vout->win.w;
1242 win->field = vout->win.field;
1243 win->global_alpha = vout->win.global_alpha;
1245 if (ovl->manager && ovl->manager->get_manager_info) {
1246 ovl->manager->get_manager_info(ovl->manager, &info);
1247 key_value = info.trans_key;
1249 win->chromakey = key_value;
1253 static int vidioc_cropcap(struct file *file, void *fh,
1254 struct v4l2_cropcap *cropcap)
1256 struct omap_vout_device *vout = fh;
1257 struct v4l2_pix_format *pix = &vout->pix;
1259 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1262 /* Width and height are always even */
1263 cropcap->bounds.width = pix->width & ~1;
1264 cropcap->bounds.height = pix->height & ~1;
1266 omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1267 cropcap->pixelaspect.numerator = 1;
1268 cropcap->pixelaspect.denominator = 1;
1272 static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1274 struct omap_vout_device *vout = fh;
1276 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1278 crop->c = vout->crop;
1282 static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1285 struct omap_vout_device *vout = fh;
1286 struct omapvideo_info *ovid;
1287 struct omap_overlay *ovl;
1288 struct omap_video_timings *timing;
1290 if (vout->streaming)
1293 mutex_lock(&vout->lock);
1294 ovid = &vout->vid_info;
1295 ovl = ovid->overlays[0];
1297 if (!ovl->manager || !ovl->manager->device) {
1301 /* get the display device attached to the overlay */
1302 timing = &ovl->manager->device->panel.timings;
1304 if (is_rotation_90_or_270(vout)) {
1305 vout->fbuf.fmt.height = timing->x_res;
1306 vout->fbuf.fmt.width = timing->y_res;
1308 vout->fbuf.fmt.height = timing->y_res;
1309 vout->fbuf.fmt.width = timing->x_res;
1312 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1313 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1314 &vout->fbuf, &crop->c);
1317 mutex_unlock(&vout->lock);
1321 static int vidioc_queryctrl(struct file *file, void *fh,
1322 struct v4l2_queryctrl *ctrl)
1327 case V4L2_CID_ROTATE:
1328 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1330 case V4L2_CID_BG_COLOR:
1331 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1333 case V4L2_CID_VFLIP:
1334 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1337 ctrl->name[0] = '\0';
1343 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1346 struct omap_vout_device *vout = fh;
1349 case V4L2_CID_ROTATE:
1350 ctrl->value = vout->control[0].value;
1352 case V4L2_CID_BG_COLOR:
1354 struct omap_overlay_manager_info info;
1355 struct omap_overlay *ovl;
1357 ovl = vout->vid_info.overlays[0];
1358 if (!ovl->manager || !ovl->manager->get_manager_info) {
1363 ovl->manager->get_manager_info(ovl->manager, &info);
1364 ctrl->value = info.default_color;
1367 case V4L2_CID_VFLIP:
1368 ctrl->value = vout->control[2].value;
1376 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1379 struct omap_vout_device *vout = fh;
1382 case V4L2_CID_ROTATE:
1384 struct omapvideo_info *ovid;
1385 int rotation = a->value;
1387 ovid = &vout->vid_info;
1389 mutex_lock(&vout->lock);
1390 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1391 mutex_unlock(&vout->lock);
1396 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1397 mutex_unlock(&vout->lock);
1402 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1404 mutex_unlock(&vout->lock);
1409 vout->control[0].value = rotation;
1410 mutex_unlock(&vout->lock);
1413 case V4L2_CID_BG_COLOR:
1415 struct omap_overlay *ovl;
1416 unsigned int color = a->value;
1417 struct omap_overlay_manager_info info;
1419 ovl = vout->vid_info.overlays[0];
1421 mutex_lock(&vout->lock);
1422 if (!ovl->manager || !ovl->manager->get_manager_info) {
1423 mutex_unlock(&vout->lock);
1428 ovl->manager->get_manager_info(ovl->manager, &info);
1429 info.default_color = color;
1430 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1431 mutex_unlock(&vout->lock);
1436 vout->control[1].value = color;
1437 mutex_unlock(&vout->lock);
1440 case V4L2_CID_VFLIP:
1442 struct omap_overlay *ovl;
1443 struct omapvideo_info *ovid;
1444 unsigned int mirror = a->value;
1446 ovid = &vout->vid_info;
1447 ovl = ovid->overlays[0];
1449 mutex_lock(&vout->lock);
1450 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1451 mutex_unlock(&vout->lock);
1456 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1457 mutex_unlock(&vout->lock);
1461 vout->mirror = mirror;
1462 vout->control[2].value = mirror;
1463 mutex_unlock(&vout->lock);
1472 static int vidioc_reqbufs(struct file *file, void *fh,
1473 struct v4l2_requestbuffers *req)
1476 unsigned int i, num_buffers = 0;
1477 struct omap_vout_device *vout = fh;
1478 struct videobuf_queue *q = &vout->vbq;
1480 if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
1482 /* if memory is not mmp or userptr
1484 if ((V4L2_MEMORY_MMAP != req->memory) &&
1485 (V4L2_MEMORY_USERPTR != req->memory))
1488 mutex_lock(&vout->lock);
1489 /* Cannot be requested when streaming is on */
1490 if (vout->streaming) {
1495 /* If buffers are already allocated free them */
1496 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1497 if (vout->mmap_count) {
1501 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1502 video1_numbuffers : video2_numbuffers;
1503 for (i = num_buffers; i < vout->buffer_allocated; i++) {
1504 omap_vout_free_buffer(vout->buf_virt_addr[i],
1506 vout->buf_virt_addr[i] = 0;
1507 vout->buf_phy_addr[i] = 0;
1509 vout->buffer_allocated = num_buffers;
1510 videobuf_mmap_free(q);
1511 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1512 if (vout->buffer_allocated) {
1513 videobuf_mmap_free(q);
1514 for (i = 0; i < vout->buffer_allocated; i++) {
1518 vout->buffer_allocated = 0;
1522 /*store the memory type in data structure */
1523 vout->memory = req->memory;
1525 INIT_LIST_HEAD(&vout->dma_queue);
1527 /* call videobuf_reqbufs api */
1528 ret = videobuf_reqbufs(q, req);
1532 vout->buffer_allocated = req->count;
1535 mutex_unlock(&vout->lock);
1539 static int vidioc_querybuf(struct file *file, void *fh,
1540 struct v4l2_buffer *b)
1542 struct omap_vout_device *vout = fh;
1544 return videobuf_querybuf(&vout->vbq, b);
1547 static int vidioc_qbuf(struct file *file, void *fh,
1548 struct v4l2_buffer *buffer)
1550 struct omap_vout_device *vout = fh;
1551 struct videobuf_queue *q = &vout->vbq;
1553 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1554 (buffer->index >= vout->buffer_allocated) ||
1555 (q->bufs[buffer->index]->memory != buffer->memory)) {
1558 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1559 if ((buffer->length < vout->pix.sizeimage) ||
1560 (0 == buffer->m.userptr)) {
1565 if ((is_rotation_enabled(vout)) &&
1566 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1567 v4l2_warn(&vout->vid_dev->v4l2_dev,
1568 "DMA Channel not allocated for Rotation\n");
1572 return videobuf_qbuf(q, buffer);
1575 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1577 struct omap_vout_device *vout = fh;
1578 struct videobuf_queue *q = &vout->vbq;
1583 struct videobuf_buffer *vb;
1585 vb = q->bufs[b->index];
1587 if (!vout->streaming)
1590 if (file->f_flags & O_NONBLOCK)
1591 /* Call videobuf_dqbuf for non blocking mode */
1592 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
1594 /* Call videobuf_dqbuf for blocking mode */
1595 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
1597 addr = (unsigned long) vout->buf_phy_addr[vb->i];
1598 size = (unsigned long) vb->size;
1599 dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
1600 size, DMA_TO_DEVICE);
1604 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1607 u32 addr = 0, mask = 0;
1608 struct omap_vout_device *vout = fh;
1609 struct videobuf_queue *q = &vout->vbq;
1610 struct omapvideo_info *ovid = &vout->vid_info;
1612 mutex_lock(&vout->lock);
1614 if (vout->streaming) {
1619 ret = videobuf_streamon(q);
1623 if (list_empty(&vout->dma_queue)) {
1628 /* Get the next frame from the buffer queue */
1629 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1630 struct videobuf_buffer, queue);
1631 /* Remove buffer from the buffer queue */
1632 list_del(&vout->cur_frm->queue);
1633 /* Mark state of the current frame to active */
1634 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1635 /* Initialize field_id and started member */
1638 /* set flag here. Next QBUF will start DMA */
1639 vout->streaming = 1;
1641 vout->first_int = 1;
1643 if (omap_vout_calculate_offset(vout)) {
1647 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1648 + vout->cropped_offset;
1650 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1653 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1655 for (j = 0; j < ovid->num_overlays; j++) {
1656 struct omap_overlay *ovl = ovid->overlays[j];
1658 if (ovl->manager && ovl->manager->device) {
1659 struct omap_overlay_info info;
1660 ovl->get_overlay_info(ovl, &info);
1663 if (ovl->set_overlay_info(ovl, &info)) {
1670 /* First save the configuration in ovelray structure */
1671 ret = omapvid_init(vout, addr);
1673 v4l2_err(&vout->vid_dev->v4l2_dev,
1674 "failed to set overlay info\n");
1675 /* Enable the pipeline and set the Go bit */
1676 ret = omapvid_apply_changes(vout);
1678 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1684 ret = videobuf_streamoff(q);
1686 mutex_unlock(&vout->lock);
1690 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1694 struct omap_vout_device *vout = fh;
1695 struct omapvideo_info *ovid = &vout->vid_info;
1697 if (!vout->streaming)
1700 vout->streaming = 0;
1701 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1704 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1706 for (j = 0; j < ovid->num_overlays; j++) {
1707 struct omap_overlay *ovl = ovid->overlays[j];
1709 if (ovl->manager && ovl->manager->device) {
1710 struct omap_overlay_info info;
1712 ovl->get_overlay_info(ovl, &info);
1714 ret = ovl->set_overlay_info(ovl, &info);
1716 v4l2_err(&vout->vid_dev->v4l2_dev,
1717 "failed to update overlay info in streamoff\n");
1721 /* Turn of the pipeline */
1722 ret = omapvid_apply_changes(vout);
1724 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
1727 INIT_LIST_HEAD(&vout->dma_queue);
1728 ret = videobuf_streamoff(&vout->vbq);
1733 static int vidioc_s_fbuf(struct file *file, void *fh,
1734 struct v4l2_framebuffer *a)
1737 struct omap_overlay *ovl;
1738 struct omapvideo_info *ovid;
1739 struct omap_vout_device *vout = fh;
1740 struct omap_overlay_manager_info info;
1741 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1743 ovid = &vout->vid_info;
1744 ovl = ovid->overlays[0];
1746 /* OMAP DSS doesn't support Source and Destination color
1748 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1749 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1751 /* OMAP DSS Doesn't support the Destination color key
1752 and alpha blending together */
1753 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1754 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1757 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1758 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1759 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
1761 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1763 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1764 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1765 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1767 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
1769 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1770 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1774 if (ovl->manager && ovl->manager->get_manager_info &&
1775 ovl->manager->set_manager_info) {
1777 ovl->manager->get_manager_info(ovl->manager, &info);
1778 info.trans_enabled = enable;
1779 info.trans_key_type = key_type;
1780 info.trans_key = vout->win.chromakey;
1782 if (ovl->manager->set_manager_info(ovl->manager, &info))
1785 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1786 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1789 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1792 if (ovl->manager && ovl->manager->get_manager_info &&
1793 ovl->manager->set_manager_info) {
1794 ovl->manager->get_manager_info(ovl->manager, &info);
1795 /* enable this only if there is no zorder cap */
1796 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1797 info.partial_alpha_enabled = enable;
1798 if (ovl->manager->set_manager_info(ovl->manager, &info))
1805 static int vidioc_g_fbuf(struct file *file, void *fh,
1806 struct v4l2_framebuffer *a)
1808 struct omap_overlay *ovl;
1809 struct omapvideo_info *ovid;
1810 struct omap_vout_device *vout = fh;
1811 struct omap_overlay_manager_info info;
1813 ovid = &vout->vid_info;
1814 ovl = ovid->overlays[0];
1817 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1818 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1820 if (ovl->manager && ovl->manager->get_manager_info) {
1821 ovl->manager->get_manager_info(ovl->manager, &info);
1822 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1823 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1824 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1825 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1827 if (ovl->manager && ovl->manager->get_manager_info) {
1828 ovl->manager->get_manager_info(ovl->manager, &info);
1829 if (info.partial_alpha_enabled)
1830 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1836 static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1837 .vidioc_querycap = vidioc_querycap,
1838 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1839 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1840 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1841 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1842 .vidioc_queryctrl = vidioc_queryctrl,
1843 .vidioc_g_ctrl = vidioc_g_ctrl,
1844 .vidioc_s_fbuf = vidioc_s_fbuf,
1845 .vidioc_g_fbuf = vidioc_g_fbuf,
1846 .vidioc_s_ctrl = vidioc_s_ctrl,
1847 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1848 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1849 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1850 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1851 .vidioc_cropcap = vidioc_cropcap,
1852 .vidioc_g_crop = vidioc_g_crop,
1853 .vidioc_s_crop = vidioc_s_crop,
1854 .vidioc_reqbufs = vidioc_reqbufs,
1855 .vidioc_querybuf = vidioc_querybuf,
1856 .vidioc_qbuf = vidioc_qbuf,
1857 .vidioc_dqbuf = vidioc_dqbuf,
1858 .vidioc_streamon = vidioc_streamon,
1859 .vidioc_streamoff = vidioc_streamoff,
1862 static const struct v4l2_file_operations omap_vout_fops = {
1863 .owner = THIS_MODULE,
1864 .unlocked_ioctl = video_ioctl2,
1865 .mmap = omap_vout_mmap,
1866 .open = omap_vout_open,
1867 .release = omap_vout_release,
1870 /* Init functions used during driver initialization */
1871 /* Initial setup of video_data */
1872 static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1874 struct video_device *vfd;
1875 struct v4l2_pix_format *pix;
1876 struct v4l2_control *control;
1877 struct omap_dss_device *display =
1878 vout->vid_info.overlays[0]->manager->device;
1880 /* set the default pix */
1883 /* Set the default picture of QVGA */
1884 pix->width = QQVGA_WIDTH;
1885 pix->height = QQVGA_HEIGHT;
1887 /* Default pixel format is RGB 5-6-5 */
1888 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1889 pix->field = V4L2_FIELD_ANY;
1890 pix->bytesperline = pix->width * 2;
1891 pix->sizeimage = pix->bytesperline * pix->height;
1893 pix->colorspace = V4L2_COLORSPACE_JPEG;
1895 vout->bpp = RGB565_BPP;
1896 vout->fbuf.fmt.width = display->panel.timings.x_res;
1897 vout->fbuf.fmt.height = display->panel.timings.y_res;
1899 /* Set the data structures for the overlay parameters*/
1900 vout->win.global_alpha = 255;
1901 vout->fbuf.flags = 0;
1902 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1903 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1904 vout->win.chromakey = 0;
1906 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1908 /*Initialize the control variables for
1909 rotation, flipping and background color. */
1910 control = vout->control;
1911 control[0].id = V4L2_CID_ROTATE;
1912 control[0].value = 0;
1915 vout->control[2].id = V4L2_CID_HFLIP;
1916 vout->control[2].value = 0;
1917 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1920 control[1].id = V4L2_CID_BG_COLOR;
1921 control[1].value = 0;
1923 /* initialize the video_device struct */
1924 vfd = vout->vfd = video_device_alloc();
1927 printk(KERN_ERR VOUT_NAME ": could not allocate"
1928 " video device struct\n");
1931 vfd->release = video_device_release;
1932 vfd->ioctl_ops = &vout_ioctl_ops;
1934 strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1936 vfd->fops = &omap_vout_fops;
1937 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
1938 mutex_init(&vout->lock);
1945 /* Setup video buffers */
1946 static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1951 struct omapvideo_info *ovid;
1952 struct omap_vout_device *vout;
1953 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1954 struct omap2video_device *vid_dev =
1955 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1957 vout = vid_dev->vouts[vid_num];
1958 ovid = &vout->vid_info;
1960 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1961 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1962 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1964 for (i = 0; i < numbuffers; i++) {
1965 vout->buf_virt_addr[i] =
1966 omap_vout_alloc_buffer(vout->buffer_size,
1967 (u32 *) &vout->buf_phy_addr[i]);
1968 if (!vout->buf_virt_addr[i]) {
1975 vout->cropped_offset = 0;
1977 if (ovid->rotation_type == VOUT_ROT_VRFB) {
1978 int static_vrfb_allocation = (vid_num == 0) ?
1979 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1980 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1981 static_vrfb_allocation);
1987 for (i = 0; i < numbuffers; i++) {
1988 omap_vout_free_buffer(vout->buf_virt_addr[i],
1990 vout->buf_virt_addr[i] = 0;
1991 vout->buf_phy_addr[i] = 0;
1997 /* Create video out devices */
1998 static int __init omap_vout_create_video_devices(struct platform_device *pdev)
2001 struct omap_vout_device *vout;
2002 struct video_device *vfd = NULL;
2003 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2004 struct omap2video_device *vid_dev = container_of(v4l2_dev,
2005 struct omap2video_device, v4l2_dev);
2007 for (k = 0; k < pdev->num_resources; k++) {
2009 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
2011 dev_err(&pdev->dev, ": could not allocate memory\n");
2016 vid_dev->vouts[k] = vout;
2017 vout->vid_dev = vid_dev;
2018 /* Select video2 if only 1 overlay is controlled by V4L2 */
2019 if (pdev->num_resources == 1)
2020 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2022 /* Else select video1 and video2 one by one. */
2023 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2024 vout->vid_info.num_overlays = 1;
2025 vout->vid_info.id = k + 1;
2027 /* Set VRFB as rotation_type for omap2 and omap3 */
2028 if (cpu_is_omap24xx() || cpu_is_omap34xx())
2029 vout->vid_info.rotation_type = VOUT_ROT_VRFB;
2031 /* Setup the default configuration for the video devices
2033 if (omap_vout_setup_video_data(vout) != 0) {
2038 /* Allocate default number of buffers for the video streaming
2039 * and reserve the VRFB space for rotation
2041 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2046 /* Register the Video device with V4L2
2049 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
2050 dev_err(&pdev->dev, ": Could not register "
2051 "Video for Linux device\n");
2056 video_set_drvdata(vfd, vout);
2058 /* Configure the overlay structure */
2059 ret = omapvid_init(vid_dev->vouts[k], 0);
2064 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2065 omap_vout_release_vrfb(vout);
2066 omap_vout_free_buffers(vout);
2068 video_device_release(vfd);
2074 dev_info(&pdev->dev, ": registered and initialized"
2075 " video device %d\n", vfd->minor);
2076 if (k == (pdev->num_resources - 1))
2082 /* Driver functions */
2083 static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2085 struct video_device *vfd;
2086 struct omapvideo_info *ovid;
2092 ovid = &vout->vid_info;
2094 if (!video_is_registered(vfd)) {
2096 * The device was never registered, so release the
2097 * video_device struct directly.
2099 video_device_release(vfd);
2102 * The unregister function will release the video_device
2103 * struct as well as unregistering it.
2105 video_unregister_device(vfd);
2108 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2109 omap_vout_release_vrfb(vout);
2110 /* Free the VRFB buffer if allocated
2113 if (vout->vrfb_static_allocation)
2114 omap_vout_free_vrfb_buffers(vout);
2116 omap_vout_free_buffers(vout);
2121 static int omap_vout_remove(struct platform_device *pdev)
2124 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2125 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2126 omap2video_device, v4l2_dev);
2128 v4l2_device_unregister(v4l2_dev);
2129 for (k = 0; k < pdev->num_resources; k++)
2130 omap_vout_cleanup_device(vid_dev->vouts[k]);
2132 for (k = 0; k < vid_dev->num_displays; k++) {
2133 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
2134 vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
2136 omap_dss_put_device(vid_dev->displays[k]);
2142 static int __init omap_vout_probe(struct platform_device *pdev)
2145 struct omap_overlay *ovl;
2146 struct omap_dss_device *dssdev = NULL;
2147 struct omap_dss_device *def_display;
2148 struct omap2video_device *vid_dev = NULL;
2150 if (pdev->num_resources == 0) {
2151 dev_err(&pdev->dev, "probed for an unknown device\n");
2155 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2156 if (vid_dev == NULL)
2159 vid_dev->num_displays = 0;
2160 for_each_dss_dev(dssdev) {
2161 omap_dss_get_device(dssdev);
2162 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2165 if (vid_dev->num_displays == 0) {
2166 dev_err(&pdev->dev, "no displays\n");
2171 vid_dev->num_overlays = omap_dss_get_num_overlays();
2172 for (i = 0; i < vid_dev->num_overlays; i++)
2173 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2175 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2176 for (i = 0; i < vid_dev->num_managers; i++)
2177 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2179 /* Get the Video1 overlay and video2 overlay.
2180 * Setup the Display attached to that overlays
2182 for (i = 1; i < vid_dev->num_overlays; i++) {
2183 ovl = omap_dss_get_overlay(i);
2184 if (ovl->manager && ovl->manager->device) {
2185 def_display = ovl->manager->device;
2187 dev_warn(&pdev->dev, "cannot find display\n");
2191 struct omap_dss_driver *dssdrv = def_display->driver;
2193 ret = dssdrv->enable(def_display);
2195 /* Here we are not considering a error
2196 * as display may be enabled by frame
2199 dev_warn(&pdev->dev,
2200 "'%s' Display already enabled\n",
2206 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2207 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2212 ret = omap_vout_create_video_devices(pdev);
2216 for (i = 0; i < vid_dev->num_displays; i++) {
2217 struct omap_dss_device *display = vid_dev->displays[i];
2219 if (display->driver->update)
2220 display->driver->update(display, 0, 0,
2221 display->panel.timings.x_res,
2222 display->panel.timings.y_res);
2227 v4l2_device_unregister(&vid_dev->v4l2_dev);
2229 for (i = 1; i < vid_dev->num_overlays; i++) {
2231 ovl = omap_dss_get_overlay(i);
2232 if (ovl->manager && ovl->manager->device)
2233 def_display = ovl->manager->device;
2235 if (def_display && def_display->driver)
2236 def_display->driver->disable(def_display);
2243 static struct platform_driver omap_vout_driver = {
2247 .probe = omap_vout_probe,
2248 .remove = omap_vout_remove,
2251 static int __init omap_vout_init(void)
2253 if (platform_driver_register(&omap_vout_driver) != 0) {
2254 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2260 static void omap_vout_cleanup(void)
2262 platform_driver_unregister(&omap_vout_driver);
2265 late_initcall(omap_vout_init);
2266 module_exit(omap_vout_cleanup);