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