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