2 * Copyright (C) 2008-2009 Texas Instruments Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Driver name : VPFE Capture driver
19 * VPFE Capture driver allows applications to capture and stream video
20 * frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
21 * TVP5146 or Raw Bayer RGB image data from an image sensor
22 * such as Microns' MT9T001, MT9T031 etc.
24 * These SoCs have, in common, a Video Processing Subsystem (VPSS) that
25 * consists of a Video Processing Front End (VPFE) for capturing
26 * video/raw image data and Video Processing Back End (VPBE) for displaying
27 * YUV data through an in-built analog encoder or Digital LCD port. This
28 * driver is for capture through VPFE. A typical EVM using these SoCs have
29 * following high level configuration.
32 * decoder(TVP5146/ YUV/
33 * MT9T001) --> Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
42 * The data flow happens from a decoder connected to the VPFE over a
43 * YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
44 * and to the input of VPFE through an optional MUX (if more inputs are
45 * to be interfaced on the EVM). The input data is first passed through
46 * CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
47 * does very little or no processing on YUV data and does pre-process Raw
48 * Bayer RGB data through modules such as Defect Pixel Correction (DFC)
49 * Color Space Conversion (CSC), data gain/offset etc. After this, data
50 * can be written to SDRAM or can be connected to the image processing
51 * block such as IPIPE (on DM355 only).
55 * - Capture using TVP5146 over BT.656
56 * - support for interfacing decoders using sub device model
57 * - Work with DM355 or DM6446 CCDC to do Raw Bayer RGB/YUV
58 * data capture to SDRAM.
60 * - Support multiple REQBUF after open
61 * - Support for de-allocating buffers through REQBUF
62 * - Support for Raw Bayer RGB capture
63 * - Support for chaining Image Processor
64 * - Support for static allocation of buffers
65 * - Support for USERPTR IO
66 * - Support for STREAMON before QBUF
67 * - Support for control ioctls
69 #include <linux/module.h>
70 #include <linux/init.h>
71 #include <linux/platform_device.h>
72 #include <linux/interrupt.h>
73 #include <linux/version.h>
74 #include <media/v4l2-common.h>
76 #include <media/davinci/vpfe_capture.h>
77 #include "ccdc_hw_device.h"
80 static u32 numbuffers
= 3;
81 static u32 bufsize
= (720 * 576 * 2);
83 module_param(numbuffers
, uint
, S_IRUGO
);
84 module_param(bufsize
, uint
, S_IRUGO
);
85 module_param(debug
, int, 0644);
87 MODULE_PARM_DESC(numbuffers
, "buffer count (default:3)");
88 MODULE_PARM_DESC(bufsize
, "buffer size in bytes (default:720 x 576 x 2)");
89 MODULE_PARM_DESC(debug
, "Debug level 0-1");
91 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
92 MODULE_LICENSE("GPL");
93 MODULE_AUTHOR("Texas Instruments");
95 /* standard information */
96 struct vpfe_standard
{
100 struct v4l2_fract pixelaspect
;
101 /* 0 - progressive, 1 - interlaced */
105 /* ccdc configuration */
107 /* This make sure vpfe is probed and ready to go */
109 /* name of ccdc device */
111 /* for storing mem maps for CCDC */
113 void *__iomem ccdc_addr
;
116 /* data structures */
117 static struct vpfe_config_params config_params
= {
120 .min_bufsize
= 720 * 480 * 2,
121 .device_bufsize
= 720 * 576 * 2,
124 /* ccdc device registered */
125 static struct ccdc_hw_device
*ccdc_dev
;
126 /* lock for accessing ccdc information */
127 static DEFINE_MUTEX(ccdc_lock
);
128 /* ccdc configuration */
129 static struct ccdc_config
*ccdc_cfg
;
131 const struct vpfe_standard vpfe_standards
[] = {
132 {V4L2_STD_525_60
, 720, 480, {11, 10}, 1},
133 {V4L2_STD_625_50
, 720, 576, {54, 59}, 1},
136 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
137 static const struct vpfe_pixel_format vpfe_pix_fmts
[] = {
141 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
142 .description
= "Bayer GrRBGb 8bit A-Law compr.",
143 .pixelformat
= V4L2_PIX_FMT_SBGGR8
,
150 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
151 .description
= "Bayer GrRBGb - 16bit",
152 .pixelformat
= V4L2_PIX_FMT_SBGGR16
,
159 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
160 .description
= "Bayer GrRBGb 8bit DPCM compr.",
161 .pixelformat
= V4L2_PIX_FMT_SGRBG10DPCM8
,
168 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
169 .description
= "YCbCr 4:2:2 Interleaved UYVY",
170 .pixelformat
= V4L2_PIX_FMT_UYVY
,
177 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
178 .description
= "YCbCr 4:2:2 Interleaved YUYV",
179 .pixelformat
= V4L2_PIX_FMT_YUYV
,
186 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
187 .description
= "Y/CbCr 4:2:0 - Semi planar",
188 .pixelformat
= V4L2_PIX_FMT_NV12
,
195 * vpfe_lookup_pix_format()
196 * lookup an entry in the vpfe pix format table based on pix_format
198 static const struct vpfe_pixel_format
*vpfe_lookup_pix_format(u32 pix_format
)
202 for (i
= 0; i
< ARRAY_SIZE(vpfe_pix_fmts
); i
++) {
203 if (pix_format
== vpfe_pix_fmts
[i
].fmtdesc
.pixelformat
)
204 return &vpfe_pix_fmts
[i
];
210 * vpfe_register_ccdc_device. CCDC module calls this to
211 * register with vpfe capture
213 int vpfe_register_ccdc_device(struct ccdc_hw_device
*dev
)
216 printk(KERN_NOTICE
"vpfe_register_ccdc_device: %s\n", dev
->name
);
218 BUG_ON(!dev
->hw_ops
.open
);
219 BUG_ON(!dev
->hw_ops
.enable
);
220 BUG_ON(!dev
->hw_ops
.set_hw_if_params
);
221 BUG_ON(!dev
->hw_ops
.configure
);
222 BUG_ON(!dev
->hw_ops
.set_buftype
);
223 BUG_ON(!dev
->hw_ops
.get_buftype
);
224 BUG_ON(!dev
->hw_ops
.enum_pix
);
225 BUG_ON(!dev
->hw_ops
.set_frame_format
);
226 BUG_ON(!dev
->hw_ops
.get_frame_format
);
227 BUG_ON(!dev
->hw_ops
.get_pixel_format
);
228 BUG_ON(!dev
->hw_ops
.set_pixel_format
);
229 BUG_ON(!dev
->hw_ops
.set_params
);
230 BUG_ON(!dev
->hw_ops
.set_image_window
);
231 BUG_ON(!dev
->hw_ops
.get_image_window
);
232 BUG_ON(!dev
->hw_ops
.get_line_length
);
233 BUG_ON(!dev
->hw_ops
.setfbaddr
);
234 BUG_ON(!dev
->hw_ops
.getfid
);
236 mutex_lock(&ccdc_lock
);
237 if (NULL
== ccdc_cfg
) {
239 * TODO. Will this ever happen? if so, we need to fix it.
240 * Proabably we need to add the request to a linked list and
241 * walk through it during vpfe probe
243 printk(KERN_ERR
"vpfe capture not initialized\n");
248 if (strcmp(dev
->name
, ccdc_cfg
->name
)) {
249 /* ignore this ccdc */
255 printk(KERN_ERR
"ccdc already registered\n");
261 dev
->hw_ops
.set_ccdc_base(ccdc_cfg
->ccdc_addr
,
262 ccdc_cfg
->ccdc_addr_size
);
264 mutex_unlock(&ccdc_lock
);
267 EXPORT_SYMBOL(vpfe_register_ccdc_device
);
270 * vpfe_unregister_ccdc_device. CCDC module calls this to
271 * unregister with vpfe capture
273 void vpfe_unregister_ccdc_device(struct ccdc_hw_device
*dev
)
276 printk(KERN_ERR
"invalid ccdc device ptr\n");
280 printk(KERN_NOTICE
"vpfe_unregister_ccdc_device, dev->name = %s\n",
283 if (strcmp(dev
->name
, ccdc_cfg
->name
)) {
284 /* ignore this ccdc */
288 mutex_lock(&ccdc_lock
);
290 mutex_unlock(&ccdc_lock
);
293 EXPORT_SYMBOL(vpfe_unregister_ccdc_device
);
296 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
298 static int vpfe_get_ccdc_image_format(struct vpfe_device
*vpfe_dev
,
299 struct v4l2_format
*f
)
301 struct v4l2_rect image_win
;
302 enum ccdc_buftype buf_type
;
303 enum ccdc_frmfmt frm_fmt
;
305 memset(f
, 0, sizeof(*f
));
306 f
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
307 ccdc_dev
->hw_ops
.get_image_window(&image_win
);
308 f
->fmt
.pix
.width
= image_win
.width
;
309 f
->fmt
.pix
.height
= image_win
.height
;
310 f
->fmt
.pix
.bytesperline
= ccdc_dev
->hw_ops
.get_line_length();
311 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
*
313 buf_type
= ccdc_dev
->hw_ops
.get_buftype();
314 f
->fmt
.pix
.pixelformat
= ccdc_dev
->hw_ops
.get_pixel_format();
315 frm_fmt
= ccdc_dev
->hw_ops
.get_frame_format();
316 if (frm_fmt
== CCDC_FRMFMT_PROGRESSIVE
)
317 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
318 else if (frm_fmt
== CCDC_FRMFMT_INTERLACED
) {
319 if (buf_type
== CCDC_BUFTYPE_FLD_INTERLEAVED
)
320 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
321 else if (buf_type
== CCDC_BUFTYPE_FLD_SEPARATED
)
322 f
->fmt
.pix
.field
= V4L2_FIELD_SEQ_TB
;
324 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf_type\n");
328 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid frm_fmt\n");
335 * vpfe_config_ccdc_image_format()
336 * For a pix format, configure ccdc to setup the capture
338 static int vpfe_config_ccdc_image_format(struct vpfe_device
*vpfe_dev
)
340 enum ccdc_frmfmt frm_fmt
= CCDC_FRMFMT_INTERLACED
;
343 if (ccdc_dev
->hw_ops
.set_pixel_format(
344 vpfe_dev
->fmt
.fmt
.pix
.pixelformat
) < 0) {
345 v4l2_err(&vpfe_dev
->v4l2_dev
,
346 "couldn't set pix format in ccdc\n");
349 /* configure the image window */
350 ccdc_dev
->hw_ops
.set_image_window(&vpfe_dev
->crop
);
352 switch (vpfe_dev
->fmt
.fmt
.pix
.field
) {
353 case V4L2_FIELD_INTERLACED
:
354 /* do nothing, since it is default */
355 ret
= ccdc_dev
->hw_ops
.set_buftype(
356 CCDC_BUFTYPE_FLD_INTERLEAVED
);
358 case V4L2_FIELD_NONE
:
359 frm_fmt
= CCDC_FRMFMT_PROGRESSIVE
;
360 /* buffer type only applicable for interlaced scan */
362 case V4L2_FIELD_SEQ_TB
:
363 ret
= ccdc_dev
->hw_ops
.set_buftype(
364 CCDC_BUFTYPE_FLD_SEPARATED
);
370 /* set the frame format */
372 ret
= ccdc_dev
->hw_ops
.set_frame_format(frm_fmt
);
376 * vpfe_config_image_format()
377 * For a given standard, this functions sets up the default
378 * pix format & crop values in the vpfe device and ccdc. It first
379 * starts with defaults based values from the standard table.
380 * It then checks if sub device support g_fmt and then override the
381 * values based on that.Sets crop values to match with scan resolution
382 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
385 static int vpfe_config_image_format(struct vpfe_device
*vpfe_dev
,
386 const v4l2_std_id
*std_id
)
388 struct vpfe_subdev_info
*sdinfo
= vpfe_dev
->current_subdev
;
391 for (i
= 0; i
< ARRAY_SIZE(vpfe_standards
); i
++) {
392 if (vpfe_standards
[i
].std_id
& *std_id
) {
393 vpfe_dev
->std_info
.active_pixels
=
394 vpfe_standards
[i
].width
;
395 vpfe_dev
->std_info
.active_lines
=
396 vpfe_standards
[i
].height
;
397 vpfe_dev
->std_info
.frame_format
=
398 vpfe_standards
[i
].frame_format
;
399 vpfe_dev
->std_index
= i
;
404 if (i
== ARRAY_SIZE(vpfe_standards
)) {
405 v4l2_err(&vpfe_dev
->v4l2_dev
, "standard not supported\n");
409 vpfe_dev
->crop
.top
= 0;
410 vpfe_dev
->crop
.left
= 0;
411 vpfe_dev
->crop
.width
= vpfe_dev
->std_info
.active_pixels
;
412 vpfe_dev
->crop
.height
= vpfe_dev
->std_info
.active_lines
;
413 vpfe_dev
->fmt
.fmt
.pix
.width
= vpfe_dev
->crop
.width
;
414 vpfe_dev
->fmt
.fmt
.pix
.height
= vpfe_dev
->crop
.height
;
416 /* first field and frame format based on standard frame format */
417 if (vpfe_dev
->std_info
.frame_format
) {
418 vpfe_dev
->fmt
.fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
419 /* assume V4L2_PIX_FMT_UYVY as default */
420 vpfe_dev
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
422 vpfe_dev
->fmt
.fmt
.pix
.field
= V4L2_FIELD_NONE
;
423 /* assume V4L2_PIX_FMT_SBGGR8 */
424 vpfe_dev
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
427 /* if sub device supports g_fmt, override the defaults */
428 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
,
429 sdinfo
->grp_id
, video
, g_fmt
, &vpfe_dev
->fmt
);
431 if (ret
&& ret
!= -ENOIOCTLCMD
) {
432 v4l2_err(&vpfe_dev
->v4l2_dev
,
433 "error in getting g_fmt from sub device\n");
437 /* Sets the values in CCDC */
438 ret
= vpfe_config_ccdc_image_format(vpfe_dev
);
442 /* Update the values of sizeimage and bytesperline */
444 vpfe_dev
->fmt
.fmt
.pix
.bytesperline
=
445 ccdc_dev
->hw_ops
.get_line_length();
446 vpfe_dev
->fmt
.fmt
.pix
.sizeimage
=
447 vpfe_dev
->fmt
.fmt
.pix
.bytesperline
*
448 vpfe_dev
->fmt
.fmt
.pix
.height
;
453 static int vpfe_initialize_device(struct vpfe_device
*vpfe_dev
)
457 /* set first input of current subdevice as the current input */
458 vpfe_dev
->current_input
= 0;
460 /* set default standard */
461 vpfe_dev
->std_index
= 0;
463 /* Configure the default format information */
464 ret
= vpfe_config_image_format(vpfe_dev
,
465 &vpfe_standards
[vpfe_dev
->std_index
].std_id
);
469 /* now open the ccdc device to initialize it */
470 mutex_lock(&ccdc_lock
);
471 if (NULL
== ccdc_dev
) {
472 v4l2_err(&vpfe_dev
->v4l2_dev
, "ccdc device not registered\n");
477 if (!try_module_get(ccdc_dev
->owner
)) {
478 v4l2_err(&vpfe_dev
->v4l2_dev
, "Couldn't lock ccdc module\n");
482 ret
= ccdc_dev
->hw_ops
.open(vpfe_dev
->pdev
);
484 vpfe_dev
->initialized
= 1;
486 mutex_unlock(&ccdc_lock
);
491 * vpfe_open : It creates object of file handle structure and
492 * stores it in private_data member of filepointer
494 static int vpfe_open(struct file
*file
)
496 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
499 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_open\n");
501 if (!vpfe_dev
->cfg
->num_subdevs
) {
502 v4l2_err(&vpfe_dev
->v4l2_dev
, "No decoder registered\n");
506 /* Allocate memory for the file handle object */
507 fh
= kmalloc(sizeof(struct vpfe_fh
), GFP_KERNEL
);
509 v4l2_err(&vpfe_dev
->v4l2_dev
,
510 "unable to allocate memory for file handle object\n");
513 /* store pointer to fh in private_data member of file */
514 file
->private_data
= fh
;
515 fh
->vpfe_dev
= vpfe_dev
;
516 mutex_lock(&vpfe_dev
->lock
);
517 /* If decoder is not initialized. initialize it */
518 if (!vpfe_dev
->initialized
) {
519 if (vpfe_initialize_device(vpfe_dev
)) {
520 mutex_unlock(&vpfe_dev
->lock
);
524 /* Increment device usrs counter */
526 /* Set io_allowed member to false */
528 /* Initialize priority of this instance to default priority */
529 fh
->prio
= V4L2_PRIORITY_UNSET
;
530 v4l2_prio_open(&vpfe_dev
->prio
, &fh
->prio
);
531 mutex_unlock(&vpfe_dev
->lock
);
535 static void vpfe_schedule_next_buffer(struct vpfe_device
*vpfe_dev
)
539 vpfe_dev
->next_frm
= list_entry(vpfe_dev
->dma_queue
.next
,
540 struct videobuf_buffer
, queue
);
541 list_del(&vpfe_dev
->next_frm
->queue
);
542 vpfe_dev
->next_frm
->state
= VIDEOBUF_ACTIVE
;
543 addr
= videobuf_to_dma_contig(vpfe_dev
->next_frm
);
544 ccdc_dev
->hw_ops
.setfbaddr(addr
);
547 static void vpfe_process_buffer_complete(struct vpfe_device
*vpfe_dev
)
549 struct timeval timevalue
;
551 do_gettimeofday(&timevalue
);
552 vpfe_dev
->cur_frm
->ts
= timevalue
;
553 vpfe_dev
->cur_frm
->state
= VIDEOBUF_DONE
;
554 vpfe_dev
->cur_frm
->size
= vpfe_dev
->fmt
.fmt
.pix
.sizeimage
;
555 wake_up_interruptible(&vpfe_dev
->cur_frm
->done
);
556 vpfe_dev
->cur_frm
= vpfe_dev
->next_frm
;
560 static irqreturn_t
vpfe_isr(int irq
, void *dev_id
)
562 struct vpfe_device
*vpfe_dev
= dev_id
;
563 enum v4l2_field field
;
567 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "\nStarting vpfe_isr...\n");
568 field
= vpfe_dev
->fmt
.fmt
.pix
.field
;
570 /* if streaming not started, don't do anything */
571 if (!vpfe_dev
->started
)
574 /* only for 6446 this will be applicable */
575 if (NULL
!= ccdc_dev
->hw_ops
.reset
)
576 ccdc_dev
->hw_ops
.reset();
578 if (field
== V4L2_FIELD_NONE
) {
579 /* handle progressive frame capture */
580 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
581 "frame format is progressive...\n");
582 if (vpfe_dev
->cur_frm
!= vpfe_dev
->next_frm
)
583 vpfe_process_buffer_complete(vpfe_dev
);
587 /* interlaced or TB capture check which field we are in hardware */
588 fid
= ccdc_dev
->hw_ops
.getfid();
590 /* switch the software maintained field id */
591 vpfe_dev
->field_id
^= 1;
592 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "field id = %x:%x.\n",
593 fid
, vpfe_dev
->field_id
);
594 if (fid
== vpfe_dev
->field_id
) {
595 /* we are in-sync here,continue */
598 * One frame is just being captured. If the next frame
599 * is available, release the current frame and move on
601 if (vpfe_dev
->cur_frm
!= vpfe_dev
->next_frm
)
602 vpfe_process_buffer_complete(vpfe_dev
);
604 * based on whether the two fields are stored
605 * interleavely or separately in memory, reconfigure
606 * the CCDC memory address
608 if (field
== V4L2_FIELD_SEQ_TB
) {
610 videobuf_to_dma_contig(vpfe_dev
->cur_frm
);
611 addr
+= vpfe_dev
->field_off
;
612 ccdc_dev
->hw_ops
.setfbaddr(addr
);
617 * if one field is just being captured configure
618 * the next frame get the next frame from the empty
619 * queue if no frame is available hold on to the
622 spin_lock(&vpfe_dev
->dma_queue_lock
);
623 if (!list_empty(&vpfe_dev
->dma_queue
) &&
624 vpfe_dev
->cur_frm
== vpfe_dev
->next_frm
)
625 vpfe_schedule_next_buffer(vpfe_dev
);
626 spin_unlock(&vpfe_dev
->dma_queue_lock
);
627 } else if (fid
== 0) {
629 * out of sync. Recover from any hardware out-of-sync.
630 * May loose one frame
632 vpfe_dev
->field_id
= fid
;
637 /* vdint1_isr - isr handler for VINT1 interrupt */
638 static irqreturn_t
vdint1_isr(int irq
, void *dev_id
)
640 struct vpfe_device
*vpfe_dev
= dev_id
;
642 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "\nInside vdint1_isr...\n");
644 /* if streaming not started, don't do anything */
645 if (!vpfe_dev
->started
)
648 spin_lock(&vpfe_dev
->dma_queue_lock
);
649 if ((vpfe_dev
->fmt
.fmt
.pix
.field
== V4L2_FIELD_NONE
) &&
650 !list_empty(&vpfe_dev
->dma_queue
) &&
651 vpfe_dev
->cur_frm
== vpfe_dev
->next_frm
)
652 vpfe_schedule_next_buffer(vpfe_dev
);
653 spin_unlock(&vpfe_dev
->dma_queue_lock
);
657 static void vpfe_detach_irq(struct vpfe_device
*vpfe_dev
)
659 enum ccdc_frmfmt frame_format
;
661 frame_format
= ccdc_dev
->hw_ops
.get_frame_format();
662 if (frame_format
== CCDC_FRMFMT_PROGRESSIVE
)
663 free_irq(IRQ_VDINT1
, vpfe_dev
);
666 static int vpfe_attach_irq(struct vpfe_device
*vpfe_dev
)
668 enum ccdc_frmfmt frame_format
;
670 frame_format
= ccdc_dev
->hw_ops
.get_frame_format();
671 if (frame_format
== CCDC_FRMFMT_PROGRESSIVE
) {
672 return request_irq(vpfe_dev
->ccdc_irq1
, vdint1_isr
,
673 IRQF_DISABLED
, "vpfe_capture1",
679 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
680 static void vpfe_stop_ccdc_capture(struct vpfe_device
*vpfe_dev
)
682 vpfe_dev
->started
= 0;
683 ccdc_dev
->hw_ops
.enable(0);
684 if (ccdc_dev
->hw_ops
.enable_out_to_sdram
)
685 ccdc_dev
->hw_ops
.enable_out_to_sdram(0);
689 * vpfe_release : This function deletes buffer queue, frees the
690 * buffers and the vpfe file handle
692 static int vpfe_release(struct file
*file
)
694 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
695 struct vpfe_fh
*fh
= file
->private_data
;
696 struct vpfe_subdev_info
*sdinfo
;
699 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_release\n");
701 /* Get the device lock */
702 mutex_lock(&vpfe_dev
->lock
);
703 /* if this instance is doing IO */
704 if (fh
->io_allowed
) {
705 if (vpfe_dev
->started
) {
706 sdinfo
= vpfe_dev
->current_subdev
;
707 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
,
710 if (ret
&& (ret
!= -ENOIOCTLCMD
))
711 v4l2_err(&vpfe_dev
->v4l2_dev
,
712 "stream off failed in subdev\n");
713 vpfe_stop_ccdc_capture(vpfe_dev
);
714 vpfe_detach_irq(vpfe_dev
);
715 videobuf_streamoff(&vpfe_dev
->buffer_queue
);
717 vpfe_dev
->io_usrs
= 0;
718 vpfe_dev
->numbuffers
= config_params
.numbuffers
;
721 /* Decrement device usrs counter */
723 /* Close the priority */
724 v4l2_prio_close(&vpfe_dev
->prio
, &fh
->prio
);
725 /* If this is the last file handle */
726 if (!vpfe_dev
->usrs
) {
727 vpfe_dev
->initialized
= 0;
728 if (ccdc_dev
->hw_ops
.close
)
729 ccdc_dev
->hw_ops
.close(vpfe_dev
->pdev
);
730 module_put(ccdc_dev
->owner
);
732 mutex_unlock(&vpfe_dev
->lock
);
733 file
->private_data
= NULL
;
734 /* Free memory allocated to file handle object */
740 * vpfe_mmap : It is used to map kernel space buffers
743 static int vpfe_mmap(struct file
*file
, struct vm_area_struct
*vma
)
745 /* Get the device object and file handle object */
746 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
748 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_mmap\n");
750 return videobuf_mmap_mapper(&vpfe_dev
->buffer_queue
, vma
);
754 * vpfe_poll: It is used for select/poll system call
756 static unsigned int vpfe_poll(struct file
*file
, poll_table
*wait
)
758 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
760 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_poll\n");
762 if (vpfe_dev
->started
)
763 return videobuf_poll_stream(file
,
764 &vpfe_dev
->buffer_queue
, wait
);
768 /* vpfe capture driver file operations */
769 static const struct v4l2_file_operations vpfe_fops
= {
770 .owner
= THIS_MODULE
,
772 .release
= vpfe_release
,
773 .unlocked_ioctl
= video_ioctl2
,
779 * vpfe_check_format()
780 * This function adjust the input pixel format as per hardware
781 * capabilities and update the same in pixfmt.
782 * Following algorithm used :-
784 * If given pixformat is not in the vpfe list of pix formats or not
785 * supported by the hardware, current value of pixformat in the device
787 * If given field is not supported, then current field is used. If field
788 * is different from current, then it is matched with that from sub device.
789 * Minimum height is 2 lines for interlaced or tb field and 1 line for
790 * progressive. Maximum height is clamped to active active lines of scan
791 * Minimum width is 32 bytes in memory and width is clamped to active
793 * bytesperline is a multiple of 32.
795 static const struct vpfe_pixel_format
*
796 vpfe_check_format(struct vpfe_device
*vpfe_dev
,
797 struct v4l2_pix_format
*pixfmt
)
799 u32 min_height
= 1, min_width
= 32, max_width
, max_height
;
800 const struct vpfe_pixel_format
*vpfe_pix_fmt
;
804 vpfe_pix_fmt
= vpfe_lookup_pix_format(pixfmt
->pixelformat
);
805 if (NULL
== vpfe_pix_fmt
) {
807 * use current pixel format in the vpfe device. We
808 * will find this pix format in the table
810 pixfmt
->pixelformat
= vpfe_dev
->fmt
.fmt
.pix
.pixelformat
;
811 vpfe_pix_fmt
= vpfe_lookup_pix_format(pixfmt
->pixelformat
);
814 /* check if hw supports it */
817 while (ccdc_dev
->hw_ops
.enum_pix(&pix
, temp
) >= 0) {
818 if (vpfe_pix_fmt
->fmtdesc
.pixelformat
== pix
) {
826 /* use current pixel format */
827 pixfmt
->pixelformat
= vpfe_dev
->fmt
.fmt
.pix
.pixelformat
;
829 * Since this is currently used in the vpfe device, we
830 * will find this pix format in the table
832 vpfe_pix_fmt
= vpfe_lookup_pix_format(pixfmt
->pixelformat
);
835 /* check what field format is supported */
836 if (pixfmt
->field
== V4L2_FIELD_ANY
) {
837 /* if field is any, use current value as default */
838 pixfmt
->field
= vpfe_dev
->fmt
.fmt
.pix
.field
;
842 * if field is not same as current field in the vpfe device
843 * try matching the field with the sub device field
845 if (vpfe_dev
->fmt
.fmt
.pix
.field
!= pixfmt
->field
) {
847 * If field value is not in the supported fields, use current
848 * field used in the device as default
850 switch (pixfmt
->field
) {
851 case V4L2_FIELD_INTERLACED
:
852 case V4L2_FIELD_SEQ_TB
:
853 /* if sub device is supporting progressive, use that */
854 if (!vpfe_dev
->std_info
.frame_format
)
855 pixfmt
->field
= V4L2_FIELD_NONE
;
857 case V4L2_FIELD_NONE
:
858 if (vpfe_dev
->std_info
.frame_format
)
859 pixfmt
->field
= V4L2_FIELD_INTERLACED
;
863 /* use current field as default */
864 pixfmt
->field
= vpfe_dev
->fmt
.fmt
.pix
.field
;
869 /* Now adjust image resolutions supported */
870 if (pixfmt
->field
== V4L2_FIELD_INTERLACED
||
871 pixfmt
->field
== V4L2_FIELD_SEQ_TB
)
874 max_width
= vpfe_dev
->std_info
.active_pixels
;
875 max_height
= vpfe_dev
->std_info
.active_lines
;
876 min_width
/= vpfe_pix_fmt
->bpp
;
878 v4l2_info(&vpfe_dev
->v4l2_dev
, "width = %d, height = %d, bpp = %d\n",
879 pixfmt
->width
, pixfmt
->height
, vpfe_pix_fmt
->bpp
);
881 pixfmt
->width
= clamp((pixfmt
->width
), min_width
, max_width
);
882 pixfmt
->height
= clamp((pixfmt
->height
), min_height
, max_height
);
884 /* If interlaced, adjust height to be a multiple of 2 */
885 if (pixfmt
->field
== V4L2_FIELD_INTERLACED
)
886 pixfmt
->height
&= (~1);
888 * recalculate bytesperline and sizeimage since width
889 * and height might have changed
891 pixfmt
->bytesperline
= (((pixfmt
->width
* vpfe_pix_fmt
->bpp
) + 31)
893 if (pixfmt
->pixelformat
== V4L2_PIX_FMT_NV12
)
895 pixfmt
->bytesperline
* pixfmt
->height
+
896 ((pixfmt
->bytesperline
* pixfmt
->height
) >> 1);
898 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
;
900 v4l2_info(&vpfe_dev
->v4l2_dev
, "adjusted width = %d, height ="
901 " %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
902 pixfmt
->width
, pixfmt
->height
, vpfe_pix_fmt
->bpp
,
903 pixfmt
->bytesperline
, pixfmt
->sizeimage
);
907 static int vpfe_querycap(struct file
*file
, void *priv
,
908 struct v4l2_capability
*cap
)
910 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
912 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querycap\n");
914 cap
->version
= VPFE_CAPTURE_VERSION_CODE
;
915 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
916 strlcpy(cap
->driver
, CAPTURE_DRV_NAME
, sizeof(cap
->driver
));
917 strlcpy(cap
->bus_info
, "VPFE", sizeof(cap
->bus_info
));
918 strlcpy(cap
->card
, vpfe_dev
->cfg
->card_name
, sizeof(cap
->card
));
922 static int vpfe_g_fmt_vid_cap(struct file
*file
, void *priv
,
923 struct v4l2_format
*fmt
)
925 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
928 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_fmt_vid_cap\n");
929 /* Fill in the information about format */
930 *fmt
= vpfe_dev
->fmt
;
934 static int vpfe_enum_fmt_vid_cap(struct file
*file
, void *priv
,
935 struct v4l2_fmtdesc
*fmt
)
937 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
938 const struct vpfe_pixel_format
*pix_fmt
;
942 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_enum_fmt_vid_cap\n");
944 if (ccdc_dev
->hw_ops
.enum_pix(&pix
, fmt
->index
) < 0)
947 /* Fill in the information about format */
948 pix_fmt
= vpfe_lookup_pix_format(pix
);
949 if (NULL
!= pix_fmt
) {
950 temp_index
= fmt
->index
;
951 *fmt
= pix_fmt
->fmtdesc
;
952 fmt
->index
= temp_index
;
958 static int vpfe_s_fmt_vid_cap(struct file
*file
, void *priv
,
959 struct v4l2_format
*fmt
)
961 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
962 const struct vpfe_pixel_format
*pix_fmts
;
965 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_fmt_vid_cap\n");
967 /* If streaming is started, return error */
968 if (vpfe_dev
->started
) {
969 v4l2_err(&vpfe_dev
->v4l2_dev
, "Streaming is started\n");
973 /* Check for valid frame format */
974 pix_fmts
= vpfe_check_format(vpfe_dev
, &fmt
->fmt
.pix
);
976 if (NULL
== pix_fmts
)
979 /* store the pixel format in the device object */
980 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
984 /* First detach any IRQ if currently attached */
985 vpfe_detach_irq(vpfe_dev
);
986 vpfe_dev
->fmt
= *fmt
;
987 /* set image capture parameters in the ccdc */
988 ret
= vpfe_config_ccdc_image_format(vpfe_dev
);
989 mutex_unlock(&vpfe_dev
->lock
);
993 static int vpfe_try_fmt_vid_cap(struct file
*file
, void *priv
,
994 struct v4l2_format
*f
)
996 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
997 const struct vpfe_pixel_format
*pix_fmts
;
999 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_try_fmt_vid_cap\n");
1001 pix_fmts
= vpfe_check_format(vpfe_dev
, &f
->fmt
.pix
);
1002 if (NULL
== pix_fmts
)
1008 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1009 * given app input index
1011 static int vpfe_get_subdev_input_index(struct vpfe_device
*vpfe_dev
,
1013 int *subdev_input_index
,
1014 int app_input_index
)
1016 struct vpfe_config
*cfg
= vpfe_dev
->cfg
;
1017 struct vpfe_subdev_info
*sdinfo
;
1020 for (i
= 0; i
< cfg
->num_subdevs
; i
++) {
1021 sdinfo
= &cfg
->sub_devs
[i
];
1022 if (app_input_index
< (j
+ sdinfo
->num_inputs
)) {
1024 *subdev_input_index
= app_input_index
- j
;
1027 j
+= sdinfo
->num_inputs
;
1033 * vpfe_get_app_input - Get app input index for a given subdev input index
1034 * driver stores the input index of the current sub device and translate it
1035 * when application request the current input
1037 static int vpfe_get_app_input_index(struct vpfe_device
*vpfe_dev
,
1038 int *app_input_index
)
1040 struct vpfe_config
*cfg
= vpfe_dev
->cfg
;
1041 struct vpfe_subdev_info
*sdinfo
;
1044 for (i
= 0; i
< cfg
->num_subdevs
; i
++) {
1045 sdinfo
= &cfg
->sub_devs
[i
];
1046 if (!strcmp(sdinfo
->name
, vpfe_dev
->current_subdev
->name
)) {
1047 if (vpfe_dev
->current_input
>= sdinfo
->num_inputs
)
1049 *app_input_index
= j
+ vpfe_dev
->current_input
;
1052 j
+= sdinfo
->num_inputs
;
1057 static int vpfe_enum_input(struct file
*file
, void *priv
,
1058 struct v4l2_input
*inp
)
1060 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1061 struct vpfe_subdev_info
*sdinfo
;
1064 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_enum_input\n");
1066 if (vpfe_get_subdev_input_index(vpfe_dev
,
1070 v4l2_err(&vpfe_dev
->v4l2_dev
, "input information not found"
1071 " for the subdev\n");
1074 sdinfo
= &vpfe_dev
->cfg
->sub_devs
[subdev
];
1075 memcpy(inp
, &sdinfo
->inputs
[index
], sizeof(struct v4l2_input
));
1079 static int vpfe_g_input(struct file
*file
, void *priv
, unsigned int *index
)
1081 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1083 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_input\n");
1085 return vpfe_get_app_input_index(vpfe_dev
, index
);
1089 static int vpfe_s_input(struct file
*file
, void *priv
, unsigned int index
)
1091 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1092 struct vpfe_subdev_info
*sdinfo
;
1093 int subdev_index
, inp_index
;
1094 struct vpfe_route
*route
;
1095 u32 input
= 0, output
= 0;
1098 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_input\n");
1100 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1105 * If streaming is started return device busy
1108 if (vpfe_dev
->started
) {
1109 v4l2_err(&vpfe_dev
->v4l2_dev
, "Streaming is on\n");
1114 if (vpfe_get_subdev_input_index(vpfe_dev
,
1118 v4l2_err(&vpfe_dev
->v4l2_dev
, "invalid input index\n");
1122 sdinfo
= &vpfe_dev
->cfg
->sub_devs
[subdev_index
];
1123 route
= &sdinfo
->routes
[inp_index
];
1124 if (route
&& sdinfo
->can_route
) {
1125 input
= route
->input
;
1126 output
= route
->output
;
1129 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1130 video
, s_routing
, input
, output
, 0);
1133 v4l2_err(&vpfe_dev
->v4l2_dev
,
1134 "vpfe_doioctl:error in setting input in decoder\n");
1138 vpfe_dev
->current_subdev
= sdinfo
;
1139 vpfe_dev
->current_input
= index
;
1140 vpfe_dev
->std_index
= 0;
1142 /* set the bus/interface parameter for the sub device in ccdc */
1143 ret
= ccdc_dev
->hw_ops
.set_hw_if_params(&sdinfo
->ccdc_if_params
);
1147 /* set the default image parameters in the device */
1148 ret
= vpfe_config_image_format(vpfe_dev
,
1149 &vpfe_standards
[vpfe_dev
->std_index
].std_id
);
1151 mutex_unlock(&vpfe_dev
->lock
);
1155 static int vpfe_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1157 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1158 struct vpfe_subdev_info
*sdinfo
;
1161 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querystd\n");
1163 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1164 sdinfo
= vpfe_dev
->current_subdev
;
1167 /* Call querystd function of decoder device */
1168 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1169 video
, querystd
, std_id
);
1170 mutex_unlock(&vpfe_dev
->lock
);
1174 static int vpfe_s_std(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1176 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1177 struct vpfe_subdev_info
*sdinfo
;
1180 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_std\n");
1182 /* Call decoder driver function to set the standard */
1183 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1187 sdinfo
= vpfe_dev
->current_subdev
;
1188 /* If streaming is started, return device busy error */
1189 if (vpfe_dev
->started
) {
1190 v4l2_err(&vpfe_dev
->v4l2_dev
, "streaming is started\n");
1195 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1196 core
, s_std
, *std_id
);
1198 v4l2_err(&vpfe_dev
->v4l2_dev
, "Failed to set standard\n");
1201 ret
= vpfe_config_image_format(vpfe_dev
, std_id
);
1204 mutex_unlock(&vpfe_dev
->lock
);
1208 static int vpfe_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1210 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1212 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_std\n");
1214 *std_id
= vpfe_standards
[vpfe_dev
->std_index
].std_id
;
1218 * Videobuf operations
1220 static int vpfe_videobuf_setup(struct videobuf_queue
*vq
,
1221 unsigned int *count
,
1224 struct vpfe_fh
*fh
= vq
->priv_data
;
1225 struct vpfe_device
*vpfe_dev
= fh
->vpfe_dev
;
1227 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buffer_setup\n");
1228 *size
= config_params
.device_bufsize
;
1230 if (*count
< config_params
.min_numbuffers
)
1231 *count
= config_params
.min_numbuffers
;
1232 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
1233 "count=%d, size=%d\n", *count
, *size
);
1237 static int vpfe_videobuf_prepare(struct videobuf_queue
*vq
,
1238 struct videobuf_buffer
*vb
,
1239 enum v4l2_field field
)
1241 struct vpfe_fh
*fh
= vq
->priv_data
;
1242 struct vpfe_device
*vpfe_dev
= fh
->vpfe_dev
;
1244 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buffer_prepare\n");
1246 /* If buffer is not initialized, initialize it */
1247 if (VIDEOBUF_NEEDS_INIT
== vb
->state
) {
1248 vb
->width
= vpfe_dev
->fmt
.fmt
.pix
.width
;
1249 vb
->height
= vpfe_dev
->fmt
.fmt
.pix
.height
;
1250 vb
->size
= vpfe_dev
->fmt
.fmt
.pix
.sizeimage
;
1253 vb
->state
= VIDEOBUF_PREPARED
;
1257 static void vpfe_videobuf_queue(struct videobuf_queue
*vq
,
1258 struct videobuf_buffer
*vb
)
1260 /* Get the file handle object and device object */
1261 struct vpfe_fh
*fh
= vq
->priv_data
;
1262 struct vpfe_device
*vpfe_dev
= fh
->vpfe_dev
;
1263 unsigned long flags
;
1265 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buffer_queue\n");
1267 /* add the buffer to the DMA queue */
1268 spin_lock_irqsave(&vpfe_dev
->dma_queue_lock
, flags
);
1269 list_add_tail(&vb
->queue
, &vpfe_dev
->dma_queue
);
1270 spin_unlock_irqrestore(&vpfe_dev
->dma_queue_lock
, flags
);
1272 /* Change state of the buffer */
1273 vb
->state
= VIDEOBUF_QUEUED
;
1276 static void vpfe_videobuf_release(struct videobuf_queue
*vq
,
1277 struct videobuf_buffer
*vb
)
1279 struct vpfe_fh
*fh
= vq
->priv_data
;
1280 struct vpfe_device
*vpfe_dev
= fh
->vpfe_dev
;
1281 unsigned long flags
;
1283 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_videobuf_release\n");
1286 * We need to flush the buffer from the dma queue since
1287 * they are de-allocated
1289 spin_lock_irqsave(&vpfe_dev
->dma_queue_lock
, flags
);
1290 INIT_LIST_HEAD(&vpfe_dev
->dma_queue
);
1291 spin_unlock_irqrestore(&vpfe_dev
->dma_queue_lock
, flags
);
1292 videobuf_dma_contig_free(vq
, vb
);
1293 vb
->state
= VIDEOBUF_NEEDS_INIT
;
1296 static struct videobuf_queue_ops vpfe_videobuf_qops
= {
1297 .buf_setup
= vpfe_videobuf_setup
,
1298 .buf_prepare
= vpfe_videobuf_prepare
,
1299 .buf_queue
= vpfe_videobuf_queue
,
1300 .buf_release
= vpfe_videobuf_release
,
1304 * vpfe_reqbufs. currently support REQBUF only once opening
1307 static int vpfe_reqbufs(struct file
*file
, void *priv
,
1308 struct v4l2_requestbuffers
*req_buf
)
1310 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1311 struct vpfe_fh
*fh
= file
->private_data
;
1314 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_reqbufs\n");
1316 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= req_buf
->type
) {
1317 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buffer type\n");
1321 if (V4L2_MEMORY_USERPTR
== req_buf
->memory
) {
1322 /* we don't support user ptr IO */
1323 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_reqbufs:"
1324 " USERPTR IO not supported\n");
1328 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1332 if (vpfe_dev
->io_usrs
!= 0) {
1333 v4l2_err(&vpfe_dev
->v4l2_dev
, "Only one IO user allowed\n");
1338 vpfe_dev
->memory
= req_buf
->memory
;
1339 videobuf_queue_dma_contig_init(&vpfe_dev
->buffer_queue
,
1340 &vpfe_videobuf_qops
,
1344 vpfe_dev
->fmt
.fmt
.pix
.field
,
1345 sizeof(struct videobuf_buffer
),
1349 vpfe_dev
->io_usrs
= 1;
1350 INIT_LIST_HEAD(&vpfe_dev
->dma_queue
);
1351 ret
= videobuf_reqbufs(&vpfe_dev
->buffer_queue
, req_buf
);
1353 mutex_unlock(&vpfe_dev
->lock
);
1357 static int vpfe_querybuf(struct file
*file
, void *priv
,
1358 struct v4l2_buffer
*buf
)
1360 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1362 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querybuf\n");
1364 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf
->type
) {
1365 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1369 if (vpfe_dev
->memory
!= V4L2_MEMORY_MMAP
) {
1370 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid memory\n");
1373 /* Call videobuf_querybuf to get information */
1374 return videobuf_querybuf(&vpfe_dev
->buffer_queue
, buf
);
1377 static int vpfe_qbuf(struct file
*file
, void *priv
,
1378 struct v4l2_buffer
*p
)
1380 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1381 struct vpfe_fh
*fh
= file
->private_data
;
1383 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_qbuf\n");
1385 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= p
->type
) {
1386 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1391 * If this file handle is not allowed to do IO,
1394 if (!fh
->io_allowed
) {
1395 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1398 return videobuf_qbuf(&vpfe_dev
->buffer_queue
, p
);
1401 static int vpfe_dqbuf(struct file
*file
, void *priv
,
1402 struct v4l2_buffer
*buf
)
1404 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1406 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_dqbuf\n");
1408 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf
->type
) {
1409 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1412 return videobuf_dqbuf(&vpfe_dev
->buffer_queue
,
1413 buf
, file
->f_flags
& O_NONBLOCK
);
1417 * vpfe_calculate_offsets : This function calculates buffers offset
1418 * for top and bottom field
1420 static void vpfe_calculate_offsets(struct vpfe_device
*vpfe_dev
)
1422 struct v4l2_rect image_win
;
1424 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_calculate_offsets\n");
1426 ccdc_dev
->hw_ops
.get_image_window(&image_win
);
1427 vpfe_dev
->field_off
= image_win
.height
* image_win
.width
;
1430 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1431 static void vpfe_start_ccdc_capture(struct vpfe_device
*vpfe_dev
)
1433 ccdc_dev
->hw_ops
.enable(1);
1434 if (ccdc_dev
->hw_ops
.enable_out_to_sdram
)
1435 ccdc_dev
->hw_ops
.enable_out_to_sdram(1);
1436 vpfe_dev
->started
= 1;
1440 * vpfe_streamon. Assume the DMA queue is not empty.
1441 * application is expected to call QBUF before calling
1442 * this ioctl. If not, driver returns error
1444 static int vpfe_streamon(struct file
*file
, void *priv
,
1445 enum v4l2_buf_type buf_type
)
1447 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1448 struct vpfe_fh
*fh
= file
->private_data
;
1449 struct vpfe_subdev_info
*sdinfo
;
1453 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_streamon\n");
1455 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf_type
) {
1456 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1460 /* If file handle is not allowed IO, return error */
1461 if (!fh
->io_allowed
) {
1462 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1466 sdinfo
= vpfe_dev
->current_subdev
;
1467 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1468 video
, s_stream
, 1);
1470 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
1471 v4l2_err(&vpfe_dev
->v4l2_dev
, "stream on failed in subdev\n");
1475 /* If buffer queue is empty, return error */
1476 if (list_empty(&vpfe_dev
->buffer_queue
.stream
)) {
1477 v4l2_err(&vpfe_dev
->v4l2_dev
, "buffer queue is empty\n");
1481 /* Call videobuf_streamon to start streaming * in videobuf */
1482 ret
= videobuf_streamon(&vpfe_dev
->buffer_queue
);
1487 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1490 /* Get the next frame from the buffer queue */
1491 vpfe_dev
->next_frm
= list_entry(vpfe_dev
->dma_queue
.next
,
1492 struct videobuf_buffer
, queue
);
1493 vpfe_dev
->cur_frm
= vpfe_dev
->next_frm
;
1494 /* Remove buffer from the buffer queue */
1495 list_del(&vpfe_dev
->cur_frm
->queue
);
1496 /* Mark state of the current frame to active */
1497 vpfe_dev
->cur_frm
->state
= VIDEOBUF_ACTIVE
;
1498 /* Initialize field_id and started member */
1499 vpfe_dev
->field_id
= 0;
1500 addr
= videobuf_to_dma_contig(vpfe_dev
->cur_frm
);
1502 /* Calculate field offset */
1503 vpfe_calculate_offsets(vpfe_dev
);
1505 if (vpfe_attach_irq(vpfe_dev
) < 0) {
1506 v4l2_err(&vpfe_dev
->v4l2_dev
,
1507 "Error in attaching interrupt handle\n");
1511 if (ccdc_dev
->hw_ops
.configure() < 0) {
1512 v4l2_err(&vpfe_dev
->v4l2_dev
,
1513 "Error in configuring ccdc\n");
1517 ccdc_dev
->hw_ops
.setfbaddr((unsigned long)(addr
));
1518 vpfe_start_ccdc_capture(vpfe_dev
);
1519 mutex_unlock(&vpfe_dev
->lock
);
1522 mutex_unlock(&vpfe_dev
->lock
);
1524 ret
= videobuf_streamoff(&vpfe_dev
->buffer_queue
);
1528 static int vpfe_streamoff(struct file
*file
, void *priv
,
1529 enum v4l2_buf_type buf_type
)
1531 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1532 struct vpfe_fh
*fh
= file
->private_data
;
1533 struct vpfe_subdev_info
*sdinfo
;
1536 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_streamoff\n");
1538 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf_type
) {
1539 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1543 /* If io is allowed for this file handle, return error */
1544 if (!fh
->io_allowed
) {
1545 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1549 /* If streaming is not started, return error */
1550 if (!vpfe_dev
->started
) {
1551 v4l2_err(&vpfe_dev
->v4l2_dev
, "device started\n");
1555 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1559 vpfe_stop_ccdc_capture(vpfe_dev
);
1560 vpfe_detach_irq(vpfe_dev
);
1562 sdinfo
= vpfe_dev
->current_subdev
;
1563 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1564 video
, s_stream
, 0);
1566 if (ret
&& (ret
!= -ENOIOCTLCMD
))
1567 v4l2_err(&vpfe_dev
->v4l2_dev
, "stream off failed in subdev\n");
1568 ret
= videobuf_streamoff(&vpfe_dev
->buffer_queue
);
1569 mutex_unlock(&vpfe_dev
->lock
);
1573 static int vpfe_cropcap(struct file
*file
, void *priv
,
1574 struct v4l2_cropcap
*crop
)
1576 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1578 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_cropcap\n");
1580 if (vpfe_dev
->std_index
> ARRAY_SIZE(vpfe_standards
))
1583 memset(crop
, 0, sizeof(struct v4l2_cropcap
));
1584 crop
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1585 crop
->bounds
.width
= crop
->defrect
.width
=
1586 vpfe_standards
[vpfe_dev
->std_index
].width
;
1587 crop
->bounds
.height
= crop
->defrect
.height
=
1588 vpfe_standards
[vpfe_dev
->std_index
].height
;
1589 crop
->pixelaspect
= vpfe_standards
[vpfe_dev
->std_index
].pixelaspect
;
1593 static int vpfe_g_crop(struct file
*file
, void *priv
,
1594 struct v4l2_crop
*crop
)
1596 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1598 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_crop\n");
1600 crop
->c
= vpfe_dev
->crop
;
1604 static int vpfe_s_crop(struct file
*file
, void *priv
,
1605 struct v4l2_crop
*crop
)
1607 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1610 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_crop\n");
1612 if (vpfe_dev
->started
) {
1613 /* make sure streaming is not started */
1614 v4l2_err(&vpfe_dev
->v4l2_dev
,
1615 "Cannot change crop when streaming is ON\n");
1619 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1623 if (crop
->c
.top
< 0 || crop
->c
.left
< 0) {
1624 v4l2_err(&vpfe_dev
->v4l2_dev
,
1625 "doesn't support negative values for top & left\n");
1630 /* adjust the width to 16 pixel boundry */
1631 crop
->c
.width
= ((crop
->c
.width
+ 15) & ~0xf);
1633 /* make sure parameters are valid */
1634 if ((crop
->c
.left
+ crop
->c
.width
>
1635 vpfe_dev
->std_info
.active_pixels
) ||
1636 (crop
->c
.top
+ crop
->c
.height
>
1637 vpfe_dev
->std_info
.active_lines
)) {
1638 v4l2_err(&vpfe_dev
->v4l2_dev
, "Error in S_CROP params\n");
1642 ccdc_dev
->hw_ops
.set_image_window(&crop
->c
);
1643 vpfe_dev
->fmt
.fmt
.pix
.width
= crop
->c
.width
;
1644 vpfe_dev
->fmt
.fmt
.pix
.height
= crop
->c
.height
;
1645 vpfe_dev
->fmt
.fmt
.pix
.bytesperline
=
1646 ccdc_dev
->hw_ops
.get_line_length();
1647 vpfe_dev
->fmt
.fmt
.pix
.sizeimage
=
1648 vpfe_dev
->fmt
.fmt
.pix
.bytesperline
*
1649 vpfe_dev
->fmt
.fmt
.pix
.height
;
1650 vpfe_dev
->crop
= crop
->c
;
1652 mutex_unlock(&vpfe_dev
->lock
);
1657 static long vpfe_param_handler(struct file
*file
, void *priv
,
1658 int cmd
, void *param
)
1660 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1663 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_param_handler\n");
1665 if (vpfe_dev
->started
) {
1666 /* only allowed if streaming is not started */
1667 v4l2_err(&vpfe_dev
->v4l2_dev
, "device already started\n");
1671 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1676 case VPFE_CMD_S_CCDC_RAW_PARAMS
:
1677 v4l2_warn(&vpfe_dev
->v4l2_dev
,
1678 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
1679 ret
= ccdc_dev
->hw_ops
.set_params(param
);
1681 v4l2_err(&vpfe_dev
->v4l2_dev
,
1682 "Error in setting parameters in CCDC\n");
1685 if (vpfe_get_ccdc_image_format(vpfe_dev
, &vpfe_dev
->fmt
) < 0) {
1686 v4l2_err(&vpfe_dev
->v4l2_dev
,
1687 "Invalid image format at CCDC\n");
1695 mutex_unlock(&vpfe_dev
->lock
);
1700 /* vpfe capture ioctl operations */
1701 static const struct v4l2_ioctl_ops vpfe_ioctl_ops
= {
1702 .vidioc_querycap
= vpfe_querycap
,
1703 .vidioc_g_fmt_vid_cap
= vpfe_g_fmt_vid_cap
,
1704 .vidioc_enum_fmt_vid_cap
= vpfe_enum_fmt_vid_cap
,
1705 .vidioc_s_fmt_vid_cap
= vpfe_s_fmt_vid_cap
,
1706 .vidioc_try_fmt_vid_cap
= vpfe_try_fmt_vid_cap
,
1707 .vidioc_enum_input
= vpfe_enum_input
,
1708 .vidioc_g_input
= vpfe_g_input
,
1709 .vidioc_s_input
= vpfe_s_input
,
1710 .vidioc_querystd
= vpfe_querystd
,
1711 .vidioc_s_std
= vpfe_s_std
,
1712 .vidioc_g_std
= vpfe_g_std
,
1713 .vidioc_reqbufs
= vpfe_reqbufs
,
1714 .vidioc_querybuf
= vpfe_querybuf
,
1715 .vidioc_qbuf
= vpfe_qbuf
,
1716 .vidioc_dqbuf
= vpfe_dqbuf
,
1717 .vidioc_streamon
= vpfe_streamon
,
1718 .vidioc_streamoff
= vpfe_streamoff
,
1719 .vidioc_cropcap
= vpfe_cropcap
,
1720 .vidioc_g_crop
= vpfe_g_crop
,
1721 .vidioc_s_crop
= vpfe_s_crop
,
1722 .vidioc_default
= vpfe_param_handler
,
1725 static struct vpfe_device
*vpfe_initialize(void)
1727 struct vpfe_device
*vpfe_dev
;
1729 /* Default number of buffers should be 3 */
1730 if ((numbuffers
> 0) &&
1731 (numbuffers
< config_params
.min_numbuffers
))
1732 numbuffers
= config_params
.min_numbuffers
;
1735 * Set buffer size to min buffers size if invalid buffer size is
1738 if (bufsize
< config_params
.min_bufsize
)
1739 bufsize
= config_params
.min_bufsize
;
1741 config_params
.numbuffers
= numbuffers
;
1744 config_params
.device_bufsize
= bufsize
;
1746 /* Allocate memory for device objects */
1747 vpfe_dev
= kzalloc(sizeof(*vpfe_dev
), GFP_KERNEL
);
1752 static void vpfe_disable_clock(struct vpfe_device
*vpfe_dev
)
1754 struct vpfe_config
*vpfe_cfg
= vpfe_dev
->cfg
;
1756 clk_disable(vpfe_cfg
->vpssclk
);
1757 clk_put(vpfe_cfg
->vpssclk
);
1758 clk_disable(vpfe_cfg
->slaveclk
);
1759 clk_put(vpfe_cfg
->slaveclk
);
1760 v4l2_info(vpfe_dev
->pdev
->driver
,
1761 "vpfe vpss master & slave clocks disabled\n");
1764 static int vpfe_enable_clock(struct vpfe_device
*vpfe_dev
)
1766 struct vpfe_config
*vpfe_cfg
= vpfe_dev
->cfg
;
1769 vpfe_cfg
->vpssclk
= clk_get(vpfe_dev
->pdev
, "vpss_master");
1770 if (NULL
== vpfe_cfg
->vpssclk
) {
1771 v4l2_err(vpfe_dev
->pdev
->driver
, "No clock defined for"
1776 if (clk_enable(vpfe_cfg
->vpssclk
)) {
1777 v4l2_err(vpfe_dev
->pdev
->driver
,
1778 "vpfe vpss master clock not enabled\n");
1781 v4l2_info(vpfe_dev
->pdev
->driver
,
1782 "vpfe vpss master clock enabled\n");
1784 vpfe_cfg
->slaveclk
= clk_get(vpfe_dev
->pdev
, "vpss_slave");
1785 if (NULL
== vpfe_cfg
->slaveclk
) {
1786 v4l2_err(vpfe_dev
->pdev
->driver
,
1787 "No clock defined for vpss slave\n");
1791 if (clk_enable(vpfe_cfg
->slaveclk
)) {
1792 v4l2_err(vpfe_dev
->pdev
->driver
,
1793 "vpfe vpss slave clock not enabled\n");
1796 v4l2_info(vpfe_dev
->pdev
->driver
, "vpfe vpss slave clock enabled\n");
1799 if (vpfe_cfg
->vpssclk
)
1800 clk_put(vpfe_cfg
->vpssclk
);
1801 if (vpfe_cfg
->slaveclk
)
1802 clk_put(vpfe_cfg
->slaveclk
);
1808 * vpfe_probe : This function creates device entries by register
1809 * itself to the V4L2 driver and initializes fields of each
1812 static __init
int vpfe_probe(struct platform_device
*pdev
)
1814 struct vpfe_subdev_info
*sdinfo
;
1815 struct vpfe_config
*vpfe_cfg
;
1816 struct resource
*res1
;
1817 struct vpfe_device
*vpfe_dev
;
1818 struct i2c_adapter
*i2c_adap
;
1819 struct video_device
*vfd
;
1820 int ret
= -ENOMEM
, i
, j
;
1821 int num_subdevs
= 0;
1823 /* Get the pointer to the device object */
1824 vpfe_dev
= vpfe_initialize();
1827 v4l2_err(pdev
->dev
.driver
,
1828 "Failed to allocate memory for vpfe_dev\n");
1832 vpfe_dev
->pdev
= &pdev
->dev
;
1834 if (NULL
== pdev
->dev
.platform_data
) {
1835 v4l2_err(pdev
->dev
.driver
, "Unable to get vpfe config\n");
1837 goto probe_free_dev_mem
;
1840 vpfe_cfg
= pdev
->dev
.platform_data
;
1841 vpfe_dev
->cfg
= vpfe_cfg
;
1842 if (NULL
== vpfe_cfg
->ccdc
||
1843 NULL
== vpfe_cfg
->card_name
||
1844 NULL
== vpfe_cfg
->sub_devs
) {
1845 v4l2_err(pdev
->dev
.driver
, "null ptr in vpfe_cfg\n");
1847 goto probe_free_dev_mem
;
1850 /* enable vpss clocks */
1851 ret
= vpfe_enable_clock(vpfe_dev
);
1853 goto probe_free_dev_mem
;
1855 mutex_lock(&ccdc_lock
);
1856 /* Allocate memory for ccdc configuration */
1857 ccdc_cfg
= kmalloc(sizeof(struct ccdc_config
), GFP_KERNEL
);
1858 if (NULL
== ccdc_cfg
) {
1859 v4l2_err(pdev
->dev
.driver
,
1860 "Memory allocation failed for ccdc_cfg\n");
1861 goto probe_disable_clock
;
1864 strncpy(ccdc_cfg
->name
, vpfe_cfg
->ccdc
, 32);
1865 /* Get VINT0 irq resource */
1866 res1
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1868 v4l2_err(pdev
->dev
.driver
,
1869 "Unable to get interrupt for VINT0\n");
1871 goto probe_disable_clock
;
1873 vpfe_dev
->ccdc_irq0
= res1
->start
;
1875 /* Get VINT1 irq resource */
1876 res1
= platform_get_resource(pdev
,
1879 v4l2_err(pdev
->dev
.driver
,
1880 "Unable to get interrupt for VINT1\n");
1882 goto probe_disable_clock
;
1884 vpfe_dev
->ccdc_irq1
= res1
->start
;
1886 /* Get address base of CCDC */
1887 res1
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1889 v4l2_err(pdev
->dev
.driver
,
1890 "Unable to get register address map\n");
1892 goto probe_disable_clock
;
1895 ccdc_cfg
->ccdc_addr_size
= res1
->end
- res1
->start
+ 1;
1896 if (!request_mem_region(res1
->start
, ccdc_cfg
->ccdc_addr_size
,
1897 pdev
->dev
.driver
->name
)) {
1898 v4l2_err(pdev
->dev
.driver
,
1899 "Failed request_mem_region for ccdc base\n");
1901 goto probe_disable_clock
;
1903 ccdc_cfg
->ccdc_addr
= ioremap_nocache(res1
->start
,
1904 ccdc_cfg
->ccdc_addr_size
);
1905 if (!ccdc_cfg
->ccdc_addr
) {
1906 v4l2_err(pdev
->dev
.driver
, "Unable to ioremap ccdc addr\n");
1908 goto probe_out_release_mem1
;
1911 ret
= request_irq(vpfe_dev
->ccdc_irq0
, vpfe_isr
, IRQF_DISABLED
,
1912 "vpfe_capture0", vpfe_dev
);
1915 v4l2_err(pdev
->dev
.driver
, "Unable to request interrupt\n");
1916 goto probe_out_unmap1
;
1919 /* Allocate memory for video device */
1920 vfd
= video_device_alloc();
1923 v4l2_err(pdev
->dev
.driver
,
1924 "Unable to alloc video device\n");
1925 goto probe_out_release_irq
;
1928 /* Initialize field of video device */
1929 vfd
->release
= video_device_release
;
1930 vfd
->fops
= &vpfe_fops
;
1931 vfd
->ioctl_ops
= &vpfe_ioctl_ops
;
1934 vfd
->current_norm
= V4L2_STD_PAL
;
1935 vfd
->v4l2_dev
= &vpfe_dev
->v4l2_dev
;
1936 snprintf(vfd
->name
, sizeof(vfd
->name
),
1939 (VPFE_CAPTURE_VERSION_CODE
>> 16) & 0xff,
1940 (VPFE_CAPTURE_VERSION_CODE
>> 8) & 0xff,
1941 (VPFE_CAPTURE_VERSION_CODE
) & 0xff);
1942 /* Set video_dev to the video device */
1943 vpfe_dev
->video_dev
= vfd
;
1945 ret
= v4l2_device_register(&pdev
->dev
, &vpfe_dev
->v4l2_dev
);
1947 v4l2_err(pdev
->dev
.driver
,
1948 "Unable to register v4l2 device.\n");
1949 goto probe_out_video_release
;
1951 v4l2_info(&vpfe_dev
->v4l2_dev
, "v4l2 device registered\n");
1952 spin_lock_init(&vpfe_dev
->irqlock
);
1953 spin_lock_init(&vpfe_dev
->dma_queue_lock
);
1954 mutex_init(&vpfe_dev
->lock
);
1956 /* Initialize field of the device objects */
1957 vpfe_dev
->numbuffers
= config_params
.numbuffers
;
1959 /* Initialize prio member of device object */
1960 v4l2_prio_init(&vpfe_dev
->prio
);
1961 /* register video device */
1962 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
1963 "trying to register vpfe device.\n");
1964 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
1965 "video_dev=%x\n", (int)&vpfe_dev
->video_dev
);
1966 vpfe_dev
->fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1967 ret
= video_register_device(vpfe_dev
->video_dev
,
1968 VFL_TYPE_GRABBER
, -1);
1971 v4l2_err(pdev
->dev
.driver
,
1972 "Unable to register video device.\n");
1973 goto probe_out_v4l2_unregister
;
1976 v4l2_info(&vpfe_dev
->v4l2_dev
, "video device registered\n");
1977 /* set the driver data in platform device */
1978 platform_set_drvdata(pdev
, vpfe_dev
);
1979 /* set driver private data */
1980 video_set_drvdata(vpfe_dev
->video_dev
, vpfe_dev
);
1981 i2c_adap
= i2c_get_adapter(1);
1982 vpfe_cfg
= pdev
->dev
.platform_data
;
1983 num_subdevs
= vpfe_cfg
->num_subdevs
;
1984 vpfe_dev
->sd
= kmalloc(sizeof(struct v4l2_subdev
*) * num_subdevs
,
1986 if (NULL
== vpfe_dev
->sd
) {
1987 v4l2_err(&vpfe_dev
->v4l2_dev
,
1988 "unable to allocate memory for subdevice pointers\n");
1990 goto probe_out_video_unregister
;
1993 for (i
= 0; i
< num_subdevs
; i
++) {
1994 struct v4l2_input
*inps
;
1996 sdinfo
= &vpfe_cfg
->sub_devs
[i
];
1998 /* Load up the subdevice */
2000 v4l2_i2c_new_subdev_board(&vpfe_dev
->v4l2_dev
,
2003 &sdinfo
->board_info
,
2005 if (vpfe_dev
->sd
[i
]) {
2006 v4l2_info(&vpfe_dev
->v4l2_dev
,
2007 "v4l2 sub device %s registered\n",
2009 vpfe_dev
->sd
[i
]->grp_id
= sdinfo
->grp_id
;
2010 /* update tvnorms from the sub devices */
2011 for (j
= 0; j
< sdinfo
->num_inputs
; j
++) {
2012 inps
= &sdinfo
->inputs
[j
];
2013 vfd
->tvnorms
|= inps
->std
;
2016 v4l2_info(&vpfe_dev
->v4l2_dev
,
2017 "v4l2 sub device %s register fails\n",
2023 /* set first sub device as current one */
2024 vpfe_dev
->current_subdev
= &vpfe_cfg
->sub_devs
[0];
2026 /* We have at least one sub device to work with */
2027 mutex_unlock(&ccdc_lock
);
2031 kfree(vpfe_dev
->sd
);
2032 probe_out_video_unregister
:
2033 video_unregister_device(vpfe_dev
->video_dev
);
2034 probe_out_v4l2_unregister
:
2035 v4l2_device_unregister(&vpfe_dev
->v4l2_dev
);
2036 probe_out_video_release
:
2037 if (vpfe_dev
->video_dev
->minor
== -1)
2038 video_device_release(vpfe_dev
->video_dev
);
2039 probe_out_release_irq
:
2040 free_irq(vpfe_dev
->ccdc_irq0
, vpfe_dev
);
2042 iounmap(ccdc_cfg
->ccdc_addr
);
2043 probe_out_release_mem1
:
2044 release_mem_region(res1
->start
, res1
->end
- res1
->start
+ 1);
2045 probe_disable_clock
:
2046 vpfe_disable_clock(vpfe_dev
);
2047 mutex_unlock(&ccdc_lock
);
2055 * vpfe_remove : It un-register device from V4L2 driver
2057 static int vpfe_remove(struct platform_device
*pdev
)
2059 struct vpfe_device
*vpfe_dev
= platform_get_drvdata(pdev
);
2060 struct resource
*res
;
2062 v4l2_info(pdev
->dev
.driver
, "vpfe_remove\n");
2064 free_irq(vpfe_dev
->ccdc_irq0
, vpfe_dev
);
2065 kfree(vpfe_dev
->sd
);
2066 v4l2_device_unregister(&vpfe_dev
->v4l2_dev
);
2067 video_unregister_device(vpfe_dev
->video_dev
);
2068 mutex_lock(&ccdc_lock
);
2069 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2070 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
2071 iounmap(ccdc_cfg
->ccdc_addr
);
2072 mutex_unlock(&ccdc_lock
);
2073 vpfe_disable_clock(vpfe_dev
);
2080 vpfe_suspend(struct device
*dev
)
2082 /* add suspend code here later */
2087 vpfe_resume(struct device
*dev
)
2089 /* add resume code here later */
2093 static struct dev_pm_ops vpfe_dev_pm_ops
= {
2094 .suspend
= vpfe_suspend
,
2095 .resume
= vpfe_resume
,
2098 static struct platform_driver vpfe_driver
= {
2100 .name
= CAPTURE_DRV_NAME
,
2101 .owner
= THIS_MODULE
,
2102 .pm
= &vpfe_dev_pm_ops
,
2104 .probe
= vpfe_probe
,
2105 .remove
= __devexit_p(vpfe_remove
),
2108 static __init
int vpfe_init(void)
2110 printk(KERN_NOTICE
"vpfe_init\n");
2111 /* Register driver to the kernel */
2112 return platform_driver_register(&vpfe_driver
);
2116 * vpfe_cleanup : This function un-registers device driver
2118 static void vpfe_cleanup(void)
2120 platform_driver_unregister(&vpfe_driver
);
2123 module_init(vpfe_init
);
2124 module_exit(vpfe_cleanup
);