Version:  2.0.40 2.2.26 2.4.37 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2

Linux/drivers/media/platform/davinci/vpfe_capture.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us