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/dma-mapping.h>
39 #include <linux/irq.h>
40 #include <linux/videodev2.h>
42 #include <media/videobuf-dma-sg.h>
43 #include <media/v4l2-device.h>
44 #include <media/v4l2-ioctl.h>
47 #include <plat/vram.h>
48 #include <plat/vrfb.h>
49 #include <plat/display.h>
51 #include "omap_voutlib.h"
52 #include "omap_voutdef.h"
54 MODULE_AUTHOR("Texas Instruments");
55 MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
56 MODULE_LICENSE("GPL");
59 /* Driver Configuration macros */
60 #define VOUT_NAME "omap_vout"
62 enum omap_vout_channels {
67 enum dma_channel_state {
72 #define QQVGA_WIDTH 160
73 #define QQVGA_HEIGHT 120
75 /* Max Resolution supported by the driver */
76 #define VID_MAX_WIDTH 1280 /* Largest width */
77 #define VID_MAX_HEIGHT 720 /* Largest height */
79 /* Mimimum requirement is 2x2 for DSS */
80 #define VID_MIN_WIDTH 2
81 #define VID_MIN_HEIGHT 2
83 /* 2048 x 2048 is max res supported by OMAP display controller */
84 #define MAX_PIXELS_PER_LINE 2048
86 #define VRFB_TX_TIMEOUT 1000
87 #define VRFB_NUM_BUFS 4
89 /* Max buffer size tobe allocated during init */
90 #define OMAP_VOUT_MAX_BUF_SIZE (VID_MAX_WIDTH*VID_MAX_HEIGHT*4)
92 static struct videobuf_queue_ops video_vbq_ops;
93 /* Variables configurable through module params*/
94 static u32 video1_numbuffers = 3;
95 static u32 video2_numbuffers = 3;
96 static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
97 static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
98 static u32 vid1_static_vrfb_alloc;
99 static u32 vid2_static_vrfb_alloc;
102 /* Module parameters */
103 module_param(video1_numbuffers, uint, S_IRUGO);
104 MODULE_PARM_DESC(video1_numbuffers,
105 "Number of buffers to be allocated at init time for Video1 device.");
107 module_param(video2_numbuffers, uint, S_IRUGO);
108 MODULE_PARM_DESC(video2_numbuffers,
109 "Number of buffers to be allocated at init time for Video2 device.");
111 module_param(video1_bufsize, uint, S_IRUGO);
112 MODULE_PARM_DESC(video1_bufsize,
113 "Size of the buffer to be allocated for video1 device");
115 module_param(video2_bufsize, uint, S_IRUGO);
116 MODULE_PARM_DESC(video2_bufsize,
117 "Size of the buffer to be allocated for video2 device");
119 module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
120 MODULE_PARM_DESC(vid1_static_vrfb_alloc,
121 "Static allocation of the VRFB buffer for video1 device");
123 module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
124 MODULE_PARM_DESC(vid2_static_vrfb_alloc,
125 "Static allocation of the VRFB buffer for video2 device");
127 module_param(debug, bool, S_IRUGO);
128 MODULE_PARM_DESC(debug, "Debug level (0-1)");
130 /* list of image formats supported by OMAP2 video pipelines */
131 const static struct v4l2_fmtdesc omap_formats[] = {
133 /* Note: V4L2 defines RGB565 as:
136 * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
138 * We interpret RGB565 as:
141 * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
143 .description = "RGB565, le",
144 .pixelformat = V4L2_PIX_FMT_RGB565,
147 /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
148 * this for RGB24 unpack mode, the last 8 bits are ignored
150 .description = "RGB32, le",
151 .pixelformat = V4L2_PIX_FMT_RGB32,
154 /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
155 * this for RGB24 packed mode
158 .description = "RGB24, le",
159 .pixelformat = V4L2_PIX_FMT_RGB24,
162 .description = "YUYV (YUV 4:2:2), packed",
163 .pixelformat = V4L2_PIX_FMT_YUYV,
166 .description = "UYVY, packed",
167 .pixelformat = V4L2_PIX_FMT_UYVY,
171 #define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
176 static unsigned long omap_vout_alloc_buffer(u32 buf_size, u32 *phys_addr)
179 unsigned long virt_addr, addr;
181 size = PAGE_ALIGN(buf_size);
182 order = get_order(size);
183 virt_addr = __get_free_pages(GFP_KERNEL | GFP_DMA, order);
188 SetPageReserved(virt_to_page(addr));
193 *phys_addr = (u32) virt_to_phys((void *) virt_addr);
200 static void omap_vout_free_buffer(unsigned long virtaddr, u32 buf_size)
203 unsigned long addr = virtaddr;
205 size = PAGE_ALIGN(buf_size);
206 order = get_order(size);
209 ClearPageReserved(virt_to_page(addr));
213 free_pages((unsigned long) virtaddr, order);
217 * Function for allocating video buffers
219 static int omap_vout_allocate_vrfb_buffers(struct omap_vout_device *vout,
220 unsigned int *count, int startindex)
224 for (i = 0; i < *count; i++) {
225 if (!vout->smsshado_virt_addr[i]) {
226 vout->smsshado_virt_addr[i] =
227 omap_vout_alloc_buffer(vout->smsshado_size,
228 &vout->smsshado_phy_addr[i]);
230 if (!vout->smsshado_virt_addr[i] && startindex != -1) {
231 if (V4L2_MEMORY_MMAP == vout->memory && i >= startindex)
234 if (!vout->smsshado_virt_addr[i]) {
235 for (j = 0; j < i; j++) {
236 omap_vout_free_buffer(
237 vout->smsshado_virt_addr[j],
238 vout->smsshado_size);
239 vout->smsshado_virt_addr[j] = 0;
240 vout->smsshado_phy_addr[j] = 0;
245 memset((void *) vout->smsshado_virt_addr[i], 0,
246 vout->smsshado_size);
254 static int omap_vout_try_format(struct v4l2_pix_format *pix)
258 pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
259 (u32)VID_MAX_HEIGHT);
260 pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
262 for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
263 if (pix->pixelformat == omap_formats[ifmt].pixelformat)
267 if (ifmt == NUM_OUTPUT_FORMATS)
270 pix->pixelformat = omap_formats[ifmt].pixelformat;
271 pix->field = V4L2_FIELD_ANY;
274 switch (pix->pixelformat) {
275 case V4L2_PIX_FMT_YUYV:
276 case V4L2_PIX_FMT_UYVY:
278 pix->colorspace = V4L2_COLORSPACE_JPEG;
281 case V4L2_PIX_FMT_RGB565:
282 case V4L2_PIX_FMT_RGB565X:
283 pix->colorspace = V4L2_COLORSPACE_SRGB;
286 case V4L2_PIX_FMT_RGB24:
287 pix->colorspace = V4L2_COLORSPACE_SRGB;
290 case V4L2_PIX_FMT_RGB32:
291 case V4L2_PIX_FMT_BGR32:
292 pix->colorspace = V4L2_COLORSPACE_SRGB;
296 pix->bytesperline = pix->width * bpp;
297 pix->sizeimage = pix->bytesperline * pix->height;
303 * omap_vout_uservirt_to_phys: This inline function is used to convert user
304 * space virtual address to physical address.
306 static u32 omap_vout_uservirt_to_phys(u32 virtp)
308 unsigned long physp = 0;
309 struct vm_area_struct *vma;
310 struct mm_struct *mm = current->mm;
312 vma = find_vma(mm, virtp);
313 /* For kernel direct-mapped memory, take the easy way */
314 if (virtp >= PAGE_OFFSET) {
315 physp = virt_to_phys((void *) virtp);
316 } else if (vma && (vma->vm_flags & VM_IO) && vma->vm_pgoff) {
317 /* this will catch, kernel-allocated, mmaped-to-usermode
319 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
321 /* otherwise, use get_user_pages() for general userland pages */
322 int res, nr_pages = 1;
324 down_read(¤t->mm->mmap_sem);
326 res = get_user_pages(current, current->mm, virtp, nr_pages, 1,
328 up_read(¤t->mm->mmap_sem);
330 if (res == nr_pages) {
331 physp = __pa(page_address(&pages[0]) +
332 (virtp & ~PAGE_MASK));
334 printk(KERN_WARNING VOUT_NAME
335 "get_user_pages failed\n");
344 * Wakes up the application once the DMA transfer to VRFB space is completed.
346 static void omap_vout_vrfb_dma_tx_callback(int lch, u16 ch_status, void *data)
348 struct vid_vrfb_dma *t = (struct vid_vrfb_dma *) data;
351 wake_up_interruptible(&t->wait);
355 * Release the VRFB context once the module exits
357 static void omap_vout_release_vrfb(struct omap_vout_device *vout)
361 for (i = 0; i < VRFB_NUM_BUFS; i++)
362 omap_vrfb_release_ctx(&vout->vrfb_context[i]);
364 if (vout->vrfb_dma_tx.req_status == DMA_CHAN_ALLOTED) {
365 vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
366 omap_free_dma(vout->vrfb_dma_tx.dma_ch);
371 * Return true if rotation is 90 or 270
373 static inline int rotate_90_or_270(const struct omap_vout_device *vout)
375 return (vout->rotation == dss_rotation_90_degree ||
376 vout->rotation == dss_rotation_270_degree);
380 * Return true if rotation is enabled
382 static inline int rotation_enabled(const struct omap_vout_device *vout)
384 return vout->rotation || vout->mirror;
388 * Reverse the rotation degree if mirroring is enabled
390 static inline int calc_rotation(const struct omap_vout_device *vout)
393 return vout->rotation;
395 switch (vout->rotation) {
396 case dss_rotation_90_degree:
397 return dss_rotation_270_degree;
398 case dss_rotation_270_degree:
399 return dss_rotation_90_degree;
400 case dss_rotation_180_degree:
401 return dss_rotation_0_degree;
403 return dss_rotation_180_degree;
408 * Free the V4L2 buffers
410 static void omap_vout_free_buffers(struct omap_vout_device *vout)
414 /* Allocate memory for the buffers */
415 numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
416 vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
418 for (i = 0; i < numbuffers; i++) {
419 omap_vout_free_buffer(vout->buf_virt_addr[i],
421 vout->buf_phy_addr[i] = 0;
422 vout->buf_virt_addr[i] = 0;
429 static void omap_vout_free_vrfb_buffers(struct omap_vout_device *vout)
433 for (j = 0; j < VRFB_NUM_BUFS; j++) {
434 omap_vout_free_buffer(vout->smsshado_virt_addr[j],
435 vout->smsshado_size);
436 vout->smsshado_virt_addr[j] = 0;
437 vout->smsshado_phy_addr[j] = 0;
442 * Allocate the buffers for the VRFB space. Data is copied from V4L2
443 * buffers to the VRFB buffers using the DMA engine.
445 static int omap_vout_vrfb_buffer_setup(struct omap_vout_device *vout,
446 unsigned int *count, unsigned int startindex)
451 /* Allocate the VRFB buffers only if the buffers are not
452 * allocated during init time.
454 if ((rotation_enabled(vout)) && !vout->vrfb_static_allocation)
455 if (omap_vout_allocate_vrfb_buffers(vout, count, startindex))
458 if (vout->dss_mode == OMAP_DSS_COLOR_YUV2 ||
459 vout->dss_mode == OMAP_DSS_COLOR_UYVY)
464 for (i = 0; i < *count; i++)
465 omap_vrfb_setup(&vout->vrfb_context[i],
466 vout->smsshado_phy_addr[i], vout->pix.width,
467 vout->pix.height, vout->bpp, yuv_mode);
473 * Convert V4L2 rotation to DSS rotation
474 * V4L2 understand 0, 90, 180, 270.
475 * Convert to 0, 1, 2 and 3 repsectively for DSS
477 static int v4l2_rot_to_dss_rot(int v4l2_rotation,
478 enum dss_rotation *rotation, bool mirror)
482 switch (v4l2_rotation) {
484 *rotation = dss_rotation_90_degree;
487 *rotation = dss_rotation_180_degree;
490 *rotation = dss_rotation_270_degree;
493 *rotation = dss_rotation_0_degree;
502 * Calculate the buffer offsets from which the streaming should
503 * start. This offset calculation is mainly required because of
504 * the VRFB 32 pixels alignment with rotation.
506 static int omap_vout_calculate_offset(struct omap_vout_device *vout)
508 struct omap_overlay *ovl;
509 enum dss_rotation rotation;
510 struct omapvideo_info *ovid;
511 bool mirroring = vout->mirror;
512 struct omap_dss_device *cur_display;
513 struct v4l2_rect *crop = &vout->crop;
514 struct v4l2_pix_format *pix = &vout->pix;
515 int *cropped_offset = &vout->cropped_offset;
516 int vr_ps = 1, ps = 2, temp_ps = 2;
517 int offset = 0, ctop = 0, cleft = 0, line_length = 0;
519 ovid = &vout->vid_info;
520 ovl = ovid->overlays[0];
521 /* get the display device attached to the overlay */
522 if (!ovl->manager || !ovl->manager->device)
525 cur_display = ovl->manager->device;
526 rotation = calc_rotation(vout);
528 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
529 V4L2_PIX_FMT_UYVY == pix->pixelformat) {
530 if (rotation_enabled(vout)) {
532 * ps - Actual pixel size for YUYV/UYVY for
533 * VRFB/Mirroring is 4 bytes
534 * vr_ps - Virtually pixel size for YUYV/UYVY is
540 ps = 2; /* otherwise the pixel size is 2 byte */
542 } else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat) {
544 } else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat) {
550 if (rotation_enabled(vout)) {
551 line_length = MAX_PIXELS_PER_LINE;
552 ctop = (pix->height - crop->height) - crop->top;
553 cleft = (pix->width - crop->width) - crop->left;
555 line_length = pix->width;
557 vout->line_length = line_length;
559 case dss_rotation_90_degree:
560 offset = vout->vrfb_context[0].yoffset *
561 vout->vrfb_context[0].bytespp;
562 temp_ps = ps / vr_ps;
563 if (mirroring == 0) {
564 *cropped_offset = offset + line_length *
565 temp_ps * cleft + crop->top * temp_ps;
567 *cropped_offset = offset + line_length * temp_ps *
568 cleft + crop->top * temp_ps + (line_length *
569 ((crop->width / (vr_ps)) - 1) * ps);
572 case dss_rotation_180_degree:
573 offset = ((MAX_PIXELS_PER_LINE * vout->vrfb_context[0].yoffset *
574 vout->vrfb_context[0].bytespp) +
575 (vout->vrfb_context[0].xoffset *
576 vout->vrfb_context[0].bytespp));
577 if (mirroring == 0) {
578 *cropped_offset = offset + (line_length * ps * ctop) +
579 (cleft / vr_ps) * ps;
582 *cropped_offset = offset + (line_length * ps * ctop) +
583 (cleft / vr_ps) * ps + (line_length *
584 (crop->height - 1) * ps);
587 case dss_rotation_270_degree:
588 offset = MAX_PIXELS_PER_LINE * vout->vrfb_context[0].xoffset *
589 vout->vrfb_context[0].bytespp;
590 temp_ps = ps / vr_ps;
591 if (mirroring == 0) {
592 *cropped_offset = offset + line_length *
593 temp_ps * crop->left + ctop * ps;
595 *cropped_offset = offset + line_length *
596 temp_ps * crop->left + ctop * ps +
597 (line_length * ((crop->width / vr_ps) - 1) *
601 case dss_rotation_0_degree:
602 if (mirroring == 0) {
603 *cropped_offset = (line_length * ps) *
604 crop->top + (crop->left / vr_ps) * ps;
606 *cropped_offset = (line_length * ps) *
607 crop->top + (crop->left / vr_ps) * ps +
608 (line_length * (crop->height - 1) * ps);
612 *cropped_offset = (line_length * ps * crop->top) /
613 vr_ps + (crop->left * ps) / vr_ps +
614 ((crop->width / vr_ps) - 1) * ps;
617 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
618 __func__, *cropped_offset);
623 * Convert V4L2 pixel format to DSS pixel format
625 static int video_mode_to_dss_mode(struct omap_vout_device *vout)
627 struct omap_overlay *ovl;
628 struct omapvideo_info *ovid;
629 struct v4l2_pix_format *pix = &vout->pix;
630 enum omap_color_mode mode;
632 ovid = &vout->vid_info;
633 ovl = ovid->overlays[0];
635 switch (pix->pixelformat) {
638 case V4L2_PIX_FMT_YUYV:
639 mode = OMAP_DSS_COLOR_YUV2;
641 case V4L2_PIX_FMT_UYVY:
642 mode = OMAP_DSS_COLOR_UYVY;
644 case V4L2_PIX_FMT_RGB565:
645 mode = OMAP_DSS_COLOR_RGB16;
647 case V4L2_PIX_FMT_RGB24:
648 mode = OMAP_DSS_COLOR_RGB24P;
650 case V4L2_PIX_FMT_RGB32:
651 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
652 OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
654 case V4L2_PIX_FMT_BGR32:
655 mode = OMAP_DSS_COLOR_RGBX32;
666 int omapvid_setup_overlay(struct omap_vout_device *vout,
667 struct omap_overlay *ovl, int posx, int posy, int outw,
671 struct omap_overlay_info info;
672 int cropheight, cropwidth, pixheight, pixwidth;
674 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
675 (outw != vout->pix.width || outh != vout->pix.height)) {
680 vout->dss_mode = video_mode_to_dss_mode(vout);
681 if (vout->dss_mode == -EINVAL) {
686 /* Setup the input plane parameters according to
687 * rotation value selected.
689 if (rotate_90_or_270(vout)) {
690 cropheight = vout->crop.width;
691 cropwidth = vout->crop.height;
692 pixheight = vout->pix.width;
693 pixwidth = vout->pix.height;
695 cropheight = vout->crop.height;
696 cropwidth = vout->crop.width;
697 pixheight = vout->pix.height;
698 pixwidth = vout->pix.width;
701 ovl->get_overlay_info(ovl, &info);
704 info.width = cropwidth;
705 info.height = cropheight;
706 info.color_mode = vout->dss_mode;
707 info.mirror = vout->mirror;
710 info.out_width = outw;
711 info.out_height = outh;
712 info.global_alpha = vout->win.global_alpha;
713 if (!rotation_enabled(vout)) {
715 info.rotation_type = OMAP_DSS_ROT_DMA;
716 info.screen_width = pixwidth;
718 info.rotation = vout->rotation;
719 info.rotation_type = OMAP_DSS_ROT_VRFB;
720 info.screen_width = 2048;
723 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
724 "%s enable=%d addr=%x width=%d\n height=%d color_mode=%d\n"
725 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
726 "out_height=%d rotation_type=%d screen_width=%d\n",
727 __func__, info.enabled, info.paddr, info.width, info.height,
728 info.color_mode, info.rotation, info.mirror, info.pos_x,
729 info.pos_y, info.out_width, info.out_height, info.rotation_type,
732 ret = ovl->set_overlay_info(ovl, &info);
739 v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
744 * Initialize the overlay structure
746 int omapvid_init(struct omap_vout_device *vout, u32 addr)
749 struct v4l2_window *win;
750 struct omap_overlay *ovl;
751 int posx, posy, outw, outh, temp;
752 struct omap_video_timings *timing;
753 struct omapvideo_info *ovid = &vout->vid_info;
756 for (i = 0; i < ovid->num_overlays; i++) {
757 ovl = ovid->overlays[i];
758 if (!ovl->manager || !ovl->manager->device)
761 timing = &ovl->manager->device->panel.timings;
764 outh = win->w.height;
765 switch (vout->rotation) {
766 case dss_rotation_90_degree:
767 /* Invert the height and width for 90
768 * and 270 degree rotation
773 posy = (timing->y_res - win->w.width) - win->w.left;
777 case dss_rotation_180_degree:
778 posx = (timing->x_res - win->w.width) - win->w.left;
779 posy = (timing->y_res - win->w.height) - win->w.top;
782 case dss_rotation_270_degree:
787 posx = (timing->x_res - win->w.height) - win->w.top;
796 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
799 goto omapvid_init_err;
804 v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
809 * Apply the changes set the go bit of DSS
811 int omapvid_apply_changes(struct omap_vout_device *vout)
814 struct omap_overlay *ovl;
815 struct omapvideo_info *ovid = &vout->vid_info;
817 for (i = 0; i < ovid->num_overlays; i++) {
818 ovl = ovid->overlays[i];
819 if (!ovl->manager || !ovl->manager->device)
821 ovl->manager->apply(ovl->manager);
827 void omap_vout_isr(void *arg, unsigned int irqstatus)
831 struct omap_overlay *ovl;
832 struct timeval timevalue;
833 struct omapvideo_info *ovid;
834 struct omap_dss_device *cur_display;
835 struct omap_vout_device *vout = (struct omap_vout_device *)arg;
837 if (!vout->streaming)
840 ovid = &vout->vid_info;
841 ovl = ovid->overlays[0];
842 /* get the display device attached to the overlay */
843 if (!ovl->manager || !ovl->manager->device)
846 cur_display = ovl->manager->device;
848 spin_lock(&vout->vbq_lock);
849 do_gettimeofday(&timevalue);
850 if (cur_display->type == OMAP_DISPLAY_TYPE_DPI) {
851 if (!(irqstatus & DISPC_IRQ_VSYNC))
854 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
855 vout->cur_frm->ts = timevalue;
856 vout->cur_frm->state = VIDEOBUF_DONE;
857 wake_up_interruptible(&vout->cur_frm->done);
858 vout->cur_frm = vout->next_frm;
861 if (list_empty(&vout->dma_queue))
864 vout->next_frm = list_entry(vout->dma_queue.next,
865 struct videobuf_buffer, queue);
866 list_del(&vout->next_frm->queue);
868 vout->next_frm->state = VIDEOBUF_ACTIVE;
870 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
871 + vout->cropped_offset;
873 /* First save the configuration in ovelray structure */
874 ret = omapvid_init(vout, addr);
876 printk(KERN_ERR VOUT_NAME
877 "failed to set overlay info\n");
878 /* Enable the pipeline and set the Go bit */
879 ret = omapvid_apply_changes(vout);
881 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
884 if (vout->first_int) {
888 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
890 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
896 if (fid != vout->field_id) {
898 vout->field_id = fid;
903 if (vout->cur_frm == vout->next_frm)
906 vout->cur_frm->ts = timevalue;
907 vout->cur_frm->state = VIDEOBUF_DONE;
908 wake_up_interruptible(&vout->cur_frm->done);
909 vout->cur_frm = vout->next_frm;
910 } else if (1 == fid) {
911 if (list_empty(&vout->dma_queue) ||
912 (vout->cur_frm != vout->next_frm))
915 vout->next_frm = list_entry(vout->dma_queue.next,
916 struct videobuf_buffer, queue);
917 list_del(&vout->next_frm->queue);
919 vout->next_frm->state = VIDEOBUF_ACTIVE;
920 addr = (unsigned long)
921 vout->queued_buf_addr[vout->next_frm->i] +
922 vout->cropped_offset;
923 /* First save the configuration in ovelray structure */
924 ret = omapvid_init(vout, addr);
926 printk(KERN_ERR VOUT_NAME
927 "failed to set overlay info\n");
928 /* Enable the pipeline and set the Go bit */
929 ret = omapvid_apply_changes(vout);
931 printk(KERN_ERR VOUT_NAME
932 "failed to change mode\n");
938 spin_unlock(&vout->vbq_lock);
942 /* Video buffer call backs */
945 * Buffer setup function is called by videobuf layer when REQBUF ioctl is
946 * called. This is used to setup buffers and return size and count of
947 * buffers allocated. After the call to this buffer, videobuf layer will
948 * setup buffer queue depending on the size and count of buffers
950 static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
953 int startindex = 0, i, j;
954 u32 phy_addr = 0, virt_addr = 0;
955 struct omap_vout_device *vout = q->priv_data;
960 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
963 startindex = (vout->vid == OMAP_VIDEO1) ?
964 video1_numbuffers : video2_numbuffers;
965 if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
968 if ((rotation_enabled(vout)) && *count > VRFB_NUM_BUFS)
969 *count = VRFB_NUM_BUFS;
971 /* If rotation is enabled, allocate memory for VRFB space also */
972 if (rotation_enabled(vout))
973 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
976 if (V4L2_MEMORY_MMAP != vout->memory)
979 /* Now allocated the V4L2 buffers */
980 *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
981 startindex = (vout->vid == OMAP_VIDEO1) ?
982 video1_numbuffers : video2_numbuffers;
984 for (i = startindex; i < *count; i++) {
985 vout->buffer_size = *size;
987 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
990 if (!rotation_enabled(vout))
992 /* Free the VRFB buffers if no space for V4L2 buffers */
993 for (j = i; j < *count; j++) {
994 omap_vout_free_buffer(
995 vout->smsshado_virt_addr[j],
996 vout->smsshado_size);
997 vout->smsshado_virt_addr[j] = 0;
998 vout->smsshado_phy_addr[j] = 0;
1001 vout->buf_virt_addr[i] = virt_addr;
1002 vout->buf_phy_addr[i] = phy_addr;
1004 *count = vout->buffer_allocated = i;
1010 * Free the V4L2 buffers additionally allocated than default
1011 * number of buffers and free all the VRFB buffers
1013 static void omap_vout_free_allbuffers(struct omap_vout_device *vout)
1015 int num_buffers = 0, i;
1017 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1018 video1_numbuffers : video2_numbuffers;
1020 for (i = num_buffers; i < vout->buffer_allocated; i++) {
1021 if (vout->buf_virt_addr[i])
1022 omap_vout_free_buffer(vout->buf_virt_addr[i],
1025 vout->buf_virt_addr[i] = 0;
1026 vout->buf_phy_addr[i] = 0;
1028 /* Free the VRFB buffers only if they are allocated
1029 * during reqbufs. Don't free if init time allocated
1031 if (!vout->vrfb_static_allocation) {
1032 for (i = 0; i < VRFB_NUM_BUFS; i++) {
1033 if (vout->smsshado_virt_addr[i]) {
1034 omap_vout_free_buffer(
1035 vout->smsshado_virt_addr[i],
1036 vout->smsshado_size);
1037 vout->smsshado_virt_addr[i] = 0;
1038 vout->smsshado_phy_addr[i] = 0;
1042 vout->buffer_allocated = num_buffers;
1046 * This function will be called when VIDIOC_QBUF ioctl is called.
1047 * It prepare buffers before give out for the display. This function
1048 * converts user space virtual address into physical address if userptr memory
1049 * exchange mechanism is used. If rotation is enabled, it copies entire
1050 * buffer into VRFB memory space before giving it to the DSS.
1052 static int omap_vout_buffer_prepare(struct videobuf_queue *q,
1053 struct videobuf_buffer *vb,
1054 enum v4l2_field field)
1056 struct vid_vrfb_dma *tx;
1057 enum dss_rotation rotation;
1058 struct videobuf_dmabuf *dmabuf = NULL;
1059 struct omap_vout_device *vout = q->priv_data;
1060 u32 dest_frame_index = 0, src_element_index = 0;
1061 u32 dest_element_index = 0, src_frame_index = 0;
1062 u32 elem_count = 0, frame_count = 0, pixsize = 2;
1064 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1065 vb->width = vout->pix.width;
1066 vb->height = vout->pix.height;
1067 vb->size = vb->width * vb->height * vout->bpp;
1070 vb->state = VIDEOBUF_PREPARED;
1071 /* if user pointer memory mechanism is used, get the physical
1072 * address of the buffer
1074 if (V4L2_MEMORY_USERPTR == vb->memory) {
1077 /* Virtual address */
1078 /* priv points to struct videobuf_pci_sg_memory. But we went
1079 * pointer to videobuf_dmabuf, which is member of
1080 * videobuf_pci_sg_memory */
1081 dmabuf = videobuf_to_dma(q->bufs[vb->i]);
1082 dmabuf->vmalloc = (void *) vb->baddr;
1084 /* Physical address */
1086 (dma_addr_t) omap_vout_uservirt_to_phys(vb->baddr);
1089 if (!rotation_enabled(vout)) {
1090 dmabuf = videobuf_to_dma(q->bufs[vb->i]);
1091 vout->queued_buf_addr[vb->i] = (u8 *) dmabuf->bus_addr;
1094 dmabuf = videobuf_to_dma(q->bufs[vb->i]);
1095 /* If rotation is enabled, copy input buffer into VRFB
1096 * memory space using DMA. We are copying input buffer
1097 * into VRFB memory space of desired angle and DSS will
1098 * read image VRFB memory for 0 degree angle
1100 pixsize = vout->bpp * vout->vrfb_bpp;
1102 * DMA transfer in double index mode
1106 dest_frame_index = ((MAX_PIXELS_PER_LINE * pixsize) -
1107 (vout->pix.width * vout->bpp)) + 1;
1109 /* Source and destination parameters */
1110 src_element_index = 0;
1111 src_frame_index = 0;
1112 dest_element_index = 1;
1113 /* Number of elements per frame */
1114 elem_count = vout->pix.width * vout->bpp;
1115 frame_count = vout->pix.height;
1116 tx = &vout->vrfb_dma_tx;
1118 omap_set_dma_transfer_params(tx->dma_ch, OMAP_DMA_DATA_TYPE_S32,
1119 (elem_count / 4), frame_count, OMAP_DMA_SYNC_ELEMENT,
1121 /* src_port required only for OMAP1 */
1122 omap_set_dma_src_params(tx->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1123 dmabuf->bus_addr, src_element_index, src_frame_index);
1124 /*set dma source burst mode for VRFB */
1125 omap_set_dma_src_burst_mode(tx->dma_ch, OMAP_DMA_DATA_BURST_16);
1126 rotation = calc_rotation(vout);
1128 /* dest_port required only for OMAP1 */
1129 omap_set_dma_dest_params(tx->dma_ch, 0, OMAP_DMA_AMODE_DOUBLE_IDX,
1130 vout->vrfb_context[vb->i].paddr[0], dest_element_index,
1132 /*set dma dest burst mode for VRFB */
1133 omap_set_dma_dest_burst_mode(tx->dma_ch, OMAP_DMA_DATA_BURST_16);
1134 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE, 0x20, 0);
1136 omap_start_dma(tx->dma_ch);
1137 interruptible_sleep_on_timeout(&tx->wait, VRFB_TX_TIMEOUT);
1139 if (tx->tx_status == 0) {
1140 omap_stop_dma(tx->dma_ch);
1143 /* Store buffers physical address into an array. Addresses
1144 * from this array will be used to configure DSS */
1145 vout->queued_buf_addr[vb->i] = (u8 *)
1146 vout->vrfb_context[vb->i].paddr[rotation];
1151 * Buffer queue funtion will be called from the videobuf layer when _QBUF
1152 * ioctl is called. It is used to enqueue buffer, which is ready to be
1155 static void omap_vout_buffer_queue(struct videobuf_queue *q,
1156 struct videobuf_buffer *vb)
1158 struct omap_vout_device *vout = q->priv_data;
1160 /* Driver is also maintainig a queue. So enqueue buffer in the driver
1162 list_add_tail(&vb->queue, &vout->dma_queue);
1164 vb->state = VIDEOBUF_QUEUED;
1168 * Buffer release function is called from videobuf layer to release buffer
1169 * which are already allocated
1171 static void omap_vout_buffer_release(struct videobuf_queue *q,
1172 struct videobuf_buffer *vb)
1174 struct omap_vout_device *vout = q->priv_data;
1176 vb->state = VIDEOBUF_NEEDS_INIT;
1178 if (V4L2_MEMORY_MMAP != vout->memory)
1185 static void omap_vout_vm_open(struct vm_area_struct *vma)
1187 struct omap_vout_device *vout = vma->vm_private_data;
1189 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
1190 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
1194 static void omap_vout_vm_close(struct vm_area_struct *vma)
1196 struct omap_vout_device *vout = vma->vm_private_data;
1198 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
1199 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
1203 static struct vm_operations_struct omap_vout_vm_ops = {
1204 .open = omap_vout_vm_open,
1205 .close = omap_vout_vm_close,
1208 static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
1212 unsigned long start = vma->vm_start;
1213 unsigned long size = (vma->vm_end - vma->vm_start);
1214 struct videobuf_dmabuf *dmabuf = NULL;
1215 struct omap_vout_device *vout = file->private_data;
1216 struct videobuf_queue *q = &vout->vbq;
1218 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
1219 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
1220 vma->vm_pgoff, vma->vm_start, vma->vm_end);
1222 /* look for the buffer to map */
1223 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1224 if (NULL == q->bufs[i])
1226 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
1228 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1232 if (VIDEO_MAX_FRAME == i) {
1233 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
1234 "offset invalid [offset=0x%lx]\n",
1235 (vma->vm_pgoff << PAGE_SHIFT));
1238 q->bufs[i]->baddr = vma->vm_start;
1240 vma->vm_flags |= VM_RESERVED;
1241 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1242 vma->vm_ops = &omap_vout_vm_ops;
1243 vma->vm_private_data = (void *) vout;
1244 dmabuf = videobuf_to_dma(q->bufs[i]);
1245 pos = dmabuf->vmalloc;
1246 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
1249 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
1250 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
1257 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1262 static int omap_vout_release(struct file *file)
1264 unsigned int ret, i;
1265 struct videobuf_queue *q;
1266 struct omapvideo_info *ovid;
1267 struct omap_vout_device *vout = file->private_data;
1269 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1270 ovid = &vout->vid_info;
1276 /* Disable all the overlay managers connected with this interface */
1277 for (i = 0; i < ovid->num_overlays; i++) {
1278 struct omap_overlay *ovl = ovid->overlays[i];
1279 if (ovl->manager && ovl->manager->device) {
1280 struct omap_overlay_info info;
1281 ovl->get_overlay_info(ovl, &info);
1283 ovl->set_overlay_info(ovl, &info);
1286 /* Turn off the pipeline */
1287 ret = omapvid_apply_changes(vout);
1289 v4l2_warn(&vout->vid_dev->v4l2_dev,
1290 "Unable to apply changes\n");
1292 /* Free all buffers */
1293 omap_vout_free_allbuffers(vout);
1294 videobuf_mmap_free(q);
1296 /* Even if apply changes fails we should continue
1297 freeing allocated memeory */
1298 if (vout->streaming) {
1301 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
1302 DISPC_IRQ_EVSYNC_ODD;
1303 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1304 vout->streaming = 0;
1306 videobuf_streamoff(q);
1307 videobuf_queue_cancel(q);
1310 if (vout->mmap_count != 0)
1311 vout->mmap_count = 0;
1314 file->private_data = NULL;
1316 if (vout->buffer_allocated)
1317 videobuf_mmap_free(q);
1319 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1323 static int omap_vout_open(struct file *file)
1325 struct videobuf_queue *q;
1326 struct omap_vout_device *vout = NULL;
1328 vout = video_drvdata(file);
1329 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1334 /* for now, we only support single open */
1340 file->private_data = vout;
1341 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1344 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1345 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1346 video_vbq_ops.buf_release = omap_vout_buffer_release;
1347 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1348 spin_lock_init(&vout->vbq_lock);
1350 videobuf_queue_sg_init(q, &video_vbq_ops, NULL, &vout->vbq_lock,
1351 vout->type, V4L2_FIELD_NONE,
1352 sizeof(struct videobuf_buffer), vout);
1354 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1361 static int vidioc_querycap(struct file *file, void *fh,
1362 struct v4l2_capability *cap)
1364 struct omap_vout_device *vout = fh;
1366 strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1367 strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1368 cap->bus_info[0] = '\0';
1369 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
1374 static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1375 struct v4l2_fmtdesc *fmt)
1377 int index = fmt->index;
1378 enum v4l2_buf_type type = fmt->type;
1382 if (index >= NUM_OUTPUT_FORMATS)
1385 fmt->flags = omap_formats[index].flags;
1386 strlcpy(fmt->description, omap_formats[index].description,
1387 sizeof(fmt->description));
1388 fmt->pixelformat = omap_formats[index].pixelformat;
1393 static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1394 struct v4l2_format *f)
1396 struct omap_vout_device *vout = fh;
1398 f->fmt.pix = vout->pix;
1403 static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1404 struct v4l2_format *f)
1406 struct omap_overlay *ovl;
1407 struct omapvideo_info *ovid;
1408 struct omap_video_timings *timing;
1409 struct omap_vout_device *vout = fh;
1411 ovid = &vout->vid_info;
1412 ovl = ovid->overlays[0];
1414 if (!ovl->manager || !ovl->manager->device)
1416 /* get the display device attached to the overlay */
1417 timing = &ovl->manager->device->panel.timings;
1419 vout->fbuf.fmt.height = timing->y_res;
1420 vout->fbuf.fmt.width = timing->x_res;
1422 omap_vout_try_format(&f->fmt.pix);
1426 static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1427 struct v4l2_format *f)
1430 struct omap_overlay *ovl;
1431 struct omapvideo_info *ovid;
1432 struct omap_video_timings *timing;
1433 struct omap_vout_device *vout = fh;
1435 if (vout->streaming)
1438 mutex_lock(&vout->lock);
1440 ovid = &vout->vid_info;
1441 ovl = ovid->overlays[0];
1443 /* get the display device attached to the overlay */
1444 if (!ovl->manager || !ovl->manager->device) {
1446 goto s_fmt_vid_out_exit;
1448 timing = &ovl->manager->device->panel.timings;
1450 /* We dont support RGB24-packed mode if vrfb rotation
1452 if ((rotation_enabled(vout)) &&
1453 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1455 goto s_fmt_vid_out_exit;
1458 /* get the framebuffer parameters */
1460 if (rotate_90_or_270(vout)) {
1461 vout->fbuf.fmt.height = timing->x_res;
1462 vout->fbuf.fmt.width = timing->y_res;
1464 vout->fbuf.fmt.height = timing->y_res;
1465 vout->fbuf.fmt.width = timing->x_res;
1468 /* change to samller size is OK */
1470 bpp = omap_vout_try_format(&f->fmt.pix);
1471 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1473 /* try & set the new output format */
1475 vout->pix = f->fmt.pix;
1478 /* If YUYV then vrfb bpp is 2, for others its 1 */
1479 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1480 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1483 /* set default crop and win */
1484 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1486 /* Save the changes in the overlay strcuture */
1487 ret = omapvid_init(vout, 0);
1489 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1490 goto s_fmt_vid_out_exit;
1496 mutex_unlock(&vout->lock);
1500 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1501 struct v4l2_format *f)
1504 struct omap_vout_device *vout = fh;
1505 struct v4l2_window *win = &f->fmt.win;
1507 ret = omap_vout_try_window(&vout->fbuf, win);
1510 if (vout->vid == OMAP_VIDEO1)
1511 win->global_alpha = 255;
1513 win->global_alpha = f->fmt.win.global_alpha;
1519 static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1520 struct v4l2_format *f)
1523 struct omap_overlay *ovl;
1524 struct omapvideo_info *ovid;
1525 struct omap_vout_device *vout = fh;
1526 struct v4l2_window *win = &f->fmt.win;
1528 mutex_lock(&vout->lock);
1529 ovid = &vout->vid_info;
1530 ovl = ovid->overlays[0];
1532 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1534 /* Video1 plane does not support global alpha */
1535 if (ovl->id == OMAP_DSS_VIDEO1)
1536 vout->win.global_alpha = 255;
1538 vout->win.global_alpha = f->fmt.win.global_alpha;
1540 vout->win.chromakey = f->fmt.win.chromakey;
1542 mutex_unlock(&vout->lock);
1546 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
1547 struct v4l2_fmtdesc *fmt)
1549 int index = fmt->index;
1550 enum v4l2_buf_type type = fmt->type;
1554 if (index >= NUM_OUTPUT_FORMATS)
1557 fmt->flags = omap_formats[index].flags;
1558 strlcpy(fmt->description, omap_formats[index].description,
1559 sizeof(fmt->description));
1560 fmt->pixelformat = omap_formats[index].pixelformat;
1564 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1565 struct v4l2_format *f)
1568 struct omap_overlay *ovl;
1569 struct omapvideo_info *ovid;
1570 struct omap_vout_device *vout = fh;
1571 struct omap_overlay_manager_info info;
1572 struct v4l2_window *win = &f->fmt.win;
1574 ovid = &vout->vid_info;
1575 ovl = ovid->overlays[0];
1577 win->w = vout->win.w;
1578 win->field = vout->win.field;
1579 win->global_alpha = vout->win.global_alpha;
1581 if (ovl->manager && ovl->manager->get_manager_info) {
1582 ovl->manager->get_manager_info(ovl->manager, &info);
1583 key_value = info.trans_key;
1585 win->chromakey = key_value;
1589 static int vidioc_cropcap(struct file *file, void *fh,
1590 struct v4l2_cropcap *cropcap)
1592 struct omap_vout_device *vout = fh;
1593 struct v4l2_pix_format *pix = &vout->pix;
1595 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1598 /* Width and height are always even */
1599 cropcap->bounds.width = pix->width & ~1;
1600 cropcap->bounds.height = pix->height & ~1;
1602 omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1603 cropcap->pixelaspect.numerator = 1;
1604 cropcap->pixelaspect.denominator = 1;
1608 static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1610 struct omap_vout_device *vout = fh;
1612 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1614 crop->c = vout->crop;
1618 static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1621 struct omap_vout_device *vout = fh;
1622 struct omapvideo_info *ovid;
1623 struct omap_overlay *ovl;
1624 struct omap_video_timings *timing;
1626 if (vout->streaming)
1629 mutex_lock(&vout->lock);
1630 ovid = &vout->vid_info;
1631 ovl = ovid->overlays[0];
1633 if (!ovl->manager || !ovl->manager->device) {
1637 /* get the display device attached to the overlay */
1638 timing = &ovl->manager->device->panel.timings;
1640 if (rotate_90_or_270(vout)) {
1641 vout->fbuf.fmt.height = timing->x_res;
1642 vout->fbuf.fmt.width = timing->y_res;
1644 vout->fbuf.fmt.height = timing->y_res;
1645 vout->fbuf.fmt.width = timing->x_res;
1648 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1649 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1650 &vout->fbuf, &crop->c);
1653 mutex_unlock(&vout->lock);
1657 static int vidioc_queryctrl(struct file *file, void *fh,
1658 struct v4l2_queryctrl *ctrl)
1663 case V4L2_CID_ROTATE:
1664 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1666 case V4L2_CID_BG_COLOR:
1667 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1669 case V4L2_CID_VFLIP:
1670 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1673 ctrl->name[0] = '\0';
1679 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1682 struct omap_vout_device *vout = fh;
1685 case V4L2_CID_ROTATE:
1686 ctrl->value = vout->control[0].value;
1688 case V4L2_CID_BG_COLOR:
1690 struct omap_overlay_manager_info info;
1691 struct omap_overlay *ovl;
1693 ovl = vout->vid_info.overlays[0];
1694 if (!ovl->manager || !ovl->manager->get_manager_info) {
1699 ovl->manager->get_manager_info(ovl->manager, &info);
1700 ctrl->value = info.default_color;
1703 case V4L2_CID_VFLIP:
1704 ctrl->value = vout->control[2].value;
1712 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1715 struct omap_vout_device *vout = fh;
1718 case V4L2_CID_ROTATE:
1720 int rotation = a->value;
1722 mutex_lock(&vout->lock);
1724 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1725 mutex_unlock(&vout->lock);
1730 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1732 mutex_unlock(&vout->lock);
1737 vout->control[0].value = rotation;
1738 mutex_unlock(&vout->lock);
1741 case V4L2_CID_BG_COLOR:
1743 struct omap_overlay *ovl;
1744 unsigned int color = a->value;
1745 struct omap_overlay_manager_info info;
1747 ovl = vout->vid_info.overlays[0];
1749 mutex_lock(&vout->lock);
1750 if (!ovl->manager || !ovl->manager->get_manager_info) {
1751 mutex_unlock(&vout->lock);
1756 ovl->manager->get_manager_info(ovl->manager, &info);
1757 info.default_color = color;
1758 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1759 mutex_unlock(&vout->lock);
1764 vout->control[1].value = color;
1765 mutex_unlock(&vout->lock);
1768 case V4L2_CID_VFLIP:
1770 struct omap_overlay *ovl;
1771 struct omapvideo_info *ovid;
1772 unsigned int mirror = a->value;
1774 ovid = &vout->vid_info;
1775 ovl = ovid->overlays[0];
1777 mutex_lock(&vout->lock);
1779 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1780 mutex_unlock(&vout->lock);
1784 vout->mirror = mirror;
1785 vout->control[2].value = mirror;
1786 mutex_unlock(&vout->lock);
1795 static int vidioc_reqbufs(struct file *file, void *fh,
1796 struct v4l2_requestbuffers *req)
1799 unsigned int i, num_buffers = 0;
1800 struct omap_vout_device *vout = fh;
1801 struct videobuf_queue *q = &vout->vbq;
1802 struct videobuf_dmabuf *dmabuf = NULL;
1804 if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
1806 /* if memory is not mmp or userptr
1808 if ((V4L2_MEMORY_MMAP != req->memory) &&
1809 (V4L2_MEMORY_USERPTR != req->memory))
1812 mutex_lock(&vout->lock);
1813 /* Cannot be requested when streaming is on */
1814 if (vout->streaming) {
1819 /* If buffers are already allocated free them */
1820 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1821 if (vout->mmap_count) {
1825 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1826 video1_numbuffers : video2_numbuffers;
1827 for (i = num_buffers; i < vout->buffer_allocated; i++) {
1828 dmabuf = videobuf_to_dma(q->bufs[i]);
1829 omap_vout_free_buffer((u32)dmabuf->vmalloc,
1831 vout->buf_virt_addr[i] = 0;
1832 vout->buf_phy_addr[i] = 0;
1834 vout->buffer_allocated = num_buffers;
1835 videobuf_mmap_free(q);
1836 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1837 if (vout->buffer_allocated) {
1838 videobuf_mmap_free(q);
1839 for (i = 0; i < vout->buffer_allocated; i++) {
1843 vout->buffer_allocated = 0;
1847 /*store the memory type in data structure */
1848 vout->memory = req->memory;
1850 INIT_LIST_HEAD(&vout->dma_queue);
1852 /* call videobuf_reqbufs api */
1853 ret = videobuf_reqbufs(q, req);
1857 vout->buffer_allocated = req->count;
1858 for (i = 0; i < req->count; i++) {
1859 dmabuf = videobuf_to_dma(q->bufs[i]);
1860 dmabuf->vmalloc = (void *) vout->buf_virt_addr[i];
1861 dmabuf->bus_addr = (dma_addr_t) vout->buf_phy_addr[i];
1865 mutex_unlock(&vout->lock);
1869 static int vidioc_querybuf(struct file *file, void *fh,
1870 struct v4l2_buffer *b)
1872 struct omap_vout_device *vout = fh;
1874 return videobuf_querybuf(&vout->vbq, b);
1877 static int vidioc_qbuf(struct file *file, void *fh,
1878 struct v4l2_buffer *buffer)
1880 struct omap_vout_device *vout = fh;
1881 struct videobuf_queue *q = &vout->vbq;
1883 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1884 (buffer->index >= vout->buffer_allocated) ||
1885 (q->bufs[buffer->index]->memory != buffer->memory)) {
1888 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1889 if ((buffer->length < vout->pix.sizeimage) ||
1890 (0 == buffer->m.userptr)) {
1895 if ((rotation_enabled(vout)) &&
1896 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1897 v4l2_warn(&vout->vid_dev->v4l2_dev,
1898 "DMA Channel not allocated for Rotation\n");
1902 return videobuf_qbuf(q, buffer);
1905 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1907 struct omap_vout_device *vout = fh;
1908 struct videobuf_queue *q = &vout->vbq;
1910 if (!vout->streaming)
1913 if (file->f_flags & O_NONBLOCK)
1914 /* Call videobuf_dqbuf for non blocking mode */
1915 return videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
1917 /* Call videobuf_dqbuf for blocking mode */
1918 return videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
1921 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1924 u32 addr = 0, mask = 0;
1925 struct omap_vout_device *vout = fh;
1926 struct videobuf_queue *q = &vout->vbq;
1927 struct omapvideo_info *ovid = &vout->vid_info;
1929 mutex_lock(&vout->lock);
1931 if (vout->streaming) {
1936 ret = videobuf_streamon(q);
1940 if (list_empty(&vout->dma_queue)) {
1945 /* Get the next frame from the buffer queue */
1946 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1947 struct videobuf_buffer, queue);
1948 /* Remove buffer from the buffer queue */
1949 list_del(&vout->cur_frm->queue);
1950 /* Mark state of the current frame to active */
1951 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1952 /* Initialize field_id and started member */
1955 /* set flag here. Next QBUF will start DMA */
1956 vout->streaming = 1;
1958 vout->first_int = 1;
1960 if (omap_vout_calculate_offset(vout)) {
1964 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1965 + vout->cropped_offset;
1967 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD;
1969 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1971 for (j = 0; j < ovid->num_overlays; j++) {
1972 struct omap_overlay *ovl = ovid->overlays[j];
1974 if (ovl->manager && ovl->manager->device) {
1975 struct omap_overlay_info info;
1976 ovl->get_overlay_info(ovl, &info);
1979 if (ovl->set_overlay_info(ovl, &info)) {
1986 /* First save the configuration in ovelray structure */
1987 ret = omapvid_init(vout, addr);
1989 v4l2_err(&vout->vid_dev->v4l2_dev,
1990 "failed to set overlay info\n");
1991 /* Enable the pipeline and set the Go bit */
1992 ret = omapvid_apply_changes(vout);
1994 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
2000 ret = videobuf_streamoff(q);
2002 mutex_unlock(&vout->lock);
2006 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
2010 struct omap_vout_device *vout = fh;
2011 struct omapvideo_info *ovid = &vout->vid_info;
2013 if (!vout->streaming)
2016 vout->streaming = 0;
2017 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD;
2019 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
2021 for (j = 0; j < ovid->num_overlays; j++) {
2022 struct omap_overlay *ovl = ovid->overlays[j];
2024 if (ovl->manager && ovl->manager->device) {
2025 struct omap_overlay_info info;
2027 ovl->get_overlay_info(ovl, &info);
2029 ret = ovl->set_overlay_info(ovl, &info);
2031 v4l2_err(&vout->vid_dev->v4l2_dev,
2032 "failed to update overlay info in streamoff\n");
2036 /* Turn of the pipeline */
2037 ret = omapvid_apply_changes(vout);
2039 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
2042 INIT_LIST_HEAD(&vout->dma_queue);
2043 ret = videobuf_streamoff(&vout->vbq);
2048 static int vidioc_s_fbuf(struct file *file, void *fh,
2049 struct v4l2_framebuffer *a)
2052 struct omap_overlay *ovl;
2053 struct omapvideo_info *ovid;
2054 struct omap_vout_device *vout = fh;
2055 struct omap_overlay_manager_info info;
2056 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
2058 ovid = &vout->vid_info;
2059 ovl = ovid->overlays[0];
2061 /* OMAP DSS doesn't support Source and Destination color
2063 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
2064 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
2066 /* OMAP DSS Doesn't support the Destination color key
2067 and alpha blending together */
2068 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
2069 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
2072 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
2073 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
2074 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
2076 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
2078 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
2079 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
2080 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
2082 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
2084 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
2085 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
2089 if (ovl->manager && ovl->manager->get_manager_info &&
2090 ovl->manager->set_manager_info) {
2092 ovl->manager->get_manager_info(ovl->manager, &info);
2093 info.trans_enabled = enable;
2094 info.trans_key_type = key_type;
2095 info.trans_key = vout->win.chromakey;
2097 if (ovl->manager->set_manager_info(ovl->manager, &info))
2100 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
2101 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
2104 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
2107 if (ovl->manager && ovl->manager->get_manager_info &&
2108 ovl->manager->set_manager_info) {
2109 ovl->manager->get_manager_info(ovl->manager, &info);
2110 info.alpha_enabled = enable;
2111 if (ovl->manager->set_manager_info(ovl->manager, &info))
2118 static int vidioc_g_fbuf(struct file *file, void *fh,
2119 struct v4l2_framebuffer *a)
2121 struct omap_overlay *ovl;
2122 struct omapvideo_info *ovid;
2123 struct omap_vout_device *vout = fh;
2124 struct omap_overlay_manager_info info;
2126 ovid = &vout->vid_info;
2127 ovl = ovid->overlays[0];
2130 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
2131 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
2133 if (ovl->manager && ovl->manager->get_manager_info) {
2134 ovl->manager->get_manager_info(ovl->manager, &info);
2135 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
2136 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
2137 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
2138 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
2140 if (ovl->manager && ovl->manager->get_manager_info) {
2141 ovl->manager->get_manager_info(ovl->manager, &info);
2142 if (info.alpha_enabled)
2143 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
2149 static const struct v4l2_ioctl_ops vout_ioctl_ops = {
2150 .vidioc_querycap = vidioc_querycap,
2151 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
2152 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
2153 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
2154 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
2155 .vidioc_queryctrl = vidioc_queryctrl,
2156 .vidioc_g_ctrl = vidioc_g_ctrl,
2157 .vidioc_s_fbuf = vidioc_s_fbuf,
2158 .vidioc_g_fbuf = vidioc_g_fbuf,
2159 .vidioc_s_ctrl = vidioc_s_ctrl,
2160 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
2161 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
2162 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
2163 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
2164 .vidioc_cropcap = vidioc_cropcap,
2165 .vidioc_g_crop = vidioc_g_crop,
2166 .vidioc_s_crop = vidioc_s_crop,
2167 .vidioc_reqbufs = vidioc_reqbufs,
2168 .vidioc_querybuf = vidioc_querybuf,
2169 .vidioc_qbuf = vidioc_qbuf,
2170 .vidioc_dqbuf = vidioc_dqbuf,
2171 .vidioc_streamon = vidioc_streamon,
2172 .vidioc_streamoff = vidioc_streamoff,
2175 static const struct v4l2_file_operations omap_vout_fops = {
2176 .owner = THIS_MODULE,
2177 .unlocked_ioctl = video_ioctl2,
2178 .mmap = omap_vout_mmap,
2179 .open = omap_vout_open,
2180 .release = omap_vout_release,
2183 /* Init functions used during driver initialization */
2184 /* Initial setup of video_data */
2185 static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
2187 struct video_device *vfd;
2188 struct v4l2_pix_format *pix;
2189 struct v4l2_control *control;
2190 struct omap_dss_device *display =
2191 vout->vid_info.overlays[0]->manager->device;
2193 /* set the default pix */
2196 /* Set the default picture of QVGA */
2197 pix->width = QQVGA_WIDTH;
2198 pix->height = QQVGA_HEIGHT;
2200 /* Default pixel format is RGB 5-6-5 */
2201 pix->pixelformat = V4L2_PIX_FMT_RGB565;
2202 pix->field = V4L2_FIELD_ANY;
2203 pix->bytesperline = pix->width * 2;
2204 pix->sizeimage = pix->bytesperline * pix->height;
2206 pix->colorspace = V4L2_COLORSPACE_JPEG;
2208 vout->bpp = RGB565_BPP;
2209 vout->fbuf.fmt.width = display->panel.timings.x_res;
2210 vout->fbuf.fmt.height = display->panel.timings.y_res;
2212 /* Set the data structures for the overlay parameters*/
2213 vout->win.global_alpha = 255;
2214 vout->fbuf.flags = 0;
2215 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
2216 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
2217 vout->win.chromakey = 0;
2219 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
2221 /*Initialize the control variables for
2222 rotation, flipping and background color. */
2223 control = vout->control;
2224 control[0].id = V4L2_CID_ROTATE;
2225 control[0].value = 0;
2228 vout->control[2].id = V4L2_CID_HFLIP;
2229 vout->control[2].value = 0;
2232 control[1].id = V4L2_CID_BG_COLOR;
2233 control[1].value = 0;
2235 /* initialize the video_device struct */
2236 vfd = vout->vfd = video_device_alloc();
2239 printk(KERN_ERR VOUT_NAME ": could not allocate"
2240 " video device struct\n");
2243 vfd->release = video_device_release;
2244 vfd->ioctl_ops = &vout_ioctl_ops;
2246 strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
2248 /* need to register for a VID_HARDWARE_* ID in videodev.h */
2249 vfd->fops = &omap_vout_fops;
2250 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
2251 mutex_init(&vout->lock);
2258 /* Setup video buffers */
2259 static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
2264 int image_width, image_height;
2265 struct video_device *vfd;
2266 struct omap_vout_device *vout;
2267 int static_vrfb_allocation = 0, vrfb_num_bufs = VRFB_NUM_BUFS;
2268 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2269 struct omap2video_device *vid_dev =
2270 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
2272 vout = vid_dev->vouts[vid_num];
2275 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
2276 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
2277 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
2279 for (i = 0; i < numbuffers; i++) {
2280 vout->buf_virt_addr[i] =
2281 omap_vout_alloc_buffer(vout->buffer_size,
2282 (u32 *) &vout->buf_phy_addr[i]);
2283 if (!vout->buf_virt_addr[i]) {
2290 for (i = 0; i < VRFB_NUM_BUFS; i++) {
2291 if (omap_vrfb_request_ctx(&vout->vrfb_context[i])) {
2292 dev_info(&pdev->dev, ": VRFB allocation failed\n");
2293 for (j = 0; j < i; j++)
2294 omap_vrfb_release_ctx(&vout->vrfb_context[j]);
2299 vout->cropped_offset = 0;
2301 /* Calculate VRFB memory size */
2302 /* allocate for worst case size */
2303 image_width = VID_MAX_WIDTH / TILE_SIZE;
2304 if (VID_MAX_WIDTH % TILE_SIZE)
2307 image_width = image_width * TILE_SIZE;
2308 image_height = VID_MAX_HEIGHT / TILE_SIZE;
2310 if (VID_MAX_HEIGHT % TILE_SIZE)
2313 image_height = image_height * TILE_SIZE;
2314 vout->smsshado_size = PAGE_ALIGN(image_width * image_height * 2 * 2);
2317 * Request and Initialize DMA, for DMA based VRFB transfer
2319 vout->vrfb_dma_tx.dev_id = OMAP_DMA_NO_DEVICE;
2320 vout->vrfb_dma_tx.dma_ch = -1;
2321 vout->vrfb_dma_tx.req_status = DMA_CHAN_ALLOTED;
2322 ret = omap_request_dma(vout->vrfb_dma_tx.dev_id, "VRFB DMA TX",
2323 omap_vout_vrfb_dma_tx_callback,
2324 (void *) &vout->vrfb_dma_tx, &vout->vrfb_dma_tx.dma_ch);
2326 vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
2327 dev_info(&pdev->dev, ": failed to allocate DMA Channel for"
2328 " video%d\n", vfd->minor);
2330 init_waitqueue_head(&vout->vrfb_dma_tx.wait);
2332 /* Allocate VRFB buffers if selected through bootargs */
2333 static_vrfb_allocation = (vid_num == 0) ?
2334 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
2336 /* statically allocated the VRFB buffer is done through
2337 commands line aruments */
2338 if (static_vrfb_allocation) {
2339 if (omap_vout_allocate_vrfb_buffers(vout, &vrfb_num_bufs, -1)) {
2341 goto release_vrfb_ctx;
2343 vout->vrfb_static_allocation = 1;
2348 for (j = 0; j < VRFB_NUM_BUFS; j++)
2349 omap_vrfb_release_ctx(&vout->vrfb_context[j]);
2352 for (i = 0; i < numbuffers; i++) {
2353 omap_vout_free_buffer(vout->buf_virt_addr[i],
2355 vout->buf_virt_addr[i] = 0;
2356 vout->buf_phy_addr[i] = 0;
2362 /* Create video out devices */
2363 static int __init omap_vout_create_video_devices(struct platform_device *pdev)
2366 struct omap_vout_device *vout;
2367 struct video_device *vfd = NULL;
2368 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2369 struct omap2video_device *vid_dev = container_of(v4l2_dev,
2370 struct omap2video_device, v4l2_dev);
2372 for (k = 0; k < pdev->num_resources; k++) {
2374 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
2376 dev_err(&pdev->dev, ": could not allocate memory\n");
2381 vid_dev->vouts[k] = vout;
2382 vout->vid_dev = vid_dev;
2383 /* Select video2 if only 1 overlay is controlled by V4L2 */
2384 if (pdev->num_resources == 1)
2385 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2387 /* Else select video1 and video2 one by one. */
2388 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2389 vout->vid_info.num_overlays = 1;
2390 vout->vid_info.id = k + 1;
2392 /* Setup the default configuration for the video devices
2394 if (omap_vout_setup_video_data(vout) != 0) {
2399 /* Allocate default number of buffers for the video streaming
2400 * and reserve the VRFB space for rotation
2402 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2407 /* Register the Video device with V4L2
2410 if (video_register_device(vfd, VFL_TYPE_GRABBER, k + 1) < 0) {
2411 dev_err(&pdev->dev, ": Could not register "
2412 "Video for Linux device\n");
2417 video_set_drvdata(vfd, vout);
2419 /* Configure the overlay structure */
2420 ret = omapvid_init(vid_dev->vouts[k], 0);
2425 omap_vout_release_vrfb(vout);
2426 omap_vout_free_buffers(vout);
2428 video_device_release(vfd);
2434 dev_info(&pdev->dev, ": registered and initialized"
2435 " video device %d\n", vfd->minor);
2436 if (k == (pdev->num_resources - 1))
2442 /* Driver functions */
2443 static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2445 struct video_device *vfd;
2452 if (!video_is_registered(vfd)) {
2454 * The device was never registered, so release the
2455 * video_device struct directly.
2457 video_device_release(vfd);
2460 * The unregister function will release the video_device
2461 * struct as well as unregistering it.
2463 video_unregister_device(vfd);
2467 omap_vout_release_vrfb(vout);
2468 omap_vout_free_buffers(vout);
2469 /* Free the VRFB buffer if allocated
2472 if (vout->vrfb_static_allocation)
2473 omap_vout_free_vrfb_buffers(vout);
2478 static int omap_vout_remove(struct platform_device *pdev)
2481 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2482 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2483 omap2video_device, v4l2_dev);
2485 v4l2_device_unregister(v4l2_dev);
2486 for (k = 0; k < pdev->num_resources; k++)
2487 omap_vout_cleanup_device(vid_dev->vouts[k]);
2489 for (k = 0; k < vid_dev->num_displays; k++) {
2490 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
2491 vid_dev->displays[k]->disable(vid_dev->displays[k]);
2493 omap_dss_put_device(vid_dev->displays[k]);
2499 static int __init omap_vout_probe(struct platform_device *pdev)
2502 struct omap_overlay *ovl;
2503 struct omap_dss_device *dssdev = NULL;
2504 struct omap_dss_device *def_display;
2505 struct omap2video_device *vid_dev = NULL;
2507 if (pdev->num_resources == 0) {
2508 dev_err(&pdev->dev, "probed for an unknown device\n");
2512 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2513 if (vid_dev == NULL)
2516 vid_dev->num_displays = 0;
2517 for_each_dss_dev(dssdev) {
2518 omap_dss_get_device(dssdev);
2519 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2522 if (vid_dev->num_displays == 0) {
2523 dev_err(&pdev->dev, "no displays\n");
2528 vid_dev->num_overlays = omap_dss_get_num_overlays();
2529 for (i = 0; i < vid_dev->num_overlays; i++)
2530 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2532 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2533 for (i = 0; i < vid_dev->num_managers; i++)
2534 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2536 /* Get the Video1 overlay and video2 overlay.
2537 * Setup the Display attached to that overlays
2539 for (i = 1; i < vid_dev->num_overlays; i++) {
2540 ovl = omap_dss_get_overlay(i);
2541 if (ovl->manager && ovl->manager->device) {
2542 def_display = ovl->manager->device;
2544 dev_warn(&pdev->dev, "cannot find display\n");
2548 ret = def_display->enable(def_display);
2550 /* Here we are not considering a error
2551 * as display may be enabled by frame
2554 dev_warn(&pdev->dev,
2555 "'%s' Display already enabled\n",
2558 /* set the update mode */
2559 if (def_display->caps &
2560 OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2561 #ifdef CONFIG_FB_OMAP2_FORCE_AUTO_UPDATE
2562 if (def_display->enable_te)
2563 def_display->enable_te(def_display, 1);
2564 if (def_display->set_update_mode)
2565 def_display->set_update_mode(def_display,
2566 OMAP_DSS_UPDATE_AUTO);
2567 #else /* MANUAL_UPDATE */
2568 if (def_display->enable_te)
2569 def_display->enable_te(def_display, 0);
2570 if (def_display->set_update_mode)
2571 def_display->set_update_mode(def_display,
2572 OMAP_DSS_UPDATE_MANUAL);
2575 if (def_display->set_update_mode)
2576 def_display->set_update_mode(def_display,
2577 OMAP_DSS_UPDATE_AUTO);
2582 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2583 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2588 ret = omap_vout_create_video_devices(pdev);
2592 for (i = 0; i < vid_dev->num_displays; i++) {
2593 struct omap_dss_device *display = vid_dev->displays[i];
2595 if (display->update)
2596 display->update(display, 0, 0,
2597 display->panel.timings.x_res,
2598 display->panel.timings.y_res);
2603 v4l2_device_unregister(&vid_dev->v4l2_dev);
2605 for (i = 1; i < vid_dev->num_overlays; i++) {
2607 ovl = omap_dss_get_overlay(i);
2608 if (ovl->manager && ovl->manager->device)
2609 def_display = ovl->manager->device;
2612 def_display->disable(def_display);
2619 static struct platform_driver omap_vout_driver = {
2623 .probe = omap_vout_probe,
2624 .remove = omap_vout_remove,
2627 static int __init omap_vout_init(void)
2629 if (platform_driver_register(&omap_vout_driver) != 0) {
2630 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2636 static void omap_vout_cleanup(void)
2638 platform_driver_unregister(&omap_vout_driver);
2641 late_initcall(omap_vout_init);
2642 module_exit(omap_vout_cleanup);