1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2008-2009 Texas Instruments Inc
5 * Driver name : VPFE Capture driver
6 * VPFE Capture driver allows applications to capture and stream video
7 * frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
8 * TVP5146 or Raw Bayer RGB image data from an image sensor
9 * such as Microns' MT9T001, MT9T031 etc.
11 * These SoCs have, in common, a Video Processing Subsystem (VPSS) that
12 * consists of a Video Processing Front End (VPFE) for capturing
13 * video/raw image data and Video Processing Back End (VPBE) for displaying
14 * YUV data through an in-built analog encoder or Digital LCD port. This
15 * driver is for capture through VPFE. A typical EVM using these SoCs have
16 * following high level configuration.
18 * decoder(TVP5146/ YUV/
19 * MT9T001) --> Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
28 * The data flow happens from a decoder connected to the VPFE over a
29 * YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
30 * and to the input of VPFE through an optional MUX (if more inputs are
31 * to be interfaced on the EVM). The input data is first passed through
32 * CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
33 * does very little or no processing on YUV data and does pre-process Raw
34 * Bayer RGB data through modules such as Defect Pixel Correction (DFC)
35 * Color Space Conversion (CSC), data gain/offset etc. After this, data
36 * can be written to SDRAM or can be connected to the image processing
37 * block such as IPIPE (on DM355 only).
41 * - Capture using TVP5146 over BT.656
42 * - support for interfacing decoders using sub device model
43 * - Work with DM355 or DM6446 CCDC to do Raw Bayer RGB/YUV
44 * data capture to SDRAM.
46 * - Support multiple REQBUF after open
47 * - Support for de-allocating buffers through REQBUF
48 * - Support for Raw Bayer RGB capture
49 * - Support for chaining Image Processor
50 * - Support for static allocation of buffers
51 * - Support for USERPTR IO
52 * - Support for STREAMON before QBUF
53 * - Support for control ioctls
55 #include <linux/module.h>
56 #include <linux/slab.h>
57 #include <linux/init.h>
58 #include <linux/platform_device.h>
59 #include <linux/interrupt.h>
60 #include <media/v4l2-common.h>
62 #include <media/davinci/vpfe_capture.h>
63 #include "ccdc_hw_device.h"
66 static u32 numbuffers = 3;
67 static u32 bufsize = (720 * 576 * 2);
69 module_param(numbuffers, uint, S_IRUGO);
70 module_param(bufsize, uint, S_IRUGO);
71 module_param(debug, int, 0644);
73 MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
74 MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
75 MODULE_PARM_DESC(debug, "Debug level 0-1");
77 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
78 MODULE_LICENSE("GPL");
79 MODULE_AUTHOR("Texas Instruments");
81 /* standard information */
82 struct vpfe_standard {
86 struct v4l2_fract pixelaspect;
87 /* 0 - progressive, 1 - interlaced */
91 /* ccdc configuration */
93 /* This make sure vpfe is probed and ready to go */
95 /* name of ccdc device */
100 static struct vpfe_config_params config_params = {
103 .min_bufsize = 720 * 480 * 2,
104 .device_bufsize = 720 * 576 * 2,
107 /* ccdc device registered */
108 static const struct ccdc_hw_device *ccdc_dev;
109 /* lock for accessing ccdc information */
110 static DEFINE_MUTEX(ccdc_lock);
111 /* ccdc configuration */
112 static struct ccdc_config *ccdc_cfg;
114 static const struct vpfe_standard vpfe_standards[] = {
115 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
116 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
119 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
120 static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
124 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
125 .description = "Bayer GrRBGb 8bit A-Law compr.",
126 .pixelformat = V4L2_PIX_FMT_SBGGR8,
133 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
134 .description = "Bayer GrRBGb - 16bit",
135 .pixelformat = V4L2_PIX_FMT_SBGGR16,
142 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
143 .description = "Bayer GrRBGb 8bit DPCM compr.",
144 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
151 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
152 .description = "YCbCr 4:2:2 Interleaved UYVY",
153 .pixelformat = V4L2_PIX_FMT_UYVY,
160 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
161 .description = "YCbCr 4:2:2 Interleaved YUYV",
162 .pixelformat = V4L2_PIX_FMT_YUYV,
169 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
170 .description = "Y/CbCr 4:2:0 - Semi planar",
171 .pixelformat = V4L2_PIX_FMT_NV12,
178 * vpfe_lookup_pix_format()
179 * lookup an entry in the vpfe pix format table based on pix_format
181 static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
185 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
186 if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
187 return &vpfe_pix_fmts[i];
193 * vpfe_register_ccdc_device. CCDC module calls this to
194 * register with vpfe capture
196 int vpfe_register_ccdc_device(const struct ccdc_hw_device *dev)
199 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
201 BUG_ON(!dev->hw_ops.open);
202 BUG_ON(!dev->hw_ops.enable);
203 BUG_ON(!dev->hw_ops.set_hw_if_params);
204 BUG_ON(!dev->hw_ops.configure);
205 BUG_ON(!dev->hw_ops.set_buftype);
206 BUG_ON(!dev->hw_ops.get_buftype);
207 BUG_ON(!dev->hw_ops.enum_pix);
208 BUG_ON(!dev->hw_ops.set_frame_format);
209 BUG_ON(!dev->hw_ops.get_frame_format);
210 BUG_ON(!dev->hw_ops.get_pixel_format);
211 BUG_ON(!dev->hw_ops.set_pixel_format);
212 BUG_ON(!dev->hw_ops.set_image_window);
213 BUG_ON(!dev->hw_ops.get_image_window);
214 BUG_ON(!dev->hw_ops.get_line_length);
215 BUG_ON(!dev->hw_ops.getfid);
217 mutex_lock(&ccdc_lock);
220 * TODO. Will this ever happen? if so, we need to fix it.
221 * Proabably we need to add the request to a linked list and
222 * walk through it during vpfe probe
224 printk(KERN_ERR "vpfe capture not initialized\n");
229 if (strcmp(dev->name, ccdc_cfg->name)) {
230 /* ignore this ccdc */
236 printk(KERN_ERR "ccdc already registered\n");
243 mutex_unlock(&ccdc_lock);
246 EXPORT_SYMBOL(vpfe_register_ccdc_device);
249 * vpfe_unregister_ccdc_device. CCDC module calls this to
250 * unregister with vpfe capture
252 void vpfe_unregister_ccdc_device(const struct ccdc_hw_device *dev)
255 printk(KERN_ERR "invalid ccdc device ptr\n");
259 printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
262 if (strcmp(dev->name, ccdc_cfg->name)) {
263 /* ignore this ccdc */
267 mutex_lock(&ccdc_lock);
269 mutex_unlock(&ccdc_lock);
271 EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
274 * vpfe_config_ccdc_image_format()
275 * For a pix format, configure ccdc to setup the capture
277 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
279 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
282 if (ccdc_dev->hw_ops.set_pixel_format(
283 vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
284 v4l2_err(&vpfe_dev->v4l2_dev,
285 "couldn't set pix format in ccdc\n");
288 /* configure the image window */
289 ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
291 switch (vpfe_dev->fmt.fmt.pix.field) {
292 case V4L2_FIELD_INTERLACED:
293 /* do nothing, since it is default */
294 ret = ccdc_dev->hw_ops.set_buftype(
295 CCDC_BUFTYPE_FLD_INTERLEAVED);
297 case V4L2_FIELD_NONE:
298 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
299 /* buffer type only applicable for interlaced scan */
301 case V4L2_FIELD_SEQ_TB:
302 ret = ccdc_dev->hw_ops.set_buftype(
303 CCDC_BUFTYPE_FLD_SEPARATED);
309 /* set the frame format */
311 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
315 * vpfe_config_image_format()
316 * For a given standard, this functions sets up the default
317 * pix format & crop values in the vpfe device and ccdc. It first
318 * starts with defaults based values from the standard table.
319 * It then checks if sub device supports get_fmt and then override the
320 * values based on that.Sets crop values to match with scan resolution
321 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
324 static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
327 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
328 struct v4l2_subdev_format fmt = {
329 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
331 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
332 struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
335 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
336 if (vpfe_standards[i].std_id & std_id) {
337 vpfe_dev->std_info.active_pixels =
338 vpfe_standards[i].width;
339 vpfe_dev->std_info.active_lines =
340 vpfe_standards[i].height;
341 vpfe_dev->std_info.frame_format =
342 vpfe_standards[i].frame_format;
343 vpfe_dev->std_index = i;
348 if (i == ARRAY_SIZE(vpfe_standards)) {
349 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
353 vpfe_dev->crop.top = 0;
354 vpfe_dev->crop.left = 0;
355 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
356 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
357 pix->width = vpfe_dev->crop.width;
358 pix->height = vpfe_dev->crop.height;
360 /* first field and frame format based on standard frame format */
361 if (vpfe_dev->std_info.frame_format) {
362 pix->field = V4L2_FIELD_INTERLACED;
363 /* assume V4L2_PIX_FMT_UYVY as default */
364 pix->pixelformat = V4L2_PIX_FMT_UYVY;
365 v4l2_fill_mbus_format(mbus_fmt, pix,
366 MEDIA_BUS_FMT_YUYV10_2X10);
368 pix->field = V4L2_FIELD_NONE;
369 /* assume V4L2_PIX_FMT_SBGGR8 */
370 pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
371 v4l2_fill_mbus_format(mbus_fmt, pix,
372 MEDIA_BUS_FMT_SBGGR8_1X8);
375 /* if sub device supports get_fmt, override the defaults */
376 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
377 sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
379 if (ret && ret != -ENOIOCTLCMD) {
380 v4l2_err(&vpfe_dev->v4l2_dev,
381 "error in getting get_fmt from sub device\n");
384 v4l2_fill_pix_format(pix, mbus_fmt);
385 pix->bytesperline = pix->width * 2;
386 pix->sizeimage = pix->bytesperline * pix->height;
388 /* Sets the values in CCDC */
389 ret = vpfe_config_ccdc_image_format(vpfe_dev);
393 /* Update the values of sizeimage and bytesperline */
394 pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
395 pix->sizeimage = pix->bytesperline * pix->height;
400 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
404 /* set first input of current subdevice as the current input */
405 vpfe_dev->current_input = 0;
407 /* set default standard */
408 vpfe_dev->std_index = 0;
410 /* Configure the default format information */
411 ret = vpfe_config_image_format(vpfe_dev,
412 vpfe_standards[vpfe_dev->std_index].std_id);
416 /* now open the ccdc device to initialize it */
417 mutex_lock(&ccdc_lock);
419 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
424 if (!try_module_get(ccdc_dev->owner)) {
425 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
429 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
431 vpfe_dev->initialized = 1;
433 /* Clear all VPFE/CCDC interrupts */
434 if (vpfe_dev->cfg->clr_intr)
435 vpfe_dev->cfg->clr_intr(-1);
438 mutex_unlock(&ccdc_lock);
443 * vpfe_open : It creates object of file handle structure and
444 * stores it in private_data member of filepointer
446 static int vpfe_open(struct file *file)
448 struct vpfe_device *vpfe_dev = video_drvdata(file);
449 struct video_device *vdev = video_devdata(file);
452 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
454 if (!vpfe_dev->cfg->num_subdevs) {
455 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
459 /* Allocate memory for the file handle object */
460 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
464 /* store pointer to fh in private_data member of file */
465 file->private_data = fh;
466 fh->vpfe_dev = vpfe_dev;
467 v4l2_fh_init(&fh->fh, vdev);
468 mutex_lock(&vpfe_dev->lock);
469 /* If decoder is not initialized. initialize it */
470 if (!vpfe_dev->initialized) {
471 if (vpfe_initialize_device(vpfe_dev)) {
472 mutex_unlock(&vpfe_dev->lock);
473 v4l2_fh_exit(&fh->fh);
478 /* Increment device usrs counter */
480 /* Set io_allowed member to false */
482 v4l2_fh_add(&fh->fh);
483 mutex_unlock(&vpfe_dev->lock);
487 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
491 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
492 struct videobuf_buffer, queue);
493 list_del(&vpfe_dev->next_frm->queue);
494 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
495 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
497 ccdc_dev->hw_ops.setfbaddr(addr);
500 static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
504 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
505 addr += vpfe_dev->field_off;
506 ccdc_dev->hw_ops.setfbaddr(addr);
509 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
511 vpfe_dev->cur_frm->ts = ktime_get_ns();
512 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
513 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
514 wake_up_interruptible(&vpfe_dev->cur_frm->done);
515 vpfe_dev->cur_frm = vpfe_dev->next_frm;
519 static irqreturn_t vpfe_isr(int irq, void *dev_id)
521 struct vpfe_device *vpfe_dev = dev_id;
522 enum v4l2_field field;
525 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
526 field = vpfe_dev->fmt.fmt.pix.field;
528 /* if streaming not started, don't do anything */
529 if (!vpfe_dev->started)
532 /* only for 6446 this will be applicable */
533 if (ccdc_dev->hw_ops.reset)
534 ccdc_dev->hw_ops.reset();
536 if (field == V4L2_FIELD_NONE) {
537 /* handle progressive frame capture */
538 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
539 "frame format is progressive...\n");
540 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
541 vpfe_process_buffer_complete(vpfe_dev);
545 /* interlaced or TB capture check which field we are in hardware */
546 fid = ccdc_dev->hw_ops.getfid();
548 /* switch the software maintained field id */
549 vpfe_dev->field_id ^= 1;
550 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
551 fid, vpfe_dev->field_id);
552 if (fid == vpfe_dev->field_id) {
553 /* we are in-sync here,continue */
556 * One frame is just being captured. If the next frame
557 * is available, release the current frame and move on
559 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
560 vpfe_process_buffer_complete(vpfe_dev);
562 * based on whether the two fields are stored
563 * interleavely or separately in memory, reconfigure
564 * the CCDC memory address
566 if (field == V4L2_FIELD_SEQ_TB)
567 vpfe_schedule_bottom_field(vpfe_dev);
571 * if one field is just being captured configure
572 * the next frame get the next frame from the empty
573 * queue if no frame is available hold on to the
576 spin_lock(&vpfe_dev->dma_queue_lock);
577 if (!list_empty(&vpfe_dev->dma_queue) &&
578 vpfe_dev->cur_frm == vpfe_dev->next_frm)
579 vpfe_schedule_next_buffer(vpfe_dev);
580 spin_unlock(&vpfe_dev->dma_queue_lock);
581 } else if (fid == 0) {
583 * out of sync. Recover from any hardware out-of-sync.
584 * May loose one frame
586 vpfe_dev->field_id = fid;
589 if (vpfe_dev->cfg->clr_intr)
590 vpfe_dev->cfg->clr_intr(irq);
595 /* vdint1_isr - isr handler for VINT1 interrupt */
596 static irqreturn_t vdint1_isr(int irq, void *dev_id)
598 struct vpfe_device *vpfe_dev = dev_id;
600 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
602 /* if streaming not started, don't do anything */
603 if (!vpfe_dev->started) {
604 if (vpfe_dev->cfg->clr_intr)
605 vpfe_dev->cfg->clr_intr(irq);
609 spin_lock(&vpfe_dev->dma_queue_lock);
610 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
611 !list_empty(&vpfe_dev->dma_queue) &&
612 vpfe_dev->cur_frm == vpfe_dev->next_frm)
613 vpfe_schedule_next_buffer(vpfe_dev);
614 spin_unlock(&vpfe_dev->dma_queue_lock);
616 if (vpfe_dev->cfg->clr_intr)
617 vpfe_dev->cfg->clr_intr(irq);
622 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
624 enum ccdc_frmfmt frame_format;
626 frame_format = ccdc_dev->hw_ops.get_frame_format();
627 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
628 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
631 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
633 enum ccdc_frmfmt frame_format;
635 frame_format = ccdc_dev->hw_ops.get_frame_format();
636 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
637 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
644 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
645 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
647 vpfe_dev->started = 0;
648 ccdc_dev->hw_ops.enable(0);
649 if (ccdc_dev->hw_ops.enable_out_to_sdram)
650 ccdc_dev->hw_ops.enable_out_to_sdram(0);
654 * vpfe_release : This function deletes buffer queue, frees the
655 * buffers and the vpfe file handle
657 static int vpfe_release(struct file *file)
659 struct vpfe_device *vpfe_dev = video_drvdata(file);
660 struct vpfe_fh *fh = file->private_data;
661 struct vpfe_subdev_info *sdinfo;
664 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
666 /* Get the device lock */
667 mutex_lock(&vpfe_dev->lock);
668 /* if this instance is doing IO */
669 if (fh->io_allowed) {
670 if (vpfe_dev->started) {
671 sdinfo = vpfe_dev->current_subdev;
672 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
675 if (ret && (ret != -ENOIOCTLCMD))
676 v4l2_err(&vpfe_dev->v4l2_dev,
677 "stream off failed in subdev\n");
678 vpfe_stop_ccdc_capture(vpfe_dev);
679 vpfe_detach_irq(vpfe_dev);
680 videobuf_streamoff(&vpfe_dev->buffer_queue);
682 vpfe_dev->io_usrs = 0;
683 vpfe_dev->numbuffers = config_params.numbuffers;
684 videobuf_stop(&vpfe_dev->buffer_queue);
685 videobuf_mmap_free(&vpfe_dev->buffer_queue);
688 /* Decrement device usrs counter */
690 v4l2_fh_del(&fh->fh);
691 v4l2_fh_exit(&fh->fh);
692 /* If this is the last file handle */
693 if (!vpfe_dev->usrs) {
694 vpfe_dev->initialized = 0;
695 if (ccdc_dev->hw_ops.close)
696 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
697 module_put(ccdc_dev->owner);
699 mutex_unlock(&vpfe_dev->lock);
700 file->private_data = NULL;
701 /* Free memory allocated to file handle object */
707 * vpfe_mmap : It is used to map kernel space buffers
710 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
712 /* Get the device object and file handle object */
713 struct vpfe_device *vpfe_dev = video_drvdata(file);
715 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
717 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
721 * vpfe_poll: It is used for select/poll system call
723 static __poll_t vpfe_poll(struct file *file, poll_table *wait)
725 struct vpfe_device *vpfe_dev = video_drvdata(file);
727 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
729 if (vpfe_dev->started)
730 return videobuf_poll_stream(file,
731 &vpfe_dev->buffer_queue, wait);
735 /* vpfe capture driver file operations */
736 static const struct v4l2_file_operations vpfe_fops = {
737 .owner = THIS_MODULE,
739 .release = vpfe_release,
740 .unlocked_ioctl = video_ioctl2,
746 * vpfe_check_format()
747 * This function adjust the input pixel format as per hardware
748 * capabilities and update the same in pixfmt.
749 * Following algorithm used :-
751 * If given pixformat is not in the vpfe list of pix formats or not
752 * supported by the hardware, current value of pixformat in the device
754 * If given field is not supported, then current field is used. If field
755 * is different from current, then it is matched with that from sub device.
756 * Minimum height is 2 lines for interlaced or tb field and 1 line for
757 * progressive. Maximum height is clamped to active active lines of scan
758 * Minimum width is 32 bytes in memory and width is clamped to active
760 * bytesperline is a multiple of 32.
762 static const struct vpfe_pixel_format *
763 vpfe_check_format(struct vpfe_device *vpfe_dev,
764 struct v4l2_pix_format *pixfmt)
766 u32 min_height = 1, min_width = 32, max_width, max_height;
767 const struct vpfe_pixel_format *vpfe_pix_fmt;
771 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
774 * use current pixel format in the vpfe device. We
775 * will find this pix format in the table
777 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
778 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
781 /* check if hw supports it */
784 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
785 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
793 /* use current pixel format */
794 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
796 * Since this is currently used in the vpfe device, we
797 * will find this pix format in the table
799 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
802 /* check what field format is supported */
803 if (pixfmt->field == V4L2_FIELD_ANY) {
804 /* if field is any, use current value as default */
805 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
809 * if field is not same as current field in the vpfe device
810 * try matching the field with the sub device field
812 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
814 * If field value is not in the supported fields, use current
815 * field used in the device as default
817 switch (pixfmt->field) {
818 case V4L2_FIELD_INTERLACED:
819 case V4L2_FIELD_SEQ_TB:
820 /* if sub device is supporting progressive, use that */
821 if (!vpfe_dev->std_info.frame_format)
822 pixfmt->field = V4L2_FIELD_NONE;
824 case V4L2_FIELD_NONE:
825 if (vpfe_dev->std_info.frame_format)
826 pixfmt->field = V4L2_FIELD_INTERLACED;
830 /* use current field as default */
831 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
836 /* Now adjust image resolutions supported */
837 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
838 pixfmt->field == V4L2_FIELD_SEQ_TB)
841 max_width = vpfe_dev->std_info.active_pixels;
842 max_height = vpfe_dev->std_info.active_lines;
843 min_width /= vpfe_pix_fmt->bpp;
845 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
846 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
848 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
849 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
851 /* If interlaced, adjust height to be a multiple of 2 */
852 if (pixfmt->field == V4L2_FIELD_INTERLACED)
853 pixfmt->height &= (~1);
855 * recalculate bytesperline and sizeimage since width
856 * and height might have changed
858 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
860 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
862 pixfmt->bytesperline * pixfmt->height +
863 ((pixfmt->bytesperline * pixfmt->height) >> 1);
865 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
867 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
868 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
869 pixfmt->bytesperline, pixfmt->sizeimage);
873 static int vpfe_querycap(struct file *file, void *priv,
874 struct v4l2_capability *cap)
876 struct vpfe_device *vpfe_dev = video_drvdata(file);
878 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
880 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
881 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
882 strscpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
883 strscpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
884 strscpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
888 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
889 struct v4l2_format *fmt)
891 struct vpfe_device *vpfe_dev = video_drvdata(file);
893 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
894 /* Fill in the information about format */
895 *fmt = vpfe_dev->fmt;
899 static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
900 struct v4l2_fmtdesc *fmt)
902 struct vpfe_device *vpfe_dev = video_drvdata(file);
903 const struct vpfe_pixel_format *pix_fmt;
907 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
909 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
912 /* Fill in the information about format */
913 pix_fmt = vpfe_lookup_pix_format(pix);
915 temp_index = fmt->index;
916 *fmt = pix_fmt->fmtdesc;
917 fmt->index = temp_index;
923 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
924 struct v4l2_format *fmt)
926 struct vpfe_device *vpfe_dev = video_drvdata(file);
927 const struct vpfe_pixel_format *pix_fmts;
930 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
932 /* If streaming is started, return error */
933 if (vpfe_dev->started) {
934 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
938 /* Check for valid frame format */
939 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
943 /* store the pixel format in the device object */
944 ret = mutex_lock_interruptible(&vpfe_dev->lock);
948 /* First detach any IRQ if currently attached */
949 vpfe_detach_irq(vpfe_dev);
950 vpfe_dev->fmt = *fmt;
951 /* set image capture parameters in the ccdc */
952 ret = vpfe_config_ccdc_image_format(vpfe_dev);
953 mutex_unlock(&vpfe_dev->lock);
957 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
958 struct v4l2_format *f)
960 struct vpfe_device *vpfe_dev = video_drvdata(file);
961 const struct vpfe_pixel_format *pix_fmts;
963 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
965 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
972 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
973 * given app input index
975 static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
977 int *subdev_input_index,
980 struct vpfe_config *cfg = vpfe_dev->cfg;
981 struct vpfe_subdev_info *sdinfo;
984 for (i = 0; i < cfg->num_subdevs; i++) {
985 sdinfo = &cfg->sub_devs[i];
986 if (app_input_index < (j + sdinfo->num_inputs)) {
988 *subdev_input_index = app_input_index - j;
991 j += sdinfo->num_inputs;
997 * vpfe_get_app_input - Get app input index for a given subdev input index
998 * driver stores the input index of the current sub device and translate it
999 * when application request the current input
1001 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1002 int *app_input_index)
1004 struct vpfe_config *cfg = vpfe_dev->cfg;
1005 struct vpfe_subdev_info *sdinfo;
1008 for (i = 0; i < cfg->num_subdevs; i++) {
1009 sdinfo = &cfg->sub_devs[i];
1010 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1011 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1013 *app_input_index = j + vpfe_dev->current_input;
1016 j += sdinfo->num_inputs;
1021 static int vpfe_enum_input(struct file *file, void *priv,
1022 struct v4l2_input *inp)
1024 struct vpfe_device *vpfe_dev = video_drvdata(file);
1025 struct vpfe_subdev_info *sdinfo;
1028 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1030 if (vpfe_get_subdev_input_index(vpfe_dev,
1034 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
1037 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1038 *inp = sdinfo->inputs[index];
1042 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1044 struct vpfe_device *vpfe_dev = video_drvdata(file);
1046 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1048 return vpfe_get_app_input_index(vpfe_dev, index);
1052 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1054 struct vpfe_device *vpfe_dev = video_drvdata(file);
1055 struct v4l2_subdev *sd;
1056 struct vpfe_subdev_info *sdinfo;
1057 int subdev_index, inp_index;
1058 struct vpfe_route *route;
1062 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1064 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1069 * If streaming is started return device busy
1072 if (vpfe_dev->started) {
1073 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1077 ret = vpfe_get_subdev_input_index(vpfe_dev,
1082 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1086 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1087 sd = vpfe_dev->sd[subdev_index];
1088 route = &sdinfo->routes[inp_index];
1089 if (route && sdinfo->can_route) {
1090 input = route->input;
1091 output = route->output;
1098 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1101 v4l2_err(&vpfe_dev->v4l2_dev,
1102 "vpfe_doioctl:error in setting input in decoder\n");
1106 vpfe_dev->current_subdev = sdinfo;
1108 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
1109 vpfe_dev->current_input = index;
1110 vpfe_dev->std_index = 0;
1112 /* set the bus/interface parameter for the sub device in ccdc */
1113 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1117 /* set the default image parameters in the device */
1118 ret = vpfe_config_image_format(vpfe_dev,
1119 vpfe_standards[vpfe_dev->std_index].std_id);
1121 mutex_unlock(&vpfe_dev->lock);
1125 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1127 struct vpfe_device *vpfe_dev = video_drvdata(file);
1128 struct vpfe_subdev_info *sdinfo;
1131 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1133 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1134 sdinfo = vpfe_dev->current_subdev;
1137 /* Call querystd function of decoder device */
1138 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1139 video, querystd, std_id);
1140 mutex_unlock(&vpfe_dev->lock);
1144 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1146 struct vpfe_device *vpfe_dev = video_drvdata(file);
1147 struct vpfe_subdev_info *sdinfo;
1150 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1152 /* Call decoder driver function to set the standard */
1153 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1157 sdinfo = vpfe_dev->current_subdev;
1158 /* If streaming is started, return device busy error */
1159 if (vpfe_dev->started) {
1160 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1165 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1166 video, s_std, std_id);
1168 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1171 ret = vpfe_config_image_format(vpfe_dev, std_id);
1174 mutex_unlock(&vpfe_dev->lock);
1178 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1180 struct vpfe_device *vpfe_dev = video_drvdata(file);
1182 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1184 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1188 * Videobuf operations
1190 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1191 unsigned int *count,
1194 struct vpfe_fh *fh = vq->priv_data;
1195 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1197 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1198 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1199 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1200 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1201 *size = config_params.device_bufsize;
1203 if (*count < config_params.min_numbuffers)
1204 *count = config_params.min_numbuffers;
1205 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1206 "count=%d, size=%d\n", *count, *size);
1210 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1211 struct videobuf_buffer *vb,
1212 enum v4l2_field field)
1214 struct vpfe_fh *fh = vq->priv_data;
1215 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1219 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1221 /* If buffer is not initialized, initialize it */
1222 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1223 vb->width = vpfe_dev->fmt.fmt.pix.width;
1224 vb->height = vpfe_dev->fmt.fmt.pix.height;
1225 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1228 ret = videobuf_iolock(vq, vb, NULL);
1232 addr = videobuf_to_dma_contig(vb);
1233 /* Make sure user addresses are aligned to 32 bytes */
1234 if (!ALIGN(addr, 32))
1237 vb->state = VIDEOBUF_PREPARED;
1242 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1243 struct videobuf_buffer *vb)
1245 /* Get the file handle object and device object */
1246 struct vpfe_fh *fh = vq->priv_data;
1247 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1248 unsigned long flags;
1250 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1252 /* add the buffer to the DMA queue */
1253 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1254 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1255 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1257 /* Change state of the buffer */
1258 vb->state = VIDEOBUF_QUEUED;
1261 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1262 struct videobuf_buffer *vb)
1264 struct vpfe_fh *fh = vq->priv_data;
1265 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1266 unsigned long flags;
1268 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1271 * We need to flush the buffer from the dma queue since
1272 * they are de-allocated
1274 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1275 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1276 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1277 videobuf_dma_contig_free(vq, vb);
1278 vb->state = VIDEOBUF_NEEDS_INIT;
1281 static const struct videobuf_queue_ops vpfe_videobuf_qops = {
1282 .buf_setup = vpfe_videobuf_setup,
1283 .buf_prepare = vpfe_videobuf_prepare,
1284 .buf_queue = vpfe_videobuf_queue,
1285 .buf_release = vpfe_videobuf_release,
1289 * vpfe_reqbufs. currently support REQBUF only once opening
1292 static int vpfe_reqbufs(struct file *file, void *priv,
1293 struct v4l2_requestbuffers *req_buf)
1295 struct vpfe_device *vpfe_dev = video_drvdata(file);
1296 struct vpfe_fh *fh = file->private_data;
1299 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1301 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1302 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1306 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1310 if (vpfe_dev->io_usrs != 0) {
1311 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1316 vpfe_dev->memory = req_buf->memory;
1317 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1318 &vpfe_videobuf_qops,
1322 vpfe_dev->fmt.fmt.pix.field,
1323 sizeof(struct videobuf_buffer),
1327 vpfe_dev->io_usrs = 1;
1328 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1329 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1331 mutex_unlock(&vpfe_dev->lock);
1335 static int vpfe_querybuf(struct file *file, void *priv,
1336 struct v4l2_buffer *buf)
1338 struct vpfe_device *vpfe_dev = video_drvdata(file);
1340 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1342 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1343 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1347 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1348 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1351 /* Call videobuf_querybuf to get information */
1352 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1355 static int vpfe_qbuf(struct file *file, void *priv,
1356 struct v4l2_buffer *p)
1358 struct vpfe_device *vpfe_dev = video_drvdata(file);
1359 struct vpfe_fh *fh = file->private_data;
1361 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1363 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1364 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1369 * If this file handle is not allowed to do IO,
1372 if (!fh->io_allowed) {
1373 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1376 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1379 static int vpfe_dqbuf(struct file *file, void *priv,
1380 struct v4l2_buffer *buf)
1382 struct vpfe_device *vpfe_dev = video_drvdata(file);
1384 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1386 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1387 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1390 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1391 buf, file->f_flags & O_NONBLOCK);
1395 * vpfe_calculate_offsets : This function calculates buffers offset
1396 * for top and bottom field
1398 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1400 struct v4l2_rect image_win;
1402 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1404 ccdc_dev->hw_ops.get_image_window(&image_win);
1405 vpfe_dev->field_off = image_win.height * image_win.width;
1408 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1409 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1411 ccdc_dev->hw_ops.enable(1);
1412 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1413 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1414 vpfe_dev->started = 1;
1418 * vpfe_streamon. Assume the DMA queue is not empty.
1419 * application is expected to call QBUF before calling
1420 * this ioctl. If not, driver returns error
1422 static int vpfe_streamon(struct file *file, void *priv,
1423 enum v4l2_buf_type buf_type)
1425 struct vpfe_device *vpfe_dev = video_drvdata(file);
1426 struct vpfe_fh *fh = file->private_data;
1427 struct vpfe_subdev_info *sdinfo;
1431 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1433 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1434 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1438 /* If file handle is not allowed IO, return error */
1439 if (!fh->io_allowed) {
1440 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1444 sdinfo = vpfe_dev->current_subdev;
1445 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1446 video, s_stream, 1);
1448 if (ret && (ret != -ENOIOCTLCMD)) {
1449 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1453 /* If buffer queue is empty, return error */
1454 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1455 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1459 /* Call videobuf_streamon to start streaming * in videobuf */
1460 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1465 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1468 /* Get the next frame from the buffer queue */
1469 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1470 struct videobuf_buffer, queue);
1471 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1472 /* Remove buffer from the buffer queue */
1473 list_del(&vpfe_dev->cur_frm->queue);
1474 /* Mark state of the current frame to active */
1475 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1476 /* Initialize field_id and started member */
1477 vpfe_dev->field_id = 0;
1478 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1480 /* Calculate field offset */
1481 vpfe_calculate_offsets(vpfe_dev);
1483 if (vpfe_attach_irq(vpfe_dev) < 0) {
1484 v4l2_err(&vpfe_dev->v4l2_dev,
1485 "Error in attaching interrupt handle\n");
1489 if (ccdc_dev->hw_ops.configure() < 0) {
1490 v4l2_err(&vpfe_dev->v4l2_dev,
1491 "Error in configuring ccdc\n");
1495 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1496 vpfe_start_ccdc_capture(vpfe_dev);
1497 mutex_unlock(&vpfe_dev->lock);
1500 mutex_unlock(&vpfe_dev->lock);
1502 videobuf_streamoff(&vpfe_dev->buffer_queue);
1506 static int vpfe_streamoff(struct file *file, void *priv,
1507 enum v4l2_buf_type buf_type)
1509 struct vpfe_device *vpfe_dev = video_drvdata(file);
1510 struct vpfe_fh *fh = file->private_data;
1511 struct vpfe_subdev_info *sdinfo;
1514 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1516 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1517 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1521 /* If io is allowed for this file handle, return error */
1522 if (!fh->io_allowed) {
1523 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1527 /* If streaming is not started, return error */
1528 if (!vpfe_dev->started) {
1529 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1533 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1537 vpfe_stop_ccdc_capture(vpfe_dev);
1538 vpfe_detach_irq(vpfe_dev);
1540 sdinfo = vpfe_dev->current_subdev;
1541 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1542 video, s_stream, 0);
1544 if (ret && (ret != -ENOIOCTLCMD))
1545 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1546 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1547 mutex_unlock(&vpfe_dev->lock);
1551 static int vpfe_g_pixelaspect(struct file *file, void *priv,
1552 int type, struct v4l2_fract *f)
1554 struct vpfe_device *vpfe_dev = video_drvdata(file);
1556 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_pixelaspect\n");
1558 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1560 /* If std_index is invalid, then just return (== 1:1 aspect) */
1561 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1564 *f = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1568 static int vpfe_g_selection(struct file *file, void *priv,
1569 struct v4l2_selection *sel)
1571 struct vpfe_device *vpfe_dev = video_drvdata(file);
1573 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
1575 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1578 switch (sel->target) {
1579 case V4L2_SEL_TGT_CROP:
1580 sel->r = vpfe_dev->crop;
1582 case V4L2_SEL_TGT_CROP_DEFAULT:
1583 case V4L2_SEL_TGT_CROP_BOUNDS:
1584 sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1585 sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1593 static int vpfe_s_selection(struct file *file, void *priv,
1594 struct v4l2_selection *sel)
1596 struct vpfe_device *vpfe_dev = video_drvdata(file);
1597 struct v4l2_rect rect = sel->r;
1600 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1602 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1603 sel->target != V4L2_SEL_TGT_CROP)
1606 if (vpfe_dev->started) {
1607 /* make sure streaming is not started */
1608 v4l2_err(&vpfe_dev->v4l2_dev,
1609 "Cannot change crop when streaming is ON\n");
1613 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1617 if (rect.top < 0 || rect.left < 0) {
1618 v4l2_err(&vpfe_dev->v4l2_dev,
1619 "doesn't support negative values for top & left\n");
1624 /* adjust the width to 16 pixel boundary */
1625 rect.width = ((rect.width + 15) & ~0xf);
1627 /* make sure parameters are valid */
1628 if ((rect.left + rect.width >
1629 vpfe_dev->std_info.active_pixels) ||
1630 (rect.top + rect.height >
1631 vpfe_dev->std_info.active_lines)) {
1632 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
1636 ccdc_dev->hw_ops.set_image_window(&rect);
1637 vpfe_dev->fmt.fmt.pix.width = rect.width;
1638 vpfe_dev->fmt.fmt.pix.height = rect.height;
1639 vpfe_dev->fmt.fmt.pix.bytesperline =
1640 ccdc_dev->hw_ops.get_line_length();
1641 vpfe_dev->fmt.fmt.pix.sizeimage =
1642 vpfe_dev->fmt.fmt.pix.bytesperline *
1643 vpfe_dev->fmt.fmt.pix.height;
1644 vpfe_dev->crop = rect;
1647 mutex_unlock(&vpfe_dev->lock);
1651 /* vpfe capture ioctl operations */
1652 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1653 .vidioc_querycap = vpfe_querycap,
1654 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1655 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1656 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1657 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1658 .vidioc_enum_input = vpfe_enum_input,
1659 .vidioc_g_input = vpfe_g_input,
1660 .vidioc_s_input = vpfe_s_input,
1661 .vidioc_querystd = vpfe_querystd,
1662 .vidioc_s_std = vpfe_s_std,
1663 .vidioc_g_std = vpfe_g_std,
1664 .vidioc_reqbufs = vpfe_reqbufs,
1665 .vidioc_querybuf = vpfe_querybuf,
1666 .vidioc_qbuf = vpfe_qbuf,
1667 .vidioc_dqbuf = vpfe_dqbuf,
1668 .vidioc_streamon = vpfe_streamon,
1669 .vidioc_streamoff = vpfe_streamoff,
1670 .vidioc_g_pixelaspect = vpfe_g_pixelaspect,
1671 .vidioc_g_selection = vpfe_g_selection,
1672 .vidioc_s_selection = vpfe_s_selection,
1675 static struct vpfe_device *vpfe_initialize(void)
1677 struct vpfe_device *vpfe_dev;
1679 /* Default number of buffers should be 3 */
1680 if ((numbuffers > 0) &&
1681 (numbuffers < config_params.min_numbuffers))
1682 numbuffers = config_params.min_numbuffers;
1685 * Set buffer size to min buffers size if invalid buffer size is
1688 if (bufsize < config_params.min_bufsize)
1689 bufsize = config_params.min_bufsize;
1691 config_params.numbuffers = numbuffers;
1694 config_params.device_bufsize = bufsize;
1696 /* Allocate memory for device objects */
1697 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1703 * vpfe_probe : This function creates device entries by register
1704 * itself to the V4L2 driver and initializes fields of each
1707 static int vpfe_probe(struct platform_device *pdev)
1709 struct vpfe_subdev_info *sdinfo;
1710 struct vpfe_config *vpfe_cfg;
1711 struct resource *res1;
1712 struct vpfe_device *vpfe_dev;
1713 struct i2c_adapter *i2c_adap;
1714 struct video_device *vfd;
1716 int num_subdevs = 0;
1718 /* Get the pointer to the device object */
1719 vpfe_dev = vpfe_initialize();
1722 v4l2_err(pdev->dev.driver,
1723 "Failed to allocate memory for vpfe_dev\n");
1727 vpfe_dev->pdev = &pdev->dev;
1729 if (!pdev->dev.platform_data) {
1730 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1732 goto probe_free_dev_mem;
1735 vpfe_cfg = pdev->dev.platform_data;
1736 vpfe_dev->cfg = vpfe_cfg;
1737 if (!vpfe_cfg->ccdc || !vpfe_cfg->card_name || !vpfe_cfg->sub_devs) {
1738 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1740 goto probe_free_dev_mem;
1743 /* Allocate memory for ccdc configuration */
1744 ccdc_cfg = kmalloc(sizeof(*ccdc_cfg), GFP_KERNEL);
1747 goto probe_free_dev_mem;
1750 mutex_lock(&ccdc_lock);
1752 strscpy(ccdc_cfg->name, vpfe_cfg->ccdc, sizeof(ccdc_cfg->name));
1753 /* Get VINT0 irq resource */
1754 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1756 v4l2_err(pdev->dev.driver,
1757 "Unable to get interrupt for VINT0\n");
1759 goto probe_free_ccdc_cfg_mem;
1761 vpfe_dev->ccdc_irq0 = res1->start;
1763 /* Get VINT1 irq resource */
1764 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1766 v4l2_err(pdev->dev.driver,
1767 "Unable to get interrupt for VINT1\n");
1769 goto probe_free_ccdc_cfg_mem;
1771 vpfe_dev->ccdc_irq1 = res1->start;
1773 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
1774 "vpfe_capture0", vpfe_dev);
1777 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1778 goto probe_free_ccdc_cfg_mem;
1781 vfd = &vpfe_dev->video_dev;
1782 /* Initialize field of video device */
1783 vfd->release = video_device_release_empty;
1784 vfd->fops = &vpfe_fops;
1785 vfd->ioctl_ops = &vpfe_ioctl_ops;
1787 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1788 snprintf(vfd->name, sizeof(vfd->name),
1791 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1792 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1793 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1795 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1797 v4l2_err(pdev->dev.driver,
1798 "Unable to register v4l2 device.\n");
1799 goto probe_out_release_irq;
1801 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1802 spin_lock_init(&vpfe_dev->irqlock);
1803 spin_lock_init(&vpfe_dev->dma_queue_lock);
1804 mutex_init(&vpfe_dev->lock);
1806 /* Initialize field of the device objects */
1807 vpfe_dev->numbuffers = config_params.numbuffers;
1809 /* register video device */
1810 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1811 "trying to register vpfe device.\n");
1812 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1813 "video_dev=%p\n", &vpfe_dev->video_dev);
1814 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1815 ret = video_register_device(&vpfe_dev->video_dev,
1816 VFL_TYPE_GRABBER, -1);
1819 v4l2_err(pdev->dev.driver,
1820 "Unable to register video device.\n");
1821 goto probe_out_v4l2_unregister;
1824 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1825 /* set the driver data in platform device */
1826 platform_set_drvdata(pdev, vpfe_dev);
1827 /* set driver private data */
1828 video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
1829 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1830 num_subdevs = vpfe_cfg->num_subdevs;
1831 vpfe_dev->sd = kmalloc_array(num_subdevs,
1832 sizeof(*vpfe_dev->sd),
1834 if (!vpfe_dev->sd) {
1836 goto probe_out_video_unregister;
1839 for (i = 0; i < num_subdevs; i++) {
1840 struct v4l2_input *inps;
1842 sdinfo = &vpfe_cfg->sub_devs[i];
1844 /* Load up the subdevice */
1846 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1848 &sdinfo->board_info,
1850 if (vpfe_dev->sd[i]) {
1851 v4l2_info(&vpfe_dev->v4l2_dev,
1852 "v4l2 sub device %s registered\n",
1854 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1855 /* update tvnorms from the sub devices */
1856 for (j = 0; j < sdinfo->num_inputs; j++) {
1857 inps = &sdinfo->inputs[j];
1858 vfd->tvnorms |= inps->std;
1861 v4l2_info(&vpfe_dev->v4l2_dev,
1862 "v4l2 sub device %s register fails\n",
1869 /* set first sub device as current one */
1870 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1871 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
1873 /* We have at least one sub device to work with */
1874 mutex_unlock(&ccdc_lock);
1878 kfree(vpfe_dev->sd);
1879 probe_out_video_unregister:
1880 video_unregister_device(&vpfe_dev->video_dev);
1881 probe_out_v4l2_unregister:
1882 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1883 probe_out_release_irq:
1884 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1885 probe_free_ccdc_cfg_mem:
1887 mutex_unlock(&ccdc_lock);
1894 * vpfe_remove : It un-register device from V4L2 driver
1896 static int vpfe_remove(struct platform_device *pdev)
1898 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
1900 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
1902 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1903 kfree(vpfe_dev->sd);
1904 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1905 video_unregister_device(&vpfe_dev->video_dev);
1911 static int vpfe_suspend(struct device *dev)
1916 static int vpfe_resume(struct device *dev)
1921 static const struct dev_pm_ops vpfe_dev_pm_ops = {
1922 .suspend = vpfe_suspend,
1923 .resume = vpfe_resume,
1926 static struct platform_driver vpfe_driver = {
1928 .name = CAPTURE_DRV_NAME,
1929 .pm = &vpfe_dev_pm_ops,
1931 .probe = vpfe_probe,
1932 .remove = vpfe_remove,
1935 module_platform_driver(vpfe_driver);