2 * Copyright (C) 2008-2009 Texas Instruments Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * Driver name : VPFE Capture driver
15 * VPFE Capture driver allows applications to capture and stream video
16 * frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
17 * TVP5146 or Raw Bayer RGB image data from an image sensor
18 * such as Microns' MT9T001, MT9T031 etc.
20 * These SoCs have, in common, a Video Processing Subsystem (VPSS) that
21 * consists of a Video Processing Front End (VPFE) for capturing
22 * video/raw image data and Video Processing Back End (VPBE) for displaying
23 * YUV data through an in-built analog encoder or Digital LCD port. This
24 * driver is for capture through VPFE. A typical EVM using these SoCs have
25 * following high level configuration.
28 * decoder(TVP5146/ YUV/
29 * MT9T001) --> Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
38 * The data flow happens from a decoder connected to the VPFE over a
39 * YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
40 * and to the input of VPFE through an optional MUX (if more inputs are
41 * to be interfaced on the EVM). The input data is first passed through
42 * CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
43 * does very little or no processing on YUV data and does pre-process Raw
44 * Bayer RGB data through modules such as Defect Pixel Correction (DFC)
45 * Color Space Conversion (CSC), data gain/offset etc. After this, data
46 * can be written to SDRAM or can be connected to the image processing
47 * block such as IPIPE (on DM355 only).
51 * - Capture using TVP5146 over BT.656
52 * - support for interfacing decoders using sub device model
53 * - Work with DM355 or DM6446 CCDC to do Raw Bayer RGB/YUV
54 * data capture to SDRAM.
56 * - Support multiple REQBUF after open
57 * - Support for de-allocating buffers through REQBUF
58 * - Support for Raw Bayer RGB capture
59 * - Support for chaining Image Processor
60 * - Support for static allocation of buffers
61 * - Support for USERPTR IO
62 * - Support for STREAMON before QBUF
63 * - Support for control ioctls
65 #include <linux/module.h>
66 #include <linux/slab.h>
67 #include <linux/init.h>
68 #include <linux/platform_device.h>
69 #include <linux/interrupt.h>
70 #include <media/v4l2-common.h>
72 #include <media/davinci/vpfe_capture.h>
73 #include "ccdc_hw_device.h"
76 static u32 numbuffers
= 3;
77 static u32 bufsize
= (720 * 576 * 2);
79 module_param(numbuffers
, uint
, S_IRUGO
);
80 module_param(bufsize
, uint
, S_IRUGO
);
81 module_param(debug
, int, 0644);
83 MODULE_PARM_DESC(numbuffers
, "buffer count (default:3)");
84 MODULE_PARM_DESC(bufsize
, "buffer size in bytes (default:720 x 576 x 2)");
85 MODULE_PARM_DESC(debug
, "Debug level 0-1");
87 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_AUTHOR("Texas Instruments");
91 /* standard information */
92 struct vpfe_standard
{
96 struct v4l2_fract pixelaspect
;
97 /* 0 - progressive, 1 - interlaced */
101 /* ccdc configuration */
103 /* This make sure vpfe is probed and ready to go */
105 /* name of ccdc device */
109 /* data structures */
110 static struct vpfe_config_params config_params
= {
113 .min_bufsize
= 720 * 480 * 2,
114 .device_bufsize
= 720 * 576 * 2,
117 /* ccdc device registered */
118 static const struct ccdc_hw_device
*ccdc_dev
;
119 /* lock for accessing ccdc information */
120 static DEFINE_MUTEX(ccdc_lock
);
121 /* ccdc configuration */
122 static struct ccdc_config
*ccdc_cfg
;
124 static const struct vpfe_standard vpfe_standards
[] = {
125 {V4L2_STD_525_60
, 720, 480, {11, 10}, 1},
126 {V4L2_STD_625_50
, 720, 576, {54, 59}, 1},
129 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
130 static const struct vpfe_pixel_format vpfe_pix_fmts
[] = {
134 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
135 .description
= "Bayer GrRBGb 8bit A-Law compr.",
136 .pixelformat
= V4L2_PIX_FMT_SBGGR8
,
143 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
144 .description
= "Bayer GrRBGb - 16bit",
145 .pixelformat
= V4L2_PIX_FMT_SBGGR16
,
152 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
153 .description
= "Bayer GrRBGb 8bit DPCM compr.",
154 .pixelformat
= V4L2_PIX_FMT_SGRBG10DPCM8
,
161 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
162 .description
= "YCbCr 4:2:2 Interleaved UYVY",
163 .pixelformat
= V4L2_PIX_FMT_UYVY
,
170 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
171 .description
= "YCbCr 4:2:2 Interleaved YUYV",
172 .pixelformat
= V4L2_PIX_FMT_YUYV
,
179 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
180 .description
= "Y/CbCr 4:2:0 - Semi planar",
181 .pixelformat
= V4L2_PIX_FMT_NV12
,
188 * vpfe_lookup_pix_format()
189 * lookup an entry in the vpfe pix format table based on pix_format
191 static const struct vpfe_pixel_format
*vpfe_lookup_pix_format(u32 pix_format
)
195 for (i
= 0; i
< ARRAY_SIZE(vpfe_pix_fmts
); i
++) {
196 if (pix_format
== vpfe_pix_fmts
[i
].fmtdesc
.pixelformat
)
197 return &vpfe_pix_fmts
[i
];
203 * vpfe_register_ccdc_device. CCDC module calls this to
204 * register with vpfe capture
206 int vpfe_register_ccdc_device(const struct ccdc_hw_device
*dev
)
209 printk(KERN_NOTICE
"vpfe_register_ccdc_device: %s\n", dev
->name
);
211 BUG_ON(!dev
->hw_ops
.open
);
212 BUG_ON(!dev
->hw_ops
.enable
);
213 BUG_ON(!dev
->hw_ops
.set_hw_if_params
);
214 BUG_ON(!dev
->hw_ops
.configure
);
215 BUG_ON(!dev
->hw_ops
.set_buftype
);
216 BUG_ON(!dev
->hw_ops
.get_buftype
);
217 BUG_ON(!dev
->hw_ops
.enum_pix
);
218 BUG_ON(!dev
->hw_ops
.set_frame_format
);
219 BUG_ON(!dev
->hw_ops
.get_frame_format
);
220 BUG_ON(!dev
->hw_ops
.get_pixel_format
);
221 BUG_ON(!dev
->hw_ops
.set_pixel_format
);
222 BUG_ON(!dev
->hw_ops
.set_image_window
);
223 BUG_ON(!dev
->hw_ops
.get_image_window
);
224 BUG_ON(!dev
->hw_ops
.get_line_length
);
225 BUG_ON(!dev
->hw_ops
.getfid
);
227 mutex_lock(&ccdc_lock
);
230 * TODO. Will this ever happen? if so, we need to fix it.
231 * Proabably we need to add the request to a linked list and
232 * walk through it during vpfe probe
234 printk(KERN_ERR
"vpfe capture not initialized\n");
239 if (strcmp(dev
->name
, ccdc_cfg
->name
)) {
240 /* ignore this ccdc */
246 printk(KERN_ERR
"ccdc already registered\n");
253 mutex_unlock(&ccdc_lock
);
256 EXPORT_SYMBOL(vpfe_register_ccdc_device
);
259 * vpfe_unregister_ccdc_device. CCDC module calls this to
260 * unregister with vpfe capture
262 void vpfe_unregister_ccdc_device(const struct ccdc_hw_device
*dev
)
265 printk(KERN_ERR
"invalid ccdc device ptr\n");
269 printk(KERN_NOTICE
"vpfe_unregister_ccdc_device, dev->name = %s\n",
272 if (strcmp(dev
->name
, ccdc_cfg
->name
)) {
273 /* ignore this ccdc */
277 mutex_lock(&ccdc_lock
);
279 mutex_unlock(&ccdc_lock
);
281 EXPORT_SYMBOL(vpfe_unregister_ccdc_device
);
284 * vpfe_config_ccdc_image_format()
285 * For a pix format, configure ccdc to setup the capture
287 static int vpfe_config_ccdc_image_format(struct vpfe_device
*vpfe_dev
)
289 enum ccdc_frmfmt frm_fmt
= CCDC_FRMFMT_INTERLACED
;
292 if (ccdc_dev
->hw_ops
.set_pixel_format(
293 vpfe_dev
->fmt
.fmt
.pix
.pixelformat
) < 0) {
294 v4l2_err(&vpfe_dev
->v4l2_dev
,
295 "couldn't set pix format in ccdc\n");
298 /* configure the image window */
299 ccdc_dev
->hw_ops
.set_image_window(&vpfe_dev
->crop
);
301 switch (vpfe_dev
->fmt
.fmt
.pix
.field
) {
302 case V4L2_FIELD_INTERLACED
:
303 /* do nothing, since it is default */
304 ret
= ccdc_dev
->hw_ops
.set_buftype(
305 CCDC_BUFTYPE_FLD_INTERLEAVED
);
307 case V4L2_FIELD_NONE
:
308 frm_fmt
= CCDC_FRMFMT_PROGRESSIVE
;
309 /* buffer type only applicable for interlaced scan */
311 case V4L2_FIELD_SEQ_TB
:
312 ret
= ccdc_dev
->hw_ops
.set_buftype(
313 CCDC_BUFTYPE_FLD_SEPARATED
);
319 /* set the frame format */
321 ret
= ccdc_dev
->hw_ops
.set_frame_format(frm_fmt
);
325 * vpfe_config_image_format()
326 * For a given standard, this functions sets up the default
327 * pix format & crop values in the vpfe device and ccdc. It first
328 * starts with defaults based values from the standard table.
329 * It then checks if sub device supports get_fmt and then override the
330 * values based on that.Sets crop values to match with scan resolution
331 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
334 static int vpfe_config_image_format(struct vpfe_device
*vpfe_dev
,
337 struct vpfe_subdev_info
*sdinfo
= vpfe_dev
->current_subdev
;
338 struct v4l2_subdev_format fmt
= {
339 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
341 struct v4l2_mbus_framefmt
*mbus_fmt
= &fmt
.format
;
342 struct v4l2_pix_format
*pix
= &vpfe_dev
->fmt
.fmt
.pix
;
345 for (i
= 0; i
< ARRAY_SIZE(vpfe_standards
); i
++) {
346 if (vpfe_standards
[i
].std_id
& std_id
) {
347 vpfe_dev
->std_info
.active_pixels
=
348 vpfe_standards
[i
].width
;
349 vpfe_dev
->std_info
.active_lines
=
350 vpfe_standards
[i
].height
;
351 vpfe_dev
->std_info
.frame_format
=
352 vpfe_standards
[i
].frame_format
;
353 vpfe_dev
->std_index
= i
;
358 if (i
== ARRAY_SIZE(vpfe_standards
)) {
359 v4l2_err(&vpfe_dev
->v4l2_dev
, "standard not supported\n");
363 vpfe_dev
->crop
.top
= 0;
364 vpfe_dev
->crop
.left
= 0;
365 vpfe_dev
->crop
.width
= vpfe_dev
->std_info
.active_pixels
;
366 vpfe_dev
->crop
.height
= vpfe_dev
->std_info
.active_lines
;
367 pix
->width
= vpfe_dev
->crop
.width
;
368 pix
->height
= vpfe_dev
->crop
.height
;
370 /* first field and frame format based on standard frame format */
371 if (vpfe_dev
->std_info
.frame_format
) {
372 pix
->field
= V4L2_FIELD_INTERLACED
;
373 /* assume V4L2_PIX_FMT_UYVY as default */
374 pix
->pixelformat
= V4L2_PIX_FMT_UYVY
;
375 v4l2_fill_mbus_format(mbus_fmt
, pix
,
376 MEDIA_BUS_FMT_YUYV10_2X10
);
378 pix
->field
= V4L2_FIELD_NONE
;
379 /* assume V4L2_PIX_FMT_SBGGR8 */
380 pix
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
381 v4l2_fill_mbus_format(mbus_fmt
, pix
,
382 MEDIA_BUS_FMT_SBGGR8_1X8
);
385 /* if sub device supports get_fmt, override the defaults */
386 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
,
387 sdinfo
->grp_id
, pad
, get_fmt
, NULL
, &fmt
);
389 if (ret
&& ret
!= -ENOIOCTLCMD
) {
390 v4l2_err(&vpfe_dev
->v4l2_dev
,
391 "error in getting get_fmt from sub device\n");
394 v4l2_fill_pix_format(pix
, mbus_fmt
);
395 pix
->bytesperline
= pix
->width
* 2;
396 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
398 /* Sets the values in CCDC */
399 ret
= vpfe_config_ccdc_image_format(vpfe_dev
);
403 /* Update the values of sizeimage and bytesperline */
404 pix
->bytesperline
= ccdc_dev
->hw_ops
.get_line_length();
405 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
410 static int vpfe_initialize_device(struct vpfe_device
*vpfe_dev
)
414 /* set first input of current subdevice as the current input */
415 vpfe_dev
->current_input
= 0;
417 /* set default standard */
418 vpfe_dev
->std_index
= 0;
420 /* Configure the default format information */
421 ret
= vpfe_config_image_format(vpfe_dev
,
422 vpfe_standards
[vpfe_dev
->std_index
].std_id
);
426 /* now open the ccdc device to initialize it */
427 mutex_lock(&ccdc_lock
);
429 v4l2_err(&vpfe_dev
->v4l2_dev
, "ccdc device not registered\n");
434 if (!try_module_get(ccdc_dev
->owner
)) {
435 v4l2_err(&vpfe_dev
->v4l2_dev
, "Couldn't lock ccdc module\n");
439 ret
= ccdc_dev
->hw_ops
.open(vpfe_dev
->pdev
);
441 vpfe_dev
->initialized
= 1;
443 /* Clear all VPFE/CCDC interrupts */
444 if (vpfe_dev
->cfg
->clr_intr
)
445 vpfe_dev
->cfg
->clr_intr(-1);
448 mutex_unlock(&ccdc_lock
);
453 * vpfe_open : It creates object of file handle structure and
454 * stores it in private_data member of filepointer
456 static int vpfe_open(struct file
*file
)
458 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
459 struct video_device
*vdev
= video_devdata(file
);
462 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_open\n");
464 if (!vpfe_dev
->cfg
->num_subdevs
) {
465 v4l2_err(&vpfe_dev
->v4l2_dev
, "No decoder registered\n");
469 /* Allocate memory for the file handle object */
470 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
474 /* store pointer to fh in private_data member of file */
475 file
->private_data
= fh
;
476 fh
->vpfe_dev
= vpfe_dev
;
477 v4l2_fh_init(&fh
->fh
, vdev
);
478 mutex_lock(&vpfe_dev
->lock
);
479 /* If decoder is not initialized. initialize it */
480 if (!vpfe_dev
->initialized
) {
481 if (vpfe_initialize_device(vpfe_dev
)) {
482 mutex_unlock(&vpfe_dev
->lock
);
483 v4l2_fh_exit(&fh
->fh
);
488 /* Increment device usrs counter */
490 /* Set io_allowed member to false */
492 v4l2_fh_add(&fh
->fh
);
493 mutex_unlock(&vpfe_dev
->lock
);
497 static void vpfe_schedule_next_buffer(struct vpfe_device
*vpfe_dev
)
501 vpfe_dev
->next_frm
= list_entry(vpfe_dev
->dma_queue
.next
,
502 struct videobuf_buffer
, queue
);
503 list_del(&vpfe_dev
->next_frm
->queue
);
504 vpfe_dev
->next_frm
->state
= VIDEOBUF_ACTIVE
;
505 addr
= videobuf_to_dma_contig(vpfe_dev
->next_frm
);
507 ccdc_dev
->hw_ops
.setfbaddr(addr
);
510 static void vpfe_schedule_bottom_field(struct vpfe_device
*vpfe_dev
)
514 addr
= videobuf_to_dma_contig(vpfe_dev
->cur_frm
);
515 addr
+= vpfe_dev
->field_off
;
516 ccdc_dev
->hw_ops
.setfbaddr(addr
);
519 static void vpfe_process_buffer_complete(struct vpfe_device
*vpfe_dev
)
521 v4l2_get_timestamp(&vpfe_dev
->cur_frm
->ts
);
522 vpfe_dev
->cur_frm
->state
= VIDEOBUF_DONE
;
523 vpfe_dev
->cur_frm
->size
= vpfe_dev
->fmt
.fmt
.pix
.sizeimage
;
524 wake_up_interruptible(&vpfe_dev
->cur_frm
->done
);
525 vpfe_dev
->cur_frm
= vpfe_dev
->next_frm
;
529 static irqreturn_t
vpfe_isr(int irq
, void *dev_id
)
531 struct vpfe_device
*vpfe_dev
= dev_id
;
532 enum v4l2_field field
;
535 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "\nStarting vpfe_isr...\n");
536 field
= vpfe_dev
->fmt
.fmt
.pix
.field
;
538 /* if streaming not started, don't do anything */
539 if (!vpfe_dev
->started
)
542 /* only for 6446 this will be applicable */
543 if (ccdc_dev
->hw_ops
.reset
)
544 ccdc_dev
->hw_ops
.reset();
546 if (field
== V4L2_FIELD_NONE
) {
547 /* handle progressive frame capture */
548 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
549 "frame format is progressive...\n");
550 if (vpfe_dev
->cur_frm
!= vpfe_dev
->next_frm
)
551 vpfe_process_buffer_complete(vpfe_dev
);
555 /* interlaced or TB capture check which field we are in hardware */
556 fid
= ccdc_dev
->hw_ops
.getfid();
558 /* switch the software maintained field id */
559 vpfe_dev
->field_id
^= 1;
560 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "field id = %x:%x.\n",
561 fid
, vpfe_dev
->field_id
);
562 if (fid
== vpfe_dev
->field_id
) {
563 /* we are in-sync here,continue */
566 * One frame is just being captured. If the next frame
567 * is available, release the current frame and move on
569 if (vpfe_dev
->cur_frm
!= vpfe_dev
->next_frm
)
570 vpfe_process_buffer_complete(vpfe_dev
);
572 * based on whether the two fields are stored
573 * interleavely or separately in memory, reconfigure
574 * the CCDC memory address
576 if (field
== V4L2_FIELD_SEQ_TB
)
577 vpfe_schedule_bottom_field(vpfe_dev
);
581 * if one field is just being captured configure
582 * the next frame get the next frame from the empty
583 * queue if no frame is available hold on to the
586 spin_lock(&vpfe_dev
->dma_queue_lock
);
587 if (!list_empty(&vpfe_dev
->dma_queue
) &&
588 vpfe_dev
->cur_frm
== vpfe_dev
->next_frm
)
589 vpfe_schedule_next_buffer(vpfe_dev
);
590 spin_unlock(&vpfe_dev
->dma_queue_lock
);
591 } else if (fid
== 0) {
593 * out of sync. Recover from any hardware out-of-sync.
594 * May loose one frame
596 vpfe_dev
->field_id
= fid
;
599 if (vpfe_dev
->cfg
->clr_intr
)
600 vpfe_dev
->cfg
->clr_intr(irq
);
605 /* vdint1_isr - isr handler for VINT1 interrupt */
606 static irqreturn_t
vdint1_isr(int irq
, void *dev_id
)
608 struct vpfe_device
*vpfe_dev
= dev_id
;
610 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "\nInside vdint1_isr...\n");
612 /* if streaming not started, don't do anything */
613 if (!vpfe_dev
->started
) {
614 if (vpfe_dev
->cfg
->clr_intr
)
615 vpfe_dev
->cfg
->clr_intr(irq
);
619 spin_lock(&vpfe_dev
->dma_queue_lock
);
620 if ((vpfe_dev
->fmt
.fmt
.pix
.field
== V4L2_FIELD_NONE
) &&
621 !list_empty(&vpfe_dev
->dma_queue
) &&
622 vpfe_dev
->cur_frm
== vpfe_dev
->next_frm
)
623 vpfe_schedule_next_buffer(vpfe_dev
);
624 spin_unlock(&vpfe_dev
->dma_queue_lock
);
626 if (vpfe_dev
->cfg
->clr_intr
)
627 vpfe_dev
->cfg
->clr_intr(irq
);
632 static void vpfe_detach_irq(struct vpfe_device
*vpfe_dev
)
634 enum ccdc_frmfmt frame_format
;
636 frame_format
= ccdc_dev
->hw_ops
.get_frame_format();
637 if (frame_format
== CCDC_FRMFMT_PROGRESSIVE
)
638 free_irq(vpfe_dev
->ccdc_irq1
, vpfe_dev
);
641 static int vpfe_attach_irq(struct vpfe_device
*vpfe_dev
)
643 enum ccdc_frmfmt frame_format
;
645 frame_format
= ccdc_dev
->hw_ops
.get_frame_format();
646 if (frame_format
== CCDC_FRMFMT_PROGRESSIVE
) {
647 return request_irq(vpfe_dev
->ccdc_irq1
, vdint1_isr
,
654 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
655 static void vpfe_stop_ccdc_capture(struct vpfe_device
*vpfe_dev
)
657 vpfe_dev
->started
= 0;
658 ccdc_dev
->hw_ops
.enable(0);
659 if (ccdc_dev
->hw_ops
.enable_out_to_sdram
)
660 ccdc_dev
->hw_ops
.enable_out_to_sdram(0);
664 * vpfe_release : This function deletes buffer queue, frees the
665 * buffers and the vpfe file handle
667 static int vpfe_release(struct file
*file
)
669 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
670 struct vpfe_fh
*fh
= file
->private_data
;
671 struct vpfe_subdev_info
*sdinfo
;
674 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_release\n");
676 /* Get the device lock */
677 mutex_lock(&vpfe_dev
->lock
);
678 /* if this instance is doing IO */
679 if (fh
->io_allowed
) {
680 if (vpfe_dev
->started
) {
681 sdinfo
= vpfe_dev
->current_subdev
;
682 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
,
685 if (ret
&& (ret
!= -ENOIOCTLCMD
))
686 v4l2_err(&vpfe_dev
->v4l2_dev
,
687 "stream off failed in subdev\n");
688 vpfe_stop_ccdc_capture(vpfe_dev
);
689 vpfe_detach_irq(vpfe_dev
);
690 videobuf_streamoff(&vpfe_dev
->buffer_queue
);
692 vpfe_dev
->io_usrs
= 0;
693 vpfe_dev
->numbuffers
= config_params
.numbuffers
;
694 videobuf_stop(&vpfe_dev
->buffer_queue
);
695 videobuf_mmap_free(&vpfe_dev
->buffer_queue
);
698 /* Decrement device usrs counter */
700 v4l2_fh_del(&fh
->fh
);
701 v4l2_fh_exit(&fh
->fh
);
702 /* If this is the last file handle */
703 if (!vpfe_dev
->usrs
) {
704 vpfe_dev
->initialized
= 0;
705 if (ccdc_dev
->hw_ops
.close
)
706 ccdc_dev
->hw_ops
.close(vpfe_dev
->pdev
);
707 module_put(ccdc_dev
->owner
);
709 mutex_unlock(&vpfe_dev
->lock
);
710 file
->private_data
= NULL
;
711 /* Free memory allocated to file handle object */
717 * vpfe_mmap : It is used to map kernel space buffers
720 static int vpfe_mmap(struct file
*file
, struct vm_area_struct
*vma
)
722 /* Get the device object and file handle object */
723 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
725 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_mmap\n");
727 return videobuf_mmap_mapper(&vpfe_dev
->buffer_queue
, vma
);
731 * vpfe_poll: It is used for select/poll system call
733 static __poll_t
vpfe_poll(struct file
*file
, poll_table
*wait
)
735 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
737 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_poll\n");
739 if (vpfe_dev
->started
)
740 return videobuf_poll_stream(file
,
741 &vpfe_dev
->buffer_queue
, wait
);
745 /* vpfe capture driver file operations */
746 static const struct v4l2_file_operations vpfe_fops
= {
747 .owner
= THIS_MODULE
,
749 .release
= vpfe_release
,
750 .unlocked_ioctl
= video_ioctl2
,
756 * vpfe_check_format()
757 * This function adjust the input pixel format as per hardware
758 * capabilities and update the same in pixfmt.
759 * Following algorithm used :-
761 * If given pixformat is not in the vpfe list of pix formats or not
762 * supported by the hardware, current value of pixformat in the device
764 * If given field is not supported, then current field is used. If field
765 * is different from current, then it is matched with that from sub device.
766 * Minimum height is 2 lines for interlaced or tb field and 1 line for
767 * progressive. Maximum height is clamped to active active lines of scan
768 * Minimum width is 32 bytes in memory and width is clamped to active
770 * bytesperline is a multiple of 32.
772 static const struct vpfe_pixel_format
*
773 vpfe_check_format(struct vpfe_device
*vpfe_dev
,
774 struct v4l2_pix_format
*pixfmt
)
776 u32 min_height
= 1, min_width
= 32, max_width
, max_height
;
777 const struct vpfe_pixel_format
*vpfe_pix_fmt
;
781 vpfe_pix_fmt
= vpfe_lookup_pix_format(pixfmt
->pixelformat
);
784 * use current pixel format in the vpfe device. We
785 * will find this pix format in the table
787 pixfmt
->pixelformat
= vpfe_dev
->fmt
.fmt
.pix
.pixelformat
;
788 vpfe_pix_fmt
= vpfe_lookup_pix_format(pixfmt
->pixelformat
);
791 /* check if hw supports it */
794 while (ccdc_dev
->hw_ops
.enum_pix(&pix
, temp
) >= 0) {
795 if (vpfe_pix_fmt
->fmtdesc
.pixelformat
== pix
) {
803 /* use current pixel format */
804 pixfmt
->pixelformat
= vpfe_dev
->fmt
.fmt
.pix
.pixelformat
;
806 * Since this is currently used in the vpfe device, we
807 * will find this pix format in the table
809 vpfe_pix_fmt
= vpfe_lookup_pix_format(pixfmt
->pixelformat
);
812 /* check what field format is supported */
813 if (pixfmt
->field
== V4L2_FIELD_ANY
) {
814 /* if field is any, use current value as default */
815 pixfmt
->field
= vpfe_dev
->fmt
.fmt
.pix
.field
;
819 * if field is not same as current field in the vpfe device
820 * try matching the field with the sub device field
822 if (vpfe_dev
->fmt
.fmt
.pix
.field
!= pixfmt
->field
) {
824 * If field value is not in the supported fields, use current
825 * field used in the device as default
827 switch (pixfmt
->field
) {
828 case V4L2_FIELD_INTERLACED
:
829 case V4L2_FIELD_SEQ_TB
:
830 /* if sub device is supporting progressive, use that */
831 if (!vpfe_dev
->std_info
.frame_format
)
832 pixfmt
->field
= V4L2_FIELD_NONE
;
834 case V4L2_FIELD_NONE
:
835 if (vpfe_dev
->std_info
.frame_format
)
836 pixfmt
->field
= V4L2_FIELD_INTERLACED
;
840 /* use current field as default */
841 pixfmt
->field
= vpfe_dev
->fmt
.fmt
.pix
.field
;
846 /* Now adjust image resolutions supported */
847 if (pixfmt
->field
== V4L2_FIELD_INTERLACED
||
848 pixfmt
->field
== V4L2_FIELD_SEQ_TB
)
851 max_width
= vpfe_dev
->std_info
.active_pixels
;
852 max_height
= vpfe_dev
->std_info
.active_lines
;
853 min_width
/= vpfe_pix_fmt
->bpp
;
855 v4l2_info(&vpfe_dev
->v4l2_dev
, "width = %d, height = %d, bpp = %d\n",
856 pixfmt
->width
, pixfmt
->height
, vpfe_pix_fmt
->bpp
);
858 pixfmt
->width
= clamp((pixfmt
->width
), min_width
, max_width
);
859 pixfmt
->height
= clamp((pixfmt
->height
), min_height
, max_height
);
861 /* If interlaced, adjust height to be a multiple of 2 */
862 if (pixfmt
->field
== V4L2_FIELD_INTERLACED
)
863 pixfmt
->height
&= (~1);
865 * recalculate bytesperline and sizeimage since width
866 * and height might have changed
868 pixfmt
->bytesperline
= (((pixfmt
->width
* vpfe_pix_fmt
->bpp
) + 31)
870 if (pixfmt
->pixelformat
== V4L2_PIX_FMT_NV12
)
872 pixfmt
->bytesperline
* pixfmt
->height
+
873 ((pixfmt
->bytesperline
* pixfmt
->height
) >> 1);
875 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
;
877 v4l2_info(&vpfe_dev
->v4l2_dev
, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
878 pixfmt
->width
, pixfmt
->height
, vpfe_pix_fmt
->bpp
,
879 pixfmt
->bytesperline
, pixfmt
->sizeimage
);
883 static int vpfe_querycap(struct file
*file
, void *priv
,
884 struct v4l2_capability
*cap
)
886 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
888 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querycap\n");
890 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
891 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
892 strlcpy(cap
->driver
, CAPTURE_DRV_NAME
, sizeof(cap
->driver
));
893 strlcpy(cap
->bus_info
, "VPFE", sizeof(cap
->bus_info
));
894 strlcpy(cap
->card
, vpfe_dev
->cfg
->card_name
, sizeof(cap
->card
));
898 static int vpfe_g_fmt_vid_cap(struct file
*file
, void *priv
,
899 struct v4l2_format
*fmt
)
901 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
903 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_fmt_vid_cap\n");
904 /* Fill in the information about format */
905 *fmt
= vpfe_dev
->fmt
;
909 static int vpfe_enum_fmt_vid_cap(struct file
*file
, void *priv
,
910 struct v4l2_fmtdesc
*fmt
)
912 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
913 const struct vpfe_pixel_format
*pix_fmt
;
917 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_enum_fmt_vid_cap\n");
919 if (ccdc_dev
->hw_ops
.enum_pix(&pix
, fmt
->index
) < 0)
922 /* Fill in the information about format */
923 pix_fmt
= vpfe_lookup_pix_format(pix
);
925 temp_index
= fmt
->index
;
926 *fmt
= pix_fmt
->fmtdesc
;
927 fmt
->index
= temp_index
;
933 static int vpfe_s_fmt_vid_cap(struct file
*file
, void *priv
,
934 struct v4l2_format
*fmt
)
936 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
937 const struct vpfe_pixel_format
*pix_fmts
;
940 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_fmt_vid_cap\n");
942 /* If streaming is started, return error */
943 if (vpfe_dev
->started
) {
944 v4l2_err(&vpfe_dev
->v4l2_dev
, "Streaming is started\n");
948 /* Check for valid frame format */
949 pix_fmts
= vpfe_check_format(vpfe_dev
, &fmt
->fmt
.pix
);
953 /* store the pixel format in the device object */
954 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
958 /* First detach any IRQ if currently attached */
959 vpfe_detach_irq(vpfe_dev
);
960 vpfe_dev
->fmt
= *fmt
;
961 /* set image capture parameters in the ccdc */
962 ret
= vpfe_config_ccdc_image_format(vpfe_dev
);
963 mutex_unlock(&vpfe_dev
->lock
);
967 static int vpfe_try_fmt_vid_cap(struct file
*file
, void *priv
,
968 struct v4l2_format
*f
)
970 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
971 const struct vpfe_pixel_format
*pix_fmts
;
973 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_try_fmt_vid_cap\n");
975 pix_fmts
= vpfe_check_format(vpfe_dev
, &f
->fmt
.pix
);
982 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
983 * given app input index
985 static int vpfe_get_subdev_input_index(struct vpfe_device
*vpfe_dev
,
987 int *subdev_input_index
,
990 struct vpfe_config
*cfg
= vpfe_dev
->cfg
;
991 struct vpfe_subdev_info
*sdinfo
;
994 for (i
= 0; i
< cfg
->num_subdevs
; i
++) {
995 sdinfo
= &cfg
->sub_devs
[i
];
996 if (app_input_index
< (j
+ sdinfo
->num_inputs
)) {
998 *subdev_input_index
= app_input_index
- j
;
1001 j
+= sdinfo
->num_inputs
;
1007 * vpfe_get_app_input - Get app input index for a given subdev input index
1008 * driver stores the input index of the current sub device and translate it
1009 * when application request the current input
1011 static int vpfe_get_app_input_index(struct vpfe_device
*vpfe_dev
,
1012 int *app_input_index
)
1014 struct vpfe_config
*cfg
= vpfe_dev
->cfg
;
1015 struct vpfe_subdev_info
*sdinfo
;
1018 for (i
= 0; i
< cfg
->num_subdevs
; i
++) {
1019 sdinfo
= &cfg
->sub_devs
[i
];
1020 if (!strcmp(sdinfo
->name
, vpfe_dev
->current_subdev
->name
)) {
1021 if (vpfe_dev
->current_input
>= sdinfo
->num_inputs
)
1023 *app_input_index
= j
+ vpfe_dev
->current_input
;
1026 j
+= sdinfo
->num_inputs
;
1031 static int vpfe_enum_input(struct file
*file
, void *priv
,
1032 struct v4l2_input
*inp
)
1034 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1035 struct vpfe_subdev_info
*sdinfo
;
1038 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_enum_input\n");
1040 if (vpfe_get_subdev_input_index(vpfe_dev
,
1044 v4l2_err(&vpfe_dev
->v4l2_dev
, "input information not found for the subdev\n");
1047 sdinfo
= &vpfe_dev
->cfg
->sub_devs
[subdev
];
1048 *inp
= sdinfo
->inputs
[index
];
1052 static int vpfe_g_input(struct file
*file
, void *priv
, unsigned int *index
)
1054 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1056 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_input\n");
1058 return vpfe_get_app_input_index(vpfe_dev
, index
);
1062 static int vpfe_s_input(struct file
*file
, void *priv
, unsigned int index
)
1064 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1065 struct v4l2_subdev
*sd
;
1066 struct vpfe_subdev_info
*sdinfo
;
1067 int subdev_index
, inp_index
;
1068 struct vpfe_route
*route
;
1072 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_input\n");
1074 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1079 * If streaming is started return device busy
1082 if (vpfe_dev
->started
) {
1083 v4l2_err(&vpfe_dev
->v4l2_dev
, "Streaming is on\n");
1087 ret
= vpfe_get_subdev_input_index(vpfe_dev
,
1092 v4l2_err(&vpfe_dev
->v4l2_dev
, "invalid input index\n");
1096 sdinfo
= &vpfe_dev
->cfg
->sub_devs
[subdev_index
];
1097 sd
= vpfe_dev
->sd
[subdev_index
];
1098 route
= &sdinfo
->routes
[inp_index
];
1099 if (route
&& sdinfo
->can_route
) {
1100 input
= route
->input
;
1101 output
= route
->output
;
1108 ret
= v4l2_subdev_call(sd
, video
, s_routing
, input
, output
, 0);
1111 v4l2_err(&vpfe_dev
->v4l2_dev
,
1112 "vpfe_doioctl:error in setting input in decoder\n");
1116 vpfe_dev
->current_subdev
= sdinfo
;
1118 vpfe_dev
->v4l2_dev
.ctrl_handler
= sd
->ctrl_handler
;
1119 vpfe_dev
->current_input
= index
;
1120 vpfe_dev
->std_index
= 0;
1122 /* set the bus/interface parameter for the sub device in ccdc */
1123 ret
= ccdc_dev
->hw_ops
.set_hw_if_params(&sdinfo
->ccdc_if_params
);
1127 /* set the default image parameters in the device */
1128 ret
= vpfe_config_image_format(vpfe_dev
,
1129 vpfe_standards
[vpfe_dev
->std_index
].std_id
);
1131 mutex_unlock(&vpfe_dev
->lock
);
1135 static int vpfe_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1137 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1138 struct vpfe_subdev_info
*sdinfo
;
1141 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querystd\n");
1143 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1144 sdinfo
= vpfe_dev
->current_subdev
;
1147 /* Call querystd function of decoder device */
1148 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1149 video
, querystd
, std_id
);
1150 mutex_unlock(&vpfe_dev
->lock
);
1154 static int vpfe_s_std(struct file
*file
, void *priv
, v4l2_std_id std_id
)
1156 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1157 struct vpfe_subdev_info
*sdinfo
;
1160 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_std\n");
1162 /* Call decoder driver function to set the standard */
1163 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1167 sdinfo
= vpfe_dev
->current_subdev
;
1168 /* If streaming is started, return device busy error */
1169 if (vpfe_dev
->started
) {
1170 v4l2_err(&vpfe_dev
->v4l2_dev
, "streaming is started\n");
1175 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1176 video
, s_std
, std_id
);
1178 v4l2_err(&vpfe_dev
->v4l2_dev
, "Failed to set standard\n");
1181 ret
= vpfe_config_image_format(vpfe_dev
, std_id
);
1184 mutex_unlock(&vpfe_dev
->lock
);
1188 static int vpfe_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1190 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1192 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_std\n");
1194 *std_id
= vpfe_standards
[vpfe_dev
->std_index
].std_id
;
1198 * Videobuf operations
1200 static int vpfe_videobuf_setup(struct videobuf_queue
*vq
,
1201 unsigned int *count
,
1204 struct vpfe_fh
*fh
= vq
->priv_data
;
1205 struct vpfe_device
*vpfe_dev
= fh
->vpfe_dev
;
1207 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buffer_setup\n");
1208 *size
= vpfe_dev
->fmt
.fmt
.pix
.sizeimage
;
1209 if (vpfe_dev
->memory
== V4L2_MEMORY_MMAP
&&
1210 vpfe_dev
->fmt
.fmt
.pix
.sizeimage
> config_params
.device_bufsize
)
1211 *size
= config_params
.device_bufsize
;
1213 if (*count
< config_params
.min_numbuffers
)
1214 *count
= config_params
.min_numbuffers
;
1215 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
1216 "count=%d, size=%d\n", *count
, *size
);
1220 static int vpfe_videobuf_prepare(struct videobuf_queue
*vq
,
1221 struct videobuf_buffer
*vb
,
1222 enum v4l2_field field
)
1224 struct vpfe_fh
*fh
= vq
->priv_data
;
1225 struct vpfe_device
*vpfe_dev
= fh
->vpfe_dev
;
1229 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buffer_prepare\n");
1231 /* If buffer is not initialized, initialize it */
1232 if (VIDEOBUF_NEEDS_INIT
== vb
->state
) {
1233 vb
->width
= vpfe_dev
->fmt
.fmt
.pix
.width
;
1234 vb
->height
= vpfe_dev
->fmt
.fmt
.pix
.height
;
1235 vb
->size
= vpfe_dev
->fmt
.fmt
.pix
.sizeimage
;
1238 ret
= videobuf_iolock(vq
, vb
, NULL
);
1242 addr
= videobuf_to_dma_contig(vb
);
1243 /* Make sure user addresses are aligned to 32 bytes */
1244 if (!ALIGN(addr
, 32))
1247 vb
->state
= VIDEOBUF_PREPARED
;
1252 static void vpfe_videobuf_queue(struct videobuf_queue
*vq
,
1253 struct videobuf_buffer
*vb
)
1255 /* Get the file handle object and device object */
1256 struct vpfe_fh
*fh
= vq
->priv_data
;
1257 struct vpfe_device
*vpfe_dev
= fh
->vpfe_dev
;
1258 unsigned long flags
;
1260 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buffer_queue\n");
1262 /* add the buffer to the DMA queue */
1263 spin_lock_irqsave(&vpfe_dev
->dma_queue_lock
, flags
);
1264 list_add_tail(&vb
->queue
, &vpfe_dev
->dma_queue
);
1265 spin_unlock_irqrestore(&vpfe_dev
->dma_queue_lock
, flags
);
1267 /* Change state of the buffer */
1268 vb
->state
= VIDEOBUF_QUEUED
;
1271 static void vpfe_videobuf_release(struct videobuf_queue
*vq
,
1272 struct videobuf_buffer
*vb
)
1274 struct vpfe_fh
*fh
= vq
->priv_data
;
1275 struct vpfe_device
*vpfe_dev
= fh
->vpfe_dev
;
1276 unsigned long flags
;
1278 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_videobuf_release\n");
1281 * We need to flush the buffer from the dma queue since
1282 * they are de-allocated
1284 spin_lock_irqsave(&vpfe_dev
->dma_queue_lock
, flags
);
1285 INIT_LIST_HEAD(&vpfe_dev
->dma_queue
);
1286 spin_unlock_irqrestore(&vpfe_dev
->dma_queue_lock
, flags
);
1287 videobuf_dma_contig_free(vq
, vb
);
1288 vb
->state
= VIDEOBUF_NEEDS_INIT
;
1291 static const struct videobuf_queue_ops vpfe_videobuf_qops
= {
1292 .buf_setup
= vpfe_videobuf_setup
,
1293 .buf_prepare
= vpfe_videobuf_prepare
,
1294 .buf_queue
= vpfe_videobuf_queue
,
1295 .buf_release
= vpfe_videobuf_release
,
1299 * vpfe_reqbufs. currently support REQBUF only once opening
1302 static int vpfe_reqbufs(struct file
*file
, void *priv
,
1303 struct v4l2_requestbuffers
*req_buf
)
1305 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1306 struct vpfe_fh
*fh
= file
->private_data
;
1309 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_reqbufs\n");
1311 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= req_buf
->type
) {
1312 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buffer type\n");
1316 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1320 if (vpfe_dev
->io_usrs
!= 0) {
1321 v4l2_err(&vpfe_dev
->v4l2_dev
, "Only one IO user allowed\n");
1326 vpfe_dev
->memory
= req_buf
->memory
;
1327 videobuf_queue_dma_contig_init(&vpfe_dev
->buffer_queue
,
1328 &vpfe_videobuf_qops
,
1332 vpfe_dev
->fmt
.fmt
.pix
.field
,
1333 sizeof(struct videobuf_buffer
),
1337 vpfe_dev
->io_usrs
= 1;
1338 INIT_LIST_HEAD(&vpfe_dev
->dma_queue
);
1339 ret
= videobuf_reqbufs(&vpfe_dev
->buffer_queue
, req_buf
);
1341 mutex_unlock(&vpfe_dev
->lock
);
1345 static int vpfe_querybuf(struct file
*file
, void *priv
,
1346 struct v4l2_buffer
*buf
)
1348 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1350 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querybuf\n");
1352 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf
->type
) {
1353 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1357 if (vpfe_dev
->memory
!= V4L2_MEMORY_MMAP
) {
1358 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid memory\n");
1361 /* Call videobuf_querybuf to get information */
1362 return videobuf_querybuf(&vpfe_dev
->buffer_queue
, buf
);
1365 static int vpfe_qbuf(struct file
*file
, void *priv
,
1366 struct v4l2_buffer
*p
)
1368 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1369 struct vpfe_fh
*fh
= file
->private_data
;
1371 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_qbuf\n");
1373 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= p
->type
) {
1374 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1379 * If this file handle is not allowed to do IO,
1382 if (!fh
->io_allowed
) {
1383 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1386 return videobuf_qbuf(&vpfe_dev
->buffer_queue
, p
);
1389 static int vpfe_dqbuf(struct file
*file
, void *priv
,
1390 struct v4l2_buffer
*buf
)
1392 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1394 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_dqbuf\n");
1396 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf
->type
) {
1397 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1400 return videobuf_dqbuf(&vpfe_dev
->buffer_queue
,
1401 buf
, file
->f_flags
& O_NONBLOCK
);
1405 * vpfe_calculate_offsets : This function calculates buffers offset
1406 * for top and bottom field
1408 static void vpfe_calculate_offsets(struct vpfe_device
*vpfe_dev
)
1410 struct v4l2_rect image_win
;
1412 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_calculate_offsets\n");
1414 ccdc_dev
->hw_ops
.get_image_window(&image_win
);
1415 vpfe_dev
->field_off
= image_win
.height
* image_win
.width
;
1418 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1419 static void vpfe_start_ccdc_capture(struct vpfe_device
*vpfe_dev
)
1421 ccdc_dev
->hw_ops
.enable(1);
1422 if (ccdc_dev
->hw_ops
.enable_out_to_sdram
)
1423 ccdc_dev
->hw_ops
.enable_out_to_sdram(1);
1424 vpfe_dev
->started
= 1;
1428 * vpfe_streamon. Assume the DMA queue is not empty.
1429 * application is expected to call QBUF before calling
1430 * this ioctl. If not, driver returns error
1432 static int vpfe_streamon(struct file
*file
, void *priv
,
1433 enum v4l2_buf_type buf_type
)
1435 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1436 struct vpfe_fh
*fh
= file
->private_data
;
1437 struct vpfe_subdev_info
*sdinfo
;
1441 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_streamon\n");
1443 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf_type
) {
1444 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1448 /* If file handle is not allowed IO, return error */
1449 if (!fh
->io_allowed
) {
1450 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1454 sdinfo
= vpfe_dev
->current_subdev
;
1455 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1456 video
, s_stream
, 1);
1458 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
1459 v4l2_err(&vpfe_dev
->v4l2_dev
, "stream on failed in subdev\n");
1463 /* If buffer queue is empty, return error */
1464 if (list_empty(&vpfe_dev
->buffer_queue
.stream
)) {
1465 v4l2_err(&vpfe_dev
->v4l2_dev
, "buffer queue is empty\n");
1469 /* Call videobuf_streamon to start streaming * in videobuf */
1470 ret
= videobuf_streamon(&vpfe_dev
->buffer_queue
);
1475 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1478 /* Get the next frame from the buffer queue */
1479 vpfe_dev
->next_frm
= list_entry(vpfe_dev
->dma_queue
.next
,
1480 struct videobuf_buffer
, queue
);
1481 vpfe_dev
->cur_frm
= vpfe_dev
->next_frm
;
1482 /* Remove buffer from the buffer queue */
1483 list_del(&vpfe_dev
->cur_frm
->queue
);
1484 /* Mark state of the current frame to active */
1485 vpfe_dev
->cur_frm
->state
= VIDEOBUF_ACTIVE
;
1486 /* Initialize field_id and started member */
1487 vpfe_dev
->field_id
= 0;
1488 addr
= videobuf_to_dma_contig(vpfe_dev
->cur_frm
);
1490 /* Calculate field offset */
1491 vpfe_calculate_offsets(vpfe_dev
);
1493 if (vpfe_attach_irq(vpfe_dev
) < 0) {
1494 v4l2_err(&vpfe_dev
->v4l2_dev
,
1495 "Error in attaching interrupt handle\n");
1499 if (ccdc_dev
->hw_ops
.configure() < 0) {
1500 v4l2_err(&vpfe_dev
->v4l2_dev
,
1501 "Error in configuring ccdc\n");
1505 ccdc_dev
->hw_ops
.setfbaddr((unsigned long)(addr
));
1506 vpfe_start_ccdc_capture(vpfe_dev
);
1507 mutex_unlock(&vpfe_dev
->lock
);
1510 mutex_unlock(&vpfe_dev
->lock
);
1512 videobuf_streamoff(&vpfe_dev
->buffer_queue
);
1516 static int vpfe_streamoff(struct file
*file
, void *priv
,
1517 enum v4l2_buf_type buf_type
)
1519 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1520 struct vpfe_fh
*fh
= file
->private_data
;
1521 struct vpfe_subdev_info
*sdinfo
;
1524 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_streamoff\n");
1526 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf_type
) {
1527 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1531 /* If io is allowed for this file handle, return error */
1532 if (!fh
->io_allowed
) {
1533 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1537 /* If streaming is not started, return error */
1538 if (!vpfe_dev
->started
) {
1539 v4l2_err(&vpfe_dev
->v4l2_dev
, "device started\n");
1543 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1547 vpfe_stop_ccdc_capture(vpfe_dev
);
1548 vpfe_detach_irq(vpfe_dev
);
1550 sdinfo
= vpfe_dev
->current_subdev
;
1551 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1552 video
, s_stream
, 0);
1554 if (ret
&& (ret
!= -ENOIOCTLCMD
))
1555 v4l2_err(&vpfe_dev
->v4l2_dev
, "stream off failed in subdev\n");
1556 ret
= videobuf_streamoff(&vpfe_dev
->buffer_queue
);
1557 mutex_unlock(&vpfe_dev
->lock
);
1561 static int vpfe_cropcap(struct file
*file
, void *priv
,
1562 struct v4l2_cropcap
*crop
)
1564 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1566 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_cropcap\n");
1568 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1570 /* If std_index is invalid, then just return (== 1:1 aspect) */
1571 if (vpfe_dev
->std_index
>= ARRAY_SIZE(vpfe_standards
))
1574 crop
->pixelaspect
= vpfe_standards
[vpfe_dev
->std_index
].pixelaspect
;
1578 static int vpfe_g_selection(struct file
*file
, void *priv
,
1579 struct v4l2_selection
*sel
)
1581 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1583 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_selection\n");
1585 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1588 switch (sel
->target
) {
1589 case V4L2_SEL_TGT_CROP
:
1590 sel
->r
= vpfe_dev
->crop
;
1592 case V4L2_SEL_TGT_CROP_DEFAULT
:
1593 case V4L2_SEL_TGT_CROP_BOUNDS
:
1594 sel
->r
.width
= vpfe_standards
[vpfe_dev
->std_index
].width
;
1595 sel
->r
.height
= vpfe_standards
[vpfe_dev
->std_index
].height
;
1603 static int vpfe_s_selection(struct file
*file
, void *priv
,
1604 struct v4l2_selection
*sel
)
1606 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1607 struct v4l2_rect rect
= sel
->r
;
1610 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_selection\n");
1612 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1613 sel
->target
!= V4L2_SEL_TGT_CROP
)
1616 if (vpfe_dev
->started
) {
1617 /* make sure streaming is not started */
1618 v4l2_err(&vpfe_dev
->v4l2_dev
,
1619 "Cannot change crop when streaming is ON\n");
1623 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1627 if (rect
.top
< 0 || rect
.left
< 0) {
1628 v4l2_err(&vpfe_dev
->v4l2_dev
,
1629 "doesn't support negative values for top & left\n");
1634 /* adjust the width to 16 pixel boundary */
1635 rect
.width
= ((rect
.width
+ 15) & ~0xf);
1637 /* make sure parameters are valid */
1638 if ((rect
.left
+ rect
.width
>
1639 vpfe_dev
->std_info
.active_pixels
) ||
1640 (rect
.top
+ rect
.height
>
1641 vpfe_dev
->std_info
.active_lines
)) {
1642 v4l2_err(&vpfe_dev
->v4l2_dev
, "Error in S_SELECTION params\n");
1646 ccdc_dev
->hw_ops
.set_image_window(&rect
);
1647 vpfe_dev
->fmt
.fmt
.pix
.width
= rect
.width
;
1648 vpfe_dev
->fmt
.fmt
.pix
.height
= rect
.height
;
1649 vpfe_dev
->fmt
.fmt
.pix
.bytesperline
=
1650 ccdc_dev
->hw_ops
.get_line_length();
1651 vpfe_dev
->fmt
.fmt
.pix
.sizeimage
=
1652 vpfe_dev
->fmt
.fmt
.pix
.bytesperline
*
1653 vpfe_dev
->fmt
.fmt
.pix
.height
;
1654 vpfe_dev
->crop
= rect
;
1657 mutex_unlock(&vpfe_dev
->lock
);
1661 /* vpfe capture ioctl operations */
1662 static const struct v4l2_ioctl_ops vpfe_ioctl_ops
= {
1663 .vidioc_querycap
= vpfe_querycap
,
1664 .vidioc_g_fmt_vid_cap
= vpfe_g_fmt_vid_cap
,
1665 .vidioc_enum_fmt_vid_cap
= vpfe_enum_fmt_vid_cap
,
1666 .vidioc_s_fmt_vid_cap
= vpfe_s_fmt_vid_cap
,
1667 .vidioc_try_fmt_vid_cap
= vpfe_try_fmt_vid_cap
,
1668 .vidioc_enum_input
= vpfe_enum_input
,
1669 .vidioc_g_input
= vpfe_g_input
,
1670 .vidioc_s_input
= vpfe_s_input
,
1671 .vidioc_querystd
= vpfe_querystd
,
1672 .vidioc_s_std
= vpfe_s_std
,
1673 .vidioc_g_std
= vpfe_g_std
,
1674 .vidioc_reqbufs
= vpfe_reqbufs
,
1675 .vidioc_querybuf
= vpfe_querybuf
,
1676 .vidioc_qbuf
= vpfe_qbuf
,
1677 .vidioc_dqbuf
= vpfe_dqbuf
,
1678 .vidioc_streamon
= vpfe_streamon
,
1679 .vidioc_streamoff
= vpfe_streamoff
,
1680 .vidioc_cropcap
= vpfe_cropcap
,
1681 .vidioc_g_selection
= vpfe_g_selection
,
1682 .vidioc_s_selection
= vpfe_s_selection
,
1685 static struct vpfe_device
*vpfe_initialize(void)
1687 struct vpfe_device
*vpfe_dev
;
1689 /* Default number of buffers should be 3 */
1690 if ((numbuffers
> 0) &&
1691 (numbuffers
< config_params
.min_numbuffers
))
1692 numbuffers
= config_params
.min_numbuffers
;
1695 * Set buffer size to min buffers size if invalid buffer size is
1698 if (bufsize
< config_params
.min_bufsize
)
1699 bufsize
= config_params
.min_bufsize
;
1701 config_params
.numbuffers
= numbuffers
;
1704 config_params
.device_bufsize
= bufsize
;
1706 /* Allocate memory for device objects */
1707 vpfe_dev
= kzalloc(sizeof(*vpfe_dev
), GFP_KERNEL
);
1713 * vpfe_probe : This function creates device entries by register
1714 * itself to the V4L2 driver and initializes fields of each
1717 static int vpfe_probe(struct platform_device
*pdev
)
1719 struct vpfe_subdev_info
*sdinfo
;
1720 struct vpfe_config
*vpfe_cfg
;
1721 struct resource
*res1
;
1722 struct vpfe_device
*vpfe_dev
;
1723 struct i2c_adapter
*i2c_adap
;
1724 struct video_device
*vfd
;
1726 int num_subdevs
= 0;
1728 /* Get the pointer to the device object */
1729 vpfe_dev
= vpfe_initialize();
1732 v4l2_err(pdev
->dev
.driver
,
1733 "Failed to allocate memory for vpfe_dev\n");
1737 vpfe_dev
->pdev
= &pdev
->dev
;
1739 if (!pdev
->dev
.platform_data
) {
1740 v4l2_err(pdev
->dev
.driver
, "Unable to get vpfe config\n");
1742 goto probe_free_dev_mem
;
1745 vpfe_cfg
= pdev
->dev
.platform_data
;
1746 vpfe_dev
->cfg
= vpfe_cfg
;
1747 if (!vpfe_cfg
->ccdc
|| !vpfe_cfg
->card_name
|| !vpfe_cfg
->sub_devs
) {
1748 v4l2_err(pdev
->dev
.driver
, "null ptr in vpfe_cfg\n");
1750 goto probe_free_dev_mem
;
1753 /* Allocate memory for ccdc configuration */
1754 ccdc_cfg
= kmalloc(sizeof(*ccdc_cfg
), GFP_KERNEL
);
1757 goto probe_free_dev_mem
;
1760 mutex_lock(&ccdc_lock
);
1762 strncpy(ccdc_cfg
->name
, vpfe_cfg
->ccdc
, 32);
1763 /* Get VINT0 irq resource */
1764 res1
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1766 v4l2_err(pdev
->dev
.driver
,
1767 "Unable to get interrupt for VINT0\n");
1769 goto probe_free_ccdc_cfg_mem
;
1771 vpfe_dev
->ccdc_irq0
= res1
->start
;
1773 /* Get VINT1 irq resource */
1774 res1
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 1);
1776 v4l2_err(pdev
->dev
.driver
,
1777 "Unable to get interrupt for VINT1\n");
1779 goto probe_free_ccdc_cfg_mem
;
1781 vpfe_dev
->ccdc_irq1
= res1
->start
;
1783 ret
= request_irq(vpfe_dev
->ccdc_irq0
, vpfe_isr
, 0,
1784 "vpfe_capture0", vpfe_dev
);
1787 v4l2_err(pdev
->dev
.driver
, "Unable to request interrupt\n");
1788 goto probe_free_ccdc_cfg_mem
;
1791 vfd
= &vpfe_dev
->video_dev
;
1792 /* Initialize field of video device */
1793 vfd
->release
= video_device_release_empty
;
1794 vfd
->fops
= &vpfe_fops
;
1795 vfd
->ioctl_ops
= &vpfe_ioctl_ops
;
1797 vfd
->v4l2_dev
= &vpfe_dev
->v4l2_dev
;
1798 snprintf(vfd
->name
, sizeof(vfd
->name
),
1801 (VPFE_CAPTURE_VERSION_CODE
>> 16) & 0xff,
1802 (VPFE_CAPTURE_VERSION_CODE
>> 8) & 0xff,
1803 (VPFE_CAPTURE_VERSION_CODE
) & 0xff);
1805 ret
= v4l2_device_register(&pdev
->dev
, &vpfe_dev
->v4l2_dev
);
1807 v4l2_err(pdev
->dev
.driver
,
1808 "Unable to register v4l2 device.\n");
1809 goto probe_out_release_irq
;
1811 v4l2_info(&vpfe_dev
->v4l2_dev
, "v4l2 device registered\n");
1812 spin_lock_init(&vpfe_dev
->irqlock
);
1813 spin_lock_init(&vpfe_dev
->dma_queue_lock
);
1814 mutex_init(&vpfe_dev
->lock
);
1816 /* Initialize field of the device objects */
1817 vpfe_dev
->numbuffers
= config_params
.numbuffers
;
1819 /* register video device */
1820 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
1821 "trying to register vpfe device.\n");
1822 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
1823 "video_dev=%p\n", &vpfe_dev
->video_dev
);
1824 vpfe_dev
->fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1825 ret
= video_register_device(&vpfe_dev
->video_dev
,
1826 VFL_TYPE_GRABBER
, -1);
1829 v4l2_err(pdev
->dev
.driver
,
1830 "Unable to register video device.\n");
1831 goto probe_out_v4l2_unregister
;
1834 v4l2_info(&vpfe_dev
->v4l2_dev
, "video device registered\n");
1835 /* set the driver data in platform device */
1836 platform_set_drvdata(pdev
, vpfe_dev
);
1837 /* set driver private data */
1838 video_set_drvdata(&vpfe_dev
->video_dev
, vpfe_dev
);
1839 i2c_adap
= i2c_get_adapter(vpfe_cfg
->i2c_adapter_id
);
1840 num_subdevs
= vpfe_cfg
->num_subdevs
;
1841 vpfe_dev
->sd
= kmalloc_array(num_subdevs
,
1842 sizeof(*vpfe_dev
->sd
),
1844 if (!vpfe_dev
->sd
) {
1846 goto probe_out_video_unregister
;
1849 for (i
= 0; i
< num_subdevs
; i
++) {
1850 struct v4l2_input
*inps
;
1852 sdinfo
= &vpfe_cfg
->sub_devs
[i
];
1854 /* Load up the subdevice */
1856 v4l2_i2c_new_subdev_board(&vpfe_dev
->v4l2_dev
,
1858 &sdinfo
->board_info
,
1860 if (vpfe_dev
->sd
[i
]) {
1861 v4l2_info(&vpfe_dev
->v4l2_dev
,
1862 "v4l2 sub device %s registered\n",
1864 vpfe_dev
->sd
[i
]->grp_id
= sdinfo
->grp_id
;
1865 /* update tvnorms from the sub devices */
1866 for (j
= 0; j
< sdinfo
->num_inputs
; j
++) {
1867 inps
= &sdinfo
->inputs
[j
];
1868 vfd
->tvnorms
|= inps
->std
;
1871 v4l2_info(&vpfe_dev
->v4l2_dev
,
1872 "v4l2 sub device %s register fails\n",
1879 /* set first sub device as current one */
1880 vpfe_dev
->current_subdev
= &vpfe_cfg
->sub_devs
[0];
1881 vpfe_dev
->v4l2_dev
.ctrl_handler
= vpfe_dev
->sd
[0]->ctrl_handler
;
1883 /* We have at least one sub device to work with */
1884 mutex_unlock(&ccdc_lock
);
1888 kfree(vpfe_dev
->sd
);
1889 probe_out_video_unregister
:
1890 video_unregister_device(&vpfe_dev
->video_dev
);
1891 probe_out_v4l2_unregister
:
1892 v4l2_device_unregister(&vpfe_dev
->v4l2_dev
);
1893 probe_out_release_irq
:
1894 free_irq(vpfe_dev
->ccdc_irq0
, vpfe_dev
);
1895 probe_free_ccdc_cfg_mem
:
1897 mutex_unlock(&ccdc_lock
);
1904 * vpfe_remove : It un-register device from V4L2 driver
1906 static int vpfe_remove(struct platform_device
*pdev
)
1908 struct vpfe_device
*vpfe_dev
= platform_get_drvdata(pdev
);
1910 v4l2_info(pdev
->dev
.driver
, "vpfe_remove\n");
1912 free_irq(vpfe_dev
->ccdc_irq0
, vpfe_dev
);
1913 kfree(vpfe_dev
->sd
);
1914 v4l2_device_unregister(&vpfe_dev
->v4l2_dev
);
1915 video_unregister_device(&vpfe_dev
->video_dev
);
1921 static int vpfe_suspend(struct device
*dev
)
1926 static int vpfe_resume(struct device
*dev
)
1931 static const struct dev_pm_ops vpfe_dev_pm_ops
= {
1932 .suspend
= vpfe_suspend
,
1933 .resume
= vpfe_resume
,
1936 static struct platform_driver vpfe_driver
= {
1938 .name
= CAPTURE_DRV_NAME
,
1939 .pm
= &vpfe_dev_pm_ops
,
1941 .probe
= vpfe_probe
,
1942 .remove
= vpfe_remove
,
1945 module_platform_driver(vpfe_driver
);