Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vpfe_capture.c
Go to the documentation of this file.
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>
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);
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 {
98  unsigned int width;
99  unsigned int height;
101  /* 0 - progressive, 1 - interlaced */
103 };
104 
105 /* ccdc configuration */
106 struct ccdc_config {
107  /* This make sure vpfe is probed and ready to go */
109  /* name of ccdc device */
110  char name[32];
111 };
112 
113 /* data structures */
114 static struct vpfe_config_params config_params = {
115  .min_numbuffers = 3,
116  .numbuffers = 3,
117  .min_bufsize = 720 * 480 * 2,
118  .device_bufsize = 720 * 576 * 2,
119 };
120 
121 /* ccdc device registered */
122 static struct ccdc_hw_device *ccdc_dev;
123 /* lock for accessing ccdc information */
124 static DEFINE_MUTEX(ccdc_lock);
125 /* ccdc configuration */
126 static struct ccdc_config *ccdc_cfg;
127 
128 const struct vpfe_standard vpfe_standards[] = {
129  {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
130  {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
131 };
132 
133 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
134 static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
135  {
136  .fmtdesc = {
137  .index = 0,
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,
148  .description = "Bayer GrRBGb - 16bit",
149  .pixelformat = V4L2_PIX_FMT_SBGGR16,
150  },
151  .bpp = 2,
152  },
153  {
154  .fmtdesc = {
155  .index = 2,
157  .description = "Bayer GrRBGb 8bit DPCM compr.",
158  .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
159  },
160  .bpp = 1,
161  },
162  {
163  .fmtdesc = {
164  .index = 3,
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,
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,
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 }
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 }
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));
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) {
347  /* do nothing, since it is default */
348  ret = ccdc_dev->hw_ops.set_buftype(
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(
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 support g_mbus_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  const v4l2_std_id *std_id)
380 {
381  struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
382  struct v4l2_mbus_framefmt mbus_fmt;
383  struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
384  int i, ret = 0;
385 
386  for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
387  if (vpfe_standards[i].std_id & *std_id) {
388  vpfe_dev->std_info.active_pixels =
389  vpfe_standards[i].width;
390  vpfe_dev->std_info.active_lines =
391  vpfe_standards[i].height;
392  vpfe_dev->std_info.frame_format =
393  vpfe_standards[i].frame_format;
394  vpfe_dev->std_index = i;
395  break;
396  }
397  }
398 
399  if (i == ARRAY_SIZE(vpfe_standards)) {
400  v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
401  return -EINVAL;
402  }
403 
404  vpfe_dev->crop.top = 0;
405  vpfe_dev->crop.left = 0;
406  vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
407  vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
408  pix->width = vpfe_dev->crop.width;
409  pix->height = vpfe_dev->crop.height;
410 
411  /* first field and frame format based on standard frame format */
412  if (vpfe_dev->std_info.frame_format) {
414  /* assume V4L2_PIX_FMT_UYVY as default */
416  v4l2_fill_mbus_format(&mbus_fmt, pix,
418  } else {
419  pix->field = V4L2_FIELD_NONE;
420  /* assume V4L2_PIX_FMT_SBGGR8 */
422  v4l2_fill_mbus_format(&mbus_fmt, pix,
424  }
425 
426  /* if sub device supports g_mbus_fmt, override the defaults */
427  ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
428  sdinfo->grp_id, video, g_mbus_fmt, &mbus_fmt);
429 
430  if (ret && ret != -ENOIOCTLCMD) {
431  v4l2_err(&vpfe_dev->v4l2_dev,
432  "error in getting g_mbus_fmt from sub device\n");
433  return ret;
434  }
435  v4l2_fill_pix_format(pix, &mbus_fmt);
436  pix->bytesperline = pix->width * 2;
437  pix->sizeimage = pix->bytesperline * pix->height;
438 
439  /* Sets the values in CCDC */
440  ret = vpfe_config_ccdc_image_format(vpfe_dev);
441  if (ret)
442  return ret;
443 
444  /* Update the values of sizeimage and bytesperline */
445  if (!ret) {
446  pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
447  pix->sizeimage = pix->bytesperline * pix->height;
448  }
449  return ret;
450 }
451 
452 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
453 {
454  int ret = 0;
455 
456  /* set first input of current subdevice as the current input */
457  vpfe_dev->current_input = 0;
458 
459  /* set default standard */
460  vpfe_dev->std_index = 0;
461 
462  /* Configure the default format information */
463  ret = vpfe_config_image_format(vpfe_dev,
464  &vpfe_standards[vpfe_dev->std_index].std_id);
465  if (ret)
466  return ret;
467 
468  /* now open the ccdc device to initialize it */
469  mutex_lock(&ccdc_lock);
470  if (NULL == ccdc_dev) {
471  v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
472  ret = -ENODEV;
473  goto unlock;
474  }
475 
476  if (!try_module_get(ccdc_dev->owner)) {
477  v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
478  ret = -ENODEV;
479  goto unlock;
480  }
481  ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
482  if (!ret)
483  vpfe_dev->initialized = 1;
484 
485  /* Clear all VPFE/CCDC interrupts */
486  if (vpfe_dev->cfg->clr_intr)
487  vpfe_dev->cfg->clr_intr(-1);
488 
489 unlock:
490  mutex_unlock(&ccdc_lock);
491  return ret;
492 }
493 
494 /*
495  * vpfe_open : It creates object of file handle structure and
496  * stores it in private_data member of filepointer
497  */
498 static int vpfe_open(struct file *file)
499 {
500  struct vpfe_device *vpfe_dev = video_drvdata(file);
501  struct vpfe_fh *fh;
502 
503  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
504 
505  if (!vpfe_dev->cfg->num_subdevs) {
506  v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
507  return -ENODEV;
508  }
509 
510  /* Allocate memory for the file handle object */
511  fh = kmalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
512  if (NULL == fh) {
513  v4l2_err(&vpfe_dev->v4l2_dev,
514  "unable to allocate memory for file handle object\n");
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  mutex_lock(&vpfe_dev->lock);
521  /* If decoder is not initialized. initialize it */
522  if (!vpfe_dev->initialized) {
523  if (vpfe_initialize_device(vpfe_dev)) {
524  mutex_unlock(&vpfe_dev->lock);
525  return -ENODEV;
526  }
527  }
528  /* Increment device usrs counter */
529  vpfe_dev->usrs++;
530  /* Set io_allowed member to false */
531  fh->io_allowed = 0;
532  /* Initialize priority of this instance to default priority */
533  fh->prio = V4L2_PRIORITY_UNSET;
534  v4l2_prio_open(&vpfe_dev->prio, &fh->prio);
535  mutex_unlock(&vpfe_dev->lock);
536  return 0;
537 }
538 
539 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
540 {
541  unsigned long addr;
542 
543  vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
544  struct videobuf_buffer, queue);
545  list_del(&vpfe_dev->next_frm->queue);
546  vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
547  addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
548 
549  ccdc_dev->hw_ops.setfbaddr(addr);
550 }
551 
552 static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
553 {
554  unsigned long addr;
555 
556  addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
557  addr += vpfe_dev->field_off;
558  ccdc_dev->hw_ops.setfbaddr(addr);
559 }
560 
561 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
562 {
563  struct timeval timevalue;
564 
565  do_gettimeofday(&timevalue);
566  vpfe_dev->cur_frm->ts = timevalue;
567  vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
568  vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
569  wake_up_interruptible(&vpfe_dev->cur_frm->done);
570  vpfe_dev->cur_frm = vpfe_dev->next_frm;
571 }
572 
573 /* ISR for VINT0*/
574 static irqreturn_t vpfe_isr(int irq, void *dev_id)
575 {
576  struct vpfe_device *vpfe_dev = dev_id;
577  enum v4l2_field field;
578  int fid;
579 
580  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
581  field = vpfe_dev->fmt.fmt.pix.field;
582 
583  /* if streaming not started, don't do anything */
584  if (!vpfe_dev->started)
585  goto clear_intr;
586 
587  /* only for 6446 this will be applicable */
588  if (NULL != ccdc_dev->hw_ops.reset)
589  ccdc_dev->hw_ops.reset();
590 
591  if (field == V4L2_FIELD_NONE) {
592  /* handle progressive frame capture */
593  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
594  "frame format is progressive...\n");
595  if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
596  vpfe_process_buffer_complete(vpfe_dev);
597  goto clear_intr;
598  }
599 
600  /* interlaced or TB capture check which field we are in hardware */
601  fid = ccdc_dev->hw_ops.getfid();
602 
603  /* switch the software maintained field id */
604  vpfe_dev->field_id ^= 1;
605  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
606  fid, vpfe_dev->field_id);
607  if (fid == vpfe_dev->field_id) {
608  /* we are in-sync here,continue */
609  if (fid == 0) {
610  /*
611  * One frame is just being captured. If the next frame
612  * is available, release the current frame and move on
613  */
614  if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
615  vpfe_process_buffer_complete(vpfe_dev);
616  /*
617  * based on whether the two fields are stored
618  * interleavely or separately in memory, reconfigure
619  * the CCDC memory address
620  */
621  if (field == V4L2_FIELD_SEQ_TB) {
622  vpfe_schedule_bottom_field(vpfe_dev);
623  }
624  goto clear_intr;
625  }
626  /*
627  * if one field is just being captured configure
628  * the next frame get the next frame from the empty
629  * queue if no frame is available hold on to the
630  * current buffer
631  */
632  spin_lock(&vpfe_dev->dma_queue_lock);
633  if (!list_empty(&vpfe_dev->dma_queue) &&
634  vpfe_dev->cur_frm == vpfe_dev->next_frm)
635  vpfe_schedule_next_buffer(vpfe_dev);
636  spin_unlock(&vpfe_dev->dma_queue_lock);
637  } else if (fid == 0) {
638  /*
639  * out of sync. Recover from any hardware out-of-sync.
640  * May loose one frame
641  */
642  vpfe_dev->field_id = fid;
643  }
644 clear_intr:
645  if (vpfe_dev->cfg->clr_intr)
646  vpfe_dev->cfg->clr_intr(irq);
647 
648  return IRQ_HANDLED;
649 }
650 
651 /* vdint1_isr - isr handler for VINT1 interrupt */
652 static irqreturn_t vdint1_isr(int irq, void *dev_id)
653 {
654  struct vpfe_device *vpfe_dev = dev_id;
655 
656  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
657 
658  /* if streaming not started, don't do anything */
659  if (!vpfe_dev->started) {
660  if (vpfe_dev->cfg->clr_intr)
661  vpfe_dev->cfg->clr_intr(irq);
662  return IRQ_HANDLED;
663  }
664 
665  spin_lock(&vpfe_dev->dma_queue_lock);
666  if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
667  !list_empty(&vpfe_dev->dma_queue) &&
668  vpfe_dev->cur_frm == vpfe_dev->next_frm)
669  vpfe_schedule_next_buffer(vpfe_dev);
670  spin_unlock(&vpfe_dev->dma_queue_lock);
671 
672  if (vpfe_dev->cfg->clr_intr)
673  vpfe_dev->cfg->clr_intr(irq);
674 
675  return IRQ_HANDLED;
676 }
677 
678 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
679 {
680  enum ccdc_frmfmt frame_format;
681 
682  frame_format = ccdc_dev->hw_ops.get_frame_format();
683  if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
684  free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
685 }
686 
687 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
688 {
689  enum ccdc_frmfmt frame_format;
690 
691  frame_format = ccdc_dev->hw_ops.get_frame_format();
692  if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
693  return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
694  IRQF_DISABLED, "vpfe_capture1",
695  vpfe_dev);
696  }
697  return 0;
698 }
699 
700 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
701 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
702 {
703  vpfe_dev->started = 0;
704  ccdc_dev->hw_ops.enable(0);
705  if (ccdc_dev->hw_ops.enable_out_to_sdram)
706  ccdc_dev->hw_ops.enable_out_to_sdram(0);
707 }
708 
709 /*
710  * vpfe_release : This function deletes buffer queue, frees the
711  * buffers and the vpfe file handle
712  */
713 static int vpfe_release(struct file *file)
714 {
715  struct vpfe_device *vpfe_dev = video_drvdata(file);
716  struct vpfe_fh *fh = file->private_data;
717  struct vpfe_subdev_info *sdinfo;
718  int ret;
719 
720  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
721 
722  /* Get the device lock */
723  mutex_lock(&vpfe_dev->lock);
724  /* if this instance is doing IO */
725  if (fh->io_allowed) {
726  if (vpfe_dev->started) {
727  sdinfo = vpfe_dev->current_subdev;
728  ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
729  sdinfo->grp_id,
730  video, s_stream, 0);
731  if (ret && (ret != -ENOIOCTLCMD))
732  v4l2_err(&vpfe_dev->v4l2_dev,
733  "stream off failed in subdev\n");
734  vpfe_stop_ccdc_capture(vpfe_dev);
735  vpfe_detach_irq(vpfe_dev);
736  videobuf_streamoff(&vpfe_dev->buffer_queue);
737  }
738  vpfe_dev->io_usrs = 0;
739  vpfe_dev->numbuffers = config_params.numbuffers;
740  }
741 
742  /* Decrement device usrs counter */
743  vpfe_dev->usrs--;
744  /* Close the priority */
745  v4l2_prio_close(&vpfe_dev->prio, fh->prio);
746  /* If this is the last file handle */
747  if (!vpfe_dev->usrs) {
748  vpfe_dev->initialized = 0;
749  if (ccdc_dev->hw_ops.close)
750  ccdc_dev->hw_ops.close(vpfe_dev->pdev);
751  module_put(ccdc_dev->owner);
752  }
753  mutex_unlock(&vpfe_dev->lock);
754  file->private_data = NULL;
755  /* Free memory allocated to file handle object */
756  kfree(fh);
757  return 0;
758 }
759 
760 /*
761  * vpfe_mmap : It is used to map kernel space buffers
762  * into user spaces
763  */
764 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
765 {
766  /* Get the device object and file handle object */
767  struct vpfe_device *vpfe_dev = video_drvdata(file);
768 
769  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
770 
771  return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
772 }
773 
774 /*
775  * vpfe_poll: It is used for select/poll system call
776  */
777 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
778 {
779  struct vpfe_device *vpfe_dev = video_drvdata(file);
780 
781  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
782 
783  if (vpfe_dev->started)
784  return videobuf_poll_stream(file,
785  &vpfe_dev->buffer_queue, wait);
786  return 0;
787 }
788 
789 /* vpfe capture driver file operations */
790 static const struct v4l2_file_operations vpfe_fops = {
791  .owner = THIS_MODULE,
792  .open = vpfe_open,
793  .release = vpfe_release,
794  .unlocked_ioctl = video_ioctl2,
795  .mmap = vpfe_mmap,
796  .poll = vpfe_poll
797 };
798 
799 /*
800  * vpfe_check_format()
801  * This function adjust the input pixel format as per hardware
802  * capabilities and update the same in pixfmt.
803  * Following algorithm used :-
804  *
805  * If given pixformat is not in the vpfe list of pix formats or not
806  * supported by the hardware, current value of pixformat in the device
807  * is used
808  * If given field is not supported, then current field is used. If field
809  * is different from current, then it is matched with that from sub device.
810  * Minimum height is 2 lines for interlaced or tb field and 1 line for
811  * progressive. Maximum height is clamped to active active lines of scan
812  * Minimum width is 32 bytes in memory and width is clamped to active
813  * pixels of scan.
814  * bytesperline is a multiple of 32.
815  */
816 static const struct vpfe_pixel_format *
817  vpfe_check_format(struct vpfe_device *vpfe_dev,
818  struct v4l2_pix_format *pixfmt)
819 {
821  const struct vpfe_pixel_format *vpfe_pix_fmt;
822  u32 pix;
823  int temp, found;
824 
825  vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
826  if (NULL == vpfe_pix_fmt) {
827  /*
828  * use current pixel format in the vpfe device. We
829  * will find this pix format in the table
830  */
831  pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
832  vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
833  }
834 
835  /* check if hw supports it */
836  temp = 0;
837  found = 0;
838  while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
839  if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
840  found = 1;
841  break;
842  }
843  temp++;
844  }
845 
846  if (!found) {
847  /* use current pixel format */
848  pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
849  /*
850  * Since this is currently used in the vpfe device, we
851  * will find this pix format in the table
852  */
853  vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
854  }
855 
856  /* check what field format is supported */
857  if (pixfmt->field == V4L2_FIELD_ANY) {
858  /* if field is any, use current value as default */
859  pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
860  }
861 
862  /*
863  * if field is not same as current field in the vpfe device
864  * try matching the field with the sub device field
865  */
866  if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
867  /*
868  * If field value is not in the supported fields, use current
869  * field used in the device as default
870  */
871  switch (pixfmt->field) {
873  case V4L2_FIELD_SEQ_TB:
874  /* if sub device is supporting progressive, use that */
875  if (!vpfe_dev->std_info.frame_format)
876  pixfmt->field = V4L2_FIELD_NONE;
877  break;
878  case V4L2_FIELD_NONE:
879  if (vpfe_dev->std_info.frame_format)
880  pixfmt->field = V4L2_FIELD_INTERLACED;
881  break;
882 
883  default:
884  /* use current field as default */
885  pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
886  break;
887  }
888  }
889 
890  /* Now adjust image resolutions supported */
891  if (pixfmt->field == V4L2_FIELD_INTERLACED ||
892  pixfmt->field == V4L2_FIELD_SEQ_TB)
893  min_height = 2;
894 
895  max_width = vpfe_dev->std_info.active_pixels;
896  max_height = vpfe_dev->std_info.active_lines;
897  min_width /= vpfe_pix_fmt->bpp;
898 
899  v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
900  pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
901 
902  pixfmt->width = clamp((pixfmt->width), min_width, max_width);
903  pixfmt->height = clamp((pixfmt->height), min_height, max_height);
904 
905  /* If interlaced, adjust height to be a multiple of 2 */
906  if (pixfmt->field == V4L2_FIELD_INTERLACED)
907  pixfmt->height &= (~1);
908  /*
909  * recalculate bytesperline and sizeimage since width
910  * and height might have changed
911  */
912  pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
913  & ~31);
914  if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
915  pixfmt->sizeimage =
916  pixfmt->bytesperline * pixfmt->height +
917  ((pixfmt->bytesperline * pixfmt->height) >> 1);
918  else
919  pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
920 
921  v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height ="
922  " %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
923  pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
924  pixfmt->bytesperline, pixfmt->sizeimage);
925  return vpfe_pix_fmt;
926 }
927 
928 static int vpfe_querycap(struct file *file, void *priv,
929  struct v4l2_capability *cap)
930 {
931  struct vpfe_device *vpfe_dev = video_drvdata(file);
932 
933  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
934 
935  cap->version = VPFE_CAPTURE_VERSION_CODE;
937  strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
938  strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
939  strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
940  return 0;
941 }
942 
943 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
944  struct v4l2_format *fmt)
945 {
946  struct vpfe_device *vpfe_dev = video_drvdata(file);
947  int ret = 0;
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 ret;
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 vpfe_subdev_info *sdinfo;
1114  int subdev_index, inp_index;
1115  struct vpfe_route *route;
1116  u32 input = 0, output = 0;
1117  int ret = -EINVAL;
1118 
1119  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1120 
1121  ret = mutex_lock_interruptible(&vpfe_dev->lock);
1122  if (ret)
1123  return ret;
1124 
1125  /*
1126  * If streaming is started return device busy
1127  * error
1128  */
1129  if (vpfe_dev->started) {
1130  v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1131  ret = -EBUSY;
1132  goto unlock_out;
1133  }
1134  ret = vpfe_get_subdev_input_index(vpfe_dev,
1135  &subdev_index,
1136  &inp_index,
1137  index);
1138  if (ret < 0) {
1139  v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1140  goto unlock_out;
1141  }
1142 
1143  sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1144  route = &sdinfo->routes[inp_index];
1145  if (route && sdinfo->can_route) {
1146  input = route->input;
1147  output = route->output;
1148  }
1149 
1150  ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1151  video, s_routing, input, output, 0);
1152 
1153  if (ret) {
1154  v4l2_err(&vpfe_dev->v4l2_dev,
1155  "vpfe_doioctl:error in setting input in decoder\n");
1156  ret = -EINVAL;
1157  goto unlock_out;
1158  }
1159  vpfe_dev->current_subdev = sdinfo;
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 = 0;
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 = 0;
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  core, 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 = 0;
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 static int vpfe_queryctrl(struct file *file, void *priv,
1446  struct v4l2_queryctrl *qctrl)
1447 {
1448  struct vpfe_device *vpfe_dev = video_drvdata(file);
1449  struct vpfe_subdev_info *sdinfo;
1450 
1451  sdinfo = vpfe_dev->current_subdev;
1452 
1453  return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1454  core, queryctrl, qctrl);
1455 
1456 }
1457 
1458 static int vpfe_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1459 {
1460  struct vpfe_device *vpfe_dev = video_drvdata(file);
1461  struct vpfe_subdev_info *sdinfo;
1462 
1463  sdinfo = vpfe_dev->current_subdev;
1464 
1465  return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1466  core, g_ctrl, ctrl);
1467 }
1468 
1469 static int vpfe_s_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1470 {
1471  struct vpfe_device *vpfe_dev = video_drvdata(file);
1472  struct vpfe_subdev_info *sdinfo;
1473 
1474  sdinfo = vpfe_dev->current_subdev;
1475 
1476  return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1477  core, s_ctrl, ctrl);
1478 }
1479 
1480 /*
1481  * vpfe_calculate_offsets : This function calculates buffers offset
1482  * for top and bottom field
1483  */
1484 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1485 {
1486  struct v4l2_rect image_win;
1487 
1488  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1489 
1490  ccdc_dev->hw_ops.get_image_window(&image_win);
1491  vpfe_dev->field_off = image_win.height * image_win.width;
1492 }
1493 
1494 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1495 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1496 {
1497  ccdc_dev->hw_ops.enable(1);
1498  if (ccdc_dev->hw_ops.enable_out_to_sdram)
1499  ccdc_dev->hw_ops.enable_out_to_sdram(1);
1500  vpfe_dev->started = 1;
1501 }
1502 
1503 /*
1504  * vpfe_streamon. Assume the DMA queue is not empty.
1505  * application is expected to call QBUF before calling
1506  * this ioctl. If not, driver returns error
1507  */
1508 static int vpfe_streamon(struct file *file, void *priv,
1509  enum v4l2_buf_type buf_type)
1510 {
1511  struct vpfe_device *vpfe_dev = video_drvdata(file);
1512  struct vpfe_fh *fh = file->private_data;
1513  struct vpfe_subdev_info *sdinfo;
1514  unsigned long addr;
1515  int ret = 0;
1516 
1517  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1518 
1519  if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1520  v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1521  return -EINVAL;
1522  }
1523 
1524  /* If file handle is not allowed IO, return error */
1525  if (!fh->io_allowed) {
1526  v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1527  return -EACCES;
1528  }
1529 
1530  sdinfo = vpfe_dev->current_subdev;
1531  ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1532  video, s_stream, 1);
1533 
1534  if (ret && (ret != -ENOIOCTLCMD)) {
1535  v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1536  return -EINVAL;
1537  }
1538 
1539  /* If buffer queue is empty, return error */
1540  if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1541  v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1542  return -EIO;
1543  }
1544 
1545  /* Call videobuf_streamon to start streaming * in videobuf */
1546  ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1547  if (ret)
1548  return ret;
1549 
1550 
1551  ret = mutex_lock_interruptible(&vpfe_dev->lock);
1552  if (ret)
1553  goto streamoff;
1554  /* Get the next frame from the buffer queue */
1555  vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1556  struct videobuf_buffer, queue);
1557  vpfe_dev->cur_frm = vpfe_dev->next_frm;
1558  /* Remove buffer from the buffer queue */
1559  list_del(&vpfe_dev->cur_frm->queue);
1560  /* Mark state of the current frame to active */
1561  vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1562  /* Initialize field_id and started member */
1563  vpfe_dev->field_id = 0;
1564  addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1565 
1566  /* Calculate field offset */
1567  vpfe_calculate_offsets(vpfe_dev);
1568 
1569  if (vpfe_attach_irq(vpfe_dev) < 0) {
1570  v4l2_err(&vpfe_dev->v4l2_dev,
1571  "Error in attaching interrupt handle\n");
1572  ret = -EFAULT;
1573  goto unlock_out;
1574  }
1575  if (ccdc_dev->hw_ops.configure() < 0) {
1576  v4l2_err(&vpfe_dev->v4l2_dev,
1577  "Error in configuring ccdc\n");
1578  ret = -EINVAL;
1579  goto unlock_out;
1580  }
1581  ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1582  vpfe_start_ccdc_capture(vpfe_dev);
1583  mutex_unlock(&vpfe_dev->lock);
1584  return ret;
1585 unlock_out:
1586  mutex_unlock(&vpfe_dev->lock);
1587 streamoff:
1588  ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1589  return ret;
1590 }
1591 
1592 static int vpfe_streamoff(struct file *file, void *priv,
1593  enum v4l2_buf_type buf_type)
1594 {
1595  struct vpfe_device *vpfe_dev = video_drvdata(file);
1596  struct vpfe_fh *fh = file->private_data;
1597  struct vpfe_subdev_info *sdinfo;
1598  int ret = 0;
1599 
1600  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1601 
1602  if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1603  v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1604  return -EINVAL;
1605  }
1606 
1607  /* If io is allowed for this file handle, return error */
1608  if (!fh->io_allowed) {
1609  v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1610  return -EACCES;
1611  }
1612 
1613  /* If streaming is not started, return error */
1614  if (!vpfe_dev->started) {
1615  v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1616  return -EINVAL;
1617  }
1618 
1619  ret = mutex_lock_interruptible(&vpfe_dev->lock);
1620  if (ret)
1621  return ret;
1622 
1623  vpfe_stop_ccdc_capture(vpfe_dev);
1624  vpfe_detach_irq(vpfe_dev);
1625 
1626  sdinfo = vpfe_dev->current_subdev;
1627  ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1628  video, s_stream, 0);
1629 
1630  if (ret && (ret != -ENOIOCTLCMD))
1631  v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1632  ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1633  mutex_unlock(&vpfe_dev->lock);
1634  return ret;
1635 }
1636 
1637 static int vpfe_cropcap(struct file *file, void *priv,
1638  struct v4l2_cropcap *crop)
1639 {
1640  struct vpfe_device *vpfe_dev = video_drvdata(file);
1641 
1642  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1643 
1644  if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1645  return -EINVAL;
1646 
1647  memset(crop, 0, sizeof(struct v4l2_cropcap));
1649  crop->bounds.width = crop->defrect.width =
1650  vpfe_standards[vpfe_dev->std_index].width;
1651  crop->bounds.height = crop->defrect.height =
1652  vpfe_standards[vpfe_dev->std_index].height;
1653  crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1654  return 0;
1655 }
1656 
1657 static int vpfe_g_crop(struct file *file, void *priv,
1658  struct v4l2_crop *crop)
1659 {
1660  struct vpfe_device *vpfe_dev = video_drvdata(file);
1661 
1662  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_crop\n");
1663 
1664  crop->c = vpfe_dev->crop;
1665  return 0;
1666 }
1667 
1668 static int vpfe_s_crop(struct file *file, void *priv,
1669  const struct v4l2_crop *crop)
1670 {
1671  struct vpfe_device *vpfe_dev = video_drvdata(file);
1672  struct v4l2_rect rect = crop->c;
1673  int ret = 0;
1674 
1675  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_crop\n");
1676 
1677  if (vpfe_dev->started) {
1678  /* make sure streaming is not started */
1679  v4l2_err(&vpfe_dev->v4l2_dev,
1680  "Cannot change crop when streaming is ON\n");
1681  return -EBUSY;
1682  }
1683 
1684  ret = mutex_lock_interruptible(&vpfe_dev->lock);
1685  if (ret)
1686  return ret;
1687 
1688  if (rect.top < 0 || rect.left < 0) {
1689  v4l2_err(&vpfe_dev->v4l2_dev,
1690  "doesn't support negative values for top & left\n");
1691  ret = -EINVAL;
1692  goto unlock_out;
1693  }
1694 
1695  /* adjust the width to 16 pixel boundary */
1696  rect.width = ((rect.width + 15) & ~0xf);
1697 
1698  /* make sure parameters are valid */
1699  if ((rect.left + rect.width >
1700  vpfe_dev->std_info.active_pixels) ||
1701  (rect.top + rect.height >
1702  vpfe_dev->std_info.active_lines)) {
1703  v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_CROP params\n");
1704  ret = -EINVAL;
1705  goto unlock_out;
1706  }
1707  ccdc_dev->hw_ops.set_image_window(&rect);
1708  vpfe_dev->fmt.fmt.pix.width = rect.width;
1709  vpfe_dev->fmt.fmt.pix.height = rect.height;
1710  vpfe_dev->fmt.fmt.pix.bytesperline =
1711  ccdc_dev->hw_ops.get_line_length();
1712  vpfe_dev->fmt.fmt.pix.sizeimage =
1713  vpfe_dev->fmt.fmt.pix.bytesperline *
1714  vpfe_dev->fmt.fmt.pix.height;
1715  vpfe_dev->crop = rect;
1716 unlock_out:
1717  mutex_unlock(&vpfe_dev->lock);
1718  return ret;
1719 }
1720 
1721 
1722 static long vpfe_param_handler(struct file *file, void *priv,
1723  bool valid_prio, int cmd, void *param)
1724 {
1725  struct vpfe_device *vpfe_dev = video_drvdata(file);
1726  int ret = 0;
1727 
1728  v4l2_dbg(2, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
1729 
1730  if (vpfe_dev->started) {
1731  /* only allowed if streaming is not started */
1732  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1733  "device already started\n");
1734  return -EBUSY;
1735  }
1736 
1737  ret = mutex_lock_interruptible(&vpfe_dev->lock);
1738  if (ret)
1739  return ret;
1740 
1741  switch (cmd) {
1743  v4l2_warn(&vpfe_dev->v4l2_dev,
1744  "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
1745  if (ccdc_dev->hw_ops.set_params) {
1746  ret = ccdc_dev->hw_ops.set_params(param);
1747  if (ret) {
1748  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1749  "Error setting parameters in CCDC\n");
1750  goto unlock_out;
1751  }
1752  ret = vpfe_get_ccdc_image_format(vpfe_dev,
1753  &vpfe_dev->fmt);
1754  if (ret < 0) {
1755  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1756  "Invalid image format at CCDC\n");
1757  goto unlock_out;
1758  }
1759  } else {
1760  ret = -EINVAL;
1761  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1762  "VPFE_CMD_S_CCDC_RAW_PARAMS not supported\n");
1763  }
1764  break;
1765  default:
1766  ret = -ENOTTY;
1767  }
1768 unlock_out:
1769  mutex_unlock(&vpfe_dev->lock);
1770  return ret;
1771 }
1772 
1773 
1774 /* vpfe capture ioctl operations */
1775 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1776  .vidioc_querycap = vpfe_querycap,
1777  .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1778  .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1779  .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1780  .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1781  .vidioc_enum_input = vpfe_enum_input,
1782  .vidioc_g_input = vpfe_g_input,
1783  .vidioc_s_input = vpfe_s_input,
1784  .vidioc_querystd = vpfe_querystd,
1785  .vidioc_s_std = vpfe_s_std,
1786  .vidioc_g_std = vpfe_g_std,
1787  .vidioc_queryctrl = vpfe_queryctrl,
1788  .vidioc_g_ctrl = vpfe_g_ctrl,
1789  .vidioc_s_ctrl = vpfe_s_ctrl,
1790  .vidioc_reqbufs = vpfe_reqbufs,
1791  .vidioc_querybuf = vpfe_querybuf,
1792  .vidioc_qbuf = vpfe_qbuf,
1793  .vidioc_dqbuf = vpfe_dqbuf,
1794  .vidioc_streamon = vpfe_streamon,
1795  .vidioc_streamoff = vpfe_streamoff,
1796  .vidioc_cropcap = vpfe_cropcap,
1797  .vidioc_g_crop = vpfe_g_crop,
1798  .vidioc_s_crop = vpfe_s_crop,
1799  .vidioc_default = vpfe_param_handler,
1800 };
1801 
1802 static struct vpfe_device *vpfe_initialize(void)
1803 {
1804  struct vpfe_device *vpfe_dev;
1805 
1806  /* Default number of buffers should be 3 */
1807  if ((numbuffers > 0) &&
1808  (numbuffers < config_params.min_numbuffers))
1809  numbuffers = config_params.min_numbuffers;
1810 
1811  /*
1812  * Set buffer size to min buffers size if invalid buffer size is
1813  * given
1814  */
1815  if (bufsize < config_params.min_bufsize)
1816  bufsize = config_params.min_bufsize;
1817 
1818  config_params.numbuffers = numbuffers;
1819 
1820  if (numbuffers)
1821  config_params.device_bufsize = bufsize;
1822 
1823  /* Allocate memory for device objects */
1824  vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1825 
1826  return vpfe_dev;
1827 }
1828 
1829 /*
1830  * vpfe_probe : This function creates device entries by register
1831  * itself to the V4L2 driver and initializes fields of each
1832  * device objects
1833  */
1834 static __devinit int vpfe_probe(struct platform_device *pdev)
1835 {
1836  struct vpfe_subdev_info *sdinfo;
1837  struct vpfe_config *vpfe_cfg;
1838  struct resource *res1;
1839  struct vpfe_device *vpfe_dev;
1840  struct i2c_adapter *i2c_adap;
1841  struct video_device *vfd;
1842  int ret = -ENOMEM, i, j;
1843  int num_subdevs = 0;
1844 
1845  /* Get the pointer to the device object */
1846  vpfe_dev = vpfe_initialize();
1847 
1848  if (!vpfe_dev) {
1849  v4l2_err(pdev->dev.driver,
1850  "Failed to allocate memory for vpfe_dev\n");
1851  return ret;
1852  }
1853 
1854  vpfe_dev->pdev = &pdev->dev;
1855 
1856  if (NULL == pdev->dev.platform_data) {
1857  v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1858  ret = -ENODEV;
1859  goto probe_free_dev_mem;
1860  }
1861 
1862  vpfe_cfg = pdev->dev.platform_data;
1863  vpfe_dev->cfg = vpfe_cfg;
1864  if (NULL == vpfe_cfg->ccdc ||
1865  NULL == vpfe_cfg->card_name ||
1866  NULL == vpfe_cfg->sub_devs) {
1867  v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1868  ret = -ENOENT;
1869  goto probe_free_dev_mem;
1870  }
1871 
1872  /* Allocate memory for ccdc configuration */
1873  ccdc_cfg = kmalloc(sizeof(struct ccdc_config), GFP_KERNEL);
1874  if (NULL == ccdc_cfg) {
1875  v4l2_err(pdev->dev.driver,
1876  "Memory allocation failed for ccdc_cfg\n");
1877  goto probe_free_lock;
1878  }
1879 
1880  mutex_lock(&ccdc_lock);
1881 
1882  strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1883  /* Get VINT0 irq resource */
1884  res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1885  if (!res1) {
1886  v4l2_err(pdev->dev.driver,
1887  "Unable to get interrupt for VINT0\n");
1888  ret = -ENODEV;
1889  goto probe_free_ccdc_cfg_mem;
1890  }
1891  vpfe_dev->ccdc_irq0 = res1->start;
1892 
1893  /* Get VINT1 irq resource */
1894  res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1895  if (!res1) {
1896  v4l2_err(pdev->dev.driver,
1897  "Unable to get interrupt for VINT1\n");
1898  ret = -ENODEV;
1899  goto probe_free_ccdc_cfg_mem;
1900  }
1901  vpfe_dev->ccdc_irq1 = res1->start;
1902 
1903  ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, IRQF_DISABLED,
1904  "vpfe_capture0", vpfe_dev);
1905 
1906  if (0 != ret) {
1907  v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1908  goto probe_free_ccdc_cfg_mem;
1909  }
1910 
1911  /* Allocate memory for video device */
1912  vfd = video_device_alloc();
1913  if (NULL == vfd) {
1914  ret = -ENOMEM;
1915  v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
1916  goto probe_out_release_irq;
1917  }
1918 
1919  /* Initialize field of video device */
1921  vfd->fops = &vpfe_fops;
1922  vfd->ioctl_ops = &vpfe_ioctl_ops;
1923  vfd->tvnorms = 0;
1924  vfd->current_norm = V4L2_STD_PAL;
1925  vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1926  snprintf(vfd->name, sizeof(vfd->name),
1927  "%s_V%d.%d.%d",
1929  (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1930  (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1931  (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1932  /* Set video_dev to the video device */
1933  vpfe_dev->video_dev = vfd;
1934 
1935  ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1936  if (ret) {
1937  v4l2_err(pdev->dev.driver,
1938  "Unable to register v4l2 device.\n");
1939  goto probe_out_video_release;
1940  }
1941  v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1942  spin_lock_init(&vpfe_dev->irqlock);
1943  spin_lock_init(&vpfe_dev->dma_queue_lock);
1944  mutex_init(&vpfe_dev->lock);
1945 
1946  /* Initialize field of the device objects */
1947  vpfe_dev->numbuffers = config_params.numbuffers;
1948 
1949  /* Initialize prio member of device object */
1950  v4l2_prio_init(&vpfe_dev->prio);
1951  /* register video device */
1952  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1953  "trying to register vpfe device.\n");
1954  v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1955  "video_dev=%x\n", (int)&vpfe_dev->video_dev);
1956  vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1957  ret = video_register_device(vpfe_dev->video_dev,
1958  VFL_TYPE_GRABBER, -1);
1959 
1960  if (ret) {
1961  v4l2_err(pdev->dev.driver,
1962  "Unable to register video device.\n");
1963  goto probe_out_v4l2_unregister;
1964  }
1965 
1966  v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1967  /* set the driver data in platform device */
1968  platform_set_drvdata(pdev, vpfe_dev);
1969  /* set driver private data */
1970  video_set_drvdata(vpfe_dev->video_dev, vpfe_dev);
1971  i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1972  num_subdevs = vpfe_cfg->num_subdevs;
1973  vpfe_dev->sd = kmalloc(sizeof(struct v4l2_subdev *) * num_subdevs,
1974  GFP_KERNEL);
1975  if (NULL == vpfe_dev->sd) {
1976  v4l2_err(&vpfe_dev->v4l2_dev,
1977  "unable to allocate memory for subdevice pointers\n");
1978  ret = -ENOMEM;
1979  goto probe_out_video_unregister;
1980  }
1981 
1982  for (i = 0; i < num_subdevs; i++) {
1983  struct v4l2_input *inps;
1984 
1985  sdinfo = &vpfe_cfg->sub_devs[i];
1986 
1987  /* Load up the subdevice */
1988  vpfe_dev->sd[i] =
1989  v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1990  i2c_adap,
1991  &sdinfo->board_info,
1992  NULL);
1993  if (vpfe_dev->sd[i]) {
1994  v4l2_info(&vpfe_dev->v4l2_dev,
1995  "v4l2 sub device %s registered\n",
1996  sdinfo->name);
1997  vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1998  /* update tvnorms from the sub devices */
1999  for (j = 0; j < sdinfo->num_inputs; j++) {
2000  inps = &sdinfo->inputs[j];
2001  vfd->tvnorms |= inps->std;
2002  }
2003  } else {
2004  v4l2_info(&vpfe_dev->v4l2_dev,
2005  "v4l2 sub device %s register fails\n",
2006  sdinfo->name);
2007  goto probe_sd_out;
2008  }
2009  }
2010 
2011  /* set first sub device as current one */
2012  vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
2013 
2014  /* We have at least one sub device to work with */
2015  mutex_unlock(&ccdc_lock);
2016  return 0;
2017 
2018 probe_sd_out:
2019  kfree(vpfe_dev->sd);
2020 probe_out_video_unregister:
2021  video_unregister_device(vpfe_dev->video_dev);
2022 probe_out_v4l2_unregister:
2023  v4l2_device_unregister(&vpfe_dev->v4l2_dev);
2024 probe_out_video_release:
2025  if (!video_is_registered(vpfe_dev->video_dev))
2026  video_device_release(vpfe_dev->video_dev);
2027 probe_out_release_irq:
2028  free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2029 probe_free_ccdc_cfg_mem:
2030  kfree(ccdc_cfg);
2031 probe_free_lock:
2032  mutex_unlock(&ccdc_lock);
2033 probe_free_dev_mem:
2034  kfree(vpfe_dev);
2035  return ret;
2036 }
2037 
2038 /*
2039  * vpfe_remove : It un-register device from V4L2 driver
2040  */
2041 static int __devexit vpfe_remove(struct platform_device *pdev)
2042 {
2043  struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
2044 
2045  v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2046 
2047  free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2048  kfree(vpfe_dev->sd);
2049  v4l2_device_unregister(&vpfe_dev->v4l2_dev);
2050  video_unregister_device(vpfe_dev->video_dev);
2051  kfree(vpfe_dev);
2052  kfree(ccdc_cfg);
2053  return 0;
2054 }
2055 
2056 static int vpfe_suspend(struct device *dev)
2057 {
2058  return 0;
2059 }
2060 
2061 static int vpfe_resume(struct device *dev)
2062 {
2063  return 0;
2064 }
2065 
2066 static const struct dev_pm_ops vpfe_dev_pm_ops = {
2067  .suspend = vpfe_suspend,
2068  .resume = vpfe_resume,
2069 };
2070 
2071 static struct platform_driver vpfe_driver = {
2072  .driver = {
2073  .name = CAPTURE_DRV_NAME,
2074  .owner = THIS_MODULE,
2075  .pm = &vpfe_dev_pm_ops,
2076  },
2077  .probe = vpfe_probe,
2078  .remove = __devexit_p(vpfe_remove),
2079 };
2080 
2081 module_platform_driver(vpfe_driver);