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