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[] = {
122 .pixelformat = V4L2_PIX_FMT_SBGGR8,
126 .pixelformat = V4L2_PIX_FMT_SBGGR16,
130 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
134 .pixelformat = V4L2_PIX_FMT_UYVY,
138 .pixelformat = V4L2_PIX_FMT_YUYV,
142 .pixelformat = V4L2_PIX_FMT_NV12,
148 * vpfe_lookup_pix_format()
149 * lookup an entry in the vpfe pix format table based on pix_format
151 static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
155 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
156 if (pix_format == vpfe_pix_fmts[i].pixelformat)
157 return &vpfe_pix_fmts[i];
163 * vpfe_register_ccdc_device. CCDC module calls this to
164 * register with vpfe capture
166 int vpfe_register_ccdc_device(const struct ccdc_hw_device *dev)
169 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
171 BUG_ON(!dev->hw_ops.open);
172 BUG_ON(!dev->hw_ops.enable);
173 BUG_ON(!dev->hw_ops.set_hw_if_params);
174 BUG_ON(!dev->hw_ops.configure);
175 BUG_ON(!dev->hw_ops.set_buftype);
176 BUG_ON(!dev->hw_ops.get_buftype);
177 BUG_ON(!dev->hw_ops.enum_pix);
178 BUG_ON(!dev->hw_ops.set_frame_format);
179 BUG_ON(!dev->hw_ops.get_frame_format);
180 BUG_ON(!dev->hw_ops.get_pixel_format);
181 BUG_ON(!dev->hw_ops.set_pixel_format);
182 BUG_ON(!dev->hw_ops.set_image_window);
183 BUG_ON(!dev->hw_ops.get_image_window);
184 BUG_ON(!dev->hw_ops.get_line_length);
185 BUG_ON(!dev->hw_ops.getfid);
187 mutex_lock(&ccdc_lock);
190 * TODO. Will this ever happen? if so, we need to fix it.
191 * Proabably we need to add the request to a linked list and
192 * walk through it during vpfe probe
194 printk(KERN_ERR "vpfe capture not initialized\n");
199 if (strcmp(dev->name, ccdc_cfg->name)) {
200 /* ignore this ccdc */
206 printk(KERN_ERR "ccdc already registered\n");
213 mutex_unlock(&ccdc_lock);
216 EXPORT_SYMBOL(vpfe_register_ccdc_device);
219 * vpfe_unregister_ccdc_device. CCDC module calls this to
220 * unregister with vpfe capture
222 void vpfe_unregister_ccdc_device(const struct ccdc_hw_device *dev)
225 printk(KERN_ERR "invalid ccdc device ptr\n");
229 printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
232 if (strcmp(dev->name, ccdc_cfg->name)) {
233 /* ignore this ccdc */
237 mutex_lock(&ccdc_lock);
239 mutex_unlock(&ccdc_lock);
241 EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
244 * vpfe_config_ccdc_image_format()
245 * For a pix format, configure ccdc to setup the capture
247 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
249 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
252 if (ccdc_dev->hw_ops.set_pixel_format(
253 vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
254 v4l2_err(&vpfe_dev->v4l2_dev,
255 "couldn't set pix format in ccdc\n");
258 /* configure the image window */
259 ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
261 switch (vpfe_dev->fmt.fmt.pix.field) {
262 case V4L2_FIELD_INTERLACED:
263 /* do nothing, since it is default */
264 ret = ccdc_dev->hw_ops.set_buftype(
265 CCDC_BUFTYPE_FLD_INTERLEAVED);
267 case V4L2_FIELD_NONE:
268 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
269 /* buffer type only applicable for interlaced scan */
271 case V4L2_FIELD_SEQ_TB:
272 ret = ccdc_dev->hw_ops.set_buftype(
273 CCDC_BUFTYPE_FLD_SEPARATED);
279 /* set the frame format */
281 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
285 * vpfe_config_image_format()
286 * For a given standard, this functions sets up the default
287 * pix format & crop values in the vpfe device and ccdc. It first
288 * starts with defaults based values from the standard table.
289 * It then checks if sub device supports get_fmt and then override the
290 * values based on that.Sets crop values to match with scan resolution
291 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
294 static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
297 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
298 struct v4l2_subdev_format fmt = {
299 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
301 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
302 struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
305 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
306 if (vpfe_standards[i].std_id & std_id) {
307 vpfe_dev->std_info.active_pixels =
308 vpfe_standards[i].width;
309 vpfe_dev->std_info.active_lines =
310 vpfe_standards[i].height;
311 vpfe_dev->std_info.frame_format =
312 vpfe_standards[i].frame_format;
313 vpfe_dev->std_index = i;
318 if (i == ARRAY_SIZE(vpfe_standards)) {
319 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
323 vpfe_dev->crop.top = 0;
324 vpfe_dev->crop.left = 0;
325 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
326 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
327 pix->width = vpfe_dev->crop.width;
328 pix->height = vpfe_dev->crop.height;
330 /* first field and frame format based on standard frame format */
331 if (vpfe_dev->std_info.frame_format) {
332 pix->field = V4L2_FIELD_INTERLACED;
333 /* assume V4L2_PIX_FMT_UYVY as default */
334 pix->pixelformat = V4L2_PIX_FMT_UYVY;
335 v4l2_fill_mbus_format(mbus_fmt, pix,
336 MEDIA_BUS_FMT_YUYV10_2X10);
338 pix->field = V4L2_FIELD_NONE;
339 /* assume V4L2_PIX_FMT_SBGGR8 */
340 pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
341 v4l2_fill_mbus_format(mbus_fmt, pix,
342 MEDIA_BUS_FMT_SBGGR8_1X8);
345 /* if sub device supports get_fmt, override the defaults */
346 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
347 sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
349 if (ret && ret != -ENOIOCTLCMD) {
350 v4l2_err(&vpfe_dev->v4l2_dev,
351 "error in getting get_fmt from sub device\n");
354 v4l2_fill_pix_format(pix, mbus_fmt);
355 pix->bytesperline = pix->width * 2;
356 pix->sizeimage = pix->bytesperline * pix->height;
358 /* Sets the values in CCDC */
359 ret = vpfe_config_ccdc_image_format(vpfe_dev);
363 /* Update the values of sizeimage and bytesperline */
364 pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
365 pix->sizeimage = pix->bytesperline * pix->height;
370 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
374 /* set first input of current subdevice as the current input */
375 vpfe_dev->current_input = 0;
377 /* set default standard */
378 vpfe_dev->std_index = 0;
380 /* Configure the default format information */
381 ret = vpfe_config_image_format(vpfe_dev,
382 vpfe_standards[vpfe_dev->std_index].std_id);
386 /* now open the ccdc device to initialize it */
387 mutex_lock(&ccdc_lock);
389 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
394 if (!try_module_get(ccdc_dev->owner)) {
395 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
399 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
401 vpfe_dev->initialized = 1;
403 /* Clear all VPFE/CCDC interrupts */
404 if (vpfe_dev->cfg->clr_intr)
405 vpfe_dev->cfg->clr_intr(-1);
408 mutex_unlock(&ccdc_lock);
413 * vpfe_open : It creates object of file handle structure and
414 * stores it in private_data member of filepointer
416 static int vpfe_open(struct file *file)
418 struct vpfe_device *vpfe_dev = video_drvdata(file);
419 struct video_device *vdev = video_devdata(file);
422 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
424 if (!vpfe_dev->cfg->num_subdevs) {
425 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
429 /* Allocate memory for the file handle object */
430 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
434 /* store pointer to fh in private_data member of file */
435 file->private_data = fh;
436 fh->vpfe_dev = vpfe_dev;
437 v4l2_fh_init(&fh->fh, vdev);
438 mutex_lock(&vpfe_dev->lock);
439 /* If decoder is not initialized. initialize it */
440 if (!vpfe_dev->initialized) {
441 if (vpfe_initialize_device(vpfe_dev)) {
442 mutex_unlock(&vpfe_dev->lock);
443 v4l2_fh_exit(&fh->fh);
448 /* Increment device usrs counter */
450 /* Set io_allowed member to false */
452 v4l2_fh_add(&fh->fh);
453 mutex_unlock(&vpfe_dev->lock);
457 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
461 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
462 struct videobuf_buffer, queue);
463 list_del(&vpfe_dev->next_frm->queue);
464 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
465 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
467 ccdc_dev->hw_ops.setfbaddr(addr);
470 static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
474 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
475 addr += vpfe_dev->field_off;
476 ccdc_dev->hw_ops.setfbaddr(addr);
479 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
481 vpfe_dev->cur_frm->ts = ktime_get_ns();
482 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
483 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
484 wake_up_interruptible(&vpfe_dev->cur_frm->done);
485 vpfe_dev->cur_frm = vpfe_dev->next_frm;
489 static irqreturn_t vpfe_isr(int irq, void *dev_id)
491 struct vpfe_device *vpfe_dev = dev_id;
492 enum v4l2_field field;
495 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
496 field = vpfe_dev->fmt.fmt.pix.field;
498 /* if streaming not started, don't do anything */
499 if (!vpfe_dev->started)
502 /* only for 6446 this will be applicable */
503 if (ccdc_dev->hw_ops.reset)
504 ccdc_dev->hw_ops.reset();
506 if (field == V4L2_FIELD_NONE) {
507 /* handle progressive frame capture */
508 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
509 "frame format is progressive...\n");
510 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
511 vpfe_process_buffer_complete(vpfe_dev);
515 /* interlaced or TB capture check which field we are in hardware */
516 fid = ccdc_dev->hw_ops.getfid();
518 /* switch the software maintained field id */
519 vpfe_dev->field_id ^= 1;
520 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
521 fid, vpfe_dev->field_id);
522 if (fid == vpfe_dev->field_id) {
523 /* we are in-sync here,continue */
526 * One frame is just being captured. If the next frame
527 * is available, release the current frame and move on
529 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
530 vpfe_process_buffer_complete(vpfe_dev);
532 * based on whether the two fields are stored
533 * interleavely or separately in memory, reconfigure
534 * the CCDC memory address
536 if (field == V4L2_FIELD_SEQ_TB)
537 vpfe_schedule_bottom_field(vpfe_dev);
541 * if one field is just being captured configure
542 * the next frame get the next frame from the empty
543 * queue if no frame is available hold on to the
546 spin_lock(&vpfe_dev->dma_queue_lock);
547 if (!list_empty(&vpfe_dev->dma_queue) &&
548 vpfe_dev->cur_frm == vpfe_dev->next_frm)
549 vpfe_schedule_next_buffer(vpfe_dev);
550 spin_unlock(&vpfe_dev->dma_queue_lock);
551 } else if (fid == 0) {
553 * out of sync. Recover from any hardware out-of-sync.
554 * May loose one frame
556 vpfe_dev->field_id = fid;
559 if (vpfe_dev->cfg->clr_intr)
560 vpfe_dev->cfg->clr_intr(irq);
565 /* vdint1_isr - isr handler for VINT1 interrupt */
566 static irqreturn_t vdint1_isr(int irq, void *dev_id)
568 struct vpfe_device *vpfe_dev = dev_id;
570 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
572 /* if streaming not started, don't do anything */
573 if (!vpfe_dev->started) {
574 if (vpfe_dev->cfg->clr_intr)
575 vpfe_dev->cfg->clr_intr(irq);
579 spin_lock(&vpfe_dev->dma_queue_lock);
580 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
581 !list_empty(&vpfe_dev->dma_queue) &&
582 vpfe_dev->cur_frm == vpfe_dev->next_frm)
583 vpfe_schedule_next_buffer(vpfe_dev);
584 spin_unlock(&vpfe_dev->dma_queue_lock);
586 if (vpfe_dev->cfg->clr_intr)
587 vpfe_dev->cfg->clr_intr(irq);
592 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
594 enum ccdc_frmfmt frame_format;
596 frame_format = ccdc_dev->hw_ops.get_frame_format();
597 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
598 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
601 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
603 enum ccdc_frmfmt frame_format;
605 frame_format = ccdc_dev->hw_ops.get_frame_format();
606 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
607 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
614 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
615 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
617 vpfe_dev->started = 0;
618 ccdc_dev->hw_ops.enable(0);
619 if (ccdc_dev->hw_ops.enable_out_to_sdram)
620 ccdc_dev->hw_ops.enable_out_to_sdram(0);
624 * vpfe_release : This function deletes buffer queue, frees the
625 * buffers and the vpfe file handle
627 static int vpfe_release(struct file *file)
629 struct vpfe_device *vpfe_dev = video_drvdata(file);
630 struct vpfe_fh *fh = file->private_data;
631 struct vpfe_subdev_info *sdinfo;
634 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
636 /* Get the device lock */
637 mutex_lock(&vpfe_dev->lock);
638 /* if this instance is doing IO */
639 if (fh->io_allowed) {
640 if (vpfe_dev->started) {
641 sdinfo = vpfe_dev->current_subdev;
642 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
645 if (ret && (ret != -ENOIOCTLCMD))
646 v4l2_err(&vpfe_dev->v4l2_dev,
647 "stream off failed in subdev\n");
648 vpfe_stop_ccdc_capture(vpfe_dev);
649 vpfe_detach_irq(vpfe_dev);
650 videobuf_streamoff(&vpfe_dev->buffer_queue);
652 vpfe_dev->io_usrs = 0;
653 vpfe_dev->numbuffers = config_params.numbuffers;
654 videobuf_stop(&vpfe_dev->buffer_queue);
655 videobuf_mmap_free(&vpfe_dev->buffer_queue);
658 /* Decrement device usrs counter */
660 v4l2_fh_del(&fh->fh);
661 v4l2_fh_exit(&fh->fh);
662 /* If this is the last file handle */
663 if (!vpfe_dev->usrs) {
664 vpfe_dev->initialized = 0;
665 if (ccdc_dev->hw_ops.close)
666 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
667 module_put(ccdc_dev->owner);
669 mutex_unlock(&vpfe_dev->lock);
670 file->private_data = NULL;
671 /* Free memory allocated to file handle object */
677 * vpfe_mmap : It is used to map kernel space buffers
680 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
682 /* Get the device object and file handle object */
683 struct vpfe_device *vpfe_dev = video_drvdata(file);
685 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
687 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
691 * vpfe_poll: It is used for select/poll system call
693 static __poll_t vpfe_poll(struct file *file, poll_table *wait)
695 struct vpfe_device *vpfe_dev = video_drvdata(file);
697 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
699 if (vpfe_dev->started)
700 return videobuf_poll_stream(file,
701 &vpfe_dev->buffer_queue, wait);
705 /* vpfe capture driver file operations */
706 static const struct v4l2_file_operations vpfe_fops = {
707 .owner = THIS_MODULE,
709 .release = vpfe_release,
710 .unlocked_ioctl = video_ioctl2,
716 * vpfe_check_format()
717 * This function adjust the input pixel format as per hardware
718 * capabilities and update the same in pixfmt.
719 * Following algorithm used :-
721 * If given pixformat is not in the vpfe list of pix formats or not
722 * supported by the hardware, current value of pixformat in the device
724 * If given field is not supported, then current field is used. If field
725 * is different from current, then it is matched with that from sub device.
726 * Minimum height is 2 lines for interlaced or tb field and 1 line for
727 * progressive. Maximum height is clamped to active active lines of scan
728 * Minimum width is 32 bytes in memory and width is clamped to active
730 * bytesperline is a multiple of 32.
732 static const struct vpfe_pixel_format *
733 vpfe_check_format(struct vpfe_device *vpfe_dev,
734 struct v4l2_pix_format *pixfmt)
736 u32 min_height = 1, min_width = 32, max_width, max_height;
737 const struct vpfe_pixel_format *vpfe_pix_fmt;
741 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
744 * use current pixel format in the vpfe device. We
745 * will find this pix format in the table
747 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
748 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
751 /* check if hw supports it */
754 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
755 if (vpfe_pix_fmt->pixelformat == pix) {
763 /* use current pixel format */
764 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
766 * Since this is currently used in the vpfe device, we
767 * will find this pix format in the table
769 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
772 /* check what field format is supported */
773 if (pixfmt->field == V4L2_FIELD_ANY) {
774 /* if field is any, use current value as default */
775 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
779 * if field is not same as current field in the vpfe device
780 * try matching the field with the sub device field
782 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
784 * If field value is not in the supported fields, use current
785 * field used in the device as default
787 switch (pixfmt->field) {
788 case V4L2_FIELD_INTERLACED:
789 case V4L2_FIELD_SEQ_TB:
790 /* if sub device is supporting progressive, use that */
791 if (!vpfe_dev->std_info.frame_format)
792 pixfmt->field = V4L2_FIELD_NONE;
794 case V4L2_FIELD_NONE:
795 if (vpfe_dev->std_info.frame_format)
796 pixfmt->field = V4L2_FIELD_INTERLACED;
800 /* use current field as default */
801 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
806 /* Now adjust image resolutions supported */
807 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
808 pixfmt->field == V4L2_FIELD_SEQ_TB)
811 max_width = vpfe_dev->std_info.active_pixels;
812 max_height = vpfe_dev->std_info.active_lines;
813 min_width /= vpfe_pix_fmt->bpp;
815 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
816 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
818 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
819 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
821 /* If interlaced, adjust height to be a multiple of 2 */
822 if (pixfmt->field == V4L2_FIELD_INTERLACED)
823 pixfmt->height &= (~1);
825 * recalculate bytesperline and sizeimage since width
826 * and height might have changed
828 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
830 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
832 pixfmt->bytesperline * pixfmt->height +
833 ((pixfmt->bytesperline * pixfmt->height) >> 1);
835 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
837 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
838 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
839 pixfmt->bytesperline, pixfmt->sizeimage);
843 static int vpfe_querycap(struct file *file, void *priv,
844 struct v4l2_capability *cap)
846 struct vpfe_device *vpfe_dev = video_drvdata(file);
848 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
850 strscpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
851 strscpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
852 strscpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
856 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
857 struct v4l2_format *fmt)
859 struct vpfe_device *vpfe_dev = video_drvdata(file);
861 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
862 /* Fill in the information about format */
863 *fmt = vpfe_dev->fmt;
867 static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
868 struct v4l2_fmtdesc *fmt)
870 struct vpfe_device *vpfe_dev = video_drvdata(file);
871 const struct vpfe_pixel_format *pix_fmt;
874 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
876 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
879 /* Fill in the information about format */
880 pix_fmt = vpfe_lookup_pix_format(pix);
882 fmt->pixelformat = fmt->pixelformat;
888 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
889 struct v4l2_format *fmt)
891 struct vpfe_device *vpfe_dev = video_drvdata(file);
892 const struct vpfe_pixel_format *pix_fmts;
895 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
897 /* If streaming is started, return error */
898 if (vpfe_dev->started) {
899 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
903 /* Check for valid frame format */
904 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
908 /* store the pixel format in the device object */
909 ret = mutex_lock_interruptible(&vpfe_dev->lock);
913 /* First detach any IRQ if currently attached */
914 vpfe_detach_irq(vpfe_dev);
915 vpfe_dev->fmt = *fmt;
916 /* set image capture parameters in the ccdc */
917 ret = vpfe_config_ccdc_image_format(vpfe_dev);
918 mutex_unlock(&vpfe_dev->lock);
922 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
923 struct v4l2_format *f)
925 struct vpfe_device *vpfe_dev = video_drvdata(file);
926 const struct vpfe_pixel_format *pix_fmts;
928 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
930 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
937 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
938 * given app input index
940 static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
942 int *subdev_input_index,
945 struct vpfe_config *cfg = vpfe_dev->cfg;
946 struct vpfe_subdev_info *sdinfo;
949 for (i = 0; i < cfg->num_subdevs; i++) {
950 sdinfo = &cfg->sub_devs[i];
951 if (app_input_index < (j + sdinfo->num_inputs)) {
953 *subdev_input_index = app_input_index - j;
956 j += sdinfo->num_inputs;
962 * vpfe_get_app_input - Get app input index for a given subdev input index
963 * driver stores the input index of the current sub device and translate it
964 * when application request the current input
966 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
967 int *app_input_index)
969 struct vpfe_config *cfg = vpfe_dev->cfg;
970 struct vpfe_subdev_info *sdinfo;
973 for (i = 0; i < cfg->num_subdevs; i++) {
974 sdinfo = &cfg->sub_devs[i];
975 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
976 if (vpfe_dev->current_input >= sdinfo->num_inputs)
978 *app_input_index = j + vpfe_dev->current_input;
981 j += sdinfo->num_inputs;
986 static int vpfe_enum_input(struct file *file, void *priv,
987 struct v4l2_input *inp)
989 struct vpfe_device *vpfe_dev = video_drvdata(file);
990 struct vpfe_subdev_info *sdinfo;
993 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
995 if (vpfe_get_subdev_input_index(vpfe_dev,
999 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
1002 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1003 *inp = sdinfo->inputs[index];
1007 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1009 struct vpfe_device *vpfe_dev = video_drvdata(file);
1011 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1013 return vpfe_get_app_input_index(vpfe_dev, index);
1017 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1019 struct vpfe_device *vpfe_dev = video_drvdata(file);
1020 struct v4l2_subdev *sd;
1021 struct vpfe_subdev_info *sdinfo;
1022 int subdev_index, inp_index;
1023 struct vpfe_route *route;
1027 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1029 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1034 * If streaming is started return device busy
1037 if (vpfe_dev->started) {
1038 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1042 ret = vpfe_get_subdev_input_index(vpfe_dev,
1047 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1051 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1052 sd = vpfe_dev->sd[subdev_index];
1053 route = &sdinfo->routes[inp_index];
1054 if (route && sdinfo->can_route) {
1055 input = route->input;
1056 output = route->output;
1063 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1066 v4l2_err(&vpfe_dev->v4l2_dev,
1067 "vpfe_doioctl:error in setting input in decoder\n");
1071 vpfe_dev->current_subdev = sdinfo;
1073 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
1074 vpfe_dev->current_input = index;
1075 vpfe_dev->std_index = 0;
1077 /* set the bus/interface parameter for the sub device in ccdc */
1078 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1082 /* set the default image parameters in the device */
1083 ret = vpfe_config_image_format(vpfe_dev,
1084 vpfe_standards[vpfe_dev->std_index].std_id);
1086 mutex_unlock(&vpfe_dev->lock);
1090 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1092 struct vpfe_device *vpfe_dev = video_drvdata(file);
1093 struct vpfe_subdev_info *sdinfo;
1096 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1098 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1099 sdinfo = vpfe_dev->current_subdev;
1102 /* Call querystd function of decoder device */
1103 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1104 video, querystd, std_id);
1105 mutex_unlock(&vpfe_dev->lock);
1109 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1111 struct vpfe_device *vpfe_dev = video_drvdata(file);
1112 struct vpfe_subdev_info *sdinfo;
1115 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1117 /* Call decoder driver function to set the standard */
1118 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1122 sdinfo = vpfe_dev->current_subdev;
1123 /* If streaming is started, return device busy error */
1124 if (vpfe_dev->started) {
1125 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1130 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1131 video, s_std, std_id);
1133 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1136 ret = vpfe_config_image_format(vpfe_dev, std_id);
1139 mutex_unlock(&vpfe_dev->lock);
1143 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1145 struct vpfe_device *vpfe_dev = video_drvdata(file);
1147 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1149 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1153 * Videobuf operations
1155 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1156 unsigned int *count,
1159 struct vpfe_fh *fh = vq->priv_data;
1160 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1162 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1163 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1164 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1165 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1166 *size = config_params.device_bufsize;
1168 if (*count < config_params.min_numbuffers)
1169 *count = config_params.min_numbuffers;
1170 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1171 "count=%d, size=%d\n", *count, *size);
1175 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1176 struct videobuf_buffer *vb,
1177 enum v4l2_field field)
1179 struct vpfe_fh *fh = vq->priv_data;
1180 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1184 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1186 /* If buffer is not initialized, initialize it */
1187 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1188 vb->width = vpfe_dev->fmt.fmt.pix.width;
1189 vb->height = vpfe_dev->fmt.fmt.pix.height;
1190 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1193 ret = videobuf_iolock(vq, vb, NULL);
1197 addr = videobuf_to_dma_contig(vb);
1198 /* Make sure user addresses are aligned to 32 bytes */
1199 if (!ALIGN(addr, 32))
1202 vb->state = VIDEOBUF_PREPARED;
1207 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1208 struct videobuf_buffer *vb)
1210 /* Get the file handle object and device object */
1211 struct vpfe_fh *fh = vq->priv_data;
1212 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1213 unsigned long flags;
1215 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1217 /* add the buffer to the DMA queue */
1218 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1219 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1220 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1222 /* Change state of the buffer */
1223 vb->state = VIDEOBUF_QUEUED;
1226 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1227 struct videobuf_buffer *vb)
1229 struct vpfe_fh *fh = vq->priv_data;
1230 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1231 unsigned long flags;
1233 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1236 * We need to flush the buffer from the dma queue since
1237 * they are de-allocated
1239 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1240 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1241 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1242 videobuf_dma_contig_free(vq, vb);
1243 vb->state = VIDEOBUF_NEEDS_INIT;
1246 static const struct videobuf_queue_ops vpfe_videobuf_qops = {
1247 .buf_setup = vpfe_videobuf_setup,
1248 .buf_prepare = vpfe_videobuf_prepare,
1249 .buf_queue = vpfe_videobuf_queue,
1250 .buf_release = vpfe_videobuf_release,
1254 * vpfe_reqbufs. currently support REQBUF only once opening
1257 static int vpfe_reqbufs(struct file *file, void *priv,
1258 struct v4l2_requestbuffers *req_buf)
1260 struct vpfe_device *vpfe_dev = video_drvdata(file);
1261 struct vpfe_fh *fh = file->private_data;
1264 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1266 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1267 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1271 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1275 if (vpfe_dev->io_usrs != 0) {
1276 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1281 vpfe_dev->memory = req_buf->memory;
1282 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1283 &vpfe_videobuf_qops,
1287 vpfe_dev->fmt.fmt.pix.field,
1288 sizeof(struct videobuf_buffer),
1292 vpfe_dev->io_usrs = 1;
1293 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1294 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1296 mutex_unlock(&vpfe_dev->lock);
1300 static int vpfe_querybuf(struct file *file, void *priv,
1301 struct v4l2_buffer *buf)
1303 struct vpfe_device *vpfe_dev = video_drvdata(file);
1305 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1307 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1308 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1312 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1313 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1316 /* Call videobuf_querybuf to get information */
1317 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1320 static int vpfe_qbuf(struct file *file, void *priv,
1321 struct v4l2_buffer *p)
1323 struct vpfe_device *vpfe_dev = video_drvdata(file);
1324 struct vpfe_fh *fh = file->private_data;
1326 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1328 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1329 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1334 * If this file handle is not allowed to do IO,
1337 if (!fh->io_allowed) {
1338 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1341 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1344 static int vpfe_dqbuf(struct file *file, void *priv,
1345 struct v4l2_buffer *buf)
1347 struct vpfe_device *vpfe_dev = video_drvdata(file);
1349 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1351 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1352 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1355 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1356 buf, file->f_flags & O_NONBLOCK);
1360 * vpfe_calculate_offsets : This function calculates buffers offset
1361 * for top and bottom field
1363 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1365 struct v4l2_rect image_win;
1367 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1369 ccdc_dev->hw_ops.get_image_window(&image_win);
1370 vpfe_dev->field_off = image_win.height * image_win.width;
1373 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1374 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1376 ccdc_dev->hw_ops.enable(1);
1377 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1378 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1379 vpfe_dev->started = 1;
1383 * vpfe_streamon. Assume the DMA queue is not empty.
1384 * application is expected to call QBUF before calling
1385 * this ioctl. If not, driver returns error
1387 static int vpfe_streamon(struct file *file, void *priv,
1388 enum v4l2_buf_type buf_type)
1390 struct vpfe_device *vpfe_dev = video_drvdata(file);
1391 struct vpfe_fh *fh = file->private_data;
1392 struct vpfe_subdev_info *sdinfo;
1396 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1398 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1399 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1403 /* If file handle is not allowed IO, return error */
1404 if (!fh->io_allowed) {
1405 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1409 sdinfo = vpfe_dev->current_subdev;
1410 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1411 video, s_stream, 1);
1413 if (ret && (ret != -ENOIOCTLCMD)) {
1414 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1418 /* If buffer queue is empty, return error */
1419 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1420 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1424 /* Call videobuf_streamon to start streaming * in videobuf */
1425 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1430 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1433 /* Get the next frame from the buffer queue */
1434 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1435 struct videobuf_buffer, queue);
1436 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1437 /* Remove buffer from the buffer queue */
1438 list_del(&vpfe_dev->cur_frm->queue);
1439 /* Mark state of the current frame to active */
1440 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1441 /* Initialize field_id and started member */
1442 vpfe_dev->field_id = 0;
1443 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1445 /* Calculate field offset */
1446 vpfe_calculate_offsets(vpfe_dev);
1448 if (vpfe_attach_irq(vpfe_dev) < 0) {
1449 v4l2_err(&vpfe_dev->v4l2_dev,
1450 "Error in attaching interrupt handle\n");
1454 if (ccdc_dev->hw_ops.configure() < 0) {
1455 v4l2_err(&vpfe_dev->v4l2_dev,
1456 "Error in configuring ccdc\n");
1460 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1461 vpfe_start_ccdc_capture(vpfe_dev);
1462 mutex_unlock(&vpfe_dev->lock);
1465 mutex_unlock(&vpfe_dev->lock);
1467 videobuf_streamoff(&vpfe_dev->buffer_queue);
1471 static int vpfe_streamoff(struct file *file, void *priv,
1472 enum v4l2_buf_type buf_type)
1474 struct vpfe_device *vpfe_dev = video_drvdata(file);
1475 struct vpfe_fh *fh = file->private_data;
1476 struct vpfe_subdev_info *sdinfo;
1479 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1481 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1482 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1486 /* If io is allowed for this file handle, return error */
1487 if (!fh->io_allowed) {
1488 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1492 /* If streaming is not started, return error */
1493 if (!vpfe_dev->started) {
1494 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1498 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1502 vpfe_stop_ccdc_capture(vpfe_dev);
1503 vpfe_detach_irq(vpfe_dev);
1505 sdinfo = vpfe_dev->current_subdev;
1506 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1507 video, s_stream, 0);
1509 if (ret && (ret != -ENOIOCTLCMD))
1510 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1511 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1512 mutex_unlock(&vpfe_dev->lock);
1516 static int vpfe_g_pixelaspect(struct file *file, void *priv,
1517 int type, struct v4l2_fract *f)
1519 struct vpfe_device *vpfe_dev = video_drvdata(file);
1521 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_pixelaspect\n");
1523 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1525 /* If std_index is invalid, then just return (== 1:1 aspect) */
1526 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1529 *f = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1533 static int vpfe_g_selection(struct file *file, void *priv,
1534 struct v4l2_selection *sel)
1536 struct vpfe_device *vpfe_dev = video_drvdata(file);
1538 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
1540 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1543 switch (sel->target) {
1544 case V4L2_SEL_TGT_CROP:
1545 sel->r = vpfe_dev->crop;
1547 case V4L2_SEL_TGT_CROP_DEFAULT:
1548 case V4L2_SEL_TGT_CROP_BOUNDS:
1549 sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1550 sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1558 static int vpfe_s_selection(struct file *file, void *priv,
1559 struct v4l2_selection *sel)
1561 struct vpfe_device *vpfe_dev = video_drvdata(file);
1562 struct v4l2_rect rect = sel->r;
1565 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1567 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1568 sel->target != V4L2_SEL_TGT_CROP)
1571 if (vpfe_dev->started) {
1572 /* make sure streaming is not started */
1573 v4l2_err(&vpfe_dev->v4l2_dev,
1574 "Cannot change crop when streaming is ON\n");
1578 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1582 if (rect.top < 0 || rect.left < 0) {
1583 v4l2_err(&vpfe_dev->v4l2_dev,
1584 "doesn't support negative values for top & left\n");
1589 /* adjust the width to 16 pixel boundary */
1590 rect.width = ((rect.width + 15) & ~0xf);
1592 /* make sure parameters are valid */
1593 if ((rect.left + rect.width >
1594 vpfe_dev->std_info.active_pixels) ||
1595 (rect.top + rect.height >
1596 vpfe_dev->std_info.active_lines)) {
1597 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
1601 ccdc_dev->hw_ops.set_image_window(&rect);
1602 vpfe_dev->fmt.fmt.pix.width = rect.width;
1603 vpfe_dev->fmt.fmt.pix.height = rect.height;
1604 vpfe_dev->fmt.fmt.pix.bytesperline =
1605 ccdc_dev->hw_ops.get_line_length();
1606 vpfe_dev->fmt.fmt.pix.sizeimage =
1607 vpfe_dev->fmt.fmt.pix.bytesperline *
1608 vpfe_dev->fmt.fmt.pix.height;
1609 vpfe_dev->crop = rect;
1612 mutex_unlock(&vpfe_dev->lock);
1616 /* vpfe capture ioctl operations */
1617 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1618 .vidioc_querycap = vpfe_querycap,
1619 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1620 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1621 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1622 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1623 .vidioc_enum_input = vpfe_enum_input,
1624 .vidioc_g_input = vpfe_g_input,
1625 .vidioc_s_input = vpfe_s_input,
1626 .vidioc_querystd = vpfe_querystd,
1627 .vidioc_s_std = vpfe_s_std,
1628 .vidioc_g_std = vpfe_g_std,
1629 .vidioc_reqbufs = vpfe_reqbufs,
1630 .vidioc_querybuf = vpfe_querybuf,
1631 .vidioc_qbuf = vpfe_qbuf,
1632 .vidioc_dqbuf = vpfe_dqbuf,
1633 .vidioc_streamon = vpfe_streamon,
1634 .vidioc_streamoff = vpfe_streamoff,
1635 .vidioc_g_pixelaspect = vpfe_g_pixelaspect,
1636 .vidioc_g_selection = vpfe_g_selection,
1637 .vidioc_s_selection = vpfe_s_selection,
1640 static struct vpfe_device *vpfe_initialize(void)
1642 struct vpfe_device *vpfe_dev;
1644 /* Default number of buffers should be 3 */
1645 if ((numbuffers > 0) &&
1646 (numbuffers < config_params.min_numbuffers))
1647 numbuffers = config_params.min_numbuffers;
1650 * Set buffer size to min buffers size if invalid buffer size is
1653 if (bufsize < config_params.min_bufsize)
1654 bufsize = config_params.min_bufsize;
1656 config_params.numbuffers = numbuffers;
1659 config_params.device_bufsize = bufsize;
1661 /* Allocate memory for device objects */
1662 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1668 * vpfe_probe : This function creates device entries by register
1669 * itself to the V4L2 driver and initializes fields of each
1672 static int vpfe_probe(struct platform_device *pdev)
1674 struct vpfe_subdev_info *sdinfo;
1675 struct vpfe_config *vpfe_cfg;
1676 struct resource *res1;
1677 struct vpfe_device *vpfe_dev;
1678 struct i2c_adapter *i2c_adap;
1679 struct video_device *vfd;
1681 int num_subdevs = 0;
1683 /* Get the pointer to the device object */
1684 vpfe_dev = vpfe_initialize();
1687 v4l2_err(pdev->dev.driver,
1688 "Failed to allocate memory for vpfe_dev\n");
1692 vpfe_dev->pdev = &pdev->dev;
1694 if (!pdev->dev.platform_data) {
1695 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1697 goto probe_free_dev_mem;
1700 vpfe_cfg = pdev->dev.platform_data;
1701 vpfe_dev->cfg = vpfe_cfg;
1702 if (!vpfe_cfg->ccdc || !vpfe_cfg->card_name || !vpfe_cfg->sub_devs) {
1703 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1705 goto probe_free_dev_mem;
1708 /* Allocate memory for ccdc configuration */
1709 ccdc_cfg = kmalloc(sizeof(*ccdc_cfg), GFP_KERNEL);
1712 goto probe_free_dev_mem;
1715 mutex_lock(&ccdc_lock);
1717 strscpy(ccdc_cfg->name, vpfe_cfg->ccdc, sizeof(ccdc_cfg->name));
1718 /* Get VINT0 irq resource */
1719 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1721 v4l2_err(pdev->dev.driver,
1722 "Unable to get interrupt for VINT0\n");
1724 goto probe_free_ccdc_cfg_mem;
1726 vpfe_dev->ccdc_irq0 = res1->start;
1728 /* Get VINT1 irq resource */
1729 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1731 v4l2_err(pdev->dev.driver,
1732 "Unable to get interrupt for VINT1\n");
1734 goto probe_free_ccdc_cfg_mem;
1736 vpfe_dev->ccdc_irq1 = res1->start;
1738 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
1739 "vpfe_capture0", vpfe_dev);
1742 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1743 goto probe_free_ccdc_cfg_mem;
1746 vfd = &vpfe_dev->video_dev;
1747 /* Initialize field of video device */
1748 vfd->release = video_device_release_empty;
1749 vfd->fops = &vpfe_fops;
1750 vfd->ioctl_ops = &vpfe_ioctl_ops;
1752 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1753 vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1754 snprintf(vfd->name, sizeof(vfd->name),
1757 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1758 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1759 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1761 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1763 v4l2_err(pdev->dev.driver,
1764 "Unable to register v4l2 device.\n");
1765 goto probe_out_release_irq;
1767 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1768 spin_lock_init(&vpfe_dev->irqlock);
1769 spin_lock_init(&vpfe_dev->dma_queue_lock);
1770 mutex_init(&vpfe_dev->lock);
1772 /* Initialize field of the device objects */
1773 vpfe_dev->numbuffers = config_params.numbuffers;
1775 /* register video device */
1776 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1777 "trying to register vpfe device.\n");
1778 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1779 "video_dev=%p\n", &vpfe_dev->video_dev);
1780 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1781 ret = video_register_device(&vpfe_dev->video_dev,
1782 VFL_TYPE_GRABBER, -1);
1785 v4l2_err(pdev->dev.driver,
1786 "Unable to register video device.\n");
1787 goto probe_out_v4l2_unregister;
1790 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1791 /* set the driver data in platform device */
1792 platform_set_drvdata(pdev, vpfe_dev);
1793 /* set driver private data */
1794 video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
1795 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1796 num_subdevs = vpfe_cfg->num_subdevs;
1797 vpfe_dev->sd = kmalloc_array(num_subdevs,
1798 sizeof(*vpfe_dev->sd),
1800 if (!vpfe_dev->sd) {
1802 goto probe_out_video_unregister;
1805 for (i = 0; i < num_subdevs; i++) {
1806 struct v4l2_input *inps;
1808 sdinfo = &vpfe_cfg->sub_devs[i];
1810 /* Load up the subdevice */
1812 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1814 &sdinfo->board_info,
1816 if (vpfe_dev->sd[i]) {
1817 v4l2_info(&vpfe_dev->v4l2_dev,
1818 "v4l2 sub device %s registered\n",
1820 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1821 /* update tvnorms from the sub devices */
1822 for (j = 0; j < sdinfo->num_inputs; j++) {
1823 inps = &sdinfo->inputs[j];
1824 vfd->tvnorms |= inps->std;
1827 v4l2_info(&vpfe_dev->v4l2_dev,
1828 "v4l2 sub device %s register fails\n",
1835 /* set first sub device as current one */
1836 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1837 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
1839 /* We have at least one sub device to work with */
1840 mutex_unlock(&ccdc_lock);
1844 kfree(vpfe_dev->sd);
1845 probe_out_video_unregister:
1846 video_unregister_device(&vpfe_dev->video_dev);
1847 probe_out_v4l2_unregister:
1848 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1849 probe_out_release_irq:
1850 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1851 probe_free_ccdc_cfg_mem:
1853 mutex_unlock(&ccdc_lock);
1860 * vpfe_remove : It un-register device from V4L2 driver
1862 static int vpfe_remove(struct platform_device *pdev)
1864 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
1866 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
1868 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1869 kfree(vpfe_dev->sd);
1870 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1871 video_unregister_device(&vpfe_dev->video_dev);
1877 static int vpfe_suspend(struct device *dev)
1882 static int vpfe_resume(struct device *dev)
1887 static const struct dev_pm_ops vpfe_dev_pm_ops = {
1888 .suspend = vpfe_suspend,
1889 .resume = vpfe_resume,
1892 static struct platform_driver vpfe_driver = {
1894 .name = CAPTURE_DRV_NAME,
1895 .pm = &vpfe_dev_pm_ops,
1897 .probe = vpfe_probe,
1898 .remove = vpfe_remove,
1901 module_platform_driver(vpfe_driver);