Merge branch 'timers-for-linus-hpet' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / media / video / davinci / vpfe_capture.c
1 /*
2  * Copyright (C) 2008-2009 Texas Instruments Inc
3  *
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.
8  *
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.
13  *
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
17  *
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.
23  *
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.
30  *
31  *
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).
52  *
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
68  */
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"
78
79 static int debug;
80 static u32 numbuffers = 3;
81 static u32 bufsize = (720 * 576 * 2);
82
83 module_param(numbuffers, uint, S_IRUGO);
84 module_param(bufsize, uint, S_IRUGO);
85 module_param(debug, int, 0644);
86
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");
90
91 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
92 MODULE_LICENSE("GPL");
93 MODULE_AUTHOR("Texas Instruments");
94
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;
103 };
104
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];
111 };
112
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,
119 };
120
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;
127
128 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},
131 };
132
133 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
134 static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
135         {
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,
141                 },
142                 .bpp = 1,
143         },
144         {
145                 .fmtdesc = {
146                         .index = 1,
147                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
148                         .description = "Bayer GrRBGb - 16bit",
149                         .pixelformat = V4L2_PIX_FMT_SBGGR16,
150                 },
151                 .bpp = 2,
152         },
153         {
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,
159                 },
160                 .bpp = 1,
161         },
162         {
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,
168                 },
169                 .bpp = 2,
170         },
171         {
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,
177                 },
178                 .bpp = 2,
179         },
180         {
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,
186                 },
187                 .bpp = 1,
188         },
189 };
190
191 /*
192  * vpfe_lookup_pix_format()
193  * lookup an entry in the vpfe pix format table based on pix_format
194  */
195 static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
196 {
197         int i;
198
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];
202         }
203         return NULL;
204 }
205
206 /*
207  * vpfe_register_ccdc_device. CCDC module calls this to
208  * register with vpfe capture
209  */
210 int vpfe_register_ccdc_device(struct ccdc_hw_device *dev)
211 {
212         int ret = 0;
213         printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
214
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);
230
231         mutex_lock(&ccdc_lock);
232         if (NULL == ccdc_cfg) {
233                 /*
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
237                  */
238                 printk(KERN_ERR "vpfe capture not initialized\n");
239                 ret = -EFAULT;
240                 goto unlock;
241         }
242
243         if (strcmp(dev->name, ccdc_cfg->name)) {
244                 /* ignore this ccdc */
245                 ret = -EINVAL;
246                 goto unlock;
247         }
248
249         if (ccdc_dev) {
250                 printk(KERN_ERR "ccdc already registered\n");
251                 ret = -EINVAL;
252                 goto unlock;
253         }
254
255         ccdc_dev = dev;
256 unlock:
257         mutex_unlock(&ccdc_lock);
258         return ret;
259 }
260 EXPORT_SYMBOL(vpfe_register_ccdc_device);
261
262 /*
263  * vpfe_unregister_ccdc_device. CCDC module calls this to
264  * unregister with vpfe capture
265  */
266 void vpfe_unregister_ccdc_device(struct ccdc_hw_device *dev)
267 {
268         if (NULL == dev) {
269                 printk(KERN_ERR "invalid ccdc device ptr\n");
270                 return;
271         }
272
273         printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
274                 dev->name);
275
276         if (strcmp(dev->name, ccdc_cfg->name)) {
277                 /* ignore this ccdc */
278                 return;
279         }
280
281         mutex_lock(&ccdc_lock);
282         ccdc_dev = NULL;
283         mutex_unlock(&ccdc_lock);
284         return;
285 }
286 EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
287
288 /*
289  * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
290  */
291 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe_dev,
292                                  struct v4l2_format *f)
293 {
294         struct v4l2_rect image_win;
295         enum ccdc_buftype buf_type;
296         enum ccdc_frmfmt frm_fmt;
297
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;
319                 }
320         } else {
321                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid frm_fmt\n");
322                 return -EINVAL;
323         }
324         return 0;
325 }
326
327 /*
328  * vpfe_config_ccdc_image_format()
329  * For a pix format, configure ccdc to setup the capture
330  */
331 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
332 {
333         enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
334         int ret = 0;
335
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;
341         }
342         /* configure the image window */
343         ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
344
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;
361         }
362
363         /* set the frame format */
364         if (!ret)
365                 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
366         return ret;
367 }
368 /*
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
377  */
378 static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
379                                     const v4l2_std_id *std_id)
380 {
381         struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
382         int i, ret = 0;
383
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;
394                 }
395         }
396
397         if (i ==  ARRAY_SIZE(vpfe_standards)) {
398                 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
399                 return -EINVAL;
400         }
401
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;
408
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;
418         }
419
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);
423
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;
428         }
429
430         /* Sets the values in CCDC */
431         ret = vpfe_config_ccdc_image_format(vpfe_dev);
432         if (ret)
433                 return ret;
434
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;
442         }
443         return ret;
444 }
445
446 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
447 {
448         int ret = 0;
449
450         /* set first input of current subdevice as the current input */
451         vpfe_dev->current_input = 0;
452
453         /* set default standard */
454         vpfe_dev->std_index = 0;
455
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;
461
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;
468         }
469
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;
474         }
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;
481 }
482
483 /*
484  * vpfe_open : It creates object of file handle structure and
485  * stores it in private_data  member of filepointer
486  */
487 static int vpfe_open(struct file *file)
488 {
489         struct vpfe_device *vpfe_dev = video_drvdata(file);
490         struct vpfe_fh *fh;
491
492         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
493
494         if (!vpfe_dev->cfg->num_subdevs) {
495                 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
496                 return -ENODEV;
497         }
498
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;
505         }
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;
515                 }
516         }
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;
526 }
527
528 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
529 {
530         unsigned long addr;
531
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);
538 }
539
540 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
541 {
542         struct timeval timevalue;
543
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;
550 }
551
552 /* ISR for VINT0*/
553 static irqreturn_t vpfe_isr(int irq, void *dev_id)
554 {
555         struct vpfe_device *vpfe_dev = dev_id;
556         enum v4l2_field field;
557         unsigned long addr;
558         int fid;
559
560         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
561         field = vpfe_dev->fmt.fmt.pix.field;
562
563         /* if streaming not started, don't do anything */
564         if (!vpfe_dev->started)
565                 return IRQ_HANDLED;
566
567         /* only for 6446 this will be applicable */
568         if (NULL != ccdc_dev->hw_ops.reset)
569                 ccdc_dev->hw_ops.reset();
570
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;
578         }
579
580         /* interlaced or TB capture check which field we are in hardware */
581         fid = ccdc_dev->hw_ops.getfid();
582
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) {
590                         /*
591                          * One frame is just being captured. If the next frame
592                          * is available, release the current frame and move on
593                          */
594                         if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
595                                 vpfe_process_buffer_complete(vpfe_dev);
596                         /*
597                          * based on whether the two fields are stored
598                          * interleavely or separately in memory, reconfigure
599                          * the CCDC memory address
600                          */
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);
606                         }
607                         return IRQ_HANDLED;
608                 }
609                 /*
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
614                  */
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) {
621                 /*
622                  * out of sync. Recover from any hardware out-of-sync.
623                  * May loose one frame
624                  */
625                 vpfe_dev->field_id = fid;
626         }
627         return IRQ_HANDLED;
628 }
629
630 /* vdint1_isr - isr handler for VINT1 interrupt */
631 static irqreturn_t vdint1_isr(int irq, void *dev_id)
632 {
633         struct vpfe_device *vpfe_dev = dev_id;
634
635         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
636
637         /* if streaming not started, don't do anything */
638         if (!vpfe_dev->started)
639                 return IRQ_HANDLED;
640
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;
648 }
649
650 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
651 {
652         enum ccdc_frmfmt frame_format;
653
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);
657 }
658
659 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
660 {
661         enum ccdc_frmfmt frame_format;
662
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);
668         }
669         return 0;
670 }
671
672 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
673 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
674 {
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);
679 }
680
681 /*
682  * vpfe_release : This function deletes buffer queue, frees the
683  * buffers and the vpfe file  handle
684  */
685 static int vpfe_release(struct file *file)
686 {
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;
691
692         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
693
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);
709                 }
710                 vpfe_dev->io_usrs = 0;
711                 vpfe_dev->numbuffers = config_params.numbuffers;
712         }
713
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);
724         }
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;
730 }
731
732 /*
733  * vpfe_mmap : It is used to map kernel space buffers
734  * into user spaces
735  */
736 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
737 {
738         /* Get the device object and file handle object */
739         struct vpfe_device *vpfe_dev = video_drvdata(file);
740
741         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
742
743         return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
744 }
745
746 /*
747  * vpfe_poll: It is used for select/poll system call
748  */
749 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
750 {
751         struct vpfe_device *vpfe_dev = video_drvdata(file);
752
753         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
754
755         if (vpfe_dev->started)
756                 return videobuf_poll_stream(file,
757                                             &vpfe_dev->buffer_queue, wait);
758         return 0;
759 }
760
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
769 };
770
771 /*
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 :-
776  *
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.
787  */
788 static const struct vpfe_pixel_format *
789         vpfe_check_format(struct vpfe_device *vpfe_dev,
790                           struct v4l2_pix_format *pixfmt)
791 {
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;
796
797         vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
798         if (NULL == vpfe_pix_fmt) {
799                 /*
800                  * use current pixel format in the vpfe device. We
801                  * will find this pix format in the table
802                  */
803                 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
804                 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
805         }
806
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;
814                 }
815                 temp++;
816         }
817
818         if (!found) {
819                 /* use current pixel format */
820                 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
821                 /*
822                  * Since this is currently used in the vpfe device, we
823                  * will find this pix format in the table
824                  */
825                 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
826         }
827
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;
832         }
833
834         /*
835          * if field is not same as current field in the vpfe device
836          * try matching the field with the sub device field
837          */
838         if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
839                 /*
840                  * If field value is not in the supported fields, use current
841                  * field used in the device as default
842                  */
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;
854
855                 default:
856                         /* use current field as default */
857                         pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
858                         break;
859                 }
860         }
861
862         /* Now adjust image resolutions supported */
863         if (pixfmt->field == V4L2_FIELD_INTERLACED ||
864             pixfmt->field == V4L2_FIELD_SEQ_TB)
865                 min_height = 2;
866
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;
870
871         v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
872                   pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
873
874         pixfmt->width = clamp((pixfmt->width), min_width, max_width);
875         pixfmt->height = clamp((pixfmt->height), min_height, max_height);
876
877         /* If interlaced, adjust height to be a multiple of 2 */
878         if (pixfmt->field == V4L2_FIELD_INTERLACED)
879                 pixfmt->height &= (~1);
880         /*
881          * recalculate bytesperline and sizeimage since width
882          * and height might have changed
883          */
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;
892
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;
898 }
899
900 static int vpfe_querycap(struct file *file, void  *priv,
901                                struct v4l2_capability *cap)
902 {
903         struct vpfe_device *vpfe_dev = video_drvdata(file);
904
905         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
906
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;
913 }
914
915 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
916                                 struct v4l2_format *fmt)
917 {
918         struct vpfe_device *vpfe_dev = video_drvdata(file);
919         int ret = 0;
920
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;
925 }
926
927 static int vpfe_enum_fmt_vid_cap(struct file *file, void  *priv,
928                                    struct v4l2_fmtdesc *fmt)
929 {
930         struct vpfe_device *vpfe_dev = video_drvdata(file);
931         const struct vpfe_pixel_format *pix_fmt;
932         int temp_index;
933         u32 pix;
934
935         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
936
937         if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
938                 return -EINVAL;
939
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;
947         }
948         return -EINVAL;
949 }
950
951 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
952                                 struct v4l2_format *fmt)
953 {
954         struct vpfe_device *vpfe_dev = video_drvdata(file);
955         const struct vpfe_pixel_format *pix_fmts;
956         int ret = 0;
957
958         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
959
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;
964         }
965
966         /* Check for valid frame format */
967         pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
968
969         if (NULL == pix_fmts)
970                 return -EINVAL;
971
972         /* store the pixel format in the device  object */
973         ret = mutex_lock_interruptible(&vpfe_dev->lock);
974         if (ret)
975                 return ret;
976
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;
984 }
985
986 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
987                                   struct v4l2_format *f)
988 {
989         struct vpfe_device *vpfe_dev = video_drvdata(file);
990         const struct vpfe_pixel_format *pix_fmts;
991
992         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
993
994         pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
995         if (NULL == pix_fmts)
996                 return -EINVAL;
997         return 0;
998 }
999
1000 /*
1001  * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1002  * given app input index
1003  */
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)
1008 {
1009         struct vpfe_config *cfg = vpfe_dev->cfg;
1010         struct vpfe_subdev_info *sdinfo;
1011         int i, j = 0;
1012
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;
1019                 }
1020                 j += sdinfo->num_inputs;
1021         }
1022         return -EINVAL;
1023 }
1024
1025 /*
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
1029  */
1030 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1031                                     int *app_input_index)
1032 {
1033         struct vpfe_config *cfg = vpfe_dev->cfg;
1034         struct vpfe_subdev_info *sdinfo;
1035         int i, j = 0;
1036
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;
1044                 }
1045                 j += sdinfo->num_inputs;
1046         }
1047         return -EINVAL;
1048 }
1049
1050 static int vpfe_enum_input(struct file *file, void *priv,
1051                                  struct v4l2_input *inp)
1052 {
1053         struct vpfe_device *vpfe_dev = video_drvdata(file);
1054         struct vpfe_subdev_info *sdinfo;
1055         int subdev, index ;
1056
1057         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1058
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;
1066         }
1067         sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1068         memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1069         return 0;
1070 }
1071
1072 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1073 {
1074         struct vpfe_device *vpfe_dev = video_drvdata(file);
1075
1076         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1077
1078         return vpfe_get_app_input_index(vpfe_dev, index);
1079 }
1080
1081
1082 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1083 {
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;
1090
1091         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1092
1093         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1094         if (ret)
1095                 return ret;
1096
1097         /*
1098          * If streaming is started return device busy
1099          * error
1100          */
1101         if (vpfe_dev->started) {
1102                 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1103                 ret = -EBUSY;
1104                 goto unlock_out;
1105         }
1106
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;
1113         }
1114
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;
1120         }
1121
1122         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1123                                          video, s_routing, input, output, 0);
1124
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;
1130         }
1131         vpfe_dev->current_subdev = sdinfo;
1132         vpfe_dev->current_input = index;
1133         vpfe_dev->std_index = 0;
1134
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;
1139
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;
1146 }
1147
1148 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1149 {
1150         struct vpfe_device *vpfe_dev = video_drvdata(file);
1151         struct vpfe_subdev_info *sdinfo;
1152         int ret = 0;
1153
1154         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1155
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;
1165 }
1166
1167 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1168 {
1169         struct vpfe_device *vpfe_dev = video_drvdata(file);
1170         struct vpfe_subdev_info *sdinfo;
1171         int ret = 0;
1172
1173         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1174
1175         /* Call decoder driver function to set the standard */
1176         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1177         if (ret)
1178                 return ret;
1179
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;
1186         }
1187
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;
1193         }
1194         ret = vpfe_config_image_format(vpfe_dev, std_id);
1195
1196 unlock_out:
1197         mutex_unlock(&vpfe_dev->lock);
1198         return ret;
1199 }
1200
1201 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1202 {
1203         struct vpfe_device *vpfe_dev = video_drvdata(file);
1204
1205         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1206
1207         *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1208         return 0;
1209 }
1210 /*
1211  *  Videobuf operations
1212  */
1213 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1214                                 unsigned int *count,
1215                                 unsigned int *size)
1216 {
1217         struct vpfe_fh *fh = vq->priv_data;
1218         struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1219
1220         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1221         *size = config_params.device_bufsize;
1222
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;
1228 }
1229
1230 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1231                                 struct videobuf_buffer *vb,
1232                                 enum v4l2_field field)
1233 {
1234         struct vpfe_fh *fh = vq->priv_data;
1235         struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1236
1237         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1238
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;
1245         }
1246         vb->state = VIDEOBUF_PREPARED;
1247         return 0;
1248 }
1249
1250 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1251                                 struct videobuf_buffer *vb)
1252 {
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;
1257
1258         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1259
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);
1264
1265         /* Change state of the buffer */
1266         vb->state = VIDEOBUF_QUEUED;
1267 }
1268
1269 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1270                                   struct videobuf_buffer *vb)
1271 {
1272         struct vpfe_fh *fh = vq->priv_data;
1273         struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1274         unsigned long flags;
1275
1276         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1277
1278         /*
1279          * We need to flush the buffer from the dma queue since
1280          * they are de-allocated
1281          */
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;
1287 }
1288
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,
1294 };
1295
1296 /*
1297  * vpfe_reqbufs. currently support REQBUF only once opening
1298  * the device.
1299  */
1300 static int vpfe_reqbufs(struct file *file, void *priv,
1301                         struct v4l2_requestbuffers *req_buf)
1302 {
1303         struct vpfe_device *vpfe_dev = video_drvdata(file);
1304         struct vpfe_fh *fh = file->private_data;
1305         int ret = 0;
1306
1307         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1308
1309         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1310                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1311                 return -EINVAL;
1312         }
1313
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;
1319         }
1320
1321         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1322         if (ret)
1323                 return ret;
1324
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;
1329         }
1330
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);
1340
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;
1348 }
1349
1350 static int vpfe_querybuf(struct file *file, void *priv,
1351                          struct v4l2_buffer *buf)
1352 {
1353         struct vpfe_device *vpfe_dev = video_drvdata(file);
1354
1355         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1356
1357         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1358                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1359                 return  -EINVAL;
1360         }
1361
1362         if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1363                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1364                 return -EINVAL;
1365         }
1366         /* Call videobuf_querybuf to get information */
1367         return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1368 }
1369
1370 static int vpfe_qbuf(struct file *file, void *priv,
1371                      struct v4l2_buffer *p)
1372 {
1373         struct vpfe_device *vpfe_dev = video_drvdata(file);
1374         struct vpfe_fh *fh = file->private_data;
1375
1376         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1377
1378         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1379                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1380                 return -EINVAL;
1381         }
1382
1383         /*
1384          * If this file handle is not allowed to do IO,
1385          * return error
1386          */
1387         if (!fh->io_allowed) {
1388                 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1389                 return -EACCES;
1390         }
1391         return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1392 }
1393
1394 static int vpfe_dqbuf(struct file *file, void *priv,
1395                       struct v4l2_buffer *buf)
1396 {
1397         struct vpfe_device *vpfe_dev = video_drvdata(file);
1398
1399         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1400
1401         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1402                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1403                 return -EINVAL;
1404         }
1405         return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1406                                       buf, file->f_flags & O_NONBLOCK);
1407 }
1408
1409 static int vpfe_queryctrl(struct file *file, void *priv,
1410                 struct v4l2_queryctrl *qctrl)
1411 {
1412         struct vpfe_device *vpfe_dev = video_drvdata(file);
1413         struct vpfe_subdev_info *sdinfo;
1414
1415         sdinfo = vpfe_dev->current_subdev;
1416
1417         return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1418                                          core, queryctrl, qctrl);
1419
1420 }
1421
1422 static int vpfe_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1423 {
1424         struct vpfe_device *vpfe_dev = video_drvdata(file);
1425         struct vpfe_subdev_info *sdinfo;
1426
1427         sdinfo = vpfe_dev->current_subdev;
1428
1429         return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1430                                          core, g_ctrl, ctrl);
1431 }
1432
1433 static int vpfe_s_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1434 {
1435         struct vpfe_device *vpfe_dev = video_drvdata(file);
1436         struct vpfe_subdev_info *sdinfo;
1437
1438         sdinfo = vpfe_dev->current_subdev;
1439
1440         return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1441                                          core, s_ctrl, ctrl);
1442 }
1443
1444 /*
1445  * vpfe_calculate_offsets : This function calculates buffers offset
1446  * for top and bottom field
1447  */
1448 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1449 {
1450         struct v4l2_rect image_win;
1451
1452         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1453
1454         ccdc_dev->hw_ops.get_image_window(&image_win);
1455         vpfe_dev->field_off = image_win.height * image_win.width;
1456 }
1457
1458 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1459 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1460 {
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;
1465 }
1466
1467 /*
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
1471  */
1472 static int vpfe_streamon(struct file *file, void *priv,
1473                          enum v4l2_buf_type buf_type)
1474 {
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;
1480
1481         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1482
1483         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1484                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1485                 return -EINVAL;
1486         }
1487
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;
1492         }
1493
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);
1497
1498         if (ret && (ret != -ENOIOCTLCMD)) {
1499                 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1500                 return -EINVAL;
1501         }
1502
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;
1507         }
1508
1509         /* Call videobuf_streamon to start streaming * in videobuf */
1510         ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1511         if (ret)
1512                 return ret;
1513
1514
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);
1529
1530         /* Calculate field offset */
1531         vpfe_calculate_offsets(vpfe_dev);
1532
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;
1538         }
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;
1544         }
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;
1554 }
1555
1556 static int vpfe_streamoff(struct file *file, void *priv,
1557                           enum v4l2_buf_type buf_type)
1558 {
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;
1563
1564         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1565
1566         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1567                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1568                 return -EINVAL;
1569         }
1570
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;
1575         }
1576
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;
1581         }
1582
1583         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1584         if (ret)
1585                 return ret;
1586
1587         vpfe_stop_ccdc_capture(vpfe_dev);
1588         vpfe_detach_irq(vpfe_dev);
1589
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);
1593
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;
1599 }
1600
1601 static int vpfe_cropcap(struct file *file, void *priv,
1602                               struct v4l2_cropcap *crop)
1603 {
1604         struct vpfe_device *vpfe_dev = video_drvdata(file);
1605
1606         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1607
1608         if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1609                 return -EINVAL;
1610
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;
1619 }
1620
1621 static int vpfe_g_crop(struct file *file, void *priv,
1622                              struct v4l2_crop *crop)
1623 {
1624         struct vpfe_device *vpfe_dev = video_drvdata(file);
1625
1626         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_crop\n");
1627
1628         crop->c = vpfe_dev->crop;
1629         return 0;
1630 }
1631
1632 static int vpfe_s_crop(struct file *file, void *priv,
1633                              struct v4l2_crop *crop)
1634 {
1635         struct vpfe_device *vpfe_dev = video_drvdata(file);
1636         int ret = 0;
1637
1638         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_crop\n");
1639
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;
1645         }
1646
1647         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1648         if (ret)
1649                 return ret;
1650
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;
1656         }
1657
1658         /* adjust the width to 16 pixel boundry */
1659         crop->c.width = ((crop->c.width + 15) & ~0xf);
1660
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;
1669         }
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;
1682 }
1683
1684
1685 static long vpfe_param_handler(struct file *file, void *priv,
1686                 int cmd, void *param)
1687 {
1688         struct vpfe_device *vpfe_dev = video_drvdata(file);
1689         int ret = 0;
1690
1691         v4l2_dbg(2, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
1692
1693         if (vpfe_dev->started) {
1694                 /* only allowed if streaming is not started */
1695                 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1696                         "device already started\n");
1697                 return -EBUSY;
1698         }
1699
1700         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1701         if (ret)
1702                 return ret;
1703
1704         switch (cmd) {
1705         case VPFE_CMD_S_CCDC_RAW_PARAMS:
1706                 v4l2_warn(&vpfe_dev->v4l2_dev,
1707                           "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
1708                 if (ccdc_dev->hw_ops.set_params) {
1709                         ret = ccdc_dev->hw_ops.set_params(param);
1710                         if (ret) {
1711                                 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1712                                         "Error setting parameters in CCDC\n");
1713                                 goto unlock_out;
1714                         }
1715                         if (vpfe_get_ccdc_image_format(vpfe_dev,
1716                                                        &vpfe_dev->fmt) < 0) {
1717                                 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1718                                         "Invalid image format at CCDC\n");
1719                                 goto unlock_out;
1720                         }
1721                 } else {
1722                         ret = -EINVAL;
1723                         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1724                                 "VPFE_CMD_S_CCDC_RAW_PARAMS not supported\n");
1725                 }
1726                 break;
1727         default:
1728                 ret = -EINVAL;
1729         }
1730 unlock_out:
1731         mutex_unlock(&vpfe_dev->lock);
1732         return ret;
1733 }
1734
1735
1736 /* vpfe capture ioctl operations */
1737 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1738         .vidioc_querycap         = vpfe_querycap,
1739         .vidioc_g_fmt_vid_cap    = vpfe_g_fmt_vid_cap,
1740         .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1741         .vidioc_s_fmt_vid_cap    = vpfe_s_fmt_vid_cap,
1742         .vidioc_try_fmt_vid_cap  = vpfe_try_fmt_vid_cap,
1743         .vidioc_enum_input       = vpfe_enum_input,
1744         .vidioc_g_input          = vpfe_g_input,
1745         .vidioc_s_input          = vpfe_s_input,
1746         .vidioc_querystd         = vpfe_querystd,
1747         .vidioc_s_std            = vpfe_s_std,
1748         .vidioc_g_std            = vpfe_g_std,
1749         .vidioc_queryctrl        = vpfe_queryctrl,
1750         .vidioc_g_ctrl           = vpfe_g_ctrl,
1751         .vidioc_s_ctrl           = vpfe_s_ctrl,
1752         .vidioc_reqbufs          = vpfe_reqbufs,
1753         .vidioc_querybuf         = vpfe_querybuf,
1754         .vidioc_qbuf             = vpfe_qbuf,
1755         .vidioc_dqbuf            = vpfe_dqbuf,
1756         .vidioc_streamon         = vpfe_streamon,
1757         .vidioc_streamoff        = vpfe_streamoff,
1758         .vidioc_cropcap          = vpfe_cropcap,
1759         .vidioc_g_crop           = vpfe_g_crop,
1760         .vidioc_s_crop           = vpfe_s_crop,
1761         .vidioc_default          = vpfe_param_handler,
1762 };
1763
1764 static struct vpfe_device *vpfe_initialize(void)
1765 {
1766         struct vpfe_device *vpfe_dev;
1767
1768         /* Default number of buffers should be 3 */
1769         if ((numbuffers > 0) &&
1770             (numbuffers < config_params.min_numbuffers))
1771                 numbuffers = config_params.min_numbuffers;
1772
1773         /*
1774          * Set buffer size to min buffers size if invalid buffer size is
1775          * given
1776          */
1777         if (bufsize < config_params.min_bufsize)
1778                 bufsize = config_params.min_bufsize;
1779
1780         config_params.numbuffers = numbuffers;
1781
1782         if (numbuffers)
1783                 config_params.device_bufsize = bufsize;
1784
1785         /* Allocate memory for device objects */
1786         vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1787
1788         return vpfe_dev;
1789 }
1790
1791 /*
1792  * vpfe_probe : This function creates device entries by register
1793  * itself to the V4L2 driver and initializes fields of each
1794  * device objects
1795  */
1796 static __init int vpfe_probe(struct platform_device *pdev)
1797 {
1798         struct vpfe_subdev_info *sdinfo;
1799         struct vpfe_config *vpfe_cfg;
1800         struct resource *res1;
1801         struct vpfe_device *vpfe_dev;
1802         struct i2c_adapter *i2c_adap;
1803         struct video_device *vfd;
1804         int ret = -ENOMEM, i, j;
1805         int num_subdevs = 0;
1806
1807         /* Get the pointer to the device object */
1808         vpfe_dev = vpfe_initialize();
1809
1810         if (!vpfe_dev) {
1811                 v4l2_err(pdev->dev.driver,
1812                         "Failed to allocate memory for vpfe_dev\n");
1813                 return ret;
1814         }
1815
1816         vpfe_dev->pdev = &pdev->dev;
1817
1818         if (NULL == pdev->dev.platform_data) {
1819                 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1820                 ret = -ENODEV;
1821                 goto probe_free_dev_mem;
1822         }
1823
1824         vpfe_cfg = pdev->dev.platform_data;
1825         vpfe_dev->cfg = vpfe_cfg;
1826         if (NULL == vpfe_cfg->ccdc ||
1827             NULL == vpfe_cfg->card_name ||
1828             NULL == vpfe_cfg->sub_devs) {
1829                 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1830                 ret = -ENOENT;
1831                 goto probe_free_dev_mem;
1832         }
1833
1834         mutex_lock(&ccdc_lock);
1835         /* Allocate memory for ccdc configuration */
1836         ccdc_cfg = kmalloc(sizeof(struct ccdc_config), GFP_KERNEL);
1837         if (NULL == ccdc_cfg) {
1838                 v4l2_err(pdev->dev.driver,
1839                          "Memory allocation failed for ccdc_cfg\n");
1840                 goto probe_free_lock;
1841         }
1842
1843         strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1844         /* Get VINT0 irq resource */
1845         res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1846         if (!res1) {
1847                 v4l2_err(pdev->dev.driver,
1848                          "Unable to get interrupt for VINT0\n");
1849                 ret = -ENODEV;
1850                 goto probe_free_ccdc_cfg_mem;
1851         }
1852         vpfe_dev->ccdc_irq0 = res1->start;
1853
1854         /* Get VINT1 irq resource */
1855         res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1856         if (!res1) {
1857                 v4l2_err(pdev->dev.driver,
1858                          "Unable to get interrupt for VINT1\n");
1859                 ret = -ENODEV;
1860                 goto probe_free_ccdc_cfg_mem;
1861         }
1862         vpfe_dev->ccdc_irq1 = res1->start;
1863
1864         ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, IRQF_DISABLED,
1865                           "vpfe_capture0", vpfe_dev);
1866
1867         if (0 != ret) {
1868                 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1869                 goto probe_free_ccdc_cfg_mem;
1870         }
1871
1872         /* Allocate memory for video device */
1873         vfd = video_device_alloc();
1874         if (NULL == vfd) {
1875                 ret = -ENOMEM;
1876                 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
1877                 goto probe_out_release_irq;
1878         }
1879
1880         /* Initialize field of video device */
1881         vfd->release            = video_device_release;
1882         vfd->fops               = &vpfe_fops;
1883         vfd->ioctl_ops          = &vpfe_ioctl_ops;
1884         vfd->tvnorms            = 0;
1885         vfd->current_norm       = V4L2_STD_PAL;
1886         vfd->v4l2_dev           = &vpfe_dev->v4l2_dev;
1887         snprintf(vfd->name, sizeof(vfd->name),
1888                  "%s_V%d.%d.%d",
1889                  CAPTURE_DRV_NAME,
1890                  (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1891                  (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1892                  (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1893         /* Set video_dev to the video device */
1894         vpfe_dev->video_dev     = vfd;
1895
1896         ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1897         if (ret) {
1898                 v4l2_err(pdev->dev.driver,
1899                         "Unable to register v4l2 device.\n");
1900                 goto probe_out_video_release;
1901         }
1902         v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1903         spin_lock_init(&vpfe_dev->irqlock);
1904         spin_lock_init(&vpfe_dev->dma_queue_lock);
1905         mutex_init(&vpfe_dev->lock);
1906
1907         /* Initialize field of the device objects */
1908         vpfe_dev->numbuffers = config_params.numbuffers;
1909
1910         /* Initialize prio member of device object */
1911         v4l2_prio_init(&vpfe_dev->prio);
1912         /* register video device */
1913         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1914                 "trying to register vpfe device.\n");
1915         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1916                 "video_dev=%x\n", (int)&vpfe_dev->video_dev);
1917         vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1918         ret = video_register_device(vpfe_dev->video_dev,
1919                                     VFL_TYPE_GRABBER, -1);
1920
1921         if (ret) {
1922                 v4l2_err(pdev->dev.driver,
1923                         "Unable to register video device.\n");
1924                 goto probe_out_v4l2_unregister;
1925         }
1926
1927         v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1928         /* set the driver data in platform device */
1929         platform_set_drvdata(pdev, vpfe_dev);
1930         /* set driver private data */
1931         video_set_drvdata(vpfe_dev->video_dev, vpfe_dev);
1932         i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1933         num_subdevs = vpfe_cfg->num_subdevs;
1934         vpfe_dev->sd = kmalloc(sizeof(struct v4l2_subdev *) * num_subdevs,
1935                                 GFP_KERNEL);
1936         if (NULL == vpfe_dev->sd) {
1937                 v4l2_err(&vpfe_dev->v4l2_dev,
1938                         "unable to allocate memory for subdevice pointers\n");
1939                 ret = -ENOMEM;
1940                 goto probe_out_video_unregister;
1941         }
1942
1943         for (i = 0; i < num_subdevs; i++) {
1944                 struct v4l2_input *inps;
1945
1946                 sdinfo = &vpfe_cfg->sub_devs[i];
1947
1948                 /* Load up the subdevice */
1949                 vpfe_dev->sd[i] =
1950                         v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1951                                                   i2c_adap,
1952                                                   sdinfo->name,
1953                                                   &sdinfo->board_info,
1954                                                   NULL);
1955                 if (vpfe_dev->sd[i]) {
1956                         v4l2_info(&vpfe_dev->v4l2_dev,
1957                                   "v4l2 sub device %s registered\n",
1958                                   sdinfo->name);
1959                         vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1960                         /* update tvnorms from the sub devices */
1961                         for (j = 0; j < sdinfo->num_inputs; j++) {
1962                                 inps = &sdinfo->inputs[j];
1963                                 vfd->tvnorms |= inps->std;
1964                         }
1965                 } else {
1966                         v4l2_info(&vpfe_dev->v4l2_dev,
1967                                   "v4l2 sub device %s register fails\n",
1968                                   sdinfo->name);
1969                         goto probe_sd_out;
1970                 }
1971         }
1972
1973         /* set first sub device as current one */
1974         vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1975
1976         /* We have at least one sub device to work with */
1977         mutex_unlock(&ccdc_lock);
1978         return 0;
1979
1980 probe_sd_out:
1981         kfree(vpfe_dev->sd);
1982 probe_out_video_unregister:
1983         video_unregister_device(vpfe_dev->video_dev);
1984 probe_out_v4l2_unregister:
1985         v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1986 probe_out_video_release:
1987         if (!video_is_registered(vpfe_dev->video_dev))
1988                 video_device_release(vpfe_dev->video_dev);
1989 probe_out_release_irq:
1990         free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1991 probe_free_ccdc_cfg_mem:
1992         kfree(ccdc_cfg);
1993 probe_free_lock:
1994         mutex_unlock(&ccdc_lock);
1995 probe_free_dev_mem:
1996         kfree(vpfe_dev);
1997         return ret;
1998 }
1999
2000 /*
2001  * vpfe_remove : It un-register device from V4L2 driver
2002  */
2003 static int __devexit vpfe_remove(struct platform_device *pdev)
2004 {
2005         struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
2006
2007         v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2008
2009         free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2010         kfree(vpfe_dev->sd);
2011         v4l2_device_unregister(&vpfe_dev->v4l2_dev);
2012         video_unregister_device(vpfe_dev->video_dev);
2013         kfree(vpfe_dev);
2014         kfree(ccdc_cfg);
2015         return 0;
2016 }
2017
2018 static int
2019 vpfe_suspend(struct device *dev)
2020 {
2021         /* add suspend code here later */
2022         return -1;
2023 }
2024
2025 static int
2026 vpfe_resume(struct device *dev)
2027 {
2028         /* add resume code here later */
2029         return -1;
2030 }
2031
2032 static const struct dev_pm_ops vpfe_dev_pm_ops = {
2033         .suspend = vpfe_suspend,
2034         .resume = vpfe_resume,
2035 };
2036
2037 static struct platform_driver vpfe_driver = {
2038         .driver = {
2039                 .name = CAPTURE_DRV_NAME,
2040                 .owner = THIS_MODULE,
2041                 .pm = &vpfe_dev_pm_ops,
2042         },
2043         .probe = vpfe_probe,
2044         .remove = __devexit_p(vpfe_remove),
2045 };
2046
2047 static __init int vpfe_init(void)
2048 {
2049         printk(KERN_NOTICE "vpfe_init\n");
2050         /* Register driver to the kernel */
2051         return platform_driver_register(&vpfe_driver);
2052 }
2053
2054 /*
2055  * vpfe_cleanup : This function un-registers device driver
2056  */
2057 static void vpfe_cleanup(void)
2058 {
2059         platform_driver_unregister(&vpfe_driver);
2060 }
2061
2062 module_init(vpfe_init);
2063 module_exit(vpfe_cleanup);