Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / media / platform / davinci / vpfe_capture.c
blobbdb7a0a00932501a7d597d615ff01233da3702d2
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/slab.h>
71 #include <linux/init.h>
72 #include <linux/platform_device.h>
73 #include <linux/interrupt.h>
74 #include <media/v4l2-common.h>
75 #include <linux/io.h>
76 #include <media/davinci/vpfe_capture.h>
77 #include "ccdc_hw_device.h"
79 static int debug;
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 {
97 v4l2_std_id std_id;
98 unsigned int width;
99 unsigned int height;
100 struct v4l2_fract pixelaspect;
101 /* 0 - progressive, 1 - interlaced */
102 int frame_format;
105 /* ccdc configuration */
106 struct ccdc_config {
107 /* This make sure vpfe is probed and ready to go */
108 int vpfe_probed;
109 /* name of ccdc device */
110 char name[32];
113 /* data structures */
114 static struct vpfe_config_params config_params = {
115 .min_numbuffers = 3,
116 .numbuffers = 3,
117 .min_bufsize = 720 * 480 * 2,
118 .device_bufsize = 720 * 576 * 2,
121 /* ccdc device registered */
122 static struct ccdc_hw_device *ccdc_dev;
123 /* lock for accessing ccdc information */
124 static DEFINE_MUTEX(ccdc_lock);
125 /* ccdc configuration */
126 static struct ccdc_config *ccdc_cfg;
128 static const struct vpfe_standard vpfe_standards[] = {
129 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
130 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
133 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
134 static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
136 .fmtdesc = {
137 .index = 0,
138 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
139 .description = "Bayer GrRBGb 8bit A-Law compr.",
140 .pixelformat = V4L2_PIX_FMT_SBGGR8,
142 .bpp = 1,
145 .fmtdesc = {
146 .index = 1,
147 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
148 .description = "Bayer GrRBGb - 16bit",
149 .pixelformat = V4L2_PIX_FMT_SBGGR16,
151 .bpp = 2,
154 .fmtdesc = {
155 .index = 2,
156 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
157 .description = "Bayer GrRBGb 8bit DPCM compr.",
158 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
160 .bpp = 1,
163 .fmtdesc = {
164 .index = 3,
165 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
166 .description = "YCbCr 4:2:2 Interleaved UYVY",
167 .pixelformat = V4L2_PIX_FMT_UYVY,
169 .bpp = 2,
172 .fmtdesc = {
173 .index = 4,
174 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
175 .description = "YCbCr 4:2:2 Interleaved YUYV",
176 .pixelformat = V4L2_PIX_FMT_YUYV,
178 .bpp = 2,
181 .fmtdesc = {
182 .index = 5,
183 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
184 .description = "Y/CbCr 4:2:0 - Semi planar",
185 .pixelformat = V4L2_PIX_FMT_NV12,
187 .bpp = 1,
192 * vpfe_lookup_pix_format()
193 * lookup an entry in the vpfe pix format table based on pix_format
195 static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
197 int i;
199 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
200 if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
201 return &vpfe_pix_fmts[i];
203 return NULL;
207 * vpfe_register_ccdc_device. CCDC module calls this to
208 * register with vpfe capture
210 int vpfe_register_ccdc_device(struct ccdc_hw_device *dev)
212 int ret = 0;
213 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
215 BUG_ON(!dev->hw_ops.open);
216 BUG_ON(!dev->hw_ops.enable);
217 BUG_ON(!dev->hw_ops.set_hw_if_params);
218 BUG_ON(!dev->hw_ops.configure);
219 BUG_ON(!dev->hw_ops.set_buftype);
220 BUG_ON(!dev->hw_ops.get_buftype);
221 BUG_ON(!dev->hw_ops.enum_pix);
222 BUG_ON(!dev->hw_ops.set_frame_format);
223 BUG_ON(!dev->hw_ops.get_frame_format);
224 BUG_ON(!dev->hw_ops.get_pixel_format);
225 BUG_ON(!dev->hw_ops.set_pixel_format);
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 supports get_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 v4l2_std_id std_id)
381 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
382 struct v4l2_subdev_format fmt = {
383 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
385 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
386 struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
387 int i, ret = 0;
389 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
390 if (vpfe_standards[i].std_id & std_id) {
391 vpfe_dev->std_info.active_pixels =
392 vpfe_standards[i].width;
393 vpfe_dev->std_info.active_lines =
394 vpfe_standards[i].height;
395 vpfe_dev->std_info.frame_format =
396 vpfe_standards[i].frame_format;
397 vpfe_dev->std_index = i;
398 break;
402 if (i == ARRAY_SIZE(vpfe_standards)) {
403 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
404 return -EINVAL;
407 vpfe_dev->crop.top = 0;
408 vpfe_dev->crop.left = 0;
409 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
410 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
411 pix->width = vpfe_dev->crop.width;
412 pix->height = vpfe_dev->crop.height;
414 /* first field and frame format based on standard frame format */
415 if (vpfe_dev->std_info.frame_format) {
416 pix->field = V4L2_FIELD_INTERLACED;
417 /* assume V4L2_PIX_FMT_UYVY as default */
418 pix->pixelformat = V4L2_PIX_FMT_UYVY;
419 v4l2_fill_mbus_format(mbus_fmt, pix,
420 MEDIA_BUS_FMT_YUYV10_2X10);
421 } else {
422 pix->field = V4L2_FIELD_NONE;
423 /* assume V4L2_PIX_FMT_SBGGR8 */
424 pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
425 v4l2_fill_mbus_format(mbus_fmt, pix,
426 MEDIA_BUS_FMT_SBGGR8_1X8);
429 /* if sub device supports get_fmt, override the defaults */
430 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
431 sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
433 if (ret && ret != -ENOIOCTLCMD) {
434 v4l2_err(&vpfe_dev->v4l2_dev,
435 "error in getting get_fmt from sub device\n");
436 return ret;
438 v4l2_fill_pix_format(pix, mbus_fmt);
439 pix->bytesperline = pix->width * 2;
440 pix->sizeimage = pix->bytesperline * pix->height;
442 /* Sets the values in CCDC */
443 ret = vpfe_config_ccdc_image_format(vpfe_dev);
444 if (ret)
445 return ret;
447 /* Update the values of sizeimage and bytesperline */
448 pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
449 pix->sizeimage = pix->bytesperline * pix->height;
451 return 0;
454 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
456 int ret = 0;
458 /* set first input of current subdevice as the current input */
459 vpfe_dev->current_input = 0;
461 /* set default standard */
462 vpfe_dev->std_index = 0;
464 /* Configure the default format information */
465 ret = vpfe_config_image_format(vpfe_dev,
466 vpfe_standards[vpfe_dev->std_index].std_id);
467 if (ret)
468 return ret;
470 /* now open the ccdc device to initialize it */
471 mutex_lock(&ccdc_lock);
472 if (NULL == ccdc_dev) {
473 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
474 ret = -ENODEV;
475 goto unlock;
478 if (!try_module_get(ccdc_dev->owner)) {
479 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
480 ret = -ENODEV;
481 goto unlock;
483 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
484 if (!ret)
485 vpfe_dev->initialized = 1;
487 /* Clear all VPFE/CCDC interrupts */
488 if (vpfe_dev->cfg->clr_intr)
489 vpfe_dev->cfg->clr_intr(-1);
491 unlock:
492 mutex_unlock(&ccdc_lock);
493 return ret;
497 * vpfe_open : It creates object of file handle structure and
498 * stores it in private_data member of filepointer
500 static int vpfe_open(struct file *file)
502 struct vpfe_device *vpfe_dev = video_drvdata(file);
503 struct video_device *vdev = video_devdata(file);
504 struct vpfe_fh *fh;
506 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
508 if (!vpfe_dev->cfg->num_subdevs) {
509 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
510 return -ENODEV;
513 /* Allocate memory for the file handle object */
514 fh = kmalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
515 if (NULL == fh) {
516 v4l2_err(&vpfe_dev->v4l2_dev,
517 "unable to allocate memory for file handle object\n");
518 return -ENOMEM;
520 /* store pointer to fh in private_data member of file */
521 file->private_data = fh;
522 fh->vpfe_dev = vpfe_dev;
523 v4l2_fh_init(&fh->fh, vdev);
524 mutex_lock(&vpfe_dev->lock);
525 /* If decoder is not initialized. initialize it */
526 if (!vpfe_dev->initialized) {
527 if (vpfe_initialize_device(vpfe_dev)) {
528 mutex_unlock(&vpfe_dev->lock);
529 return -ENODEV;
532 /* Increment device usrs counter */
533 vpfe_dev->usrs++;
534 /* Set io_allowed member to false */
535 fh->io_allowed = 0;
536 v4l2_fh_add(&fh->fh);
537 mutex_unlock(&vpfe_dev->lock);
538 return 0;
541 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
543 unsigned long addr;
545 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
546 struct videobuf_buffer, queue);
547 list_del(&vpfe_dev->next_frm->queue);
548 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
549 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
551 ccdc_dev->hw_ops.setfbaddr(addr);
554 static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
556 unsigned long addr;
558 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
559 addr += vpfe_dev->field_off;
560 ccdc_dev->hw_ops.setfbaddr(addr);
563 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
565 v4l2_get_timestamp(&vpfe_dev->cur_frm->ts);
566 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
567 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
568 wake_up_interruptible(&vpfe_dev->cur_frm->done);
569 vpfe_dev->cur_frm = vpfe_dev->next_frm;
572 /* ISR for VINT0*/
573 static irqreturn_t vpfe_isr(int irq, void *dev_id)
575 struct vpfe_device *vpfe_dev = dev_id;
576 enum v4l2_field field;
577 int fid;
579 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
580 field = vpfe_dev->fmt.fmt.pix.field;
582 /* if streaming not started, don't do anything */
583 if (!vpfe_dev->started)
584 goto clear_intr;
586 /* only for 6446 this will be applicable */
587 if (NULL != ccdc_dev->hw_ops.reset)
588 ccdc_dev->hw_ops.reset();
590 if (field == V4L2_FIELD_NONE) {
591 /* handle progressive frame capture */
592 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
593 "frame format is progressive...\n");
594 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
595 vpfe_process_buffer_complete(vpfe_dev);
596 goto clear_intr;
599 /* interlaced or TB capture check which field we are in hardware */
600 fid = ccdc_dev->hw_ops.getfid();
602 /* switch the software maintained field id */
603 vpfe_dev->field_id ^= 1;
604 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
605 fid, vpfe_dev->field_id);
606 if (fid == vpfe_dev->field_id) {
607 /* we are in-sync here,continue */
608 if (fid == 0) {
610 * One frame is just being captured. If the next frame
611 * is available, release the current frame and move on
613 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
614 vpfe_process_buffer_complete(vpfe_dev);
616 * based on whether the two fields are stored
617 * interleavely or separately in memory, reconfigure
618 * the CCDC memory address
620 if (field == V4L2_FIELD_SEQ_TB) {
621 vpfe_schedule_bottom_field(vpfe_dev);
623 goto clear_intr;
626 * if one field is just being captured configure
627 * the next frame get the next frame from the empty
628 * queue if no frame is available hold on to the
629 * current buffer
631 spin_lock(&vpfe_dev->dma_queue_lock);
632 if (!list_empty(&vpfe_dev->dma_queue) &&
633 vpfe_dev->cur_frm == vpfe_dev->next_frm)
634 vpfe_schedule_next_buffer(vpfe_dev);
635 spin_unlock(&vpfe_dev->dma_queue_lock);
636 } else if (fid == 0) {
638 * out of sync. Recover from any hardware out-of-sync.
639 * May loose one frame
641 vpfe_dev->field_id = fid;
643 clear_intr:
644 if (vpfe_dev->cfg->clr_intr)
645 vpfe_dev->cfg->clr_intr(irq);
647 return IRQ_HANDLED;
650 /* vdint1_isr - isr handler for VINT1 interrupt */
651 static irqreturn_t vdint1_isr(int irq, void *dev_id)
653 struct vpfe_device *vpfe_dev = dev_id;
655 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
657 /* if streaming not started, don't do anything */
658 if (!vpfe_dev->started) {
659 if (vpfe_dev->cfg->clr_intr)
660 vpfe_dev->cfg->clr_intr(irq);
661 return IRQ_HANDLED;
664 spin_lock(&vpfe_dev->dma_queue_lock);
665 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
666 !list_empty(&vpfe_dev->dma_queue) &&
667 vpfe_dev->cur_frm == vpfe_dev->next_frm)
668 vpfe_schedule_next_buffer(vpfe_dev);
669 spin_unlock(&vpfe_dev->dma_queue_lock);
671 if (vpfe_dev->cfg->clr_intr)
672 vpfe_dev->cfg->clr_intr(irq);
674 return IRQ_HANDLED;
677 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
679 enum ccdc_frmfmt frame_format;
681 frame_format = ccdc_dev->hw_ops.get_frame_format();
682 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
683 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
686 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
688 enum ccdc_frmfmt frame_format;
690 frame_format = ccdc_dev->hw_ops.get_frame_format();
691 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
692 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
693 0, "vpfe_capture1",
694 vpfe_dev);
696 return 0;
699 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
700 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
702 vpfe_dev->started = 0;
703 ccdc_dev->hw_ops.enable(0);
704 if (ccdc_dev->hw_ops.enable_out_to_sdram)
705 ccdc_dev->hw_ops.enable_out_to_sdram(0);
709 * vpfe_release : This function deletes buffer queue, frees the
710 * buffers and the vpfe file handle
712 static int vpfe_release(struct file *file)
714 struct vpfe_device *vpfe_dev = video_drvdata(file);
715 struct vpfe_fh *fh = file->private_data;
716 struct vpfe_subdev_info *sdinfo;
717 int ret;
719 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
721 /* Get the device lock */
722 mutex_lock(&vpfe_dev->lock);
723 /* if this instance is doing IO */
724 if (fh->io_allowed) {
725 if (vpfe_dev->started) {
726 sdinfo = vpfe_dev->current_subdev;
727 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
728 sdinfo->grp_id,
729 video, s_stream, 0);
730 if (ret && (ret != -ENOIOCTLCMD))
731 v4l2_err(&vpfe_dev->v4l2_dev,
732 "stream off failed in subdev\n");
733 vpfe_stop_ccdc_capture(vpfe_dev);
734 vpfe_detach_irq(vpfe_dev);
735 videobuf_streamoff(&vpfe_dev->buffer_queue);
737 vpfe_dev->io_usrs = 0;
738 vpfe_dev->numbuffers = config_params.numbuffers;
739 videobuf_stop(&vpfe_dev->buffer_queue);
740 videobuf_mmap_free(&vpfe_dev->buffer_queue);
743 /* Decrement device usrs counter */
744 vpfe_dev->usrs--;
745 v4l2_fh_del(&fh->fh);
746 v4l2_fh_exit(&fh->fh);
747 /* If this is the last file handle */
748 if (!vpfe_dev->usrs) {
749 vpfe_dev->initialized = 0;
750 if (ccdc_dev->hw_ops.close)
751 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
752 module_put(ccdc_dev->owner);
754 mutex_unlock(&vpfe_dev->lock);
755 file->private_data = NULL;
756 /* Free memory allocated to file handle object */
757 kfree(fh);
758 return 0;
762 * vpfe_mmap : It is used to map kernel space buffers
763 * into user spaces
765 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
767 /* Get the device object and file handle object */
768 struct vpfe_device *vpfe_dev = video_drvdata(file);
770 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
772 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
776 * vpfe_poll: It is used for select/poll system call
778 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
780 struct vpfe_device *vpfe_dev = video_drvdata(file);
782 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
784 if (vpfe_dev->started)
785 return videobuf_poll_stream(file,
786 &vpfe_dev->buffer_queue, wait);
787 return 0;
790 /* vpfe capture driver file operations */
791 static const struct v4l2_file_operations vpfe_fops = {
792 .owner = THIS_MODULE,
793 .open = vpfe_open,
794 .release = vpfe_release,
795 .unlocked_ioctl = video_ioctl2,
796 .mmap = vpfe_mmap,
797 .poll = vpfe_poll
801 * vpfe_check_format()
802 * This function adjust the input pixel format as per hardware
803 * capabilities and update the same in pixfmt.
804 * Following algorithm used :-
806 * If given pixformat is not in the vpfe list of pix formats or not
807 * supported by the hardware, current value of pixformat in the device
808 * is used
809 * If given field is not supported, then current field is used. If field
810 * is different from current, then it is matched with that from sub device.
811 * Minimum height is 2 lines for interlaced or tb field and 1 line for
812 * progressive. Maximum height is clamped to active active lines of scan
813 * Minimum width is 32 bytes in memory and width is clamped to active
814 * pixels of scan.
815 * bytesperline is a multiple of 32.
817 static const struct vpfe_pixel_format *
818 vpfe_check_format(struct vpfe_device *vpfe_dev,
819 struct v4l2_pix_format *pixfmt)
821 u32 min_height = 1, min_width = 32, max_width, max_height;
822 const struct vpfe_pixel_format *vpfe_pix_fmt;
823 u32 pix;
824 int temp, found;
826 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
827 if (NULL == vpfe_pix_fmt) {
829 * use current pixel format in the vpfe device. We
830 * will find this pix format in the table
832 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
833 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
836 /* check if hw supports it */
837 temp = 0;
838 found = 0;
839 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
840 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
841 found = 1;
842 break;
844 temp++;
847 if (!found) {
848 /* use current pixel format */
849 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
851 * Since this is currently used in the vpfe device, we
852 * will find this pix format in the table
854 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
857 /* check what field format is supported */
858 if (pixfmt->field == V4L2_FIELD_ANY) {
859 /* if field is any, use current value as default */
860 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
864 * if field is not same as current field in the vpfe device
865 * try matching the field with the sub device field
867 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
869 * If field value is not in the supported fields, use current
870 * field used in the device as default
872 switch (pixfmt->field) {
873 case V4L2_FIELD_INTERLACED:
874 case V4L2_FIELD_SEQ_TB:
875 /* if sub device is supporting progressive, use that */
876 if (!vpfe_dev->std_info.frame_format)
877 pixfmt->field = V4L2_FIELD_NONE;
878 break;
879 case V4L2_FIELD_NONE:
880 if (vpfe_dev->std_info.frame_format)
881 pixfmt->field = V4L2_FIELD_INTERLACED;
882 break;
884 default:
885 /* use current field as default */
886 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
887 break;
891 /* Now adjust image resolutions supported */
892 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
893 pixfmt->field == V4L2_FIELD_SEQ_TB)
894 min_height = 2;
896 max_width = vpfe_dev->std_info.active_pixels;
897 max_height = vpfe_dev->std_info.active_lines;
898 min_width /= vpfe_pix_fmt->bpp;
900 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
901 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
903 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
904 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
906 /* If interlaced, adjust height to be a multiple of 2 */
907 if (pixfmt->field == V4L2_FIELD_INTERLACED)
908 pixfmt->height &= (~1);
910 * recalculate bytesperline and sizeimage since width
911 * and height might have changed
913 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
914 & ~31);
915 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
916 pixfmt->sizeimage =
917 pixfmt->bytesperline * pixfmt->height +
918 ((pixfmt->bytesperline * pixfmt->height) >> 1);
919 else
920 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
922 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height ="
923 " %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
924 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
925 pixfmt->bytesperline, pixfmt->sizeimage);
926 return vpfe_pix_fmt;
929 static int vpfe_querycap(struct file *file, void *priv,
930 struct v4l2_capability *cap)
932 struct vpfe_device *vpfe_dev = video_drvdata(file);
934 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
936 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
937 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
938 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
939 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
940 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
941 return 0;
944 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
945 struct v4l2_format *fmt)
947 struct vpfe_device *vpfe_dev = video_drvdata(file);
949 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
950 /* Fill in the information about format */
951 *fmt = vpfe_dev->fmt;
952 return 0;
955 static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
956 struct v4l2_fmtdesc *fmt)
958 struct vpfe_device *vpfe_dev = video_drvdata(file);
959 const struct vpfe_pixel_format *pix_fmt;
960 int temp_index;
961 u32 pix;
963 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
965 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
966 return -EINVAL;
968 /* Fill in the information about format */
969 pix_fmt = vpfe_lookup_pix_format(pix);
970 if (NULL != pix_fmt) {
971 temp_index = fmt->index;
972 *fmt = pix_fmt->fmtdesc;
973 fmt->index = temp_index;
974 return 0;
976 return -EINVAL;
979 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
980 struct v4l2_format *fmt)
982 struct vpfe_device *vpfe_dev = video_drvdata(file);
983 const struct vpfe_pixel_format *pix_fmts;
984 int ret = 0;
986 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
988 /* If streaming is started, return error */
989 if (vpfe_dev->started) {
990 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
991 return -EBUSY;
994 /* Check for valid frame format */
995 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
997 if (NULL == pix_fmts)
998 return -EINVAL;
1000 /* store the pixel format in the device object */
1001 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1002 if (ret)
1003 return ret;
1005 /* First detach any IRQ if currently attached */
1006 vpfe_detach_irq(vpfe_dev);
1007 vpfe_dev->fmt = *fmt;
1008 /* set image capture parameters in the ccdc */
1009 ret = vpfe_config_ccdc_image_format(vpfe_dev);
1010 mutex_unlock(&vpfe_dev->lock);
1011 return ret;
1014 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
1015 struct v4l2_format *f)
1017 struct vpfe_device *vpfe_dev = video_drvdata(file);
1018 const struct vpfe_pixel_format *pix_fmts;
1020 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
1022 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
1023 if (NULL == pix_fmts)
1024 return -EINVAL;
1025 return 0;
1029 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1030 * given app input index
1032 static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1033 int *subdev_index,
1034 int *subdev_input_index,
1035 int app_input_index)
1037 struct vpfe_config *cfg = vpfe_dev->cfg;
1038 struct vpfe_subdev_info *sdinfo;
1039 int i, j = 0;
1041 for (i = 0; i < cfg->num_subdevs; i++) {
1042 sdinfo = &cfg->sub_devs[i];
1043 if (app_input_index < (j + sdinfo->num_inputs)) {
1044 *subdev_index = i;
1045 *subdev_input_index = app_input_index - j;
1046 return 0;
1048 j += sdinfo->num_inputs;
1050 return -EINVAL;
1054 * vpfe_get_app_input - Get app input index for a given subdev input index
1055 * driver stores the input index of the current sub device and translate it
1056 * when application request the current input
1058 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1059 int *app_input_index)
1061 struct vpfe_config *cfg = vpfe_dev->cfg;
1062 struct vpfe_subdev_info *sdinfo;
1063 int i, j = 0;
1065 for (i = 0; i < cfg->num_subdevs; i++) {
1066 sdinfo = &cfg->sub_devs[i];
1067 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1068 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1069 return -1;
1070 *app_input_index = j + vpfe_dev->current_input;
1071 return 0;
1073 j += sdinfo->num_inputs;
1075 return -EINVAL;
1078 static int vpfe_enum_input(struct file *file, void *priv,
1079 struct v4l2_input *inp)
1081 struct vpfe_device *vpfe_dev = video_drvdata(file);
1082 struct vpfe_subdev_info *sdinfo;
1083 int subdev, index ;
1085 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1087 if (vpfe_get_subdev_input_index(vpfe_dev,
1088 &subdev,
1089 &index,
1090 inp->index) < 0) {
1091 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found"
1092 " for the subdev\n");
1093 return -EINVAL;
1095 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1096 memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1097 return 0;
1100 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1102 struct vpfe_device *vpfe_dev = video_drvdata(file);
1104 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1106 return vpfe_get_app_input_index(vpfe_dev, index);
1110 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1112 struct vpfe_device *vpfe_dev = video_drvdata(file);
1113 struct v4l2_subdev *sd;
1114 struct vpfe_subdev_info *sdinfo;
1115 int subdev_index, inp_index;
1116 struct vpfe_route *route;
1117 u32 input = 0, output = 0;
1118 int ret = -EINVAL;
1120 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1122 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1123 if (ret)
1124 return ret;
1127 * If streaming is started return device busy
1128 * error
1130 if (vpfe_dev->started) {
1131 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1132 ret = -EBUSY;
1133 goto unlock_out;
1135 ret = vpfe_get_subdev_input_index(vpfe_dev,
1136 &subdev_index,
1137 &inp_index,
1138 index);
1139 if (ret < 0) {
1140 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1141 goto unlock_out;
1144 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1145 sd = vpfe_dev->sd[subdev_index];
1146 route = &sdinfo->routes[inp_index];
1147 if (route && sdinfo->can_route) {
1148 input = route->input;
1149 output = route->output;
1152 if (sd)
1153 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1155 if (ret) {
1156 v4l2_err(&vpfe_dev->v4l2_dev,
1157 "vpfe_doioctl:error in setting input in decoder\n");
1158 ret = -EINVAL;
1159 goto unlock_out;
1161 vpfe_dev->current_subdev = sdinfo;
1162 if (sd)
1163 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
1164 vpfe_dev->current_input = index;
1165 vpfe_dev->std_index = 0;
1167 /* set the bus/interface parameter for the sub device in ccdc */
1168 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1169 if (ret)
1170 goto unlock_out;
1172 /* set the default image parameters in the device */
1173 ret = vpfe_config_image_format(vpfe_dev,
1174 vpfe_standards[vpfe_dev->std_index].std_id);
1175 unlock_out:
1176 mutex_unlock(&vpfe_dev->lock);
1177 return ret;
1180 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1182 struct vpfe_device *vpfe_dev = video_drvdata(file);
1183 struct vpfe_subdev_info *sdinfo;
1184 int ret = 0;
1186 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1188 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1189 sdinfo = vpfe_dev->current_subdev;
1190 if (ret)
1191 return ret;
1192 /* Call querystd function of decoder device */
1193 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1194 video, querystd, std_id);
1195 mutex_unlock(&vpfe_dev->lock);
1196 return ret;
1199 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1201 struct vpfe_device *vpfe_dev = video_drvdata(file);
1202 struct vpfe_subdev_info *sdinfo;
1203 int ret = 0;
1205 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1207 /* Call decoder driver function to set the standard */
1208 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1209 if (ret)
1210 return ret;
1212 sdinfo = vpfe_dev->current_subdev;
1213 /* If streaming is started, return device busy error */
1214 if (vpfe_dev->started) {
1215 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1216 ret = -EBUSY;
1217 goto unlock_out;
1220 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1221 video, s_std, std_id);
1222 if (ret < 0) {
1223 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1224 goto unlock_out;
1226 ret = vpfe_config_image_format(vpfe_dev, std_id);
1228 unlock_out:
1229 mutex_unlock(&vpfe_dev->lock);
1230 return ret;
1233 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1235 struct vpfe_device *vpfe_dev = video_drvdata(file);
1237 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1239 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1240 return 0;
1243 * Videobuf operations
1245 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1246 unsigned int *count,
1247 unsigned int *size)
1249 struct vpfe_fh *fh = vq->priv_data;
1250 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1252 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1253 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1254 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1255 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1256 *size = config_params.device_bufsize;
1258 if (*count < config_params.min_numbuffers)
1259 *count = config_params.min_numbuffers;
1260 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1261 "count=%d, size=%d\n", *count, *size);
1262 return 0;
1265 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1266 struct videobuf_buffer *vb,
1267 enum v4l2_field field)
1269 struct vpfe_fh *fh = vq->priv_data;
1270 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1271 unsigned long addr;
1272 int ret;
1274 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1276 /* If buffer is not initialized, initialize it */
1277 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1278 vb->width = vpfe_dev->fmt.fmt.pix.width;
1279 vb->height = vpfe_dev->fmt.fmt.pix.height;
1280 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1281 vb->field = field;
1283 ret = videobuf_iolock(vq, vb, NULL);
1284 if (ret < 0)
1285 return ret;
1287 addr = videobuf_to_dma_contig(vb);
1288 /* Make sure user addresses are aligned to 32 bytes */
1289 if (!ALIGN(addr, 32))
1290 return -EINVAL;
1292 vb->state = VIDEOBUF_PREPARED;
1294 return 0;
1297 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1298 struct videobuf_buffer *vb)
1300 /* Get the file handle object and device object */
1301 struct vpfe_fh *fh = vq->priv_data;
1302 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1303 unsigned long flags;
1305 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1307 /* add the buffer to the DMA queue */
1308 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1309 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1310 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1312 /* Change state of the buffer */
1313 vb->state = VIDEOBUF_QUEUED;
1316 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1317 struct videobuf_buffer *vb)
1319 struct vpfe_fh *fh = vq->priv_data;
1320 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1321 unsigned long flags;
1323 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1326 * We need to flush the buffer from the dma queue since
1327 * they are de-allocated
1329 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1330 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1331 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1332 videobuf_dma_contig_free(vq, vb);
1333 vb->state = VIDEOBUF_NEEDS_INIT;
1336 static struct videobuf_queue_ops vpfe_videobuf_qops = {
1337 .buf_setup = vpfe_videobuf_setup,
1338 .buf_prepare = vpfe_videobuf_prepare,
1339 .buf_queue = vpfe_videobuf_queue,
1340 .buf_release = vpfe_videobuf_release,
1344 * vpfe_reqbufs. currently support REQBUF only once opening
1345 * the device.
1347 static int vpfe_reqbufs(struct file *file, void *priv,
1348 struct v4l2_requestbuffers *req_buf)
1350 struct vpfe_device *vpfe_dev = video_drvdata(file);
1351 struct vpfe_fh *fh = file->private_data;
1352 int ret = 0;
1354 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1356 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1357 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1358 return -EINVAL;
1361 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1362 if (ret)
1363 return ret;
1365 if (vpfe_dev->io_usrs != 0) {
1366 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1367 ret = -EBUSY;
1368 goto unlock_out;
1371 vpfe_dev->memory = req_buf->memory;
1372 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1373 &vpfe_videobuf_qops,
1374 vpfe_dev->pdev,
1375 &vpfe_dev->irqlock,
1376 req_buf->type,
1377 vpfe_dev->fmt.fmt.pix.field,
1378 sizeof(struct videobuf_buffer),
1379 fh, NULL);
1381 fh->io_allowed = 1;
1382 vpfe_dev->io_usrs = 1;
1383 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1384 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1385 unlock_out:
1386 mutex_unlock(&vpfe_dev->lock);
1387 return ret;
1390 static int vpfe_querybuf(struct file *file, void *priv,
1391 struct v4l2_buffer *buf)
1393 struct vpfe_device *vpfe_dev = video_drvdata(file);
1395 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1397 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1398 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1399 return -EINVAL;
1402 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1403 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1404 return -EINVAL;
1406 /* Call videobuf_querybuf to get information */
1407 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1410 static int vpfe_qbuf(struct file *file, void *priv,
1411 struct v4l2_buffer *p)
1413 struct vpfe_device *vpfe_dev = video_drvdata(file);
1414 struct vpfe_fh *fh = file->private_data;
1416 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1418 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1419 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1420 return -EINVAL;
1424 * If this file handle is not allowed to do IO,
1425 * return error
1427 if (!fh->io_allowed) {
1428 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1429 return -EACCES;
1431 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1434 static int vpfe_dqbuf(struct file *file, void *priv,
1435 struct v4l2_buffer *buf)
1437 struct vpfe_device *vpfe_dev = video_drvdata(file);
1439 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1441 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1442 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1443 return -EINVAL;
1445 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1446 buf, file->f_flags & O_NONBLOCK);
1450 * vpfe_calculate_offsets : This function calculates buffers offset
1451 * for top and bottom field
1453 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1455 struct v4l2_rect image_win;
1457 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1459 ccdc_dev->hw_ops.get_image_window(&image_win);
1460 vpfe_dev->field_off = image_win.height * image_win.width;
1463 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1464 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1466 ccdc_dev->hw_ops.enable(1);
1467 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1468 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1469 vpfe_dev->started = 1;
1473 * vpfe_streamon. Assume the DMA queue is not empty.
1474 * application is expected to call QBUF before calling
1475 * this ioctl. If not, driver returns error
1477 static int vpfe_streamon(struct file *file, void *priv,
1478 enum v4l2_buf_type buf_type)
1480 struct vpfe_device *vpfe_dev = video_drvdata(file);
1481 struct vpfe_fh *fh = file->private_data;
1482 struct vpfe_subdev_info *sdinfo;
1483 unsigned long addr;
1484 int ret = 0;
1486 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1488 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1489 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1490 return -EINVAL;
1493 /* If file handle is not allowed IO, return error */
1494 if (!fh->io_allowed) {
1495 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1496 return -EACCES;
1499 sdinfo = vpfe_dev->current_subdev;
1500 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1501 video, s_stream, 1);
1503 if (ret && (ret != -ENOIOCTLCMD)) {
1504 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1505 return -EINVAL;
1508 /* If buffer queue is empty, return error */
1509 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1510 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1511 return -EIO;
1514 /* Call videobuf_streamon to start streaming * in videobuf */
1515 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1516 if (ret)
1517 return ret;
1520 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1521 if (ret)
1522 goto streamoff;
1523 /* Get the next frame from the buffer queue */
1524 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1525 struct videobuf_buffer, queue);
1526 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1527 /* Remove buffer from the buffer queue */
1528 list_del(&vpfe_dev->cur_frm->queue);
1529 /* Mark state of the current frame to active */
1530 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1531 /* Initialize field_id and started member */
1532 vpfe_dev->field_id = 0;
1533 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1535 /* Calculate field offset */
1536 vpfe_calculate_offsets(vpfe_dev);
1538 if (vpfe_attach_irq(vpfe_dev) < 0) {
1539 v4l2_err(&vpfe_dev->v4l2_dev,
1540 "Error in attaching interrupt handle\n");
1541 ret = -EFAULT;
1542 goto unlock_out;
1544 if (ccdc_dev->hw_ops.configure() < 0) {
1545 v4l2_err(&vpfe_dev->v4l2_dev,
1546 "Error in configuring ccdc\n");
1547 ret = -EINVAL;
1548 goto unlock_out;
1550 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1551 vpfe_start_ccdc_capture(vpfe_dev);
1552 mutex_unlock(&vpfe_dev->lock);
1553 return ret;
1554 unlock_out:
1555 mutex_unlock(&vpfe_dev->lock);
1556 streamoff:
1557 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1558 return ret;
1561 static int vpfe_streamoff(struct file *file, void *priv,
1562 enum v4l2_buf_type buf_type)
1564 struct vpfe_device *vpfe_dev = video_drvdata(file);
1565 struct vpfe_fh *fh = file->private_data;
1566 struct vpfe_subdev_info *sdinfo;
1567 int ret = 0;
1569 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1571 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1572 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1573 return -EINVAL;
1576 /* If io is allowed for this file handle, return error */
1577 if (!fh->io_allowed) {
1578 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1579 return -EACCES;
1582 /* If streaming is not started, return error */
1583 if (!vpfe_dev->started) {
1584 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1585 return -EINVAL;
1588 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1589 if (ret)
1590 return ret;
1592 vpfe_stop_ccdc_capture(vpfe_dev);
1593 vpfe_detach_irq(vpfe_dev);
1595 sdinfo = vpfe_dev->current_subdev;
1596 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1597 video, s_stream, 0);
1599 if (ret && (ret != -ENOIOCTLCMD))
1600 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1601 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1602 mutex_unlock(&vpfe_dev->lock);
1603 return ret;
1606 static int vpfe_cropcap(struct file *file, void *priv,
1607 struct v4l2_cropcap *crop)
1609 struct vpfe_device *vpfe_dev = video_drvdata(file);
1611 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1613 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1614 return -EINVAL;
1615 /* If std_index is invalid, then just return (== 1:1 aspect) */
1616 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1617 return 0;
1619 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1620 return 0;
1623 static int vpfe_g_selection(struct file *file, void *priv,
1624 struct v4l2_selection *sel)
1626 struct vpfe_device *vpfe_dev = video_drvdata(file);
1628 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
1630 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1631 return -EINVAL;
1633 switch (sel->target) {
1634 case V4L2_SEL_TGT_CROP:
1635 sel->r = vpfe_dev->crop;
1636 break;
1637 case V4L2_SEL_TGT_CROP_DEFAULT:
1638 case V4L2_SEL_TGT_CROP_BOUNDS:
1639 sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1640 sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1641 break;
1642 default:
1643 return -EINVAL;
1645 return 0;
1648 static int vpfe_s_selection(struct file *file, void *priv,
1649 struct v4l2_selection *sel)
1651 struct vpfe_device *vpfe_dev = video_drvdata(file);
1652 struct v4l2_rect rect = sel->r;
1653 int ret = 0;
1655 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1657 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1658 sel->target != V4L2_SEL_TGT_CROP)
1659 return -EINVAL;
1661 if (vpfe_dev->started) {
1662 /* make sure streaming is not started */
1663 v4l2_err(&vpfe_dev->v4l2_dev,
1664 "Cannot change crop when streaming is ON\n");
1665 return -EBUSY;
1668 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1669 if (ret)
1670 return ret;
1672 if (rect.top < 0 || rect.left < 0) {
1673 v4l2_err(&vpfe_dev->v4l2_dev,
1674 "doesn't support negative values for top & left\n");
1675 ret = -EINVAL;
1676 goto unlock_out;
1679 /* adjust the width to 16 pixel boundary */
1680 rect.width = ((rect.width + 15) & ~0xf);
1682 /* make sure parameters are valid */
1683 if ((rect.left + rect.width >
1684 vpfe_dev->std_info.active_pixels) ||
1685 (rect.top + rect.height >
1686 vpfe_dev->std_info.active_lines)) {
1687 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
1688 ret = -EINVAL;
1689 goto unlock_out;
1691 ccdc_dev->hw_ops.set_image_window(&rect);
1692 vpfe_dev->fmt.fmt.pix.width = rect.width;
1693 vpfe_dev->fmt.fmt.pix.height = rect.height;
1694 vpfe_dev->fmt.fmt.pix.bytesperline =
1695 ccdc_dev->hw_ops.get_line_length();
1696 vpfe_dev->fmt.fmt.pix.sizeimage =
1697 vpfe_dev->fmt.fmt.pix.bytesperline *
1698 vpfe_dev->fmt.fmt.pix.height;
1699 vpfe_dev->crop = rect;
1700 sel->r = rect;
1701 unlock_out:
1702 mutex_unlock(&vpfe_dev->lock);
1703 return ret;
1707 static long vpfe_param_handler(struct file *file, void *priv,
1708 bool valid_prio, unsigned int cmd, void *param)
1710 struct vpfe_device *vpfe_dev = video_drvdata(file);
1711 int ret = 0;
1713 v4l2_dbg(2, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
1715 if (vpfe_dev->started) {
1716 /* only allowed if streaming is not started */
1717 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1718 "device already started\n");
1719 return -EBUSY;
1722 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1723 if (ret)
1724 return ret;
1726 switch (cmd) {
1727 case VPFE_CMD_S_CCDC_RAW_PARAMS:
1728 ret = -EINVAL;
1729 v4l2_warn(&vpfe_dev->v4l2_dev,
1730 "VPFE_CMD_S_CCDC_RAW_PARAMS not supported\n");
1731 break;
1732 default:
1733 ret = -ENOTTY;
1735 unlock_out:
1736 mutex_unlock(&vpfe_dev->lock);
1737 return ret;
1741 /* vpfe capture ioctl operations */
1742 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1743 .vidioc_querycap = vpfe_querycap,
1744 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1745 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1746 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1747 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1748 .vidioc_enum_input = vpfe_enum_input,
1749 .vidioc_g_input = vpfe_g_input,
1750 .vidioc_s_input = vpfe_s_input,
1751 .vidioc_querystd = vpfe_querystd,
1752 .vidioc_s_std = vpfe_s_std,
1753 .vidioc_g_std = vpfe_g_std,
1754 .vidioc_reqbufs = vpfe_reqbufs,
1755 .vidioc_querybuf = vpfe_querybuf,
1756 .vidioc_qbuf = vpfe_qbuf,
1757 .vidioc_dqbuf = vpfe_dqbuf,
1758 .vidioc_streamon = vpfe_streamon,
1759 .vidioc_streamoff = vpfe_streamoff,
1760 .vidioc_cropcap = vpfe_cropcap,
1761 .vidioc_g_selection = vpfe_g_selection,
1762 .vidioc_s_selection = vpfe_s_selection,
1763 .vidioc_default = vpfe_param_handler,
1766 static struct vpfe_device *vpfe_initialize(void)
1768 struct vpfe_device *vpfe_dev;
1770 /* Default number of buffers should be 3 */
1771 if ((numbuffers > 0) &&
1772 (numbuffers < config_params.min_numbuffers))
1773 numbuffers = config_params.min_numbuffers;
1776 * Set buffer size to min buffers size if invalid buffer size is
1777 * given
1779 if (bufsize < config_params.min_bufsize)
1780 bufsize = config_params.min_bufsize;
1782 config_params.numbuffers = numbuffers;
1784 if (numbuffers)
1785 config_params.device_bufsize = bufsize;
1787 /* Allocate memory for device objects */
1788 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1790 return vpfe_dev;
1794 * vpfe_probe : This function creates device entries by register
1795 * itself to the V4L2 driver and initializes fields of each
1796 * device objects
1798 static int vpfe_probe(struct platform_device *pdev)
1800 struct vpfe_subdev_info *sdinfo;
1801 struct vpfe_config *vpfe_cfg;
1802 struct resource *res1;
1803 struct vpfe_device *vpfe_dev;
1804 struct i2c_adapter *i2c_adap;
1805 struct video_device *vfd;
1806 int ret = -ENOMEM, i, j;
1807 int num_subdevs = 0;
1809 /* Get the pointer to the device object */
1810 vpfe_dev = vpfe_initialize();
1812 if (!vpfe_dev) {
1813 v4l2_err(pdev->dev.driver,
1814 "Failed to allocate memory for vpfe_dev\n");
1815 return ret;
1818 vpfe_dev->pdev = &pdev->dev;
1820 if (NULL == pdev->dev.platform_data) {
1821 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1822 ret = -ENODEV;
1823 goto probe_free_dev_mem;
1826 vpfe_cfg = pdev->dev.platform_data;
1827 vpfe_dev->cfg = vpfe_cfg;
1828 if (NULL == vpfe_cfg->ccdc ||
1829 NULL == vpfe_cfg->card_name ||
1830 NULL == vpfe_cfg->sub_devs) {
1831 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1832 ret = -ENOENT;
1833 goto probe_free_dev_mem;
1836 /* Allocate memory for ccdc configuration */
1837 ccdc_cfg = kmalloc(sizeof(struct ccdc_config), GFP_KERNEL);
1838 if (NULL == ccdc_cfg) {
1839 v4l2_err(pdev->dev.driver,
1840 "Memory allocation failed for ccdc_cfg\n");
1841 goto probe_free_dev_mem;
1844 mutex_lock(&ccdc_lock);
1846 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1847 /* Get VINT0 irq resource */
1848 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1849 if (!res1) {
1850 v4l2_err(pdev->dev.driver,
1851 "Unable to get interrupt for VINT0\n");
1852 ret = -ENODEV;
1853 goto probe_free_ccdc_cfg_mem;
1855 vpfe_dev->ccdc_irq0 = res1->start;
1857 /* Get VINT1 irq resource */
1858 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1859 if (!res1) {
1860 v4l2_err(pdev->dev.driver,
1861 "Unable to get interrupt for VINT1\n");
1862 ret = -ENODEV;
1863 goto probe_free_ccdc_cfg_mem;
1865 vpfe_dev->ccdc_irq1 = res1->start;
1867 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
1868 "vpfe_capture0", vpfe_dev);
1870 if (0 != ret) {
1871 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1872 goto probe_free_ccdc_cfg_mem;
1875 vfd = &vpfe_dev->video_dev;
1876 /* Initialize field of video device */
1877 vfd->release = video_device_release_empty;
1878 vfd->fops = &vpfe_fops;
1879 vfd->ioctl_ops = &vpfe_ioctl_ops;
1880 vfd->tvnorms = 0;
1881 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1882 snprintf(vfd->name, sizeof(vfd->name),
1883 "%s_V%d.%d.%d",
1884 CAPTURE_DRV_NAME,
1885 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1886 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1887 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1889 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1890 if (ret) {
1891 v4l2_err(pdev->dev.driver,
1892 "Unable to register v4l2 device.\n");
1893 goto probe_out_release_irq;
1895 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1896 spin_lock_init(&vpfe_dev->irqlock);
1897 spin_lock_init(&vpfe_dev->dma_queue_lock);
1898 mutex_init(&vpfe_dev->lock);
1900 /* Initialize field of the device objects */
1901 vpfe_dev->numbuffers = config_params.numbuffers;
1903 /* register video device */
1904 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1905 "trying to register vpfe device.\n");
1906 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1907 "video_dev=%p\n", &vpfe_dev->video_dev);
1908 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1909 ret = video_register_device(&vpfe_dev->video_dev,
1910 VFL_TYPE_GRABBER, -1);
1912 if (ret) {
1913 v4l2_err(pdev->dev.driver,
1914 "Unable to register video device.\n");
1915 goto probe_out_v4l2_unregister;
1918 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1919 /* set the driver data in platform device */
1920 platform_set_drvdata(pdev, vpfe_dev);
1921 /* set driver private data */
1922 video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
1923 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1924 num_subdevs = vpfe_cfg->num_subdevs;
1925 vpfe_dev->sd = kmalloc(sizeof(struct v4l2_subdev *) * num_subdevs,
1926 GFP_KERNEL);
1927 if (NULL == vpfe_dev->sd) {
1928 v4l2_err(&vpfe_dev->v4l2_dev,
1929 "unable to allocate memory for subdevice pointers\n");
1930 ret = -ENOMEM;
1931 goto probe_out_video_unregister;
1934 for (i = 0; i < num_subdevs; i++) {
1935 struct v4l2_input *inps;
1937 sdinfo = &vpfe_cfg->sub_devs[i];
1939 /* Load up the subdevice */
1940 vpfe_dev->sd[i] =
1941 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1942 i2c_adap,
1943 &sdinfo->board_info,
1944 NULL);
1945 if (vpfe_dev->sd[i]) {
1946 v4l2_info(&vpfe_dev->v4l2_dev,
1947 "v4l2 sub device %s registered\n",
1948 sdinfo->name);
1949 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1950 /* update tvnorms from the sub devices */
1951 for (j = 0; j < sdinfo->num_inputs; j++) {
1952 inps = &sdinfo->inputs[j];
1953 vfd->tvnorms |= inps->std;
1955 } else {
1956 v4l2_info(&vpfe_dev->v4l2_dev,
1957 "v4l2 sub device %s register fails\n",
1958 sdinfo->name);
1959 goto probe_sd_out;
1963 /* set first sub device as current one */
1964 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1965 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
1967 /* We have at least one sub device to work with */
1968 mutex_unlock(&ccdc_lock);
1969 return 0;
1971 probe_sd_out:
1972 kfree(vpfe_dev->sd);
1973 probe_out_video_unregister:
1974 video_unregister_device(&vpfe_dev->video_dev);
1975 probe_out_v4l2_unregister:
1976 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1977 probe_out_release_irq:
1978 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1979 probe_free_ccdc_cfg_mem:
1980 kfree(ccdc_cfg);
1981 mutex_unlock(&ccdc_lock);
1982 probe_free_dev_mem:
1983 kfree(vpfe_dev);
1984 return ret;
1988 * vpfe_remove : It un-register device from V4L2 driver
1990 static int vpfe_remove(struct platform_device *pdev)
1992 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
1994 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
1996 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1997 kfree(vpfe_dev->sd);
1998 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1999 video_unregister_device(&vpfe_dev->video_dev);
2000 kfree(vpfe_dev);
2001 kfree(ccdc_cfg);
2002 return 0;
2005 static int vpfe_suspend(struct device *dev)
2007 return 0;
2010 static int vpfe_resume(struct device *dev)
2012 return 0;
2015 static const struct dev_pm_ops vpfe_dev_pm_ops = {
2016 .suspend = vpfe_suspend,
2017 .resume = vpfe_resume,
2020 static struct platform_driver vpfe_driver = {
2021 .driver = {
2022 .name = CAPTURE_DRV_NAME,
2023 .pm = &vpfe_dev_pm_ops,
2025 .probe = vpfe_probe,
2026 .remove = vpfe_remove,
2029 module_platform_driver(vpfe_driver);