Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

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

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