Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / davinci / vpfe_capture.c
blob885cd54499cf6952abbae826e7526ca5ac186673
1 /*
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)
34 * data input | |
35 * V |
36 * SDRAM |
37 * V
38 * Image Processor
39 * |
40 * V
41 * SDRAM
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).
53 * Features supported
54 * - MMAP IO
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.
59 * TODO list
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 <media/v4l2-common.h>
74 #include <linux/io.h>
75 #include <media/davinci/vpfe_capture.h>
76 #include "ccdc_hw_device.h"
78 static int debug;
79 static u32 numbuffers = 3;
80 static u32 bufsize = (720 * 576 * 2);
82 module_param(numbuffers, uint, S_IRUGO);
83 module_param(bufsize, uint, S_IRUGO);
84 module_param(debug, int, 0644);
86 MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
87 MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
88 MODULE_PARM_DESC(debug, "Debug level 0-1");
90 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
91 MODULE_LICENSE("GPL");
92 MODULE_AUTHOR("Texas Instruments");
94 /* standard information */
95 struct vpfe_standard {
96 v4l2_std_id std_id;
97 unsigned int width;
98 unsigned int height;
99 struct v4l2_fract pixelaspect;
100 /* 0 - progressive, 1 - interlaced */
101 int frame_format;
104 /* ccdc configuration */
105 struct ccdc_config {
106 /* This make sure vpfe is probed and ready to go */
107 int vpfe_probed;
108 /* name of ccdc device */
109 char name[32];
112 /* data structures */
113 static struct vpfe_config_params config_params = {
114 .min_numbuffers = 3,
115 .numbuffers = 3,
116 .min_bufsize = 720 * 480 * 2,
117 .device_bufsize = 720 * 576 * 2,
120 /* ccdc device registered */
121 static struct ccdc_hw_device *ccdc_dev;
122 /* lock for accessing ccdc information */
123 static DEFINE_MUTEX(ccdc_lock);
124 /* ccdc configuration */
125 static struct ccdc_config *ccdc_cfg;
127 const struct vpfe_standard vpfe_standards[] = {
128 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
129 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
132 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
133 static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
135 .fmtdesc = {
136 .index = 0,
137 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
138 .description = "Bayer GrRBGb 8bit A-Law compr.",
139 .pixelformat = V4L2_PIX_FMT_SBGGR8,
141 .bpp = 1,
144 .fmtdesc = {
145 .index = 1,
146 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
147 .description = "Bayer GrRBGb - 16bit",
148 .pixelformat = V4L2_PIX_FMT_SBGGR16,
150 .bpp = 2,
153 .fmtdesc = {
154 .index = 2,
155 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
156 .description = "Bayer GrRBGb 8bit DPCM compr.",
157 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
159 .bpp = 1,
162 .fmtdesc = {
163 .index = 3,
164 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
165 .description = "YCbCr 4:2:2 Interleaved UYVY",
166 .pixelformat = V4L2_PIX_FMT_UYVY,
168 .bpp = 2,
171 .fmtdesc = {
172 .index = 4,
173 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
174 .description = "YCbCr 4:2:2 Interleaved YUYV",
175 .pixelformat = V4L2_PIX_FMT_YUYV,
177 .bpp = 2,
180 .fmtdesc = {
181 .index = 5,
182 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
183 .description = "Y/CbCr 4:2:0 - Semi planar",
184 .pixelformat = V4L2_PIX_FMT_NV12,
186 .bpp = 1,
191 * vpfe_lookup_pix_format()
192 * lookup an entry in the vpfe pix format table based on pix_format
194 static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
196 int i;
198 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
199 if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
200 return &vpfe_pix_fmts[i];
202 return NULL;
206 * vpfe_register_ccdc_device. CCDC module calls this to
207 * register with vpfe capture
209 int vpfe_register_ccdc_device(struct ccdc_hw_device *dev)
211 int ret = 0;
212 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
214 BUG_ON(!dev->hw_ops.open);
215 BUG_ON(!dev->hw_ops.enable);
216 BUG_ON(!dev->hw_ops.set_hw_if_params);
217 BUG_ON(!dev->hw_ops.configure);
218 BUG_ON(!dev->hw_ops.set_buftype);
219 BUG_ON(!dev->hw_ops.get_buftype);
220 BUG_ON(!dev->hw_ops.enum_pix);
221 BUG_ON(!dev->hw_ops.set_frame_format);
222 BUG_ON(!dev->hw_ops.get_frame_format);
223 BUG_ON(!dev->hw_ops.get_pixel_format);
224 BUG_ON(!dev->hw_ops.set_pixel_format);
225 BUG_ON(!dev->hw_ops.set_params);
226 BUG_ON(!dev->hw_ops.set_image_window);
227 BUG_ON(!dev->hw_ops.get_image_window);
228 BUG_ON(!dev->hw_ops.get_line_length);
229 BUG_ON(!dev->hw_ops.getfid);
231 mutex_lock(&ccdc_lock);
232 if (NULL == ccdc_cfg) {
234 * TODO. Will this ever happen? if so, we need to fix it.
235 * Proabably we need to add the request to a linked list and
236 * walk through it during vpfe probe
238 printk(KERN_ERR "vpfe capture not initialized\n");
239 ret = -EFAULT;
240 goto unlock;
243 if (strcmp(dev->name, ccdc_cfg->name)) {
244 /* ignore this ccdc */
245 ret = -EINVAL;
246 goto unlock;
249 if (ccdc_dev) {
250 printk(KERN_ERR "ccdc already registered\n");
251 ret = -EINVAL;
252 goto unlock;
255 ccdc_dev = dev;
256 unlock:
257 mutex_unlock(&ccdc_lock);
258 return ret;
260 EXPORT_SYMBOL(vpfe_register_ccdc_device);
263 * vpfe_unregister_ccdc_device. CCDC module calls this to
264 * unregister with vpfe capture
266 void vpfe_unregister_ccdc_device(struct ccdc_hw_device *dev)
268 if (NULL == dev) {
269 printk(KERN_ERR "invalid ccdc device ptr\n");
270 return;
273 printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
274 dev->name);
276 if (strcmp(dev->name, ccdc_cfg->name)) {
277 /* ignore this ccdc */
278 return;
281 mutex_lock(&ccdc_lock);
282 ccdc_dev = NULL;
283 mutex_unlock(&ccdc_lock);
284 return;
286 EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
289 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
291 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe_dev,
292 struct v4l2_format *f)
294 struct v4l2_rect image_win;
295 enum ccdc_buftype buf_type;
296 enum ccdc_frmfmt frm_fmt;
298 memset(f, 0, sizeof(*f));
299 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
300 ccdc_dev->hw_ops.get_image_window(&image_win);
301 f->fmt.pix.width = image_win.width;
302 f->fmt.pix.height = image_win.height;
303 f->fmt.pix.bytesperline = ccdc_dev->hw_ops.get_line_length();
304 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
305 f->fmt.pix.height;
306 buf_type = ccdc_dev->hw_ops.get_buftype();
307 f->fmt.pix.pixelformat = ccdc_dev->hw_ops.get_pixel_format();
308 frm_fmt = ccdc_dev->hw_ops.get_frame_format();
309 if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
310 f->fmt.pix.field = V4L2_FIELD_NONE;
311 else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
312 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
313 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
314 else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED)
315 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
316 else {
317 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf_type\n");
318 return -EINVAL;
320 } else {
321 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid frm_fmt\n");
322 return -EINVAL;
324 return 0;
328 * vpfe_config_ccdc_image_format()
329 * For a pix format, configure ccdc to setup the capture
331 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
333 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
334 int ret = 0;
336 if (ccdc_dev->hw_ops.set_pixel_format(
337 vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
338 v4l2_err(&vpfe_dev->v4l2_dev,
339 "couldn't set pix format in ccdc\n");
340 return -EINVAL;
342 /* configure the image window */
343 ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
345 switch (vpfe_dev->fmt.fmt.pix.field) {
346 case V4L2_FIELD_INTERLACED:
347 /* do nothing, since it is default */
348 ret = ccdc_dev->hw_ops.set_buftype(
349 CCDC_BUFTYPE_FLD_INTERLEAVED);
350 break;
351 case V4L2_FIELD_NONE:
352 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
353 /* buffer type only applicable for interlaced scan */
354 break;
355 case V4L2_FIELD_SEQ_TB:
356 ret = ccdc_dev->hw_ops.set_buftype(
357 CCDC_BUFTYPE_FLD_SEPARATED);
358 break;
359 default:
360 return -EINVAL;
363 /* set the frame format */
364 if (!ret)
365 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
366 return ret;
369 * vpfe_config_image_format()
370 * For a given standard, this functions sets up the default
371 * pix format & crop values in the vpfe device and ccdc. It first
372 * starts with defaults based values from the standard table.
373 * It then checks if sub device support g_fmt and then override the
374 * values based on that.Sets crop values to match with scan resolution
375 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
376 * values in ccdc
378 static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
379 const v4l2_std_id *std_id)
381 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
382 int i, ret = 0;
384 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
385 if (vpfe_standards[i].std_id & *std_id) {
386 vpfe_dev->std_info.active_pixels =
387 vpfe_standards[i].width;
388 vpfe_dev->std_info.active_lines =
389 vpfe_standards[i].height;
390 vpfe_dev->std_info.frame_format =
391 vpfe_standards[i].frame_format;
392 vpfe_dev->std_index = i;
393 break;
397 if (i == ARRAY_SIZE(vpfe_standards)) {
398 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
399 return -EINVAL;
402 vpfe_dev->crop.top = 0;
403 vpfe_dev->crop.left = 0;
404 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
405 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
406 vpfe_dev->fmt.fmt.pix.width = vpfe_dev->crop.width;
407 vpfe_dev->fmt.fmt.pix.height = vpfe_dev->crop.height;
409 /* first field and frame format based on standard frame format */
410 if (vpfe_dev->std_info.frame_format) {
411 vpfe_dev->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
412 /* assume V4L2_PIX_FMT_UYVY as default */
413 vpfe_dev->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
414 } else {
415 vpfe_dev->fmt.fmt.pix.field = V4L2_FIELD_NONE;
416 /* assume V4L2_PIX_FMT_SBGGR8 */
417 vpfe_dev->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
420 /* if sub device supports g_fmt, override the defaults */
421 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
422 sdinfo->grp_id, video, g_fmt, &vpfe_dev->fmt);
424 if (ret && ret != -ENOIOCTLCMD) {
425 v4l2_err(&vpfe_dev->v4l2_dev,
426 "error in getting g_fmt from sub device\n");
427 return ret;
430 /* Sets the values in CCDC */
431 ret = vpfe_config_ccdc_image_format(vpfe_dev);
432 if (ret)
433 return ret;
435 /* Update the values of sizeimage and bytesperline */
436 if (!ret) {
437 vpfe_dev->fmt.fmt.pix.bytesperline =
438 ccdc_dev->hw_ops.get_line_length();
439 vpfe_dev->fmt.fmt.pix.sizeimage =
440 vpfe_dev->fmt.fmt.pix.bytesperline *
441 vpfe_dev->fmt.fmt.pix.height;
443 return ret;
446 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
448 int ret = 0;
450 /* set first input of current subdevice as the current input */
451 vpfe_dev->current_input = 0;
453 /* set default standard */
454 vpfe_dev->std_index = 0;
456 /* Configure the default format information */
457 ret = vpfe_config_image_format(vpfe_dev,
458 &vpfe_standards[vpfe_dev->std_index].std_id);
459 if (ret)
460 return ret;
462 /* now open the ccdc device to initialize it */
463 mutex_lock(&ccdc_lock);
464 if (NULL == ccdc_dev) {
465 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
466 ret = -ENODEV;
467 goto unlock;
470 if (!try_module_get(ccdc_dev->owner)) {
471 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
472 ret = -ENODEV;
473 goto unlock;
475 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
476 if (!ret)
477 vpfe_dev->initialized = 1;
478 unlock:
479 mutex_unlock(&ccdc_lock);
480 return ret;
484 * vpfe_open : It creates object of file handle structure and
485 * stores it in private_data member of filepointer
487 static int vpfe_open(struct file *file)
489 struct vpfe_device *vpfe_dev = video_drvdata(file);
490 struct vpfe_fh *fh;
492 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
494 if (!vpfe_dev->cfg->num_subdevs) {
495 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
496 return -ENODEV;
499 /* Allocate memory for the file handle object */
500 fh = kmalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
501 if (NULL == fh) {
502 v4l2_err(&vpfe_dev->v4l2_dev,
503 "unable to allocate memory for file handle object\n");
504 return -ENOMEM;
506 /* store pointer to fh in private_data member of file */
507 file->private_data = fh;
508 fh->vpfe_dev = vpfe_dev;
509 mutex_lock(&vpfe_dev->lock);
510 /* If decoder is not initialized. initialize it */
511 if (!vpfe_dev->initialized) {
512 if (vpfe_initialize_device(vpfe_dev)) {
513 mutex_unlock(&vpfe_dev->lock);
514 return -ENODEV;
517 /* Increment device usrs counter */
518 vpfe_dev->usrs++;
519 /* Set io_allowed member to false */
520 fh->io_allowed = 0;
521 /* Initialize priority of this instance to default priority */
522 fh->prio = V4L2_PRIORITY_UNSET;
523 v4l2_prio_open(&vpfe_dev->prio, &fh->prio);
524 mutex_unlock(&vpfe_dev->lock);
525 return 0;
528 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
530 unsigned long addr;
532 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
533 struct videobuf_buffer, queue);
534 list_del(&vpfe_dev->next_frm->queue);
535 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
536 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
537 ccdc_dev->hw_ops.setfbaddr(addr);
540 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
542 struct timeval timevalue;
544 do_gettimeofday(&timevalue);
545 vpfe_dev->cur_frm->ts = timevalue;
546 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
547 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
548 wake_up_interruptible(&vpfe_dev->cur_frm->done);
549 vpfe_dev->cur_frm = vpfe_dev->next_frm;
552 /* ISR for VINT0*/
553 static irqreturn_t vpfe_isr(int irq, void *dev_id)
555 struct vpfe_device *vpfe_dev = dev_id;
556 enum v4l2_field field;
557 unsigned long addr;
558 int fid;
560 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
561 field = vpfe_dev->fmt.fmt.pix.field;
563 /* if streaming not started, don't do anything */
564 if (!vpfe_dev->started)
565 return IRQ_HANDLED;
567 /* only for 6446 this will be applicable */
568 if (NULL != ccdc_dev->hw_ops.reset)
569 ccdc_dev->hw_ops.reset();
571 if (field == V4L2_FIELD_NONE) {
572 /* handle progressive frame capture */
573 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
574 "frame format is progressive...\n");
575 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
576 vpfe_process_buffer_complete(vpfe_dev);
577 return IRQ_HANDLED;
580 /* interlaced or TB capture check which field we are in hardware */
581 fid = ccdc_dev->hw_ops.getfid();
583 /* switch the software maintained field id */
584 vpfe_dev->field_id ^= 1;
585 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
586 fid, vpfe_dev->field_id);
587 if (fid == vpfe_dev->field_id) {
588 /* we are in-sync here,continue */
589 if (fid == 0) {
591 * One frame is just being captured. If the next frame
592 * is available, release the current frame and move on
594 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
595 vpfe_process_buffer_complete(vpfe_dev);
597 * based on whether the two fields are stored
598 * interleavely or separately in memory, reconfigure
599 * the CCDC memory address
601 if (field == V4L2_FIELD_SEQ_TB) {
602 addr =
603 videobuf_to_dma_contig(vpfe_dev->cur_frm);
604 addr += vpfe_dev->field_off;
605 ccdc_dev->hw_ops.setfbaddr(addr);
607 return IRQ_HANDLED;
610 * if one field is just being captured configure
611 * the next frame get the next frame from the empty
612 * queue if no frame is available hold on to the
613 * current buffer
615 spin_lock(&vpfe_dev->dma_queue_lock);
616 if (!list_empty(&vpfe_dev->dma_queue) &&
617 vpfe_dev->cur_frm == vpfe_dev->next_frm)
618 vpfe_schedule_next_buffer(vpfe_dev);
619 spin_unlock(&vpfe_dev->dma_queue_lock);
620 } else if (fid == 0) {
622 * out of sync. Recover from any hardware out-of-sync.
623 * May loose one frame
625 vpfe_dev->field_id = fid;
627 return IRQ_HANDLED;
630 /* vdint1_isr - isr handler for VINT1 interrupt */
631 static irqreturn_t vdint1_isr(int irq, void *dev_id)
633 struct vpfe_device *vpfe_dev = dev_id;
635 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
637 /* if streaming not started, don't do anything */
638 if (!vpfe_dev->started)
639 return IRQ_HANDLED;
641 spin_lock(&vpfe_dev->dma_queue_lock);
642 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
643 !list_empty(&vpfe_dev->dma_queue) &&
644 vpfe_dev->cur_frm == vpfe_dev->next_frm)
645 vpfe_schedule_next_buffer(vpfe_dev);
646 spin_unlock(&vpfe_dev->dma_queue_lock);
647 return IRQ_HANDLED;
650 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
652 enum ccdc_frmfmt frame_format;
654 frame_format = ccdc_dev->hw_ops.get_frame_format();
655 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
656 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
659 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
661 enum ccdc_frmfmt frame_format;
663 frame_format = ccdc_dev->hw_ops.get_frame_format();
664 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
665 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
666 IRQF_DISABLED, "vpfe_capture1",
667 vpfe_dev);
669 return 0;
672 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
673 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
675 vpfe_dev->started = 0;
676 ccdc_dev->hw_ops.enable(0);
677 if (ccdc_dev->hw_ops.enable_out_to_sdram)
678 ccdc_dev->hw_ops.enable_out_to_sdram(0);
682 * vpfe_release : This function deletes buffer queue, frees the
683 * buffers and the vpfe file handle
685 static int vpfe_release(struct file *file)
687 struct vpfe_device *vpfe_dev = video_drvdata(file);
688 struct vpfe_fh *fh = file->private_data;
689 struct vpfe_subdev_info *sdinfo;
690 int ret;
692 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
694 /* Get the device lock */
695 mutex_lock(&vpfe_dev->lock);
696 /* if this instance is doing IO */
697 if (fh->io_allowed) {
698 if (vpfe_dev->started) {
699 sdinfo = vpfe_dev->current_subdev;
700 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
701 sdinfo->grp_id,
702 video, s_stream, 0);
703 if (ret && (ret != -ENOIOCTLCMD))
704 v4l2_err(&vpfe_dev->v4l2_dev,
705 "stream off failed in subdev\n");
706 vpfe_stop_ccdc_capture(vpfe_dev);
707 vpfe_detach_irq(vpfe_dev);
708 videobuf_streamoff(&vpfe_dev->buffer_queue);
710 vpfe_dev->io_usrs = 0;
711 vpfe_dev->numbuffers = config_params.numbuffers;
714 /* Decrement device usrs counter */
715 vpfe_dev->usrs--;
716 /* Close the priority */
717 v4l2_prio_close(&vpfe_dev->prio, &fh->prio);
718 /* If this is the last file handle */
719 if (!vpfe_dev->usrs) {
720 vpfe_dev->initialized = 0;
721 if (ccdc_dev->hw_ops.close)
722 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
723 module_put(ccdc_dev->owner);
725 mutex_unlock(&vpfe_dev->lock);
726 file->private_data = NULL;
727 /* Free memory allocated to file handle object */
728 kfree(fh);
729 return 0;
733 * vpfe_mmap : It is used to map kernel space buffers
734 * into user spaces
736 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
738 /* Get the device object and file handle object */
739 struct vpfe_device *vpfe_dev = video_drvdata(file);
741 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
743 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
747 * vpfe_poll: It is used for select/poll system call
749 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
751 struct vpfe_device *vpfe_dev = video_drvdata(file);
753 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
755 if (vpfe_dev->started)
756 return videobuf_poll_stream(file,
757 &vpfe_dev->buffer_queue, wait);
758 return 0;
761 /* vpfe capture driver file operations */
762 static const struct v4l2_file_operations vpfe_fops = {
763 .owner = THIS_MODULE,
764 .open = vpfe_open,
765 .release = vpfe_release,
766 .unlocked_ioctl = video_ioctl2,
767 .mmap = vpfe_mmap,
768 .poll = vpfe_poll
772 * vpfe_check_format()
773 * This function adjust the input pixel format as per hardware
774 * capabilities and update the same in pixfmt.
775 * Following algorithm used :-
777 * If given pixformat is not in the vpfe list of pix formats or not
778 * supported by the hardware, current value of pixformat in the device
779 * is used
780 * If given field is not supported, then current field is used. If field
781 * is different from current, then it is matched with that from sub device.
782 * Minimum height is 2 lines for interlaced or tb field and 1 line for
783 * progressive. Maximum height is clamped to active active lines of scan
784 * Minimum width is 32 bytes in memory and width is clamped to active
785 * pixels of scan.
786 * bytesperline is a multiple of 32.
788 static const struct vpfe_pixel_format *
789 vpfe_check_format(struct vpfe_device *vpfe_dev,
790 struct v4l2_pix_format *pixfmt)
792 u32 min_height = 1, min_width = 32, max_width, max_height;
793 const struct vpfe_pixel_format *vpfe_pix_fmt;
794 u32 pix;
795 int temp, found;
797 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
798 if (NULL == vpfe_pix_fmt) {
800 * use current pixel format in the vpfe device. We
801 * will find this pix format in the table
803 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
804 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
807 /* check if hw supports it */
808 temp = 0;
809 found = 0;
810 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
811 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
812 found = 1;
813 break;
815 temp++;
818 if (!found) {
819 /* use current pixel format */
820 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
822 * Since this is currently used in the vpfe device, we
823 * will find this pix format in the table
825 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
828 /* check what field format is supported */
829 if (pixfmt->field == V4L2_FIELD_ANY) {
830 /* if field is any, use current value as default */
831 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
835 * if field is not same as current field in the vpfe device
836 * try matching the field with the sub device field
838 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
840 * If field value is not in the supported fields, use current
841 * field used in the device as default
843 switch (pixfmt->field) {
844 case V4L2_FIELD_INTERLACED:
845 case V4L2_FIELD_SEQ_TB:
846 /* if sub device is supporting progressive, use that */
847 if (!vpfe_dev->std_info.frame_format)
848 pixfmt->field = V4L2_FIELD_NONE;
849 break;
850 case V4L2_FIELD_NONE:
851 if (vpfe_dev->std_info.frame_format)
852 pixfmt->field = V4L2_FIELD_INTERLACED;
853 break;
855 default:
856 /* use current field as default */
857 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
858 break;
862 /* Now adjust image resolutions supported */
863 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
864 pixfmt->field == V4L2_FIELD_SEQ_TB)
865 min_height = 2;
867 max_width = vpfe_dev->std_info.active_pixels;
868 max_height = vpfe_dev->std_info.active_lines;
869 min_width /= vpfe_pix_fmt->bpp;
871 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
872 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
874 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
875 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
877 /* If interlaced, adjust height to be a multiple of 2 */
878 if (pixfmt->field == V4L2_FIELD_INTERLACED)
879 pixfmt->height &= (~1);
881 * recalculate bytesperline and sizeimage since width
882 * and height might have changed
884 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
885 & ~31);
886 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
887 pixfmt->sizeimage =
888 pixfmt->bytesperline * pixfmt->height +
889 ((pixfmt->bytesperline * pixfmt->height) >> 1);
890 else
891 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
893 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height ="
894 " %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
895 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
896 pixfmt->bytesperline, pixfmt->sizeimage);
897 return vpfe_pix_fmt;
900 static int vpfe_querycap(struct file *file, void *priv,
901 struct v4l2_capability *cap)
903 struct vpfe_device *vpfe_dev = video_drvdata(file);
905 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
907 cap->version = VPFE_CAPTURE_VERSION_CODE;
908 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
909 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
910 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
911 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
912 return 0;
915 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
916 struct v4l2_format *fmt)
918 struct vpfe_device *vpfe_dev = video_drvdata(file);
919 int ret = 0;
921 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
922 /* Fill in the information about format */
923 *fmt = vpfe_dev->fmt;
924 return ret;
927 static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
928 struct v4l2_fmtdesc *fmt)
930 struct vpfe_device *vpfe_dev = video_drvdata(file);
931 const struct vpfe_pixel_format *pix_fmt;
932 int temp_index;
933 u32 pix;
935 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
937 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
938 return -EINVAL;
940 /* Fill in the information about format */
941 pix_fmt = vpfe_lookup_pix_format(pix);
942 if (NULL != pix_fmt) {
943 temp_index = fmt->index;
944 *fmt = pix_fmt->fmtdesc;
945 fmt->index = temp_index;
946 return 0;
948 return -EINVAL;
951 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
952 struct v4l2_format *fmt)
954 struct vpfe_device *vpfe_dev = video_drvdata(file);
955 const struct vpfe_pixel_format *pix_fmts;
956 int ret = 0;
958 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
960 /* If streaming is started, return error */
961 if (vpfe_dev->started) {
962 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
963 return -EBUSY;
966 /* Check for valid frame format */
967 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
969 if (NULL == pix_fmts)
970 return -EINVAL;
972 /* store the pixel format in the device object */
973 ret = mutex_lock_interruptible(&vpfe_dev->lock);
974 if (ret)
975 return ret;
977 /* First detach any IRQ if currently attached */
978 vpfe_detach_irq(vpfe_dev);
979 vpfe_dev->fmt = *fmt;
980 /* set image capture parameters in the ccdc */
981 ret = vpfe_config_ccdc_image_format(vpfe_dev);
982 mutex_unlock(&vpfe_dev->lock);
983 return ret;
986 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
987 struct v4l2_format *f)
989 struct vpfe_device *vpfe_dev = video_drvdata(file);
990 const struct vpfe_pixel_format *pix_fmts;
992 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
994 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
995 if (NULL == pix_fmts)
996 return -EINVAL;
997 return 0;
1001 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1002 * given app input index
1004 static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1005 int *subdev_index,
1006 int *subdev_input_index,
1007 int app_input_index)
1009 struct vpfe_config *cfg = vpfe_dev->cfg;
1010 struct vpfe_subdev_info *sdinfo;
1011 int i, j = 0;
1013 for (i = 0; i < cfg->num_subdevs; i++) {
1014 sdinfo = &cfg->sub_devs[i];
1015 if (app_input_index < (j + sdinfo->num_inputs)) {
1016 *subdev_index = i;
1017 *subdev_input_index = app_input_index - j;
1018 return 0;
1020 j += sdinfo->num_inputs;
1022 return -EINVAL;
1026 * vpfe_get_app_input - Get app input index for a given subdev input index
1027 * driver stores the input index of the current sub device and translate it
1028 * when application request the current input
1030 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1031 int *app_input_index)
1033 struct vpfe_config *cfg = vpfe_dev->cfg;
1034 struct vpfe_subdev_info *sdinfo;
1035 int i, j = 0;
1037 for (i = 0; i < cfg->num_subdevs; i++) {
1038 sdinfo = &cfg->sub_devs[i];
1039 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1040 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1041 return -1;
1042 *app_input_index = j + vpfe_dev->current_input;
1043 return 0;
1045 j += sdinfo->num_inputs;
1047 return -EINVAL;
1050 static int vpfe_enum_input(struct file *file, void *priv,
1051 struct v4l2_input *inp)
1053 struct vpfe_device *vpfe_dev = video_drvdata(file);
1054 struct vpfe_subdev_info *sdinfo;
1055 int subdev, index ;
1057 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1059 if (vpfe_get_subdev_input_index(vpfe_dev,
1060 &subdev,
1061 &index,
1062 inp->index) < 0) {
1063 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found"
1064 " for the subdev\n");
1065 return -EINVAL;
1067 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1068 memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1069 return 0;
1072 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1074 struct vpfe_device *vpfe_dev = video_drvdata(file);
1076 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1078 return vpfe_get_app_input_index(vpfe_dev, index);
1082 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1084 struct vpfe_device *vpfe_dev = video_drvdata(file);
1085 struct vpfe_subdev_info *sdinfo;
1086 int subdev_index, inp_index;
1087 struct vpfe_route *route;
1088 u32 input = 0, output = 0;
1089 int ret = -EINVAL;
1091 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1093 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1094 if (ret)
1095 return ret;
1098 * If streaming is started return device busy
1099 * error
1101 if (vpfe_dev->started) {
1102 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1103 ret = -EBUSY;
1104 goto unlock_out;
1107 if (vpfe_get_subdev_input_index(vpfe_dev,
1108 &subdev_index,
1109 &inp_index,
1110 index) < 0) {
1111 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1112 goto unlock_out;
1115 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1116 route = &sdinfo->routes[inp_index];
1117 if (route && sdinfo->can_route) {
1118 input = route->input;
1119 output = route->output;
1122 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1123 video, s_routing, input, output, 0);
1125 if (ret) {
1126 v4l2_err(&vpfe_dev->v4l2_dev,
1127 "vpfe_doioctl:error in setting input in decoder\n");
1128 ret = -EINVAL;
1129 goto unlock_out;
1131 vpfe_dev->current_subdev = sdinfo;
1132 vpfe_dev->current_input = index;
1133 vpfe_dev->std_index = 0;
1135 /* set the bus/interface parameter for the sub device in ccdc */
1136 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1137 if (ret)
1138 goto unlock_out;
1140 /* set the default image parameters in the device */
1141 ret = vpfe_config_image_format(vpfe_dev,
1142 &vpfe_standards[vpfe_dev->std_index].std_id);
1143 unlock_out:
1144 mutex_unlock(&vpfe_dev->lock);
1145 return ret;
1148 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1150 struct vpfe_device *vpfe_dev = video_drvdata(file);
1151 struct vpfe_subdev_info *sdinfo;
1152 int ret = 0;
1154 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1156 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1157 sdinfo = vpfe_dev->current_subdev;
1158 if (ret)
1159 return ret;
1160 /* Call querystd function of decoder device */
1161 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1162 video, querystd, std_id);
1163 mutex_unlock(&vpfe_dev->lock);
1164 return ret;
1167 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1169 struct vpfe_device *vpfe_dev = video_drvdata(file);
1170 struct vpfe_subdev_info *sdinfo;
1171 int ret = 0;
1173 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1175 /* Call decoder driver function to set the standard */
1176 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1177 if (ret)
1178 return ret;
1180 sdinfo = vpfe_dev->current_subdev;
1181 /* If streaming is started, return device busy error */
1182 if (vpfe_dev->started) {
1183 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1184 ret = -EBUSY;
1185 goto unlock_out;
1188 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1189 core, s_std, *std_id);
1190 if (ret < 0) {
1191 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1192 goto unlock_out;
1194 ret = vpfe_config_image_format(vpfe_dev, std_id);
1196 unlock_out:
1197 mutex_unlock(&vpfe_dev->lock);
1198 return ret;
1201 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1203 struct vpfe_device *vpfe_dev = video_drvdata(file);
1205 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1207 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1208 return 0;
1211 * Videobuf operations
1213 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1214 unsigned int *count,
1215 unsigned int *size)
1217 struct vpfe_fh *fh = vq->priv_data;
1218 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1220 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1221 *size = config_params.device_bufsize;
1223 if (*count < config_params.min_numbuffers)
1224 *count = config_params.min_numbuffers;
1225 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1226 "count=%d, size=%d\n", *count, *size);
1227 return 0;
1230 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1231 struct videobuf_buffer *vb,
1232 enum v4l2_field field)
1234 struct vpfe_fh *fh = vq->priv_data;
1235 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1237 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1239 /* If buffer is not initialized, initialize it */
1240 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1241 vb->width = vpfe_dev->fmt.fmt.pix.width;
1242 vb->height = vpfe_dev->fmt.fmt.pix.height;
1243 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1244 vb->field = field;
1246 vb->state = VIDEOBUF_PREPARED;
1247 return 0;
1250 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1251 struct videobuf_buffer *vb)
1253 /* Get the file handle object and device object */
1254 struct vpfe_fh *fh = vq->priv_data;
1255 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1256 unsigned long flags;
1258 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1260 /* add the buffer to the DMA queue */
1261 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1262 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1263 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1265 /* Change state of the buffer */
1266 vb->state = VIDEOBUF_QUEUED;
1269 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1270 struct videobuf_buffer *vb)
1272 struct vpfe_fh *fh = vq->priv_data;
1273 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1274 unsigned long flags;
1276 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1279 * We need to flush the buffer from the dma queue since
1280 * they are de-allocated
1282 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1283 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1284 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1285 videobuf_dma_contig_free(vq, vb);
1286 vb->state = VIDEOBUF_NEEDS_INIT;
1289 static struct videobuf_queue_ops vpfe_videobuf_qops = {
1290 .buf_setup = vpfe_videobuf_setup,
1291 .buf_prepare = vpfe_videobuf_prepare,
1292 .buf_queue = vpfe_videobuf_queue,
1293 .buf_release = vpfe_videobuf_release,
1297 * vpfe_reqbufs. currently support REQBUF only once opening
1298 * the device.
1300 static int vpfe_reqbufs(struct file *file, void *priv,
1301 struct v4l2_requestbuffers *req_buf)
1303 struct vpfe_device *vpfe_dev = video_drvdata(file);
1304 struct vpfe_fh *fh = file->private_data;
1305 int ret = 0;
1307 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1309 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1310 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1311 return -EINVAL;
1314 if (V4L2_MEMORY_USERPTR == req_buf->memory) {
1315 /* we don't support user ptr IO */
1316 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs:"
1317 " USERPTR IO not supported\n");
1318 return -EINVAL;
1321 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1322 if (ret)
1323 return ret;
1325 if (vpfe_dev->io_usrs != 0) {
1326 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1327 ret = -EBUSY;
1328 goto unlock_out;
1331 vpfe_dev->memory = req_buf->memory;
1332 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1333 &vpfe_videobuf_qops,
1334 vpfe_dev->pdev,
1335 &vpfe_dev->irqlock,
1336 req_buf->type,
1337 vpfe_dev->fmt.fmt.pix.field,
1338 sizeof(struct videobuf_buffer),
1339 fh);
1341 fh->io_allowed = 1;
1342 vpfe_dev->io_usrs = 1;
1343 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1344 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1345 unlock_out:
1346 mutex_unlock(&vpfe_dev->lock);
1347 return ret;
1350 static int vpfe_querybuf(struct file *file, void *priv,
1351 struct v4l2_buffer *buf)
1353 struct vpfe_device *vpfe_dev = video_drvdata(file);
1355 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1357 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1358 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1359 return -EINVAL;
1362 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1363 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1364 return -EINVAL;
1366 /* Call videobuf_querybuf to get information */
1367 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1370 static int vpfe_qbuf(struct file *file, void *priv,
1371 struct v4l2_buffer *p)
1373 struct vpfe_device *vpfe_dev = video_drvdata(file);
1374 struct vpfe_fh *fh = file->private_data;
1376 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1378 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1379 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1380 return -EINVAL;
1384 * If this file handle is not allowed to do IO,
1385 * return error
1387 if (!fh->io_allowed) {
1388 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1389 return -EACCES;
1391 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1394 static int vpfe_dqbuf(struct file *file, void *priv,
1395 struct v4l2_buffer *buf)
1397 struct vpfe_device *vpfe_dev = video_drvdata(file);
1399 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1401 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1402 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1403 return -EINVAL;
1405 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1406 buf, file->f_flags & O_NONBLOCK);
1409 static int vpfe_queryctrl(struct file *file, void *priv,
1410 struct v4l2_queryctrl *qctrl)
1412 struct vpfe_device *vpfe_dev = video_drvdata(file);
1413 struct vpfe_subdev_info *sdinfo;
1415 sdinfo = vpfe_dev->current_subdev;
1417 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1418 core, queryctrl, qctrl);
1422 static int vpfe_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1424 struct vpfe_device *vpfe_dev = video_drvdata(file);
1425 struct vpfe_subdev_info *sdinfo;
1427 sdinfo = vpfe_dev->current_subdev;
1429 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1430 core, g_ctrl, ctrl);
1433 static int vpfe_s_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1435 struct vpfe_device *vpfe_dev = video_drvdata(file);
1436 struct vpfe_subdev_info *sdinfo;
1438 sdinfo = vpfe_dev->current_subdev;
1440 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1441 core, s_ctrl, ctrl);
1445 * vpfe_calculate_offsets : This function calculates buffers offset
1446 * for top and bottom field
1448 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1450 struct v4l2_rect image_win;
1452 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1454 ccdc_dev->hw_ops.get_image_window(&image_win);
1455 vpfe_dev->field_off = image_win.height * image_win.width;
1458 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1459 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1461 ccdc_dev->hw_ops.enable(1);
1462 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1463 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1464 vpfe_dev->started = 1;
1468 * vpfe_streamon. Assume the DMA queue is not empty.
1469 * application is expected to call QBUF before calling
1470 * this ioctl. If not, driver returns error
1472 static int vpfe_streamon(struct file *file, void *priv,
1473 enum v4l2_buf_type buf_type)
1475 struct vpfe_device *vpfe_dev = video_drvdata(file);
1476 struct vpfe_fh *fh = file->private_data;
1477 struct vpfe_subdev_info *sdinfo;
1478 unsigned long addr;
1479 int ret = 0;
1481 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1483 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1484 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1485 return -EINVAL;
1488 /* If file handle is not allowed IO, return error */
1489 if (!fh->io_allowed) {
1490 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1491 return -EACCES;
1494 sdinfo = vpfe_dev->current_subdev;
1495 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1496 video, s_stream, 1);
1498 if (ret && (ret != -ENOIOCTLCMD)) {
1499 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1500 return -EINVAL;
1503 /* If buffer queue is empty, return error */
1504 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1505 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1506 return -EIO;
1509 /* Call videobuf_streamon to start streaming * in videobuf */
1510 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1511 if (ret)
1512 return ret;
1515 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1516 if (ret)
1517 goto streamoff;
1518 /* Get the next frame from the buffer queue */
1519 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1520 struct videobuf_buffer, queue);
1521 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1522 /* Remove buffer from the buffer queue */
1523 list_del(&vpfe_dev->cur_frm->queue);
1524 /* Mark state of the current frame to active */
1525 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1526 /* Initialize field_id and started member */
1527 vpfe_dev->field_id = 0;
1528 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1530 /* Calculate field offset */
1531 vpfe_calculate_offsets(vpfe_dev);
1533 if (vpfe_attach_irq(vpfe_dev) < 0) {
1534 v4l2_err(&vpfe_dev->v4l2_dev,
1535 "Error in attaching interrupt handle\n");
1536 ret = -EFAULT;
1537 goto unlock_out;
1539 if (ccdc_dev->hw_ops.configure() < 0) {
1540 v4l2_err(&vpfe_dev->v4l2_dev,
1541 "Error in configuring ccdc\n");
1542 ret = -EINVAL;
1543 goto unlock_out;
1545 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1546 vpfe_start_ccdc_capture(vpfe_dev);
1547 mutex_unlock(&vpfe_dev->lock);
1548 return ret;
1549 unlock_out:
1550 mutex_unlock(&vpfe_dev->lock);
1551 streamoff:
1552 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1553 return ret;
1556 static int vpfe_streamoff(struct file *file, void *priv,
1557 enum v4l2_buf_type buf_type)
1559 struct vpfe_device *vpfe_dev = video_drvdata(file);
1560 struct vpfe_fh *fh = file->private_data;
1561 struct vpfe_subdev_info *sdinfo;
1562 int ret = 0;
1564 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1566 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1567 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1568 return -EINVAL;
1571 /* If io is allowed for this file handle, return error */
1572 if (!fh->io_allowed) {
1573 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1574 return -EACCES;
1577 /* If streaming is not started, return error */
1578 if (!vpfe_dev->started) {
1579 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1580 return -EINVAL;
1583 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1584 if (ret)
1585 return ret;
1587 vpfe_stop_ccdc_capture(vpfe_dev);
1588 vpfe_detach_irq(vpfe_dev);
1590 sdinfo = vpfe_dev->current_subdev;
1591 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1592 video, s_stream, 0);
1594 if (ret && (ret != -ENOIOCTLCMD))
1595 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1596 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1597 mutex_unlock(&vpfe_dev->lock);
1598 return ret;
1601 static int vpfe_cropcap(struct file *file, void *priv,
1602 struct v4l2_cropcap *crop)
1604 struct vpfe_device *vpfe_dev = video_drvdata(file);
1606 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1608 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1609 return -EINVAL;
1611 memset(crop, 0, sizeof(struct v4l2_cropcap));
1612 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1613 crop->bounds.width = crop->defrect.width =
1614 vpfe_standards[vpfe_dev->std_index].width;
1615 crop->bounds.height = crop->defrect.height =
1616 vpfe_standards[vpfe_dev->std_index].height;
1617 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1618 return 0;
1621 static int vpfe_g_crop(struct file *file, void *priv,
1622 struct v4l2_crop *crop)
1624 struct vpfe_device *vpfe_dev = video_drvdata(file);
1626 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_crop\n");
1628 crop->c = vpfe_dev->crop;
1629 return 0;
1632 static int vpfe_s_crop(struct file *file, void *priv,
1633 struct v4l2_crop *crop)
1635 struct vpfe_device *vpfe_dev = video_drvdata(file);
1636 int ret = 0;
1638 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_crop\n");
1640 if (vpfe_dev->started) {
1641 /* make sure streaming is not started */
1642 v4l2_err(&vpfe_dev->v4l2_dev,
1643 "Cannot change crop when streaming is ON\n");
1644 return -EBUSY;
1647 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1648 if (ret)
1649 return ret;
1651 if (crop->c.top < 0 || crop->c.left < 0) {
1652 v4l2_err(&vpfe_dev->v4l2_dev,
1653 "doesn't support negative values for top & left\n");
1654 ret = -EINVAL;
1655 goto unlock_out;
1658 /* adjust the width to 16 pixel boundry */
1659 crop->c.width = ((crop->c.width + 15) & ~0xf);
1661 /* make sure parameters are valid */
1662 if ((crop->c.left + crop->c.width >
1663 vpfe_dev->std_info.active_pixels) ||
1664 (crop->c.top + crop->c.height >
1665 vpfe_dev->std_info.active_lines)) {
1666 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_CROP params\n");
1667 ret = -EINVAL;
1668 goto unlock_out;
1670 ccdc_dev->hw_ops.set_image_window(&crop->c);
1671 vpfe_dev->fmt.fmt.pix.width = crop->c.width;
1672 vpfe_dev->fmt.fmt.pix.height = crop->c.height;
1673 vpfe_dev->fmt.fmt.pix.bytesperline =
1674 ccdc_dev->hw_ops.get_line_length();
1675 vpfe_dev->fmt.fmt.pix.sizeimage =
1676 vpfe_dev->fmt.fmt.pix.bytesperline *
1677 vpfe_dev->fmt.fmt.pix.height;
1678 vpfe_dev->crop = crop->c;
1679 unlock_out:
1680 mutex_unlock(&vpfe_dev->lock);
1681 return ret;
1685 static long vpfe_param_handler(struct file *file, void *priv,
1686 int cmd, void *param)
1688 struct vpfe_device *vpfe_dev = video_drvdata(file);
1689 int ret = 0;
1691 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
1693 if (vpfe_dev->started) {
1694 /* only allowed if streaming is not started */
1695 v4l2_err(&vpfe_dev->v4l2_dev, "device already started\n");
1696 return -EBUSY;
1699 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1700 if (ret)
1701 return ret;
1703 switch (cmd) {
1704 case VPFE_CMD_S_CCDC_RAW_PARAMS:
1705 v4l2_warn(&vpfe_dev->v4l2_dev,
1706 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
1707 ret = ccdc_dev->hw_ops.set_params(param);
1708 if (ret) {
1709 v4l2_err(&vpfe_dev->v4l2_dev,
1710 "Error in setting parameters in CCDC\n");
1711 goto unlock_out;
1713 if (vpfe_get_ccdc_image_format(vpfe_dev, &vpfe_dev->fmt) < 0) {
1714 v4l2_err(&vpfe_dev->v4l2_dev,
1715 "Invalid image format at CCDC\n");
1716 goto unlock_out;
1718 break;
1719 default:
1720 ret = -EINVAL;
1722 unlock_out:
1723 mutex_unlock(&vpfe_dev->lock);
1724 return ret;
1728 /* vpfe capture ioctl operations */
1729 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1730 .vidioc_querycap = vpfe_querycap,
1731 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1732 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1733 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1734 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1735 .vidioc_enum_input = vpfe_enum_input,
1736 .vidioc_g_input = vpfe_g_input,
1737 .vidioc_s_input = vpfe_s_input,
1738 .vidioc_querystd = vpfe_querystd,
1739 .vidioc_s_std = vpfe_s_std,
1740 .vidioc_g_std = vpfe_g_std,
1741 .vidioc_queryctrl = vpfe_queryctrl,
1742 .vidioc_g_ctrl = vpfe_g_ctrl,
1743 .vidioc_s_ctrl = vpfe_s_ctrl,
1744 .vidioc_reqbufs = vpfe_reqbufs,
1745 .vidioc_querybuf = vpfe_querybuf,
1746 .vidioc_qbuf = vpfe_qbuf,
1747 .vidioc_dqbuf = vpfe_dqbuf,
1748 .vidioc_streamon = vpfe_streamon,
1749 .vidioc_streamoff = vpfe_streamoff,
1750 .vidioc_cropcap = vpfe_cropcap,
1751 .vidioc_g_crop = vpfe_g_crop,
1752 .vidioc_s_crop = vpfe_s_crop,
1753 .vidioc_default = vpfe_param_handler,
1756 static struct vpfe_device *vpfe_initialize(void)
1758 struct vpfe_device *vpfe_dev;
1760 /* Default number of buffers should be 3 */
1761 if ((numbuffers > 0) &&
1762 (numbuffers < config_params.min_numbuffers))
1763 numbuffers = config_params.min_numbuffers;
1766 * Set buffer size to min buffers size if invalid buffer size is
1767 * given
1769 if (bufsize < config_params.min_bufsize)
1770 bufsize = config_params.min_bufsize;
1772 config_params.numbuffers = numbuffers;
1774 if (numbuffers)
1775 config_params.device_bufsize = bufsize;
1777 /* Allocate memory for device objects */
1778 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1780 return vpfe_dev;
1784 * vpfe_probe : This function creates device entries by register
1785 * itself to the V4L2 driver and initializes fields of each
1786 * device objects
1788 static __init int vpfe_probe(struct platform_device *pdev)
1790 struct vpfe_subdev_info *sdinfo;
1791 struct vpfe_config *vpfe_cfg;
1792 struct resource *res1;
1793 struct vpfe_device *vpfe_dev;
1794 struct i2c_adapter *i2c_adap;
1795 struct video_device *vfd;
1796 int ret = -ENOMEM, i, j;
1797 int num_subdevs = 0;
1799 /* Get the pointer to the device object */
1800 vpfe_dev = vpfe_initialize();
1802 if (!vpfe_dev) {
1803 v4l2_err(pdev->dev.driver,
1804 "Failed to allocate memory for vpfe_dev\n");
1805 return ret;
1808 vpfe_dev->pdev = &pdev->dev;
1810 if (NULL == pdev->dev.platform_data) {
1811 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1812 ret = -ENODEV;
1813 goto probe_free_dev_mem;
1816 vpfe_cfg = pdev->dev.platform_data;
1817 vpfe_dev->cfg = vpfe_cfg;
1818 if (NULL == vpfe_cfg->ccdc ||
1819 NULL == vpfe_cfg->card_name ||
1820 NULL == vpfe_cfg->sub_devs) {
1821 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1822 ret = -ENOENT;
1823 goto probe_free_dev_mem;
1826 mutex_lock(&ccdc_lock);
1827 /* Allocate memory for ccdc configuration */
1828 ccdc_cfg = kmalloc(sizeof(struct ccdc_config), GFP_KERNEL);
1829 if (NULL == ccdc_cfg) {
1830 v4l2_err(pdev->dev.driver,
1831 "Memory allocation failed for ccdc_cfg\n");
1832 goto probe_free_dev_mem;
1835 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1836 /* Get VINT0 irq resource */
1837 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1838 if (!res1) {
1839 v4l2_err(pdev->dev.driver,
1840 "Unable to get interrupt for VINT0\n");
1841 ret = -ENODEV;
1842 goto probe_free_ccdc_cfg_mem;
1844 vpfe_dev->ccdc_irq0 = res1->start;
1846 /* Get VINT1 irq resource */
1847 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1848 if (!res1) {
1849 v4l2_err(pdev->dev.driver,
1850 "Unable to get interrupt for VINT1\n");
1851 ret = -ENODEV;
1852 goto probe_free_ccdc_cfg_mem;
1854 vpfe_dev->ccdc_irq1 = res1->start;
1856 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, IRQF_DISABLED,
1857 "vpfe_capture0", vpfe_dev);
1859 if (0 != ret) {
1860 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1861 goto probe_free_ccdc_cfg_mem;
1864 /* Allocate memory for video device */
1865 vfd = video_device_alloc();
1866 if (NULL == vfd) {
1867 ret = -ENOMEM;
1868 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
1869 goto probe_out_release_irq;
1872 /* Initialize field of video device */
1873 vfd->release = video_device_release;
1874 vfd->fops = &vpfe_fops;
1875 vfd->ioctl_ops = &vpfe_ioctl_ops;
1876 vfd->tvnorms = 0;
1877 vfd->current_norm = V4L2_STD_PAL;
1878 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1879 snprintf(vfd->name, sizeof(vfd->name),
1880 "%s_V%d.%d.%d",
1881 CAPTURE_DRV_NAME,
1882 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1883 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1884 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1885 /* Set video_dev to the video device */
1886 vpfe_dev->video_dev = vfd;
1888 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1889 if (ret) {
1890 v4l2_err(pdev->dev.driver,
1891 "Unable to register v4l2 device.\n");
1892 goto probe_out_video_release;
1894 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1895 spin_lock_init(&vpfe_dev->irqlock);
1896 spin_lock_init(&vpfe_dev->dma_queue_lock);
1897 mutex_init(&vpfe_dev->lock);
1899 /* Initialize field of the device objects */
1900 vpfe_dev->numbuffers = config_params.numbuffers;
1902 /* Initialize prio member of device object */
1903 v4l2_prio_init(&vpfe_dev->prio);
1904 /* register video device */
1905 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1906 "trying to register vpfe device.\n");
1907 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1908 "video_dev=%x\n", (int)&vpfe_dev->video_dev);
1909 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1910 ret = video_register_device(vpfe_dev->video_dev,
1911 VFL_TYPE_GRABBER, -1);
1913 if (ret) {
1914 v4l2_err(pdev->dev.driver,
1915 "Unable to register video device.\n");
1916 goto probe_out_v4l2_unregister;
1919 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1920 /* set the driver data in platform device */
1921 platform_set_drvdata(pdev, vpfe_dev);
1922 /* set driver private data */
1923 video_set_drvdata(vpfe_dev->video_dev, vpfe_dev);
1924 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1925 num_subdevs = vpfe_cfg->num_subdevs;
1926 vpfe_dev->sd = kmalloc(sizeof(struct v4l2_subdev *) * num_subdevs,
1927 GFP_KERNEL);
1928 if (NULL == vpfe_dev->sd) {
1929 v4l2_err(&vpfe_dev->v4l2_dev,
1930 "unable to allocate memory for subdevice pointers\n");
1931 ret = -ENOMEM;
1932 goto probe_out_video_unregister;
1935 for (i = 0; i < num_subdevs; i++) {
1936 struct v4l2_input *inps;
1938 sdinfo = &vpfe_cfg->sub_devs[i];
1940 /* Load up the subdevice */
1941 vpfe_dev->sd[i] =
1942 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1943 i2c_adap,
1944 sdinfo->name,
1945 &sdinfo->board_info,
1946 NULL);
1947 if (vpfe_dev->sd[i]) {
1948 v4l2_info(&vpfe_dev->v4l2_dev,
1949 "v4l2 sub device %s registered\n",
1950 sdinfo->name);
1951 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1952 /* update tvnorms from the sub devices */
1953 for (j = 0; j < sdinfo->num_inputs; j++) {
1954 inps = &sdinfo->inputs[j];
1955 vfd->tvnorms |= inps->std;
1957 } else {
1958 v4l2_info(&vpfe_dev->v4l2_dev,
1959 "v4l2 sub device %s register fails\n",
1960 sdinfo->name);
1961 goto probe_sd_out;
1965 /* set first sub device as current one */
1966 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1968 /* We have at least one sub device to work with */
1969 mutex_unlock(&ccdc_lock);
1970 return 0;
1972 probe_sd_out:
1973 kfree(vpfe_dev->sd);
1974 probe_out_video_unregister:
1975 video_unregister_device(vpfe_dev->video_dev);
1976 probe_out_v4l2_unregister:
1977 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1978 probe_out_video_release:
1979 if (!video_is_registered(vpfe_dev->video_dev))
1980 video_device_release(vpfe_dev->video_dev);
1981 probe_out_release_irq:
1982 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1983 probe_free_ccdc_cfg_mem:
1984 mutex_unlock(&ccdc_lock);
1985 kfree(ccdc_cfg);
1986 probe_free_dev_mem:
1987 kfree(vpfe_dev);
1988 return ret;
1992 * vpfe_remove : It un-register device from V4L2 driver
1994 static int __devexit vpfe_remove(struct platform_device *pdev)
1996 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
1998 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2000 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2001 kfree(vpfe_dev->sd);
2002 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
2003 video_unregister_device(vpfe_dev->video_dev);
2004 kfree(vpfe_dev);
2005 kfree(ccdc_cfg);
2006 return 0;
2009 static int
2010 vpfe_suspend(struct device *dev)
2012 /* add suspend code here later */
2013 return -1;
2016 static int
2017 vpfe_resume(struct device *dev)
2019 /* add resume code here later */
2020 return -1;
2023 static const struct dev_pm_ops vpfe_dev_pm_ops = {
2024 .suspend = vpfe_suspend,
2025 .resume = vpfe_resume,
2028 static struct platform_driver vpfe_driver = {
2029 .driver = {
2030 .name = CAPTURE_DRV_NAME,
2031 .owner = THIS_MODULE,
2032 .pm = &vpfe_dev_pm_ops,
2034 .probe = vpfe_probe,
2035 .remove = __devexit_p(vpfe_remove),
2038 static __init int vpfe_init(void)
2040 printk(KERN_NOTICE "vpfe_init\n");
2041 /* Register driver to the kernel */
2042 return platform_driver_register(&vpfe_driver);
2046 * vpfe_cleanup : This function un-registers device driver
2048 static void vpfe_cleanup(void)
2050 platform_driver_unregister(&vpfe_driver);
2053 module_init(vpfe_init);
2054 module_exit(vpfe_cleanup);