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>
47 #include <video/omapvrfb.h>
48 #include <video/omapfb_dss.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 bool vid1_static_vrfb_alloc;
73 static bool 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 .pixelformat = V4L2_PIX_FMT_RGB565,
120 /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
121 * this for RGB24 unpack mode, the last 8 bits are ignored
123 .pixelformat = V4L2_PIX_FMT_RGB32,
126 /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
127 * this for RGB24 packed mode
130 .pixelformat = V4L2_PIX_FMT_RGB24,
133 .pixelformat = V4L2_PIX_FMT_YUYV,
136 .pixelformat = V4L2_PIX_FMT_UYVY,
140 #define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
145 static int omap_vout_try_format(struct v4l2_pix_format *pix)
149 pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
150 (u32)VID_MAX_HEIGHT);
151 pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
153 for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
154 if (pix->pixelformat == omap_formats[ifmt].pixelformat)
158 if (ifmt == NUM_OUTPUT_FORMATS)
161 pix->pixelformat = omap_formats[ifmt].pixelformat;
162 pix->field = V4L2_FIELD_ANY;
164 switch (pix->pixelformat) {
165 case V4L2_PIX_FMT_YUYV:
166 case V4L2_PIX_FMT_UYVY:
168 pix->colorspace = V4L2_COLORSPACE_JPEG;
171 case V4L2_PIX_FMT_RGB565:
172 case V4L2_PIX_FMT_RGB565X:
173 pix->colorspace = V4L2_COLORSPACE_SRGB;
176 case V4L2_PIX_FMT_RGB24:
177 pix->colorspace = V4L2_COLORSPACE_SRGB;
180 case V4L2_PIX_FMT_RGB32:
181 case V4L2_PIX_FMT_BGR32:
182 pix->colorspace = V4L2_COLORSPACE_SRGB;
186 pix->bytesperline = pix->width * bpp;
187 pix->sizeimage = pix->bytesperline * pix->height;
193 * omap_vout_get_userptr: Convert user space virtual address to physical
196 static int omap_vout_get_userptr(struct videobuf_buffer *vb, long virtp,
199 struct frame_vector *vec;
202 /* For kernel direct-mapped memory, take the easy way */
203 if (virtp >= PAGE_OFFSET) {
204 *physp = virt_to_phys((void *)virtp);
208 vec = frame_vector_create(1);
212 ret = get_vaddr_frames(virtp, 1, FOLL_WRITE, vec);
214 frame_vector_destroy(vec);
217 *physp = __pfn_to_phys(frame_vector_pfns(vec)[0]);
224 * Free the V4L2 buffers
226 void omap_vout_free_buffers(struct omap_vout_device *vout)
230 /* Allocate memory for the buffers */
231 numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
232 vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
234 for (i = 0; i < numbuffers; i++) {
235 omap_vout_free_buffer(vout->buf_virt_addr[i],
237 vout->buf_phy_addr[i] = 0;
238 vout->buf_virt_addr[i] = 0;
243 * Convert V4L2 rotation to DSS rotation
244 * V4L2 understand 0, 90, 180, 270.
245 * Convert to 0, 1, 2 and 3 respectively for DSS
247 static int v4l2_rot_to_dss_rot(int v4l2_rotation,
248 enum dss_rotation *rotation, bool mirror)
252 switch (v4l2_rotation) {
254 *rotation = dss_rotation_90_degree;
257 *rotation = dss_rotation_180_degree;
260 *rotation = dss_rotation_270_degree;
263 *rotation = dss_rotation_0_degree;
271 static int omap_vout_calculate_offset(struct omap_vout_device *vout)
273 struct omapvideo_info *ovid;
274 struct v4l2_rect *crop = &vout->crop;
275 struct v4l2_pix_format *pix = &vout->pix;
276 int *cropped_offset = &vout->cropped_offset;
277 int ps = 2, line_length = 0;
279 ovid = &vout->vid_info;
281 if (ovid->rotation_type == VOUT_ROT_VRFB) {
282 omap_vout_calculate_vrfb_offset(vout);
284 vout->line_length = line_length = pix->width;
286 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
287 V4L2_PIX_FMT_UYVY == pix->pixelformat)
289 else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
291 else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
296 *cropped_offset = (line_length * ps) *
297 crop->top + crop->left * ps;
300 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
301 __func__, vout->cropped_offset);
307 * Convert V4L2 pixel format to DSS pixel format
309 static int video_mode_to_dss_mode(struct omap_vout_device *vout)
311 struct omap_overlay *ovl;
312 struct omapvideo_info *ovid;
313 struct v4l2_pix_format *pix = &vout->pix;
314 enum omap_color_mode mode;
316 ovid = &vout->vid_info;
317 ovl = ovid->overlays[0];
319 switch (pix->pixelformat) {
320 case V4L2_PIX_FMT_YUYV:
321 mode = OMAP_DSS_COLOR_YUV2;
323 case V4L2_PIX_FMT_UYVY:
324 mode = OMAP_DSS_COLOR_UYVY;
326 case V4L2_PIX_FMT_RGB565:
327 mode = OMAP_DSS_COLOR_RGB16;
329 case V4L2_PIX_FMT_RGB24:
330 mode = OMAP_DSS_COLOR_RGB24P;
332 case V4L2_PIX_FMT_RGB32:
333 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
334 OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
336 case V4L2_PIX_FMT_BGR32:
337 mode = OMAP_DSS_COLOR_RGBX32;
349 static int omapvid_setup_overlay(struct omap_vout_device *vout,
350 struct omap_overlay *ovl, int posx, int posy, int outw,
354 struct omap_overlay_info info;
355 int cropheight, cropwidth, pixwidth;
357 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
358 (outw != vout->pix.width || outh != vout->pix.height)) {
363 vout->dss_mode = video_mode_to_dss_mode(vout);
364 if (vout->dss_mode == -EINVAL) {
369 /* Setup the input plane parameters according to
370 * rotation value selected.
372 if (is_rotation_90_or_270(vout)) {
373 cropheight = vout->crop.width;
374 cropwidth = vout->crop.height;
375 pixwidth = vout->pix.height;
377 cropheight = vout->crop.height;
378 cropwidth = vout->crop.width;
379 pixwidth = vout->pix.width;
382 ovl->get_overlay_info(ovl, &info);
384 info.width = cropwidth;
385 info.height = cropheight;
386 info.color_mode = vout->dss_mode;
387 info.mirror = vout->mirror;
390 info.out_width = outw;
391 info.out_height = outh;
392 info.global_alpha = vout->win.global_alpha;
393 if (!is_rotation_enabled(vout)) {
395 info.rotation_type = OMAP_DSS_ROT_DMA;
396 info.screen_width = pixwidth;
398 info.rotation = vout->rotation;
399 info.rotation_type = OMAP_DSS_ROT_VRFB;
400 info.screen_width = 2048;
403 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
404 "%s enable=%d addr=%pad width=%d\n height=%d color_mode=%d\n"
405 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
406 "out_height=%d rotation_type=%d screen_width=%d\n", __func__,
407 ovl->is_enabled(ovl), &info.paddr, info.width, info.height,
408 info.color_mode, info.rotation, info.mirror, info.pos_x,
409 info.pos_y, info.out_width, info.out_height, info.rotation_type,
412 ret = ovl->set_overlay_info(ovl, &info);
419 v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
424 * Initialize the overlay structure
426 static int omapvid_init(struct omap_vout_device *vout, u32 addr)
429 struct v4l2_window *win;
430 struct omap_overlay *ovl;
431 int posx, posy, outw, outh;
432 struct omap_video_timings *timing;
433 struct omapvideo_info *ovid = &vout->vid_info;
436 for (i = 0; i < ovid->num_overlays; i++) {
437 struct omap_dss_device *dssdev;
439 ovl = ovid->overlays[i];
440 dssdev = ovl->get_device(ovl);
445 timing = &dssdev->panel.timings;
448 outh = win->w.height;
449 switch (vout->rotation) {
450 case dss_rotation_90_degree:
451 /* Invert the height and width for 90
452 * and 270 degree rotation
455 posy = (timing->y_res - win->w.width) - win->w.left;
459 case dss_rotation_180_degree:
460 posx = (timing->x_res - win->w.width) - win->w.left;
461 posy = (timing->y_res - win->w.height) - win->w.top;
464 case dss_rotation_270_degree:
467 posx = (timing->x_res - win->w.height) - win->w.top;
476 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
479 goto omapvid_init_err;
484 v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
489 * Apply the changes set the go bit of DSS
491 static int omapvid_apply_changes(struct omap_vout_device *vout)
494 struct omap_overlay *ovl;
495 struct omapvideo_info *ovid = &vout->vid_info;
497 for (i = 0; i < ovid->num_overlays; i++) {
498 struct omap_dss_device *dssdev;
500 ovl = ovid->overlays[i];
501 dssdev = ovl->get_device(ovl);
504 ovl->manager->apply(ovl->manager);
510 static int omapvid_handle_interlace_display(struct omap_vout_device *vout,
511 unsigned int irqstatus, u64 ts)
515 if (vout->first_int) {
520 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
522 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
528 if (fid != vout->field_id) {
530 vout->field_id = fid;
531 } else if (0 == fid) {
532 if (vout->cur_frm == vout->next_frm)
535 vout->cur_frm->ts = ts;
536 vout->cur_frm->state = VIDEOBUF_DONE;
537 wake_up_interruptible(&vout->cur_frm->done);
538 vout->cur_frm = vout->next_frm;
540 if (list_empty(&vout->dma_queue) ||
541 (vout->cur_frm != vout->next_frm))
545 return vout->field_id;
550 static void omap_vout_isr(void *arg, unsigned int irqstatus)
552 int ret, fid, mgr_id;
554 struct omap_overlay *ovl;
556 struct omapvideo_info *ovid;
557 struct omap_dss_device *cur_display;
558 struct omap_vout_device *vout = (struct omap_vout_device *)arg;
560 if (!vout->streaming)
563 ovid = &vout->vid_info;
564 ovl = ovid->overlays[0];
566 mgr_id = ovl->manager->id;
568 /* get the display device attached to the overlay */
569 cur_display = ovl->get_device(ovl);
574 spin_lock(&vout->vbq_lock);
577 switch (cur_display->type) {
578 case OMAP_DISPLAY_TYPE_DSI:
579 case OMAP_DISPLAY_TYPE_DPI:
580 case OMAP_DISPLAY_TYPE_DVI:
581 if (mgr_id == OMAP_DSS_CHANNEL_LCD)
582 irq = DISPC_IRQ_VSYNC;
583 else if (mgr_id == OMAP_DSS_CHANNEL_LCD2)
584 irq = DISPC_IRQ_VSYNC2;
588 if (!(irqstatus & irq))
591 case OMAP_DISPLAY_TYPE_VENC:
592 fid = omapvid_handle_interlace_display(vout, irqstatus,
597 case OMAP_DISPLAY_TYPE_HDMI:
598 if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
605 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
606 vout->cur_frm->ts = ts;
607 vout->cur_frm->state = VIDEOBUF_DONE;
608 wake_up_interruptible(&vout->cur_frm->done);
609 vout->cur_frm = vout->next_frm;
613 if (list_empty(&vout->dma_queue))
616 vout->next_frm = list_entry(vout->dma_queue.next,
617 struct videobuf_buffer, queue);
618 list_del(&vout->next_frm->queue);
620 vout->next_frm->state = VIDEOBUF_ACTIVE;
622 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
623 + vout->cropped_offset;
625 /* First save the configuration in ovelray structure */
626 ret = omapvid_init(vout, addr);
628 printk(KERN_ERR VOUT_NAME
629 "failed to set overlay info\n");
633 /* Enable the pipeline and set the Go bit */
634 ret = omapvid_apply_changes(vout);
636 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
639 spin_unlock(&vout->vbq_lock);
642 /* Video buffer call backs */
645 * Buffer setup function is called by videobuf layer when REQBUF ioctl is
646 * called. This is used to setup buffers and return size and count of
647 * buffers allocated. After the call to this buffer, videobuf layer will
648 * setup buffer queue depending on the size and count of buffers
650 static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
653 int startindex = 0, i, j;
654 u32 phy_addr = 0, virt_addr = 0;
655 struct omap_vout_device *vout = q->priv_data;
656 struct omapvideo_info *ovid = &vout->vid_info;
657 int vid_max_buf_size;
662 vid_max_buf_size = vout->vid == OMAP_VIDEO1 ? video1_bufsize :
665 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
668 startindex = (vout->vid == OMAP_VIDEO1) ?
669 video1_numbuffers : video2_numbuffers;
670 if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
673 if (ovid->rotation_type == VOUT_ROT_VRFB) {
674 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
678 if (V4L2_MEMORY_MMAP != vout->memory)
681 /* Now allocated the V4L2 buffers */
682 *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
683 startindex = (vout->vid == OMAP_VIDEO1) ?
684 video1_numbuffers : video2_numbuffers;
686 /* Check the size of the buffer */
687 if (*size > vid_max_buf_size) {
688 v4l2_err(&vout->vid_dev->v4l2_dev,
689 "buffer allocation mismatch [%u] [%u]\n",
690 *size, vout->buffer_size);
694 for (i = startindex; i < *count; i++) {
695 vout->buffer_size = *size;
697 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
700 if (ovid->rotation_type == VOUT_ROT_NONE)
703 if (!is_rotation_enabled(vout))
706 /* Free the VRFB buffers if no space for V4L2 buffers */
707 for (j = i; j < *count; j++) {
708 omap_vout_free_buffer(vout->smsshado_virt_addr[j],
709 vout->smsshado_size);
710 vout->smsshado_virt_addr[j] = 0;
711 vout->smsshado_phy_addr[j] = 0;
714 vout->buf_virt_addr[i] = virt_addr;
715 vout->buf_phy_addr[i] = phy_addr;
717 *count = vout->buffer_allocated = i;
723 * Free the V4L2 buffers additionally allocated than default
726 static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
728 int num_buffers = 0, i;
730 num_buffers = (vout->vid == OMAP_VIDEO1) ?
731 video1_numbuffers : video2_numbuffers;
733 for (i = num_buffers; i < vout->buffer_allocated; i++) {
734 if (vout->buf_virt_addr[i])
735 omap_vout_free_buffer(vout->buf_virt_addr[i],
738 vout->buf_virt_addr[i] = 0;
739 vout->buf_phy_addr[i] = 0;
741 vout->buffer_allocated = num_buffers;
745 * This function will be called when VIDIOC_QBUF ioctl is called.
746 * It prepare buffers before give out for the display. This function
747 * converts user space virtual address into physical address if userptr memory
748 * exchange mechanism is used. If rotation is enabled, it copies entire
749 * buffer into VRFB memory space before giving it to the DSS.
751 static int omap_vout_buffer_prepare(struct videobuf_queue *q,
752 struct videobuf_buffer *vb,
753 enum v4l2_field field)
755 struct omap_vout_device *vout = q->priv_data;
756 struct omapvideo_info *ovid = &vout->vid_info;
758 if (VIDEOBUF_NEEDS_INIT == vb->state) {
759 vb->width = vout->pix.width;
760 vb->height = vout->pix.height;
761 vb->size = vb->width * vb->height * vout->bpp;
764 vb->state = VIDEOBUF_PREPARED;
765 /* if user pointer memory mechanism is used, get the physical
766 * address of the buffer
768 if (V4L2_MEMORY_USERPTR == vb->memory) {
773 /* Physical address */
774 ret = omap_vout_get_userptr(vb, vb->baddr,
775 (u32 *)&vout->queued_buf_addr[vb->i]);
779 unsigned long addr, dma_addr;
782 addr = (unsigned long) vout->buf_virt_addr[vb->i];
783 size = (unsigned long) vb->size;
785 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
786 size, DMA_TO_DEVICE);
787 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
788 v4l2_err(&vout->vid_dev->v4l2_dev,
789 "dma_map_single failed\n");
791 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
794 if (ovid->rotation_type == VOUT_ROT_VRFB)
795 return omap_vout_prepare_vrfb(vout, vb);
801 * Buffer queue function will be called from the videobuf layer when _QBUF
802 * ioctl is called. It is used to enqueue buffer, which is ready to be
805 static void omap_vout_buffer_queue(struct videobuf_queue *q,
806 struct videobuf_buffer *vb)
808 struct omap_vout_device *vout = q->priv_data;
810 /* Driver is also maintainig a queue. So enqueue buffer in the driver
812 list_add_tail(&vb->queue, &vout->dma_queue);
814 vb->state = VIDEOBUF_QUEUED;
818 * Buffer release function is called from videobuf layer to release buffer
819 * which are already allocated
821 static void omap_vout_buffer_release(struct videobuf_queue *q,
822 struct videobuf_buffer *vb)
824 vb->state = VIDEOBUF_NEEDS_INIT;
825 if (vb->memory == V4L2_MEMORY_USERPTR && vb->priv) {
826 struct frame_vector *vec = vb->priv;
828 put_vaddr_frames(vec);
829 frame_vector_destroy(vec);
836 static __poll_t omap_vout_poll(struct file *file,
837 struct poll_table_struct *wait)
839 struct omap_vout_device *vout = video_drvdata(file);
840 struct videobuf_queue *q = &vout->vbq;
842 return videobuf_poll_stream(file, q, wait);
845 static void omap_vout_vm_open(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_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
854 static void omap_vout_vm_close(struct vm_area_struct *vma)
856 struct omap_vout_device *vout = vma->vm_private_data;
858 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
859 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
863 static const struct vm_operations_struct omap_vout_vm_ops = {
864 .open = omap_vout_vm_open,
865 .close = omap_vout_vm_close,
868 static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
872 unsigned long start = vma->vm_start;
873 unsigned long size = (vma->vm_end - vma->vm_start);
874 struct omap_vout_device *vout = video_drvdata(file);
875 struct videobuf_queue *q = &vout->vbq;
877 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
878 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
879 vma->vm_pgoff, vma->vm_start, vma->vm_end);
881 /* look for the buffer to map */
882 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
883 if (NULL == q->bufs[i])
885 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
887 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
891 if (VIDEO_MAX_FRAME == i) {
892 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
893 "offset invalid [offset=0x%lx]\n",
894 (vma->vm_pgoff << PAGE_SHIFT));
897 /* Check the size of the buffer */
898 if (size > vout->buffer_size) {
899 v4l2_err(&vout->vid_dev->v4l2_dev,
900 "insufficient memory [%lu] [%u]\n",
901 size, vout->buffer_size);
905 q->bufs[i]->baddr = vma->vm_start;
907 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
908 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
909 vma->vm_ops = &omap_vout_vm_ops;
910 vma->vm_private_data = (void *) vout;
911 pos = (void *)vout->buf_virt_addr[i];
912 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
915 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
916 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
923 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
928 static int omap_vout_release(struct file *file)
931 struct videobuf_queue *q;
932 struct omapvideo_info *ovid;
933 struct omap_vout_device *vout = video_drvdata(file);
935 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
936 ovid = &vout->vid_info;
942 /* Disable all the overlay managers connected with this interface */
943 for (i = 0; i < ovid->num_overlays; i++) {
944 struct omap_overlay *ovl = ovid->overlays[i];
945 struct omap_dss_device *dssdev = ovl->get_device(ovl);
950 /* Turn off the pipeline */
951 ret = omapvid_apply_changes(vout);
953 v4l2_warn(&vout->vid_dev->v4l2_dev,
954 "Unable to apply changes\n");
956 /* Free all buffers */
957 omap_vout_free_extra_buffers(vout);
959 /* Free the VRFB buffers only if they are allocated
960 * during reqbufs. Don't free if init time allocated
962 if (ovid->rotation_type == VOUT_ROT_VRFB) {
963 if (!vout->vrfb_static_allocation)
964 omap_vout_free_vrfb_buffers(vout);
966 videobuf_mmap_free(q);
968 /* Even if apply changes fails we should continue
969 freeing allocated memory */
970 if (vout->streaming) {
973 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
974 DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
975 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
976 vout->streaming = false;
978 videobuf_streamoff(q);
979 videobuf_queue_cancel(q);
982 if (vout->mmap_count != 0)
983 vout->mmap_count = 0;
986 v4l2_fh_release(file);
988 if (vout->buffer_allocated)
989 videobuf_mmap_free(q);
991 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
995 static int omap_vout_open(struct file *file)
997 struct videobuf_queue *q;
998 struct omap_vout_device *vout = video_drvdata(file);
1004 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1006 /* for now, we only support single open */
1010 ret = v4l2_fh_open(file);
1015 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1018 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1019 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1020 video_vbq_ops.buf_release = omap_vout_buffer_release;
1021 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1022 spin_lock_init(&vout->vbq_lock);
1024 videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1025 &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
1026 sizeof(struct videobuf_buffer), vout, NULL);
1028 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1035 static int vidioc_querycap(struct file *file, void *fh,
1036 struct v4l2_capability *cap)
1038 struct omap_vout_device *vout = video_drvdata(file);
1040 strscpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1041 strscpy(cap->card, vout->vfd->name, sizeof(cap->card));
1042 cap->bus_info[0] = '\0';
1046 static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1047 struct v4l2_fmtdesc *fmt)
1049 int index = fmt->index;
1051 if (index >= NUM_OUTPUT_FORMATS)
1054 fmt->flags = omap_formats[index].flags;
1055 fmt->pixelformat = omap_formats[index].pixelformat;
1060 static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1061 struct v4l2_format *f)
1063 struct omap_vout_device *vout = video_drvdata(file);
1065 f->fmt.pix = vout->pix;
1070 static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1071 struct v4l2_format *f)
1073 struct omap_overlay *ovl;
1074 struct omapvideo_info *ovid;
1075 struct omap_video_timings *timing;
1076 struct omap_vout_device *vout = video_drvdata(file);
1077 struct omap_dss_device *dssdev;
1079 ovid = &vout->vid_info;
1080 ovl = ovid->overlays[0];
1081 /* get the display device attached to the overlay */
1082 dssdev = ovl->get_device(ovl);
1087 timing = &dssdev->panel.timings;
1089 vout->fbuf.fmt.height = timing->y_res;
1090 vout->fbuf.fmt.width = timing->x_res;
1092 omap_vout_try_format(&f->fmt.pix);
1096 static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1097 struct v4l2_format *f)
1100 struct omap_overlay *ovl;
1101 struct omapvideo_info *ovid;
1102 struct omap_video_timings *timing;
1103 struct omap_vout_device *vout = video_drvdata(file);
1104 struct omap_dss_device *dssdev;
1106 if (vout->streaming)
1109 mutex_lock(&vout->lock);
1111 ovid = &vout->vid_info;
1112 ovl = ovid->overlays[0];
1113 dssdev = ovl->get_device(ovl);
1115 /* get the display device attached to the overlay */
1118 goto s_fmt_vid_out_exit;
1120 timing = &dssdev->panel.timings;
1122 /* We don't support RGB24-packed mode if vrfb rotation
1124 if ((is_rotation_enabled(vout)) &&
1125 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1127 goto s_fmt_vid_out_exit;
1130 /* get the framebuffer parameters */
1132 if (is_rotation_90_or_270(vout)) {
1133 vout->fbuf.fmt.height = timing->x_res;
1134 vout->fbuf.fmt.width = timing->y_res;
1136 vout->fbuf.fmt.height = timing->y_res;
1137 vout->fbuf.fmt.width = timing->x_res;
1140 /* change to smaller size is OK */
1142 bpp = omap_vout_try_format(&f->fmt.pix);
1143 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1145 /* try & set the new output format */
1147 vout->pix = f->fmt.pix;
1150 /* If YUYV then vrfb bpp is 2, for others its 1 */
1151 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1152 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1155 /* set default crop and win */
1156 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1161 mutex_unlock(&vout->lock);
1165 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1166 struct v4l2_format *f)
1169 struct omap_vout_device *vout = video_drvdata(file);
1170 struct omap_overlay *ovl;
1171 struct omapvideo_info *ovid;
1172 struct v4l2_window *win = &f->fmt.win;
1174 ovid = &vout->vid_info;
1175 ovl = ovid->overlays[0];
1177 ret = omap_vout_try_window(&vout->fbuf, win);
1180 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1181 win->global_alpha = 255;
1183 win->global_alpha = f->fmt.win.global_alpha;
1189 static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1190 struct v4l2_format *f)
1193 struct omap_overlay *ovl;
1194 struct omapvideo_info *ovid;
1195 struct omap_vout_device *vout = video_drvdata(file);
1196 struct v4l2_window *win = &f->fmt.win;
1198 mutex_lock(&vout->lock);
1199 ovid = &vout->vid_info;
1200 ovl = ovid->overlays[0];
1202 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1204 /* Video1 plane does not support global alpha on OMAP3 */
1205 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1206 vout->win.global_alpha = 255;
1208 vout->win.global_alpha = f->fmt.win.global_alpha;
1210 vout->win.chromakey = f->fmt.win.chromakey;
1212 mutex_unlock(&vout->lock);
1216 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1217 struct v4l2_format *f)
1220 struct omap_overlay *ovl;
1221 struct omapvideo_info *ovid;
1222 struct omap_vout_device *vout = video_drvdata(file);
1223 struct omap_overlay_manager_info info;
1224 struct v4l2_window *win = &f->fmt.win;
1226 ovid = &vout->vid_info;
1227 ovl = ovid->overlays[0];
1229 win->w = vout->win.w;
1230 win->field = vout->win.field;
1231 win->global_alpha = vout->win.global_alpha;
1233 if (ovl->manager && ovl->manager->get_manager_info) {
1234 ovl->manager->get_manager_info(ovl->manager, &info);
1235 key_value = info.trans_key;
1237 win->chromakey = key_value;
1241 static int vidioc_g_selection(struct file *file, void *fh, struct v4l2_selection *sel)
1243 struct omap_vout_device *vout = video_drvdata(file);
1244 struct v4l2_pix_format *pix = &vout->pix;
1246 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1249 switch (sel->target) {
1250 case V4L2_SEL_TGT_CROP:
1251 sel->r = vout->crop;
1253 case V4L2_SEL_TGT_CROP_DEFAULT:
1254 omap_vout_default_crop(&vout->pix, &vout->fbuf, &sel->r);
1256 case V4L2_SEL_TGT_CROP_BOUNDS:
1257 /* Width and height are always even */
1258 sel->r.width = pix->width & ~1;
1259 sel->r.height = pix->height & ~1;
1267 static int vidioc_s_selection(struct file *file, void *fh, struct v4l2_selection *sel)
1270 struct omap_vout_device *vout = video_drvdata(file);
1271 struct omapvideo_info *ovid;
1272 struct omap_overlay *ovl;
1273 struct omap_video_timings *timing;
1274 struct omap_dss_device *dssdev;
1276 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1279 if (sel->target != V4L2_SEL_TGT_CROP)
1282 if (vout->streaming)
1285 mutex_lock(&vout->lock);
1286 ovid = &vout->vid_info;
1287 ovl = ovid->overlays[0];
1288 /* get the display device attached to the overlay */
1289 dssdev = ovl->get_device(ovl);
1296 timing = &dssdev->panel.timings;
1298 if (is_rotation_90_or_270(vout)) {
1299 vout->fbuf.fmt.height = timing->x_res;
1300 vout->fbuf.fmt.width = timing->y_res;
1302 vout->fbuf.fmt.height = timing->y_res;
1303 vout->fbuf.fmt.width = timing->x_res;
1306 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1307 &vout->fbuf, &sel->r);
1310 mutex_unlock(&vout->lock);
1314 static int omap_vout_s_ctrl(struct v4l2_ctrl *ctrl)
1316 struct omap_vout_device *vout =
1317 container_of(ctrl->handler, struct omap_vout_device, ctrl_handler);
1321 case V4L2_CID_ROTATE: {
1322 struct omapvideo_info *ovid;
1323 int rotation = ctrl->val;
1325 ovid = &vout->vid_info;
1327 mutex_lock(&vout->lock);
1328 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1329 mutex_unlock(&vout->lock);
1334 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1335 mutex_unlock(&vout->lock);
1340 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1342 mutex_unlock(&vout->lock);
1346 mutex_unlock(&vout->lock);
1349 case V4L2_CID_BG_COLOR:
1351 struct omap_overlay *ovl;
1352 unsigned int color = ctrl->val;
1353 struct omap_overlay_manager_info info;
1355 ovl = vout->vid_info.overlays[0];
1357 mutex_lock(&vout->lock);
1358 if (!ovl->manager || !ovl->manager->get_manager_info) {
1359 mutex_unlock(&vout->lock);
1364 ovl->manager->get_manager_info(ovl->manager, &info);
1365 info.default_color = color;
1366 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1367 mutex_unlock(&vout->lock);
1371 mutex_unlock(&vout->lock);
1374 case V4L2_CID_VFLIP:
1376 struct omapvideo_info *ovid;
1377 unsigned int mirror = ctrl->val;
1379 ovid = &vout->vid_info;
1381 mutex_lock(&vout->lock);
1382 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1383 mutex_unlock(&vout->lock);
1388 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1389 mutex_unlock(&vout->lock);
1393 vout->mirror = mirror;
1394 mutex_unlock(&vout->lock);
1403 static const struct v4l2_ctrl_ops omap_vout_ctrl_ops = {
1404 .s_ctrl = omap_vout_s_ctrl,
1407 static int vidioc_reqbufs(struct file *file, void *fh,
1408 struct v4l2_requestbuffers *req)
1411 unsigned int i, num_buffers = 0;
1412 struct omap_vout_device *vout = video_drvdata(file);
1413 struct videobuf_queue *q = &vout->vbq;
1415 if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1417 /* if memory is not mmp or userptr
1419 if ((V4L2_MEMORY_MMAP != req->memory) &&
1420 (V4L2_MEMORY_USERPTR != req->memory))
1423 mutex_lock(&vout->lock);
1424 /* Cannot be requested when streaming is on */
1425 if (vout->streaming) {
1430 /* If buffers are already allocated free them */
1431 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1432 if (vout->mmap_count) {
1436 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1437 video1_numbuffers : video2_numbuffers;
1438 for (i = num_buffers; i < vout->buffer_allocated; i++) {
1439 omap_vout_free_buffer(vout->buf_virt_addr[i],
1441 vout->buf_virt_addr[i] = 0;
1442 vout->buf_phy_addr[i] = 0;
1444 vout->buffer_allocated = num_buffers;
1445 videobuf_mmap_free(q);
1446 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1447 if (vout->buffer_allocated) {
1448 videobuf_mmap_free(q);
1449 for (i = 0; i < vout->buffer_allocated; i++) {
1453 vout->buffer_allocated = 0;
1457 /*store the memory type in data structure */
1458 vout->memory = req->memory;
1460 INIT_LIST_HEAD(&vout->dma_queue);
1462 /* call videobuf_reqbufs api */
1463 ret = videobuf_reqbufs(q, req);
1467 vout->buffer_allocated = req->count;
1470 mutex_unlock(&vout->lock);
1474 static int vidioc_querybuf(struct file *file, void *fh,
1475 struct v4l2_buffer *b)
1477 struct omap_vout_device *vout = video_drvdata(file);
1479 return videobuf_querybuf(&vout->vbq, b);
1482 static int vidioc_qbuf(struct file *file, void *fh,
1483 struct v4l2_buffer *buffer)
1485 struct omap_vout_device *vout = video_drvdata(file);
1486 struct videobuf_queue *q = &vout->vbq;
1488 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1489 (buffer->index >= vout->buffer_allocated) ||
1490 (q->bufs[buffer->index]->memory != buffer->memory)) {
1493 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1494 if ((buffer->length < vout->pix.sizeimage) ||
1495 (0 == buffer->m.userptr)) {
1500 if ((is_rotation_enabled(vout)) &&
1501 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1502 v4l2_warn(&vout->vid_dev->v4l2_dev,
1503 "DMA Channel not allocated for Rotation\n");
1507 return videobuf_qbuf(q, buffer);
1510 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1512 struct omap_vout_device *vout = video_drvdata(file);
1513 struct videobuf_queue *q = &vout->vbq;
1518 struct videobuf_buffer *vb;
1520 if (!vout->streaming)
1523 ret = videobuf_dqbuf(q, b, !!(file->f_flags & O_NONBLOCK));
1527 vb = q->bufs[b->index];
1529 addr = (unsigned long) vout->buf_phy_addr[vb->i];
1530 size = (unsigned long) vb->size;
1531 dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
1532 size, DMA_TO_DEVICE);
1536 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1539 u32 addr = 0, mask = 0;
1540 struct omap_vout_device *vout = video_drvdata(file);
1541 struct videobuf_queue *q = &vout->vbq;
1542 struct omapvideo_info *ovid = &vout->vid_info;
1544 mutex_lock(&vout->lock);
1546 if (vout->streaming) {
1551 ret = videobuf_streamon(q);
1555 if (list_empty(&vout->dma_queue)) {
1560 /* Get the next frame from the buffer queue */
1561 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1562 struct videobuf_buffer, queue);
1563 /* Remove buffer from the buffer queue */
1564 list_del(&vout->cur_frm->queue);
1565 /* Mark state of the current frame to active */
1566 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1567 /* Initialize field_id and started member */
1570 /* set flag here. Next QBUF will start DMA */
1571 vout->streaming = true;
1573 vout->first_int = 1;
1575 if (omap_vout_calculate_offset(vout)) {
1579 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1580 + vout->cropped_offset;
1582 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1585 /* First save the configuration in ovelray structure */
1586 ret = omapvid_init(vout, addr);
1588 v4l2_err(&vout->vid_dev->v4l2_dev,
1589 "failed to set overlay info\n");
1593 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1595 /* Enable the pipeline and set the Go bit */
1596 ret = omapvid_apply_changes(vout);
1598 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1600 for (j = 0; j < ovid->num_overlays; j++) {
1601 struct omap_overlay *ovl = ovid->overlays[j];
1602 struct omap_dss_device *dssdev = ovl->get_device(ovl);
1605 ret = ovl->enable(ovl);
1615 ret = videobuf_streamoff(q);
1617 mutex_unlock(&vout->lock);
1621 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1625 struct omap_vout_device *vout = video_drvdata(file);
1626 struct omapvideo_info *ovid = &vout->vid_info;
1628 if (!vout->streaming)
1631 vout->streaming = false;
1632 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1635 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1637 for (j = 0; j < ovid->num_overlays; j++) {
1638 struct omap_overlay *ovl = ovid->overlays[j];
1639 struct omap_dss_device *dssdev = ovl->get_device(ovl);
1645 /* Turn of the pipeline */
1646 ret = omapvid_apply_changes(vout);
1648 v4l2_err(&vout->vid_dev->v4l2_dev,
1649 "failed to change mode in streamoff\n");
1651 INIT_LIST_HEAD(&vout->dma_queue);
1652 ret = videobuf_streamoff(&vout->vbq);
1657 static int vidioc_s_fbuf(struct file *file, void *fh,
1658 const struct v4l2_framebuffer *a)
1661 struct omap_overlay *ovl;
1662 struct omapvideo_info *ovid;
1663 struct omap_vout_device *vout = video_drvdata(file);
1664 struct omap_overlay_manager_info info;
1665 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1667 ovid = &vout->vid_info;
1668 ovl = ovid->overlays[0];
1670 /* OMAP DSS doesn't support Source and Destination color
1672 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1673 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1675 /* OMAP DSS Doesn't support the Destination color key
1676 and alpha blending together */
1677 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1678 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1681 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1682 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1683 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
1685 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1687 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1688 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1689 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1691 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
1693 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1694 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1698 if (ovl->manager && ovl->manager->get_manager_info &&
1699 ovl->manager->set_manager_info) {
1701 ovl->manager->get_manager_info(ovl->manager, &info);
1702 info.trans_enabled = enable;
1703 info.trans_key_type = key_type;
1704 info.trans_key = vout->win.chromakey;
1706 if (ovl->manager->set_manager_info(ovl->manager, &info))
1709 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1710 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1713 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1716 if (ovl->manager && ovl->manager->get_manager_info &&
1717 ovl->manager->set_manager_info) {
1718 ovl->manager->get_manager_info(ovl->manager, &info);
1719 /* enable this only if there is no zorder cap */
1720 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1721 info.partial_alpha_enabled = enable;
1722 if (ovl->manager->set_manager_info(ovl->manager, &info))
1729 static int vidioc_g_fbuf(struct file *file, void *fh,
1730 struct v4l2_framebuffer *a)
1732 struct omap_overlay *ovl;
1733 struct omapvideo_info *ovid;
1734 struct omap_vout_device *vout = video_drvdata(file);
1735 struct omap_overlay_manager_info info;
1737 ovid = &vout->vid_info;
1738 ovl = ovid->overlays[0];
1740 /* The video overlay must stay within the framebuffer and can't be
1741 positioned independently. */
1742 a->flags = V4L2_FBUF_FLAG_OVERLAY;
1743 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1744 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1746 if (ovl->manager && ovl->manager->get_manager_info) {
1747 ovl->manager->get_manager_info(ovl->manager, &info);
1748 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1749 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1750 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1751 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1753 if (ovl->manager && ovl->manager->get_manager_info) {
1754 ovl->manager->get_manager_info(ovl->manager, &info);
1755 if (info.partial_alpha_enabled)
1756 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1762 static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1763 .vidioc_querycap = vidioc_querycap,
1764 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1765 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1766 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1767 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1768 .vidioc_s_fbuf = vidioc_s_fbuf,
1769 .vidioc_g_fbuf = vidioc_g_fbuf,
1770 .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_overlay,
1771 .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_overlay,
1772 .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_overlay,
1773 .vidioc_g_selection = vidioc_g_selection,
1774 .vidioc_s_selection = vidioc_s_selection,
1775 .vidioc_reqbufs = vidioc_reqbufs,
1776 .vidioc_querybuf = vidioc_querybuf,
1777 .vidioc_qbuf = vidioc_qbuf,
1778 .vidioc_dqbuf = vidioc_dqbuf,
1779 .vidioc_streamon = vidioc_streamon,
1780 .vidioc_streamoff = vidioc_streamoff,
1783 static const struct v4l2_file_operations omap_vout_fops = {
1784 .owner = THIS_MODULE,
1785 .poll = omap_vout_poll,
1786 .unlocked_ioctl = video_ioctl2,
1787 .mmap = omap_vout_mmap,
1788 .open = omap_vout_open,
1789 .release = omap_vout_release,
1792 /* Init functions used during driver initialization */
1793 /* Initial setup of video_data */
1794 static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1796 struct video_device *vfd;
1797 struct v4l2_pix_format *pix;
1798 struct omap_overlay *ovl = vout->vid_info.overlays[0];
1799 struct omap_dss_device *display = ovl->get_device(ovl);
1800 struct v4l2_ctrl_handler *hdl;
1802 /* set the default pix */
1805 /* Set the default picture of QVGA */
1806 pix->width = QQVGA_WIDTH;
1807 pix->height = QQVGA_HEIGHT;
1809 /* Default pixel format is RGB 5-6-5 */
1810 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1811 pix->field = V4L2_FIELD_ANY;
1812 pix->bytesperline = pix->width * 2;
1813 pix->sizeimage = pix->bytesperline * pix->height;
1814 pix->colorspace = V4L2_COLORSPACE_JPEG;
1816 vout->bpp = RGB565_BPP;
1817 vout->fbuf.fmt.width = display->panel.timings.x_res;
1818 vout->fbuf.fmt.height = display->panel.timings.y_res;
1820 /* Set the data structures for the overlay parameters*/
1821 vout->win.global_alpha = 255;
1822 vout->fbuf.flags = 0;
1823 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1824 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1825 vout->win.chromakey = 0;
1827 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1829 hdl = &vout->ctrl_handler;
1830 v4l2_ctrl_handler_init(hdl, 3);
1831 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1832 V4L2_CID_ROTATE, 0, 270, 90, 0);
1833 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1834 V4L2_CID_BG_COLOR, 0, 0xffffff, 1, 0);
1835 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1836 V4L2_CID_VFLIP, 0, 1, 1, 0);
1841 vout->mirror = false;
1842 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1845 /* initialize the video_device struct */
1846 vfd = vout->vfd = video_device_alloc();
1849 printk(KERN_ERR VOUT_NAME
1850 ": could not allocate video device struct\n");
1851 v4l2_ctrl_handler_free(hdl);
1854 vfd->ctrl_handler = hdl;
1855 vfd->release = video_device_release;
1856 vfd->ioctl_ops = &vout_ioctl_ops;
1858 strscpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1860 vfd->fops = &omap_vout_fops;
1861 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
1862 vfd->vfl_dir = VFL_DIR_TX;
1863 vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT |
1864 V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
1865 mutex_init(&vout->lock);
1872 /* Setup video buffers */
1873 static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1878 struct omapvideo_info *ovid;
1879 struct omap_vout_device *vout;
1880 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1881 struct omap2video_device *vid_dev =
1882 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1884 vout = vid_dev->vouts[vid_num];
1885 ovid = &vout->vid_info;
1887 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1888 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1889 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1891 for (i = 0; i < numbuffers; i++) {
1892 vout->buf_virt_addr[i] =
1893 omap_vout_alloc_buffer(vout->buffer_size,
1894 (u32 *) &vout->buf_phy_addr[i]);
1895 if (!vout->buf_virt_addr[i]) {
1902 vout->cropped_offset = 0;
1904 if (ovid->rotation_type == VOUT_ROT_VRFB) {
1905 bool static_vrfb_allocation = (vid_num == 0) ?
1906 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1907 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1908 static_vrfb_allocation);
1914 for (i = 0; i < numbuffers; i++) {
1915 omap_vout_free_buffer(vout->buf_virt_addr[i],
1917 vout->buf_virt_addr[i] = 0;
1918 vout->buf_phy_addr[i] = 0;
1924 /* Create video out devices */
1925 static int __init omap_vout_create_video_devices(struct platform_device *pdev)
1928 struct omap_vout_device *vout;
1929 struct video_device *vfd = NULL;
1930 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1931 struct omap2video_device *vid_dev = container_of(v4l2_dev,
1932 struct omap2video_device, v4l2_dev);
1934 for (k = 0; k < pdev->num_resources; k++) {
1936 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
1938 dev_err(&pdev->dev, ": could not allocate memory\n");
1943 vid_dev->vouts[k] = vout;
1944 vout->vid_dev = vid_dev;
1945 /* Select video2 if only 1 overlay is controlled by V4L2 */
1946 if (pdev->num_resources == 1)
1947 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
1949 /* Else select video1 and video2 one by one. */
1950 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
1951 vout->vid_info.num_overlays = 1;
1952 vout->vid_info.id = k + 1;
1954 /* Set VRFB as rotation_type for omap2 and omap3 */
1955 if (omap_vout_dss_omap24xx() || omap_vout_dss_omap34xx())
1956 vout->vid_info.rotation_type = VOUT_ROT_VRFB;
1958 /* Setup the default configuration for the video devices
1960 if (omap_vout_setup_video_data(vout) != 0) {
1965 /* Allocate default number of buffers for the video streaming
1966 * and reserve the VRFB space for rotation
1968 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
1973 /* Register the Video device with V4L2
1976 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
1978 ": Could not register Video for Linux device\n");
1983 video_set_drvdata(vfd, vout);
1985 dev_info(&pdev->dev,
1986 ": registered and initialized video device %d\n",
1988 if (k == (pdev->num_resources - 1))
1993 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1994 omap_vout_release_vrfb(vout);
1995 omap_vout_free_buffers(vout);
1997 video_device_release(vfd);
2005 /* Driver functions */
2006 static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2008 struct video_device *vfd;
2009 struct omapvideo_info *ovid;
2015 ovid = &vout->vid_info;
2017 if (!video_is_registered(vfd)) {
2019 * The device was never registered, so release the
2020 * video_device struct directly.
2022 video_device_release(vfd);
2025 * The unregister function will release the video_device
2026 * struct as well as unregistering it.
2028 video_unregister_device(vfd);
2031 v4l2_ctrl_handler_free(&vout->ctrl_handler);
2032 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2033 omap_vout_release_vrfb(vout);
2034 /* Free the VRFB buffer if allocated
2037 if (vout->vrfb_static_allocation)
2038 omap_vout_free_vrfb_buffers(vout);
2040 omap_vout_free_buffers(vout);
2045 static int omap_vout_remove(struct platform_device *pdev)
2048 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2049 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2050 omap2video_device, v4l2_dev);
2052 v4l2_device_unregister(v4l2_dev);
2053 for (k = 0; k < pdev->num_resources; k++)
2054 omap_vout_cleanup_device(vid_dev->vouts[k]);
2056 for (k = 0; k < vid_dev->num_displays; k++) {
2057 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
2058 vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
2060 omap_dss_put_device(vid_dev->displays[k]);
2066 static int __init omap_vout_probe(struct platform_device *pdev)
2069 struct omap_overlay *ovl;
2070 struct omap_dss_device *dssdev = NULL;
2071 struct omap_dss_device *def_display;
2072 struct omap2video_device *vid_dev = NULL;
2074 if (omapdss_is_initialized() == false)
2075 return -EPROBE_DEFER;
2077 ret = omapdss_compat_init();
2079 dev_err(&pdev->dev, "failed to init dss\n");
2083 if (pdev->num_resources == 0) {
2084 dev_err(&pdev->dev, "probed for an unknown device\n");
2089 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2090 if (vid_dev == NULL) {
2095 vid_dev->num_displays = 0;
2096 for_each_dss_dev(dssdev) {
2097 omap_dss_get_device(dssdev);
2099 if (!dssdev->driver) {
2100 dev_warn(&pdev->dev, "no driver for display: %s\n",
2102 omap_dss_put_device(dssdev);
2106 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2109 if (vid_dev->num_displays == 0) {
2110 dev_err(&pdev->dev, "no displays\n");
2115 vid_dev->num_overlays = omap_dss_get_num_overlays();
2116 for (i = 0; i < vid_dev->num_overlays; i++)
2117 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2119 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2120 for (i = 0; i < vid_dev->num_managers; i++)
2121 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2123 /* Get the Video1 overlay and video2 overlay.
2124 * Setup the Display attached to that overlays
2126 for (i = 1; i < vid_dev->num_overlays; i++) {
2127 ovl = omap_dss_get_overlay(i);
2128 dssdev = ovl->get_device(ovl);
2131 def_display = dssdev;
2133 dev_warn(&pdev->dev, "cannot find display\n");
2137 struct omap_dss_driver *dssdrv = def_display->driver;
2139 ret = dssdrv->enable(def_display);
2141 /* Here we are not considering a error
2142 * as display may be enabled by frame
2145 dev_warn(&pdev->dev,
2146 "'%s' Display already enabled\n",
2152 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2153 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2158 ret = omap_vout_create_video_devices(pdev);
2162 for (i = 0; i < vid_dev->num_displays; i++) {
2163 struct omap_dss_device *display = vid_dev->displays[i];
2165 if (display->driver->update)
2166 display->driver->update(display, 0, 0,
2167 display->panel.timings.x_res,
2168 display->panel.timings.y_res);
2173 v4l2_device_unregister(&vid_dev->v4l2_dev);
2175 for (i = 1; i < vid_dev->num_overlays; i++) {
2177 ovl = omap_dss_get_overlay(i);
2178 dssdev = ovl->get_device(ovl);
2181 def_display = dssdev;
2183 if (def_display && def_display->driver)
2184 def_display->driver->disable(def_display);
2189 omapdss_compat_uninit();
2193 static struct platform_driver omap_vout_driver = {
2197 .remove = omap_vout_remove,
2200 static int __init omap_vout_init(void)
2202 if (platform_driver_probe(&omap_vout_driver, omap_vout_probe) != 0) {
2203 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2209 static void omap_vout_cleanup(void)
2211 platform_driver_unregister(&omap_vout_driver);
2214 late_initcall(omap_vout_init);
2215 module_exit(omap_vout_cleanup);