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(vpfe_dev
->ccdc_irq1
, 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
);
1416 static int vpfe_queryctrl(struct file
*file
, void *priv
,
1417 struct v4l2_queryctrl
*qctrl
)
1419 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1420 struct vpfe_subdev_info
*sdinfo
;
1422 sdinfo
= vpfe_dev
->current_subdev
;
1424 return v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1425 core
, queryctrl
, qctrl
);
1429 static int vpfe_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*ctrl
)
1431 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1432 struct vpfe_subdev_info
*sdinfo
;
1434 sdinfo
= vpfe_dev
->current_subdev
;
1436 return v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1437 core
, g_ctrl
, ctrl
);
1440 static int vpfe_s_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*ctrl
)
1442 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1443 struct vpfe_subdev_info
*sdinfo
;
1445 sdinfo
= vpfe_dev
->current_subdev
;
1447 return v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1448 core
, s_ctrl
, ctrl
);
1452 * vpfe_calculate_offsets : This function calculates buffers offset
1453 * for top and bottom field
1455 static void vpfe_calculate_offsets(struct vpfe_device
*vpfe_dev
)
1457 struct v4l2_rect image_win
;
1459 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_calculate_offsets\n");
1461 ccdc_dev
->hw_ops
.get_image_window(&image_win
);
1462 vpfe_dev
->field_off
= image_win
.height
* image_win
.width
;
1465 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1466 static void vpfe_start_ccdc_capture(struct vpfe_device
*vpfe_dev
)
1468 ccdc_dev
->hw_ops
.enable(1);
1469 if (ccdc_dev
->hw_ops
.enable_out_to_sdram
)
1470 ccdc_dev
->hw_ops
.enable_out_to_sdram(1);
1471 vpfe_dev
->started
= 1;
1475 * vpfe_streamon. Assume the DMA queue is not empty.
1476 * application is expected to call QBUF before calling
1477 * this ioctl. If not, driver returns error
1479 static int vpfe_streamon(struct file
*file
, void *priv
,
1480 enum v4l2_buf_type buf_type
)
1482 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1483 struct vpfe_fh
*fh
= file
->private_data
;
1484 struct vpfe_subdev_info
*sdinfo
;
1488 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_streamon\n");
1490 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf_type
) {
1491 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1495 /* If file handle is not allowed IO, return error */
1496 if (!fh
->io_allowed
) {
1497 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1501 sdinfo
= vpfe_dev
->current_subdev
;
1502 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1503 video
, s_stream
, 1);
1505 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
1506 v4l2_err(&vpfe_dev
->v4l2_dev
, "stream on failed in subdev\n");
1510 /* If buffer queue is empty, return error */
1511 if (list_empty(&vpfe_dev
->buffer_queue
.stream
)) {
1512 v4l2_err(&vpfe_dev
->v4l2_dev
, "buffer queue is empty\n");
1516 /* Call videobuf_streamon to start streaming * in videobuf */
1517 ret
= videobuf_streamon(&vpfe_dev
->buffer_queue
);
1522 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1525 /* Get the next frame from the buffer queue */
1526 vpfe_dev
->next_frm
= list_entry(vpfe_dev
->dma_queue
.next
,
1527 struct videobuf_buffer
, queue
);
1528 vpfe_dev
->cur_frm
= vpfe_dev
->next_frm
;
1529 /* Remove buffer from the buffer queue */
1530 list_del(&vpfe_dev
->cur_frm
->queue
);
1531 /* Mark state of the current frame to active */
1532 vpfe_dev
->cur_frm
->state
= VIDEOBUF_ACTIVE
;
1533 /* Initialize field_id and started member */
1534 vpfe_dev
->field_id
= 0;
1535 addr
= videobuf_to_dma_contig(vpfe_dev
->cur_frm
);
1537 /* Calculate field offset */
1538 vpfe_calculate_offsets(vpfe_dev
);
1540 if (vpfe_attach_irq(vpfe_dev
) < 0) {
1541 v4l2_err(&vpfe_dev
->v4l2_dev
,
1542 "Error in attaching interrupt handle\n");
1546 if (ccdc_dev
->hw_ops
.configure() < 0) {
1547 v4l2_err(&vpfe_dev
->v4l2_dev
,
1548 "Error in configuring ccdc\n");
1552 ccdc_dev
->hw_ops
.setfbaddr((unsigned long)(addr
));
1553 vpfe_start_ccdc_capture(vpfe_dev
);
1554 mutex_unlock(&vpfe_dev
->lock
);
1557 mutex_unlock(&vpfe_dev
->lock
);
1559 ret
= videobuf_streamoff(&vpfe_dev
->buffer_queue
);
1563 static int vpfe_streamoff(struct file
*file
, void *priv
,
1564 enum v4l2_buf_type buf_type
)
1566 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1567 struct vpfe_fh
*fh
= file
->private_data
;
1568 struct vpfe_subdev_info
*sdinfo
;
1571 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_streamoff\n");
1573 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= buf_type
) {
1574 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1578 /* If io is allowed for this file handle, return error */
1579 if (!fh
->io_allowed
) {
1580 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1584 /* If streaming is not started, return error */
1585 if (!vpfe_dev
->started
) {
1586 v4l2_err(&vpfe_dev
->v4l2_dev
, "device started\n");
1590 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1594 vpfe_stop_ccdc_capture(vpfe_dev
);
1595 vpfe_detach_irq(vpfe_dev
);
1597 sdinfo
= vpfe_dev
->current_subdev
;
1598 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
1599 video
, s_stream
, 0);
1601 if (ret
&& (ret
!= -ENOIOCTLCMD
))
1602 v4l2_err(&vpfe_dev
->v4l2_dev
, "stream off failed in subdev\n");
1603 ret
= videobuf_streamoff(&vpfe_dev
->buffer_queue
);
1604 mutex_unlock(&vpfe_dev
->lock
);
1608 static int vpfe_cropcap(struct file
*file
, void *priv
,
1609 struct v4l2_cropcap
*crop
)
1611 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1613 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_cropcap\n");
1615 if (vpfe_dev
->std_index
>= ARRAY_SIZE(vpfe_standards
))
1618 memset(crop
, 0, sizeof(struct v4l2_cropcap
));
1619 crop
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1620 crop
->bounds
.width
= crop
->defrect
.width
=
1621 vpfe_standards
[vpfe_dev
->std_index
].width
;
1622 crop
->bounds
.height
= crop
->defrect
.height
=
1623 vpfe_standards
[vpfe_dev
->std_index
].height
;
1624 crop
->pixelaspect
= vpfe_standards
[vpfe_dev
->std_index
].pixelaspect
;
1628 static int vpfe_g_crop(struct file
*file
, void *priv
,
1629 struct v4l2_crop
*crop
)
1631 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1633 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_crop\n");
1635 crop
->c
= vpfe_dev
->crop
;
1639 static int vpfe_s_crop(struct file
*file
, void *priv
,
1640 struct v4l2_crop
*crop
)
1642 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1645 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_crop\n");
1647 if (vpfe_dev
->started
) {
1648 /* make sure streaming is not started */
1649 v4l2_err(&vpfe_dev
->v4l2_dev
,
1650 "Cannot change crop when streaming is ON\n");
1654 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1658 if (crop
->c
.top
< 0 || crop
->c
.left
< 0) {
1659 v4l2_err(&vpfe_dev
->v4l2_dev
,
1660 "doesn't support negative values for top & left\n");
1665 /* adjust the width to 16 pixel boundry */
1666 crop
->c
.width
= ((crop
->c
.width
+ 15) & ~0xf);
1668 /* make sure parameters are valid */
1669 if ((crop
->c
.left
+ crop
->c
.width
>
1670 vpfe_dev
->std_info
.active_pixels
) ||
1671 (crop
->c
.top
+ crop
->c
.height
>
1672 vpfe_dev
->std_info
.active_lines
)) {
1673 v4l2_err(&vpfe_dev
->v4l2_dev
, "Error in S_CROP params\n");
1677 ccdc_dev
->hw_ops
.set_image_window(&crop
->c
);
1678 vpfe_dev
->fmt
.fmt
.pix
.width
= crop
->c
.width
;
1679 vpfe_dev
->fmt
.fmt
.pix
.height
= crop
->c
.height
;
1680 vpfe_dev
->fmt
.fmt
.pix
.bytesperline
=
1681 ccdc_dev
->hw_ops
.get_line_length();
1682 vpfe_dev
->fmt
.fmt
.pix
.sizeimage
=
1683 vpfe_dev
->fmt
.fmt
.pix
.bytesperline
*
1684 vpfe_dev
->fmt
.fmt
.pix
.height
;
1685 vpfe_dev
->crop
= crop
->c
;
1687 mutex_unlock(&vpfe_dev
->lock
);
1692 static long vpfe_param_handler(struct file
*file
, void *priv
,
1693 int cmd
, void *param
)
1695 struct vpfe_device
*vpfe_dev
= video_drvdata(file
);
1698 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_param_handler\n");
1700 if (vpfe_dev
->started
) {
1701 /* only allowed if streaming is not started */
1702 v4l2_err(&vpfe_dev
->v4l2_dev
, "device already started\n");
1706 ret
= mutex_lock_interruptible(&vpfe_dev
->lock
);
1711 case VPFE_CMD_S_CCDC_RAW_PARAMS
:
1712 v4l2_warn(&vpfe_dev
->v4l2_dev
,
1713 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
1714 ret
= ccdc_dev
->hw_ops
.set_params(param
);
1716 v4l2_err(&vpfe_dev
->v4l2_dev
,
1717 "Error in setting parameters in CCDC\n");
1720 if (vpfe_get_ccdc_image_format(vpfe_dev
, &vpfe_dev
->fmt
) < 0) {
1721 v4l2_err(&vpfe_dev
->v4l2_dev
,
1722 "Invalid image format at CCDC\n");
1730 mutex_unlock(&vpfe_dev
->lock
);
1735 /* vpfe capture ioctl operations */
1736 static const struct v4l2_ioctl_ops vpfe_ioctl_ops
= {
1737 .vidioc_querycap
= vpfe_querycap
,
1738 .vidioc_g_fmt_vid_cap
= vpfe_g_fmt_vid_cap
,
1739 .vidioc_enum_fmt_vid_cap
= vpfe_enum_fmt_vid_cap
,
1740 .vidioc_s_fmt_vid_cap
= vpfe_s_fmt_vid_cap
,
1741 .vidioc_try_fmt_vid_cap
= vpfe_try_fmt_vid_cap
,
1742 .vidioc_enum_input
= vpfe_enum_input
,
1743 .vidioc_g_input
= vpfe_g_input
,
1744 .vidioc_s_input
= vpfe_s_input
,
1745 .vidioc_querystd
= vpfe_querystd
,
1746 .vidioc_s_std
= vpfe_s_std
,
1747 .vidioc_g_std
= vpfe_g_std
,
1748 .vidioc_queryctrl
= vpfe_queryctrl
,
1749 .vidioc_g_ctrl
= vpfe_g_ctrl
,
1750 .vidioc_s_ctrl
= vpfe_s_ctrl
,
1751 .vidioc_reqbufs
= vpfe_reqbufs
,
1752 .vidioc_querybuf
= vpfe_querybuf
,
1753 .vidioc_qbuf
= vpfe_qbuf
,
1754 .vidioc_dqbuf
= vpfe_dqbuf
,
1755 .vidioc_streamon
= vpfe_streamon
,
1756 .vidioc_streamoff
= vpfe_streamoff
,
1757 .vidioc_cropcap
= vpfe_cropcap
,
1758 .vidioc_g_crop
= vpfe_g_crop
,
1759 .vidioc_s_crop
= vpfe_s_crop
,
1760 .vidioc_default
= vpfe_param_handler
,
1763 static struct vpfe_device
*vpfe_initialize(void)
1765 struct vpfe_device
*vpfe_dev
;
1767 /* Default number of buffers should be 3 */
1768 if ((numbuffers
> 0) &&
1769 (numbuffers
< config_params
.min_numbuffers
))
1770 numbuffers
= config_params
.min_numbuffers
;
1773 * Set buffer size to min buffers size if invalid buffer size is
1776 if (bufsize
< config_params
.min_bufsize
)
1777 bufsize
= config_params
.min_bufsize
;
1779 config_params
.numbuffers
= numbuffers
;
1782 config_params
.device_bufsize
= bufsize
;
1784 /* Allocate memory for device objects */
1785 vpfe_dev
= kzalloc(sizeof(*vpfe_dev
), GFP_KERNEL
);
1790 static void vpfe_disable_clock(struct vpfe_device
*vpfe_dev
)
1792 struct vpfe_config
*vpfe_cfg
= vpfe_dev
->cfg
;
1794 clk_disable(vpfe_cfg
->vpssclk
);
1795 clk_put(vpfe_cfg
->vpssclk
);
1796 clk_disable(vpfe_cfg
->slaveclk
);
1797 clk_put(vpfe_cfg
->slaveclk
);
1798 v4l2_info(vpfe_dev
->pdev
->driver
,
1799 "vpfe vpss master & slave clocks disabled\n");
1802 static int vpfe_enable_clock(struct vpfe_device
*vpfe_dev
)
1804 struct vpfe_config
*vpfe_cfg
= vpfe_dev
->cfg
;
1807 vpfe_cfg
->vpssclk
= clk_get(vpfe_dev
->pdev
, "vpss_master");
1808 if (NULL
== vpfe_cfg
->vpssclk
) {
1809 v4l2_err(vpfe_dev
->pdev
->driver
, "No clock defined for"
1814 if (clk_enable(vpfe_cfg
->vpssclk
)) {
1815 v4l2_err(vpfe_dev
->pdev
->driver
,
1816 "vpfe vpss master clock not enabled\n");
1819 v4l2_info(vpfe_dev
->pdev
->driver
,
1820 "vpfe vpss master clock enabled\n");
1822 vpfe_cfg
->slaveclk
= clk_get(vpfe_dev
->pdev
, "vpss_slave");
1823 if (NULL
== vpfe_cfg
->slaveclk
) {
1824 v4l2_err(vpfe_dev
->pdev
->driver
,
1825 "No clock defined for vpss slave\n");
1829 if (clk_enable(vpfe_cfg
->slaveclk
)) {
1830 v4l2_err(vpfe_dev
->pdev
->driver
,
1831 "vpfe vpss slave clock not enabled\n");
1834 v4l2_info(vpfe_dev
->pdev
->driver
, "vpfe vpss slave clock enabled\n");
1837 if (vpfe_cfg
->vpssclk
)
1838 clk_put(vpfe_cfg
->vpssclk
);
1839 if (vpfe_cfg
->slaveclk
)
1840 clk_put(vpfe_cfg
->slaveclk
);
1846 * vpfe_probe : This function creates device entries by register
1847 * itself to the V4L2 driver and initializes fields of each
1850 static __init
int vpfe_probe(struct platform_device
*pdev
)
1852 struct vpfe_subdev_info
*sdinfo
;
1853 struct vpfe_config
*vpfe_cfg
;
1854 struct resource
*res1
;
1855 struct vpfe_device
*vpfe_dev
;
1856 struct i2c_adapter
*i2c_adap
;
1857 struct video_device
*vfd
;
1858 int ret
= -ENOMEM
, i
, j
;
1859 int num_subdevs
= 0;
1861 /* Get the pointer to the device object */
1862 vpfe_dev
= vpfe_initialize();
1865 v4l2_err(pdev
->dev
.driver
,
1866 "Failed to allocate memory for vpfe_dev\n");
1870 vpfe_dev
->pdev
= &pdev
->dev
;
1872 if (NULL
== pdev
->dev
.platform_data
) {
1873 v4l2_err(pdev
->dev
.driver
, "Unable to get vpfe config\n");
1875 goto probe_free_dev_mem
;
1878 vpfe_cfg
= pdev
->dev
.platform_data
;
1879 vpfe_dev
->cfg
= vpfe_cfg
;
1880 if (NULL
== vpfe_cfg
->ccdc
||
1881 NULL
== vpfe_cfg
->card_name
||
1882 NULL
== vpfe_cfg
->sub_devs
) {
1883 v4l2_err(pdev
->dev
.driver
, "null ptr in vpfe_cfg\n");
1885 goto probe_free_dev_mem
;
1888 /* enable vpss clocks */
1889 ret
= vpfe_enable_clock(vpfe_dev
);
1891 goto probe_free_dev_mem
;
1893 mutex_lock(&ccdc_lock
);
1894 /* Allocate memory for ccdc configuration */
1895 ccdc_cfg
= kmalloc(sizeof(struct ccdc_config
), GFP_KERNEL
);
1896 if (NULL
== ccdc_cfg
) {
1897 v4l2_err(pdev
->dev
.driver
,
1898 "Memory allocation failed for ccdc_cfg\n");
1899 goto probe_disable_clock
;
1902 strncpy(ccdc_cfg
->name
, vpfe_cfg
->ccdc
, 32);
1903 /* Get VINT0 irq resource */
1904 res1
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1906 v4l2_err(pdev
->dev
.driver
,
1907 "Unable to get interrupt for VINT0\n");
1909 goto probe_disable_clock
;
1911 vpfe_dev
->ccdc_irq0
= res1
->start
;
1913 /* Get VINT1 irq resource */
1914 res1
= platform_get_resource(pdev
,
1917 v4l2_err(pdev
->dev
.driver
,
1918 "Unable to get interrupt for VINT1\n");
1920 goto probe_disable_clock
;
1922 vpfe_dev
->ccdc_irq1
= res1
->start
;
1924 /* Get address base of CCDC */
1925 res1
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1927 v4l2_err(pdev
->dev
.driver
,
1928 "Unable to get register address map\n");
1930 goto probe_disable_clock
;
1933 ccdc_cfg
->ccdc_addr_size
= res1
->end
- res1
->start
+ 1;
1934 if (!request_mem_region(res1
->start
, ccdc_cfg
->ccdc_addr_size
,
1935 pdev
->dev
.driver
->name
)) {
1936 v4l2_err(pdev
->dev
.driver
,
1937 "Failed request_mem_region for ccdc base\n");
1939 goto probe_disable_clock
;
1941 ccdc_cfg
->ccdc_addr
= ioremap_nocache(res1
->start
,
1942 ccdc_cfg
->ccdc_addr_size
);
1943 if (!ccdc_cfg
->ccdc_addr
) {
1944 v4l2_err(pdev
->dev
.driver
, "Unable to ioremap ccdc addr\n");
1946 goto probe_out_release_mem1
;
1949 ret
= request_irq(vpfe_dev
->ccdc_irq0
, vpfe_isr
, IRQF_DISABLED
,
1950 "vpfe_capture0", vpfe_dev
);
1953 v4l2_err(pdev
->dev
.driver
, "Unable to request interrupt\n");
1954 goto probe_out_unmap1
;
1957 /* Allocate memory for video device */
1958 vfd
= video_device_alloc();
1961 v4l2_err(pdev
->dev
.driver
,
1962 "Unable to alloc video device\n");
1963 goto probe_out_release_irq
;
1966 /* Initialize field of video device */
1967 vfd
->release
= video_device_release
;
1968 vfd
->fops
= &vpfe_fops
;
1969 vfd
->ioctl_ops
= &vpfe_ioctl_ops
;
1972 vfd
->current_norm
= V4L2_STD_PAL
;
1973 vfd
->v4l2_dev
= &vpfe_dev
->v4l2_dev
;
1974 snprintf(vfd
->name
, sizeof(vfd
->name
),
1977 (VPFE_CAPTURE_VERSION_CODE
>> 16) & 0xff,
1978 (VPFE_CAPTURE_VERSION_CODE
>> 8) & 0xff,
1979 (VPFE_CAPTURE_VERSION_CODE
) & 0xff);
1980 /* Set video_dev to the video device */
1981 vpfe_dev
->video_dev
= vfd
;
1983 ret
= v4l2_device_register(&pdev
->dev
, &vpfe_dev
->v4l2_dev
);
1985 v4l2_err(pdev
->dev
.driver
,
1986 "Unable to register v4l2 device.\n");
1987 goto probe_out_video_release
;
1989 v4l2_info(&vpfe_dev
->v4l2_dev
, "v4l2 device registered\n");
1990 spin_lock_init(&vpfe_dev
->irqlock
);
1991 spin_lock_init(&vpfe_dev
->dma_queue_lock
);
1992 mutex_init(&vpfe_dev
->lock
);
1994 /* Initialize field of the device objects */
1995 vpfe_dev
->numbuffers
= config_params
.numbuffers
;
1997 /* Initialize prio member of device object */
1998 v4l2_prio_init(&vpfe_dev
->prio
);
1999 /* register video device */
2000 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
2001 "trying to register vpfe device.\n");
2002 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
2003 "video_dev=%x\n", (int)&vpfe_dev
->video_dev
);
2004 vpfe_dev
->fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2005 ret
= video_register_device(vpfe_dev
->video_dev
,
2006 VFL_TYPE_GRABBER
, -1);
2009 v4l2_err(pdev
->dev
.driver
,
2010 "Unable to register video device.\n");
2011 goto probe_out_v4l2_unregister
;
2014 v4l2_info(&vpfe_dev
->v4l2_dev
, "video device registered\n");
2015 /* set the driver data in platform device */
2016 platform_set_drvdata(pdev
, vpfe_dev
);
2017 /* set driver private data */
2018 video_set_drvdata(vpfe_dev
->video_dev
, vpfe_dev
);
2019 i2c_adap
= i2c_get_adapter(vpfe_cfg
->i2c_adapter_id
);
2020 num_subdevs
= vpfe_cfg
->num_subdevs
;
2021 vpfe_dev
->sd
= kmalloc(sizeof(struct v4l2_subdev
*) * num_subdevs
,
2023 if (NULL
== vpfe_dev
->sd
) {
2024 v4l2_err(&vpfe_dev
->v4l2_dev
,
2025 "unable to allocate memory for subdevice pointers\n");
2027 goto probe_out_video_unregister
;
2030 for (i
= 0; i
< num_subdevs
; i
++) {
2031 struct v4l2_input
*inps
;
2033 sdinfo
= &vpfe_cfg
->sub_devs
[i
];
2035 /* Load up the subdevice */
2037 v4l2_i2c_new_subdev_board(&vpfe_dev
->v4l2_dev
,
2040 &sdinfo
->board_info
,
2042 if (vpfe_dev
->sd
[i
]) {
2043 v4l2_info(&vpfe_dev
->v4l2_dev
,
2044 "v4l2 sub device %s registered\n",
2046 vpfe_dev
->sd
[i
]->grp_id
= sdinfo
->grp_id
;
2047 /* update tvnorms from the sub devices */
2048 for (j
= 0; j
< sdinfo
->num_inputs
; j
++) {
2049 inps
= &sdinfo
->inputs
[j
];
2050 vfd
->tvnorms
|= inps
->std
;
2053 v4l2_info(&vpfe_dev
->v4l2_dev
,
2054 "v4l2 sub device %s register fails\n",
2060 /* set first sub device as current one */
2061 vpfe_dev
->current_subdev
= &vpfe_cfg
->sub_devs
[0];
2063 /* We have at least one sub device to work with */
2064 mutex_unlock(&ccdc_lock
);
2068 kfree(vpfe_dev
->sd
);
2069 probe_out_video_unregister
:
2070 video_unregister_device(vpfe_dev
->video_dev
);
2071 probe_out_v4l2_unregister
:
2072 v4l2_device_unregister(&vpfe_dev
->v4l2_dev
);
2073 probe_out_video_release
:
2074 if (vpfe_dev
->video_dev
->minor
== -1)
2075 video_device_release(vpfe_dev
->video_dev
);
2076 probe_out_release_irq
:
2077 free_irq(vpfe_dev
->ccdc_irq0
, vpfe_dev
);
2079 iounmap(ccdc_cfg
->ccdc_addr
);
2080 probe_out_release_mem1
:
2081 release_mem_region(res1
->start
, res1
->end
- res1
->start
+ 1);
2082 probe_disable_clock
:
2083 vpfe_disable_clock(vpfe_dev
);
2084 mutex_unlock(&ccdc_lock
);
2092 * vpfe_remove : It un-register device from V4L2 driver
2094 static int vpfe_remove(struct platform_device
*pdev
)
2096 struct vpfe_device
*vpfe_dev
= platform_get_drvdata(pdev
);
2097 struct resource
*res
;
2099 v4l2_info(pdev
->dev
.driver
, "vpfe_remove\n");
2101 free_irq(vpfe_dev
->ccdc_irq0
, vpfe_dev
);
2102 kfree(vpfe_dev
->sd
);
2103 v4l2_device_unregister(&vpfe_dev
->v4l2_dev
);
2104 video_unregister_device(vpfe_dev
->video_dev
);
2105 mutex_lock(&ccdc_lock
);
2106 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2107 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
2108 iounmap(ccdc_cfg
->ccdc_addr
);
2109 mutex_unlock(&ccdc_lock
);
2110 vpfe_disable_clock(vpfe_dev
);
2117 vpfe_suspend(struct device
*dev
)
2119 /* add suspend code here later */
2124 vpfe_resume(struct device
*dev
)
2126 /* add resume code here later */
2130 static struct dev_pm_ops vpfe_dev_pm_ops
= {
2131 .suspend
= vpfe_suspend
,
2132 .resume
= vpfe_resume
,
2135 static struct platform_driver vpfe_driver
= {
2137 .name
= CAPTURE_DRV_NAME
,
2138 .owner
= THIS_MODULE
,
2139 .pm
= &vpfe_dev_pm_ops
,
2141 .probe
= vpfe_probe
,
2142 .remove
= __devexit_p(vpfe_remove
),
2145 static __init
int vpfe_init(void)
2147 printk(KERN_NOTICE
"vpfe_init\n");
2148 /* Register driver to the kernel */
2149 return platform_driver_register(&vpfe_driver
);
2153 * vpfe_cleanup : This function un-registers device driver
2155 static void vpfe_cleanup(void)
2157 platform_driver_unregister(&vpfe_driver
);
2160 module_init(vpfe_init
);
2161 module_exit(vpfe_cleanup
);