ALSA: opl4 - Fix a wrong argument in proc write callback
[safe/jmp/linux-2.6] / 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_params);
227         BUG_ON(!dev->hw_ops.set_image_window);
228         BUG_ON(!dev->hw_ops.get_image_window);
229         BUG_ON(!dev->hw_ops.get_line_length);
230         BUG_ON(!dev->hw_ops.getfid);
231
232         mutex_lock(&ccdc_lock);
233         if (NULL == ccdc_cfg) {
234                 /*
235                  * TODO. Will this ever happen? if so, we need to fix it.
236                  * Proabably we need to add the request to a linked list and
237                  * walk through it during vpfe probe
238                  */
239                 printk(KERN_ERR "vpfe capture not initialized\n");
240                 ret = -EFAULT;
241                 goto unlock;
242         }
243
244         if (strcmp(dev->name, ccdc_cfg->name)) {
245                 /* ignore this ccdc */
246                 ret = -EINVAL;
247                 goto unlock;
248         }
249
250         if (ccdc_dev) {
251                 printk(KERN_ERR "ccdc already registered\n");
252                 ret = -EINVAL;
253                 goto unlock;
254         }
255
256         ccdc_dev = dev;
257 unlock:
258         mutex_unlock(&ccdc_lock);
259         return ret;
260 }
261 EXPORT_SYMBOL(vpfe_register_ccdc_device);
262
263 /*
264  * vpfe_unregister_ccdc_device. CCDC module calls this to
265  * unregister with vpfe capture
266  */
267 void vpfe_unregister_ccdc_device(struct ccdc_hw_device *dev)
268 {
269         if (NULL == dev) {
270                 printk(KERN_ERR "invalid ccdc device ptr\n");
271                 return;
272         }
273
274         printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
275                 dev->name);
276
277         if (strcmp(dev->name, ccdc_cfg->name)) {
278                 /* ignore this ccdc */
279                 return;
280         }
281
282         mutex_lock(&ccdc_lock);
283         ccdc_dev = NULL;
284         mutex_unlock(&ccdc_lock);
285         return;
286 }
287 EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
288
289 /*
290  * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
291  */
292 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe_dev,
293                                  struct v4l2_format *f)
294 {
295         struct v4l2_rect image_win;
296         enum ccdc_buftype buf_type;
297         enum ccdc_frmfmt frm_fmt;
298
299         memset(f, 0, sizeof(*f));
300         f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
301         ccdc_dev->hw_ops.get_image_window(&image_win);
302         f->fmt.pix.width = image_win.width;
303         f->fmt.pix.height = image_win.height;
304         f->fmt.pix.bytesperline = ccdc_dev->hw_ops.get_line_length();
305         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
306                                 f->fmt.pix.height;
307         buf_type = ccdc_dev->hw_ops.get_buftype();
308         f->fmt.pix.pixelformat = ccdc_dev->hw_ops.get_pixel_format();
309         frm_fmt = ccdc_dev->hw_ops.get_frame_format();
310         if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
311                 f->fmt.pix.field = V4L2_FIELD_NONE;
312         else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
313                 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
314                         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
315                 else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED)
316                         f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
317                 else {
318                         v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf_type\n");
319                         return -EINVAL;
320                 }
321         } else {
322                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid frm_fmt\n");
323                 return -EINVAL;
324         }
325         return 0;
326 }
327
328 /*
329  * vpfe_config_ccdc_image_format()
330  * For a pix format, configure ccdc to setup the capture
331  */
332 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
333 {
334         enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
335         int ret = 0;
336
337         if (ccdc_dev->hw_ops.set_pixel_format(
338                         vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
339                 v4l2_err(&vpfe_dev->v4l2_dev,
340                         "couldn't set pix format in ccdc\n");
341                 return -EINVAL;
342         }
343         /* configure the image window */
344         ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
345
346         switch (vpfe_dev->fmt.fmt.pix.field) {
347         case V4L2_FIELD_INTERLACED:
348                 /* do nothing, since it is default */
349                 ret = ccdc_dev->hw_ops.set_buftype(
350                                 CCDC_BUFTYPE_FLD_INTERLEAVED);
351                 break;
352         case V4L2_FIELD_NONE:
353                 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
354                 /* buffer type only applicable for interlaced scan */
355                 break;
356         case V4L2_FIELD_SEQ_TB:
357                 ret = ccdc_dev->hw_ops.set_buftype(
358                                 CCDC_BUFTYPE_FLD_SEPARATED);
359                 break;
360         default:
361                 return -EINVAL;
362         }
363
364         /* set the frame format */
365         if (!ret)
366                 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
367         return ret;
368 }
369 /*
370  * vpfe_config_image_format()
371  * For a given standard, this functions sets up the default
372  * pix format & crop values in the vpfe device and ccdc.  It first
373  * starts with defaults based values from the standard table.
374  * It then checks if sub device support g_fmt and then override the
375  * values based on that.Sets crop values to match with scan resolution
376  * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
377  * values in ccdc
378  */
379 static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
380                                     const v4l2_std_id *std_id)
381 {
382         struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
383         int i, ret = 0;
384
385         for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
386                 if (vpfe_standards[i].std_id & *std_id) {
387                         vpfe_dev->std_info.active_pixels =
388                                         vpfe_standards[i].width;
389                         vpfe_dev->std_info.active_lines =
390                                         vpfe_standards[i].height;
391                         vpfe_dev->std_info.frame_format =
392                                         vpfe_standards[i].frame_format;
393                         vpfe_dev->std_index = i;
394                         break;
395                 }
396         }
397
398         if (i ==  ARRAY_SIZE(vpfe_standards)) {
399                 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
400                 return -EINVAL;
401         }
402
403         vpfe_dev->crop.top = 0;
404         vpfe_dev->crop.left = 0;
405         vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
406         vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
407         vpfe_dev->fmt.fmt.pix.width = vpfe_dev->crop.width;
408         vpfe_dev->fmt.fmt.pix.height = vpfe_dev->crop.height;
409
410         /* first field and frame format based on standard frame format */
411         if (vpfe_dev->std_info.frame_format) {
412                 vpfe_dev->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
413                 /* assume V4L2_PIX_FMT_UYVY as default */
414                 vpfe_dev->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
415         } else {
416                 vpfe_dev->fmt.fmt.pix.field = V4L2_FIELD_NONE;
417                 /* assume V4L2_PIX_FMT_SBGGR8 */
418                 vpfe_dev->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
419         }
420
421         /* if sub device supports g_fmt, override the defaults */
422         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
423                         sdinfo->grp_id, video, g_fmt, &vpfe_dev->fmt);
424
425         if (ret && ret != -ENOIOCTLCMD) {
426                 v4l2_err(&vpfe_dev->v4l2_dev,
427                         "error in getting g_fmt from sub device\n");
428                 return ret;
429         }
430
431         /* Sets the values in CCDC */
432         ret = vpfe_config_ccdc_image_format(vpfe_dev);
433         if (ret)
434                 return ret;
435
436         /* Update the values of sizeimage and bytesperline */
437         if (!ret) {
438                 vpfe_dev->fmt.fmt.pix.bytesperline =
439                         ccdc_dev->hw_ops.get_line_length();
440                 vpfe_dev->fmt.fmt.pix.sizeimage =
441                         vpfe_dev->fmt.fmt.pix.bytesperline *
442                         vpfe_dev->fmt.fmt.pix.height;
443         }
444         return ret;
445 }
446
447 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
448 {
449         int ret = 0;
450
451         /* set first input of current subdevice as the current input */
452         vpfe_dev->current_input = 0;
453
454         /* set default standard */
455         vpfe_dev->std_index = 0;
456
457         /* Configure the default format information */
458         ret = vpfe_config_image_format(vpfe_dev,
459                                 &vpfe_standards[vpfe_dev->std_index].std_id);
460         if (ret)
461                 return ret;
462
463         /* now open the ccdc device to initialize it */
464         mutex_lock(&ccdc_lock);
465         if (NULL == ccdc_dev) {
466                 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
467                 ret = -ENODEV;
468                 goto unlock;
469         }
470
471         if (!try_module_get(ccdc_dev->owner)) {
472                 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
473                 ret = -ENODEV;
474                 goto unlock;
475         }
476         ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
477         if (!ret)
478                 vpfe_dev->initialized = 1;
479 unlock:
480         mutex_unlock(&ccdc_lock);
481         return ret;
482 }
483
484 /*
485  * vpfe_open : It creates object of file handle structure and
486  * stores it in private_data  member of filepointer
487  */
488 static int vpfe_open(struct file *file)
489 {
490         struct vpfe_device *vpfe_dev = video_drvdata(file);
491         struct vpfe_fh *fh;
492
493         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
494
495         if (!vpfe_dev->cfg->num_subdevs) {
496                 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
497                 return -ENODEV;
498         }
499
500         /* Allocate memory for the file handle object */
501         fh = kmalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
502         if (NULL == fh) {
503                 v4l2_err(&vpfe_dev->v4l2_dev,
504                         "unable to allocate memory for file handle object\n");
505                 return -ENOMEM;
506         }
507         /* store pointer to fh in private_data member of file */
508         file->private_data = fh;
509         fh->vpfe_dev = vpfe_dev;
510         mutex_lock(&vpfe_dev->lock);
511         /* If decoder is not initialized. initialize it */
512         if (!vpfe_dev->initialized) {
513                 if (vpfe_initialize_device(vpfe_dev)) {
514                         mutex_unlock(&vpfe_dev->lock);
515                         return -ENODEV;
516                 }
517         }
518         /* Increment device usrs counter */
519         vpfe_dev->usrs++;
520         /* Set io_allowed member to false */
521         fh->io_allowed = 0;
522         /* Initialize priority of this instance to default priority */
523         fh->prio = V4L2_PRIORITY_UNSET;
524         v4l2_prio_open(&vpfe_dev->prio, &fh->prio);
525         mutex_unlock(&vpfe_dev->lock);
526         return 0;
527 }
528
529 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
530 {
531         unsigned long addr;
532
533         vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
534                                         struct videobuf_buffer, queue);
535         list_del(&vpfe_dev->next_frm->queue);
536         vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
537         addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
538         ccdc_dev->hw_ops.setfbaddr(addr);
539 }
540
541 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
542 {
543         struct timeval timevalue;
544
545         do_gettimeofday(&timevalue);
546         vpfe_dev->cur_frm->ts = timevalue;
547         vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
548         vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
549         wake_up_interruptible(&vpfe_dev->cur_frm->done);
550         vpfe_dev->cur_frm = vpfe_dev->next_frm;
551 }
552
553 /* ISR for VINT0*/
554 static irqreturn_t vpfe_isr(int irq, void *dev_id)
555 {
556         struct vpfe_device *vpfe_dev = dev_id;
557         enum v4l2_field field;
558         unsigned long addr;
559         int fid;
560
561         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
562         field = vpfe_dev->fmt.fmt.pix.field;
563
564         /* if streaming not started, don't do anything */
565         if (!vpfe_dev->started)
566                 return IRQ_HANDLED;
567
568         /* only for 6446 this will be applicable */
569         if (NULL != ccdc_dev->hw_ops.reset)
570                 ccdc_dev->hw_ops.reset();
571
572         if (field == V4L2_FIELD_NONE) {
573                 /* handle progressive frame capture */
574                 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
575                         "frame format is progressive...\n");
576                 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
577                         vpfe_process_buffer_complete(vpfe_dev);
578                 return IRQ_HANDLED;
579         }
580
581         /* interlaced or TB capture check which field we are in hardware */
582         fid = ccdc_dev->hw_ops.getfid();
583
584         /* switch the software maintained field id */
585         vpfe_dev->field_id ^= 1;
586         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
587                 fid, vpfe_dev->field_id);
588         if (fid == vpfe_dev->field_id) {
589                 /* we are in-sync here,continue */
590                 if (fid == 0) {
591                         /*
592                          * One frame is just being captured. If the next frame
593                          * is available, release the current frame and move on
594                          */
595                         if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
596                                 vpfe_process_buffer_complete(vpfe_dev);
597                         /*
598                          * based on whether the two fields are stored
599                          * interleavely or separately in memory, reconfigure
600                          * the CCDC memory address
601                          */
602                         if (field == V4L2_FIELD_SEQ_TB) {
603                                 addr =
604                                   videobuf_to_dma_contig(vpfe_dev->cur_frm);
605                                 addr += vpfe_dev->field_off;
606                                 ccdc_dev->hw_ops.setfbaddr(addr);
607                         }
608                         return IRQ_HANDLED;
609                 }
610                 /*
611                  * if one field is just being captured configure
612                  * the next frame get the next frame from the empty
613                  * queue if no frame is available hold on to the
614                  * current buffer
615                  */
616                 spin_lock(&vpfe_dev->dma_queue_lock);
617                 if (!list_empty(&vpfe_dev->dma_queue) &&
618                     vpfe_dev->cur_frm == vpfe_dev->next_frm)
619                         vpfe_schedule_next_buffer(vpfe_dev);
620                 spin_unlock(&vpfe_dev->dma_queue_lock);
621         } else if (fid == 0) {
622                 /*
623                  * out of sync. Recover from any hardware out-of-sync.
624                  * May loose one frame
625                  */
626                 vpfe_dev->field_id = fid;
627         }
628         return IRQ_HANDLED;
629 }
630
631 /* vdint1_isr - isr handler for VINT1 interrupt */
632 static irqreturn_t vdint1_isr(int irq, void *dev_id)
633 {
634         struct vpfe_device *vpfe_dev = dev_id;
635
636         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
637
638         /* if streaming not started, don't do anything */
639         if (!vpfe_dev->started)
640                 return IRQ_HANDLED;
641
642         spin_lock(&vpfe_dev->dma_queue_lock);
643         if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
644             !list_empty(&vpfe_dev->dma_queue) &&
645             vpfe_dev->cur_frm == vpfe_dev->next_frm)
646                 vpfe_schedule_next_buffer(vpfe_dev);
647         spin_unlock(&vpfe_dev->dma_queue_lock);
648         return IRQ_HANDLED;
649 }
650
651 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
652 {
653         enum ccdc_frmfmt frame_format;
654
655         frame_format = ccdc_dev->hw_ops.get_frame_format();
656         if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
657                 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
658 }
659
660 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
661 {
662         enum ccdc_frmfmt frame_format;
663
664         frame_format = ccdc_dev->hw_ops.get_frame_format();
665         if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
666                 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
667                                     IRQF_DISABLED, "vpfe_capture1",
668                                     vpfe_dev);
669         }
670         return 0;
671 }
672
673 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
674 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
675 {
676         vpfe_dev->started = 0;
677         ccdc_dev->hw_ops.enable(0);
678         if (ccdc_dev->hw_ops.enable_out_to_sdram)
679                 ccdc_dev->hw_ops.enable_out_to_sdram(0);
680 }
681
682 /*
683  * vpfe_release : This function deletes buffer queue, frees the
684  * buffers and the vpfe file  handle
685  */
686 static int vpfe_release(struct file *file)
687 {
688         struct vpfe_device *vpfe_dev = video_drvdata(file);
689         struct vpfe_fh *fh = file->private_data;
690         struct vpfe_subdev_info *sdinfo;
691         int ret;
692
693         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
694
695         /* Get the device lock */
696         mutex_lock(&vpfe_dev->lock);
697         /* if this instance is doing IO */
698         if (fh->io_allowed) {
699                 if (vpfe_dev->started) {
700                         sdinfo = vpfe_dev->current_subdev;
701                         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
702                                                          sdinfo->grp_id,
703                                                          video, s_stream, 0);
704                         if (ret && (ret != -ENOIOCTLCMD))
705                                 v4l2_err(&vpfe_dev->v4l2_dev,
706                                 "stream off failed in subdev\n");
707                         vpfe_stop_ccdc_capture(vpfe_dev);
708                         vpfe_detach_irq(vpfe_dev);
709                         videobuf_streamoff(&vpfe_dev->buffer_queue);
710                 }
711                 vpfe_dev->io_usrs = 0;
712                 vpfe_dev->numbuffers = config_params.numbuffers;
713         }
714
715         /* Decrement device usrs counter */
716         vpfe_dev->usrs--;
717         /* Close the priority */
718         v4l2_prio_close(&vpfe_dev->prio, &fh->prio);
719         /* If this is the last file handle */
720         if (!vpfe_dev->usrs) {
721                 vpfe_dev->initialized = 0;
722                 if (ccdc_dev->hw_ops.close)
723                         ccdc_dev->hw_ops.close(vpfe_dev->pdev);
724                 module_put(ccdc_dev->owner);
725         }
726         mutex_unlock(&vpfe_dev->lock);
727         file->private_data = NULL;
728         /* Free memory allocated to file handle object */
729         kfree(fh);
730         return 0;
731 }
732
733 /*
734  * vpfe_mmap : It is used to map kernel space buffers
735  * into user spaces
736  */
737 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
738 {
739         /* Get the device object and file handle object */
740         struct vpfe_device *vpfe_dev = video_drvdata(file);
741
742         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
743
744         return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
745 }
746
747 /*
748  * vpfe_poll: It is used for select/poll system call
749  */
750 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
751 {
752         struct vpfe_device *vpfe_dev = video_drvdata(file);
753
754         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
755
756         if (vpfe_dev->started)
757                 return videobuf_poll_stream(file,
758                                             &vpfe_dev->buffer_queue, wait);
759         return 0;
760 }
761
762 /* vpfe capture driver file operations */
763 static const struct v4l2_file_operations vpfe_fops = {
764         .owner = THIS_MODULE,
765         .open = vpfe_open,
766         .release = vpfe_release,
767         .unlocked_ioctl = video_ioctl2,
768         .mmap = vpfe_mmap,
769         .poll = vpfe_poll
770 };
771
772 /*
773  * vpfe_check_format()
774  * This function adjust the input pixel format as per hardware
775  * capabilities and update the same in pixfmt.
776  * Following algorithm used :-
777  *
778  *      If given pixformat is not in the vpfe list of pix formats or not
779  *      supported by the hardware, current value of pixformat in the device
780  *      is used
781  *      If given field is not supported, then current field is used. If field
782  *      is different from current, then it is matched with that from sub device.
783  *      Minimum height is 2 lines for interlaced or tb field and 1 line for
784  *      progressive. Maximum height is clamped to active active lines of scan
785  *      Minimum width is 32 bytes in memory and width is clamped to active
786  *      pixels of scan.
787  *      bytesperline is a multiple of 32.
788  */
789 static const struct vpfe_pixel_format *
790         vpfe_check_format(struct vpfe_device *vpfe_dev,
791                           struct v4l2_pix_format *pixfmt)
792 {
793         u32 min_height = 1, min_width = 32, max_width, max_height;
794         const struct vpfe_pixel_format *vpfe_pix_fmt;
795         u32 pix;
796         int temp, found;
797
798         vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
799         if (NULL == vpfe_pix_fmt) {
800                 /*
801                  * use current pixel format in the vpfe device. We
802                  * will find this pix format in the table
803                  */
804                 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
805                 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
806         }
807
808         /* check if hw supports it */
809         temp = 0;
810         found = 0;
811         while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
812                 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
813                         found = 1;
814                         break;
815                 }
816                 temp++;
817         }
818
819         if (!found) {
820                 /* use current pixel format */
821                 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
822                 /*
823                  * Since this is currently used in the vpfe device, we
824                  * will find this pix format in the table
825                  */
826                 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
827         }
828
829         /* check what field format is supported */
830         if (pixfmt->field == V4L2_FIELD_ANY) {
831                 /* if field is any, use current value as default */
832                 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
833         }
834
835         /*
836          * if field is not same as current field in the vpfe device
837          * try matching the field with the sub device field
838          */
839         if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
840                 /*
841                  * If field value is not in the supported fields, use current
842                  * field used in the device as default
843                  */
844                 switch (pixfmt->field) {
845                 case V4L2_FIELD_INTERLACED:
846                 case V4L2_FIELD_SEQ_TB:
847                         /* if sub device is supporting progressive, use that */
848                         if (!vpfe_dev->std_info.frame_format)
849                                 pixfmt->field = V4L2_FIELD_NONE;
850                         break;
851                 case V4L2_FIELD_NONE:
852                         if (vpfe_dev->std_info.frame_format)
853                                 pixfmt->field = V4L2_FIELD_INTERLACED;
854                         break;
855
856                 default:
857                         /* use current field as default */
858                         pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
859                         break;
860                 }
861         }
862
863         /* Now adjust image resolutions supported */
864         if (pixfmt->field == V4L2_FIELD_INTERLACED ||
865             pixfmt->field == V4L2_FIELD_SEQ_TB)
866                 min_height = 2;
867
868         max_width = vpfe_dev->std_info.active_pixels;
869         max_height = vpfe_dev->std_info.active_lines;
870         min_width /= vpfe_pix_fmt->bpp;
871
872         v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
873                   pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
874
875         pixfmt->width = clamp((pixfmt->width), min_width, max_width);
876         pixfmt->height = clamp((pixfmt->height), min_height, max_height);
877
878         /* If interlaced, adjust height to be a multiple of 2 */
879         if (pixfmt->field == V4L2_FIELD_INTERLACED)
880                 pixfmt->height &= (~1);
881         /*
882          * recalculate bytesperline and sizeimage since width
883          * and height might have changed
884          */
885         pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
886                                 & ~31);
887         if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
888                 pixfmt->sizeimage =
889                         pixfmt->bytesperline * pixfmt->height +
890                         ((pixfmt->bytesperline * pixfmt->height) >> 1);
891         else
892                 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
893
894         v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height ="
895                  " %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
896                  pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
897                  pixfmt->bytesperline, pixfmt->sizeimage);
898         return vpfe_pix_fmt;
899 }
900
901 static int vpfe_querycap(struct file *file, void  *priv,
902                                struct v4l2_capability *cap)
903 {
904         struct vpfe_device *vpfe_dev = video_drvdata(file);
905
906         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
907
908         cap->version = VPFE_CAPTURE_VERSION_CODE;
909         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
910         strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
911         strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
912         strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
913         return 0;
914 }
915
916 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
917                                 struct v4l2_format *fmt)
918 {
919         struct vpfe_device *vpfe_dev = video_drvdata(file);
920         int ret = 0;
921
922         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
923         /* Fill in the information about format */
924         *fmt = vpfe_dev->fmt;
925         return ret;
926 }
927
928 static int vpfe_enum_fmt_vid_cap(struct file *file, void  *priv,
929                                    struct v4l2_fmtdesc *fmt)
930 {
931         struct vpfe_device *vpfe_dev = video_drvdata(file);
932         const struct vpfe_pixel_format *pix_fmt;
933         int temp_index;
934         u32 pix;
935
936         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
937
938         if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
939                 return -EINVAL;
940
941         /* Fill in the information about format */
942         pix_fmt = vpfe_lookup_pix_format(pix);
943         if (NULL != pix_fmt) {
944                 temp_index = fmt->index;
945                 *fmt = pix_fmt->fmtdesc;
946                 fmt->index = temp_index;
947                 return 0;
948         }
949         return -EINVAL;
950 }
951
952 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
953                                 struct v4l2_format *fmt)
954 {
955         struct vpfe_device *vpfe_dev = video_drvdata(file);
956         const struct vpfe_pixel_format *pix_fmts;
957         int ret = 0;
958
959         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
960
961         /* If streaming is started, return error */
962         if (vpfe_dev->started) {
963                 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
964                 return -EBUSY;
965         }
966
967         /* Check for valid frame format */
968         pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
969
970         if (NULL == pix_fmts)
971                 return -EINVAL;
972
973         /* store the pixel format in the device  object */
974         ret = mutex_lock_interruptible(&vpfe_dev->lock);
975         if (ret)
976                 return ret;
977
978         /* First detach any IRQ if currently attached */
979         vpfe_detach_irq(vpfe_dev);
980         vpfe_dev->fmt = *fmt;
981         /* set image capture parameters in the ccdc */
982         ret = vpfe_config_ccdc_image_format(vpfe_dev);
983         mutex_unlock(&vpfe_dev->lock);
984         return ret;
985 }
986
987 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
988                                   struct v4l2_format *f)
989 {
990         struct vpfe_device *vpfe_dev = video_drvdata(file);
991         const struct vpfe_pixel_format *pix_fmts;
992
993         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
994
995         pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
996         if (NULL == pix_fmts)
997                 return -EINVAL;
998         return 0;
999 }
1000
1001 /*
1002  * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1003  * given app input index
1004  */
1005 static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1006                                         int *subdev_index,
1007                                         int *subdev_input_index,
1008                                         int app_input_index)
1009 {
1010         struct vpfe_config *cfg = vpfe_dev->cfg;
1011         struct vpfe_subdev_info *sdinfo;
1012         int i, j = 0;
1013
1014         for (i = 0; i < cfg->num_subdevs; i++) {
1015                 sdinfo = &cfg->sub_devs[i];
1016                 if (app_input_index < (j + sdinfo->num_inputs)) {
1017                         *subdev_index = i;
1018                         *subdev_input_index = app_input_index - j;
1019                         return 0;
1020                 }
1021                 j += sdinfo->num_inputs;
1022         }
1023         return -EINVAL;
1024 }
1025
1026 /*
1027  * vpfe_get_app_input - Get app input index for a given subdev input index
1028  * driver stores the input index of the current sub device and translate it
1029  * when application request the current input
1030  */
1031 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1032                                     int *app_input_index)
1033 {
1034         struct vpfe_config *cfg = vpfe_dev->cfg;
1035         struct vpfe_subdev_info *sdinfo;
1036         int i, j = 0;
1037
1038         for (i = 0; i < cfg->num_subdevs; i++) {
1039                 sdinfo = &cfg->sub_devs[i];
1040                 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1041                         if (vpfe_dev->current_input >= sdinfo->num_inputs)
1042                                 return -1;
1043                         *app_input_index = j + vpfe_dev->current_input;
1044                         return 0;
1045                 }
1046                 j += sdinfo->num_inputs;
1047         }
1048         return -EINVAL;
1049 }
1050
1051 static int vpfe_enum_input(struct file *file, void *priv,
1052                                  struct v4l2_input *inp)
1053 {
1054         struct vpfe_device *vpfe_dev = video_drvdata(file);
1055         struct vpfe_subdev_info *sdinfo;
1056         int subdev, index ;
1057
1058         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1059
1060         if (vpfe_get_subdev_input_index(vpfe_dev,
1061                                         &subdev,
1062                                         &index,
1063                                         inp->index) < 0) {
1064                 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found"
1065                          " for the subdev\n");
1066                 return -EINVAL;
1067         }
1068         sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1069         memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1070         return 0;
1071 }
1072
1073 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1074 {
1075         struct vpfe_device *vpfe_dev = video_drvdata(file);
1076
1077         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1078
1079         return vpfe_get_app_input_index(vpfe_dev, index);
1080 }
1081
1082
1083 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1084 {
1085         struct vpfe_device *vpfe_dev = video_drvdata(file);
1086         struct vpfe_subdev_info *sdinfo;
1087         int subdev_index, inp_index;
1088         struct vpfe_route *route;
1089         u32 input = 0, output = 0;
1090         int ret = -EINVAL;
1091
1092         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1093
1094         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1095         if (ret)
1096                 return ret;
1097
1098         /*
1099          * If streaming is started return device busy
1100          * error
1101          */
1102         if (vpfe_dev->started) {
1103                 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1104                 ret = -EBUSY;
1105                 goto unlock_out;
1106         }
1107
1108         if (vpfe_get_subdev_input_index(vpfe_dev,
1109                                         &subdev_index,
1110                                         &inp_index,
1111                                         index) < 0) {
1112                 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1113                 goto unlock_out;
1114         }
1115
1116         sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1117         route = &sdinfo->routes[inp_index];
1118         if (route && sdinfo->can_route) {
1119                 input = route->input;
1120                 output = route->output;
1121         }
1122
1123         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1124                                          video, s_routing, input, output, 0);
1125
1126         if (ret) {
1127                 v4l2_err(&vpfe_dev->v4l2_dev,
1128                         "vpfe_doioctl:error in setting input in decoder\n");
1129                 ret = -EINVAL;
1130                 goto unlock_out;
1131         }
1132         vpfe_dev->current_subdev = sdinfo;
1133         vpfe_dev->current_input = index;
1134         vpfe_dev->std_index = 0;
1135
1136         /* set the bus/interface parameter for the sub device in ccdc */
1137         ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1138         if (ret)
1139                 goto unlock_out;
1140
1141         /* set the default image parameters in the device */
1142         ret = vpfe_config_image_format(vpfe_dev,
1143                                 &vpfe_standards[vpfe_dev->std_index].std_id);
1144 unlock_out:
1145         mutex_unlock(&vpfe_dev->lock);
1146         return ret;
1147 }
1148
1149 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1150 {
1151         struct vpfe_device *vpfe_dev = video_drvdata(file);
1152         struct vpfe_subdev_info *sdinfo;
1153         int ret = 0;
1154
1155         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1156
1157         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1158         sdinfo = vpfe_dev->current_subdev;
1159         if (ret)
1160                 return ret;
1161         /* Call querystd function of decoder device */
1162         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1163                                          video, querystd, std_id);
1164         mutex_unlock(&vpfe_dev->lock);
1165         return ret;
1166 }
1167
1168 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1169 {
1170         struct vpfe_device *vpfe_dev = video_drvdata(file);
1171         struct vpfe_subdev_info *sdinfo;
1172         int ret = 0;
1173
1174         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1175
1176         /* Call decoder driver function to set the standard */
1177         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1178         if (ret)
1179                 return ret;
1180
1181         sdinfo = vpfe_dev->current_subdev;
1182         /* If streaming is started, return device busy error */
1183         if (vpfe_dev->started) {
1184                 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1185                 ret = -EBUSY;
1186                 goto unlock_out;
1187         }
1188
1189         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1190                                          core, s_std, *std_id);
1191         if (ret < 0) {
1192                 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1193                 goto unlock_out;
1194         }
1195         ret = vpfe_config_image_format(vpfe_dev, std_id);
1196
1197 unlock_out:
1198         mutex_unlock(&vpfe_dev->lock);
1199         return ret;
1200 }
1201
1202 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1203 {
1204         struct vpfe_device *vpfe_dev = video_drvdata(file);
1205
1206         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1207
1208         *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1209         return 0;
1210 }
1211 /*
1212  *  Videobuf operations
1213  */
1214 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1215                                 unsigned int *count,
1216                                 unsigned int *size)
1217 {
1218         struct vpfe_fh *fh = vq->priv_data;
1219         struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1220
1221         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1222         *size = config_params.device_bufsize;
1223
1224         if (*count < config_params.min_numbuffers)
1225                 *count = config_params.min_numbuffers;
1226         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1227                 "count=%d, size=%d\n", *count, *size);
1228         return 0;
1229 }
1230
1231 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1232                                 struct videobuf_buffer *vb,
1233                                 enum v4l2_field field)
1234 {
1235         struct vpfe_fh *fh = vq->priv_data;
1236         struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1237
1238         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1239
1240         /* If buffer is not initialized, initialize it */
1241         if (VIDEOBUF_NEEDS_INIT == vb->state) {
1242                 vb->width = vpfe_dev->fmt.fmt.pix.width;
1243                 vb->height = vpfe_dev->fmt.fmt.pix.height;
1244                 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1245                 vb->field = field;
1246         }
1247         vb->state = VIDEOBUF_PREPARED;
1248         return 0;
1249 }
1250
1251 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1252                                 struct videobuf_buffer *vb)
1253 {
1254         /* Get the file handle object and device object */
1255         struct vpfe_fh *fh = vq->priv_data;
1256         struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1257         unsigned long flags;
1258
1259         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1260
1261         /* add the buffer to the DMA queue */
1262         spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1263         list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1264         spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1265
1266         /* Change state of the buffer */
1267         vb->state = VIDEOBUF_QUEUED;
1268 }
1269
1270 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1271                                   struct videobuf_buffer *vb)
1272 {
1273         struct vpfe_fh *fh = vq->priv_data;
1274         struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1275         unsigned long flags;
1276
1277         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1278
1279         /*
1280          * We need to flush the buffer from the dma queue since
1281          * they are de-allocated
1282          */
1283         spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1284         INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1285         spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1286         videobuf_dma_contig_free(vq, vb);
1287         vb->state = VIDEOBUF_NEEDS_INIT;
1288 }
1289
1290 static struct videobuf_queue_ops vpfe_videobuf_qops = {
1291         .buf_setup      = vpfe_videobuf_setup,
1292         .buf_prepare    = vpfe_videobuf_prepare,
1293         .buf_queue      = vpfe_videobuf_queue,
1294         .buf_release    = vpfe_videobuf_release,
1295 };
1296
1297 /*
1298  * vpfe_reqbufs. currently support REQBUF only once opening
1299  * the device.
1300  */
1301 static int vpfe_reqbufs(struct file *file, void *priv,
1302                         struct v4l2_requestbuffers *req_buf)
1303 {
1304         struct vpfe_device *vpfe_dev = video_drvdata(file);
1305         struct vpfe_fh *fh = file->private_data;
1306         int ret = 0;
1307
1308         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1309
1310         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1311                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1312                 return -EINVAL;
1313         }
1314
1315         if (V4L2_MEMORY_USERPTR == req_buf->memory) {
1316                 /* we don't support user ptr IO */
1317                 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs:"
1318                          " USERPTR IO not supported\n");
1319                 return  -EINVAL;
1320         }
1321
1322         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1323         if (ret)
1324                 return ret;
1325
1326         if (vpfe_dev->io_usrs != 0) {
1327                 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1328                 ret = -EBUSY;
1329                 goto unlock_out;
1330         }
1331
1332         vpfe_dev->memory = req_buf->memory;
1333         videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1334                                 &vpfe_videobuf_qops,
1335                                 vpfe_dev->pdev,
1336                                 &vpfe_dev->irqlock,
1337                                 req_buf->type,
1338                                 vpfe_dev->fmt.fmt.pix.field,
1339                                 sizeof(struct videobuf_buffer),
1340                                 fh);
1341
1342         fh->io_allowed = 1;
1343         vpfe_dev->io_usrs = 1;
1344         INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1345         ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1346 unlock_out:
1347         mutex_unlock(&vpfe_dev->lock);
1348         return ret;
1349 }
1350
1351 static int vpfe_querybuf(struct file *file, void *priv,
1352                          struct v4l2_buffer *buf)
1353 {
1354         struct vpfe_device *vpfe_dev = video_drvdata(file);
1355
1356         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1357
1358         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1359                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1360                 return  -EINVAL;
1361         }
1362
1363         if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1364                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1365                 return -EINVAL;
1366         }
1367         /* Call videobuf_querybuf to get information */
1368         return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1369 }
1370
1371 static int vpfe_qbuf(struct file *file, void *priv,
1372                      struct v4l2_buffer *p)
1373 {
1374         struct vpfe_device *vpfe_dev = video_drvdata(file);
1375         struct vpfe_fh *fh = file->private_data;
1376
1377         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1378
1379         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1380                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1381                 return -EINVAL;
1382         }
1383
1384         /*
1385          * If this file handle is not allowed to do IO,
1386          * return error
1387          */
1388         if (!fh->io_allowed) {
1389                 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1390                 return -EACCES;
1391         }
1392         return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1393 }
1394
1395 static int vpfe_dqbuf(struct file *file, void *priv,
1396                       struct v4l2_buffer *buf)
1397 {
1398         struct vpfe_device *vpfe_dev = video_drvdata(file);
1399
1400         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1401
1402         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1403                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1404                 return -EINVAL;
1405         }
1406         return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1407                                       buf, file->f_flags & O_NONBLOCK);
1408 }
1409
1410 static int vpfe_queryctrl(struct file *file, void *priv,
1411                 struct v4l2_queryctrl *qctrl)
1412 {
1413         struct vpfe_device *vpfe_dev = video_drvdata(file);
1414         struct vpfe_subdev_info *sdinfo;
1415
1416         sdinfo = vpfe_dev->current_subdev;
1417
1418         return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1419                                          core, queryctrl, qctrl);
1420
1421 }
1422
1423 static int vpfe_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1424 {
1425         struct vpfe_device *vpfe_dev = video_drvdata(file);
1426         struct vpfe_subdev_info *sdinfo;
1427
1428         sdinfo = vpfe_dev->current_subdev;
1429
1430         return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1431                                          core, g_ctrl, ctrl);
1432 }
1433
1434 static int vpfe_s_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1435 {
1436         struct vpfe_device *vpfe_dev = video_drvdata(file);
1437         struct vpfe_subdev_info *sdinfo;
1438
1439         sdinfo = vpfe_dev->current_subdev;
1440
1441         return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1442                                          core, s_ctrl, ctrl);
1443 }
1444
1445 /*
1446  * vpfe_calculate_offsets : This function calculates buffers offset
1447  * for top and bottom field
1448  */
1449 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1450 {
1451         struct v4l2_rect image_win;
1452
1453         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1454
1455         ccdc_dev->hw_ops.get_image_window(&image_win);
1456         vpfe_dev->field_off = image_win.height * image_win.width;
1457 }
1458
1459 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1460 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1461 {
1462         ccdc_dev->hw_ops.enable(1);
1463         if (ccdc_dev->hw_ops.enable_out_to_sdram)
1464                 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1465         vpfe_dev->started = 1;
1466 }
1467
1468 /*
1469  * vpfe_streamon. Assume the DMA queue is not empty.
1470  * application is expected to call QBUF before calling
1471  * this ioctl. If not, driver returns error
1472  */
1473 static int vpfe_streamon(struct file *file, void *priv,
1474                          enum v4l2_buf_type buf_type)
1475 {
1476         struct vpfe_device *vpfe_dev = video_drvdata(file);
1477         struct vpfe_fh *fh = file->private_data;
1478         struct vpfe_subdev_info *sdinfo;
1479         unsigned long addr;
1480         int ret = 0;
1481
1482         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1483
1484         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1485                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1486                 return -EINVAL;
1487         }
1488
1489         /* If file handle is not allowed IO, return error */
1490         if (!fh->io_allowed) {
1491                 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1492                 return -EACCES;
1493         }
1494
1495         sdinfo = vpfe_dev->current_subdev;
1496         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1497                                         video, s_stream, 1);
1498
1499         if (ret && (ret != -ENOIOCTLCMD)) {
1500                 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1501                 return -EINVAL;
1502         }
1503
1504         /* If buffer queue is empty, return error */
1505         if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1506                 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1507                 return -EIO;
1508         }
1509
1510         /* Call videobuf_streamon to start streaming * in videobuf */
1511         ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1512         if (ret)
1513                 return ret;
1514
1515
1516         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1517         if (ret)
1518                 goto streamoff;
1519         /* Get the next frame from the buffer queue */
1520         vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1521                                         struct videobuf_buffer, queue);
1522         vpfe_dev->cur_frm = vpfe_dev->next_frm;
1523         /* Remove buffer from the buffer queue */
1524         list_del(&vpfe_dev->cur_frm->queue);
1525         /* Mark state of the current frame to active */
1526         vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1527         /* Initialize field_id and started member */
1528         vpfe_dev->field_id = 0;
1529         addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1530
1531         /* Calculate field offset */
1532         vpfe_calculate_offsets(vpfe_dev);
1533
1534         if (vpfe_attach_irq(vpfe_dev) < 0) {
1535                 v4l2_err(&vpfe_dev->v4l2_dev,
1536                          "Error in attaching interrupt handle\n");
1537                 ret = -EFAULT;
1538                 goto unlock_out;
1539         }
1540         if (ccdc_dev->hw_ops.configure() < 0) {
1541                 v4l2_err(&vpfe_dev->v4l2_dev,
1542                          "Error in configuring ccdc\n");
1543                 ret = -EINVAL;
1544                 goto unlock_out;
1545         }
1546         ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1547         vpfe_start_ccdc_capture(vpfe_dev);
1548         mutex_unlock(&vpfe_dev->lock);
1549         return ret;
1550 unlock_out:
1551         mutex_unlock(&vpfe_dev->lock);
1552 streamoff:
1553         ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1554         return ret;
1555 }
1556
1557 static int vpfe_streamoff(struct file *file, void *priv,
1558                           enum v4l2_buf_type buf_type)
1559 {
1560         struct vpfe_device *vpfe_dev = video_drvdata(file);
1561         struct vpfe_fh *fh = file->private_data;
1562         struct vpfe_subdev_info *sdinfo;
1563         int ret = 0;
1564
1565         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1566
1567         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1568                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1569                 return -EINVAL;
1570         }
1571
1572         /* If io is allowed for this file handle, return error */
1573         if (!fh->io_allowed) {
1574                 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1575                 return -EACCES;
1576         }
1577
1578         /* If streaming is not started, return error */
1579         if (!vpfe_dev->started) {
1580                 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1581                 return -EINVAL;
1582         }
1583
1584         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1585         if (ret)
1586                 return ret;
1587
1588         vpfe_stop_ccdc_capture(vpfe_dev);
1589         vpfe_detach_irq(vpfe_dev);
1590
1591         sdinfo = vpfe_dev->current_subdev;
1592         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1593                                         video, s_stream, 0);
1594
1595         if (ret && (ret != -ENOIOCTLCMD))
1596                 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1597         ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1598         mutex_unlock(&vpfe_dev->lock);
1599         return ret;
1600 }
1601
1602 static int vpfe_cropcap(struct file *file, void *priv,
1603                               struct v4l2_cropcap *crop)
1604 {
1605         struct vpfe_device *vpfe_dev = video_drvdata(file);
1606
1607         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1608
1609         if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1610                 return -EINVAL;
1611
1612         memset(crop, 0, sizeof(struct v4l2_cropcap));
1613         crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1614         crop->bounds.width = crop->defrect.width =
1615                 vpfe_standards[vpfe_dev->std_index].width;
1616         crop->bounds.height = crop->defrect.height =
1617                 vpfe_standards[vpfe_dev->std_index].height;
1618         crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1619         return 0;
1620 }
1621
1622 static int vpfe_g_crop(struct file *file, void *priv,
1623                              struct v4l2_crop *crop)
1624 {
1625         struct vpfe_device *vpfe_dev = video_drvdata(file);
1626
1627         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_crop\n");
1628
1629         crop->c = vpfe_dev->crop;
1630         return 0;
1631 }
1632
1633 static int vpfe_s_crop(struct file *file, void *priv,
1634                              struct v4l2_crop *crop)
1635 {
1636         struct vpfe_device *vpfe_dev = video_drvdata(file);
1637         int ret = 0;
1638
1639         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_crop\n");
1640
1641         if (vpfe_dev->started) {
1642                 /* make sure streaming is not started */
1643                 v4l2_err(&vpfe_dev->v4l2_dev,
1644                         "Cannot change crop when streaming is ON\n");
1645                 return -EBUSY;
1646         }
1647
1648         ret = mutex_lock_interruptible(&vpfe_dev->lock);
1649         if (ret)
1650                 return ret;
1651
1652         if (crop->c.top < 0 || crop->c.left < 0) {
1653                 v4l2_err(&vpfe_dev->v4l2_dev,
1654                         "doesn't support negative values for top & left\n");
1655                 ret = -EINVAL;
1656                 goto unlock_out;
1657         }
1658
1659         /* adjust the width to 16 pixel boundry */
1660         crop->c.width = ((crop->c.width + 15) & ~0xf);
1661
1662         /* make sure parameters are valid */
1663         if ((crop->c.left + crop->c.width >
1664                 vpfe_dev->std_info.active_pixels) ||
1665             (crop->c.top + crop->c.height >
1666                 vpfe_dev->std_info.active_lines)) {
1667                 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_CROP params\n");
1668                 ret = -EINVAL;
1669                 goto unlock_out;
1670         }
1671         ccdc_dev->hw_ops.set_image_window(&crop->c);
1672         vpfe_dev->fmt.fmt.pix.width = crop->c.width;
1673         vpfe_dev->fmt.fmt.pix.height = crop->c.height;
1674         vpfe_dev->fmt.fmt.pix.bytesperline =
1675                 ccdc_dev->hw_ops.get_line_length();
1676         vpfe_dev->fmt.fmt.pix.sizeimage =
1677                 vpfe_dev->fmt.fmt.pix.bytesperline *
1678                 vpfe_dev->fmt.fmt.pix.height;
1679         vpfe_dev->crop = crop->c;
1680 unlock_out:
1681         mutex_unlock(&vpfe_dev->lock);
1682         return ret;
1683 }
1684
1685
1686 static long vpfe_param_handler(struct file *file, void *priv,
1687                 int cmd, void *param)
1688 {
1689         struct vpfe_device *vpfe_dev = video_drvdata(file);
1690         int ret = 0;
1691
1692         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
1693
1694         if (vpfe_dev->started) {
1695                 /* only allowed if streaming is not started */
1696                 v4l2_err(&vpfe_dev->v4l2_dev, "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                 ret = ccdc_dev->hw_ops.set_params(param);
1709                 if (ret) {
1710                         v4l2_err(&vpfe_dev->v4l2_dev,
1711                                 "Error in setting parameters in CCDC\n");
1712                         goto unlock_out;
1713                 }
1714                 if (vpfe_get_ccdc_image_format(vpfe_dev, &vpfe_dev->fmt) < 0) {
1715                         v4l2_err(&vpfe_dev->v4l2_dev,
1716                                 "Invalid image format at CCDC\n");
1717                         goto unlock_out;
1718                 }
1719                 break;
1720         default:
1721                 ret = -EINVAL;
1722         }
1723 unlock_out:
1724         mutex_unlock(&vpfe_dev->lock);
1725         return ret;
1726 }
1727
1728
1729 /* vpfe capture ioctl operations */
1730 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1731         .vidioc_querycap         = vpfe_querycap,
1732         .vidioc_g_fmt_vid_cap    = vpfe_g_fmt_vid_cap,
1733         .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1734         .vidioc_s_fmt_vid_cap    = vpfe_s_fmt_vid_cap,
1735         .vidioc_try_fmt_vid_cap  = vpfe_try_fmt_vid_cap,
1736         .vidioc_enum_input       = vpfe_enum_input,
1737         .vidioc_g_input          = vpfe_g_input,
1738         .vidioc_s_input          = vpfe_s_input,
1739         .vidioc_querystd         = vpfe_querystd,
1740         .vidioc_s_std            = vpfe_s_std,
1741         .vidioc_g_std            = vpfe_g_std,
1742         .vidioc_queryctrl        = vpfe_queryctrl,
1743         .vidioc_g_ctrl           = vpfe_g_ctrl,
1744         .vidioc_s_ctrl           = vpfe_s_ctrl,
1745         .vidioc_reqbufs          = vpfe_reqbufs,
1746         .vidioc_querybuf         = vpfe_querybuf,
1747         .vidioc_qbuf             = vpfe_qbuf,
1748         .vidioc_dqbuf            = vpfe_dqbuf,
1749         .vidioc_streamon         = vpfe_streamon,
1750         .vidioc_streamoff        = vpfe_streamoff,
1751         .vidioc_cropcap          = vpfe_cropcap,
1752         .vidioc_g_crop           = vpfe_g_crop,
1753         .vidioc_s_crop           = vpfe_s_crop,
1754         .vidioc_default          = vpfe_param_handler,
1755 };
1756
1757 static struct vpfe_device *vpfe_initialize(void)
1758 {
1759         struct vpfe_device *vpfe_dev;
1760
1761         /* Default number of buffers should be 3 */
1762         if ((numbuffers > 0) &&
1763             (numbuffers < config_params.min_numbuffers))
1764                 numbuffers = config_params.min_numbuffers;
1765
1766         /*
1767          * Set buffer size to min buffers size if invalid buffer size is
1768          * given
1769          */
1770         if (bufsize < config_params.min_bufsize)
1771                 bufsize = config_params.min_bufsize;
1772
1773         config_params.numbuffers = numbuffers;
1774
1775         if (numbuffers)
1776                 config_params.device_bufsize = bufsize;
1777
1778         /* Allocate memory for device objects */
1779         vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1780
1781         return vpfe_dev;
1782 }
1783
1784 /*
1785  * vpfe_probe : This function creates device entries by register
1786  * itself to the V4L2 driver and initializes fields of each
1787  * device objects
1788  */
1789 static __init int vpfe_probe(struct platform_device *pdev)
1790 {
1791         struct vpfe_subdev_info *sdinfo;
1792         struct vpfe_config *vpfe_cfg;
1793         struct resource *res1;
1794         struct vpfe_device *vpfe_dev;
1795         struct i2c_adapter *i2c_adap;
1796         struct video_device *vfd;
1797         int ret = -ENOMEM, i, j;
1798         int num_subdevs = 0;
1799
1800         /* Get the pointer to the device object */
1801         vpfe_dev = vpfe_initialize();
1802
1803         if (!vpfe_dev) {
1804                 v4l2_err(pdev->dev.driver,
1805                         "Failed to allocate memory for vpfe_dev\n");
1806                 return ret;
1807         }
1808
1809         vpfe_dev->pdev = &pdev->dev;
1810
1811         if (NULL == pdev->dev.platform_data) {
1812                 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1813                 ret = -ENODEV;
1814                 goto probe_free_dev_mem;
1815         }
1816
1817         vpfe_cfg = pdev->dev.platform_data;
1818         vpfe_dev->cfg = vpfe_cfg;
1819         if (NULL == vpfe_cfg->ccdc ||
1820             NULL == vpfe_cfg->card_name ||
1821             NULL == vpfe_cfg->sub_devs) {
1822                 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1823                 ret = -ENOENT;
1824                 goto probe_free_dev_mem;
1825         }
1826
1827         mutex_lock(&ccdc_lock);
1828         /* Allocate memory for ccdc configuration */
1829         ccdc_cfg = kmalloc(sizeof(struct ccdc_config), GFP_KERNEL);
1830         if (NULL == ccdc_cfg) {
1831                 v4l2_err(pdev->dev.driver,
1832                          "Memory allocation failed for ccdc_cfg\n");
1833                 goto probe_free_dev_mem;
1834         }
1835
1836         strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1837         /* Get VINT0 irq resource */
1838         res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1839         if (!res1) {
1840                 v4l2_err(pdev->dev.driver,
1841                          "Unable to get interrupt for VINT0\n");
1842                 ret = -ENODEV;
1843                 goto probe_free_ccdc_cfg_mem;
1844         }
1845         vpfe_dev->ccdc_irq0 = res1->start;
1846
1847         /* Get VINT1 irq resource */
1848         res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1849         if (!res1) {
1850                 v4l2_err(pdev->dev.driver,
1851                          "Unable to get interrupt for VINT1\n");
1852                 ret = -ENODEV;
1853                 goto probe_free_ccdc_cfg_mem;
1854         }
1855         vpfe_dev->ccdc_irq1 = res1->start;
1856
1857         ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, IRQF_DISABLED,
1858                           "vpfe_capture0", vpfe_dev);
1859
1860         if (0 != ret) {
1861                 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1862                 goto probe_free_ccdc_cfg_mem;
1863         }
1864
1865         /* Allocate memory for video device */
1866         vfd = video_device_alloc();
1867         if (NULL == vfd) {
1868                 ret = -ENOMEM;
1869                 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
1870                 goto probe_out_release_irq;
1871         }
1872
1873         /* Initialize field of video device */
1874         vfd->release            = video_device_release;
1875         vfd->fops               = &vpfe_fops;
1876         vfd->ioctl_ops          = &vpfe_ioctl_ops;
1877         vfd->tvnorms            = 0;
1878         vfd->current_norm       = V4L2_STD_PAL;
1879         vfd->v4l2_dev           = &vpfe_dev->v4l2_dev;
1880         snprintf(vfd->name, sizeof(vfd->name),
1881                  "%s_V%d.%d.%d",
1882                  CAPTURE_DRV_NAME,
1883                  (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1884                  (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1885                  (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1886         /* Set video_dev to the video device */
1887         vpfe_dev->video_dev     = vfd;
1888
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_video_release;
1894         }
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);
1899
1900         /* Initialize field of the device objects */
1901         vpfe_dev->numbuffers = config_params.numbuffers;
1902
1903         /* Initialize prio member of device object */
1904         v4l2_prio_init(&vpfe_dev->prio);
1905         /* register video device */
1906         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1907                 "trying to register vpfe device.\n");
1908         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1909                 "video_dev=%x\n", (int)&vpfe_dev->video_dev);
1910         vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1911         ret = video_register_device(vpfe_dev->video_dev,
1912                                     VFL_TYPE_GRABBER, -1);
1913
1914         if (ret) {
1915                 v4l2_err(pdev->dev.driver,
1916                         "Unable to register video device.\n");
1917                 goto probe_out_v4l2_unregister;
1918         }
1919
1920         v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1921         /* set the driver data in platform device */
1922         platform_set_drvdata(pdev, vpfe_dev);
1923         /* set driver private data */
1924         video_set_drvdata(vpfe_dev->video_dev, vpfe_dev);
1925         i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1926         num_subdevs = vpfe_cfg->num_subdevs;
1927         vpfe_dev->sd = kmalloc(sizeof(struct v4l2_subdev *) * num_subdevs,
1928                                 GFP_KERNEL);
1929         if (NULL == vpfe_dev->sd) {
1930                 v4l2_err(&vpfe_dev->v4l2_dev,
1931                         "unable to allocate memory for subdevice pointers\n");
1932                 ret = -ENOMEM;
1933                 goto probe_out_video_unregister;
1934         }
1935
1936         for (i = 0; i < num_subdevs; i++) {
1937                 struct v4l2_input *inps;
1938
1939                 sdinfo = &vpfe_cfg->sub_devs[i];
1940
1941                 /* Load up the subdevice */
1942                 vpfe_dev->sd[i] =
1943                         v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1944                                                   i2c_adap,
1945                                                   sdinfo->name,
1946                                                   &sdinfo->board_info,
1947                                                   NULL);
1948                 if (vpfe_dev->sd[i]) {
1949                         v4l2_info(&vpfe_dev->v4l2_dev,
1950                                   "v4l2 sub device %s registered\n",
1951                                   sdinfo->name);
1952                         vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1953                         /* update tvnorms from the sub devices */
1954                         for (j = 0; j < sdinfo->num_inputs; j++) {
1955                                 inps = &sdinfo->inputs[j];
1956                                 vfd->tvnorms |= inps->std;
1957                         }
1958                 } else {
1959                         v4l2_info(&vpfe_dev->v4l2_dev,
1960                                   "v4l2 sub device %s register fails\n",
1961                                   sdinfo->name);
1962                         goto probe_sd_out;
1963                 }
1964         }
1965
1966         /* set first sub device as current one */
1967         vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1968
1969         /* We have at least one sub device to work with */
1970         mutex_unlock(&ccdc_lock);
1971         return 0;
1972
1973 probe_sd_out:
1974         kfree(vpfe_dev->sd);
1975 probe_out_video_unregister:
1976         video_unregister_device(vpfe_dev->video_dev);
1977 probe_out_v4l2_unregister:
1978         v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1979 probe_out_video_release:
1980         if (!video_is_registered(vpfe_dev->video_dev))
1981                 video_device_release(vpfe_dev->video_dev);
1982 probe_out_release_irq:
1983         free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1984 probe_free_ccdc_cfg_mem:
1985         mutex_unlock(&ccdc_lock);
1986         kfree(ccdc_cfg);
1987 probe_free_dev_mem:
1988         kfree(vpfe_dev);
1989         return ret;
1990 }
1991
1992 /*
1993  * vpfe_remove : It un-register device from V4L2 driver
1994  */
1995 static int __devexit vpfe_remove(struct platform_device *pdev)
1996 {
1997         struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
1998
1999         v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2000
2001         free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2002         kfree(vpfe_dev->sd);
2003         v4l2_device_unregister(&vpfe_dev->v4l2_dev);
2004         video_unregister_device(vpfe_dev->video_dev);
2005         kfree(vpfe_dev);
2006         kfree(ccdc_cfg);
2007         return 0;
2008 }
2009
2010 static int
2011 vpfe_suspend(struct device *dev)
2012 {
2013         /* add suspend code here later */
2014         return -1;
2015 }
2016
2017 static int
2018 vpfe_resume(struct device *dev)
2019 {
2020         /* add resume code here later */
2021         return -1;
2022 }
2023
2024 static const struct dev_pm_ops vpfe_dev_pm_ops = {
2025         .suspend = vpfe_suspend,
2026         .resume = vpfe_resume,
2027 };
2028
2029 static struct platform_driver vpfe_driver = {
2030         .driver = {
2031                 .name = CAPTURE_DRV_NAME,
2032                 .owner = THIS_MODULE,
2033                 .pm = &vpfe_dev_pm_ops,
2034         },
2035         .probe = vpfe_probe,
2036         .remove = __devexit_p(vpfe_remove),
2037 };
2038
2039 static __init int vpfe_init(void)
2040 {
2041         printk(KERN_NOTICE "vpfe_init\n");
2042         /* Register driver to the kernel */
2043         return platform_driver_register(&vpfe_driver);
2044 }
2045
2046 /*
2047  * vpfe_cleanup : This function un-registers device driver
2048  */
2049 static void vpfe_cleanup(void)
2050 {
2051         platform_driver_unregister(&vpfe_driver);
2052 }
2053
2054 module_init(vpfe_init);
2055 module_exit(vpfe_cleanup);