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