2 * Copyright (C) 2008-2009 Texas Instruments Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * Driver name : VPFE Capture driver
15 * VPFE Capture driver allows applications to capture and stream video
16 * frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
17 * TVP5146 or Raw Bayer RGB image data from an image sensor
18 * such as Microns' MT9T001, MT9T031 etc.
20 * These SoCs have, in common, a Video Processing Subsystem (VPSS) that
21 * consists of a Video Processing Front End (VPFE) for capturing
22 * video/raw image data and Video Processing Back End (VPBE) for displaying
23 * YUV data through an in-built analog encoder or Digital LCD port. This
24 * driver is for capture through VPFE. A typical EVM using these SoCs have
25 * following high level configuration.
28 * decoder(TVP5146/ YUV/
29 * MT9T001) --> Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
38 * The data flow happens from a decoder connected to the VPFE over a
39 * YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
40 * and to the input of VPFE through an optional MUX (if more inputs are
41 * to be interfaced on the EVM). The input data is first passed through
42 * CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
43 * does very little or no processing on YUV data and does pre-process Raw
44 * Bayer RGB data through modules such as Defect Pixel Correction (DFC)
45 * Color Space Conversion (CSC), data gain/offset etc. After this, data
46 * can be written to SDRAM or can be connected to the image processing
47 * block such as IPIPE (on DM355 only).
51 * - Capture using TVP5146 over BT.656
52 * - support for interfacing decoders using sub device model
53 * - Work with DM355 or DM6446 CCDC to do Raw Bayer RGB/YUV
54 * data capture to SDRAM.
56 * - Support multiple REQBUF after open
57 * - Support for de-allocating buffers through REQBUF
58 * - Support for Raw Bayer RGB capture
59 * - Support for chaining Image Processor
60 * - Support for static allocation of buffers
61 * - Support for USERPTR IO
62 * - Support for STREAMON before QBUF
63 * - Support for control ioctls
65 #include <linux/module.h>
66 #include <linux/slab.h>
67 #include <linux/init.h>
68 #include <linux/platform_device.h>
69 #include <linux/interrupt.h>
70 #include <media/v4l2-common.h>
72 #include <media/davinci/vpfe_capture.h>
73 #include "ccdc_hw_device.h"
76 static u32 numbuffers = 3;
77 static u32 bufsize = (720 * 576 * 2);
79 module_param(numbuffers, uint, S_IRUGO);
80 module_param(bufsize, uint, S_IRUGO);
81 module_param(debug, int, 0644);
83 MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
84 MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
85 MODULE_PARM_DESC(debug, "Debug level 0-1");
87 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_AUTHOR("Texas Instruments");
91 /* standard information */
92 struct vpfe_standard {
96 struct v4l2_fract pixelaspect;
97 /* 0 - progressive, 1 - interlaced */
101 /* ccdc configuration */
103 /* This make sure vpfe is probed and ready to go */
105 /* name of ccdc device */
109 /* data structures */
110 static struct vpfe_config_params config_params = {
113 .min_bufsize = 720 * 480 * 2,
114 .device_bufsize = 720 * 576 * 2,
117 /* ccdc device registered */
118 static struct ccdc_hw_device *ccdc_dev;
119 /* lock for accessing ccdc information */
120 static DEFINE_MUTEX(ccdc_lock);
121 /* ccdc configuration */
122 static struct ccdc_config *ccdc_cfg;
124 static const struct vpfe_standard vpfe_standards[] = {
125 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
126 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
129 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
130 static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
134 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
135 .description = "Bayer GrRBGb 8bit A-Law compr.",
136 .pixelformat = V4L2_PIX_FMT_SBGGR8,
143 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
144 .description = "Bayer GrRBGb - 16bit",
145 .pixelformat = V4L2_PIX_FMT_SBGGR16,
152 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
153 .description = "Bayer GrRBGb 8bit DPCM compr.",
154 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
161 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
162 .description = "YCbCr 4:2:2 Interleaved UYVY",
163 .pixelformat = V4L2_PIX_FMT_UYVY,
170 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
171 .description = "YCbCr 4:2:2 Interleaved YUYV",
172 .pixelformat = V4L2_PIX_FMT_YUYV,
179 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
180 .description = "Y/CbCr 4:2:0 - Semi planar",
181 .pixelformat = V4L2_PIX_FMT_NV12,
188 * vpfe_lookup_pix_format()
189 * lookup an entry in the vpfe pix format table based on pix_format
191 static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
195 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
196 if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
197 return &vpfe_pix_fmts[i];
203 * vpfe_register_ccdc_device. CCDC module calls this to
204 * register with vpfe capture
206 int vpfe_register_ccdc_device(struct ccdc_hw_device *dev)
209 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
211 BUG_ON(!dev->hw_ops.open);
212 BUG_ON(!dev->hw_ops.enable);
213 BUG_ON(!dev->hw_ops.set_hw_if_params);
214 BUG_ON(!dev->hw_ops.configure);
215 BUG_ON(!dev->hw_ops.set_buftype);
216 BUG_ON(!dev->hw_ops.get_buftype);
217 BUG_ON(!dev->hw_ops.enum_pix);
218 BUG_ON(!dev->hw_ops.set_frame_format);
219 BUG_ON(!dev->hw_ops.get_frame_format);
220 BUG_ON(!dev->hw_ops.get_pixel_format);
221 BUG_ON(!dev->hw_ops.set_pixel_format);
222 BUG_ON(!dev->hw_ops.set_image_window);
223 BUG_ON(!dev->hw_ops.get_image_window);
224 BUG_ON(!dev->hw_ops.get_line_length);
225 BUG_ON(!dev->hw_ops.getfid);
227 mutex_lock(&ccdc_lock);
230 * TODO. Will this ever happen? if so, we need to fix it.
231 * Proabably we need to add the request to a linked list and
232 * walk through it during vpfe probe
234 printk(KERN_ERR "vpfe capture not initialized\n");
239 if (strcmp(dev->name, ccdc_cfg->name)) {
240 /* ignore this ccdc */
246 printk(KERN_ERR "ccdc already registered\n");
253 mutex_unlock(&ccdc_lock);
256 EXPORT_SYMBOL(vpfe_register_ccdc_device);
259 * vpfe_unregister_ccdc_device. CCDC module calls this to
260 * unregister with vpfe capture
262 void vpfe_unregister_ccdc_device(struct ccdc_hw_device *dev)
265 printk(KERN_ERR "invalid ccdc device ptr\n");
269 printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
272 if (strcmp(dev->name, ccdc_cfg->name)) {
273 /* ignore this ccdc */
277 mutex_lock(&ccdc_lock);
279 mutex_unlock(&ccdc_lock);
281 EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
284 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
286 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe_dev,
287 struct v4l2_format *f)
289 struct v4l2_rect image_win;
290 enum ccdc_buftype buf_type;
291 enum ccdc_frmfmt frm_fmt;
293 memset(f, 0, sizeof(*f));
294 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
295 ccdc_dev->hw_ops.get_image_window(&image_win);
296 f->fmt.pix.width = image_win.width;
297 f->fmt.pix.height = image_win.height;
298 f->fmt.pix.bytesperline = ccdc_dev->hw_ops.get_line_length();
299 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
301 buf_type = ccdc_dev->hw_ops.get_buftype();
302 f->fmt.pix.pixelformat = ccdc_dev->hw_ops.get_pixel_format();
303 frm_fmt = ccdc_dev->hw_ops.get_frame_format();
304 if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
305 f->fmt.pix.field = V4L2_FIELD_NONE;
306 else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
307 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
308 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
309 else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED)
310 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
312 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf_type\n");
316 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid frm_fmt\n");
323 * vpfe_config_ccdc_image_format()
324 * For a pix format, configure ccdc to setup the capture
326 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
328 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
331 if (ccdc_dev->hw_ops.set_pixel_format(
332 vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
333 v4l2_err(&vpfe_dev->v4l2_dev,
334 "couldn't set pix format in ccdc\n");
337 /* configure the image window */
338 ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
340 switch (vpfe_dev->fmt.fmt.pix.field) {
341 case V4L2_FIELD_INTERLACED:
342 /* do nothing, since it is default */
343 ret = ccdc_dev->hw_ops.set_buftype(
344 CCDC_BUFTYPE_FLD_INTERLEAVED);
346 case V4L2_FIELD_NONE:
347 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
348 /* buffer type only applicable for interlaced scan */
350 case V4L2_FIELD_SEQ_TB:
351 ret = ccdc_dev->hw_ops.set_buftype(
352 CCDC_BUFTYPE_FLD_SEPARATED);
358 /* set the frame format */
360 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
364 * vpfe_config_image_format()
365 * For a given standard, this functions sets up the default
366 * pix format & crop values in the vpfe device and ccdc. It first
367 * starts with defaults based values from the standard table.
368 * It then checks if sub device supports get_fmt and then override the
369 * values based on that.Sets crop values to match with scan resolution
370 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
373 static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
376 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
377 struct v4l2_subdev_format fmt = {
378 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
380 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
381 struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
384 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
385 if (vpfe_standards[i].std_id & std_id) {
386 vpfe_dev->std_info.active_pixels =
387 vpfe_standards[i].width;
388 vpfe_dev->std_info.active_lines =
389 vpfe_standards[i].height;
390 vpfe_dev->std_info.frame_format =
391 vpfe_standards[i].frame_format;
392 vpfe_dev->std_index = i;
397 if (i == ARRAY_SIZE(vpfe_standards)) {
398 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
402 vpfe_dev->crop.top = 0;
403 vpfe_dev->crop.left = 0;
404 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
405 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
406 pix->width = vpfe_dev->crop.width;
407 pix->height = vpfe_dev->crop.height;
409 /* first field and frame format based on standard frame format */
410 if (vpfe_dev->std_info.frame_format) {
411 pix->field = V4L2_FIELD_INTERLACED;
412 /* assume V4L2_PIX_FMT_UYVY as default */
413 pix->pixelformat = V4L2_PIX_FMT_UYVY;
414 v4l2_fill_mbus_format(mbus_fmt, pix,
415 MEDIA_BUS_FMT_YUYV10_2X10);
417 pix->field = V4L2_FIELD_NONE;
418 /* assume V4L2_PIX_FMT_SBGGR8 */
419 pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
420 v4l2_fill_mbus_format(mbus_fmt, pix,
421 MEDIA_BUS_FMT_SBGGR8_1X8);
424 /* if sub device supports get_fmt, override the defaults */
425 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
426 sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
428 if (ret && ret != -ENOIOCTLCMD) {
429 v4l2_err(&vpfe_dev->v4l2_dev,
430 "error in getting get_fmt from sub device\n");
433 v4l2_fill_pix_format(pix, mbus_fmt);
434 pix->bytesperline = pix->width * 2;
435 pix->sizeimage = pix->bytesperline * pix->height;
437 /* Sets the values in CCDC */
438 ret = vpfe_config_ccdc_image_format(vpfe_dev);
442 /* Update the values of sizeimage and bytesperline */
443 pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
444 pix->sizeimage = pix->bytesperline * pix->height;
449 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
453 /* set first input of current subdevice as the current input */
454 vpfe_dev->current_input = 0;
456 /* set default standard */
457 vpfe_dev->std_index = 0;
459 /* Configure the default format information */
460 ret = vpfe_config_image_format(vpfe_dev,
461 vpfe_standards[vpfe_dev->std_index].std_id);
465 /* now open the ccdc device to initialize it */
466 mutex_lock(&ccdc_lock);
468 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
473 if (!try_module_get(ccdc_dev->owner)) {
474 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
478 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
480 vpfe_dev->initialized = 1;
482 /* Clear all VPFE/CCDC interrupts */
483 if (vpfe_dev->cfg->clr_intr)
484 vpfe_dev->cfg->clr_intr(-1);
487 mutex_unlock(&ccdc_lock);
492 * vpfe_open : It creates object of file handle structure and
493 * stores it in private_data member of filepointer
495 static int vpfe_open(struct file *file)
497 struct vpfe_device *vpfe_dev = video_drvdata(file);
498 struct video_device *vdev = video_devdata(file);
501 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
503 if (!vpfe_dev->cfg->num_subdevs) {
504 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
508 /* Allocate memory for the file handle object */
509 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
513 /* store pointer to fh in private_data member of file */
514 file->private_data = fh;
515 fh->vpfe_dev = vpfe_dev;
516 v4l2_fh_init(&fh->fh, vdev);
517 mutex_lock(&vpfe_dev->lock);
518 /* If decoder is not initialized. initialize it */
519 if (!vpfe_dev->initialized) {
520 if (vpfe_initialize_device(vpfe_dev)) {
521 mutex_unlock(&vpfe_dev->lock);
522 v4l2_fh_exit(&fh->fh);
527 /* Increment device usrs counter */
529 /* Set io_allowed member to false */
531 v4l2_fh_add(&fh->fh);
532 mutex_unlock(&vpfe_dev->lock);
536 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
540 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
541 struct videobuf_buffer, queue);
542 list_del(&vpfe_dev->next_frm->queue);
543 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
544 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
546 ccdc_dev->hw_ops.setfbaddr(addr);
549 static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
553 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
554 addr += vpfe_dev->field_off;
555 ccdc_dev->hw_ops.setfbaddr(addr);
558 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
560 v4l2_get_timestamp(&vpfe_dev->cur_frm->ts);
561 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
562 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
563 wake_up_interruptible(&vpfe_dev->cur_frm->done);
564 vpfe_dev->cur_frm = vpfe_dev->next_frm;
568 static irqreturn_t vpfe_isr(int irq, void *dev_id)
570 struct vpfe_device *vpfe_dev = dev_id;
571 enum v4l2_field field;
574 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
575 field = vpfe_dev->fmt.fmt.pix.field;
577 /* if streaming not started, don't do anything */
578 if (!vpfe_dev->started)
581 /* only for 6446 this will be applicable */
582 if (ccdc_dev->hw_ops.reset)
583 ccdc_dev->hw_ops.reset();
585 if (field == V4L2_FIELD_NONE) {
586 /* handle progressive frame capture */
587 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
588 "frame format is progressive...\n");
589 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
590 vpfe_process_buffer_complete(vpfe_dev);
594 /* interlaced or TB capture check which field we are in hardware */
595 fid = ccdc_dev->hw_ops.getfid();
597 /* switch the software maintained field id */
598 vpfe_dev->field_id ^= 1;
599 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
600 fid, vpfe_dev->field_id);
601 if (fid == vpfe_dev->field_id) {
602 /* we are in-sync here,continue */
605 * One frame is just being captured. If the next frame
606 * is available, release the current frame and move on
608 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
609 vpfe_process_buffer_complete(vpfe_dev);
611 * based on whether the two fields are stored
612 * interleavely or separately in memory, reconfigure
613 * the CCDC memory address
615 if (field == V4L2_FIELD_SEQ_TB)
616 vpfe_schedule_bottom_field(vpfe_dev);
620 * if one field is just being captured configure
621 * the next frame get the next frame from the empty
622 * queue if no frame is available hold on to the
625 spin_lock(&vpfe_dev->dma_queue_lock);
626 if (!list_empty(&vpfe_dev->dma_queue) &&
627 vpfe_dev->cur_frm == vpfe_dev->next_frm)
628 vpfe_schedule_next_buffer(vpfe_dev);
629 spin_unlock(&vpfe_dev->dma_queue_lock);
630 } else if (fid == 0) {
632 * out of sync. Recover from any hardware out-of-sync.
633 * May loose one frame
635 vpfe_dev->field_id = fid;
638 if (vpfe_dev->cfg->clr_intr)
639 vpfe_dev->cfg->clr_intr(irq);
644 /* vdint1_isr - isr handler for VINT1 interrupt */
645 static irqreturn_t vdint1_isr(int irq, void *dev_id)
647 struct vpfe_device *vpfe_dev = dev_id;
649 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
651 /* if streaming not started, don't do anything */
652 if (!vpfe_dev->started) {
653 if (vpfe_dev->cfg->clr_intr)
654 vpfe_dev->cfg->clr_intr(irq);
658 spin_lock(&vpfe_dev->dma_queue_lock);
659 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
660 !list_empty(&vpfe_dev->dma_queue) &&
661 vpfe_dev->cur_frm == vpfe_dev->next_frm)
662 vpfe_schedule_next_buffer(vpfe_dev);
663 spin_unlock(&vpfe_dev->dma_queue_lock);
665 if (vpfe_dev->cfg->clr_intr)
666 vpfe_dev->cfg->clr_intr(irq);
671 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
673 enum ccdc_frmfmt frame_format;
675 frame_format = ccdc_dev->hw_ops.get_frame_format();
676 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
677 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
680 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
682 enum ccdc_frmfmt frame_format;
684 frame_format = ccdc_dev->hw_ops.get_frame_format();
685 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
686 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
693 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
694 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
696 vpfe_dev->started = 0;
697 ccdc_dev->hw_ops.enable(0);
698 if (ccdc_dev->hw_ops.enable_out_to_sdram)
699 ccdc_dev->hw_ops.enable_out_to_sdram(0);
703 * vpfe_release : This function deletes buffer queue, frees the
704 * buffers and the vpfe file handle
706 static int vpfe_release(struct file *file)
708 struct vpfe_device *vpfe_dev = video_drvdata(file);
709 struct vpfe_fh *fh = file->private_data;
710 struct vpfe_subdev_info *sdinfo;
713 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
715 /* Get the device lock */
716 mutex_lock(&vpfe_dev->lock);
717 /* if this instance is doing IO */
718 if (fh->io_allowed) {
719 if (vpfe_dev->started) {
720 sdinfo = vpfe_dev->current_subdev;
721 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
724 if (ret && (ret != -ENOIOCTLCMD))
725 v4l2_err(&vpfe_dev->v4l2_dev,
726 "stream off failed in subdev\n");
727 vpfe_stop_ccdc_capture(vpfe_dev);
728 vpfe_detach_irq(vpfe_dev);
729 videobuf_streamoff(&vpfe_dev->buffer_queue);
731 vpfe_dev->io_usrs = 0;
732 vpfe_dev->numbuffers = config_params.numbuffers;
733 videobuf_stop(&vpfe_dev->buffer_queue);
734 videobuf_mmap_free(&vpfe_dev->buffer_queue);
737 /* Decrement device usrs counter */
739 v4l2_fh_del(&fh->fh);
740 v4l2_fh_exit(&fh->fh);
741 /* If this is the last file handle */
742 if (!vpfe_dev->usrs) {
743 vpfe_dev->initialized = 0;
744 if (ccdc_dev->hw_ops.close)
745 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
746 module_put(ccdc_dev->owner);
748 mutex_unlock(&vpfe_dev->lock);
749 file->private_data = NULL;
750 /* Free memory allocated to file handle object */
756 * vpfe_mmap : It is used to map kernel space buffers
759 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
761 /* Get the device object and file handle object */
762 struct vpfe_device *vpfe_dev = video_drvdata(file);
764 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
766 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
770 * vpfe_poll: It is used for select/poll system call
772 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
774 struct vpfe_device *vpfe_dev = video_drvdata(file);
776 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
778 if (vpfe_dev->started)
779 return videobuf_poll_stream(file,
780 &vpfe_dev->buffer_queue, wait);
784 /* vpfe capture driver file operations */
785 static const struct v4l2_file_operations vpfe_fops = {
786 .owner = THIS_MODULE,
788 .release = vpfe_release,
789 .unlocked_ioctl = video_ioctl2,
795 * vpfe_check_format()
796 * This function adjust the input pixel format as per hardware
797 * capabilities and update the same in pixfmt.
798 * Following algorithm used :-
800 * If given pixformat is not in the vpfe list of pix formats or not
801 * supported by the hardware, current value of pixformat in the device
803 * If given field is not supported, then current field is used. If field
804 * is different from current, then it is matched with that from sub device.
805 * Minimum height is 2 lines for interlaced or tb field and 1 line for
806 * progressive. Maximum height is clamped to active active lines of scan
807 * Minimum width is 32 bytes in memory and width is clamped to active
809 * bytesperline is a multiple of 32.
811 static const struct vpfe_pixel_format *
812 vpfe_check_format(struct vpfe_device *vpfe_dev,
813 struct v4l2_pix_format *pixfmt)
815 u32 min_height = 1, min_width = 32, max_width, max_height;
816 const struct vpfe_pixel_format *vpfe_pix_fmt;
820 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
823 * use current pixel format in the vpfe device. We
824 * will find this pix format in the table
826 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
827 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
830 /* check if hw supports it */
833 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
834 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
842 /* use current pixel format */
843 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
845 * Since this is currently used in the vpfe device, we
846 * will find this pix format in the table
848 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
851 /* check what field format is supported */
852 if (pixfmt->field == V4L2_FIELD_ANY) {
853 /* if field is any, use current value as default */
854 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
858 * if field is not same as current field in the vpfe device
859 * try matching the field with the sub device field
861 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
863 * If field value is not in the supported fields, use current
864 * field used in the device as default
866 switch (pixfmt->field) {
867 case V4L2_FIELD_INTERLACED:
868 case V4L2_FIELD_SEQ_TB:
869 /* if sub device is supporting progressive, use that */
870 if (!vpfe_dev->std_info.frame_format)
871 pixfmt->field = V4L2_FIELD_NONE;
873 case V4L2_FIELD_NONE:
874 if (vpfe_dev->std_info.frame_format)
875 pixfmt->field = V4L2_FIELD_INTERLACED;
879 /* use current field as default */
880 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
885 /* Now adjust image resolutions supported */
886 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
887 pixfmt->field == V4L2_FIELD_SEQ_TB)
890 max_width = vpfe_dev->std_info.active_pixels;
891 max_height = vpfe_dev->std_info.active_lines;
892 min_width /= vpfe_pix_fmt->bpp;
894 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
895 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
897 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
898 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
900 /* If interlaced, adjust height to be a multiple of 2 */
901 if (pixfmt->field == V4L2_FIELD_INTERLACED)
902 pixfmt->height &= (~1);
904 * recalculate bytesperline and sizeimage since width
905 * and height might have changed
907 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
909 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
911 pixfmt->bytesperline * pixfmt->height +
912 ((pixfmt->bytesperline * pixfmt->height) >> 1);
914 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
916 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
917 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
918 pixfmt->bytesperline, pixfmt->sizeimage);
922 static int vpfe_querycap(struct file *file, void *priv,
923 struct v4l2_capability *cap)
925 struct vpfe_device *vpfe_dev = video_drvdata(file);
927 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
929 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
930 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
931 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
932 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
933 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
937 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
938 struct v4l2_format *fmt)
940 struct vpfe_device *vpfe_dev = video_drvdata(file);
942 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
943 /* Fill in the information about format */
944 *fmt = vpfe_dev->fmt;
948 static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
949 struct v4l2_fmtdesc *fmt)
951 struct vpfe_device *vpfe_dev = video_drvdata(file);
952 const struct vpfe_pixel_format *pix_fmt;
956 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
958 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
961 /* Fill in the information about format */
962 pix_fmt = vpfe_lookup_pix_format(pix);
964 temp_index = fmt->index;
965 *fmt = pix_fmt->fmtdesc;
966 fmt->index = temp_index;
972 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
973 struct v4l2_format *fmt)
975 struct vpfe_device *vpfe_dev = video_drvdata(file);
976 const struct vpfe_pixel_format *pix_fmts;
979 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
981 /* If streaming is started, return error */
982 if (vpfe_dev->started) {
983 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
987 /* Check for valid frame format */
988 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
992 /* store the pixel format in the device object */
993 ret = mutex_lock_interruptible(&vpfe_dev->lock);
997 /* First detach any IRQ if currently attached */
998 vpfe_detach_irq(vpfe_dev);
999 vpfe_dev->fmt = *fmt;
1000 /* set image capture parameters in the ccdc */
1001 ret = vpfe_config_ccdc_image_format(vpfe_dev);
1002 mutex_unlock(&vpfe_dev->lock);
1006 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
1007 struct v4l2_format *f)
1009 struct vpfe_device *vpfe_dev = video_drvdata(file);
1010 const struct vpfe_pixel_format *pix_fmts;
1012 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
1014 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
1021 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1022 * given app input index
1024 static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1026 int *subdev_input_index,
1027 int app_input_index)
1029 struct vpfe_config *cfg = vpfe_dev->cfg;
1030 struct vpfe_subdev_info *sdinfo;
1033 for (i = 0; i < cfg->num_subdevs; i++) {
1034 sdinfo = &cfg->sub_devs[i];
1035 if (app_input_index < (j + sdinfo->num_inputs)) {
1037 *subdev_input_index = app_input_index - j;
1040 j += sdinfo->num_inputs;
1046 * vpfe_get_app_input - Get app input index for a given subdev input index
1047 * driver stores the input index of the current sub device and translate it
1048 * when application request the current input
1050 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1051 int *app_input_index)
1053 struct vpfe_config *cfg = vpfe_dev->cfg;
1054 struct vpfe_subdev_info *sdinfo;
1057 for (i = 0; i < cfg->num_subdevs; i++) {
1058 sdinfo = &cfg->sub_devs[i];
1059 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1060 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1062 *app_input_index = j + vpfe_dev->current_input;
1065 j += sdinfo->num_inputs;
1070 static int vpfe_enum_input(struct file *file, void *priv,
1071 struct v4l2_input *inp)
1073 struct vpfe_device *vpfe_dev = video_drvdata(file);
1074 struct vpfe_subdev_info *sdinfo;
1077 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1079 if (vpfe_get_subdev_input_index(vpfe_dev,
1083 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
1086 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1087 *inp = sdinfo->inputs[index];
1091 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1093 struct vpfe_device *vpfe_dev = video_drvdata(file);
1095 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1097 return vpfe_get_app_input_index(vpfe_dev, index);
1101 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1103 struct vpfe_device *vpfe_dev = video_drvdata(file);
1104 struct v4l2_subdev *sd;
1105 struct vpfe_subdev_info *sdinfo;
1106 int subdev_index, inp_index;
1107 struct vpfe_route *route;
1111 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1113 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1118 * If streaming is started return device busy
1121 if (vpfe_dev->started) {
1122 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1126 ret = vpfe_get_subdev_input_index(vpfe_dev,
1131 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1135 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1136 sd = vpfe_dev->sd[subdev_index];
1137 route = &sdinfo->routes[inp_index];
1138 if (route && sdinfo->can_route) {
1139 input = route->input;
1140 output = route->output;
1147 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1150 v4l2_err(&vpfe_dev->v4l2_dev,
1151 "vpfe_doioctl:error in setting input in decoder\n");
1155 vpfe_dev->current_subdev = sdinfo;
1157 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
1158 vpfe_dev->current_input = index;
1159 vpfe_dev->std_index = 0;
1161 /* set the bus/interface parameter for the sub device in ccdc */
1162 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1166 /* set the default image parameters in the device */
1167 ret = vpfe_config_image_format(vpfe_dev,
1168 vpfe_standards[vpfe_dev->std_index].std_id);
1170 mutex_unlock(&vpfe_dev->lock);
1174 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1176 struct vpfe_device *vpfe_dev = video_drvdata(file);
1177 struct vpfe_subdev_info *sdinfo;
1180 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1182 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1183 sdinfo = vpfe_dev->current_subdev;
1186 /* Call querystd function of decoder device */
1187 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1188 video, querystd, std_id);
1189 mutex_unlock(&vpfe_dev->lock);
1193 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1195 struct vpfe_device *vpfe_dev = video_drvdata(file);
1196 struct vpfe_subdev_info *sdinfo;
1199 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1201 /* Call decoder driver function to set the standard */
1202 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1206 sdinfo = vpfe_dev->current_subdev;
1207 /* If streaming is started, return device busy error */
1208 if (vpfe_dev->started) {
1209 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1214 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1215 video, s_std, std_id);
1217 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1220 ret = vpfe_config_image_format(vpfe_dev, std_id);
1223 mutex_unlock(&vpfe_dev->lock);
1227 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1229 struct vpfe_device *vpfe_dev = video_drvdata(file);
1231 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1233 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1237 * Videobuf operations
1239 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1240 unsigned int *count,
1243 struct vpfe_fh *fh = vq->priv_data;
1244 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1246 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1247 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1248 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1249 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1250 *size = config_params.device_bufsize;
1252 if (*count < config_params.min_numbuffers)
1253 *count = config_params.min_numbuffers;
1254 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1255 "count=%d, size=%d\n", *count, *size);
1259 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1260 struct videobuf_buffer *vb,
1261 enum v4l2_field field)
1263 struct vpfe_fh *fh = vq->priv_data;
1264 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1268 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1270 /* If buffer is not initialized, initialize it */
1271 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1272 vb->width = vpfe_dev->fmt.fmt.pix.width;
1273 vb->height = vpfe_dev->fmt.fmt.pix.height;
1274 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1277 ret = videobuf_iolock(vq, vb, NULL);
1281 addr = videobuf_to_dma_contig(vb);
1282 /* Make sure user addresses are aligned to 32 bytes */
1283 if (!ALIGN(addr, 32))
1286 vb->state = VIDEOBUF_PREPARED;
1291 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1292 struct videobuf_buffer *vb)
1294 /* Get the file handle object and device object */
1295 struct vpfe_fh *fh = vq->priv_data;
1296 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1297 unsigned long flags;
1299 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1301 /* add the buffer to the DMA queue */
1302 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1303 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1304 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1306 /* Change state of the buffer */
1307 vb->state = VIDEOBUF_QUEUED;
1310 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1311 struct videobuf_buffer *vb)
1313 struct vpfe_fh *fh = vq->priv_data;
1314 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1315 unsigned long flags;
1317 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1320 * We need to flush the buffer from the dma queue since
1321 * they are de-allocated
1323 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1324 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1325 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1326 videobuf_dma_contig_free(vq, vb);
1327 vb->state = VIDEOBUF_NEEDS_INIT;
1330 static struct videobuf_queue_ops vpfe_videobuf_qops = {
1331 .buf_setup = vpfe_videobuf_setup,
1332 .buf_prepare = vpfe_videobuf_prepare,
1333 .buf_queue = vpfe_videobuf_queue,
1334 .buf_release = vpfe_videobuf_release,
1338 * vpfe_reqbufs. currently support REQBUF only once opening
1341 static int vpfe_reqbufs(struct file *file, void *priv,
1342 struct v4l2_requestbuffers *req_buf)
1344 struct vpfe_device *vpfe_dev = video_drvdata(file);
1345 struct vpfe_fh *fh = file->private_data;
1348 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1350 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1351 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1355 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1359 if (vpfe_dev->io_usrs != 0) {
1360 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1365 vpfe_dev->memory = req_buf->memory;
1366 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1367 &vpfe_videobuf_qops,
1371 vpfe_dev->fmt.fmt.pix.field,
1372 sizeof(struct videobuf_buffer),
1376 vpfe_dev->io_usrs = 1;
1377 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1378 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1380 mutex_unlock(&vpfe_dev->lock);
1384 static int vpfe_querybuf(struct file *file, void *priv,
1385 struct v4l2_buffer *buf)
1387 struct vpfe_device *vpfe_dev = video_drvdata(file);
1389 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1391 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1392 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1396 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1397 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1400 /* Call videobuf_querybuf to get information */
1401 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1404 static int vpfe_qbuf(struct file *file, void *priv,
1405 struct v4l2_buffer *p)
1407 struct vpfe_device *vpfe_dev = video_drvdata(file);
1408 struct vpfe_fh *fh = file->private_data;
1410 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1412 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1413 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1418 * If this file handle is not allowed to do IO,
1421 if (!fh->io_allowed) {
1422 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1425 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1428 static int vpfe_dqbuf(struct file *file, void *priv,
1429 struct v4l2_buffer *buf)
1431 struct vpfe_device *vpfe_dev = video_drvdata(file);
1433 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1435 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1436 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1439 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1440 buf, file->f_flags & O_NONBLOCK);
1444 * vpfe_calculate_offsets : This function calculates buffers offset
1445 * for top and bottom field
1447 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1449 struct v4l2_rect image_win;
1451 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1453 ccdc_dev->hw_ops.get_image_window(&image_win);
1454 vpfe_dev->field_off = image_win.height * image_win.width;
1457 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1458 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1460 ccdc_dev->hw_ops.enable(1);
1461 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1462 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1463 vpfe_dev->started = 1;
1467 * vpfe_streamon. Assume the DMA queue is not empty.
1468 * application is expected to call QBUF before calling
1469 * this ioctl. If not, driver returns error
1471 static int vpfe_streamon(struct file *file, void *priv,
1472 enum v4l2_buf_type buf_type)
1474 struct vpfe_device *vpfe_dev = video_drvdata(file);
1475 struct vpfe_fh *fh = file->private_data;
1476 struct vpfe_subdev_info *sdinfo;
1480 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1482 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1483 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1487 /* If file handle is not allowed IO, return error */
1488 if (!fh->io_allowed) {
1489 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1493 sdinfo = vpfe_dev->current_subdev;
1494 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1495 video, s_stream, 1);
1497 if (ret && (ret != -ENOIOCTLCMD)) {
1498 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1502 /* If buffer queue is empty, return error */
1503 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1504 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1508 /* Call videobuf_streamon to start streaming * in videobuf */
1509 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1514 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1517 /* Get the next frame from the buffer queue */
1518 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1519 struct videobuf_buffer, queue);
1520 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1521 /* Remove buffer from the buffer queue */
1522 list_del(&vpfe_dev->cur_frm->queue);
1523 /* Mark state of the current frame to active */
1524 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1525 /* Initialize field_id and started member */
1526 vpfe_dev->field_id = 0;
1527 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1529 /* Calculate field offset */
1530 vpfe_calculate_offsets(vpfe_dev);
1532 if (vpfe_attach_irq(vpfe_dev) < 0) {
1533 v4l2_err(&vpfe_dev->v4l2_dev,
1534 "Error in attaching interrupt handle\n");
1538 if (ccdc_dev->hw_ops.configure() < 0) {
1539 v4l2_err(&vpfe_dev->v4l2_dev,
1540 "Error in configuring ccdc\n");
1544 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1545 vpfe_start_ccdc_capture(vpfe_dev);
1546 mutex_unlock(&vpfe_dev->lock);
1549 mutex_unlock(&vpfe_dev->lock);
1551 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1555 static int vpfe_streamoff(struct file *file, void *priv,
1556 enum v4l2_buf_type buf_type)
1558 struct vpfe_device *vpfe_dev = video_drvdata(file);
1559 struct vpfe_fh *fh = file->private_data;
1560 struct vpfe_subdev_info *sdinfo;
1563 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1565 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1566 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1570 /* If io is allowed for this file handle, return error */
1571 if (!fh->io_allowed) {
1572 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1576 /* If streaming is not started, return error */
1577 if (!vpfe_dev->started) {
1578 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1582 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1586 vpfe_stop_ccdc_capture(vpfe_dev);
1587 vpfe_detach_irq(vpfe_dev);
1589 sdinfo = vpfe_dev->current_subdev;
1590 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1591 video, s_stream, 0);
1593 if (ret && (ret != -ENOIOCTLCMD))
1594 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1595 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1596 mutex_unlock(&vpfe_dev->lock);
1600 static int vpfe_cropcap(struct file *file, void *priv,
1601 struct v4l2_cropcap *crop)
1603 struct vpfe_device *vpfe_dev = video_drvdata(file);
1605 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1607 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1609 /* If std_index is invalid, then just return (== 1:1 aspect) */
1610 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1613 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1617 static int vpfe_g_selection(struct file *file, void *priv,
1618 struct v4l2_selection *sel)
1620 struct vpfe_device *vpfe_dev = video_drvdata(file);
1622 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
1624 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1627 switch (sel->target) {
1628 case V4L2_SEL_TGT_CROP:
1629 sel->r = vpfe_dev->crop;
1631 case V4L2_SEL_TGT_CROP_DEFAULT:
1632 case V4L2_SEL_TGT_CROP_BOUNDS:
1633 sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1634 sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1642 static int vpfe_s_selection(struct file *file, void *priv,
1643 struct v4l2_selection *sel)
1645 struct vpfe_device *vpfe_dev = video_drvdata(file);
1646 struct v4l2_rect rect = sel->r;
1649 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1651 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1652 sel->target != V4L2_SEL_TGT_CROP)
1655 if (vpfe_dev->started) {
1656 /* make sure streaming is not started */
1657 v4l2_err(&vpfe_dev->v4l2_dev,
1658 "Cannot change crop when streaming is ON\n");
1662 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1666 if (rect.top < 0 || rect.left < 0) {
1667 v4l2_err(&vpfe_dev->v4l2_dev,
1668 "doesn't support negative values for top & left\n");
1673 /* adjust the width to 16 pixel boundary */
1674 rect.width = ((rect.width + 15) & ~0xf);
1676 /* make sure parameters are valid */
1677 if ((rect.left + rect.width >
1678 vpfe_dev->std_info.active_pixels) ||
1679 (rect.top + rect.height >
1680 vpfe_dev->std_info.active_lines)) {
1681 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
1685 ccdc_dev->hw_ops.set_image_window(&rect);
1686 vpfe_dev->fmt.fmt.pix.width = rect.width;
1687 vpfe_dev->fmt.fmt.pix.height = rect.height;
1688 vpfe_dev->fmt.fmt.pix.bytesperline =
1689 ccdc_dev->hw_ops.get_line_length();
1690 vpfe_dev->fmt.fmt.pix.sizeimage =
1691 vpfe_dev->fmt.fmt.pix.bytesperline *
1692 vpfe_dev->fmt.fmt.pix.height;
1693 vpfe_dev->crop = rect;
1696 mutex_unlock(&vpfe_dev->lock);
1701 static long vpfe_param_handler(struct file *file, void *priv,
1702 bool valid_prio, unsigned int cmd, void *param)
1704 struct vpfe_device *vpfe_dev = video_drvdata(file);
1707 v4l2_dbg(2, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
1709 if (vpfe_dev->started) {
1710 /* only allowed if streaming is not started */
1711 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1712 "device already started\n");
1716 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1721 case VPFE_CMD_S_CCDC_RAW_PARAMS:
1722 v4l2_warn(&vpfe_dev->v4l2_dev,
1723 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
1724 if (ccdc_dev->hw_ops.set_params) {
1725 ret = ccdc_dev->hw_ops.set_params(param);
1727 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1728 "Error setting parameters in CCDC\n");
1731 ret = vpfe_get_ccdc_image_format(vpfe_dev,
1734 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1735 "Invalid image format at CCDC\n");
1740 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1741 "VPFE_CMD_S_CCDC_RAW_PARAMS not supported\n");
1748 mutex_unlock(&vpfe_dev->lock);
1753 /* vpfe capture ioctl operations */
1754 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1755 .vidioc_querycap = vpfe_querycap,
1756 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1757 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1758 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1759 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1760 .vidioc_enum_input = vpfe_enum_input,
1761 .vidioc_g_input = vpfe_g_input,
1762 .vidioc_s_input = vpfe_s_input,
1763 .vidioc_querystd = vpfe_querystd,
1764 .vidioc_s_std = vpfe_s_std,
1765 .vidioc_g_std = vpfe_g_std,
1766 .vidioc_reqbufs = vpfe_reqbufs,
1767 .vidioc_querybuf = vpfe_querybuf,
1768 .vidioc_qbuf = vpfe_qbuf,
1769 .vidioc_dqbuf = vpfe_dqbuf,
1770 .vidioc_streamon = vpfe_streamon,
1771 .vidioc_streamoff = vpfe_streamoff,
1772 .vidioc_cropcap = vpfe_cropcap,
1773 .vidioc_g_selection = vpfe_g_selection,
1774 .vidioc_s_selection = vpfe_s_selection,
1775 .vidioc_default = vpfe_param_handler,
1778 static struct vpfe_device *vpfe_initialize(void)
1780 struct vpfe_device *vpfe_dev;
1782 /* Default number of buffers should be 3 */
1783 if ((numbuffers > 0) &&
1784 (numbuffers < config_params.min_numbuffers))
1785 numbuffers = config_params.min_numbuffers;
1788 * Set buffer size to min buffers size if invalid buffer size is
1791 if (bufsize < config_params.min_bufsize)
1792 bufsize = config_params.min_bufsize;
1794 config_params.numbuffers = numbuffers;
1797 config_params.device_bufsize = bufsize;
1799 /* Allocate memory for device objects */
1800 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1806 * vpfe_probe : This function creates device entries by register
1807 * itself to the V4L2 driver and initializes fields of each
1810 static int vpfe_probe(struct platform_device *pdev)
1812 struct vpfe_subdev_info *sdinfo;
1813 struct vpfe_config *vpfe_cfg;
1814 struct resource *res1;
1815 struct vpfe_device *vpfe_dev;
1816 struct i2c_adapter *i2c_adap;
1817 struct video_device *vfd;
1819 int num_subdevs = 0;
1821 /* Get the pointer to the device object */
1822 vpfe_dev = vpfe_initialize();
1825 v4l2_err(pdev->dev.driver,
1826 "Failed to allocate memory for vpfe_dev\n");
1830 vpfe_dev->pdev = &pdev->dev;
1832 if (!pdev->dev.platform_data) {
1833 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1835 goto probe_free_dev_mem;
1838 vpfe_cfg = pdev->dev.platform_data;
1839 vpfe_dev->cfg = vpfe_cfg;
1840 if (!vpfe_cfg->ccdc || !vpfe_cfg->card_name || !vpfe_cfg->sub_devs) {
1841 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1843 goto probe_free_dev_mem;
1846 /* Allocate memory for ccdc configuration */
1847 ccdc_cfg = kmalloc(sizeof(*ccdc_cfg), GFP_KERNEL);
1850 goto probe_free_dev_mem;
1853 mutex_lock(&ccdc_lock);
1855 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1856 /* Get VINT0 irq resource */
1857 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1859 v4l2_err(pdev->dev.driver,
1860 "Unable to get interrupt for VINT0\n");
1862 goto probe_free_ccdc_cfg_mem;
1864 vpfe_dev->ccdc_irq0 = res1->start;
1866 /* Get VINT1 irq resource */
1867 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1869 v4l2_err(pdev->dev.driver,
1870 "Unable to get interrupt for VINT1\n");
1872 goto probe_free_ccdc_cfg_mem;
1874 vpfe_dev->ccdc_irq1 = res1->start;
1876 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
1877 "vpfe_capture0", vpfe_dev);
1880 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1881 goto probe_free_ccdc_cfg_mem;
1884 vfd = &vpfe_dev->video_dev;
1885 /* Initialize field of video device */
1886 vfd->release = video_device_release_empty;
1887 vfd->fops = &vpfe_fops;
1888 vfd->ioctl_ops = &vpfe_ioctl_ops;
1890 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1891 snprintf(vfd->name, sizeof(vfd->name),
1894 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1895 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1896 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1898 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1900 v4l2_err(pdev->dev.driver,
1901 "Unable to register v4l2 device.\n");
1902 goto probe_out_release_irq;
1904 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1905 spin_lock_init(&vpfe_dev->irqlock);
1906 spin_lock_init(&vpfe_dev->dma_queue_lock);
1907 mutex_init(&vpfe_dev->lock);
1909 /* Initialize field of the device objects */
1910 vpfe_dev->numbuffers = config_params.numbuffers;
1912 /* register video device */
1913 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1914 "trying to register vpfe device.\n");
1915 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1916 "video_dev=%p\n", &vpfe_dev->video_dev);
1917 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1918 ret = video_register_device(&vpfe_dev->video_dev,
1919 VFL_TYPE_GRABBER, -1);
1922 v4l2_err(pdev->dev.driver,
1923 "Unable to register video device.\n");
1924 goto probe_out_v4l2_unregister;
1927 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1928 /* set the driver data in platform device */
1929 platform_set_drvdata(pdev, vpfe_dev);
1930 /* set driver private data */
1931 video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
1932 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1933 num_subdevs = vpfe_cfg->num_subdevs;
1934 vpfe_dev->sd = kmalloc_array(num_subdevs,
1935 sizeof(*vpfe_dev->sd),
1937 if (!vpfe_dev->sd) {
1939 goto probe_out_video_unregister;
1942 for (i = 0; i < num_subdevs; i++) {
1943 struct v4l2_input *inps;
1945 sdinfo = &vpfe_cfg->sub_devs[i];
1947 /* Load up the subdevice */
1949 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1951 &sdinfo->board_info,
1953 if (vpfe_dev->sd[i]) {
1954 v4l2_info(&vpfe_dev->v4l2_dev,
1955 "v4l2 sub device %s registered\n",
1957 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1958 /* update tvnorms from the sub devices */
1959 for (j = 0; j < sdinfo->num_inputs; j++) {
1960 inps = &sdinfo->inputs[j];
1961 vfd->tvnorms |= inps->std;
1964 v4l2_info(&vpfe_dev->v4l2_dev,
1965 "v4l2 sub device %s register fails\n",
1972 /* set first sub device as current one */
1973 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1974 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
1976 /* We have at least one sub device to work with */
1977 mutex_unlock(&ccdc_lock);
1981 kfree(vpfe_dev->sd);
1982 probe_out_video_unregister:
1983 video_unregister_device(&vpfe_dev->video_dev);
1984 probe_out_v4l2_unregister:
1985 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1986 probe_out_release_irq:
1987 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1988 probe_free_ccdc_cfg_mem:
1990 mutex_unlock(&ccdc_lock);
1997 * vpfe_remove : It un-register device from V4L2 driver
1999 static int vpfe_remove(struct platform_device *pdev)
2001 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
2003 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2005 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2006 kfree(vpfe_dev->sd);
2007 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
2008 video_unregister_device(&vpfe_dev->video_dev);
2014 static int vpfe_suspend(struct device *dev)
2019 static int vpfe_resume(struct device *dev)
2024 static const struct dev_pm_ops vpfe_dev_pm_ops = {
2025 .suspend = vpfe_suspend,
2026 .resume = vpfe_resume,
2029 static struct platform_driver vpfe_driver = {
2031 .name = CAPTURE_DRV_NAME,
2032 .pm = &vpfe_dev_pm_ops,
2034 .probe = vpfe_probe,
2035 .remove = vpfe_remove,
2038 module_platform_driver(vpfe_driver);