media: omap_vout: Fix compile error in 3.1
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / omap / omap_vout.c
blob3422da0034f444e31fb32e4842fd89bb1b153607
1 /*
2 * omap_vout.c
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.
19 * History:
20 * 20-APR-2006 Khasim Modified VRFB based Rotation,
21 * The image data is always read from 0 degree
22 * view and written
23 * to the virtual space of desired rotation angle
24 * 4-DEC-2006 Jian Changed to support better memory management
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 <plat/dma.h>
48 #include <plat/vrfb.h>
49 #include <video/omapdss.h>
51 #include "omap_voutlib.h"
52 #include "omap_voutdef.h"
53 #include "omap_vout_vrfb.h"
55 MODULE_AUTHOR("Texas Instruments");
56 MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
57 MODULE_LICENSE("GPL");
59 /* Driver Configuration macros */
60 #define VOUT_NAME "omap_vout"
62 enum omap_vout_channels {
63 OMAP_VIDEO1,
64 OMAP_VIDEO2,
67 static struct videobuf_queue_ops video_vbq_ops;
68 /* Variables configurable through module params*/
69 static u32 video1_numbuffers = 3;
70 static u32 video2_numbuffers = 3;
71 static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
72 static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
73 static u32 vid1_static_vrfb_alloc;
74 static u32 vid2_static_vrfb_alloc;
75 static int debug;
77 /* Module parameters */
78 module_param(video1_numbuffers, uint, S_IRUGO);
79 MODULE_PARM_DESC(video1_numbuffers,
80 "Number of buffers to be allocated at init time for Video1 device.");
82 module_param(video2_numbuffers, uint, S_IRUGO);
83 MODULE_PARM_DESC(video2_numbuffers,
84 "Number of buffers to be allocated at init time for Video2 device.");
86 module_param(video1_bufsize, uint, S_IRUGO);
87 MODULE_PARM_DESC(video1_bufsize,
88 "Size of the buffer to be allocated for video1 device");
90 module_param(video2_bufsize, uint, S_IRUGO);
91 MODULE_PARM_DESC(video2_bufsize,
92 "Size of the buffer to be allocated for video2 device");
94 module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
95 MODULE_PARM_DESC(vid1_static_vrfb_alloc,
96 "Static allocation of the VRFB buffer for video1 device");
98 module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
99 MODULE_PARM_DESC(vid2_static_vrfb_alloc,
100 "Static allocation of the VRFB buffer for video2 device");
102 module_param(debug, bool, S_IRUGO);
103 MODULE_PARM_DESC(debug, "Debug level (0-1)");
105 /* list of image formats supported by OMAP2 video pipelines */
106 static const struct v4l2_fmtdesc omap_formats[] = {
108 /* Note: V4L2 defines RGB565 as:
110 * Byte 0 Byte 1
111 * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
113 * We interpret RGB565 as:
115 * Byte 0 Byte 1
116 * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
118 .description = "RGB565, le",
119 .pixelformat = V4L2_PIX_FMT_RGB565,
122 /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
123 * this for RGB24 unpack mode, the last 8 bits are ignored
124 * */
125 .description = "RGB32, le",
126 .pixelformat = V4L2_PIX_FMT_RGB32,
129 /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
130 * this for RGB24 packed mode
133 .description = "RGB24, le",
134 .pixelformat = V4L2_PIX_FMT_RGB24,
137 .description = "YUYV (YUV 4:2:2), packed",
138 .pixelformat = V4L2_PIX_FMT_YUYV,
141 .description = "UYVY, packed",
142 .pixelformat = V4L2_PIX_FMT_UYVY,
146 #define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
149 * Try format
151 static int omap_vout_try_format(struct v4l2_pix_format *pix)
153 int ifmt, bpp = 0;
155 pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
156 (u32)VID_MAX_HEIGHT);
157 pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
159 for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
160 if (pix->pixelformat == omap_formats[ifmt].pixelformat)
161 break;
164 if (ifmt == NUM_OUTPUT_FORMATS)
165 ifmt = 0;
167 pix->pixelformat = omap_formats[ifmt].pixelformat;
168 pix->field = V4L2_FIELD_ANY;
169 pix->priv = 0;
171 switch (pix->pixelformat) {
172 case V4L2_PIX_FMT_YUYV:
173 case V4L2_PIX_FMT_UYVY:
174 default:
175 pix->colorspace = V4L2_COLORSPACE_JPEG;
176 bpp = YUYV_BPP;
177 break;
178 case V4L2_PIX_FMT_RGB565:
179 case V4L2_PIX_FMT_RGB565X:
180 pix->colorspace = V4L2_COLORSPACE_SRGB;
181 bpp = RGB565_BPP;
182 break;
183 case V4L2_PIX_FMT_RGB24:
184 pix->colorspace = V4L2_COLORSPACE_SRGB;
185 bpp = RGB24_BPP;
186 break;
187 case V4L2_PIX_FMT_RGB32:
188 case V4L2_PIX_FMT_BGR32:
189 pix->colorspace = V4L2_COLORSPACE_SRGB;
190 bpp = RGB32_BPP;
191 break;
193 pix->bytesperline = pix->width * bpp;
194 pix->sizeimage = pix->bytesperline * pix->height;
196 return bpp;
200 * omap_vout_uservirt_to_phys: This inline function is used to convert user
201 * space virtual address to physical address.
203 static u32 omap_vout_uservirt_to_phys(u32 virtp)
205 unsigned long physp = 0;
206 struct vm_area_struct *vma;
207 struct mm_struct *mm = current->mm;
209 vma = find_vma(mm, virtp);
210 /* For kernel direct-mapped memory, take the easy way */
211 if (virtp >= PAGE_OFFSET) {
212 physp = virt_to_phys((void *) virtp);
213 } else if (vma && (vma->vm_flags & VM_IO) && vma->vm_pgoff) {
214 /* this will catch, kernel-allocated, mmaped-to-usermode
215 addresses */
216 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
217 } else {
218 /* otherwise, use get_user_pages() for general userland pages */
219 int res, nr_pages = 1;
220 struct page *pages;
221 down_read(&current->mm->mmap_sem);
223 res = get_user_pages(current, current->mm, virtp, nr_pages, 1,
224 0, &pages, NULL);
225 up_read(&current->mm->mmap_sem);
227 if (res == nr_pages) {
228 physp = __pa(page_address(&pages[0]) +
229 (virtp & ~PAGE_MASK));
230 } else {
231 printk(KERN_WARNING VOUT_NAME
232 "get_user_pages failed\n");
233 return 0;
237 return physp;
241 * Free the V4L2 buffers
243 void omap_vout_free_buffers(struct omap_vout_device *vout)
245 int i, numbuffers;
247 /* Allocate memory for the buffers */
248 numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
249 vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
251 for (i = 0; i < numbuffers; i++) {
252 omap_vout_free_buffer(vout->buf_virt_addr[i],
253 vout->buffer_size);
254 vout->buf_phy_addr[i] = 0;
255 vout->buf_virt_addr[i] = 0;
260 * Convert V4L2 rotation to DSS rotation
261 * V4L2 understand 0, 90, 180, 270.
262 * Convert to 0, 1, 2 and 3 respectively for DSS
264 static int v4l2_rot_to_dss_rot(int v4l2_rotation,
265 enum dss_rotation *rotation, bool mirror)
267 int ret = 0;
269 switch (v4l2_rotation) {
270 case 90:
271 *rotation = dss_rotation_90_degree;
272 break;
273 case 180:
274 *rotation = dss_rotation_180_degree;
275 break;
276 case 270:
277 *rotation = dss_rotation_270_degree;
278 break;
279 case 0:
280 *rotation = dss_rotation_0_degree;
281 break;
282 default:
283 ret = -EINVAL;
285 return ret;
288 static int omap_vout_calculate_offset(struct omap_vout_device *vout)
290 struct omapvideo_info *ovid;
291 struct v4l2_rect *crop = &vout->crop;
292 struct v4l2_pix_format *pix = &vout->pix;
293 int *cropped_offset = &vout->cropped_offset;
294 int ps = 2, line_length = 0;
296 ovid = &vout->vid_info;
298 if (ovid->rotation_type == VOUT_ROT_VRFB) {
299 omap_vout_calculate_vrfb_offset(vout);
300 } else {
301 vout->line_length = line_length = pix->width;
303 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
304 V4L2_PIX_FMT_UYVY == pix->pixelformat)
305 ps = 2;
306 else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
307 ps = 4;
308 else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
309 ps = 3;
311 vout->ps = ps;
313 *cropped_offset = (line_length * ps) *
314 crop->top + crop->left * ps;
317 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
318 __func__, vout->cropped_offset);
320 return 0;
324 * Convert V4L2 pixel format to DSS pixel format
326 static int video_mode_to_dss_mode(struct omap_vout_device *vout)
328 struct omap_overlay *ovl;
329 struct omapvideo_info *ovid;
330 struct v4l2_pix_format *pix = &vout->pix;
331 enum omap_color_mode mode;
333 ovid = &vout->vid_info;
334 ovl = ovid->overlays[0];
336 switch (pix->pixelformat) {
337 case 0:
338 break;
339 case V4L2_PIX_FMT_YUYV:
340 mode = OMAP_DSS_COLOR_YUV2;
341 break;
342 case V4L2_PIX_FMT_UYVY:
343 mode = OMAP_DSS_COLOR_UYVY;
344 break;
345 case V4L2_PIX_FMT_RGB565:
346 mode = OMAP_DSS_COLOR_RGB16;
347 break;
348 case V4L2_PIX_FMT_RGB24:
349 mode = OMAP_DSS_COLOR_RGB24P;
350 break;
351 case V4L2_PIX_FMT_RGB32:
352 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
353 OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
354 break;
355 case V4L2_PIX_FMT_BGR32:
356 mode = OMAP_DSS_COLOR_RGBX32;
357 break;
358 default:
359 mode = -EINVAL;
361 return mode;
365 * Setup the overlay
367 static int omapvid_setup_overlay(struct omap_vout_device *vout,
368 struct omap_overlay *ovl, int posx, int posy, int outw,
369 int outh, u32 addr)
371 int ret = 0;
372 struct omap_overlay_info info;
373 int cropheight, cropwidth, pixheight, pixwidth;
375 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
376 (outw != vout->pix.width || outh != vout->pix.height)) {
377 ret = -EINVAL;
378 goto setup_ovl_err;
381 vout->dss_mode = video_mode_to_dss_mode(vout);
382 if (vout->dss_mode == -EINVAL) {
383 ret = -EINVAL;
384 goto setup_ovl_err;
387 /* Setup the input plane parameters according to
388 * rotation value selected.
390 if (is_rotation_90_or_270(vout)) {
391 cropheight = vout->crop.width;
392 cropwidth = vout->crop.height;
393 pixheight = vout->pix.width;
394 pixwidth = vout->pix.height;
395 } else {
396 cropheight = vout->crop.height;
397 cropwidth = vout->crop.width;
398 pixheight = vout->pix.height;
399 pixwidth = vout->pix.width;
402 ovl->get_overlay_info(ovl, &info);
403 info.paddr = addr;
404 info.vaddr = NULL;
405 info.width = cropwidth;
406 info.height = cropheight;
407 info.color_mode = vout->dss_mode;
408 info.mirror = vout->mirror;
409 info.pos_x = posx;
410 info.pos_y = posy;
411 info.out_width = outw;
412 info.out_height = outh;
413 info.global_alpha = vout->win.global_alpha;
414 if (!is_rotation_enabled(vout)) {
415 info.rotation = 0;
416 info.rotation_type = OMAP_DSS_ROT_DMA;
417 info.screen_width = pixwidth;
418 } else {
419 info.rotation = vout->rotation;
420 info.rotation_type = OMAP_DSS_ROT_VRFB;
421 info.screen_width = 2048;
424 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
425 "%s enable=%d addr=%x width=%d\n height=%d color_mode=%d\n"
426 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
427 "out_height=%d rotation_type=%d screen_width=%d\n",
428 __func__, info.enabled, info.paddr, info.width, info.height,
429 info.color_mode, info.rotation, info.mirror, info.pos_x,
430 info.pos_y, info.out_width, info.out_height, info.rotation_type,
431 info.screen_width);
433 ret = ovl->set_overlay_info(ovl, &info);
434 if (ret)
435 goto setup_ovl_err;
437 return 0;
439 setup_ovl_err:
440 v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
441 return ret;
445 * Initialize the overlay structure
447 static int omapvid_init(struct omap_vout_device *vout, u32 addr)
449 int ret = 0, i;
450 struct v4l2_window *win;
451 struct omap_overlay *ovl;
452 int posx, posy, outw, outh, temp;
453 struct omap_video_timings *timing;
454 struct omapvideo_info *ovid = &vout->vid_info;
456 win = &vout->win;
457 for (i = 0; i < ovid->num_overlays; i++) {
458 ovl = ovid->overlays[i];
459 if (!ovl->manager || !ovl->manager->device)
460 return -EINVAL;
462 timing = &ovl->manager->device->panel.timings;
464 outw = win->w.width;
465 outh = win->w.height;
466 switch (vout->rotation) {
467 case dss_rotation_90_degree:
468 /* Invert the height and width for 90
469 * and 270 degree rotation
471 temp = outw;
472 outw = outh;
473 outh = temp;
474 posy = (timing->y_res - win->w.width) - win->w.left;
475 posx = win->w.top;
476 break;
478 case dss_rotation_180_degree:
479 posx = (timing->x_res - win->w.width) - win->w.left;
480 posy = (timing->y_res - win->w.height) - win->w.top;
481 break;
483 case dss_rotation_270_degree:
484 temp = outw;
485 outw = outh;
486 outh = temp;
487 posy = win->w.left;
488 posx = (timing->x_res - win->w.height) - win->w.top;
489 break;
491 default:
492 posx = win->w.left;
493 posy = win->w.top;
494 break;
497 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
498 outw, outh, addr);
499 if (ret)
500 goto omapvid_init_err;
502 return 0;
504 omapvid_init_err:
505 v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
506 return ret;
510 * Apply the changes set the go bit of DSS
512 static int omapvid_apply_changes(struct omap_vout_device *vout)
514 int i;
515 struct omap_overlay *ovl;
516 struct omapvideo_info *ovid = &vout->vid_info;
518 for (i = 0; i < ovid->num_overlays; i++) {
519 ovl = ovid->overlays[i];
520 if (!ovl->manager || !ovl->manager->device)
521 return -EINVAL;
522 ovl->manager->apply(ovl->manager);
525 return 0;
528 static void omap_vout_isr(void *arg, unsigned int irqstatus)
530 int ret;
531 u32 addr, fid;
532 struct omap_overlay *ovl;
533 struct timeval timevalue;
534 struct omapvideo_info *ovid;
535 struct omap_dss_device *cur_display;
536 struct omap_vout_device *vout = (struct omap_vout_device *)arg;
538 if (!vout->streaming)
539 return;
541 ovid = &vout->vid_info;
542 ovl = ovid->overlays[0];
543 /* get the display device attached to the overlay */
544 if (!ovl->manager || !ovl->manager->device)
545 return;
547 cur_display = ovl->manager->device;
549 spin_lock(&vout->vbq_lock);
550 do_gettimeofday(&timevalue);
552 if (cur_display->type != OMAP_DISPLAY_TYPE_VENC) {
553 switch (cur_display->type) {
554 case OMAP_DISPLAY_TYPE_DPI:
555 if (!(irqstatus & (DISPC_IRQ_VSYNC | DISPC_IRQ_VSYNC2)))
556 goto vout_isr_err;
557 break;
558 case OMAP_DISPLAY_TYPE_HDMI:
559 if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
560 goto vout_isr_err;
561 break;
562 default:
563 goto vout_isr_err;
565 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
566 vout->cur_frm->ts = timevalue;
567 vout->cur_frm->state = VIDEOBUF_DONE;
568 wake_up_interruptible(&vout->cur_frm->done);
569 vout->cur_frm = vout->next_frm;
571 vout->first_int = 0;
572 if (list_empty(&vout->dma_queue))
573 goto vout_isr_err;
575 vout->next_frm = list_entry(vout->dma_queue.next,
576 struct videobuf_buffer, queue);
577 list_del(&vout->next_frm->queue);
579 vout->next_frm->state = VIDEOBUF_ACTIVE;
581 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
582 + vout->cropped_offset;
584 /* First save the configuration in ovelray structure */
585 ret = omapvid_init(vout, addr);
586 if (ret)
587 printk(KERN_ERR VOUT_NAME
588 "failed to set overlay info\n");
589 /* Enable the pipeline and set the Go bit */
590 ret = omapvid_apply_changes(vout);
591 if (ret)
592 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
593 } else {
595 if (vout->first_int) {
596 vout->first_int = 0;
597 goto vout_isr_err;
599 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
600 fid = 1;
601 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
602 fid = 0;
603 else
604 goto vout_isr_err;
606 vout->field_id ^= 1;
607 if (fid != vout->field_id) {
608 if (0 == fid)
609 vout->field_id = fid;
611 goto vout_isr_err;
613 if (0 == fid) {
614 if (vout->cur_frm == vout->next_frm)
615 goto vout_isr_err;
617 vout->cur_frm->ts = timevalue;
618 vout->cur_frm->state = VIDEOBUF_DONE;
619 wake_up_interruptible(&vout->cur_frm->done);
620 vout->cur_frm = vout->next_frm;
621 } else if (1 == fid) {
622 if (list_empty(&vout->dma_queue) ||
623 (vout->cur_frm != vout->next_frm))
624 goto vout_isr_err;
626 vout->next_frm = list_entry(vout->dma_queue.next,
627 struct videobuf_buffer, queue);
628 list_del(&vout->next_frm->queue);
630 vout->next_frm->state = VIDEOBUF_ACTIVE;
631 addr = (unsigned long)
632 vout->queued_buf_addr[vout->next_frm->i] +
633 vout->cropped_offset;
634 /* First save the configuration in ovelray structure */
635 ret = omapvid_init(vout, addr);
636 if (ret)
637 printk(KERN_ERR VOUT_NAME
638 "failed to set overlay info\n");
639 /* Enable the pipeline and set the Go bit */
640 ret = omapvid_apply_changes(vout);
641 if (ret)
642 printk(KERN_ERR VOUT_NAME
643 "failed to change mode\n");
648 vout_isr_err:
649 spin_unlock(&vout->vbq_lock);
653 /* Video buffer call backs */
656 * Buffer setup function is called by videobuf layer when REQBUF ioctl is
657 * called. This is used to setup buffers and return size and count of
658 * buffers allocated. After the call to this buffer, videobuf layer will
659 * setup buffer queue depending on the size and count of buffers
661 static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
662 unsigned int *size)
664 int startindex = 0, i, j;
665 u32 phy_addr = 0, virt_addr = 0;
666 struct omap_vout_device *vout = q->priv_data;
667 struct omapvideo_info *ovid = &vout->vid_info;
669 if (!vout)
670 return -EINVAL;
672 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
673 return -EINVAL;
675 startindex = (vout->vid == OMAP_VIDEO1) ?
676 video1_numbuffers : video2_numbuffers;
677 if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
678 *count = startindex;
680 if (ovid->rotation_type == VOUT_ROT_VRFB) {
681 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
682 return -ENOMEM;
685 if (V4L2_MEMORY_MMAP != vout->memory)
686 return 0;
688 /* Now allocated the V4L2 buffers */
689 *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
690 startindex = (vout->vid == OMAP_VIDEO1) ?
691 video1_numbuffers : video2_numbuffers;
693 /* Check the size of the buffer */
694 if (*size > vout->buffer_size) {
695 v4l2_err(&vout->vid_dev->v4l2_dev,
696 "buffer allocation mismatch [%u] [%u]\n",
697 *size, vout->buffer_size);
698 return -ENOMEM;
701 for (i = startindex; i < *count; i++) {
702 vout->buffer_size = *size;
704 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
705 &phy_addr);
706 if (!virt_addr) {
707 if (ovid->rotation_type == VOUT_ROT_NONE) {
708 break;
709 } else {
710 if (!is_rotation_enabled(vout))
711 break;
712 /* Free the VRFB buffers if no space for V4L2 buffers */
713 for (j = i; j < *count; j++) {
714 omap_vout_free_buffer(
715 vout->smsshado_virt_addr[j],
716 vout->smsshado_size);
717 vout->smsshado_virt_addr[j] = 0;
718 vout->smsshado_phy_addr[j] = 0;
722 vout->buf_virt_addr[i] = virt_addr;
723 vout->buf_phy_addr[i] = phy_addr;
725 *count = vout->buffer_allocated = i;
727 return 0;
731 * Free the V4L2 buffers additionally allocated than default
732 * number of buffers
734 static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
736 int num_buffers = 0, i;
738 num_buffers = (vout->vid == OMAP_VIDEO1) ?
739 video1_numbuffers : video2_numbuffers;
741 for (i = num_buffers; i < vout->buffer_allocated; i++) {
742 if (vout->buf_virt_addr[i])
743 omap_vout_free_buffer(vout->buf_virt_addr[i],
744 vout->buffer_size);
746 vout->buf_virt_addr[i] = 0;
747 vout->buf_phy_addr[i] = 0;
749 vout->buffer_allocated = num_buffers;
753 * This function will be called when VIDIOC_QBUF ioctl is called.
754 * It prepare buffers before give out for the display. This function
755 * converts user space virtual address into physical address if userptr memory
756 * exchange mechanism is used. If rotation is enabled, it copies entire
757 * buffer into VRFB memory space before giving it to the DSS.
759 static int omap_vout_buffer_prepare(struct videobuf_queue *q,
760 struct videobuf_buffer *vb,
761 enum v4l2_field field)
763 struct omap_vout_device *vout = q->priv_data;
764 struct omapvideo_info *ovid = &vout->vid_info;
766 if (VIDEOBUF_NEEDS_INIT == vb->state) {
767 vb->width = vout->pix.width;
768 vb->height = vout->pix.height;
769 vb->size = vb->width * vb->height * vout->bpp;
770 vb->field = field;
772 vb->state = VIDEOBUF_PREPARED;
773 /* if user pointer memory mechanism is used, get the physical
774 * address of the buffer
776 if (V4L2_MEMORY_USERPTR == vb->memory) {
777 if (0 == vb->baddr)
778 return -EINVAL;
779 /* Physical address */
780 vout->queued_buf_addr[vb->i] = (u8 *)
781 omap_vout_uservirt_to_phys(vb->baddr);
782 } else {
783 u32 addr, dma_addr;
784 unsigned long size;
786 addr = (unsigned long) vout->buf_virt_addr[vb->i];
787 size = (unsigned long) vb->size;
789 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
790 size, DMA_TO_DEVICE);
791 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
792 v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
794 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
797 if (ovid->rotation_type == VOUT_ROT_VRFB)
798 return omap_vout_prepare_vrfb(vout, vb);
799 else
800 return 0;
804 * Buffer queue function will be called from the videobuf layer when _QBUF
805 * ioctl is called. It is used to enqueue buffer, which is ready to be
806 * displayed.
808 static void omap_vout_buffer_queue(struct videobuf_queue *q,
809 struct videobuf_buffer *vb)
811 struct omap_vout_device *vout = q->priv_data;
813 /* Driver is also maintainig a queue. So enqueue buffer in the driver
814 * queue */
815 list_add_tail(&vb->queue, &vout->dma_queue);
817 vb->state = VIDEOBUF_QUEUED;
821 * Buffer release function is called from videobuf layer to release buffer
822 * which are already allocated
824 static void omap_vout_buffer_release(struct videobuf_queue *q,
825 struct videobuf_buffer *vb)
827 struct omap_vout_device *vout = q->priv_data;
829 vb->state = VIDEOBUF_NEEDS_INIT;
831 if (V4L2_MEMORY_MMAP != vout->memory)
832 return;
836 * File operations
838 static void omap_vout_vm_open(struct vm_area_struct *vma)
840 struct omap_vout_device *vout = vma->vm_private_data;
842 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
843 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
844 vout->mmap_count++;
847 static void omap_vout_vm_close(struct vm_area_struct *vma)
849 struct omap_vout_device *vout = vma->vm_private_data;
851 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
852 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
853 vout->mmap_count--;
856 static struct vm_operations_struct omap_vout_vm_ops = {
857 .open = omap_vout_vm_open,
858 .close = omap_vout_vm_close,
861 static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
863 int i;
864 void *pos;
865 unsigned long start = vma->vm_start;
866 unsigned long size = (vma->vm_end - vma->vm_start);
867 struct omap_vout_device *vout = file->private_data;
868 struct videobuf_queue *q = &vout->vbq;
870 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
871 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
872 vma->vm_pgoff, vma->vm_start, vma->vm_end);
874 /* look for the buffer to map */
875 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
876 if (NULL == q->bufs[i])
877 continue;
878 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
879 continue;
880 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
881 break;
884 if (VIDEO_MAX_FRAME == i) {
885 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
886 "offset invalid [offset=0x%lx]\n",
887 (vma->vm_pgoff << PAGE_SHIFT));
888 return -EINVAL;
890 /* Check the size of the buffer */
891 if (size > vout->buffer_size) {
892 v4l2_err(&vout->vid_dev->v4l2_dev,
893 "insufficient memory [%lu] [%u]\n",
894 size, vout->buffer_size);
895 return -ENOMEM;
898 q->bufs[i]->baddr = vma->vm_start;
900 vma->vm_flags |= VM_RESERVED;
901 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
902 vma->vm_ops = &omap_vout_vm_ops;
903 vma->vm_private_data = (void *) vout;
904 pos = (void *)vout->buf_virt_addr[i];
905 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
906 while (size > 0) {
907 unsigned long pfn;
908 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
909 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
910 return -EAGAIN;
911 start += PAGE_SIZE;
912 pos += PAGE_SIZE;
913 size -= PAGE_SIZE;
915 vout->mmap_count++;
916 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
918 return 0;
921 static int omap_vout_release(struct file *file)
923 unsigned int ret, i;
924 struct videobuf_queue *q;
925 struct omapvideo_info *ovid;
926 struct omap_vout_device *vout = file->private_data;
928 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
929 ovid = &vout->vid_info;
931 if (!vout)
932 return 0;
934 q = &vout->vbq;
935 /* Disable all the overlay managers connected with this interface */
936 for (i = 0; i < ovid->num_overlays; i++) {
937 struct omap_overlay *ovl = ovid->overlays[i];
938 if (ovl->manager && ovl->manager->device) {
939 struct omap_overlay_info info;
940 ovl->get_overlay_info(ovl, &info);
941 info.enabled = 0;
942 ovl->set_overlay_info(ovl, &info);
945 /* Turn off the pipeline */
946 ret = omapvid_apply_changes(vout);
947 if (ret)
948 v4l2_warn(&vout->vid_dev->v4l2_dev,
949 "Unable to apply changes\n");
951 /* Free all buffers */
952 omap_vout_free_extra_buffers(vout);
954 /* Free the VRFB buffers only if they are allocated
955 * during reqbufs. Don't free if init time allocated
957 if (ovid->rotation_type == VOUT_ROT_VRFB) {
958 if (!vout->vrfb_static_allocation)
959 omap_vout_free_vrfb_buffers(vout);
961 videobuf_mmap_free(q);
963 /* Even if apply changes fails we should continue
964 freeing allocated memory */
965 if (vout->streaming) {
966 u32 mask = 0;
968 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
969 DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
970 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
971 vout->streaming = 0;
973 videobuf_streamoff(q);
974 videobuf_queue_cancel(q);
977 if (vout->mmap_count != 0)
978 vout->mmap_count = 0;
980 vout->opened -= 1;
981 file->private_data = NULL;
983 if (vout->buffer_allocated)
984 videobuf_mmap_free(q);
986 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
987 return ret;
990 static int omap_vout_open(struct file *file)
992 struct videobuf_queue *q;
993 struct omap_vout_device *vout = NULL;
995 vout = video_drvdata(file);
996 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
998 if (vout == NULL)
999 return -ENODEV;
1001 /* for now, we only support single open */
1002 if (vout->opened)
1003 return -EBUSY;
1005 vout->opened += 1;
1007 file->private_data = vout;
1008 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1010 q = &vout->vbq;
1011 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1012 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1013 video_vbq_ops.buf_release = omap_vout_buffer_release;
1014 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1015 spin_lock_init(&vout->vbq_lock);
1017 videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1018 &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
1019 sizeof(struct videobuf_buffer), vout, NULL);
1021 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1022 return 0;
1026 * V4L2 ioctls
1028 static int vidioc_querycap(struct file *file, void *fh,
1029 struct v4l2_capability *cap)
1031 struct omap_vout_device *vout = fh;
1033 strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1034 strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1035 cap->bus_info[0] = '\0';
1036 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
1038 return 0;
1041 static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1042 struct v4l2_fmtdesc *fmt)
1044 int index = fmt->index;
1046 if (index >= NUM_OUTPUT_FORMATS)
1047 return -EINVAL;
1049 fmt->flags = omap_formats[index].flags;
1050 strlcpy(fmt->description, omap_formats[index].description,
1051 sizeof(fmt->description));
1052 fmt->pixelformat = omap_formats[index].pixelformat;
1054 return 0;
1057 static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1058 struct v4l2_format *f)
1060 struct omap_vout_device *vout = fh;
1062 f->fmt.pix = vout->pix;
1063 return 0;
1067 static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1068 struct v4l2_format *f)
1070 struct omap_overlay *ovl;
1071 struct omapvideo_info *ovid;
1072 struct omap_video_timings *timing;
1073 struct omap_vout_device *vout = fh;
1075 ovid = &vout->vid_info;
1076 ovl = ovid->overlays[0];
1078 if (!ovl->manager || !ovl->manager->device)
1079 return -EINVAL;
1080 /* get the display device attached to the overlay */
1081 timing = &ovl->manager->device->panel.timings;
1083 vout->fbuf.fmt.height = timing->y_res;
1084 vout->fbuf.fmt.width = timing->x_res;
1086 omap_vout_try_format(&f->fmt.pix);
1087 return 0;
1090 static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1091 struct v4l2_format *f)
1093 int ret, bpp;
1094 struct omap_overlay *ovl;
1095 struct omapvideo_info *ovid;
1096 struct omap_video_timings *timing;
1097 struct omap_vout_device *vout = fh;
1099 if (vout->streaming)
1100 return -EBUSY;
1102 mutex_lock(&vout->lock);
1104 ovid = &vout->vid_info;
1105 ovl = ovid->overlays[0];
1107 /* get the display device attached to the overlay */
1108 if (!ovl->manager || !ovl->manager->device) {
1109 ret = -EINVAL;
1110 goto s_fmt_vid_out_exit;
1112 timing = &ovl->manager->device->panel.timings;
1114 /* We dont support RGB24-packed mode if vrfb rotation
1115 * is enabled*/
1116 if ((is_rotation_enabled(vout)) &&
1117 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1118 ret = -EINVAL;
1119 goto s_fmt_vid_out_exit;
1122 /* get the framebuffer parameters */
1124 if (is_rotation_90_or_270(vout)) {
1125 vout->fbuf.fmt.height = timing->x_res;
1126 vout->fbuf.fmt.width = timing->y_res;
1127 } else {
1128 vout->fbuf.fmt.height = timing->y_res;
1129 vout->fbuf.fmt.width = timing->x_res;
1132 /* change to samller size is OK */
1134 bpp = omap_vout_try_format(&f->fmt.pix);
1135 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1137 /* try & set the new output format */
1138 vout->bpp = bpp;
1139 vout->pix = f->fmt.pix;
1140 vout->vrfb_bpp = 1;
1142 /* If YUYV then vrfb bpp is 2, for others its 1 */
1143 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1144 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1145 vout->vrfb_bpp = 2;
1147 /* set default crop and win */
1148 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1150 /* Save the changes in the overlay strcuture */
1151 ret = omapvid_init(vout, 0);
1152 if (ret) {
1153 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1154 goto s_fmt_vid_out_exit;
1157 ret = 0;
1159 s_fmt_vid_out_exit:
1160 mutex_unlock(&vout->lock);
1161 return ret;
1164 static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1165 struct v4l2_format *f)
1167 int ret = 0;
1168 struct omap_vout_device *vout = fh;
1169 struct v4l2_window *win = &f->fmt.win;
1171 ret = omap_vout_try_window(&vout->fbuf, win);
1173 if (!ret) {
1174 if (vout->vid == OMAP_VIDEO1)
1175 win->global_alpha = 255;
1176 else
1177 win->global_alpha = f->fmt.win.global_alpha;
1180 return ret;
1183 static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1184 struct v4l2_format *f)
1186 int ret = 0;
1187 struct omap_overlay *ovl;
1188 struct omapvideo_info *ovid;
1189 struct omap_vout_device *vout = fh;
1190 struct v4l2_window *win = &f->fmt.win;
1192 mutex_lock(&vout->lock);
1193 ovid = &vout->vid_info;
1194 ovl = ovid->overlays[0];
1196 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1197 if (!ret) {
1198 /* Video1 plane does not support global alpha */
1199 if (ovl->id == OMAP_DSS_VIDEO1)
1200 vout->win.global_alpha = 255;
1201 else
1202 vout->win.global_alpha = f->fmt.win.global_alpha;
1204 vout->win.chromakey = f->fmt.win.chromakey;
1206 mutex_unlock(&vout->lock);
1207 return ret;
1210 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
1211 struct v4l2_fmtdesc *fmt)
1213 int index = fmt->index;
1215 if (index >= NUM_OUTPUT_FORMATS)
1216 return -EINVAL;
1218 fmt->flags = omap_formats[index].flags;
1219 strlcpy(fmt->description, omap_formats[index].description,
1220 sizeof(fmt->description));
1221 fmt->pixelformat = omap_formats[index].pixelformat;
1222 return 0;
1225 static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1226 struct v4l2_format *f)
1228 u32 key_value = 0;
1229 struct omap_overlay *ovl;
1230 struct omapvideo_info *ovid;
1231 struct omap_vout_device *vout = fh;
1232 struct omap_overlay_manager_info info;
1233 struct v4l2_window *win = &f->fmt.win;
1235 ovid = &vout->vid_info;
1236 ovl = ovid->overlays[0];
1238 win->w = vout->win.w;
1239 win->field = vout->win.field;
1240 win->global_alpha = vout->win.global_alpha;
1242 if (ovl->manager && ovl->manager->get_manager_info) {
1243 ovl->manager->get_manager_info(ovl->manager, &info);
1244 key_value = info.trans_key;
1246 win->chromakey = key_value;
1247 return 0;
1250 static int vidioc_cropcap(struct file *file, void *fh,
1251 struct v4l2_cropcap *cropcap)
1253 struct omap_vout_device *vout = fh;
1254 struct v4l2_pix_format *pix = &vout->pix;
1256 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1257 return -EINVAL;
1259 /* Width and height are always even */
1260 cropcap->bounds.width = pix->width & ~1;
1261 cropcap->bounds.height = pix->height & ~1;
1263 omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1264 cropcap->pixelaspect.numerator = 1;
1265 cropcap->pixelaspect.denominator = 1;
1266 return 0;
1269 static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1271 struct omap_vout_device *vout = fh;
1273 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1274 return -EINVAL;
1275 crop->c = vout->crop;
1276 return 0;
1279 static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1281 int ret = -EINVAL;
1282 struct omap_vout_device *vout = fh;
1283 struct omapvideo_info *ovid;
1284 struct omap_overlay *ovl;
1285 struct omap_video_timings *timing;
1287 if (vout->streaming)
1288 return -EBUSY;
1290 mutex_lock(&vout->lock);
1291 ovid = &vout->vid_info;
1292 ovl = ovid->overlays[0];
1294 if (!ovl->manager || !ovl->manager->device) {
1295 ret = -EINVAL;
1296 goto s_crop_err;
1298 /* get the display device attached to the overlay */
1299 timing = &ovl->manager->device->panel.timings;
1301 if (is_rotation_90_or_270(vout)) {
1302 vout->fbuf.fmt.height = timing->x_res;
1303 vout->fbuf.fmt.width = timing->y_res;
1304 } else {
1305 vout->fbuf.fmt.height = timing->y_res;
1306 vout->fbuf.fmt.width = timing->x_res;
1309 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1310 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1311 &vout->fbuf, &crop->c);
1313 s_crop_err:
1314 mutex_unlock(&vout->lock);
1315 return ret;
1318 static int vidioc_queryctrl(struct file *file, void *fh,
1319 struct v4l2_queryctrl *ctrl)
1321 int ret = 0;
1323 switch (ctrl->id) {
1324 case V4L2_CID_ROTATE:
1325 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1326 break;
1327 case V4L2_CID_BG_COLOR:
1328 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1329 break;
1330 case V4L2_CID_VFLIP:
1331 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1332 break;
1333 default:
1334 ctrl->name[0] = '\0';
1335 ret = -EINVAL;
1337 return ret;
1340 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1342 int ret = 0;
1343 struct omap_vout_device *vout = fh;
1345 switch (ctrl->id) {
1346 case V4L2_CID_ROTATE:
1347 ctrl->value = vout->control[0].value;
1348 break;
1349 case V4L2_CID_BG_COLOR:
1351 struct omap_overlay_manager_info info;
1352 struct omap_overlay *ovl;
1354 ovl = vout->vid_info.overlays[0];
1355 if (!ovl->manager || !ovl->manager->get_manager_info) {
1356 ret = -EINVAL;
1357 break;
1360 ovl->manager->get_manager_info(ovl->manager, &info);
1361 ctrl->value = info.default_color;
1362 break;
1364 case V4L2_CID_VFLIP:
1365 ctrl->value = vout->control[2].value;
1366 break;
1367 default:
1368 ret = -EINVAL;
1370 return ret;
1373 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1375 int ret = 0;
1376 struct omap_vout_device *vout = fh;
1378 switch (a->id) {
1379 case V4L2_CID_ROTATE:
1381 struct omapvideo_info *ovid;
1382 int rotation = a->value;
1384 ovid = &vout->vid_info;
1386 mutex_lock(&vout->lock);
1387 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1388 mutex_unlock(&vout->lock);
1389 ret = -ERANGE;
1390 break;
1393 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1394 mutex_unlock(&vout->lock);
1395 ret = -EINVAL;
1396 break;
1399 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1400 vout->mirror)) {
1401 mutex_unlock(&vout->lock);
1402 ret = -EINVAL;
1403 break;
1406 vout->control[0].value = rotation;
1407 mutex_unlock(&vout->lock);
1408 break;
1410 case V4L2_CID_BG_COLOR:
1412 struct omap_overlay *ovl;
1413 unsigned int color = a->value;
1414 struct omap_overlay_manager_info info;
1416 ovl = vout->vid_info.overlays[0];
1418 mutex_lock(&vout->lock);
1419 if (!ovl->manager || !ovl->manager->get_manager_info) {
1420 mutex_unlock(&vout->lock);
1421 ret = -EINVAL;
1422 break;
1425 ovl->manager->get_manager_info(ovl->manager, &info);
1426 info.default_color = color;
1427 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1428 mutex_unlock(&vout->lock);
1429 ret = -EINVAL;
1430 break;
1433 vout->control[1].value = color;
1434 mutex_unlock(&vout->lock);
1435 break;
1437 case V4L2_CID_VFLIP:
1439 struct omap_overlay *ovl;
1440 struct omapvideo_info *ovid;
1441 unsigned int mirror = a->value;
1443 ovid = &vout->vid_info;
1444 ovl = ovid->overlays[0];
1446 mutex_lock(&vout->lock);
1447 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1448 mutex_unlock(&vout->lock);
1449 ret = -ERANGE;
1450 break;
1453 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1454 mutex_unlock(&vout->lock);
1455 ret = -EINVAL;
1456 break;
1458 vout->mirror = mirror;
1459 vout->control[2].value = mirror;
1460 mutex_unlock(&vout->lock);
1461 break;
1463 default:
1464 ret = -EINVAL;
1466 return ret;
1469 static int vidioc_reqbufs(struct file *file, void *fh,
1470 struct v4l2_requestbuffers *req)
1472 int ret = 0;
1473 unsigned int i, num_buffers = 0;
1474 struct omap_vout_device *vout = fh;
1475 struct videobuf_queue *q = &vout->vbq;
1477 if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
1478 return -EINVAL;
1479 /* if memory is not mmp or userptr
1480 return error */
1481 if ((V4L2_MEMORY_MMAP != req->memory) &&
1482 (V4L2_MEMORY_USERPTR != req->memory))
1483 return -EINVAL;
1485 mutex_lock(&vout->lock);
1486 /* Cannot be requested when streaming is on */
1487 if (vout->streaming) {
1488 ret = -EBUSY;
1489 goto reqbuf_err;
1492 /* If buffers are already allocated free them */
1493 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1494 if (vout->mmap_count) {
1495 ret = -EBUSY;
1496 goto reqbuf_err;
1498 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1499 video1_numbuffers : video2_numbuffers;
1500 for (i = num_buffers; i < vout->buffer_allocated; i++) {
1501 omap_vout_free_buffer(vout->buf_virt_addr[i],
1502 vout->buffer_size);
1503 vout->buf_virt_addr[i] = 0;
1504 vout->buf_phy_addr[i] = 0;
1506 vout->buffer_allocated = num_buffers;
1507 videobuf_mmap_free(q);
1508 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1509 if (vout->buffer_allocated) {
1510 videobuf_mmap_free(q);
1511 for (i = 0; i < vout->buffer_allocated; i++) {
1512 kfree(q->bufs[i]);
1513 q->bufs[i] = NULL;
1515 vout->buffer_allocated = 0;
1519 /*store the memory type in data structure */
1520 vout->memory = req->memory;
1522 INIT_LIST_HEAD(&vout->dma_queue);
1524 /* call videobuf_reqbufs api */
1525 ret = videobuf_reqbufs(q, req);
1526 if (ret < 0)
1527 goto reqbuf_err;
1529 vout->buffer_allocated = req->count;
1531 reqbuf_err:
1532 mutex_unlock(&vout->lock);
1533 return ret;
1536 static int vidioc_querybuf(struct file *file, void *fh,
1537 struct v4l2_buffer *b)
1539 struct omap_vout_device *vout = fh;
1541 return videobuf_querybuf(&vout->vbq, b);
1544 static int vidioc_qbuf(struct file *file, void *fh,
1545 struct v4l2_buffer *buffer)
1547 struct omap_vout_device *vout = fh;
1548 struct videobuf_queue *q = &vout->vbq;
1550 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1551 (buffer->index >= vout->buffer_allocated) ||
1552 (q->bufs[buffer->index]->memory != buffer->memory)) {
1553 return -EINVAL;
1555 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1556 if ((buffer->length < vout->pix.sizeimage) ||
1557 (0 == buffer->m.userptr)) {
1558 return -EINVAL;
1562 if ((is_rotation_enabled(vout)) &&
1563 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1564 v4l2_warn(&vout->vid_dev->v4l2_dev,
1565 "DMA Channel not allocated for Rotation\n");
1566 return -EINVAL;
1569 return videobuf_qbuf(q, buffer);
1572 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1574 struct omap_vout_device *vout = fh;
1575 struct videobuf_queue *q = &vout->vbq;
1577 int ret;
1578 u32 addr;
1579 unsigned long size;
1580 struct videobuf_buffer *vb;
1582 vb = q->bufs[b->index];
1584 if (!vout->streaming)
1585 return -EINVAL;
1587 if (file->f_flags & O_NONBLOCK)
1588 /* Call videobuf_dqbuf for non blocking mode */
1589 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
1590 else
1591 /* Call videobuf_dqbuf for blocking mode */
1592 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
1594 addr = (unsigned long) vout->buf_phy_addr[vb->i];
1595 size = (unsigned long) vb->size;
1596 dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
1597 size, DMA_TO_DEVICE);
1598 return ret;
1601 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1603 int ret = 0, j;
1604 u32 addr = 0, mask = 0;
1605 struct omap_vout_device *vout = fh;
1606 struct videobuf_queue *q = &vout->vbq;
1607 struct omapvideo_info *ovid = &vout->vid_info;
1609 mutex_lock(&vout->lock);
1611 if (vout->streaming) {
1612 ret = -EBUSY;
1613 goto streamon_err;
1616 ret = videobuf_streamon(q);
1617 if (ret)
1618 goto streamon_err;
1620 if (list_empty(&vout->dma_queue)) {
1621 ret = -EIO;
1622 goto streamon_err1;
1625 /* Get the next frame from the buffer queue */
1626 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1627 struct videobuf_buffer, queue);
1628 /* Remove buffer from the buffer queue */
1629 list_del(&vout->cur_frm->queue);
1630 /* Mark state of the current frame to active */
1631 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1632 /* Initialize field_id and started member */
1633 vout->field_id = 0;
1635 /* set flag here. Next QBUF will start DMA */
1636 vout->streaming = 1;
1638 vout->first_int = 1;
1640 if (omap_vout_calculate_offset(vout)) {
1641 ret = -EINVAL;
1642 goto streamon_err1;
1644 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1645 + vout->cropped_offset;
1647 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1648 | DISPC_IRQ_VSYNC2;
1650 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1652 for (j = 0; j < ovid->num_overlays; j++) {
1653 struct omap_overlay *ovl = ovid->overlays[j];
1655 if (ovl->manager && ovl->manager->device) {
1656 struct omap_overlay_info info;
1657 ovl->get_overlay_info(ovl, &info);
1658 info.enabled = 1;
1659 info.paddr = addr;
1660 if (ovl->set_overlay_info(ovl, &info)) {
1661 ret = -EINVAL;
1662 goto streamon_err1;
1667 /* First save the configuration in ovelray structure */
1668 ret = omapvid_init(vout, addr);
1669 if (ret)
1670 v4l2_err(&vout->vid_dev->v4l2_dev,
1671 "failed to set overlay info\n");
1672 /* Enable the pipeline and set the Go bit */
1673 ret = omapvid_apply_changes(vout);
1674 if (ret)
1675 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1677 ret = 0;
1679 streamon_err1:
1680 if (ret)
1681 ret = videobuf_streamoff(q);
1682 streamon_err:
1683 mutex_unlock(&vout->lock);
1684 return ret;
1687 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1689 u32 mask = 0;
1690 int ret = 0, j;
1691 struct omap_vout_device *vout = fh;
1692 struct omapvideo_info *ovid = &vout->vid_info;
1694 if (!vout->streaming)
1695 return -EINVAL;
1697 vout->streaming = 0;
1698 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1699 | DISPC_IRQ_VSYNC2;
1701 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1703 for (j = 0; j < ovid->num_overlays; j++) {
1704 struct omap_overlay *ovl = ovid->overlays[j];
1706 if (ovl->manager && ovl->manager->device) {
1707 struct omap_overlay_info info;
1709 ovl->get_overlay_info(ovl, &info);
1710 info.enabled = 0;
1711 ret = ovl->set_overlay_info(ovl, &info);
1712 if (ret)
1713 v4l2_err(&vout->vid_dev->v4l2_dev,
1714 "failed to update overlay info in streamoff\n");
1718 /* Turn of the pipeline */
1719 ret = omapvid_apply_changes(vout);
1720 if (ret)
1721 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
1722 " streamoff\n");
1724 INIT_LIST_HEAD(&vout->dma_queue);
1725 ret = videobuf_streamoff(&vout->vbq);
1727 return ret;
1730 static int vidioc_s_fbuf(struct file *file, void *fh,
1731 struct v4l2_framebuffer *a)
1733 int enable = 0;
1734 struct omap_overlay *ovl;
1735 struct omapvideo_info *ovid;
1736 struct omap_vout_device *vout = fh;
1737 struct omap_overlay_manager_info info;
1738 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1740 ovid = &vout->vid_info;
1741 ovl = ovid->overlays[0];
1743 /* OMAP DSS doesn't support Source and Destination color
1744 key together */
1745 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1746 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1747 return -EINVAL;
1748 /* OMAP DSS Doesn't support the Destination color key
1749 and alpha blending together */
1750 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1751 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1752 return -EINVAL;
1754 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1755 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1756 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
1757 } else
1758 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1760 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1761 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1762 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1763 } else
1764 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
1766 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1767 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1768 enable = 1;
1769 else
1770 enable = 0;
1771 if (ovl->manager && ovl->manager->get_manager_info &&
1772 ovl->manager->set_manager_info) {
1774 ovl->manager->get_manager_info(ovl->manager, &info);
1775 info.trans_enabled = enable;
1776 info.trans_key_type = key_type;
1777 info.trans_key = vout->win.chromakey;
1779 if (ovl->manager->set_manager_info(ovl->manager, &info))
1780 return -EINVAL;
1782 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1783 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1784 enable = 1;
1785 } else {
1786 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1787 enable = 0;
1789 if (ovl->manager && ovl->manager->get_manager_info &&
1790 ovl->manager->set_manager_info) {
1791 ovl->manager->get_manager_info(ovl->manager, &info);
1792 info.alpha_enabled = enable;
1793 if (ovl->manager->set_manager_info(ovl->manager, &info))
1794 return -EINVAL;
1797 return 0;
1800 static int vidioc_g_fbuf(struct file *file, void *fh,
1801 struct v4l2_framebuffer *a)
1803 struct omap_overlay *ovl;
1804 struct omapvideo_info *ovid;
1805 struct omap_vout_device *vout = fh;
1806 struct omap_overlay_manager_info info;
1808 ovid = &vout->vid_info;
1809 ovl = ovid->overlays[0];
1811 a->flags = 0x0;
1812 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1813 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1815 if (ovl->manager && ovl->manager->get_manager_info) {
1816 ovl->manager->get_manager_info(ovl->manager, &info);
1817 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1818 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1819 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1820 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1822 if (ovl->manager && ovl->manager->get_manager_info) {
1823 ovl->manager->get_manager_info(ovl->manager, &info);
1824 if (info.alpha_enabled)
1825 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1828 return 0;
1831 static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1832 .vidioc_querycap = vidioc_querycap,
1833 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1834 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1835 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1836 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1837 .vidioc_queryctrl = vidioc_queryctrl,
1838 .vidioc_g_ctrl = vidioc_g_ctrl,
1839 .vidioc_s_fbuf = vidioc_s_fbuf,
1840 .vidioc_g_fbuf = vidioc_g_fbuf,
1841 .vidioc_s_ctrl = vidioc_s_ctrl,
1842 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1843 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1844 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1845 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1846 .vidioc_cropcap = vidioc_cropcap,
1847 .vidioc_g_crop = vidioc_g_crop,
1848 .vidioc_s_crop = vidioc_s_crop,
1849 .vidioc_reqbufs = vidioc_reqbufs,
1850 .vidioc_querybuf = vidioc_querybuf,
1851 .vidioc_qbuf = vidioc_qbuf,
1852 .vidioc_dqbuf = vidioc_dqbuf,
1853 .vidioc_streamon = vidioc_streamon,
1854 .vidioc_streamoff = vidioc_streamoff,
1857 static const struct v4l2_file_operations omap_vout_fops = {
1858 .owner = THIS_MODULE,
1859 .unlocked_ioctl = video_ioctl2,
1860 .mmap = omap_vout_mmap,
1861 .open = omap_vout_open,
1862 .release = omap_vout_release,
1865 /* Init functions used during driver initialization */
1866 /* Initial setup of video_data */
1867 static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1869 struct video_device *vfd;
1870 struct v4l2_pix_format *pix;
1871 struct v4l2_control *control;
1872 struct omap_dss_device *display =
1873 vout->vid_info.overlays[0]->manager->device;
1875 /* set the default pix */
1876 pix = &vout->pix;
1878 /* Set the default picture of QVGA */
1879 pix->width = QQVGA_WIDTH;
1880 pix->height = QQVGA_HEIGHT;
1882 /* Default pixel format is RGB 5-6-5 */
1883 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1884 pix->field = V4L2_FIELD_ANY;
1885 pix->bytesperline = pix->width * 2;
1886 pix->sizeimage = pix->bytesperline * pix->height;
1887 pix->priv = 0;
1888 pix->colorspace = V4L2_COLORSPACE_JPEG;
1890 vout->bpp = RGB565_BPP;
1891 vout->fbuf.fmt.width = display->panel.timings.x_res;
1892 vout->fbuf.fmt.height = display->panel.timings.y_res;
1894 /* Set the data structures for the overlay parameters*/
1895 vout->win.global_alpha = 255;
1896 vout->fbuf.flags = 0;
1897 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1898 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1899 vout->win.chromakey = 0;
1901 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1903 /*Initialize the control variables for
1904 rotation, flipping and background color. */
1905 control = vout->control;
1906 control[0].id = V4L2_CID_ROTATE;
1907 control[0].value = 0;
1908 vout->rotation = 0;
1909 vout->mirror = 0;
1910 vout->control[2].id = V4L2_CID_HFLIP;
1911 vout->control[2].value = 0;
1912 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1913 vout->vrfb_bpp = 2;
1915 control[1].id = V4L2_CID_BG_COLOR;
1916 control[1].value = 0;
1918 /* initialize the video_device struct */
1919 vfd = vout->vfd = video_device_alloc();
1921 if (!vfd) {
1922 printk(KERN_ERR VOUT_NAME ": could not allocate"
1923 " video device struct\n");
1924 return -ENOMEM;
1926 vfd->release = video_device_release;
1927 vfd->ioctl_ops = &vout_ioctl_ops;
1929 strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1931 vfd->fops = &omap_vout_fops;
1932 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
1933 mutex_init(&vout->lock);
1935 vfd->minor = -1;
1936 return 0;
1940 /* Setup video buffers */
1941 static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1942 int vid_num)
1944 u32 numbuffers;
1945 int ret = 0, i;
1946 struct omapvideo_info *ovid;
1947 struct omap_vout_device *vout;
1948 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1949 struct omap2video_device *vid_dev =
1950 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1952 vout = vid_dev->vouts[vid_num];
1953 ovid = &vout->vid_info;
1955 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1956 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1957 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1959 for (i = 0; i < numbuffers; i++) {
1960 vout->buf_virt_addr[i] =
1961 omap_vout_alloc_buffer(vout->buffer_size,
1962 (u32 *) &vout->buf_phy_addr[i]);
1963 if (!vout->buf_virt_addr[i]) {
1964 numbuffers = i;
1965 ret = -ENOMEM;
1966 goto free_buffers;
1970 vout->cropped_offset = 0;
1972 if (ovid->rotation_type == VOUT_ROT_VRFB) {
1973 int static_vrfb_allocation = (vid_num == 0) ?
1974 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1975 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1976 static_vrfb_allocation);
1979 return ret;
1981 free_buffers:
1982 for (i = 0; i < numbuffers; i++) {
1983 omap_vout_free_buffer(vout->buf_virt_addr[i],
1984 vout->buffer_size);
1985 vout->buf_virt_addr[i] = 0;
1986 vout->buf_phy_addr[i] = 0;
1988 return ret;
1992 /* Create video out devices */
1993 static int __init omap_vout_create_video_devices(struct platform_device *pdev)
1995 int ret = 0, k;
1996 struct omap_vout_device *vout;
1997 struct video_device *vfd = NULL;
1998 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1999 struct omap2video_device *vid_dev = container_of(v4l2_dev,
2000 struct omap2video_device, v4l2_dev);
2002 for (k = 0; k < pdev->num_resources; k++) {
2004 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
2005 if (!vout) {
2006 dev_err(&pdev->dev, ": could not allocate memory\n");
2007 return -ENOMEM;
2010 vout->vid = k;
2011 vid_dev->vouts[k] = vout;
2012 vout->vid_dev = vid_dev;
2013 /* Select video2 if only 1 overlay is controlled by V4L2 */
2014 if (pdev->num_resources == 1)
2015 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2016 else
2017 /* Else select video1 and video2 one by one. */
2018 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2019 vout->vid_info.num_overlays = 1;
2020 vout->vid_info.id = k + 1;
2022 /* Set VRFB as rotation_type for omap2 and omap3 */
2023 if (cpu_is_omap24xx() || cpu_is_omap34xx())
2024 vout->vid_info.rotation_type = VOUT_ROT_VRFB;
2026 /* Setup the default configuration for the video devices
2028 if (omap_vout_setup_video_data(vout) != 0) {
2029 ret = -ENOMEM;
2030 goto error;
2033 /* Allocate default number of buffers for the video streaming
2034 * and reserve the VRFB space for rotation
2036 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2037 ret = -ENOMEM;
2038 goto error1;
2041 /* Register the Video device with V4L2
2043 vfd = vout->vfd;
2044 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
2045 dev_err(&pdev->dev, ": Could not register "
2046 "Video for Linux device\n");
2047 vfd->minor = -1;
2048 ret = -ENODEV;
2049 goto error2;
2051 video_set_drvdata(vfd, vout);
2053 /* Configure the overlay structure */
2054 ret = omapvid_init(vid_dev->vouts[k], 0);
2055 if (!ret)
2056 goto success;
2058 error2:
2059 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2060 omap_vout_release_vrfb(vout);
2061 omap_vout_free_buffers(vout);
2062 error1:
2063 video_device_release(vfd);
2064 error:
2065 kfree(vout);
2066 return ret;
2068 success:
2069 dev_info(&pdev->dev, ": registered and initialized"
2070 " video device %d\n", vfd->minor);
2071 if (k == (pdev->num_resources - 1))
2072 return 0;
2075 return -ENODEV;
2077 /* Driver functions */
2078 static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2080 struct video_device *vfd;
2081 struct omapvideo_info *ovid;
2083 if (!vout)
2084 return;
2086 vfd = vout->vfd;
2087 ovid = &vout->vid_info;
2088 if (vfd) {
2089 if (!video_is_registered(vfd)) {
2091 * The device was never registered, so release the
2092 * video_device struct directly.
2094 video_device_release(vfd);
2095 } else {
2097 * The unregister function will release the video_device
2098 * struct as well as unregistering it.
2100 video_unregister_device(vfd);
2103 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2104 omap_vout_release_vrfb(vout);
2105 /* Free the VRFB buffer if allocated
2106 * init time
2108 if (vout->vrfb_static_allocation)
2109 omap_vout_free_vrfb_buffers(vout);
2111 omap_vout_free_buffers(vout);
2113 kfree(vout);
2116 static int omap_vout_remove(struct platform_device *pdev)
2118 int k;
2119 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2120 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2121 omap2video_device, v4l2_dev);
2123 v4l2_device_unregister(v4l2_dev);
2124 for (k = 0; k < pdev->num_resources; k++)
2125 omap_vout_cleanup_device(vid_dev->vouts[k]);
2127 for (k = 0; k < vid_dev->num_displays; k++) {
2128 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
2129 vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
2131 omap_dss_put_device(vid_dev->displays[k]);
2133 kfree(vid_dev);
2134 return 0;
2137 static int __init omap_vout_probe(struct platform_device *pdev)
2139 int ret = 0, i;
2140 struct omap_overlay *ovl;
2141 struct omap_dss_device *dssdev = NULL;
2142 struct omap_dss_device *def_display;
2143 struct omap2video_device *vid_dev = NULL;
2145 if (pdev->num_resources == 0) {
2146 dev_err(&pdev->dev, "probed for an unknown device\n");
2147 return -ENODEV;
2150 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2151 if (vid_dev == NULL)
2152 return -ENOMEM;
2154 vid_dev->num_displays = 0;
2155 for_each_dss_dev(dssdev) {
2156 omap_dss_get_device(dssdev);
2157 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2160 if (vid_dev->num_displays == 0) {
2161 dev_err(&pdev->dev, "no displays\n");
2162 ret = -EINVAL;
2163 goto probe_err0;
2166 vid_dev->num_overlays = omap_dss_get_num_overlays();
2167 for (i = 0; i < vid_dev->num_overlays; i++)
2168 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2170 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2171 for (i = 0; i < vid_dev->num_managers; i++)
2172 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2174 /* Get the Video1 overlay and video2 overlay.
2175 * Setup the Display attached to that overlays
2177 for (i = 1; i < vid_dev->num_overlays; i++) {
2178 ovl = omap_dss_get_overlay(i);
2179 if (ovl->manager && ovl->manager->device) {
2180 def_display = ovl->manager->device;
2181 } else {
2182 dev_warn(&pdev->dev, "cannot find display\n");
2183 def_display = NULL;
2185 if (def_display) {
2186 struct omap_dss_driver *dssdrv = def_display->driver;
2188 ret = dssdrv->enable(def_display);
2189 if (ret) {
2190 /* Here we are not considering a error
2191 * as display may be enabled by frame
2192 * buffer driver
2194 dev_warn(&pdev->dev,
2195 "'%s' Display already enabled\n",
2196 def_display->name);
2201 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2202 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2203 ret = -ENODEV;
2204 goto probe_err1;
2207 ret = omap_vout_create_video_devices(pdev);
2208 if (ret)
2209 goto probe_err2;
2211 for (i = 0; i < vid_dev->num_displays; i++) {
2212 struct omap_dss_device *display = vid_dev->displays[i];
2214 if (display->driver->update)
2215 display->driver->update(display, 0, 0,
2216 display->panel.timings.x_res,
2217 display->panel.timings.y_res);
2219 return 0;
2221 probe_err2:
2222 v4l2_device_unregister(&vid_dev->v4l2_dev);
2223 probe_err1:
2224 for (i = 1; i < vid_dev->num_overlays; i++) {
2225 def_display = NULL;
2226 ovl = omap_dss_get_overlay(i);
2227 if (ovl->manager && ovl->manager->device)
2228 def_display = ovl->manager->device;
2230 if (def_display && def_display->driver)
2231 def_display->driver->disable(def_display);
2233 probe_err0:
2234 kfree(vid_dev);
2235 return ret;
2238 static struct platform_driver omap_vout_driver = {
2239 .driver = {
2240 .name = VOUT_NAME,
2242 .probe = omap_vout_probe,
2243 .remove = omap_vout_remove,
2246 static int __init omap_vout_init(void)
2248 if (platform_driver_register(&omap_vout_driver) != 0) {
2249 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2250 return -EINVAL;
2252 return 0;
2255 static void omap_vout_cleanup(void)
2257 platform_driver_unregister(&omap_vout_driver);
2260 late_initcall(omap_vout_init);
2261 module_exit(omap_vout_cleanup);