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