[media] media: video: s5p-tv: fix build break
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / media / video / omap / omap_vout.c
CommitLineData
5c7ab634
VH
1/*
2 * omap_vout.c
3 *
4 * Copyright (C) 2005-2010 Texas Instruments.
5 *
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.
9 *
10 * Leveraged code from the OMAP2 camera driver
11 * Video-for-Linux (Version 2) camera capture driver for
12 * the OMAP24xx camera controller.
13 *
14 * Author: Andy Lowe (source@mvista.com)
15 *
16 * Copyright (C) 2004 MontaVista Software, Inc.
17 * Copyright (C) 2010 Texas Instruments.
18 *
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
25 *
26 * 17-Nov-2008 Hardik Changed driver to use video_ioctl2
27 *
28 * 23-Feb-2010 Vaibhav H Modified to use new DSS2 interface
29 *
30 */
31
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>
5c7ab634
VH
38#include <linux/irq.h>
39#include <linux/videodev2.h>
72915e85 40#include <linux/dma-mapping.h>
5c7ab634 41
dd880dd4 42#include <media/videobuf-dma-contig.h>
5c7ab634
VH
43#include <media/v4l2-device.h>
44#include <media/v4l2-ioctl.h>
45
46#include <plat/dma.h>
5c7ab634 47#include <plat/vrfb.h>
a0b38cc4 48#include <video/omapdss.h>
5c7ab634
VH
49
50#include "omap_voutlib.h"
51#include "omap_voutdef.h"
445e258f 52#include "omap_vout_vrfb.h"
5c7ab634
VH
53
54MODULE_AUTHOR("Texas Instruments");
55MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
56MODULE_LICENSE("GPL");
57
5c7ab634
VH
58/* Driver Configuration macros */
59#define VOUT_NAME "omap_vout"
60
61enum omap_vout_channels {
62 OMAP_VIDEO1,
63 OMAP_VIDEO2,
64};
65
5c7ab634
VH
66static struct videobuf_queue_ops video_vbq_ops;
67/* Variables configurable through module params*/
68static u32 video1_numbuffers = 3;
69static u32 video2_numbuffers = 3;
70static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
71static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
72static u32 vid1_static_vrfb_alloc;
73static u32 vid2_static_vrfb_alloc;
74static int debug;
75
76/* Module parameters */
77module_param(video1_numbuffers, uint, S_IRUGO);
78MODULE_PARM_DESC(video1_numbuffers,
79 "Number of buffers to be allocated at init time for Video1 device.");
80
81module_param(video2_numbuffers, uint, S_IRUGO);
82MODULE_PARM_DESC(video2_numbuffers,
83 "Number of buffers to be allocated at init time for Video2 device.");
84
85module_param(video1_bufsize, uint, S_IRUGO);
86MODULE_PARM_DESC(video1_bufsize,
87 "Size of the buffer to be allocated for video1 device");
88
89module_param(video2_bufsize, uint, S_IRUGO);
90MODULE_PARM_DESC(video2_bufsize,
91 "Size of the buffer to be allocated for video2 device");
92
93module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
94MODULE_PARM_DESC(vid1_static_vrfb_alloc,
95 "Static allocation of the VRFB buffer for video1 device");
96
97module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
98MODULE_PARM_DESC(vid2_static_vrfb_alloc,
99 "Static allocation of the VRFB buffer for video2 device");
100
101module_param(debug, bool, S_IRUGO);
102MODULE_PARM_DESC(debug, "Debug level (0-1)");
103
104/* list of image formats supported by OMAP2 video pipelines */
0d334f7f 105static const struct v4l2_fmtdesc omap_formats[] = {
5c7ab634
VH
106 {
107 /* Note: V4L2 defines RGB565 as:
108 *
109 * Byte 0 Byte 1
110 * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
111 *
112 * We interpret RGB565 as:
113 *
114 * Byte 0 Byte 1
115 * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
116 */
117 .description = "RGB565, le",
118 .pixelformat = V4L2_PIX_FMT_RGB565,
119 },
120 {
121 /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
122 * this for RGB24 unpack mode, the last 8 bits are ignored
123 * */
124 .description = "RGB32, le",
125 .pixelformat = V4L2_PIX_FMT_RGB32,
126 },
127 {
128 /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
129 * this for RGB24 packed mode
130 *
131 */
132 .description = "RGB24, le",
133 .pixelformat = V4L2_PIX_FMT_RGB24,
134 },
135 {
136 .description = "YUYV (YUV 4:2:2), packed",
137 .pixelformat = V4L2_PIX_FMT_YUYV,
138 },
139 {
140 .description = "UYVY, packed",
141 .pixelformat = V4L2_PIX_FMT_UYVY,
142 },
143};
144
145#define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
146
5c7ab634
VH
147/*
148 * Try format
149 */
150static int omap_vout_try_format(struct v4l2_pix_format *pix)
151{
152 int ifmt, bpp = 0;
153
154 pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
155 (u32)VID_MAX_HEIGHT);
156 pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
157
158 for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
159 if (pix->pixelformat == omap_formats[ifmt].pixelformat)
160 break;
161 }
162
163 if (ifmt == NUM_OUTPUT_FORMATS)
164 ifmt = 0;
165
166 pix->pixelformat = omap_formats[ifmt].pixelformat;
167 pix->field = V4L2_FIELD_ANY;
168 pix->priv = 0;
169
170 switch (pix->pixelformat) {
171 case V4L2_PIX_FMT_YUYV:
172 case V4L2_PIX_FMT_UYVY:
173 default:
174 pix->colorspace = V4L2_COLORSPACE_JPEG;
175 bpp = YUYV_BPP;
176 break;
177 case V4L2_PIX_FMT_RGB565:
178 case V4L2_PIX_FMT_RGB565X:
179 pix->colorspace = V4L2_COLORSPACE_SRGB;
180 bpp = RGB565_BPP;
181 break;
182 case V4L2_PIX_FMT_RGB24:
183 pix->colorspace = V4L2_COLORSPACE_SRGB;
184 bpp = RGB24_BPP;
185 break;
186 case V4L2_PIX_FMT_RGB32:
187 case V4L2_PIX_FMT_BGR32:
188 pix->colorspace = V4L2_COLORSPACE_SRGB;
189 bpp = RGB32_BPP;
190 break;
191 }
192 pix->bytesperline = pix->width * bpp;
193 pix->sizeimage = pix->bytesperline * pix->height;
194
195 return bpp;
196}
197
198/*
199 * omap_vout_uservirt_to_phys: This inline function is used to convert user
200 * space virtual address to physical address.
201 */
202static u32 omap_vout_uservirt_to_phys(u32 virtp)
203{
204 unsigned long physp = 0;
205 struct vm_area_struct *vma;
206 struct mm_struct *mm = current->mm;
207
208 vma = find_vma(mm, virtp);
209 /* For kernel direct-mapped memory, take the easy way */
210 if (virtp >= PAGE_OFFSET) {
211 physp = virt_to_phys((void *) virtp);
212 } else if (vma && (vma->vm_flags & VM_IO) && vma->vm_pgoff) {
213 /* this will catch, kernel-allocated, mmaped-to-usermode
214 addresses */
215 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
216 } else {
217 /* otherwise, use get_user_pages() for general userland pages */
218 int res, nr_pages = 1;
219 struct page *pages;
220 down_read(&current->mm->mmap_sem);
221
222 res = get_user_pages(current, current->mm, virtp, nr_pages, 1,
223 0, &pages, NULL);
224 up_read(&current->mm->mmap_sem);
225
226 if (res == nr_pages) {
227 physp = __pa(page_address(&pages[0]) +
228 (virtp & ~PAGE_MASK));
229 } else {
230 printk(KERN_WARNING VOUT_NAME
231 "get_user_pages failed\n");
232 return 0;
233 }
234 }
235
236 return physp;
237}
238
5c7ab634
VH
239/*
240 * Free the V4L2 buffers
241 */
445e258f 242void omap_vout_free_buffers(struct omap_vout_device *vout)
5c7ab634
VH
243{
244 int i, numbuffers;
245
246 /* Allocate memory for the buffers */
247 numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
248 vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
249
250 for (i = 0; i < numbuffers; i++) {
251 omap_vout_free_buffer(vout->buf_virt_addr[i],
252 vout->buffer_size);
253 vout->buf_phy_addr[i] = 0;
254 vout->buf_virt_addr[i] = 0;
255 }
256}
257
5c7ab634
VH
258/*
259 * Convert V4L2 rotation to DSS rotation
260 * V4L2 understand 0, 90, 180, 270.
25985edc 261 * Convert to 0, 1, 2 and 3 respectively for DSS
5c7ab634
VH
262 */
263static int v4l2_rot_to_dss_rot(int v4l2_rotation,
264 enum dss_rotation *rotation, bool mirror)
265{
266 int ret = 0;
267
268 switch (v4l2_rotation) {
269 case 90:
270 *rotation = dss_rotation_90_degree;
271 break;
272 case 180:
273 *rotation = dss_rotation_180_degree;
274 break;
275 case 270:
276 *rotation = dss_rotation_270_degree;
277 break;
278 case 0:
279 *rotation = dss_rotation_0_degree;
280 break;
281 default:
282 ret = -EINVAL;
283 }
284 return ret;
285}
286
5c7ab634
VH
287static int omap_vout_calculate_offset(struct omap_vout_device *vout)
288{
5c7ab634 289 struct omapvideo_info *ovid;
5c7ab634
VH
290 struct v4l2_rect *crop = &vout->crop;
291 struct v4l2_pix_format *pix = &vout->pix;
292 int *cropped_offset = &vout->cropped_offset;
445e258f 293 int ps = 2, line_length = 0;
5c7ab634
VH
294
295 ovid = &vout->vid_info;
5c7ab634 296
445e258f
AT
297 if (ovid->rotation_type == VOUT_ROT_VRFB) {
298 omap_vout_calculate_vrfb_offset(vout);
299 } else {
300 vout->line_length = line_length = pix->width;
5c7ab634 301
445e258f
AT
302 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
303 V4L2_PIX_FMT_UYVY == pix->pixelformat)
304 ps = 2;
305 else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
5c7ab634 306 ps = 4;
445e258f
AT
307 else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
308 ps = 3;
5c7ab634 309
445e258f
AT
310 vout->ps = ps;
311
312 *cropped_offset = (line_length * ps) *
313 crop->top + crop->left * ps;
5c7ab634 314 }
445e258f 315
5c7ab634 316 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
445e258f
AT
317 __func__, vout->cropped_offset);
318
5c7ab634
VH
319 return 0;
320}
321
322/*
323 * Convert V4L2 pixel format to DSS pixel format
324 */
72fcf2a8 325static int video_mode_to_dss_mode(struct omap_vout_device *vout)
5c7ab634
VH
326{
327 struct omap_overlay *ovl;
328 struct omapvideo_info *ovid;
329 struct v4l2_pix_format *pix = &vout->pix;
330 enum omap_color_mode mode;
331
332 ovid = &vout->vid_info;
333 ovl = ovid->overlays[0];
334
335 switch (pix->pixelformat) {
336 case 0:
337 break;
338 case V4L2_PIX_FMT_YUYV:
339 mode = OMAP_DSS_COLOR_YUV2;
340 break;
341 case V4L2_PIX_FMT_UYVY:
342 mode = OMAP_DSS_COLOR_UYVY;
343 break;
344 case V4L2_PIX_FMT_RGB565:
345 mode = OMAP_DSS_COLOR_RGB16;
346 break;
347 case V4L2_PIX_FMT_RGB24:
348 mode = OMAP_DSS_COLOR_RGB24P;
349 break;
350 case V4L2_PIX_FMT_RGB32:
351 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
352 OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
353 break;
354 case V4L2_PIX_FMT_BGR32:
355 mode = OMAP_DSS_COLOR_RGBX32;
356 break;
357 default:
358 mode = -EINVAL;
359 }
360 return mode;
361}
362
363/*
364 * Setup the overlay
365 */
a137ac87 366static int omapvid_setup_overlay(struct omap_vout_device *vout,
5c7ab634
VH
367 struct omap_overlay *ovl, int posx, int posy, int outw,
368 int outh, u32 addr)
369{
370 int ret = 0;
371 struct omap_overlay_info info;
372 int cropheight, cropwidth, pixheight, pixwidth;
373
374 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
375 (outw != vout->pix.width || outh != vout->pix.height)) {
376 ret = -EINVAL;
377 goto setup_ovl_err;
378 }
379
380 vout->dss_mode = video_mode_to_dss_mode(vout);
381 if (vout->dss_mode == -EINVAL) {
382 ret = -EINVAL;
383 goto setup_ovl_err;
384 }
385
386 /* Setup the input plane parameters according to
387 * rotation value selected.
388 */
b366888a 389 if (is_rotation_90_or_270(vout)) {
5c7ab634
VH
390 cropheight = vout->crop.width;
391 cropwidth = vout->crop.height;
392 pixheight = vout->pix.width;
393 pixwidth = vout->pix.height;
394 } else {
395 cropheight = vout->crop.height;
396 cropwidth = vout->crop.width;
397 pixheight = vout->pix.height;
398 pixwidth = vout->pix.width;
399 }
400
401 ovl->get_overlay_info(ovl, &info);
402 info.paddr = addr;
5c7ab634
VH
403 info.width = cropwidth;
404 info.height = cropheight;
405 info.color_mode = vout->dss_mode;
406 info.mirror = vout->mirror;
407 info.pos_x = posx;
408 info.pos_y = posy;
409 info.out_width = outw;
410 info.out_height = outh;
411 info.global_alpha = vout->win.global_alpha;
b366888a 412 if (!is_rotation_enabled(vout)) {
5c7ab634
VH
413 info.rotation = 0;
414 info.rotation_type = OMAP_DSS_ROT_DMA;
415 info.screen_width = pixwidth;
416 } else {
417 info.rotation = vout->rotation;
418 info.rotation_type = OMAP_DSS_ROT_VRFB;
419 info.screen_width = 2048;
420 }
421
422 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
423 "%s enable=%d addr=%x width=%d\n height=%d color_mode=%d\n"
424 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
425 "out_height=%d rotation_type=%d screen_width=%d\n",
426 __func__, info.enabled, info.paddr, info.width, info.height,
427 info.color_mode, info.rotation, info.mirror, info.pos_x,
428 info.pos_y, info.out_width, info.out_height, info.rotation_type,
429 info.screen_width);
430
431 ret = ovl->set_overlay_info(ovl, &info);
432 if (ret)
433 goto setup_ovl_err;
434
435 return 0;
436
437setup_ovl_err:
438 v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
439 return ret;
440}
441
442/*
443 * Initialize the overlay structure
444 */
a137ac87 445static int omapvid_init(struct omap_vout_device *vout, u32 addr)
5c7ab634
VH
446{
447 int ret = 0, i;
448 struct v4l2_window *win;
449 struct omap_overlay *ovl;
450 int posx, posy, outw, outh, temp;
451 struct omap_video_timings *timing;
452 struct omapvideo_info *ovid = &vout->vid_info;
453
454 win = &vout->win;
455 for (i = 0; i < ovid->num_overlays; i++) {
456 ovl = ovid->overlays[i];
457 if (!ovl->manager || !ovl->manager->device)
458 return -EINVAL;
459
460 timing = &ovl->manager->device->panel.timings;
461
462 outw = win->w.width;
463 outh = win->w.height;
464 switch (vout->rotation) {
465 case dss_rotation_90_degree:
466 /* Invert the height and width for 90
467 * and 270 degree rotation
468 */
469 temp = outw;
470 outw = outh;
471 outh = temp;
472 posy = (timing->y_res - win->w.width) - win->w.left;
473 posx = win->w.top;
474 break;
475
476 case dss_rotation_180_degree:
477 posx = (timing->x_res - win->w.width) - win->w.left;
478 posy = (timing->y_res - win->w.height) - win->w.top;
479 break;
480
481 case dss_rotation_270_degree:
482 temp = outw;
483 outw = outh;
484 outh = temp;
485 posy = win->w.left;
486 posx = (timing->x_res - win->w.height) - win->w.top;
487 break;
488
489 default:
490 posx = win->w.left;
491 posy = win->w.top;
492 break;
493 }
494
495 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
496 outw, outh, addr);
497 if (ret)
498 goto omapvid_init_err;
499 }
500 return 0;
501
502omapvid_init_err:
503 v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
504 return ret;
505}
506
507/*
508 * Apply the changes set the go bit of DSS
509 */
a137ac87 510static int omapvid_apply_changes(struct omap_vout_device *vout)
5c7ab634
VH
511{
512 int i;
513 struct omap_overlay *ovl;
514 struct omapvideo_info *ovid = &vout->vid_info;
515
516 for (i = 0; i < ovid->num_overlays; i++) {
517 ovl = ovid->overlays[i];
518 if (!ovl->manager || !ovl->manager->device)
519 return -EINVAL;
520 ovl->manager->apply(ovl->manager);
521 }
522
523 return 0;
524}
525
a137ac87 526static void omap_vout_isr(void *arg, unsigned int irqstatus)
5c7ab634
VH
527{
528 int ret;
529 u32 addr, fid;
530 struct omap_overlay *ovl;
531 struct timeval timevalue;
532 struct omapvideo_info *ovid;
533 struct omap_dss_device *cur_display;
534 struct omap_vout_device *vout = (struct omap_vout_device *)arg;
535
536 if (!vout->streaming)
537 return;
538
539 ovid = &vout->vid_info;
540 ovl = ovid->overlays[0];
541 /* get the display device attached to the overlay */
542 if (!ovl->manager || !ovl->manager->device)
543 return;
544
545 cur_display = ovl->manager->device;
546
547 spin_lock(&vout->vbq_lock);
548 do_gettimeofday(&timevalue);
5c7ab634 549
5251dd6c
AJ
550 if (cur_display->type != OMAP_DISPLAY_TYPE_VENC) {
551 switch (cur_display->type) {
552 case OMAP_DISPLAY_TYPE_DPI:
553 if (!(irqstatus & (DISPC_IRQ_VSYNC | DISPC_IRQ_VSYNC2)))
554 goto vout_isr_err;
555 break;
556 case OMAP_DISPLAY_TYPE_HDMI:
557 if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
558 goto vout_isr_err;
559 break;
560 default:
561 goto vout_isr_err;
562 }
5c7ab634
VH
563 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
564 vout->cur_frm->ts = timevalue;
565 vout->cur_frm->state = VIDEOBUF_DONE;
566 wake_up_interruptible(&vout->cur_frm->done);
567 vout->cur_frm = vout->next_frm;
568 }
569 vout->first_int = 0;
570 if (list_empty(&vout->dma_queue))
571 goto vout_isr_err;
572
573 vout->next_frm = list_entry(vout->dma_queue.next,
574 struct videobuf_buffer, queue);
575 list_del(&vout->next_frm->queue);
576
577 vout->next_frm->state = VIDEOBUF_ACTIVE;
578
579 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
580 + vout->cropped_offset;
581
582 /* First save the configuration in ovelray structure */
583 ret = omapvid_init(vout, addr);
584 if (ret)
585 printk(KERN_ERR VOUT_NAME
5251dd6c 586 "failed to set overlay info\n");
5c7ab634
VH
587 /* Enable the pipeline and set the Go bit */
588 ret = omapvid_apply_changes(vout);
589 if (ret)
590 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
591 } else {
592
593 if (vout->first_int) {
594 vout->first_int = 0;
595 goto vout_isr_err;
596 }
597 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
598 fid = 1;
599 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
600 fid = 0;
601 else
602 goto vout_isr_err;
603
604 vout->field_id ^= 1;
605 if (fid != vout->field_id) {
606 if (0 == fid)
607 vout->field_id = fid;
608
609 goto vout_isr_err;
610 }
611 if (0 == fid) {
612 if (vout->cur_frm == vout->next_frm)
613 goto vout_isr_err;
614
615 vout->cur_frm->ts = timevalue;
616 vout->cur_frm->state = VIDEOBUF_DONE;
617 wake_up_interruptible(&vout->cur_frm->done);
618 vout->cur_frm = vout->next_frm;
619 } else if (1 == fid) {
620 if (list_empty(&vout->dma_queue) ||
621 (vout->cur_frm != vout->next_frm))
622 goto vout_isr_err;
623
624 vout->next_frm = list_entry(vout->dma_queue.next,
625 struct videobuf_buffer, queue);
626 list_del(&vout->next_frm->queue);
627
628 vout->next_frm->state = VIDEOBUF_ACTIVE;
629 addr = (unsigned long)
630 vout->queued_buf_addr[vout->next_frm->i] +
631 vout->cropped_offset;
632 /* First save the configuration in ovelray structure */
633 ret = omapvid_init(vout, addr);
634 if (ret)
635 printk(KERN_ERR VOUT_NAME
636 "failed to set overlay info\n");
637 /* Enable the pipeline and set the Go bit */
638 ret = omapvid_apply_changes(vout);
639 if (ret)
640 printk(KERN_ERR VOUT_NAME
641 "failed to change mode\n");
642 }
643
644 }
645
646vout_isr_err:
647 spin_unlock(&vout->vbq_lock);
648}
649
650
651/* Video buffer call backs */
652
653/*
654 * Buffer setup function is called by videobuf layer when REQBUF ioctl is
655 * called. This is used to setup buffers and return size and count of
656 * buffers allocated. After the call to this buffer, videobuf layer will
657 * setup buffer queue depending on the size and count of buffers
658 */
659static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
660 unsigned int *size)
661{
662 int startindex = 0, i, j;
663 u32 phy_addr = 0, virt_addr = 0;
664 struct omap_vout_device *vout = q->priv_data;
445e258f 665 struct omapvideo_info *ovid = &vout->vid_info;
5c7ab634
VH
666
667 if (!vout)
668 return -EINVAL;
669
670 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
671 return -EINVAL;
672
673 startindex = (vout->vid == OMAP_VIDEO1) ?
674 video1_numbuffers : video2_numbuffers;
675 if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
676 *count = startindex;
677
445e258f 678 if (ovid->rotation_type == VOUT_ROT_VRFB) {
5c7ab634
VH
679 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
680 return -ENOMEM;
445e258f 681 }
5c7ab634
VH
682
683 if (V4L2_MEMORY_MMAP != vout->memory)
684 return 0;
685
686 /* Now allocated the V4L2 buffers */
687 *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
688 startindex = (vout->vid == OMAP_VIDEO1) ?
689 video1_numbuffers : video2_numbuffers;
690
383e4f69
VH
691 /* Check the size of the buffer */
692 if (*size > vout->buffer_size) {
693 v4l2_err(&vout->vid_dev->v4l2_dev,
694 "buffer allocation mismatch [%u] [%u]\n",
695 *size, vout->buffer_size);
696 return -ENOMEM;
697 }
698
5c7ab634
VH
699 for (i = startindex; i < *count; i++) {
700 vout->buffer_size = *size;
701
702 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
703 &phy_addr);
704 if (!virt_addr) {
445e258f 705 if (ovid->rotation_type == VOUT_ROT_NONE) {
5c7ab634 706 break;
445e258f
AT
707 } else {
708 if (!is_rotation_enabled(vout))
709 break;
5c7ab634
VH
710 /* Free the VRFB buffers if no space for V4L2 buffers */
711 for (j = i; j < *count; j++) {
712 omap_vout_free_buffer(
713 vout->smsshado_virt_addr[j],
714 vout->smsshado_size);
715 vout->smsshado_virt_addr[j] = 0;
716 vout->smsshado_phy_addr[j] = 0;
445e258f 717 }
5c7ab634
VH
718 }
719 }
720 vout->buf_virt_addr[i] = virt_addr;
721 vout->buf_phy_addr[i] = phy_addr;
722 }
723 *count = vout->buffer_allocated = i;
724
725 return 0;
726}
727
728/*
729 * Free the V4L2 buffers additionally allocated than default
445e258f 730 * number of buffers
5c7ab634 731 */
445e258f 732static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
5c7ab634
VH
733{
734 int num_buffers = 0, i;
735
736 num_buffers = (vout->vid == OMAP_VIDEO1) ?
737 video1_numbuffers : video2_numbuffers;
738
739 for (i = num_buffers; i < vout->buffer_allocated; i++) {
740 if (vout->buf_virt_addr[i])
741 omap_vout_free_buffer(vout->buf_virt_addr[i],
742 vout->buffer_size);
743
744 vout->buf_virt_addr[i] = 0;
745 vout->buf_phy_addr[i] = 0;
746 }
5c7ab634
VH
747 vout->buffer_allocated = num_buffers;
748}
749
750/*
751 * This function will be called when VIDIOC_QBUF ioctl is called.
752 * It prepare buffers before give out for the display. This function
753 * converts user space virtual address into physical address if userptr memory
754 * exchange mechanism is used. If rotation is enabled, it copies entire
755 * buffer into VRFB memory space before giving it to the DSS.
756 */
757static int omap_vout_buffer_prepare(struct videobuf_queue *q,
445e258f
AT
758 struct videobuf_buffer *vb,
759 enum v4l2_field field)
5c7ab634 760{
5c7ab634 761 struct omap_vout_device *vout = q->priv_data;
445e258f 762 struct omapvideo_info *ovid = &vout->vid_info;
5c7ab634
VH
763
764 if (VIDEOBUF_NEEDS_INIT == vb->state) {
765 vb->width = vout->pix.width;
766 vb->height = vout->pix.height;
767 vb->size = vb->width * vb->height * vout->bpp;
768 vb->field = field;
769 }
770 vb->state = VIDEOBUF_PREPARED;
771 /* if user pointer memory mechanism is used, get the physical
772 * address of the buffer
773 */
774 if (V4L2_MEMORY_USERPTR == vb->memory) {
775 if (0 == vb->baddr)
776 return -EINVAL;
5c7ab634 777 /* Physical address */
dd880dd4
VH
778 vout->queued_buf_addr[vb->i] = (u8 *)
779 omap_vout_uservirt_to_phys(vb->baddr);
780 } else {
72915e85
AJ
781 u32 addr, dma_addr;
782 unsigned long size;
783
784 addr = (unsigned long) vout->buf_virt_addr[vb->i];
785 size = (unsigned long) vb->size;
786
787 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
788 size, DMA_TO_DEVICE);
789 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
790 v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
791
dd880dd4 792 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
5c7ab634
VH
793 }
794
445e258f
AT
795 if (ovid->rotation_type == VOUT_ROT_VRFB)
796 return omap_vout_prepare_vrfb(vout, vb);
797 else
5c7ab634 798 return 0;
5c7ab634
VH
799}
800
801/*
25985edc 802 * Buffer queue function will be called from the videobuf layer when _QBUF
5c7ab634
VH
803 * ioctl is called. It is used to enqueue buffer, which is ready to be
804 * displayed.
805 */
806static void omap_vout_buffer_queue(struct videobuf_queue *q,
807 struct videobuf_buffer *vb)
808{
809 struct omap_vout_device *vout = q->priv_data;
810
811 /* Driver is also maintainig a queue. So enqueue buffer in the driver
812 * queue */
813 list_add_tail(&vb->queue, &vout->dma_queue);
814
815 vb->state = VIDEOBUF_QUEUED;
816}
817
818/*
819 * Buffer release function is called from videobuf layer to release buffer
820 * which are already allocated
821 */
822static void omap_vout_buffer_release(struct videobuf_queue *q,
823 struct videobuf_buffer *vb)
824{
825 struct omap_vout_device *vout = q->priv_data;
826
827 vb->state = VIDEOBUF_NEEDS_INIT;
828
829 if (V4L2_MEMORY_MMAP != vout->memory)
830 return;
831}
832
833/*
834 * File operations
835 */
94f3f48f
LP
836static unsigned int omap_vout_poll(struct file *file,
837 struct poll_table_struct *wait)
838{
839 struct omap_vout_device *vout = file->private_data;
840 struct videobuf_queue *q = &vout->vbq;
841
842 return videobuf_poll_stream(file, q, wait);
843}
844
5c7ab634
VH
845static void omap_vout_vm_open(struct vm_area_struct *vma)
846{
847 struct omap_vout_device *vout = vma->vm_private_data;
848
849 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
850 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
851 vout->mmap_count++;
852}
853
854static void omap_vout_vm_close(struct vm_area_struct *vma)
855{
856 struct omap_vout_device *vout = vma->vm_private_data;
857
858 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
859 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
860 vout->mmap_count--;
861}
862
863static struct vm_operations_struct omap_vout_vm_ops = {
864 .open = omap_vout_vm_open,
865 .close = omap_vout_vm_close,
866};
867
868static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
869{
870 int i;
871 void *pos;
872 unsigned long start = vma->vm_start;
873 unsigned long size = (vma->vm_end - vma->vm_start);
5c7ab634
VH
874 struct omap_vout_device *vout = file->private_data;
875 struct videobuf_queue *q = &vout->vbq;
876
877 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
878 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
879 vma->vm_pgoff, vma->vm_start, vma->vm_end);
880
881 /* look for the buffer to map */
882 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
883 if (NULL == q->bufs[i])
884 continue;
885 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
886 continue;
887 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
888 break;
889 }
890
891 if (VIDEO_MAX_FRAME == i) {
892 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
893 "offset invalid [offset=0x%lx]\n",
894 (vma->vm_pgoff << PAGE_SHIFT));
895 return -EINVAL;
896 }
383e4f69
VH
897 /* Check the size of the buffer */
898 if (size > vout->buffer_size) {
899 v4l2_err(&vout->vid_dev->v4l2_dev,
900 "insufficient memory [%lu] [%u]\n",
901 size, vout->buffer_size);
902 return -ENOMEM;
903 }
904
5c7ab634
VH
905 q->bufs[i]->baddr = vma->vm_start;
906
907 vma->vm_flags |= VM_RESERVED;
908 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
909 vma->vm_ops = &omap_vout_vm_ops;
910 vma->vm_private_data = (void *) vout;
dd880dd4 911 pos = (void *)vout->buf_virt_addr[i];
5c7ab634
VH
912 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
913 while (size > 0) {
914 unsigned long pfn;
915 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
916 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
917 return -EAGAIN;
918 start += PAGE_SIZE;
919 pos += PAGE_SIZE;
920 size -= PAGE_SIZE;
921 }
922 vout->mmap_count++;
923 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
924
925 return 0;
926}
927
928static int omap_vout_release(struct file *file)
929{
930 unsigned int ret, i;
931 struct videobuf_queue *q;
932 struct omapvideo_info *ovid;
933 struct omap_vout_device *vout = file->private_data;
934
935 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
936 ovid = &vout->vid_info;
937
938 if (!vout)
939 return 0;
940
941 q = &vout->vbq;
942 /* Disable all the overlay managers connected with this interface */
943 for (i = 0; i < ovid->num_overlays; i++) {
944 struct omap_overlay *ovl = ovid->overlays[i];
945 if (ovl->manager && ovl->manager->device) {
946 struct omap_overlay_info info;
947 ovl->get_overlay_info(ovl, &info);
948 info.enabled = 0;
949 ovl->set_overlay_info(ovl, &info);
950 }
951 }
952 /* Turn off the pipeline */
953 ret = omapvid_apply_changes(vout);
954 if (ret)
955 v4l2_warn(&vout->vid_dev->v4l2_dev,
956 "Unable to apply changes\n");
957
958 /* Free all buffers */
445e258f
AT
959 omap_vout_free_extra_buffers(vout);
960
961 /* Free the VRFB buffers only if they are allocated
962 * during reqbufs. Don't free if init time allocated
963 */
964 if (ovid->rotation_type == VOUT_ROT_VRFB) {
965 if (!vout->vrfb_static_allocation)
966 omap_vout_free_vrfb_buffers(vout);
967 }
5c7ab634
VH
968 videobuf_mmap_free(q);
969
970 /* Even if apply changes fails we should continue
b595076a 971 freeing allocated memory */
5c7ab634
VH
972 if (vout->streaming) {
973 u32 mask = 0;
974
975 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
5251dd6c 976 DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
5c7ab634
VH
977 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
978 vout->streaming = 0;
979
980 videobuf_streamoff(q);
981 videobuf_queue_cancel(q);
982 }
983
984 if (vout->mmap_count != 0)
985 vout->mmap_count = 0;
986
987 vout->opened -= 1;
988 file->private_data = NULL;
989
990 if (vout->buffer_allocated)
991 videobuf_mmap_free(q);
992
993 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
994 return ret;
995}
996
997static int omap_vout_open(struct file *file)
998{
999 struct videobuf_queue *q;
1000 struct omap_vout_device *vout = NULL;
1001
1002 vout = video_drvdata(file);
1003 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1004
1005 if (vout == NULL)
1006 return -ENODEV;
1007
1008 /* for now, we only support single open */
1009 if (vout->opened)
1010 return -EBUSY;
1011
1012 vout->opened += 1;
1013
1014 file->private_data = vout;
1015 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1016
1017 q = &vout->vbq;
1018 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1019 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1020 video_vbq_ops.buf_release = omap_vout_buffer_release;
1021 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1022 spin_lock_init(&vout->vbq_lock);
1023
dd880dd4
VH
1024 videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1025 &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
e3cfd447 1026 sizeof(struct videobuf_buffer), vout, NULL);
5c7ab634
VH
1027
1028 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1029 return 0;
1030}
1031
1032/*
1033 * V4L2 ioctls
1034 */
1035static int vidioc_querycap(struct file *file, void *fh,
1036 struct v4l2_capability *cap)
1037{
1038 struct omap_vout_device *vout = fh;
1039
1040 strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1041 strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1042 cap->bus_info[0] = '\0';
1043 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
1044
1045 return 0;
1046}
1047
1048static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1049 struct v4l2_fmtdesc *fmt)
1050{
1051 int index = fmt->index;
5c7ab634 1052
5c7ab634
VH
1053 if (index >= NUM_OUTPUT_FORMATS)
1054 return -EINVAL;
1055
1056 fmt->flags = omap_formats[index].flags;
1057 strlcpy(fmt->description, omap_formats[index].description,
1058 sizeof(fmt->description));
1059 fmt->pixelformat = omap_formats[index].pixelformat;
1060
1061 return 0;
1062}
1063
1064static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1065 struct v4l2_format *f)
1066{
1067 struct omap_vout_device *vout = fh;
1068
1069 f->fmt.pix = vout->pix;
1070 return 0;
1071
1072}
1073
1074static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1075 struct v4l2_format *f)
1076{
1077 struct omap_overlay *ovl;
1078 struct omapvideo_info *ovid;
1079 struct omap_video_timings *timing;
1080 struct omap_vout_device *vout = fh;
1081
1082 ovid = &vout->vid_info;
1083 ovl = ovid->overlays[0];
1084
1085 if (!ovl->manager || !ovl->manager->device)
1086 return -EINVAL;
1087 /* get the display device attached to the overlay */
1088 timing = &ovl->manager->device->panel.timings;
1089
1090 vout->fbuf.fmt.height = timing->y_res;
1091 vout->fbuf.fmt.width = timing->x_res;
1092
1093 omap_vout_try_format(&f->fmt.pix);
1094 return 0;
1095}
1096
1097static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1098 struct v4l2_format *f)
1099{
1100 int ret, bpp;
1101 struct omap_overlay *ovl;
1102 struct omapvideo_info *ovid;
1103 struct omap_video_timings *timing;
1104 struct omap_vout_device *vout = fh;
1105
1106 if (vout->streaming)
1107 return -EBUSY;
1108
1109 mutex_lock(&vout->lock);
1110
1111 ovid = &vout->vid_info;
1112 ovl = ovid->overlays[0];
1113
1114 /* get the display device attached to the overlay */
1115 if (!ovl->manager || !ovl->manager->device) {
1116 ret = -EINVAL;
1117 goto s_fmt_vid_out_exit;
1118 }
1119 timing = &ovl->manager->device->panel.timings;
1120
1121 /* We dont support RGB24-packed mode if vrfb rotation
1122 * is enabled*/
b366888a 1123 if ((is_rotation_enabled(vout)) &&
5c7ab634
VH
1124 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1125 ret = -EINVAL;
1126 goto s_fmt_vid_out_exit;
1127 }
1128
1129 /* get the framebuffer parameters */
1130
b366888a 1131 if (is_rotation_90_or_270(vout)) {
5c7ab634
VH
1132 vout->fbuf.fmt.height = timing->x_res;
1133 vout->fbuf.fmt.width = timing->y_res;
1134 } else {
1135 vout->fbuf.fmt.height = timing->y_res;
1136 vout->fbuf.fmt.width = timing->x_res;
1137 }
1138
1139 /* change to samller size is OK */
1140
1141 bpp = omap_vout_try_format(&f->fmt.pix);
1142 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1143
1144 /* try & set the new output format */
1145 vout->bpp = bpp;
1146 vout->pix = f->fmt.pix;
1147 vout->vrfb_bpp = 1;
1148
1149 /* If YUYV then vrfb bpp is 2, for others its 1 */
1150 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1151 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1152 vout->vrfb_bpp = 2;
1153
1154 /* set default crop and win */
1155 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1156
1157 /* Save the changes in the overlay strcuture */
1158 ret = omapvid_init(vout, 0);
1159 if (ret) {
1160 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1161 goto s_fmt_vid_out_exit;
1162 }
1163
1164 ret = 0;
1165
1166s_fmt_vid_out_exit:
1167 mutex_unlock(&vout->lock);
1168 return ret;
1169}
1170
1171static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1172 struct v4l2_format *f)
1173{
1174 int ret = 0;
1175 struct omap_vout_device *vout = fh;
11354dd5
AT
1176 struct omap_overlay *ovl;
1177 struct omapvideo_info *ovid;
5c7ab634
VH
1178 struct v4l2_window *win = &f->fmt.win;
1179
11354dd5
AT
1180 ovid = &vout->vid_info;
1181 ovl = ovid->overlays[0];
1182
5c7ab634
VH
1183 ret = omap_vout_try_window(&vout->fbuf, win);
1184
1185 if (!ret) {
11354dd5 1186 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
5c7ab634
VH
1187 win->global_alpha = 255;
1188 else
1189 win->global_alpha = f->fmt.win.global_alpha;
1190 }
1191
1192 return ret;
1193}
1194
1195static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1196 struct v4l2_format *f)
1197{
1198 int ret = 0;
1199 struct omap_overlay *ovl;
1200 struct omapvideo_info *ovid;
1201 struct omap_vout_device *vout = fh;
1202 struct v4l2_window *win = &f->fmt.win;
1203
1204 mutex_lock(&vout->lock);
1205 ovid = &vout->vid_info;
1206 ovl = ovid->overlays[0];
1207
1208 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1209 if (!ret) {
11354dd5
AT
1210 /* Video1 plane does not support global alpha on OMAP3 */
1211 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
5c7ab634
VH
1212 vout->win.global_alpha = 255;
1213 else
1214 vout->win.global_alpha = f->fmt.win.global_alpha;
1215
1216 vout->win.chromakey = f->fmt.win.chromakey;
1217 }
1218 mutex_unlock(&vout->lock);
1219 return ret;
1220}
1221
1222static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
1223 struct v4l2_fmtdesc *fmt)
1224{
1225 int index = fmt->index;
5c7ab634 1226
5c7ab634
VH
1227 if (index >= NUM_OUTPUT_FORMATS)
1228 return -EINVAL;
1229
1230 fmt->flags = omap_formats[index].flags;
1231 strlcpy(fmt->description, omap_formats[index].description,
1232 sizeof(fmt->description));
1233 fmt->pixelformat = omap_formats[index].pixelformat;
1234 return 0;
1235}
1236
1237static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1238 struct v4l2_format *f)
1239{
1240 u32 key_value = 0;
1241 struct omap_overlay *ovl;
1242 struct omapvideo_info *ovid;
1243 struct omap_vout_device *vout = fh;
1244 struct omap_overlay_manager_info info;
1245 struct v4l2_window *win = &f->fmt.win;
1246
1247 ovid = &vout->vid_info;
1248 ovl = ovid->overlays[0];
1249
1250 win->w = vout->win.w;
1251 win->field = vout->win.field;
1252 win->global_alpha = vout->win.global_alpha;
1253
1254 if (ovl->manager && ovl->manager->get_manager_info) {
1255 ovl->manager->get_manager_info(ovl->manager, &info);
1256 key_value = info.trans_key;
1257 }
1258 win->chromakey = key_value;
1259 return 0;
1260}
1261
1262static int vidioc_cropcap(struct file *file, void *fh,
1263 struct v4l2_cropcap *cropcap)
1264{
1265 struct omap_vout_device *vout = fh;
1266 struct v4l2_pix_format *pix = &vout->pix;
1267
1268 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1269 return -EINVAL;
1270
1271 /* Width and height are always even */
1272 cropcap->bounds.width = pix->width & ~1;
1273 cropcap->bounds.height = pix->height & ~1;
1274
1275 omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1276 cropcap->pixelaspect.numerator = 1;
1277 cropcap->pixelaspect.denominator = 1;
1278 return 0;
1279}
1280
1281static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1282{
1283 struct omap_vout_device *vout = fh;
1284
1285 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1286 return -EINVAL;
1287 crop->c = vout->crop;
1288 return 0;
1289}
1290
1291static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1292{
1293 int ret = -EINVAL;
1294 struct omap_vout_device *vout = fh;
1295 struct omapvideo_info *ovid;
1296 struct omap_overlay *ovl;
1297 struct omap_video_timings *timing;
1298
1299 if (vout->streaming)
1300 return -EBUSY;
1301
1302 mutex_lock(&vout->lock);
1303 ovid = &vout->vid_info;
1304 ovl = ovid->overlays[0];
1305
1306 if (!ovl->manager || !ovl->manager->device) {
1307 ret = -EINVAL;
1308 goto s_crop_err;
1309 }
1310 /* get the display device attached to the overlay */
1311 timing = &ovl->manager->device->panel.timings;
1312
b366888a 1313 if (is_rotation_90_or_270(vout)) {
5c7ab634
VH
1314 vout->fbuf.fmt.height = timing->x_res;
1315 vout->fbuf.fmt.width = timing->y_res;
1316 } else {
1317 vout->fbuf.fmt.height = timing->y_res;
1318 vout->fbuf.fmt.width = timing->x_res;
1319 }
1320
1321 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1322 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1323 &vout->fbuf, &crop->c);
1324
1325s_crop_err:
1326 mutex_unlock(&vout->lock);
1327 return ret;
1328}
1329
1330static int vidioc_queryctrl(struct file *file, void *fh,
1331 struct v4l2_queryctrl *ctrl)
1332{
1333 int ret = 0;
1334
1335 switch (ctrl->id) {
1336 case V4L2_CID_ROTATE:
1337 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1338 break;
1339 case V4L2_CID_BG_COLOR:
1340 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1341 break;
1342 case V4L2_CID_VFLIP:
1343 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1344 break;
1345 default:
1346 ctrl->name[0] = '\0';
1347 ret = -EINVAL;
1348 }
1349 return ret;
1350}
1351
1352static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1353{
1354 int ret = 0;
1355 struct omap_vout_device *vout = fh;
1356
1357 switch (ctrl->id) {
1358 case V4L2_CID_ROTATE:
1359 ctrl->value = vout->control[0].value;
1360 break;
1361 case V4L2_CID_BG_COLOR:
1362 {
1363 struct omap_overlay_manager_info info;
1364 struct omap_overlay *ovl;
1365
1366 ovl = vout->vid_info.overlays[0];
1367 if (!ovl->manager || !ovl->manager->get_manager_info) {
1368 ret = -EINVAL;
1369 break;
1370 }
1371
1372 ovl->manager->get_manager_info(ovl->manager, &info);
1373 ctrl->value = info.default_color;
1374 break;
1375 }
1376 case V4L2_CID_VFLIP:
1377 ctrl->value = vout->control[2].value;
1378 break;
1379 default:
1380 ret = -EINVAL;
1381 }
1382 return ret;
1383}
1384
1385static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1386{
1387 int ret = 0;
1388 struct omap_vout_device *vout = fh;
1389
1390 switch (a->id) {
1391 case V4L2_CID_ROTATE:
1392 {
445e258f 1393 struct omapvideo_info *ovid;
5c7ab634
VH
1394 int rotation = a->value;
1395
445e258f
AT
1396 ovid = &vout->vid_info;
1397
5c7ab634 1398 mutex_lock(&vout->lock);
445e258f
AT
1399 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1400 mutex_unlock(&vout->lock);
1401 ret = -ERANGE;
1402 break;
1403 }
5c7ab634
VH
1404
1405 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1406 mutex_unlock(&vout->lock);
1407 ret = -EINVAL;
1408 break;
1409 }
1410
1411 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1412 vout->mirror)) {
1413 mutex_unlock(&vout->lock);
1414 ret = -EINVAL;
1415 break;
1416 }
1417
1418 vout->control[0].value = rotation;
1419 mutex_unlock(&vout->lock);
1420 break;
1421 }
1422 case V4L2_CID_BG_COLOR:
1423 {
1424 struct omap_overlay *ovl;
1425 unsigned int color = a->value;
1426 struct omap_overlay_manager_info info;
1427
1428 ovl = vout->vid_info.overlays[0];
1429
1430 mutex_lock(&vout->lock);
1431 if (!ovl->manager || !ovl->manager->get_manager_info) {
1432 mutex_unlock(&vout->lock);
1433 ret = -EINVAL;
1434 break;
1435 }
1436
1437 ovl->manager->get_manager_info(ovl->manager, &info);
1438 info.default_color = color;
1439 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1440 mutex_unlock(&vout->lock);
1441 ret = -EINVAL;
1442 break;
1443 }
1444
1445 vout->control[1].value = color;
1446 mutex_unlock(&vout->lock);
1447 break;
1448 }
1449 case V4L2_CID_VFLIP:
1450 {
1451 struct omap_overlay *ovl;
1452 struct omapvideo_info *ovid;
1453 unsigned int mirror = a->value;
1454
1455 ovid = &vout->vid_info;
1456 ovl = ovid->overlays[0];
1457
1458 mutex_lock(&vout->lock);
445e258f
AT
1459 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1460 mutex_unlock(&vout->lock);
1461 ret = -ERANGE;
1462 break;
1463 }
5c7ab634
VH
1464
1465 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1466 mutex_unlock(&vout->lock);
1467 ret = -EINVAL;
1468 break;
1469 }
1470 vout->mirror = mirror;
1471 vout->control[2].value = mirror;
1472 mutex_unlock(&vout->lock);
1473 break;
1474 }
1475 default:
1476 ret = -EINVAL;
1477 }
1478 return ret;
1479}
1480
1481static int vidioc_reqbufs(struct file *file, void *fh,
1482 struct v4l2_requestbuffers *req)
1483{
1484 int ret = 0;
1485 unsigned int i, num_buffers = 0;
1486 struct omap_vout_device *vout = fh;
1487 struct videobuf_queue *q = &vout->vbq;
5c7ab634
VH
1488
1489 if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
1490 return -EINVAL;
1491 /* if memory is not mmp or userptr
1492 return error */
1493 if ((V4L2_MEMORY_MMAP != req->memory) &&
1494 (V4L2_MEMORY_USERPTR != req->memory))
1495 return -EINVAL;
1496
1497 mutex_lock(&vout->lock);
1498 /* Cannot be requested when streaming is on */
1499 if (vout->streaming) {
1500 ret = -EBUSY;
1501 goto reqbuf_err;
1502 }
1503
1504 /* If buffers are already allocated free them */
1505 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1506 if (vout->mmap_count) {
1507 ret = -EBUSY;
1508 goto reqbuf_err;
1509 }
1510 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1511 video1_numbuffers : video2_numbuffers;
1512 for (i = num_buffers; i < vout->buffer_allocated; i++) {
dd880dd4 1513 omap_vout_free_buffer(vout->buf_virt_addr[i],
5c7ab634
VH
1514 vout->buffer_size);
1515 vout->buf_virt_addr[i] = 0;
1516 vout->buf_phy_addr[i] = 0;
1517 }
1518 vout->buffer_allocated = num_buffers;
1519 videobuf_mmap_free(q);
1520 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1521 if (vout->buffer_allocated) {
1522 videobuf_mmap_free(q);
1523 for (i = 0; i < vout->buffer_allocated; i++) {
1524 kfree(q->bufs[i]);
1525 q->bufs[i] = NULL;
1526 }
1527 vout->buffer_allocated = 0;
1528 }
1529 }
1530
1531 /*store the memory type in data structure */
1532 vout->memory = req->memory;
1533
1534 INIT_LIST_HEAD(&vout->dma_queue);
1535
1536 /* call videobuf_reqbufs api */
1537 ret = videobuf_reqbufs(q, req);
1538 if (ret < 0)
1539 goto reqbuf_err;
1540
1541 vout->buffer_allocated = req->count;
dd880dd4 1542
5c7ab634
VH
1543reqbuf_err:
1544 mutex_unlock(&vout->lock);
1545 return ret;
1546}
1547
1548static int vidioc_querybuf(struct file *file, void *fh,
1549 struct v4l2_buffer *b)
1550{
1551 struct omap_vout_device *vout = fh;
1552
1553 return videobuf_querybuf(&vout->vbq, b);
1554}
1555
1556static int vidioc_qbuf(struct file *file, void *fh,
1557 struct v4l2_buffer *buffer)
1558{
1559 struct omap_vout_device *vout = fh;
1560 struct videobuf_queue *q = &vout->vbq;
1561
1562 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1563 (buffer->index >= vout->buffer_allocated) ||
1564 (q->bufs[buffer->index]->memory != buffer->memory)) {
1565 return -EINVAL;
1566 }
1567 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1568 if ((buffer->length < vout->pix.sizeimage) ||
1569 (0 == buffer->m.userptr)) {
1570 return -EINVAL;
1571 }
1572 }
1573
b366888a 1574 if ((is_rotation_enabled(vout)) &&
5c7ab634
VH
1575 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1576 v4l2_warn(&vout->vid_dev->v4l2_dev,
1577 "DMA Channel not allocated for Rotation\n");
1578 return -EINVAL;
1579 }
1580
1581 return videobuf_qbuf(q, buffer);
1582}
1583
1584static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1585{
1586 struct omap_vout_device *vout = fh;
1587 struct videobuf_queue *q = &vout->vbq;
1588
72915e85
AJ
1589 int ret;
1590 u32 addr;
1591 unsigned long size;
1592 struct videobuf_buffer *vb;
1593
1594 vb = q->bufs[b->index];
1595
5c7ab634
VH
1596 if (!vout->streaming)
1597 return -EINVAL;
1598
1599 if (file->f_flags & O_NONBLOCK)
1600 /* Call videobuf_dqbuf for non blocking mode */
72915e85 1601 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
5c7ab634
VH
1602 else
1603 /* Call videobuf_dqbuf for blocking mode */
72915e85
AJ
1604 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
1605
1606 addr = (unsigned long) vout->buf_phy_addr[vb->i];
1607 size = (unsigned long) vb->size;
1608 dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
1609 size, DMA_TO_DEVICE);
1610 return ret;
5c7ab634
VH
1611}
1612
1613static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1614{
1615 int ret = 0, j;
1616 u32 addr = 0, mask = 0;
1617 struct omap_vout_device *vout = fh;
1618 struct videobuf_queue *q = &vout->vbq;
1619 struct omapvideo_info *ovid = &vout->vid_info;
1620
1621 mutex_lock(&vout->lock);
1622
1623 if (vout->streaming) {
1624 ret = -EBUSY;
1625 goto streamon_err;
1626 }
1627
1628 ret = videobuf_streamon(q);
1629 if (ret)
1630 goto streamon_err;
1631
1632 if (list_empty(&vout->dma_queue)) {
1633 ret = -EIO;
1634 goto streamon_err1;
1635 }
1636
1637 /* Get the next frame from the buffer queue */
1638 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1639 struct videobuf_buffer, queue);
1640 /* Remove buffer from the buffer queue */
1641 list_del(&vout->cur_frm->queue);
1642 /* Mark state of the current frame to active */
1643 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1644 /* Initialize field_id and started member */
1645 vout->field_id = 0;
1646
1647 /* set flag here. Next QBUF will start DMA */
1648 vout->streaming = 1;
1649
1650 vout->first_int = 1;
1651
1652 if (omap_vout_calculate_offset(vout)) {
1653 ret = -EINVAL;
1654 goto streamon_err1;
1655 }
1656 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1657 + vout->cropped_offset;
1658
5251dd6c
AJ
1659 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1660 | DISPC_IRQ_VSYNC2;
5c7ab634
VH
1661
1662 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1663
1664 for (j = 0; j < ovid->num_overlays; j++) {
1665 struct omap_overlay *ovl = ovid->overlays[j];
1666
1667 if (ovl->manager && ovl->manager->device) {
1668 struct omap_overlay_info info;
1669 ovl->get_overlay_info(ovl, &info);
1670 info.enabled = 1;
1671 info.paddr = addr;
1672 if (ovl->set_overlay_info(ovl, &info)) {
1673 ret = -EINVAL;
1674 goto streamon_err1;
1675 }
1676 }
1677 }
1678
1679 /* First save the configuration in ovelray structure */
1680 ret = omapvid_init(vout, addr);
1681 if (ret)
1682 v4l2_err(&vout->vid_dev->v4l2_dev,
1683 "failed to set overlay info\n");
1684 /* Enable the pipeline and set the Go bit */
1685 ret = omapvid_apply_changes(vout);
1686 if (ret)
1687 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1688
1689 ret = 0;
1690
1691streamon_err1:
1692 if (ret)
1693 ret = videobuf_streamoff(q);
1694streamon_err:
1695 mutex_unlock(&vout->lock);
1696 return ret;
1697}
1698
1699static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1700{
1701 u32 mask = 0;
1702 int ret = 0, j;
1703 struct omap_vout_device *vout = fh;
1704 struct omapvideo_info *ovid = &vout->vid_info;
1705
1706 if (!vout->streaming)
1707 return -EINVAL;
1708
1709 vout->streaming = 0;
5251dd6c
AJ
1710 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1711 | DISPC_IRQ_VSYNC2;
5c7ab634
VH
1712
1713 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1714
1715 for (j = 0; j < ovid->num_overlays; j++) {
1716 struct omap_overlay *ovl = ovid->overlays[j];
1717
1718 if (ovl->manager && ovl->manager->device) {
1719 struct omap_overlay_info info;
1720
1721 ovl->get_overlay_info(ovl, &info);
1722 info.enabled = 0;
1723 ret = ovl->set_overlay_info(ovl, &info);
1724 if (ret)
1725 v4l2_err(&vout->vid_dev->v4l2_dev,
1726 "failed to update overlay info in streamoff\n");
1727 }
1728 }
1729
1730 /* Turn of the pipeline */
1731 ret = omapvid_apply_changes(vout);
1732 if (ret)
1733 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
1734 " streamoff\n");
1735
1736 INIT_LIST_HEAD(&vout->dma_queue);
1737 ret = videobuf_streamoff(&vout->vbq);
1738
1739 return ret;
1740}
1741
1742static int vidioc_s_fbuf(struct file *file, void *fh,
1743 struct v4l2_framebuffer *a)
1744{
1745 int enable = 0;
1746 struct omap_overlay *ovl;
1747 struct omapvideo_info *ovid;
1748 struct omap_vout_device *vout = fh;
1749 struct omap_overlay_manager_info info;
1750 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1751
1752 ovid = &vout->vid_info;
1753 ovl = ovid->overlays[0];
1754
1755 /* OMAP DSS doesn't support Source and Destination color
1756 key together */
1757 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1758 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1759 return -EINVAL;
1760 /* OMAP DSS Doesn't support the Destination color key
1761 and alpha blending together */
1762 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1763 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1764 return -EINVAL;
1765
1766 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1767 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1768 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
1769 } else
1770 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1771
1772 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1773 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1774 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1775 } else
1776 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
1777
1778 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1779 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1780 enable = 1;
1781 else
1782 enable = 0;
1783 if (ovl->manager && ovl->manager->get_manager_info &&
1784 ovl->manager->set_manager_info) {
1785
1786 ovl->manager->get_manager_info(ovl->manager, &info);
1787 info.trans_enabled = enable;
1788 info.trans_key_type = key_type;
1789 info.trans_key = vout->win.chromakey;
1790
1791 if (ovl->manager->set_manager_info(ovl->manager, &info))
1792 return -EINVAL;
1793 }
1794 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1795 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1796 enable = 1;
1797 } else {
1798 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1799 enable = 0;
1800 }
1801 if (ovl->manager && ovl->manager->get_manager_info &&
1802 ovl->manager->set_manager_info) {
1803 ovl->manager->get_manager_info(ovl->manager, &info);
11354dd5
AT
1804 /* enable this only if there is no zorder cap */
1805 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1806 info.partial_alpha_enabled = enable;
5c7ab634
VH
1807 if (ovl->manager->set_manager_info(ovl->manager, &info))
1808 return -EINVAL;
1809 }
1810
1811 return 0;
1812}
1813
1814static int vidioc_g_fbuf(struct file *file, void *fh,
1815 struct v4l2_framebuffer *a)
1816{
1817 struct omap_overlay *ovl;
1818 struct omapvideo_info *ovid;
1819 struct omap_vout_device *vout = fh;
1820 struct omap_overlay_manager_info info;
1821
1822 ovid = &vout->vid_info;
1823 ovl = ovid->overlays[0];
1824
1825 a->flags = 0x0;
1826 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1827 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1828
1829 if (ovl->manager && ovl->manager->get_manager_info) {
1830 ovl->manager->get_manager_info(ovl->manager, &info);
1831 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1832 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1833 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1834 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1835 }
1836 if (ovl->manager && ovl->manager->get_manager_info) {
1837 ovl->manager->get_manager_info(ovl->manager, &info);
11354dd5 1838 if (info.partial_alpha_enabled)
5c7ab634
VH
1839 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1840 }
1841
1842 return 0;
1843}
1844
1845static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1846 .vidioc_querycap = vidioc_querycap,
1847 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1848 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1849 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1850 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1851 .vidioc_queryctrl = vidioc_queryctrl,
1852 .vidioc_g_ctrl = vidioc_g_ctrl,
1853 .vidioc_s_fbuf = vidioc_s_fbuf,
1854 .vidioc_g_fbuf = vidioc_g_fbuf,
1855 .vidioc_s_ctrl = vidioc_s_ctrl,
1856 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1857 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1858 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1859 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1860 .vidioc_cropcap = vidioc_cropcap,
1861 .vidioc_g_crop = vidioc_g_crop,
1862 .vidioc_s_crop = vidioc_s_crop,
1863 .vidioc_reqbufs = vidioc_reqbufs,
1864 .vidioc_querybuf = vidioc_querybuf,
1865 .vidioc_qbuf = vidioc_qbuf,
1866 .vidioc_dqbuf = vidioc_dqbuf,
1867 .vidioc_streamon = vidioc_streamon,
1868 .vidioc_streamoff = vidioc_streamoff,
1869};
1870
1871static const struct v4l2_file_operations omap_vout_fops = {
1872 .owner = THIS_MODULE,
94f3f48f 1873 .poll = omap_vout_poll,
5c7ab634
VH
1874 .unlocked_ioctl = video_ioctl2,
1875 .mmap = omap_vout_mmap,
1876 .open = omap_vout_open,
1877 .release = omap_vout_release,
1878};
1879
1880/* Init functions used during driver initialization */
1881/* Initial setup of video_data */
1882static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1883{
1884 struct video_device *vfd;
1885 struct v4l2_pix_format *pix;
1886 struct v4l2_control *control;
1887 struct omap_dss_device *display =
1888 vout->vid_info.overlays[0]->manager->device;
1889
1890 /* set the default pix */
1891 pix = &vout->pix;
1892
1893 /* Set the default picture of QVGA */
1894 pix->width = QQVGA_WIDTH;
1895 pix->height = QQVGA_HEIGHT;
1896
1897 /* Default pixel format is RGB 5-6-5 */
1898 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1899 pix->field = V4L2_FIELD_ANY;
1900 pix->bytesperline = pix->width * 2;
1901 pix->sizeimage = pix->bytesperline * pix->height;
1902 pix->priv = 0;
1903 pix->colorspace = V4L2_COLORSPACE_JPEG;
1904
1905 vout->bpp = RGB565_BPP;
1906 vout->fbuf.fmt.width = display->panel.timings.x_res;
1907 vout->fbuf.fmt.height = display->panel.timings.y_res;
1908
1909 /* Set the data structures for the overlay parameters*/
1910 vout->win.global_alpha = 255;
1911 vout->fbuf.flags = 0;
1912 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1913 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1914 vout->win.chromakey = 0;
1915
1916 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1917
1918 /*Initialize the control variables for
1919 rotation, flipping and background color. */
1920 control = vout->control;
1921 control[0].id = V4L2_CID_ROTATE;
1922 control[0].value = 0;
1923 vout->rotation = 0;
1924 vout->mirror = 0;
1925 vout->control[2].id = V4L2_CID_HFLIP;
1926 vout->control[2].value = 0;
445e258f
AT
1927 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1928 vout->vrfb_bpp = 2;
5c7ab634
VH
1929
1930 control[1].id = V4L2_CID_BG_COLOR;
1931 control[1].value = 0;
1932
1933 /* initialize the video_device struct */
1934 vfd = vout->vfd = video_device_alloc();
1935
1936 if (!vfd) {
1937 printk(KERN_ERR VOUT_NAME ": could not allocate"
1938 " video device struct\n");
1939 return -ENOMEM;
1940 }
1941 vfd->release = video_device_release;
1942 vfd->ioctl_ops = &vout_ioctl_ops;
1943
1944 strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1945
5c7ab634
VH
1946 vfd->fops = &omap_vout_fops;
1947 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
1948 mutex_init(&vout->lock);
1949
1950 vfd->minor = -1;
1951 return 0;
1952
1953}
1954
1955/* Setup video buffers */
1956static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1957 int vid_num)
1958{
1959 u32 numbuffers;
445e258f
AT
1960 int ret = 0, i;
1961 struct omapvideo_info *ovid;
5c7ab634 1962 struct omap_vout_device *vout;
5c7ab634
VH
1963 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1964 struct omap2video_device *vid_dev =
1965 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1966
1967 vout = vid_dev->vouts[vid_num];
445e258f 1968 ovid = &vout->vid_info;
5c7ab634
VH
1969
1970 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1971 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1972 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1973
1974 for (i = 0; i < numbuffers; i++) {
1975 vout->buf_virt_addr[i] =
1976 omap_vout_alloc_buffer(vout->buffer_size,
1977 (u32 *) &vout->buf_phy_addr[i]);
1978 if (!vout->buf_virt_addr[i]) {
1979 numbuffers = i;
1980 ret = -ENOMEM;
1981 goto free_buffers;
1982 }
1983 }
1984
5c7ab634
VH
1985 vout->cropped_offset = 0;
1986
445e258f
AT
1987 if (ovid->rotation_type == VOUT_ROT_VRFB) {
1988 int static_vrfb_allocation = (vid_num == 0) ?
1989 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1990 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1991 static_vrfb_allocation);
5c7ab634 1992 }
5c7ab634 1993
445e258f 1994 return ret;
5c7ab634
VH
1995
1996free_buffers:
1997 for (i = 0; i < numbuffers; i++) {
1998 omap_vout_free_buffer(vout->buf_virt_addr[i],
1999 vout->buffer_size);
2000 vout->buf_virt_addr[i] = 0;
2001 vout->buf_phy_addr[i] = 0;
2002 }
2003 return ret;
2004
2005}
2006
2007/* Create video out devices */
2008static int __init omap_vout_create_video_devices(struct platform_device *pdev)
2009{
2010 int ret = 0, k;
2011 struct omap_vout_device *vout;
2012 struct video_device *vfd = NULL;
2013 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2014 struct omap2video_device *vid_dev = container_of(v4l2_dev,
2015 struct omap2video_device, v4l2_dev);
2016
2017 for (k = 0; k < pdev->num_resources; k++) {
2018
2ef17c9f 2019 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
5c7ab634
VH
2020 if (!vout) {
2021 dev_err(&pdev->dev, ": could not allocate memory\n");
2022 return -ENOMEM;
2023 }
5c7ab634
VH
2024
2025 vout->vid = k;
2026 vid_dev->vouts[k] = vout;
2027 vout->vid_dev = vid_dev;
2028 /* Select video2 if only 1 overlay is controlled by V4L2 */
2029 if (pdev->num_resources == 1)
2030 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2031 else
2032 /* Else select video1 and video2 one by one. */
2033 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2034 vout->vid_info.num_overlays = 1;
2035 vout->vid_info.id = k + 1;
2036
445e258f
AT
2037 /* Set VRFB as rotation_type for omap2 and omap3 */
2038 if (cpu_is_omap24xx() || cpu_is_omap34xx())
2039 vout->vid_info.rotation_type = VOUT_ROT_VRFB;
2040
5c7ab634
VH
2041 /* Setup the default configuration for the video devices
2042 */
2043 if (omap_vout_setup_video_data(vout) != 0) {
2044 ret = -ENOMEM;
2045 goto error;
2046 }
2047
2048 /* Allocate default number of buffers for the video streaming
2049 * and reserve the VRFB space for rotation
2050 */
2051 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2052 ret = -ENOMEM;
2053 goto error1;
2054 }
2055
2056 /* Register the Video device with V4L2
2057 */
2058 vfd = vout->vfd;
8f3a307b 2059 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
5c7ab634
VH
2060 dev_err(&pdev->dev, ": Could not register "
2061 "Video for Linux device\n");
2062 vfd->minor = -1;
2063 ret = -ENODEV;
2064 goto error2;
2065 }
2066 video_set_drvdata(vfd, vout);
2067
2068 /* Configure the overlay structure */
2069 ret = omapvid_init(vid_dev->vouts[k], 0);
2070 if (!ret)
2071 goto success;
2072
2073error2:
445e258f
AT
2074 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2075 omap_vout_release_vrfb(vout);
5c7ab634
VH
2076 omap_vout_free_buffers(vout);
2077error1:
2078 video_device_release(vfd);
2079error:
2080 kfree(vout);
2081 return ret;
2082
2083success:
2084 dev_info(&pdev->dev, ": registered and initialized"
2085 " video device %d\n", vfd->minor);
2086 if (k == (pdev->num_resources - 1))
2087 return 0;
2088 }
2089
2090 return -ENODEV;
2091}
2092/* Driver functions */
2093static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2094{
2095 struct video_device *vfd;
445e258f 2096 struct omapvideo_info *ovid;
5c7ab634
VH
2097
2098 if (!vout)
2099 return;
2100
2101 vfd = vout->vfd;
445e258f 2102 ovid = &vout->vid_info;
5c7ab634
VH
2103 if (vfd) {
2104 if (!video_is_registered(vfd)) {
2105 /*
2106 * The device was never registered, so release the
2107 * video_device struct directly.
2108 */
2109 video_device_release(vfd);
2110 } else {
2111 /*
2112 * The unregister function will release the video_device
2113 * struct as well as unregistering it.
2114 */
2115 video_unregister_device(vfd);
2116 }
2117 }
445e258f
AT
2118 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2119 omap_vout_release_vrfb(vout);
2120 /* Free the VRFB buffer if allocated
2121 * init time
2122 */
2123 if (vout->vrfb_static_allocation)
2124 omap_vout_free_vrfb_buffers(vout);
2125 }
5c7ab634 2126 omap_vout_free_buffers(vout);
5c7ab634
VH
2127
2128 kfree(vout);
2129}
2130
2131static int omap_vout_remove(struct platform_device *pdev)
2132{
2133 int k;
2134 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2135 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2136 omap2video_device, v4l2_dev);
2137
2138 v4l2_device_unregister(v4l2_dev);
2139 for (k = 0; k < pdev->num_resources; k++)
2140 omap_vout_cleanup_device(vid_dev->vouts[k]);
2141
2142 for (k = 0; k < vid_dev->num_displays; k++) {
2143 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
5ba9bb0e 2144 vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
5c7ab634
VH
2145
2146 omap_dss_put_device(vid_dev->displays[k]);
2147 }
2148 kfree(vid_dev);
2149 return 0;
2150}
2151
2152static int __init omap_vout_probe(struct platform_device *pdev)
2153{
2154 int ret = 0, i;
2155 struct omap_overlay *ovl;
2156 struct omap_dss_device *dssdev = NULL;
2157 struct omap_dss_device *def_display;
2158 struct omap2video_device *vid_dev = NULL;
2159
2160 if (pdev->num_resources == 0) {
2161 dev_err(&pdev->dev, "probed for an unknown device\n");
2162 return -ENODEV;
2163 }
2164
2165 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2166 if (vid_dev == NULL)
2167 return -ENOMEM;
2168
2169 vid_dev->num_displays = 0;
2170 for_each_dss_dev(dssdev) {
2171 omap_dss_get_device(dssdev);
2172 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2173 }
2174
2175 if (vid_dev->num_displays == 0) {
2176 dev_err(&pdev->dev, "no displays\n");
2177 ret = -EINVAL;
2178 goto probe_err0;
2179 }
2180
2181 vid_dev->num_overlays = omap_dss_get_num_overlays();
2182 for (i = 0; i < vid_dev->num_overlays; i++)
2183 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2184
2185 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2186 for (i = 0; i < vid_dev->num_managers; i++)
2187 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2188
2189 /* Get the Video1 overlay and video2 overlay.
2190 * Setup the Display attached to that overlays
2191 */
2192 for (i = 1; i < vid_dev->num_overlays; i++) {
2193 ovl = omap_dss_get_overlay(i);
2194 if (ovl->manager && ovl->manager->device) {
2195 def_display = ovl->manager->device;
2196 } else {
2197 dev_warn(&pdev->dev, "cannot find display\n");
2198 def_display = NULL;
2199 }
2200 if (def_display) {
5ba9bb0e
VH
2201 struct omap_dss_driver *dssdrv = def_display->driver;
2202
2203 ret = dssdrv->enable(def_display);
5c7ab634
VH
2204 if (ret) {
2205 /* Here we are not considering a error
2206 * as display may be enabled by frame
2207 * buffer driver
2208 */
2209 dev_warn(&pdev->dev,
2210 "'%s' Display already enabled\n",
2211 def_display->name);
2212 }
5c7ab634
VH
2213 }
2214 }
2215
2216 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2217 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2218 ret = -ENODEV;
2219 goto probe_err1;
2220 }
2221
2222 ret = omap_vout_create_video_devices(pdev);
2223 if (ret)
2224 goto probe_err2;
2225
2226 for (i = 0; i < vid_dev->num_displays; i++) {
2227 struct omap_dss_device *display = vid_dev->displays[i];
2228
5ba9bb0e
VH
2229 if (display->driver->update)
2230 display->driver->update(display, 0, 0,
5c7ab634
VH
2231 display->panel.timings.x_res,
2232 display->panel.timings.y_res);
2233 }
2234 return 0;
2235
2236probe_err2:
2237 v4l2_device_unregister(&vid_dev->v4l2_dev);
2238probe_err1:
2239 for (i = 1; i < vid_dev->num_overlays; i++) {
2240 def_display = NULL;
2241 ovl = omap_dss_get_overlay(i);
2242 if (ovl->manager && ovl->manager->device)
2243 def_display = ovl->manager->device;
2244
5ba9bb0e
VH
2245 if (def_display && def_display->driver)
2246 def_display->driver->disable(def_display);
5c7ab634
VH
2247 }
2248probe_err0:
2249 kfree(vid_dev);
2250 return ret;
2251}
2252
2253static struct platform_driver omap_vout_driver = {
2254 .driver = {
2255 .name = VOUT_NAME,
2256 },
2257 .probe = omap_vout_probe,
2258 .remove = omap_vout_remove,
2259};
2260
2261static int __init omap_vout_init(void)
2262{
2263 if (platform_driver_register(&omap_vout_driver) != 0) {
2264 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2265 return -EINVAL;
2266 }
2267 return 0;
2268}
2269
2270static void omap_vout_cleanup(void)
2271{
2272 platform_driver_unregister(&omap_vout_driver);
2273}
2274
2275late_initcall(omap_vout_init);
2276module_exit(omap_vout_cleanup);