]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/video/omap/omap_vout.c
video/omap: fix build dependencies
[mirror_ubuntu-bionic-kernel.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 */
836static void omap_vout_vm_open(struct vm_area_struct *vma)
837{
838 struct omap_vout_device *vout = vma->vm_private_data;
839
840 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
841 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
842 vout->mmap_count++;
843}
844
845static void omap_vout_vm_close(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_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
851 vout->mmap_count--;
852}
853
854static struct vm_operations_struct omap_vout_vm_ops = {
855 .open = omap_vout_vm_open,
856 .close = omap_vout_vm_close,
857};
858
859static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
860{
861 int i;
862 void *pos;
863 unsigned long start = vma->vm_start;
864 unsigned long size = (vma->vm_end - vma->vm_start);
5c7ab634
VH
865 struct omap_vout_device *vout = file->private_data;
866 struct videobuf_queue *q = &vout->vbq;
867
868 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
869 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
870 vma->vm_pgoff, vma->vm_start, vma->vm_end);
871
872 /* look for the buffer to map */
873 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
874 if (NULL == q->bufs[i])
875 continue;
876 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
877 continue;
878 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
879 break;
880 }
881
882 if (VIDEO_MAX_FRAME == i) {
883 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
884 "offset invalid [offset=0x%lx]\n",
885 (vma->vm_pgoff << PAGE_SHIFT));
886 return -EINVAL;
887 }
383e4f69
VH
888 /* Check the size of the buffer */
889 if (size > vout->buffer_size) {
890 v4l2_err(&vout->vid_dev->v4l2_dev,
891 "insufficient memory [%lu] [%u]\n",
892 size, vout->buffer_size);
893 return -ENOMEM;
894 }
895
5c7ab634
VH
896 q->bufs[i]->baddr = vma->vm_start;
897
898 vma->vm_flags |= VM_RESERVED;
899 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
900 vma->vm_ops = &omap_vout_vm_ops;
901 vma->vm_private_data = (void *) vout;
dd880dd4 902 pos = (void *)vout->buf_virt_addr[i];
5c7ab634
VH
903 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
904 while (size > 0) {
905 unsigned long pfn;
906 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
907 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
908 return -EAGAIN;
909 start += PAGE_SIZE;
910 pos += PAGE_SIZE;
911 size -= PAGE_SIZE;
912 }
913 vout->mmap_count++;
914 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
915
916 return 0;
917}
918
919static int omap_vout_release(struct file *file)
920{
921 unsigned int ret, i;
922 struct videobuf_queue *q;
923 struct omapvideo_info *ovid;
924 struct omap_vout_device *vout = file->private_data;
925
926 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
927 ovid = &vout->vid_info;
928
929 if (!vout)
930 return 0;
931
932 q = &vout->vbq;
933 /* Disable all the overlay managers connected with this interface */
934 for (i = 0; i < ovid->num_overlays; i++) {
935 struct omap_overlay *ovl = ovid->overlays[i];
936 if (ovl->manager && ovl->manager->device) {
937 struct omap_overlay_info info;
938 ovl->get_overlay_info(ovl, &info);
939 info.enabled = 0;
940 ovl->set_overlay_info(ovl, &info);
941 }
942 }
943 /* Turn off the pipeline */
944 ret = omapvid_apply_changes(vout);
945 if (ret)
946 v4l2_warn(&vout->vid_dev->v4l2_dev,
947 "Unable to apply changes\n");
948
949 /* Free all buffers */
445e258f
AT
950 omap_vout_free_extra_buffers(vout);
951
952 /* Free the VRFB buffers only if they are allocated
953 * during reqbufs. Don't free if init time allocated
954 */
955 if (ovid->rotation_type == VOUT_ROT_VRFB) {
956 if (!vout->vrfb_static_allocation)
957 omap_vout_free_vrfb_buffers(vout);
958 }
5c7ab634
VH
959 videobuf_mmap_free(q);
960
961 /* Even if apply changes fails we should continue
b595076a 962 freeing allocated memory */
5c7ab634
VH
963 if (vout->streaming) {
964 u32 mask = 0;
965
966 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
5251dd6c 967 DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
5c7ab634
VH
968 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
969 vout->streaming = 0;
970
971 videobuf_streamoff(q);
972 videobuf_queue_cancel(q);
973 }
974
975 if (vout->mmap_count != 0)
976 vout->mmap_count = 0;
977
978 vout->opened -= 1;
979 file->private_data = NULL;
980
981 if (vout->buffer_allocated)
982 videobuf_mmap_free(q);
983
984 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
985 return ret;
986}
987
988static int omap_vout_open(struct file *file)
989{
990 struct videobuf_queue *q;
991 struct omap_vout_device *vout = NULL;
992
993 vout = video_drvdata(file);
994 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
995
996 if (vout == NULL)
997 return -ENODEV;
998
999 /* for now, we only support single open */
1000 if (vout->opened)
1001 return -EBUSY;
1002
1003 vout->opened += 1;
1004
1005 file->private_data = vout;
1006 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1007
1008 q = &vout->vbq;
1009 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1010 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1011 video_vbq_ops.buf_release = omap_vout_buffer_release;
1012 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1013 spin_lock_init(&vout->vbq_lock);
1014
dd880dd4
VH
1015 videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1016 &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
e3cfd447 1017 sizeof(struct videobuf_buffer), vout, NULL);
5c7ab634
VH
1018
1019 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1020 return 0;
1021}
1022
1023/*
1024 * V4L2 ioctls
1025 */
1026static int vidioc_querycap(struct file *file, void *fh,
1027 struct v4l2_capability *cap)
1028{
1029 struct omap_vout_device *vout = fh;
1030
1031 strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1032 strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1033 cap->bus_info[0] = '\0';
1034 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
1035
1036 return 0;
1037}
1038
1039static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1040 struct v4l2_fmtdesc *fmt)
1041{
1042 int index = fmt->index;
5c7ab634 1043
5c7ab634
VH
1044 if (index >= NUM_OUTPUT_FORMATS)
1045 return -EINVAL;
1046
1047 fmt->flags = omap_formats[index].flags;
1048 strlcpy(fmt->description, omap_formats[index].description,
1049 sizeof(fmt->description));
1050 fmt->pixelformat = omap_formats[index].pixelformat;
1051
1052 return 0;
1053}
1054
1055static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1056 struct v4l2_format *f)
1057{
1058 struct omap_vout_device *vout = fh;
1059
1060 f->fmt.pix = vout->pix;
1061 return 0;
1062
1063}
1064
1065static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1066 struct v4l2_format *f)
1067{
1068 struct omap_overlay *ovl;
1069 struct omapvideo_info *ovid;
1070 struct omap_video_timings *timing;
1071 struct omap_vout_device *vout = fh;
1072
1073 ovid = &vout->vid_info;
1074 ovl = ovid->overlays[0];
1075
1076 if (!ovl->manager || !ovl->manager->device)
1077 return -EINVAL;
1078 /* get the display device attached to the overlay */
1079 timing = &ovl->manager->device->panel.timings;
1080
1081 vout->fbuf.fmt.height = timing->y_res;
1082 vout->fbuf.fmt.width = timing->x_res;
1083
1084 omap_vout_try_format(&f->fmt.pix);
1085 return 0;
1086}
1087
1088static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1089 struct v4l2_format *f)
1090{
1091 int ret, bpp;
1092 struct omap_overlay *ovl;
1093 struct omapvideo_info *ovid;
1094 struct omap_video_timings *timing;
1095 struct omap_vout_device *vout = fh;
1096
1097 if (vout->streaming)
1098 return -EBUSY;
1099
1100 mutex_lock(&vout->lock);
1101
1102 ovid = &vout->vid_info;
1103 ovl = ovid->overlays[0];
1104
1105 /* get the display device attached to the overlay */
1106 if (!ovl->manager || !ovl->manager->device) {
1107 ret = -EINVAL;
1108 goto s_fmt_vid_out_exit;
1109 }
1110 timing = &ovl->manager->device->panel.timings;
1111
1112 /* We dont support RGB24-packed mode if vrfb rotation
1113 * is enabled*/
b366888a 1114 if ((is_rotation_enabled(vout)) &&
5c7ab634
VH
1115 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1116 ret = -EINVAL;
1117 goto s_fmt_vid_out_exit;
1118 }
1119
1120 /* get the framebuffer parameters */
1121
b366888a 1122 if (is_rotation_90_or_270(vout)) {
5c7ab634
VH
1123 vout->fbuf.fmt.height = timing->x_res;
1124 vout->fbuf.fmt.width = timing->y_res;
1125 } else {
1126 vout->fbuf.fmt.height = timing->y_res;
1127 vout->fbuf.fmt.width = timing->x_res;
1128 }
1129
1130 /* change to samller size is OK */
1131
1132 bpp = omap_vout_try_format(&f->fmt.pix);
1133 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1134
1135 /* try & set the new output format */
1136 vout->bpp = bpp;
1137 vout->pix = f->fmt.pix;
1138 vout->vrfb_bpp = 1;
1139
1140 /* If YUYV then vrfb bpp is 2, for others its 1 */
1141 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1142 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1143 vout->vrfb_bpp = 2;
1144
1145 /* set default crop and win */
1146 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1147
1148 /* Save the changes in the overlay strcuture */
1149 ret = omapvid_init(vout, 0);
1150 if (ret) {
1151 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1152 goto s_fmt_vid_out_exit;
1153 }
1154
1155 ret = 0;
1156
1157s_fmt_vid_out_exit:
1158 mutex_unlock(&vout->lock);
1159 return ret;
1160}
1161
1162static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1163 struct v4l2_format *f)
1164{
1165 int ret = 0;
1166 struct omap_vout_device *vout = fh;
1167 struct v4l2_window *win = &f->fmt.win;
1168
1169 ret = omap_vout_try_window(&vout->fbuf, win);
1170
1171 if (!ret) {
1172 if (vout->vid == OMAP_VIDEO1)
1173 win->global_alpha = 255;
1174 else
1175 win->global_alpha = f->fmt.win.global_alpha;
1176 }
1177
1178 return ret;
1179}
1180
1181static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1182 struct v4l2_format *f)
1183{
1184 int ret = 0;
1185 struct omap_overlay *ovl;
1186 struct omapvideo_info *ovid;
1187 struct omap_vout_device *vout = fh;
1188 struct v4l2_window *win = &f->fmt.win;
1189
1190 mutex_lock(&vout->lock);
1191 ovid = &vout->vid_info;
1192 ovl = ovid->overlays[0];
1193
1194 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1195 if (!ret) {
1196 /* Video1 plane does not support global alpha */
1197 if (ovl->id == OMAP_DSS_VIDEO1)
1198 vout->win.global_alpha = 255;
1199 else
1200 vout->win.global_alpha = f->fmt.win.global_alpha;
1201
1202 vout->win.chromakey = f->fmt.win.chromakey;
1203 }
1204 mutex_unlock(&vout->lock);
1205 return ret;
1206}
1207
1208static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
1209 struct v4l2_fmtdesc *fmt)
1210{
1211 int index = fmt->index;
5c7ab634 1212
5c7ab634
VH
1213 if (index >= NUM_OUTPUT_FORMATS)
1214 return -EINVAL;
1215
1216 fmt->flags = omap_formats[index].flags;
1217 strlcpy(fmt->description, omap_formats[index].description,
1218 sizeof(fmt->description));
1219 fmt->pixelformat = omap_formats[index].pixelformat;
1220 return 0;
1221}
1222
1223static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1224 struct v4l2_format *f)
1225{
1226 u32 key_value = 0;
1227 struct omap_overlay *ovl;
1228 struct omapvideo_info *ovid;
1229 struct omap_vout_device *vout = fh;
1230 struct omap_overlay_manager_info info;
1231 struct v4l2_window *win = &f->fmt.win;
1232
1233 ovid = &vout->vid_info;
1234 ovl = ovid->overlays[0];
1235
1236 win->w = vout->win.w;
1237 win->field = vout->win.field;
1238 win->global_alpha = vout->win.global_alpha;
1239
1240 if (ovl->manager && ovl->manager->get_manager_info) {
1241 ovl->manager->get_manager_info(ovl->manager, &info);
1242 key_value = info.trans_key;
1243 }
1244 win->chromakey = key_value;
1245 return 0;
1246}
1247
1248static int vidioc_cropcap(struct file *file, void *fh,
1249 struct v4l2_cropcap *cropcap)
1250{
1251 struct omap_vout_device *vout = fh;
1252 struct v4l2_pix_format *pix = &vout->pix;
1253
1254 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1255 return -EINVAL;
1256
1257 /* Width and height are always even */
1258 cropcap->bounds.width = pix->width & ~1;
1259 cropcap->bounds.height = pix->height & ~1;
1260
1261 omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
1262 cropcap->pixelaspect.numerator = 1;
1263 cropcap->pixelaspect.denominator = 1;
1264 return 0;
1265}
1266
1267static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1268{
1269 struct omap_vout_device *vout = fh;
1270
1271 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1272 return -EINVAL;
1273 crop->c = vout->crop;
1274 return 0;
1275}
1276
1277static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
1278{
1279 int ret = -EINVAL;
1280 struct omap_vout_device *vout = fh;
1281 struct omapvideo_info *ovid;
1282 struct omap_overlay *ovl;
1283 struct omap_video_timings *timing;
1284
1285 if (vout->streaming)
1286 return -EBUSY;
1287
1288 mutex_lock(&vout->lock);
1289 ovid = &vout->vid_info;
1290 ovl = ovid->overlays[0];
1291
1292 if (!ovl->manager || !ovl->manager->device) {
1293 ret = -EINVAL;
1294 goto s_crop_err;
1295 }
1296 /* get the display device attached to the overlay */
1297 timing = &ovl->manager->device->panel.timings;
1298
b366888a 1299 if (is_rotation_90_or_270(vout)) {
5c7ab634
VH
1300 vout->fbuf.fmt.height = timing->x_res;
1301 vout->fbuf.fmt.width = timing->y_res;
1302 } else {
1303 vout->fbuf.fmt.height = timing->y_res;
1304 vout->fbuf.fmt.width = timing->x_res;
1305 }
1306
1307 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1308 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1309 &vout->fbuf, &crop->c);
1310
1311s_crop_err:
1312 mutex_unlock(&vout->lock);
1313 return ret;
1314}
1315
1316static int vidioc_queryctrl(struct file *file, void *fh,
1317 struct v4l2_queryctrl *ctrl)
1318{
1319 int ret = 0;
1320
1321 switch (ctrl->id) {
1322 case V4L2_CID_ROTATE:
1323 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1324 break;
1325 case V4L2_CID_BG_COLOR:
1326 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1327 break;
1328 case V4L2_CID_VFLIP:
1329 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1330 break;
1331 default:
1332 ctrl->name[0] = '\0';
1333 ret = -EINVAL;
1334 }
1335 return ret;
1336}
1337
1338static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1339{
1340 int ret = 0;
1341 struct omap_vout_device *vout = fh;
1342
1343 switch (ctrl->id) {
1344 case V4L2_CID_ROTATE:
1345 ctrl->value = vout->control[0].value;
1346 break;
1347 case V4L2_CID_BG_COLOR:
1348 {
1349 struct omap_overlay_manager_info info;
1350 struct omap_overlay *ovl;
1351
1352 ovl = vout->vid_info.overlays[0];
1353 if (!ovl->manager || !ovl->manager->get_manager_info) {
1354 ret = -EINVAL;
1355 break;
1356 }
1357
1358 ovl->manager->get_manager_info(ovl->manager, &info);
1359 ctrl->value = info.default_color;
1360 break;
1361 }
1362 case V4L2_CID_VFLIP:
1363 ctrl->value = vout->control[2].value;
1364 break;
1365 default:
1366 ret = -EINVAL;
1367 }
1368 return ret;
1369}
1370
1371static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1372{
1373 int ret = 0;
1374 struct omap_vout_device *vout = fh;
1375
1376 switch (a->id) {
1377 case V4L2_CID_ROTATE:
1378 {
445e258f 1379 struct omapvideo_info *ovid;
5c7ab634
VH
1380 int rotation = a->value;
1381
445e258f
AT
1382 ovid = &vout->vid_info;
1383
5c7ab634 1384 mutex_lock(&vout->lock);
445e258f
AT
1385 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1386 mutex_unlock(&vout->lock);
1387 ret = -ERANGE;
1388 break;
1389 }
5c7ab634
VH
1390
1391 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1392 mutex_unlock(&vout->lock);
1393 ret = -EINVAL;
1394 break;
1395 }
1396
1397 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1398 vout->mirror)) {
1399 mutex_unlock(&vout->lock);
1400 ret = -EINVAL;
1401 break;
1402 }
1403
1404 vout->control[0].value = rotation;
1405 mutex_unlock(&vout->lock);
1406 break;
1407 }
1408 case V4L2_CID_BG_COLOR:
1409 {
1410 struct omap_overlay *ovl;
1411 unsigned int color = a->value;
1412 struct omap_overlay_manager_info info;
1413
1414 ovl = vout->vid_info.overlays[0];
1415
1416 mutex_lock(&vout->lock);
1417 if (!ovl->manager || !ovl->manager->get_manager_info) {
1418 mutex_unlock(&vout->lock);
1419 ret = -EINVAL;
1420 break;
1421 }
1422
1423 ovl->manager->get_manager_info(ovl->manager, &info);
1424 info.default_color = color;
1425 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1426 mutex_unlock(&vout->lock);
1427 ret = -EINVAL;
1428 break;
1429 }
1430
1431 vout->control[1].value = color;
1432 mutex_unlock(&vout->lock);
1433 break;
1434 }
1435 case V4L2_CID_VFLIP:
1436 {
1437 struct omap_overlay *ovl;
1438 struct omapvideo_info *ovid;
1439 unsigned int mirror = a->value;
1440
1441 ovid = &vout->vid_info;
1442 ovl = ovid->overlays[0];
1443
1444 mutex_lock(&vout->lock);
445e258f
AT
1445 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1446 mutex_unlock(&vout->lock);
1447 ret = -ERANGE;
1448 break;
1449 }
5c7ab634
VH
1450
1451 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1452 mutex_unlock(&vout->lock);
1453 ret = -EINVAL;
1454 break;
1455 }
1456 vout->mirror = mirror;
1457 vout->control[2].value = mirror;
1458 mutex_unlock(&vout->lock);
1459 break;
1460 }
1461 default:
1462 ret = -EINVAL;
1463 }
1464 return ret;
1465}
1466
1467static int vidioc_reqbufs(struct file *file, void *fh,
1468 struct v4l2_requestbuffers *req)
1469{
1470 int ret = 0;
1471 unsigned int i, num_buffers = 0;
1472 struct omap_vout_device *vout = fh;
1473 struct videobuf_queue *q = &vout->vbq;
5c7ab634
VH
1474
1475 if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
1476 return -EINVAL;
1477 /* if memory is not mmp or userptr
1478 return error */
1479 if ((V4L2_MEMORY_MMAP != req->memory) &&
1480 (V4L2_MEMORY_USERPTR != req->memory))
1481 return -EINVAL;
1482
1483 mutex_lock(&vout->lock);
1484 /* Cannot be requested when streaming is on */
1485 if (vout->streaming) {
1486 ret = -EBUSY;
1487 goto reqbuf_err;
1488 }
1489
1490 /* If buffers are already allocated free them */
1491 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1492 if (vout->mmap_count) {
1493 ret = -EBUSY;
1494 goto reqbuf_err;
1495 }
1496 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1497 video1_numbuffers : video2_numbuffers;
1498 for (i = num_buffers; i < vout->buffer_allocated; i++) {
dd880dd4 1499 omap_vout_free_buffer(vout->buf_virt_addr[i],
5c7ab634
VH
1500 vout->buffer_size);
1501 vout->buf_virt_addr[i] = 0;
1502 vout->buf_phy_addr[i] = 0;
1503 }
1504 vout->buffer_allocated = num_buffers;
1505 videobuf_mmap_free(q);
1506 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1507 if (vout->buffer_allocated) {
1508 videobuf_mmap_free(q);
1509 for (i = 0; i < vout->buffer_allocated; i++) {
1510 kfree(q->bufs[i]);
1511 q->bufs[i] = NULL;
1512 }
1513 vout->buffer_allocated = 0;
1514 }
1515 }
1516
1517 /*store the memory type in data structure */
1518 vout->memory = req->memory;
1519
1520 INIT_LIST_HEAD(&vout->dma_queue);
1521
1522 /* call videobuf_reqbufs api */
1523 ret = videobuf_reqbufs(q, req);
1524 if (ret < 0)
1525 goto reqbuf_err;
1526
1527 vout->buffer_allocated = req->count;
dd880dd4 1528
5c7ab634
VH
1529reqbuf_err:
1530 mutex_unlock(&vout->lock);
1531 return ret;
1532}
1533
1534static int vidioc_querybuf(struct file *file, void *fh,
1535 struct v4l2_buffer *b)
1536{
1537 struct omap_vout_device *vout = fh;
1538
1539 return videobuf_querybuf(&vout->vbq, b);
1540}
1541
1542static int vidioc_qbuf(struct file *file, void *fh,
1543 struct v4l2_buffer *buffer)
1544{
1545 struct omap_vout_device *vout = fh;
1546 struct videobuf_queue *q = &vout->vbq;
1547
1548 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1549 (buffer->index >= vout->buffer_allocated) ||
1550 (q->bufs[buffer->index]->memory != buffer->memory)) {
1551 return -EINVAL;
1552 }
1553 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1554 if ((buffer->length < vout->pix.sizeimage) ||
1555 (0 == buffer->m.userptr)) {
1556 return -EINVAL;
1557 }
1558 }
1559
b366888a 1560 if ((is_rotation_enabled(vout)) &&
5c7ab634
VH
1561 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1562 v4l2_warn(&vout->vid_dev->v4l2_dev,
1563 "DMA Channel not allocated for Rotation\n");
1564 return -EINVAL;
1565 }
1566
1567 return videobuf_qbuf(q, buffer);
1568}
1569
1570static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1571{
1572 struct omap_vout_device *vout = fh;
1573 struct videobuf_queue *q = &vout->vbq;
1574
72915e85
AJ
1575 int ret;
1576 u32 addr;
1577 unsigned long size;
1578 struct videobuf_buffer *vb;
1579
1580 vb = q->bufs[b->index];
1581
5c7ab634
VH
1582 if (!vout->streaming)
1583 return -EINVAL;
1584
1585 if (file->f_flags & O_NONBLOCK)
1586 /* Call videobuf_dqbuf for non blocking mode */
72915e85 1587 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
5c7ab634
VH
1588 else
1589 /* Call videobuf_dqbuf for blocking mode */
72915e85
AJ
1590 ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
1591
1592 addr = (unsigned long) vout->buf_phy_addr[vb->i];
1593 size = (unsigned long) vb->size;
1594 dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
1595 size, DMA_TO_DEVICE);
1596 return ret;
5c7ab634
VH
1597}
1598
1599static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1600{
1601 int ret = 0, j;
1602 u32 addr = 0, mask = 0;
1603 struct omap_vout_device *vout = fh;
1604 struct videobuf_queue *q = &vout->vbq;
1605 struct omapvideo_info *ovid = &vout->vid_info;
1606
1607 mutex_lock(&vout->lock);
1608
1609 if (vout->streaming) {
1610 ret = -EBUSY;
1611 goto streamon_err;
1612 }
1613
1614 ret = videobuf_streamon(q);
1615 if (ret)
1616 goto streamon_err;
1617
1618 if (list_empty(&vout->dma_queue)) {
1619 ret = -EIO;
1620 goto streamon_err1;
1621 }
1622
1623 /* Get the next frame from the buffer queue */
1624 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1625 struct videobuf_buffer, queue);
1626 /* Remove buffer from the buffer queue */
1627 list_del(&vout->cur_frm->queue);
1628 /* Mark state of the current frame to active */
1629 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1630 /* Initialize field_id and started member */
1631 vout->field_id = 0;
1632
1633 /* set flag here. Next QBUF will start DMA */
1634 vout->streaming = 1;
1635
1636 vout->first_int = 1;
1637
1638 if (omap_vout_calculate_offset(vout)) {
1639 ret = -EINVAL;
1640 goto streamon_err1;
1641 }
1642 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1643 + vout->cropped_offset;
1644
5251dd6c
AJ
1645 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1646 | DISPC_IRQ_VSYNC2;
5c7ab634
VH
1647
1648 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1649
1650 for (j = 0; j < ovid->num_overlays; j++) {
1651 struct omap_overlay *ovl = ovid->overlays[j];
1652
1653 if (ovl->manager && ovl->manager->device) {
1654 struct omap_overlay_info info;
1655 ovl->get_overlay_info(ovl, &info);
1656 info.enabled = 1;
1657 info.paddr = addr;
1658 if (ovl->set_overlay_info(ovl, &info)) {
1659 ret = -EINVAL;
1660 goto streamon_err1;
1661 }
1662 }
1663 }
1664
1665 /* First save the configuration in ovelray structure */
1666 ret = omapvid_init(vout, addr);
1667 if (ret)
1668 v4l2_err(&vout->vid_dev->v4l2_dev,
1669 "failed to set overlay info\n");
1670 /* Enable the pipeline and set the Go bit */
1671 ret = omapvid_apply_changes(vout);
1672 if (ret)
1673 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1674
1675 ret = 0;
1676
1677streamon_err1:
1678 if (ret)
1679 ret = videobuf_streamoff(q);
1680streamon_err:
1681 mutex_unlock(&vout->lock);
1682 return ret;
1683}
1684
1685static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1686{
1687 u32 mask = 0;
1688 int ret = 0, j;
1689 struct omap_vout_device *vout = fh;
1690 struct omapvideo_info *ovid = &vout->vid_info;
1691
1692 if (!vout->streaming)
1693 return -EINVAL;
1694
1695 vout->streaming = 0;
5251dd6c
AJ
1696 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1697 | DISPC_IRQ_VSYNC2;
5c7ab634
VH
1698
1699 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1700
1701 for (j = 0; j < ovid->num_overlays; j++) {
1702 struct omap_overlay *ovl = ovid->overlays[j];
1703
1704 if (ovl->manager && ovl->manager->device) {
1705 struct omap_overlay_info info;
1706
1707 ovl->get_overlay_info(ovl, &info);
1708 info.enabled = 0;
1709 ret = ovl->set_overlay_info(ovl, &info);
1710 if (ret)
1711 v4l2_err(&vout->vid_dev->v4l2_dev,
1712 "failed to update overlay info in streamoff\n");
1713 }
1714 }
1715
1716 /* Turn of the pipeline */
1717 ret = omapvid_apply_changes(vout);
1718 if (ret)
1719 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
1720 " streamoff\n");
1721
1722 INIT_LIST_HEAD(&vout->dma_queue);
1723 ret = videobuf_streamoff(&vout->vbq);
1724
1725 return ret;
1726}
1727
1728static int vidioc_s_fbuf(struct file *file, void *fh,
1729 struct v4l2_framebuffer *a)
1730{
1731 int enable = 0;
1732 struct omap_overlay *ovl;
1733 struct omapvideo_info *ovid;
1734 struct omap_vout_device *vout = fh;
1735 struct omap_overlay_manager_info info;
1736 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1737
1738 ovid = &vout->vid_info;
1739 ovl = ovid->overlays[0];
1740
1741 /* OMAP DSS doesn't support Source and Destination color
1742 key together */
1743 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1744 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1745 return -EINVAL;
1746 /* OMAP DSS Doesn't support the Destination color key
1747 and alpha blending together */
1748 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1749 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1750 return -EINVAL;
1751
1752 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1753 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1754 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
1755 } else
1756 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1757
1758 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1759 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1760 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1761 } else
1762 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
1763
1764 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1765 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1766 enable = 1;
1767 else
1768 enable = 0;
1769 if (ovl->manager && ovl->manager->get_manager_info &&
1770 ovl->manager->set_manager_info) {
1771
1772 ovl->manager->get_manager_info(ovl->manager, &info);
1773 info.trans_enabled = enable;
1774 info.trans_key_type = key_type;
1775 info.trans_key = vout->win.chromakey;
1776
1777 if (ovl->manager->set_manager_info(ovl->manager, &info))
1778 return -EINVAL;
1779 }
1780 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1781 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1782 enable = 1;
1783 } else {
1784 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1785 enable = 0;
1786 }
1787 if (ovl->manager && ovl->manager->get_manager_info &&
1788 ovl->manager->set_manager_info) {
1789 ovl->manager->get_manager_info(ovl->manager, &info);
1790 info.alpha_enabled = enable;
1791 if (ovl->manager->set_manager_info(ovl->manager, &info))
1792 return -EINVAL;
1793 }
1794
1795 return 0;
1796}
1797
1798static int vidioc_g_fbuf(struct file *file, void *fh,
1799 struct v4l2_framebuffer *a)
1800{
1801 struct omap_overlay *ovl;
1802 struct omapvideo_info *ovid;
1803 struct omap_vout_device *vout = fh;
1804 struct omap_overlay_manager_info info;
1805
1806 ovid = &vout->vid_info;
1807 ovl = ovid->overlays[0];
1808
1809 a->flags = 0x0;
1810 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1811 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1812
1813 if (ovl->manager && ovl->manager->get_manager_info) {
1814 ovl->manager->get_manager_info(ovl->manager, &info);
1815 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1816 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1817 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1818 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1819 }
1820 if (ovl->manager && ovl->manager->get_manager_info) {
1821 ovl->manager->get_manager_info(ovl->manager, &info);
1822 if (info.alpha_enabled)
1823 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1824 }
1825
1826 return 0;
1827}
1828
1829static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1830 .vidioc_querycap = vidioc_querycap,
1831 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1832 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1833 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1834 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1835 .vidioc_queryctrl = vidioc_queryctrl,
1836 .vidioc_g_ctrl = vidioc_g_ctrl,
1837 .vidioc_s_fbuf = vidioc_s_fbuf,
1838 .vidioc_g_fbuf = vidioc_g_fbuf,
1839 .vidioc_s_ctrl = vidioc_s_ctrl,
1840 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1841 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1842 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1843 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1844 .vidioc_cropcap = vidioc_cropcap,
1845 .vidioc_g_crop = vidioc_g_crop,
1846 .vidioc_s_crop = vidioc_s_crop,
1847 .vidioc_reqbufs = vidioc_reqbufs,
1848 .vidioc_querybuf = vidioc_querybuf,
1849 .vidioc_qbuf = vidioc_qbuf,
1850 .vidioc_dqbuf = vidioc_dqbuf,
1851 .vidioc_streamon = vidioc_streamon,
1852 .vidioc_streamoff = vidioc_streamoff,
1853};
1854
1855static const struct v4l2_file_operations omap_vout_fops = {
1856 .owner = THIS_MODULE,
1857 .unlocked_ioctl = video_ioctl2,
1858 .mmap = omap_vout_mmap,
1859 .open = omap_vout_open,
1860 .release = omap_vout_release,
1861};
1862
1863/* Init functions used during driver initialization */
1864/* Initial setup of video_data */
1865static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1866{
1867 struct video_device *vfd;
1868 struct v4l2_pix_format *pix;
1869 struct v4l2_control *control;
1870 struct omap_dss_device *display =
1871 vout->vid_info.overlays[0]->manager->device;
1872
1873 /* set the default pix */
1874 pix = &vout->pix;
1875
1876 /* Set the default picture of QVGA */
1877 pix->width = QQVGA_WIDTH;
1878 pix->height = QQVGA_HEIGHT;
1879
1880 /* Default pixel format is RGB 5-6-5 */
1881 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1882 pix->field = V4L2_FIELD_ANY;
1883 pix->bytesperline = pix->width * 2;
1884 pix->sizeimage = pix->bytesperline * pix->height;
1885 pix->priv = 0;
1886 pix->colorspace = V4L2_COLORSPACE_JPEG;
1887
1888 vout->bpp = RGB565_BPP;
1889 vout->fbuf.fmt.width = display->panel.timings.x_res;
1890 vout->fbuf.fmt.height = display->panel.timings.y_res;
1891
1892 /* Set the data structures for the overlay parameters*/
1893 vout->win.global_alpha = 255;
1894 vout->fbuf.flags = 0;
1895 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1896 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1897 vout->win.chromakey = 0;
1898
1899 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1900
1901 /*Initialize the control variables for
1902 rotation, flipping and background color. */
1903 control = vout->control;
1904 control[0].id = V4L2_CID_ROTATE;
1905 control[0].value = 0;
1906 vout->rotation = 0;
1907 vout->mirror = 0;
1908 vout->control[2].id = V4L2_CID_HFLIP;
1909 vout->control[2].value = 0;
445e258f
AT
1910 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1911 vout->vrfb_bpp = 2;
5c7ab634
VH
1912
1913 control[1].id = V4L2_CID_BG_COLOR;
1914 control[1].value = 0;
1915
1916 /* initialize the video_device struct */
1917 vfd = vout->vfd = video_device_alloc();
1918
1919 if (!vfd) {
1920 printk(KERN_ERR VOUT_NAME ": could not allocate"
1921 " video device struct\n");
1922 return -ENOMEM;
1923 }
1924 vfd->release = video_device_release;
1925 vfd->ioctl_ops = &vout_ioctl_ops;
1926
1927 strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1928
5c7ab634
VH
1929 vfd->fops = &omap_vout_fops;
1930 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
1931 mutex_init(&vout->lock);
1932
1933 vfd->minor = -1;
1934 return 0;
1935
1936}
1937
1938/* Setup video buffers */
1939static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1940 int vid_num)
1941{
1942 u32 numbuffers;
445e258f
AT
1943 int ret = 0, i;
1944 struct omapvideo_info *ovid;
5c7ab634 1945 struct omap_vout_device *vout;
5c7ab634
VH
1946 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1947 struct omap2video_device *vid_dev =
1948 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1949
1950 vout = vid_dev->vouts[vid_num];
445e258f 1951 ovid = &vout->vid_info;
5c7ab634
VH
1952
1953 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1954 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1955 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1956
1957 for (i = 0; i < numbuffers; i++) {
1958 vout->buf_virt_addr[i] =
1959 omap_vout_alloc_buffer(vout->buffer_size,
1960 (u32 *) &vout->buf_phy_addr[i]);
1961 if (!vout->buf_virt_addr[i]) {
1962 numbuffers = i;
1963 ret = -ENOMEM;
1964 goto free_buffers;
1965 }
1966 }
1967
5c7ab634
VH
1968 vout->cropped_offset = 0;
1969
445e258f
AT
1970 if (ovid->rotation_type == VOUT_ROT_VRFB) {
1971 int static_vrfb_allocation = (vid_num == 0) ?
1972 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1973 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1974 static_vrfb_allocation);
5c7ab634 1975 }
5c7ab634 1976
445e258f 1977 return ret;
5c7ab634
VH
1978
1979free_buffers:
1980 for (i = 0; i < numbuffers; i++) {
1981 omap_vout_free_buffer(vout->buf_virt_addr[i],
1982 vout->buffer_size);
1983 vout->buf_virt_addr[i] = 0;
1984 vout->buf_phy_addr[i] = 0;
1985 }
1986 return ret;
1987
1988}
1989
1990/* Create video out devices */
1991static int __init omap_vout_create_video_devices(struct platform_device *pdev)
1992{
1993 int ret = 0, k;
1994 struct omap_vout_device *vout;
1995 struct video_device *vfd = NULL;
1996 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1997 struct omap2video_device *vid_dev = container_of(v4l2_dev,
1998 struct omap2video_device, v4l2_dev);
1999
2000 for (k = 0; k < pdev->num_resources; k++) {
2001
2ef17c9f 2002 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
5c7ab634
VH
2003 if (!vout) {
2004 dev_err(&pdev->dev, ": could not allocate memory\n");
2005 return -ENOMEM;
2006 }
5c7ab634
VH
2007
2008 vout->vid = k;
2009 vid_dev->vouts[k] = vout;
2010 vout->vid_dev = vid_dev;
2011 /* Select video2 if only 1 overlay is controlled by V4L2 */
2012 if (pdev->num_resources == 1)
2013 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
2014 else
2015 /* Else select video1 and video2 one by one. */
2016 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
2017 vout->vid_info.num_overlays = 1;
2018 vout->vid_info.id = k + 1;
2019
445e258f
AT
2020 /* Set VRFB as rotation_type for omap2 and omap3 */
2021 if (cpu_is_omap24xx() || cpu_is_omap34xx())
2022 vout->vid_info.rotation_type = VOUT_ROT_VRFB;
2023
5c7ab634
VH
2024 /* Setup the default configuration for the video devices
2025 */
2026 if (omap_vout_setup_video_data(vout) != 0) {
2027 ret = -ENOMEM;
2028 goto error;
2029 }
2030
2031 /* Allocate default number of buffers for the video streaming
2032 * and reserve the VRFB space for rotation
2033 */
2034 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
2035 ret = -ENOMEM;
2036 goto error1;
2037 }
2038
2039 /* Register the Video device with V4L2
2040 */
2041 vfd = vout->vfd;
8f3a307b 2042 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
5c7ab634
VH
2043 dev_err(&pdev->dev, ": Could not register "
2044 "Video for Linux device\n");
2045 vfd->minor = -1;
2046 ret = -ENODEV;
2047 goto error2;
2048 }
2049 video_set_drvdata(vfd, vout);
2050
2051 /* Configure the overlay structure */
2052 ret = omapvid_init(vid_dev->vouts[k], 0);
2053 if (!ret)
2054 goto success;
2055
2056error2:
445e258f
AT
2057 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2058 omap_vout_release_vrfb(vout);
5c7ab634
VH
2059 omap_vout_free_buffers(vout);
2060error1:
2061 video_device_release(vfd);
2062error:
2063 kfree(vout);
2064 return ret;
2065
2066success:
2067 dev_info(&pdev->dev, ": registered and initialized"
2068 " video device %d\n", vfd->minor);
2069 if (k == (pdev->num_resources - 1))
2070 return 0;
2071 }
2072
2073 return -ENODEV;
2074}
2075/* Driver functions */
2076static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2077{
2078 struct video_device *vfd;
445e258f 2079 struct omapvideo_info *ovid;
5c7ab634
VH
2080
2081 if (!vout)
2082 return;
2083
2084 vfd = vout->vfd;
445e258f 2085 ovid = &vout->vid_info;
5c7ab634
VH
2086 if (vfd) {
2087 if (!video_is_registered(vfd)) {
2088 /*
2089 * The device was never registered, so release the
2090 * video_device struct directly.
2091 */
2092 video_device_release(vfd);
2093 } else {
2094 /*
2095 * The unregister function will release the video_device
2096 * struct as well as unregistering it.
2097 */
2098 video_unregister_device(vfd);
2099 }
2100 }
445e258f
AT
2101 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2102 omap_vout_release_vrfb(vout);
2103 /* Free the VRFB buffer if allocated
2104 * init time
2105 */
2106 if (vout->vrfb_static_allocation)
2107 omap_vout_free_vrfb_buffers(vout);
2108 }
5c7ab634 2109 omap_vout_free_buffers(vout);
5c7ab634
VH
2110
2111 kfree(vout);
2112}
2113
2114static int omap_vout_remove(struct platform_device *pdev)
2115{
2116 int k;
2117 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2118 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2119 omap2video_device, v4l2_dev);
2120
2121 v4l2_device_unregister(v4l2_dev);
2122 for (k = 0; k < pdev->num_resources; k++)
2123 omap_vout_cleanup_device(vid_dev->vouts[k]);
2124
2125 for (k = 0; k < vid_dev->num_displays; k++) {
2126 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
5ba9bb0e 2127 vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
5c7ab634
VH
2128
2129 omap_dss_put_device(vid_dev->displays[k]);
2130 }
2131 kfree(vid_dev);
2132 return 0;
2133}
2134
2135static int __init omap_vout_probe(struct platform_device *pdev)
2136{
2137 int ret = 0, i;
2138 struct omap_overlay *ovl;
2139 struct omap_dss_device *dssdev = NULL;
2140 struct omap_dss_device *def_display;
2141 struct omap2video_device *vid_dev = NULL;
2142
2143 if (pdev->num_resources == 0) {
2144 dev_err(&pdev->dev, "probed for an unknown device\n");
2145 return -ENODEV;
2146 }
2147
2148 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
2149 if (vid_dev == NULL)
2150 return -ENOMEM;
2151
2152 vid_dev->num_displays = 0;
2153 for_each_dss_dev(dssdev) {
2154 omap_dss_get_device(dssdev);
2155 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2156 }
2157
2158 if (vid_dev->num_displays == 0) {
2159 dev_err(&pdev->dev, "no displays\n");
2160 ret = -EINVAL;
2161 goto probe_err0;
2162 }
2163
2164 vid_dev->num_overlays = omap_dss_get_num_overlays();
2165 for (i = 0; i < vid_dev->num_overlays; i++)
2166 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2167
2168 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2169 for (i = 0; i < vid_dev->num_managers; i++)
2170 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2171
2172 /* Get the Video1 overlay and video2 overlay.
2173 * Setup the Display attached to that overlays
2174 */
2175 for (i = 1; i < vid_dev->num_overlays; i++) {
2176 ovl = omap_dss_get_overlay(i);
2177 if (ovl->manager && ovl->manager->device) {
2178 def_display = ovl->manager->device;
2179 } else {
2180 dev_warn(&pdev->dev, "cannot find display\n");
2181 def_display = NULL;
2182 }
2183 if (def_display) {
5ba9bb0e
VH
2184 struct omap_dss_driver *dssdrv = def_display->driver;
2185
2186 ret = dssdrv->enable(def_display);
5c7ab634
VH
2187 if (ret) {
2188 /* Here we are not considering a error
2189 * as display may be enabled by frame
2190 * buffer driver
2191 */
2192 dev_warn(&pdev->dev,
2193 "'%s' Display already enabled\n",
2194 def_display->name);
2195 }
2196 /* set the update mode */
2197 if (def_display->caps &
2198 OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
5ba9bb0e
VH
2199 if (dssdrv->enable_te)
2200 dssdrv->enable_te(def_display, 0);
2201 if (dssdrv->set_update_mode)
2202 dssdrv->set_update_mode(def_display,
5c7ab634 2203 OMAP_DSS_UPDATE_MANUAL);
5c7ab634 2204 } else {
5ba9bb0e
VH
2205 if (dssdrv->set_update_mode)
2206 dssdrv->set_update_mode(def_display,
5c7ab634
VH
2207 OMAP_DSS_UPDATE_AUTO);
2208 }
2209 }
2210 }
2211
2212 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2213 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2214 ret = -ENODEV;
2215 goto probe_err1;
2216 }
2217
2218 ret = omap_vout_create_video_devices(pdev);
2219 if (ret)
2220 goto probe_err2;
2221
2222 for (i = 0; i < vid_dev->num_displays; i++) {
2223 struct omap_dss_device *display = vid_dev->displays[i];
2224
5ba9bb0e
VH
2225 if (display->driver->update)
2226 display->driver->update(display, 0, 0,
5c7ab634
VH
2227 display->panel.timings.x_res,
2228 display->panel.timings.y_res);
2229 }
2230 return 0;
2231
2232probe_err2:
2233 v4l2_device_unregister(&vid_dev->v4l2_dev);
2234probe_err1:
2235 for (i = 1; i < vid_dev->num_overlays; i++) {
2236 def_display = NULL;
2237 ovl = omap_dss_get_overlay(i);
2238 if (ovl->manager && ovl->manager->device)
2239 def_display = ovl->manager->device;
2240
5ba9bb0e
VH
2241 if (def_display && def_display->driver)
2242 def_display->driver->disable(def_display);
5c7ab634
VH
2243 }
2244probe_err0:
2245 kfree(vid_dev);
2246 return ret;
2247}
2248
2249static struct platform_driver omap_vout_driver = {
2250 .driver = {
2251 .name = VOUT_NAME,
2252 },
2253 .probe = omap_vout_probe,
2254 .remove = omap_vout_remove,
2255};
2256
2257static int __init omap_vout_init(void)
2258{
2259 if (platform_driver_register(&omap_vout_driver) != 0) {
2260 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2261 return -EINVAL;
2262 }
2263 return 0;
2264}
2265
2266static void omap_vout_cleanup(void)
2267{
2268 platform_driver_unregister(&omap_vout_driver);
2269}
2270
2271late_initcall(omap_vout_init);
2272module_exit(omap_vout_cleanup);