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 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Driver name : VPFE Capture driver
19 * VPFE Capture driver allows applications to capture and stream video
20 * frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
21 * TVP5146 or Raw Bayer RGB image data from an image sensor
22 * such as Microns' MT9T001, MT9T031 etc.
24 * These SoCs have, in common, a Video Processing Subsystem (VPSS) that
25 * consists of a Video Processing Front End (VPFE) for capturing
26 * video/raw image data and Video Processing Back End (VPBE) for displaying
27 * YUV data through an in-built analog encoder or Digital LCD port. This
28 * driver is for capture through VPFE. A typical EVM using these SoCs have
29 * following high level configuration.
32 * decoder(TVP5146/ YUV/
33 * MT9T001) --> Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
42 * The data flow happens from a decoder connected to the VPFE over a
43 * YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
44 * and to the input of VPFE through an optional MUX (if more inputs are
45 * to be interfaced on the EVM). The input data is first passed through
46 * CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
47 * does very little or no processing on YUV data and does pre-process Raw
48 * Bayer RGB data through modules such as Defect Pixel Correction (DFC)
49 * Color Space Conversion (CSC), data gain/offset etc. After this, data
50 * can be written to SDRAM or can be connected to the image processing
51 * block such as IPIPE (on DM355 only).
55 * - Capture using TVP5146 over BT.656
56 * - support for interfacing decoders using sub device model
57 * - Work with DM355 or DM6446 CCDC to do Raw Bayer RGB/YUV
58 * data capture to SDRAM.
60 * - Support multiple REQBUF after open
61 * - Support for de-allocating buffers through REQBUF
62 * - Support for Raw Bayer RGB capture
63 * - Support for chaining Image Processor
64 * - Support for static allocation of buffers
65 * - Support for USERPTR IO
66 * - Support for STREAMON before QBUF
67 * - Support for control ioctls
69 #include <linux/module.h>
70 #include <linux/slab.h>
71 #include <linux/init.h>
72 #include <linux/platform_device.h>
73 #include <linux/interrupt.h>
74 #include <media/v4l2-common.h>
76 #include <media/davinci/vpfe_capture.h>
77 #include "ccdc_hw_device.h"
80 static u32 numbuffers = 3;
81 static u32 bufsize = (720 * 576 * 2);
83 module_param(numbuffers, uint, S_IRUGO);
84 module_param(bufsize, uint, S_IRUGO);
85 module_param(debug, int, 0644);
87 MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
88 MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
89 MODULE_PARM_DESC(debug, "Debug level 0-1");
91 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
92 MODULE_LICENSE("GPL");
93 MODULE_AUTHOR("Texas Instruments");
95 /* standard information */
96 struct vpfe_standard {
100 struct v4l2_fract pixelaspect;
101 /* 0 - progressive, 1 - interlaced */
105 /* ccdc configuration */
107 /* This make sure vpfe is probed and ready to go */
109 /* name of ccdc device */
113 /* data structures */
114 static struct vpfe_config_params config_params = {
117 .min_bufsize = 720 * 480 * 2,
118 .device_bufsize = 720 * 576 * 2,
121 /* ccdc device registered */
122 static struct ccdc_hw_device *ccdc_dev;
123 /* lock for accessing ccdc information */
124 static DEFINE_MUTEX(ccdc_lock);
125 /* ccdc configuration */
126 static struct ccdc_config *ccdc_cfg;
128 static const struct vpfe_standard vpfe_standards[] = {
129 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
130 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
133 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
134 static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
138 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
139 .description = "Bayer GrRBGb 8bit A-Law compr.",
140 .pixelformat = V4L2_PIX_FMT_SBGGR8,
147 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
148 .description = "Bayer GrRBGb - 16bit",
149 .pixelformat = V4L2_PIX_FMT_SBGGR16,
156 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
157 .description = "Bayer GrRBGb 8bit DPCM compr.",
158 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
165 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
166 .description = "YCbCr 4:2:2 Interleaved UYVY",
167 .pixelformat = V4L2_PIX_FMT_UYVY,
174 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
175 .description = "YCbCr 4:2:2 Interleaved YUYV",
176 .pixelformat = V4L2_PIX_FMT_YUYV,
183 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
184 .description = "Y/CbCr 4:2:0 - Semi planar",
185 .pixelformat = V4L2_PIX_FMT_NV12,
192 * vpfe_lookup_pix_format()
193 * lookup an entry in the vpfe pix format table based on pix_format
195 static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
199 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
200 if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
201 return &vpfe_pix_fmts[i];
207 * vpfe_register_ccdc_device. CCDC module calls this to
208 * register with vpfe capture
210 int vpfe_register_ccdc_device(struct ccdc_hw_device *dev)
213 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
215 BUG_ON(!dev->hw_ops.open);
216 BUG_ON(!dev->hw_ops.enable);
217 BUG_ON(!dev->hw_ops.set_hw_if_params);
218 BUG_ON(!dev->hw_ops.configure);
219 BUG_ON(!dev->hw_ops.set_buftype);
220 BUG_ON(!dev->hw_ops.get_buftype);
221 BUG_ON(!dev->hw_ops.enum_pix);
222 BUG_ON(!dev->hw_ops.set_frame_format);
223 BUG_ON(!dev->hw_ops.get_frame_format);
224 BUG_ON(!dev->hw_ops.get_pixel_format);
225 BUG_ON(!dev->hw_ops.set_pixel_format);
226 BUG_ON(!dev->hw_ops.set_image_window);
227 BUG_ON(!dev->hw_ops.get_image_window);
228 BUG_ON(!dev->hw_ops.get_line_length);
229 BUG_ON(!dev->hw_ops.getfid);
231 mutex_lock(&ccdc_lock);
232 if (NULL == ccdc_cfg) {
234 * TODO. Will this ever happen? if so, we need to fix it.
235 * Proabably we need to add the request to a linked list and
236 * walk through it during vpfe probe
238 printk(KERN_ERR "vpfe capture not initialized\n");
243 if (strcmp(dev->name, ccdc_cfg->name)) {
244 /* ignore this ccdc */
250 printk(KERN_ERR "ccdc already registered\n");
257 mutex_unlock(&ccdc_lock);
260 EXPORT_SYMBOL(vpfe_register_ccdc_device);
263 * vpfe_unregister_ccdc_device. CCDC module calls this to
264 * unregister with vpfe capture
266 void vpfe_unregister_ccdc_device(struct ccdc_hw_device *dev)
269 printk(KERN_ERR "invalid ccdc device ptr\n");
273 printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
276 if (strcmp(dev->name, ccdc_cfg->name)) {
277 /* ignore this ccdc */
281 mutex_lock(&ccdc_lock);
283 mutex_unlock(&ccdc_lock);
286 EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
289 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
291 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe_dev,
292 struct v4l2_format *f)
294 struct v4l2_rect image_win;
295 enum ccdc_buftype buf_type;
296 enum ccdc_frmfmt frm_fmt;
298 memset(f, 0, sizeof(*f));
299 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
300 ccdc_dev->hw_ops.get_image_window(&image_win);
301 f->fmt.pix.width = image_win.width;
302 f->fmt.pix.height = image_win.height;
303 f->fmt.pix.bytesperline = ccdc_dev->hw_ops.get_line_length();
304 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
306 buf_type = ccdc_dev->hw_ops.get_buftype();
307 f->fmt.pix.pixelformat = ccdc_dev->hw_ops.get_pixel_format();
308 frm_fmt = ccdc_dev->hw_ops.get_frame_format();
309 if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
310 f->fmt.pix.field = V4L2_FIELD_NONE;
311 else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
312 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
313 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
314 else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED)
315 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
317 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf_type\n");
321 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid frm_fmt\n");
328 * vpfe_config_ccdc_image_format()
329 * For a pix format, configure ccdc to setup the capture
331 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
333 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
336 if (ccdc_dev->hw_ops.set_pixel_format(
337 vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
338 v4l2_err(&vpfe_dev->v4l2_dev,
339 "couldn't set pix format in ccdc\n");
342 /* configure the image window */
343 ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
345 switch (vpfe_dev->fmt.fmt.pix.field) {
346 case V4L2_FIELD_INTERLACED:
347 /* do nothing, since it is default */
348 ret = ccdc_dev->hw_ops.set_buftype(
349 CCDC_BUFTYPE_FLD_INTERLEAVED);
351 case V4L2_FIELD_NONE:
352 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
353 /* buffer type only applicable for interlaced scan */
355 case V4L2_FIELD_SEQ_TB:
356 ret = ccdc_dev->hw_ops.set_buftype(
357 CCDC_BUFTYPE_FLD_SEPARATED);
363 /* set the frame format */
365 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
369 * vpfe_config_image_format()
370 * For a given standard, this functions sets up the default
371 * pix format & crop values in the vpfe device and ccdc. It first
372 * starts with defaults based values from the standard table.
373 * It then checks if sub device support g_mbus_fmt and then override the
374 * values based on that.Sets crop values to match with scan resolution
375 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
378 static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
381 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
382 struct v4l2_mbus_framefmt mbus_fmt;
383 struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
386 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
387 if (vpfe_standards[i].std_id & std_id) {
388 vpfe_dev->std_info.active_pixels =
389 vpfe_standards[i].width;
390 vpfe_dev->std_info.active_lines =
391 vpfe_standards[i].height;
392 vpfe_dev->std_info.frame_format =
393 vpfe_standards[i].frame_format;
394 vpfe_dev->std_index = i;
399 if (i == ARRAY_SIZE(vpfe_standards)) {
400 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
404 vpfe_dev->crop.top = 0;
405 vpfe_dev->crop.left = 0;
406 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
407 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
408 pix->width = vpfe_dev->crop.width;
409 pix->height = vpfe_dev->crop.height;
411 /* first field and frame format based on standard frame format */
412 if (vpfe_dev->std_info.frame_format) {
413 pix->field = V4L2_FIELD_INTERLACED;
414 /* assume V4L2_PIX_FMT_UYVY as default */
415 pix->pixelformat = V4L2_PIX_FMT_UYVY;
416 v4l2_fill_mbus_format(&mbus_fmt, pix,
417 MEDIA_BUS_FMT_YUYV10_2X10);
419 pix->field = V4L2_FIELD_NONE;
420 /* assume V4L2_PIX_FMT_SBGGR8 */
421 pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
422 v4l2_fill_mbus_format(&mbus_fmt, pix,
423 MEDIA_BUS_FMT_SBGGR8_1X8);
426 /* if sub device supports g_mbus_fmt, override the defaults */
427 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
428 sdinfo->grp_id, video, g_mbus_fmt, &mbus_fmt);
430 if (ret && ret != -ENOIOCTLCMD) {
431 v4l2_err(&vpfe_dev->v4l2_dev,
432 "error in getting g_mbus_fmt from sub device\n");
435 v4l2_fill_pix_format(pix, &mbus_fmt);
436 pix->bytesperline = pix->width * 2;
437 pix->sizeimage = pix->bytesperline * pix->height;
439 /* Sets the values in CCDC */
440 ret = vpfe_config_ccdc_image_format(vpfe_dev);
444 /* Update the values of sizeimage and bytesperline */
445 pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
446 pix->sizeimage = pix->bytesperline * pix->height;
451 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
455 /* set first input of current subdevice as the current input */
456 vpfe_dev->current_input = 0;
458 /* set default standard */
459 vpfe_dev->std_index = 0;
461 /* Configure the default format information */
462 ret = vpfe_config_image_format(vpfe_dev,
463 vpfe_standards[vpfe_dev->std_index].std_id);
467 /* now open the ccdc device to initialize it */
468 mutex_lock(&ccdc_lock);
469 if (NULL == ccdc_dev) {
470 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
475 if (!try_module_get(ccdc_dev->owner)) {
476 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
480 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
482 vpfe_dev->initialized = 1;
484 /* Clear all VPFE/CCDC interrupts */
485 if (vpfe_dev->cfg->clr_intr)
486 vpfe_dev->cfg->clr_intr(-1);
489 mutex_unlock(&ccdc_lock);
494 * vpfe_open : It creates object of file handle structure and
495 * stores it in private_data member of filepointer
497 static int vpfe_open(struct file *file)
499 struct vpfe_device *vpfe_dev = video_drvdata(file);
500 struct video_device *vdev = video_devdata(file);
503 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
505 if (!vpfe_dev->cfg->num_subdevs) {
506 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
510 /* Allocate memory for the file handle object */
511 fh = kmalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
513 v4l2_err(&vpfe_dev->v4l2_dev,
514 "unable to allocate memory for file handle object\n");
517 /* store pointer to fh in private_data member of file */
518 file->private_data = fh;
519 fh->vpfe_dev = vpfe_dev;
520 v4l2_fh_init(&fh->fh, vdev);
521 mutex_lock(&vpfe_dev->lock);
522 /* If decoder is not initialized. initialize it */
523 if (!vpfe_dev->initialized) {
524 if (vpfe_initialize_device(vpfe_dev)) {
525 mutex_unlock(&vpfe_dev->lock);
529 /* Increment device usrs counter */
531 /* Set io_allowed member to false */
533 v4l2_fh_add(&fh->fh);
534 mutex_unlock(&vpfe_dev->lock);
538 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
542 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
543 struct videobuf_buffer, queue);
544 list_del(&vpfe_dev->next_frm->queue);
545 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
546 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
548 ccdc_dev->hw_ops.setfbaddr(addr);
551 static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
555 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
556 addr += vpfe_dev->field_off;
557 ccdc_dev->hw_ops.setfbaddr(addr);
560 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
562 v4l2_get_timestamp(&vpfe_dev->cur_frm->ts);
563 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
564 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
565 wake_up_interruptible(&vpfe_dev->cur_frm->done);
566 vpfe_dev->cur_frm = vpfe_dev->next_frm;
570 static irqreturn_t vpfe_isr(int irq, void *dev_id)
572 struct vpfe_device *vpfe_dev = dev_id;
573 enum v4l2_field field;
576 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
577 field = vpfe_dev->fmt.fmt.pix.field;
579 /* if streaming not started, don't do anything */
580 if (!vpfe_dev->started)
583 /* only for 6446 this will be applicable */
584 if (NULL != ccdc_dev->hw_ops.reset)
585 ccdc_dev->hw_ops.reset();
587 if (field == V4L2_FIELD_NONE) {
588 /* handle progressive frame capture */
589 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
590 "frame format is progressive...\n");
591 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
592 vpfe_process_buffer_complete(vpfe_dev);
596 /* interlaced or TB capture check which field we are in hardware */
597 fid = ccdc_dev->hw_ops.getfid();
599 /* switch the software maintained field id */
600 vpfe_dev->field_id ^= 1;
601 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
602 fid, vpfe_dev->field_id);
603 if (fid == vpfe_dev->field_id) {
604 /* we are in-sync here,continue */
607 * One frame is just being captured. If the next frame
608 * is available, release the current frame and move on
610 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
611 vpfe_process_buffer_complete(vpfe_dev);
613 * based on whether the two fields are stored
614 * interleavely or separately in memory, reconfigure
615 * the CCDC memory address
617 if (field == V4L2_FIELD_SEQ_TB) {
618 vpfe_schedule_bottom_field(vpfe_dev);
623 * if one field is just being captured configure
624 * the next frame get the next frame from the empty
625 * queue if no frame is available hold on to the
628 spin_lock(&vpfe_dev->dma_queue_lock);
629 if (!list_empty(&vpfe_dev->dma_queue) &&
630 vpfe_dev->cur_frm == vpfe_dev->next_frm)
631 vpfe_schedule_next_buffer(vpfe_dev);
632 spin_unlock(&vpfe_dev->dma_queue_lock);
633 } else if (fid == 0) {
635 * out of sync. Recover from any hardware out-of-sync.
636 * May loose one frame
638 vpfe_dev->field_id = fid;
641 if (vpfe_dev->cfg->clr_intr)
642 vpfe_dev->cfg->clr_intr(irq);
647 /* vdint1_isr - isr handler for VINT1 interrupt */
648 static irqreturn_t vdint1_isr(int irq, void *dev_id)
650 struct vpfe_device *vpfe_dev = dev_id;
652 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
654 /* if streaming not started, don't do anything */
655 if (!vpfe_dev->started) {
656 if (vpfe_dev->cfg->clr_intr)
657 vpfe_dev->cfg->clr_intr(irq);
661 spin_lock(&vpfe_dev->dma_queue_lock);
662 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
663 !list_empty(&vpfe_dev->dma_queue) &&
664 vpfe_dev->cur_frm == vpfe_dev->next_frm)
665 vpfe_schedule_next_buffer(vpfe_dev);
666 spin_unlock(&vpfe_dev->dma_queue_lock);
668 if (vpfe_dev->cfg->clr_intr)
669 vpfe_dev->cfg->clr_intr(irq);
674 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
676 enum ccdc_frmfmt frame_format;
678 frame_format = ccdc_dev->hw_ops.get_frame_format();
679 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
680 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
683 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
685 enum ccdc_frmfmt frame_format;
687 frame_format = ccdc_dev->hw_ops.get_frame_format();
688 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
689 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
696 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
697 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
699 vpfe_dev->started = 0;
700 ccdc_dev->hw_ops.enable(0);
701 if (ccdc_dev->hw_ops.enable_out_to_sdram)
702 ccdc_dev->hw_ops.enable_out_to_sdram(0);
706 * vpfe_release : This function deletes buffer queue, frees the
707 * buffers and the vpfe file handle
709 static int vpfe_release(struct file *file)
711 struct vpfe_device *vpfe_dev = video_drvdata(file);
712 struct vpfe_fh *fh = file->private_data;
713 struct vpfe_subdev_info *sdinfo;
716 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
718 /* Get the device lock */
719 mutex_lock(&vpfe_dev->lock);
720 /* if this instance is doing IO */
721 if (fh->io_allowed) {
722 if (vpfe_dev->started) {
723 sdinfo = vpfe_dev->current_subdev;
724 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
727 if (ret && (ret != -ENOIOCTLCMD))
728 v4l2_err(&vpfe_dev->v4l2_dev,
729 "stream off failed in subdev\n");
730 vpfe_stop_ccdc_capture(vpfe_dev);
731 vpfe_detach_irq(vpfe_dev);
732 videobuf_streamoff(&vpfe_dev->buffer_queue);
734 vpfe_dev->io_usrs = 0;
735 vpfe_dev->numbuffers = config_params.numbuffers;
736 videobuf_stop(&vpfe_dev->buffer_queue);
737 videobuf_mmap_free(&vpfe_dev->buffer_queue);
740 /* Decrement device usrs counter */
742 v4l2_fh_del(&fh->fh);
743 v4l2_fh_exit(&fh->fh);
744 /* If this is the last file handle */
745 if (!vpfe_dev->usrs) {
746 vpfe_dev->initialized = 0;
747 if (ccdc_dev->hw_ops.close)
748 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
749 module_put(ccdc_dev->owner);
751 mutex_unlock(&vpfe_dev->lock);
752 file->private_data = NULL;
753 /* Free memory allocated to file handle object */
759 * vpfe_mmap : It is used to map kernel space buffers
762 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
764 /* Get the device object and file handle object */
765 struct vpfe_device *vpfe_dev = video_drvdata(file);
767 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
769 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
773 * vpfe_poll: It is used for select/poll system call
775 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
777 struct vpfe_device *vpfe_dev = video_drvdata(file);
779 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
781 if (vpfe_dev->started)
782 return videobuf_poll_stream(file,
783 &vpfe_dev->buffer_queue, wait);
787 /* vpfe capture driver file operations */
788 static const struct v4l2_file_operations vpfe_fops = {
789 .owner = THIS_MODULE,
791 .release = vpfe_release,
792 .unlocked_ioctl = video_ioctl2,
798 * vpfe_check_format()
799 * This function adjust the input pixel format as per hardware
800 * capabilities and update the same in pixfmt.
801 * Following algorithm used :-
803 * If given pixformat is not in the vpfe list of pix formats or not
804 * supported by the hardware, current value of pixformat in the device
806 * If given field is not supported, then current field is used. If field
807 * is different from current, then it is matched with that from sub device.
808 * Minimum height is 2 lines for interlaced or tb field and 1 line for
809 * progressive. Maximum height is clamped to active active lines of scan
810 * Minimum width is 32 bytes in memory and width is clamped to active
812 * bytesperline is a multiple of 32.
814 static const struct vpfe_pixel_format *
815 vpfe_check_format(struct vpfe_device *vpfe_dev,
816 struct v4l2_pix_format *pixfmt)
818 u32 min_height = 1, min_width = 32, max_width, max_height;
819 const struct vpfe_pixel_format *vpfe_pix_fmt;
823 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
824 if (NULL == vpfe_pix_fmt) {
826 * use current pixel format in the vpfe device. We
827 * will find this pix format in the table
829 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
830 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
833 /* check if hw supports it */
836 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
837 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
845 /* use current pixel format */
846 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
848 * Since this is currently used in the vpfe device, we
849 * will find this pix format in the table
851 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
854 /* check what field format is supported */
855 if (pixfmt->field == V4L2_FIELD_ANY) {
856 /* if field is any, use current value as default */
857 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
861 * if field is not same as current field in the vpfe device
862 * try matching the field with the sub device field
864 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
866 * If field value is not in the supported fields, use current
867 * field used in the device as default
869 switch (pixfmt->field) {
870 case V4L2_FIELD_INTERLACED:
871 case V4L2_FIELD_SEQ_TB:
872 /* if sub device is supporting progressive, use that */
873 if (!vpfe_dev->std_info.frame_format)
874 pixfmt->field = V4L2_FIELD_NONE;
876 case V4L2_FIELD_NONE:
877 if (vpfe_dev->std_info.frame_format)
878 pixfmt->field = V4L2_FIELD_INTERLACED;
882 /* use current field as default */
883 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
888 /* Now adjust image resolutions supported */
889 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
890 pixfmt->field == V4L2_FIELD_SEQ_TB)
893 max_width = vpfe_dev->std_info.active_pixels;
894 max_height = vpfe_dev->std_info.active_lines;
895 min_width /= vpfe_pix_fmt->bpp;
897 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
898 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
900 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
901 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
903 /* If interlaced, adjust height to be a multiple of 2 */
904 if (pixfmt->field == V4L2_FIELD_INTERLACED)
905 pixfmt->height &= (~1);
907 * recalculate bytesperline and sizeimage since width
908 * and height might have changed
910 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
912 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
914 pixfmt->bytesperline * pixfmt->height +
915 ((pixfmt->bytesperline * pixfmt->height) >> 1);
917 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
919 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height ="
920 " %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
921 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
922 pixfmt->bytesperline, pixfmt->sizeimage);
926 static int vpfe_querycap(struct file *file, void *priv,
927 struct v4l2_capability *cap)
929 struct vpfe_device *vpfe_dev = video_drvdata(file);
931 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
933 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
934 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
935 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
936 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
937 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
941 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
942 struct v4l2_format *fmt)
944 struct vpfe_device *vpfe_dev = video_drvdata(file);
946 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
947 /* Fill in the information about format */
948 *fmt = vpfe_dev->fmt;
952 static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
953 struct v4l2_fmtdesc *fmt)
955 struct vpfe_device *vpfe_dev = video_drvdata(file);
956 const struct vpfe_pixel_format *pix_fmt;
960 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
962 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
965 /* Fill in the information about format */
966 pix_fmt = vpfe_lookup_pix_format(pix);
967 if (NULL != pix_fmt) {
968 temp_index = fmt->index;
969 *fmt = pix_fmt->fmtdesc;
970 fmt->index = temp_index;
976 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
977 struct v4l2_format *fmt)
979 struct vpfe_device *vpfe_dev = video_drvdata(file);
980 const struct vpfe_pixel_format *pix_fmts;
983 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
985 /* If streaming is started, return error */
986 if (vpfe_dev->started) {
987 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
991 /* Check for valid frame format */
992 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
994 if (NULL == pix_fmts)
997 /* store the pixel format in the device object */
998 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1002 /* First detach any IRQ if currently attached */
1003 vpfe_detach_irq(vpfe_dev);
1004 vpfe_dev->fmt = *fmt;
1005 /* set image capture parameters in the ccdc */
1006 ret = vpfe_config_ccdc_image_format(vpfe_dev);
1007 mutex_unlock(&vpfe_dev->lock);
1011 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
1012 struct v4l2_format *f)
1014 struct vpfe_device *vpfe_dev = video_drvdata(file);
1015 const struct vpfe_pixel_format *pix_fmts;
1017 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
1019 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
1020 if (NULL == pix_fmts)
1026 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1027 * given app input index
1029 static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1031 int *subdev_input_index,
1032 int app_input_index)
1034 struct vpfe_config *cfg = vpfe_dev->cfg;
1035 struct vpfe_subdev_info *sdinfo;
1038 for (i = 0; i < cfg->num_subdevs; i++) {
1039 sdinfo = &cfg->sub_devs[i];
1040 if (app_input_index < (j + sdinfo->num_inputs)) {
1042 *subdev_input_index = app_input_index - j;
1045 j += sdinfo->num_inputs;
1051 * vpfe_get_app_input - Get app input index for a given subdev input index
1052 * driver stores the input index of the current sub device and translate it
1053 * when application request the current input
1055 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1056 int *app_input_index)
1058 struct vpfe_config *cfg = vpfe_dev->cfg;
1059 struct vpfe_subdev_info *sdinfo;
1062 for (i = 0; i < cfg->num_subdevs; i++) {
1063 sdinfo = &cfg->sub_devs[i];
1064 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1065 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1067 *app_input_index = j + vpfe_dev->current_input;
1070 j += sdinfo->num_inputs;
1075 static int vpfe_enum_input(struct file *file, void *priv,
1076 struct v4l2_input *inp)
1078 struct vpfe_device *vpfe_dev = video_drvdata(file);
1079 struct vpfe_subdev_info *sdinfo;
1082 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1084 if (vpfe_get_subdev_input_index(vpfe_dev,
1088 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found"
1089 " for the subdev\n");
1092 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1093 memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1097 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1099 struct vpfe_device *vpfe_dev = video_drvdata(file);
1101 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1103 return vpfe_get_app_input_index(vpfe_dev, index);
1107 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1109 struct vpfe_device *vpfe_dev = video_drvdata(file);
1110 struct v4l2_subdev *sd;
1111 struct vpfe_subdev_info *sdinfo;
1112 int subdev_index, inp_index;
1113 struct vpfe_route *route;
1114 u32 input = 0, output = 0;
1117 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1119 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1124 * If streaming is started return device busy
1127 if (vpfe_dev->started) {
1128 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1132 ret = vpfe_get_subdev_input_index(vpfe_dev,
1137 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1141 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1142 sd = vpfe_dev->sd[subdev_index];
1143 route = &sdinfo->routes[inp_index];
1144 if (route && sdinfo->can_route) {
1145 input = route->input;
1146 output = route->output;
1150 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1153 v4l2_err(&vpfe_dev->v4l2_dev,
1154 "vpfe_doioctl:error in setting input in decoder\n");
1158 vpfe_dev->current_subdev = sdinfo;
1160 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
1161 vpfe_dev->current_input = index;
1162 vpfe_dev->std_index = 0;
1164 /* set the bus/interface parameter for the sub device in ccdc */
1165 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1169 /* set the default image parameters in the device */
1170 ret = vpfe_config_image_format(vpfe_dev,
1171 vpfe_standards[vpfe_dev->std_index].std_id);
1173 mutex_unlock(&vpfe_dev->lock);
1177 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1179 struct vpfe_device *vpfe_dev = video_drvdata(file);
1180 struct vpfe_subdev_info *sdinfo;
1183 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1185 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1186 sdinfo = vpfe_dev->current_subdev;
1189 /* Call querystd function of decoder device */
1190 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1191 video, querystd, std_id);
1192 mutex_unlock(&vpfe_dev->lock);
1196 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1198 struct vpfe_device *vpfe_dev = video_drvdata(file);
1199 struct vpfe_subdev_info *sdinfo;
1202 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1204 /* Call decoder driver function to set the standard */
1205 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1209 sdinfo = vpfe_dev->current_subdev;
1210 /* If streaming is started, return device busy error */
1211 if (vpfe_dev->started) {
1212 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1217 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1218 video, s_std, std_id);
1220 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1223 ret = vpfe_config_image_format(vpfe_dev, std_id);
1226 mutex_unlock(&vpfe_dev->lock);
1230 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1232 struct vpfe_device *vpfe_dev = video_drvdata(file);
1234 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1236 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1240 * Videobuf operations
1242 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1243 unsigned int *count,
1246 struct vpfe_fh *fh = vq->priv_data;
1247 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1249 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1250 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1251 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1252 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1253 *size = config_params.device_bufsize;
1255 if (*count < config_params.min_numbuffers)
1256 *count = config_params.min_numbuffers;
1257 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1258 "count=%d, size=%d\n", *count, *size);
1262 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1263 struct videobuf_buffer *vb,
1264 enum v4l2_field field)
1266 struct vpfe_fh *fh = vq->priv_data;
1267 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1271 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1273 /* If buffer is not initialized, initialize it */
1274 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1275 vb->width = vpfe_dev->fmt.fmt.pix.width;
1276 vb->height = vpfe_dev->fmt.fmt.pix.height;
1277 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1280 ret = videobuf_iolock(vq, vb, NULL);
1284 addr = videobuf_to_dma_contig(vb);
1285 /* Make sure user addresses are aligned to 32 bytes */
1286 if (!ALIGN(addr, 32))
1289 vb->state = VIDEOBUF_PREPARED;
1294 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1295 struct videobuf_buffer *vb)
1297 /* Get the file handle object and device object */
1298 struct vpfe_fh *fh = vq->priv_data;
1299 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1300 unsigned long flags;
1302 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1304 /* add the buffer to the DMA queue */
1305 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1306 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1307 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1309 /* Change state of the buffer */
1310 vb->state = VIDEOBUF_QUEUED;
1313 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1314 struct videobuf_buffer *vb)
1316 struct vpfe_fh *fh = vq->priv_data;
1317 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1318 unsigned long flags;
1320 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1323 * We need to flush the buffer from the dma queue since
1324 * they are de-allocated
1326 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1327 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1328 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1329 videobuf_dma_contig_free(vq, vb);
1330 vb->state = VIDEOBUF_NEEDS_INIT;
1333 static struct videobuf_queue_ops vpfe_videobuf_qops = {
1334 .buf_setup = vpfe_videobuf_setup,
1335 .buf_prepare = vpfe_videobuf_prepare,
1336 .buf_queue = vpfe_videobuf_queue,
1337 .buf_release = vpfe_videobuf_release,
1341 * vpfe_reqbufs. currently support REQBUF only once opening
1344 static int vpfe_reqbufs(struct file *file, void *priv,
1345 struct v4l2_requestbuffers *req_buf)
1347 struct vpfe_device *vpfe_dev = video_drvdata(file);
1348 struct vpfe_fh *fh = file->private_data;
1351 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1353 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1354 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1358 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1362 if (vpfe_dev->io_usrs != 0) {
1363 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1368 vpfe_dev->memory = req_buf->memory;
1369 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1370 &vpfe_videobuf_qops,
1374 vpfe_dev->fmt.fmt.pix.field,
1375 sizeof(struct videobuf_buffer),
1379 vpfe_dev->io_usrs = 1;
1380 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1381 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1383 mutex_unlock(&vpfe_dev->lock);
1387 static int vpfe_querybuf(struct file *file, void *priv,
1388 struct v4l2_buffer *buf)
1390 struct vpfe_device *vpfe_dev = video_drvdata(file);
1392 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1394 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1395 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1399 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1400 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1403 /* Call videobuf_querybuf to get information */
1404 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1407 static int vpfe_qbuf(struct file *file, void *priv,
1408 struct v4l2_buffer *p)
1410 struct vpfe_device *vpfe_dev = video_drvdata(file);
1411 struct vpfe_fh *fh = file->private_data;
1413 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1415 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1416 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1421 * If this file handle is not allowed to do IO,
1424 if (!fh->io_allowed) {
1425 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1428 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1431 static int vpfe_dqbuf(struct file *file, void *priv,
1432 struct v4l2_buffer *buf)
1434 struct vpfe_device *vpfe_dev = video_drvdata(file);
1436 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1438 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1439 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1442 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1443 buf, file->f_flags & O_NONBLOCK);
1447 * vpfe_calculate_offsets : This function calculates buffers offset
1448 * for top and bottom field
1450 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1452 struct v4l2_rect image_win;
1454 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1456 ccdc_dev->hw_ops.get_image_window(&image_win);
1457 vpfe_dev->field_off = image_win.height * image_win.width;
1460 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1461 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1463 ccdc_dev->hw_ops.enable(1);
1464 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1465 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1466 vpfe_dev->started = 1;
1470 * vpfe_streamon. Assume the DMA queue is not empty.
1471 * application is expected to call QBUF before calling
1472 * this ioctl. If not, driver returns error
1474 static int vpfe_streamon(struct file *file, void *priv,
1475 enum v4l2_buf_type buf_type)
1477 struct vpfe_device *vpfe_dev = video_drvdata(file);
1478 struct vpfe_fh *fh = file->private_data;
1479 struct vpfe_subdev_info *sdinfo;
1483 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1485 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1486 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1490 /* If file handle is not allowed IO, return error */
1491 if (!fh->io_allowed) {
1492 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1496 sdinfo = vpfe_dev->current_subdev;
1497 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1498 video, s_stream, 1);
1500 if (ret && (ret != -ENOIOCTLCMD)) {
1501 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1505 /* If buffer queue is empty, return error */
1506 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1507 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1511 /* Call videobuf_streamon to start streaming * in videobuf */
1512 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1517 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1520 /* Get the next frame from the buffer queue */
1521 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1522 struct videobuf_buffer, queue);
1523 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1524 /* Remove buffer from the buffer queue */
1525 list_del(&vpfe_dev->cur_frm->queue);
1526 /* Mark state of the current frame to active */
1527 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1528 /* Initialize field_id and started member */
1529 vpfe_dev->field_id = 0;
1530 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1532 /* Calculate field offset */
1533 vpfe_calculate_offsets(vpfe_dev);
1535 if (vpfe_attach_irq(vpfe_dev) < 0) {
1536 v4l2_err(&vpfe_dev->v4l2_dev,
1537 "Error in attaching interrupt handle\n");
1541 if (ccdc_dev->hw_ops.configure() < 0) {
1542 v4l2_err(&vpfe_dev->v4l2_dev,
1543 "Error in configuring ccdc\n");
1547 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1548 vpfe_start_ccdc_capture(vpfe_dev);
1549 mutex_unlock(&vpfe_dev->lock);
1552 mutex_unlock(&vpfe_dev->lock);
1554 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1558 static int vpfe_streamoff(struct file *file, void *priv,
1559 enum v4l2_buf_type buf_type)
1561 struct vpfe_device *vpfe_dev = video_drvdata(file);
1562 struct vpfe_fh *fh = file->private_data;
1563 struct vpfe_subdev_info *sdinfo;
1566 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1568 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1569 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1573 /* If io is allowed for this file handle, return error */
1574 if (!fh->io_allowed) {
1575 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1579 /* If streaming is not started, return error */
1580 if (!vpfe_dev->started) {
1581 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1585 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1589 vpfe_stop_ccdc_capture(vpfe_dev);
1590 vpfe_detach_irq(vpfe_dev);
1592 sdinfo = vpfe_dev->current_subdev;
1593 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1594 video, s_stream, 0);
1596 if (ret && (ret != -ENOIOCTLCMD))
1597 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1598 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1599 mutex_unlock(&vpfe_dev->lock);
1603 static int vpfe_cropcap(struct file *file, void *priv,
1604 struct v4l2_cropcap *crop)
1606 struct vpfe_device *vpfe_dev = video_drvdata(file);
1608 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1610 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1613 memset(crop, 0, sizeof(struct v4l2_cropcap));
1614 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1615 crop->bounds.width = crop->defrect.width =
1616 vpfe_standards[vpfe_dev->std_index].width;
1617 crop->bounds.height = crop->defrect.height =
1618 vpfe_standards[vpfe_dev->std_index].height;
1619 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1623 static int vpfe_g_crop(struct file *file, void *priv,
1624 struct v4l2_crop *crop)
1626 struct vpfe_device *vpfe_dev = video_drvdata(file);
1628 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_crop\n");
1630 crop->c = vpfe_dev->crop;
1634 static int vpfe_s_crop(struct file *file, void *priv,
1635 const struct v4l2_crop *crop)
1637 struct vpfe_device *vpfe_dev = video_drvdata(file);
1638 struct v4l2_rect rect = crop->c;
1641 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_crop\n");
1643 if (vpfe_dev->started) {
1644 /* make sure streaming is not started */
1645 v4l2_err(&vpfe_dev->v4l2_dev,
1646 "Cannot change crop when streaming is ON\n");
1650 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1654 if (rect.top < 0 || rect.left < 0) {
1655 v4l2_err(&vpfe_dev->v4l2_dev,
1656 "doesn't support negative values for top & left\n");
1661 /* adjust the width to 16 pixel boundary */
1662 rect.width = ((rect.width + 15) & ~0xf);
1664 /* make sure parameters are valid */
1665 if ((rect.left + rect.width >
1666 vpfe_dev->std_info.active_pixels) ||
1667 (rect.top + rect.height >
1668 vpfe_dev->std_info.active_lines)) {
1669 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_CROP params\n");
1673 ccdc_dev->hw_ops.set_image_window(&rect);
1674 vpfe_dev->fmt.fmt.pix.width = rect.width;
1675 vpfe_dev->fmt.fmt.pix.height = rect.height;
1676 vpfe_dev->fmt.fmt.pix.bytesperline =
1677 ccdc_dev->hw_ops.get_line_length();
1678 vpfe_dev->fmt.fmt.pix.sizeimage =
1679 vpfe_dev->fmt.fmt.pix.bytesperline *
1680 vpfe_dev->fmt.fmt.pix.height;
1681 vpfe_dev->crop = rect;
1683 mutex_unlock(&vpfe_dev->lock);
1688 static long vpfe_param_handler(struct file *file, void *priv,
1689 bool valid_prio, unsigned int cmd, void *param)
1691 struct vpfe_device *vpfe_dev = video_drvdata(file);
1694 v4l2_dbg(2, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
1696 if (vpfe_dev->started) {
1697 /* only allowed if streaming is not started */
1698 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1699 "device already started\n");
1703 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1708 case VPFE_CMD_S_CCDC_RAW_PARAMS:
1709 v4l2_warn(&vpfe_dev->v4l2_dev,
1710 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
1711 if (ccdc_dev->hw_ops.set_params) {
1712 ret = ccdc_dev->hw_ops.set_params(param);
1714 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1715 "Error setting parameters in CCDC\n");
1718 ret = vpfe_get_ccdc_image_format(vpfe_dev,
1721 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1722 "Invalid image format at CCDC\n");
1727 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1728 "VPFE_CMD_S_CCDC_RAW_PARAMS not supported\n");
1735 mutex_unlock(&vpfe_dev->lock);
1740 /* vpfe capture ioctl operations */
1741 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1742 .vidioc_querycap = vpfe_querycap,
1743 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1744 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1745 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1746 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1747 .vidioc_enum_input = vpfe_enum_input,
1748 .vidioc_g_input = vpfe_g_input,
1749 .vidioc_s_input = vpfe_s_input,
1750 .vidioc_querystd = vpfe_querystd,
1751 .vidioc_s_std = vpfe_s_std,
1752 .vidioc_g_std = vpfe_g_std,
1753 .vidioc_reqbufs = vpfe_reqbufs,
1754 .vidioc_querybuf = vpfe_querybuf,
1755 .vidioc_qbuf = vpfe_qbuf,
1756 .vidioc_dqbuf = vpfe_dqbuf,
1757 .vidioc_streamon = vpfe_streamon,
1758 .vidioc_streamoff = vpfe_streamoff,
1759 .vidioc_cropcap = vpfe_cropcap,
1760 .vidioc_g_crop = vpfe_g_crop,
1761 .vidioc_s_crop = vpfe_s_crop,
1762 .vidioc_default = vpfe_param_handler,
1765 static struct vpfe_device *vpfe_initialize(void)
1767 struct vpfe_device *vpfe_dev;
1769 /* Default number of buffers should be 3 */
1770 if ((numbuffers > 0) &&
1771 (numbuffers < config_params.min_numbuffers))
1772 numbuffers = config_params.min_numbuffers;
1775 * Set buffer size to min buffers size if invalid buffer size is
1778 if (bufsize < config_params.min_bufsize)
1779 bufsize = config_params.min_bufsize;
1781 config_params.numbuffers = numbuffers;
1784 config_params.device_bufsize = bufsize;
1786 /* Allocate memory for device objects */
1787 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1793 * vpfe_probe : This function creates device entries by register
1794 * itself to the V4L2 driver and initializes fields of each
1797 static int vpfe_probe(struct platform_device *pdev)
1799 struct vpfe_subdev_info *sdinfo;
1800 struct vpfe_config *vpfe_cfg;
1801 struct resource *res1;
1802 struct vpfe_device *vpfe_dev;
1803 struct i2c_adapter *i2c_adap;
1804 struct video_device *vfd;
1805 int ret = -ENOMEM, i, j;
1806 int num_subdevs = 0;
1808 /* Get the pointer to the device object */
1809 vpfe_dev = vpfe_initialize();
1812 v4l2_err(pdev->dev.driver,
1813 "Failed to allocate memory for vpfe_dev\n");
1817 vpfe_dev->pdev = &pdev->dev;
1819 if (NULL == pdev->dev.platform_data) {
1820 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1822 goto probe_free_dev_mem;
1825 vpfe_cfg = pdev->dev.platform_data;
1826 vpfe_dev->cfg = vpfe_cfg;
1827 if (NULL == vpfe_cfg->ccdc ||
1828 NULL == vpfe_cfg->card_name ||
1829 NULL == vpfe_cfg->sub_devs) {
1830 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1832 goto probe_free_dev_mem;
1835 /* Allocate memory for ccdc configuration */
1836 ccdc_cfg = kmalloc(sizeof(struct ccdc_config), GFP_KERNEL);
1837 if (NULL == ccdc_cfg) {
1838 v4l2_err(pdev->dev.driver,
1839 "Memory allocation failed for ccdc_cfg\n");
1840 goto probe_free_dev_mem;
1843 mutex_lock(&ccdc_lock);
1845 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1846 /* Get VINT0 irq resource */
1847 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1849 v4l2_err(pdev->dev.driver,
1850 "Unable to get interrupt for VINT0\n");
1852 goto probe_free_ccdc_cfg_mem;
1854 vpfe_dev->ccdc_irq0 = res1->start;
1856 /* Get VINT1 irq resource */
1857 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1859 v4l2_err(pdev->dev.driver,
1860 "Unable to get interrupt for VINT1\n");
1862 goto probe_free_ccdc_cfg_mem;
1864 vpfe_dev->ccdc_irq1 = res1->start;
1866 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
1867 "vpfe_capture0", vpfe_dev);
1870 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1871 goto probe_free_ccdc_cfg_mem;
1874 /* Allocate memory for video device */
1875 vfd = video_device_alloc();
1878 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
1879 goto probe_out_release_irq;
1882 /* Initialize field of video device */
1883 vfd->release = video_device_release;
1884 vfd->fops = &vpfe_fops;
1885 vfd->ioctl_ops = &vpfe_ioctl_ops;
1887 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1888 snprintf(vfd->name, sizeof(vfd->name),
1891 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1892 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1893 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1894 /* Set video_dev to the video device */
1895 vpfe_dev->video_dev = vfd;
1897 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1899 v4l2_err(pdev->dev.driver,
1900 "Unable to register v4l2 device.\n");
1901 goto probe_out_video_release;
1903 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1904 spin_lock_init(&vpfe_dev->irqlock);
1905 spin_lock_init(&vpfe_dev->dma_queue_lock);
1906 mutex_init(&vpfe_dev->lock);
1908 /* Initialize field of the device objects */
1909 vpfe_dev->numbuffers = config_params.numbuffers;
1911 /* register video device */
1912 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1913 "trying to register vpfe device.\n");
1914 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1915 "video_dev=%p\n", &vpfe_dev->video_dev);
1916 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1917 ret = video_register_device(vpfe_dev->video_dev,
1918 VFL_TYPE_GRABBER, -1);
1921 v4l2_err(pdev->dev.driver,
1922 "Unable to register video device.\n");
1923 goto probe_out_v4l2_unregister;
1926 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1927 /* set the driver data in platform device */
1928 platform_set_drvdata(pdev, vpfe_dev);
1929 /* set driver private data */
1930 video_set_drvdata(vpfe_dev->video_dev, vpfe_dev);
1931 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1932 num_subdevs = vpfe_cfg->num_subdevs;
1933 vpfe_dev->sd = kmalloc(sizeof(struct v4l2_subdev *) * num_subdevs,
1935 if (NULL == vpfe_dev->sd) {
1936 v4l2_err(&vpfe_dev->v4l2_dev,
1937 "unable to allocate memory for subdevice pointers\n");
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",
1971 /* set first sub device as current one */
1972 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1973 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
1975 /* We have at least one sub device to work with */
1976 mutex_unlock(&ccdc_lock);
1980 kfree(vpfe_dev->sd);
1981 probe_out_video_unregister:
1982 video_unregister_device(vpfe_dev->video_dev);
1983 probe_out_v4l2_unregister:
1984 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1985 probe_out_video_release:
1986 if (!video_is_registered(vpfe_dev->video_dev))
1987 video_device_release(vpfe_dev->video_dev);
1988 probe_out_release_irq:
1989 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1990 probe_free_ccdc_cfg_mem:
1992 mutex_unlock(&ccdc_lock);
1999 * vpfe_remove : It un-register device from V4L2 driver
2001 static int vpfe_remove(struct platform_device *pdev)
2003 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
2005 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2007 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2008 kfree(vpfe_dev->sd);
2009 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
2010 video_unregister_device(vpfe_dev->video_dev);
2016 static int vpfe_suspend(struct device *dev)
2021 static int vpfe_resume(struct device *dev)
2026 static const struct dev_pm_ops vpfe_dev_pm_ops = {
2027 .suspend = vpfe_suspend,
2028 .resume = vpfe_resume,
2031 static struct platform_driver vpfe_driver = {
2033 .name = CAPTURE_DRV_NAME,
2034 .owner = THIS_MODULE,
2035 .pm = &vpfe_dev_pm_ops,
2037 .probe = vpfe_probe,
2038 .remove = vpfe_remove,
2041 module_platform_driver(vpfe_driver);