vpif_capture.c revision 46656afa8a9a410b7939b248712e4e48921929da
1/*
2 * Copyright (C) 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 * TODO : add support for VBI & HBI data service
19 *	  add static buffer allocation
20 */
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
28#include <linux/workqueue.h>
29#include <linux/string.h>
30#include <linux/videodev2.h>
31#include <linux/wait.h>
32#include <linux/time.h>
33#include <linux/i2c.h>
34#include <linux/platform_device.h>
35#include <linux/io.h>
36#include <linux/version.h>
37#include <linux/slab.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-ioctl.h>
40#include <media/v4l2-chip-ident.h>
41
42#include "vpif_capture.h"
43#include "vpif.h"
44
45MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
46MODULE_LICENSE("GPL");
47
48#define vpif_err(fmt, arg...)	v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49#define vpif_dbg(level, debug, fmt, arg...)	\
50		v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51
52static int debug = 1;
53static u32 ch0_numbuffers = 3;
54static u32 ch1_numbuffers = 3;
55static u32 ch0_bufsize = 1920 * 1080 * 2;
56static u32 ch1_bufsize = 720 * 576 * 2;
57
58module_param(debug, int, 0644);
59module_param(ch0_numbuffers, uint, S_IRUGO);
60module_param(ch1_numbuffers, uint, S_IRUGO);
61module_param(ch0_bufsize, uint, S_IRUGO);
62module_param(ch1_bufsize, uint, S_IRUGO);
63
64MODULE_PARM_DESC(debug, "Debug level 0-1");
65MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
66MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
67MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
68MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
69
70static struct vpif_config_params config_params = {
71	.min_numbuffers = 3,
72	.numbuffers[0] = 3,
73	.numbuffers[1] = 3,
74	.min_bufsize[0] = 720 * 480 * 2,
75	.min_bufsize[1] = 720 * 480 * 2,
76	.channel_bufsize[0] = 1920 * 1080 * 2,
77	.channel_bufsize[1] = 720 * 576 * 2,
78};
79
80/* global variables */
81static struct vpif_device vpif_obj = { {NULL} };
82static struct device *vpif_dev;
83
84/**
85 * vpif_uservirt_to_phys : translate user/virtual address to phy address
86 * @virtp: user/virtual address
87 *
88 * This inline function is used to convert user space virtual address to
89 * physical address.
90 */
91static inline u32 vpif_uservirt_to_phys(u32 virtp)
92{
93	unsigned long physp = 0;
94	struct mm_struct *mm = current->mm;
95	struct vm_area_struct *vma;
96
97	vma = find_vma(mm, virtp);
98
99	/* For kernel direct-mapped memory, take the easy way */
100	if (virtp >= PAGE_OFFSET)
101		physp = virt_to_phys((void *)virtp);
102	else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
103		/**
104		 * this will catch, kernel-allocated, mmaped-to-usermode
105		 * addresses
106		 */
107		physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
108	else {
109		/* otherwise, use get_user_pages() for general userland pages */
110		int res, nr_pages = 1;
111			struct page *pages;
112
113		down_read(&current->mm->mmap_sem);
114
115		res = get_user_pages(current, current->mm,
116				     virtp, nr_pages, 1, 0, &pages, NULL);
117		up_read(&current->mm->mmap_sem);
118
119		if (res == nr_pages)
120			physp = __pa(page_address(&pages[0]) +
121				     (virtp & ~PAGE_MASK));
122		else {
123			vpif_err("get_user_pages failed\n");
124			return 0;
125		}
126	}
127	return physp;
128}
129
130/**
131 * buffer_prepare :  callback function for buffer prepare
132 * @q : buffer queue ptr
133 * @vb: ptr to video buffer
134 * @field: field info
135 *
136 * This is the callback function for buffer prepare when videobuf_qbuf()
137 * function is called. The buffer is prepared and user space virtual address
138 * or user address is converted into  physical address
139 */
140static int vpif_buffer_prepare(struct videobuf_queue *q,
141			       struct videobuf_buffer *vb,
142			       enum v4l2_field field)
143{
144	/* Get the file handle object and channel object */
145	struct vpif_fh *fh = q->priv_data;
146	struct channel_obj *ch = fh->channel;
147	struct common_obj *common;
148	unsigned long addr;
149
150
151	vpif_dbg(2, debug, "vpif_buffer_prepare\n");
152
153	common = &ch->common[VPIF_VIDEO_INDEX];
154
155	/* If buffer is not initialized, initialize it */
156	if (VIDEOBUF_NEEDS_INIT == vb->state) {
157		vb->width = common->width;
158		vb->height = common->height;
159		vb->size = vb->width * vb->height;
160		vb->field = field;
161	}
162	vb->state = VIDEOBUF_PREPARED;
163	/**
164	 * if user pointer memory mechanism is used, get the physical
165	 * address of the buffer
166	 */
167	if (V4L2_MEMORY_USERPTR == common->memory) {
168		if (0 == vb->baddr) {
169			vpif_dbg(1, debug, "buffer address is 0\n");
170			return -EINVAL;
171
172		}
173		vb->boff = vpif_uservirt_to_phys(vb->baddr);
174		if (!IS_ALIGNED(vb->boff, 8))
175			goto exit;
176	}
177
178	addr = vb->boff;
179	if (q->streaming) {
180		if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
181		    !IS_ALIGNED((addr + common->ybtm_off), 8) ||
182		    !IS_ALIGNED((addr + common->ctop_off), 8) ||
183		    !IS_ALIGNED((addr + common->cbtm_off), 8))
184			goto exit;
185	}
186	return 0;
187exit:
188	vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
189	return -EINVAL;
190}
191
192/**
193 * vpif_buffer_setup : Callback function for buffer setup.
194 * @q: buffer queue ptr
195 * @count: number of buffers
196 * @size: size of the buffer
197 *
198 * This callback function is called when reqbuf() is called to adjust
199 * the buffer count and buffer size
200 */
201static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
202			     unsigned int *size)
203{
204	/* Get the file handle object and channel object */
205	struct vpif_fh *fh = q->priv_data;
206	struct channel_obj *ch = fh->channel;
207	struct common_obj *common;
208
209	common = &ch->common[VPIF_VIDEO_INDEX];
210
211	vpif_dbg(2, debug, "vpif_buffer_setup\n");
212
213	/* If memory type is not mmap, return */
214	if (V4L2_MEMORY_MMAP != common->memory)
215		return 0;
216
217	/* Calculate the size of the buffer */
218	*size = config_params.channel_bufsize[ch->channel_id];
219
220	if (*count < config_params.min_numbuffers)
221		*count = config_params.min_numbuffers;
222	return 0;
223}
224
225/**
226 * vpif_buffer_queue : Callback function to add buffer to DMA queue
227 * @q: ptr to videobuf_queue
228 * @vb: ptr to videobuf_buffer
229 */
230static void vpif_buffer_queue(struct videobuf_queue *q,
231			      struct videobuf_buffer *vb)
232{
233	/* Get the file handle object and channel object */
234	struct vpif_fh *fh = q->priv_data;
235	struct channel_obj *ch = fh->channel;
236	struct common_obj *common;
237
238	common = &ch->common[VPIF_VIDEO_INDEX];
239
240	vpif_dbg(2, debug, "vpif_buffer_queue\n");
241
242	/* add the buffer to the DMA queue */
243	list_add_tail(&vb->queue, &common->dma_queue);
244	/* Change state of the buffer */
245	vb->state = VIDEOBUF_QUEUED;
246}
247
248/**
249 * vpif_buffer_release : Callback function to free buffer
250 * @q: buffer queue ptr
251 * @vb: ptr to video buffer
252 *
253 * This function is called from the videobuf layer to free memory
254 * allocated to  the buffers
255 */
256static void vpif_buffer_release(struct videobuf_queue *q,
257				struct videobuf_buffer *vb)
258{
259	/* Get the file handle object and channel object */
260	struct vpif_fh *fh = q->priv_data;
261	struct channel_obj *ch = fh->channel;
262	struct common_obj *common;
263
264	common = &ch->common[VPIF_VIDEO_INDEX];
265
266	videobuf_dma_contig_free(q, vb);
267	vb->state = VIDEOBUF_NEEDS_INIT;
268}
269
270static struct videobuf_queue_ops video_qops = {
271	.buf_setup = vpif_buffer_setup,
272	.buf_prepare = vpif_buffer_prepare,
273	.buf_queue = vpif_buffer_queue,
274	.buf_release = vpif_buffer_release,
275};
276
277static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
278	{ {1, 1} };
279
280/**
281 * vpif_process_buffer_complete: process a completed buffer
282 * @common: ptr to common channel object
283 *
284 * This function time stamp the buffer and mark it as DONE. It also
285 * wake up any process waiting on the QUEUE and set the next buffer
286 * as current
287 */
288static void vpif_process_buffer_complete(struct common_obj *common)
289{
290	do_gettimeofday(&common->cur_frm->ts);
291	common->cur_frm->state = VIDEOBUF_DONE;
292	wake_up_interruptible(&common->cur_frm->done);
293	/* Make curFrm pointing to nextFrm */
294	common->cur_frm = common->next_frm;
295}
296
297/**
298 * vpif_schedule_next_buffer: set next buffer address for capture
299 * @common : ptr to common channel object
300 *
301 * This function will get next buffer from the dma queue and
302 * set the buffer address in the vpif register for capture.
303 * the buffer is marked active
304 */
305static void vpif_schedule_next_buffer(struct common_obj *common)
306{
307	unsigned long addr = 0;
308
309	common->next_frm = list_entry(common->dma_queue.next,
310				     struct videobuf_buffer, queue);
311	/* Remove that buffer from the buffer queue */
312	list_del(&common->next_frm->queue);
313	common->next_frm->state = VIDEOBUF_ACTIVE;
314	if (V4L2_MEMORY_USERPTR == common->memory)
315		addr = common->next_frm->boff;
316	else
317		addr = videobuf_to_dma_contig(common->next_frm);
318
319	/* Set top and bottom field addresses in VPIF registers */
320	common->set_addr(addr + common->ytop_off,
321			 addr + common->ybtm_off,
322			 addr + common->ctop_off,
323			 addr + common->cbtm_off);
324}
325
326/**
327 * vpif_channel_isr : ISR handler for vpif capture
328 * @irq: irq number
329 * @dev_id: dev_id ptr
330 *
331 * It changes status of the captured buffer, takes next buffer from the queue
332 * and sets its address in VPIF registers
333 */
334static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
335{
336	struct vpif_device *dev = &vpif_obj;
337	struct common_obj *common;
338	struct channel_obj *ch;
339	enum v4l2_field field;
340	int channel_id = 0;
341	int fid = -1, i;
342
343	channel_id = *(int *)(dev_id);
344	ch = dev->dev[channel_id];
345
346	field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
347
348	for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
349		common = &ch->common[i];
350		/* skip If streaming is not started in this channel */
351		if (0 == common->started)
352			continue;
353
354		/* Check the field format */
355		if (1 == ch->vpifparams.std_info.frm_fmt) {
356			/* Progressive mode */
357			if (list_empty(&common->dma_queue))
358				continue;
359
360			if (!channel_first_int[i][channel_id])
361				vpif_process_buffer_complete(common);
362
363			channel_first_int[i][channel_id] = 0;
364
365			vpif_schedule_next_buffer(common);
366
367
368			channel_first_int[i][channel_id] = 0;
369		} else {
370			/**
371			 * Interlaced mode. If it is first interrupt, ignore
372			 * it
373			 */
374			if (channel_first_int[i][channel_id]) {
375				channel_first_int[i][channel_id] = 0;
376				continue;
377			}
378			if (0 == i) {
379				ch->field_id ^= 1;
380				/* Get field id from VPIF registers */
381				fid = vpif_channel_getfid(ch->channel_id);
382				if (fid != ch->field_id) {
383					/**
384					 * If field id does not match stored
385					 * field id, make them in sync
386					 */
387					if (0 == fid)
388						ch->field_id = fid;
389					return IRQ_HANDLED;
390				}
391			}
392			/* device field id and local field id are in sync */
393			if (0 == fid) {
394				/* this is even field */
395				if (common->cur_frm == common->next_frm)
396					continue;
397
398				/* mark the current buffer as done */
399				vpif_process_buffer_complete(common);
400			} else if (1 == fid) {
401				/* odd field */
402				if (list_empty(&common->dma_queue) ||
403				    (common->cur_frm != common->next_frm))
404					continue;
405
406				vpif_schedule_next_buffer(common);
407			}
408		}
409	}
410	return IRQ_HANDLED;
411}
412
413/**
414 * vpif_update_std_info() - update standard related info
415 * @ch: ptr to channel object
416 *
417 * For a given standard selected by application, update values
418 * in the device data structures
419 */
420static int vpif_update_std_info(struct channel_obj *ch)
421{
422	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
423	struct vpif_params *vpifparams = &ch->vpifparams;
424	const struct vpif_channel_config_params *config;
425	struct vpif_channel_config_params *std_info = &vpifparams->std_info;
426	struct video_obj *vid_ch = &ch->video;
427	int index;
428
429	vpif_dbg(2, debug, "vpif_update_std_info\n");
430
431	for (index = 0; index < vpif_ch_params_count; index++) {
432		config = &ch_params[index];
433		if (config->hd_sd == 0) {
434			vpif_dbg(2, debug, "SD format\n");
435			if (config->stdid & vid_ch->stdid) {
436				memcpy(std_info, config, sizeof(*config));
437				break;
438			}
439		} else {
440			vpif_dbg(2, debug, "HD format\n");
441			if (config->dv_preset == vid_ch->dv_preset) {
442				memcpy(std_info, config, sizeof(*config));
443				break;
444			}
445		}
446	}
447
448	/* standard not found */
449	if (index == vpif_ch_params_count)
450		return -EINVAL;
451
452	common->fmt.fmt.pix.width = std_info->width;
453	common->width = std_info->width;
454	common->fmt.fmt.pix.height = std_info->height;
455	common->height = std_info->height;
456	common->fmt.fmt.pix.bytesperline = std_info->width;
457	vpifparams->video_params.hpitch = std_info->width;
458	vpifparams->video_params.storage_mode = std_info->frm_fmt;
459
460	return 0;
461}
462
463/**
464 * vpif_calculate_offsets : This function calculates buffers offsets
465 * @ch : ptr to channel object
466 *
467 * This function calculates buffer offsets for Y and C in the top and
468 * bottom field
469 */
470static void vpif_calculate_offsets(struct channel_obj *ch)
471{
472	unsigned int hpitch, vpitch, sizeimage;
473	struct video_obj *vid_ch = &(ch->video);
474	struct vpif_params *vpifparams = &ch->vpifparams;
475	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
476	enum v4l2_field field = common->fmt.fmt.pix.field;
477
478	vpif_dbg(2, debug, "vpif_calculate_offsets\n");
479
480	if (V4L2_FIELD_ANY == field) {
481		if (vpifparams->std_info.frm_fmt)
482			vid_ch->buf_field = V4L2_FIELD_NONE;
483		else
484			vid_ch->buf_field = V4L2_FIELD_INTERLACED;
485	} else
486		vid_ch->buf_field = common->fmt.fmt.pix.field;
487
488	if (V4L2_MEMORY_USERPTR == common->memory)
489		sizeimage = common->fmt.fmt.pix.sizeimage;
490	else
491		sizeimage = config_params.channel_bufsize[ch->channel_id];
492
493	hpitch = common->fmt.fmt.pix.bytesperline;
494	vpitch = sizeimage / (hpitch * 2);
495
496	if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
497	    (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
498		/* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
499		common->ytop_off = 0;
500		common->ybtm_off = hpitch;
501		common->ctop_off = sizeimage / 2;
502		common->cbtm_off = sizeimage / 2 + hpitch;
503	} else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
504		/* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
505		common->ytop_off = 0;
506		common->ybtm_off = sizeimage / 4;
507		common->ctop_off = sizeimage / 2;
508		common->cbtm_off = common->ctop_off + sizeimage / 4;
509	} else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
510		/* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
511		common->ybtm_off = 0;
512		common->ytop_off = sizeimage / 4;
513		common->cbtm_off = sizeimage / 2;
514		common->ctop_off = common->cbtm_off + sizeimage / 4;
515	}
516	if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
517	    (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
518		vpifparams->video_params.storage_mode = 1;
519	else
520		vpifparams->video_params.storage_mode = 0;
521
522	if (1 == vpifparams->std_info.frm_fmt)
523		vpifparams->video_params.hpitch =
524		    common->fmt.fmt.pix.bytesperline;
525	else {
526		if ((field == V4L2_FIELD_ANY)
527		    || (field == V4L2_FIELD_INTERLACED))
528			vpifparams->video_params.hpitch =
529			    common->fmt.fmt.pix.bytesperline * 2;
530		else
531			vpifparams->video_params.hpitch =
532			    common->fmt.fmt.pix.bytesperline;
533	}
534
535	ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
536}
537
538/**
539 * vpif_config_format: configure default frame format in the device
540 * ch : ptr to channel object
541 */
542static void vpif_config_format(struct channel_obj *ch)
543{
544	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
545
546	vpif_dbg(2, debug, "vpif_config_format\n");
547
548	common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
549	if (config_params.numbuffers[ch->channel_id] == 0)
550		common->memory = V4L2_MEMORY_USERPTR;
551	else
552		common->memory = V4L2_MEMORY_MMAP;
553
554	common->fmt.fmt.pix.sizeimage
555	    = config_params.channel_bufsize[ch->channel_id];
556
557	if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
558		common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
559	else
560		common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
561	common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
562}
563
564/**
565 * vpif_get_default_field() - Get default field type based on interface
566 * @vpif_params - ptr to vpif params
567 */
568static inline enum v4l2_field vpif_get_default_field(
569				struct vpif_interface *iface)
570{
571	return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
572						V4L2_FIELD_INTERLACED;
573}
574
575/**
576 * vpif_check_format()  - check given pixel format for compatibility
577 * @ch - channel  ptr
578 * @pixfmt - Given pixel format
579 * @update - update the values as per hardware requirement
580 *
581 * Check the application pixel format for S_FMT and update the input
582 * values as per hardware limits for TRY_FMT. The default pixel and
583 * field format is selected based on interface type.
584 */
585static int vpif_check_format(struct channel_obj *ch,
586			     struct v4l2_pix_format *pixfmt,
587			     int update)
588{
589	struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
590	struct vpif_params *vpif_params = &ch->vpifparams;
591	enum v4l2_field field = pixfmt->field;
592	u32 sizeimage, hpitch, vpitch;
593	int ret = -EINVAL;
594
595	vpif_dbg(2, debug, "vpif_check_format\n");
596	/**
597	 * first check for the pixel format. If if_type is Raw bayer,
598	 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
599	 * V4L2_PIX_FMT_YUV422P is supported
600	 */
601	if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
602		if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
603			if (!update) {
604				vpif_dbg(2, debug, "invalid pix format\n");
605				goto exit;
606			}
607			pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
608		}
609	} else {
610		if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
611			if (!update) {
612				vpif_dbg(2, debug, "invalid pixel format\n");
613				goto exit;
614			}
615			pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
616		}
617	}
618
619	if (!(VPIF_VALID_FIELD(field))) {
620		if (!update) {
621			vpif_dbg(2, debug, "invalid field format\n");
622			goto exit;
623		}
624		/**
625		 * By default use FIELD_NONE for RAW Bayer capture
626		 * and FIELD_INTERLACED for other interfaces
627		 */
628		field = vpif_get_default_field(&vpif_params->iface);
629	} else if (field == V4L2_FIELD_ANY)
630		/* unsupported field. Use default */
631		field = vpif_get_default_field(&vpif_params->iface);
632
633	/* validate the hpitch */
634	hpitch = pixfmt->bytesperline;
635	if (hpitch < vpif_params->std_info.width) {
636		if (!update) {
637			vpif_dbg(2, debug, "invalid hpitch\n");
638			goto exit;
639		}
640		hpitch = vpif_params->std_info.width;
641	}
642
643	if (V4L2_MEMORY_USERPTR == common->memory)
644		sizeimage = pixfmt->sizeimage;
645	else
646		sizeimage = config_params.channel_bufsize[ch->channel_id];
647
648	vpitch = sizeimage / (hpitch * 2);
649
650	/* validate the vpitch */
651	if (vpitch < vpif_params->std_info.height) {
652		if (!update) {
653			vpif_dbg(2, debug, "Invalid vpitch\n");
654			goto exit;
655		}
656		vpitch = vpif_params->std_info.height;
657	}
658
659	/* Check for 8 byte alignment */
660	if (!ALIGN(hpitch, 8)) {
661		if (!update) {
662			vpif_dbg(2, debug, "invalid pitch alignment\n");
663			goto exit;
664		}
665		/* adjust to next 8 byte boundary */
666		hpitch = (((hpitch + 7) / 8) * 8);
667	}
668	/* if update is set, modify the bytesperline and sizeimage */
669	if (update) {
670		pixfmt->bytesperline = hpitch;
671		pixfmt->sizeimage = hpitch * vpitch * 2;
672	}
673	/**
674	 * Image width and height is always based on current standard width and
675	 * height
676	 */
677	pixfmt->width = common->fmt.fmt.pix.width;
678	pixfmt->height = common->fmt.fmt.pix.height;
679	return 0;
680exit:
681	return ret;
682}
683
684/**
685 * vpif_config_addr() - function to configure buffer address in vpif
686 * @ch - channel ptr
687 * @muxmode - channel mux mode
688 */
689static void vpif_config_addr(struct channel_obj *ch, int muxmode)
690{
691	struct common_obj *common;
692
693	vpif_dbg(2, debug, "vpif_config_addr\n");
694
695	common = &(ch->common[VPIF_VIDEO_INDEX]);
696
697	if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
698		common->set_addr = ch1_set_videobuf_addr;
699	else if (2 == muxmode)
700		common->set_addr = ch0_set_videobuf_addr_yc_nmux;
701	else
702		common->set_addr = ch0_set_videobuf_addr;
703}
704
705/**
706 * vpfe_mmap : It is used to map kernel space buffers into user spaces
707 * @filep: file pointer
708 * @vma: ptr to vm_area_struct
709 */
710static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
711{
712	/* Get the channel object and file handle object */
713	struct vpif_fh *fh = filep->private_data;
714	struct channel_obj *ch = fh->channel;
715	struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
716
717	vpif_dbg(2, debug, "vpif_mmap\n");
718
719	return videobuf_mmap_mapper(&common->buffer_queue, vma);
720}
721
722/**
723 * vpif_poll: It is used for select/poll system call
724 * @filep: file pointer
725 * @wait: poll table to wait
726 */
727static unsigned int vpif_poll(struct file *filep, poll_table * wait)
728{
729	struct vpif_fh *fh = filep->private_data;
730	struct channel_obj *channel = fh->channel;
731	struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
732
733	vpif_dbg(2, debug, "vpif_poll\n");
734
735	if (common->started)
736		return videobuf_poll_stream(filep, &common->buffer_queue, wait);
737	return 0;
738}
739
740/**
741 * vpif_open : vpif open handler
742 * @filep: file ptr
743 *
744 * It creates object of file handle structure and stores it in private_data
745 * member of filepointer
746 */
747static int vpif_open(struct file *filep)
748{
749	struct vpif_capture_config *config = vpif_dev->platform_data;
750	struct video_device *vdev = video_devdata(filep);
751	struct common_obj *common;
752	struct video_obj *vid_ch;
753	struct channel_obj *ch;
754	struct vpif_fh *fh;
755	int i;
756
757	vpif_dbg(2, debug, "vpif_open\n");
758
759	ch = video_get_drvdata(vdev);
760
761	vid_ch = &ch->video;
762	common = &ch->common[VPIF_VIDEO_INDEX];
763
764	if (NULL == ch->curr_subdev_info) {
765		/**
766		 * search through the sub device to see a registered
767		 * sub device and make it as current sub device
768		 */
769		for (i = 0; i < config->subdev_count; i++) {
770			if (vpif_obj.sd[i]) {
771				/* the sub device is registered */
772				ch->curr_subdev_info = &config->subdev_info[i];
773				/* make first input as the current input */
774				vid_ch->input_idx = 0;
775				break;
776			}
777		}
778		if (i == config->subdev_count) {
779			vpif_err("No sub device registered\n");
780			return -ENOENT;
781		}
782	}
783
784	/* Allocate memory for the file handle object */
785	fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
786	if (NULL == fh) {
787		vpif_err("unable to allocate memory for file handle object\n");
788		return -ENOMEM;
789	}
790
791	/* store pointer to fh in private_data member of filep */
792	filep->private_data = fh;
793	fh->channel = ch;
794	fh->initialized = 0;
795	/* If decoder is not initialized. initialize it */
796	if (!ch->initialized) {
797		fh->initialized = 1;
798		ch->initialized = 1;
799		memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
800	}
801	/* Increment channel usrs counter */
802	ch->usrs++;
803	/* Set io_allowed member to false */
804	fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
805	/* Initialize priority of this instance to default priority */
806	fh->prio = V4L2_PRIORITY_UNSET;
807	v4l2_prio_open(&ch->prio, &fh->prio);
808	return 0;
809}
810
811/**
812 * vpif_release : function to clean up file close
813 * @filep: file pointer
814 *
815 * This function deletes buffer queue, frees the buffers and the vpfe file
816 * handle
817 */
818static int vpif_release(struct file *filep)
819{
820	struct vpif_fh *fh = filep->private_data;
821	struct channel_obj *ch = fh->channel;
822	struct common_obj *common;
823
824	vpif_dbg(2, debug, "vpif_release\n");
825
826	common = &ch->common[VPIF_VIDEO_INDEX];
827
828	/* if this instance is doing IO */
829	if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
830		/* Reset io_usrs member of channel object */
831		common->io_usrs = 0;
832		/* Disable channel as per its device type and channel id */
833		if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
834			enable_channel0(0);
835			channel0_intr_enable(0);
836		}
837		if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
838		    (2 == common->started)) {
839			enable_channel1(0);
840			channel1_intr_enable(0);
841		}
842		common->started = 0;
843		/* Free buffers allocated */
844		videobuf_queue_cancel(&common->buffer_queue);
845		videobuf_mmap_free(&common->buffer_queue);
846	}
847
848	/* Decrement channel usrs counter */
849	ch->usrs--;
850
851	/* Close the priority */
852	v4l2_prio_close(&ch->prio, fh->prio);
853
854	if (fh->initialized)
855		ch->initialized = 0;
856
857	filep->private_data = NULL;
858	kfree(fh);
859	return 0;
860}
861
862/**
863 * vpif_reqbufs() - request buffer handler
864 * @file: file ptr
865 * @priv: file handle
866 * @reqbuf: request buffer structure ptr
867 */
868static int vpif_reqbufs(struct file *file, void *priv,
869			struct v4l2_requestbuffers *reqbuf)
870{
871	struct vpif_fh *fh = priv;
872	struct channel_obj *ch = fh->channel;
873	struct common_obj *common;
874	u8 index = 0;
875
876	vpif_dbg(2, debug, "vpif_reqbufs\n");
877
878	/**
879	 * This file handle has not initialized the channel,
880	 * It is not allowed to do settings
881	 */
882	if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
883	    || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
884		if (!fh->initialized) {
885			vpif_dbg(1, debug, "Channel Busy\n");
886			return -EBUSY;
887		}
888	}
889
890	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
891		return -EINVAL;
892
893	index = VPIF_VIDEO_INDEX;
894
895	common = &ch->common[index];
896
897	if (0 != common->io_usrs)
898		return -EBUSY;
899
900	/* Initialize videobuf queue as per the buffer type */
901	videobuf_queue_dma_contig_init(&common->buffer_queue,
902					    &video_qops, NULL,
903					    &common->irqlock,
904					    reqbuf->type,
905					    common->fmt.fmt.pix.field,
906					    sizeof(struct videobuf_buffer), fh,
907					    &common->lock);
908
909	/* Set io allowed member of file handle to TRUE */
910	fh->io_allowed[index] = 1;
911	/* Increment io usrs member of channel object to 1 */
912	common->io_usrs = 1;
913	/* Store type of memory requested in channel object */
914	common->memory = reqbuf->memory;
915	INIT_LIST_HEAD(&common->dma_queue);
916
917	/* Allocate buffers */
918	return videobuf_reqbufs(&common->buffer_queue, reqbuf);
919}
920
921/**
922 * vpif_querybuf() - query buffer handler
923 * @file: file ptr
924 * @priv: file handle
925 * @buf: v4l2 buffer structure ptr
926 */
927static int vpif_querybuf(struct file *file, void *priv,
928				struct v4l2_buffer *buf)
929{
930	struct vpif_fh *fh = priv;
931	struct channel_obj *ch = fh->channel;
932	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
933
934	vpif_dbg(2, debug, "vpif_querybuf\n");
935
936	if (common->fmt.type != buf->type)
937		return -EINVAL;
938
939	if (common->memory != V4L2_MEMORY_MMAP) {
940		vpif_dbg(1, debug, "Invalid memory\n");
941		return -EINVAL;
942	}
943
944	return videobuf_querybuf(&common->buffer_queue, buf);
945}
946
947/**
948 * vpif_qbuf() - query buffer handler
949 * @file: file ptr
950 * @priv: file handle
951 * @buf: v4l2 buffer structure ptr
952 */
953static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
954{
955
956	struct vpif_fh *fh = priv;
957	struct channel_obj *ch = fh->channel;
958	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
959	struct v4l2_buffer tbuf = *buf;
960	struct videobuf_buffer *buf1;
961	unsigned long addr = 0;
962	unsigned long flags;
963	int ret = 0;
964
965	vpif_dbg(2, debug, "vpif_qbuf\n");
966
967	if (common->fmt.type != tbuf.type) {
968		vpif_err("invalid buffer type\n");
969		return -EINVAL;
970	}
971
972	if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
973		vpif_err("fh io not allowed \n");
974		return -EACCES;
975	}
976
977	if (!(list_empty(&common->dma_queue)) ||
978	    (common->cur_frm != common->next_frm) ||
979	    !common->started ||
980	    (common->started && (0 == ch->field_id)))
981		return videobuf_qbuf(&common->buffer_queue, buf);
982
983	/* bufferqueue is empty store buffer address in VPIF registers */
984	mutex_lock(&common->buffer_queue.vb_lock);
985	buf1 = common->buffer_queue.bufs[tbuf.index];
986
987	if ((buf1->state == VIDEOBUF_QUEUED) ||
988	    (buf1->state == VIDEOBUF_ACTIVE)) {
989		vpif_err("invalid state\n");
990		goto qbuf_exit;
991	}
992
993	switch (buf1->memory) {
994	case V4L2_MEMORY_MMAP:
995		if (buf1->baddr == 0)
996			goto qbuf_exit;
997		break;
998
999	case V4L2_MEMORY_USERPTR:
1000		if (tbuf.length < buf1->bsize)
1001			goto qbuf_exit;
1002
1003		if ((VIDEOBUF_NEEDS_INIT != buf1->state)
1004			    && (buf1->baddr != tbuf.m.userptr)) {
1005			vpif_buffer_release(&common->buffer_queue, buf1);
1006			buf1->baddr = tbuf.m.userptr;
1007		}
1008		break;
1009
1010	default:
1011		goto qbuf_exit;
1012	}
1013
1014	local_irq_save(flags);
1015	ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
1016					common->buffer_queue.field);
1017	if (ret < 0) {
1018		local_irq_restore(flags);
1019		goto qbuf_exit;
1020	}
1021
1022	buf1->state = VIDEOBUF_ACTIVE;
1023
1024	if (V4L2_MEMORY_USERPTR == common->memory)
1025		addr = buf1->boff;
1026	else
1027		addr = videobuf_to_dma_contig(buf1);
1028
1029	common->next_frm = buf1;
1030	common->set_addr(addr + common->ytop_off,
1031			 addr + common->ybtm_off,
1032			 addr + common->ctop_off,
1033			 addr + common->cbtm_off);
1034
1035	local_irq_restore(flags);
1036	list_add_tail(&buf1->stream, &common->buffer_queue.stream);
1037	mutex_unlock(&common->buffer_queue.vb_lock);
1038	return 0;
1039
1040qbuf_exit:
1041	mutex_unlock(&common->buffer_queue.vb_lock);
1042	return -EINVAL;
1043}
1044
1045/**
1046 * vpif_dqbuf() - query buffer handler
1047 * @file: file ptr
1048 * @priv: file handle
1049 * @buf: v4l2 buffer structure ptr
1050 */
1051static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1052{
1053	struct vpif_fh *fh = priv;
1054	struct channel_obj *ch = fh->channel;
1055	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1056
1057	vpif_dbg(2, debug, "vpif_dqbuf\n");
1058
1059	return videobuf_dqbuf(&common->buffer_queue, buf,
1060					file->f_flags & O_NONBLOCK);
1061}
1062
1063/**
1064 * vpif_streamon() - streamon handler
1065 * @file: file ptr
1066 * @priv: file handle
1067 * @buftype: v4l2 buffer type
1068 */
1069static int vpif_streamon(struct file *file, void *priv,
1070				enum v4l2_buf_type buftype)
1071{
1072
1073	struct vpif_capture_config *config = vpif_dev->platform_data;
1074	struct vpif_fh *fh = priv;
1075	struct channel_obj *ch = fh->channel;
1076	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1077	struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1078	struct vpif_params *vpif;
1079	unsigned long addr = 0;
1080	int ret = 0;
1081
1082	vpif_dbg(2, debug, "vpif_streamon\n");
1083
1084	vpif = &ch->vpifparams;
1085
1086	if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1087		vpif_dbg(1, debug, "buffer type not supported\n");
1088		return -EINVAL;
1089	}
1090
1091	/* If file handle is not allowed IO, return error */
1092	if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1093		vpif_dbg(1, debug, "io not allowed\n");
1094		return -EACCES;
1095	}
1096
1097	/* If Streaming is already started, return error */
1098	if (common->started) {
1099		vpif_dbg(1, debug, "channel->started\n");
1100		return -EBUSY;
1101	}
1102
1103	if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1104	    oth_ch->common[VPIF_VIDEO_INDEX].started &&
1105	    vpif->std_info.ycmux_mode == 0) ||
1106	   ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1107	    (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1108		vpif_dbg(1, debug, "other channel is being used\n");
1109		return -EBUSY;
1110	}
1111
1112	ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1113	if (ret)
1114		return ret;
1115
1116	/* Enable streamon on the sub device */
1117	ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1118				s_stream, 1);
1119
1120	if (ret && (ret != -ENOIOCTLCMD)) {
1121		vpif_dbg(1, debug, "stream on failed in subdev\n");
1122		return ret;
1123	}
1124
1125	/* Call videobuf_streamon to start streaming in videobuf */
1126	ret = videobuf_streamon(&common->buffer_queue);
1127	if (ret) {
1128		vpif_dbg(1, debug, "videobuf_streamon\n");
1129		return ret;
1130	}
1131
1132	/* If buffer queue is empty, return error */
1133	if (list_empty(&common->dma_queue)) {
1134		vpif_dbg(1, debug, "buffer queue is empty\n");
1135		ret = -EIO;
1136		goto exit;
1137	}
1138
1139	/* Get the next frame from the buffer queue */
1140	common->cur_frm = list_entry(common->dma_queue.next,
1141				    struct videobuf_buffer, queue);
1142	common->next_frm = common->cur_frm;
1143
1144	/* Remove buffer from the buffer queue */
1145	list_del(&common->cur_frm->queue);
1146	/* Mark state of the current frame to active */
1147	common->cur_frm->state = VIDEOBUF_ACTIVE;
1148	/* Initialize field_id and started member */
1149	ch->field_id = 0;
1150	common->started = 1;
1151
1152	if (V4L2_MEMORY_USERPTR == common->memory)
1153		addr = common->cur_frm->boff;
1154	else
1155		addr = videobuf_to_dma_contig(common->cur_frm);
1156
1157	/* Calculate the offset for Y and C data in the buffer */
1158	vpif_calculate_offsets(ch);
1159
1160	if ((vpif->std_info.frm_fmt &&
1161	    ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
1162	     (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
1163	    (!vpif->std_info.frm_fmt &&
1164	     (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1165		vpif_dbg(1, debug, "conflict in field format and std format\n");
1166		ret = -EINVAL;
1167		goto exit;
1168	}
1169
1170	/* configure 1 or 2 channel mode */
1171	ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1172
1173	if (ret < 0) {
1174		vpif_dbg(1, debug, "can't set vpif channel mode\n");
1175		goto exit;
1176	}
1177
1178	/* Call vpif_set_params function to set the parameters and addresses */
1179	ret = vpif_set_video_params(vpif, ch->channel_id);
1180
1181	if (ret < 0) {
1182		vpif_dbg(1, debug, "can't set video params\n");
1183		goto exit;
1184	}
1185
1186	common->started = ret;
1187	vpif_config_addr(ch, ret);
1188
1189	common->set_addr(addr + common->ytop_off,
1190			 addr + common->ybtm_off,
1191			 addr + common->ctop_off,
1192			 addr + common->cbtm_off);
1193
1194	/**
1195	 * Set interrupt for both the fields in VPIF Register enable channel in
1196	 * VPIF register
1197	 */
1198	if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1199		channel0_intr_assert();
1200		channel0_intr_enable(1);
1201		enable_channel0(1);
1202	}
1203	if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
1204	    (common->started == 2)) {
1205		channel1_intr_assert();
1206		channel1_intr_enable(1);
1207		enable_channel1(1);
1208	}
1209	channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1210	return ret;
1211
1212exit:
1213	videobuf_streamoff(&common->buffer_queue);
1214	return ret;
1215}
1216
1217/**
1218 * vpif_streamoff() - streamoff handler
1219 * @file: file ptr
1220 * @priv: file handle
1221 * @buftype: v4l2 buffer type
1222 */
1223static int vpif_streamoff(struct file *file, void *priv,
1224				enum v4l2_buf_type buftype)
1225{
1226
1227	struct vpif_fh *fh = priv;
1228	struct channel_obj *ch = fh->channel;
1229	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1230	int ret;
1231
1232	vpif_dbg(2, debug, "vpif_streamoff\n");
1233
1234	if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1235		vpif_dbg(1, debug, "buffer type not supported\n");
1236		return -EINVAL;
1237	}
1238
1239	/* If io is allowed for this file handle, return error */
1240	if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1241		vpif_dbg(1, debug, "io not allowed\n");
1242		return -EACCES;
1243	}
1244
1245	/* If streaming is not started, return error */
1246	if (!common->started) {
1247		vpif_dbg(1, debug, "channel->started\n");
1248		return -EINVAL;
1249	}
1250
1251	/* disable channel */
1252	if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1253		enable_channel0(0);
1254		channel0_intr_enable(0);
1255	} else {
1256		enable_channel1(0);
1257		channel1_intr_enable(0);
1258	}
1259
1260	common->started = 0;
1261
1262	ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1263				s_stream, 0);
1264
1265	if (ret && (ret != -ENOIOCTLCMD))
1266		vpif_dbg(1, debug, "stream off failed in subdev\n");
1267
1268	return videobuf_streamoff(&common->buffer_queue);
1269}
1270
1271/**
1272 * vpif_map_sub_device_to_input() - Maps sub device to input
1273 * @ch - ptr to channel
1274 * @config - ptr to capture configuration
1275 * @input_index - Given input index from application
1276 * @sub_device_index - index into sd table
1277 *
1278 * lookup the sub device information for a given input index.
1279 * we report all the inputs to application. inputs table also
1280 * has sub device name for the each input
1281 */
1282static struct vpif_subdev_info *vpif_map_sub_device_to_input(
1283				struct channel_obj *ch,
1284				struct vpif_capture_config *vpif_cfg,
1285				int input_index,
1286				int *sub_device_index)
1287{
1288	struct vpif_capture_chan_config *chan_cfg;
1289	struct vpif_subdev_info *subdev_info = NULL;
1290	const char *subdev_name = NULL;
1291	int i;
1292
1293	vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1294
1295	chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1296
1297	/**
1298	 * search through the inputs to find the sub device supporting
1299	 * the input
1300	 */
1301	for (i = 0; i < chan_cfg->input_count; i++) {
1302		/* For each sub device, loop through input */
1303		if (i == input_index) {
1304			subdev_name = chan_cfg->inputs[i].subdev_name;
1305			break;
1306		}
1307	}
1308
1309	/* if reached maximum. return null */
1310	if (i == chan_cfg->input_count || (NULL == subdev_name))
1311		return subdev_info;
1312
1313	/* loop through the sub device list to get the sub device info */
1314	for (i = 0; i < vpif_cfg->subdev_count; i++) {
1315		subdev_info = &vpif_cfg->subdev_info[i];
1316		if (!strcmp(subdev_info->name, subdev_name))
1317			break;
1318	}
1319
1320	if (i == vpif_cfg->subdev_count)
1321		return subdev_info;
1322
1323	/* check if the sub device is registered */
1324	if (NULL == vpif_obj.sd[i])
1325		return NULL;
1326
1327	*sub_device_index = i;
1328	return subdev_info;
1329}
1330
1331/**
1332 * vpif_querystd() - querystd handler
1333 * @file: file ptr
1334 * @priv: file handle
1335 * @std_id: ptr to std id
1336 *
1337 * This function is called to detect standard at the selected input
1338 */
1339static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1340{
1341	struct vpif_fh *fh = priv;
1342	struct channel_obj *ch = fh->channel;
1343	int ret = 0;
1344
1345	vpif_dbg(2, debug, "vpif_querystd\n");
1346
1347	/* Call querystd function of decoder device */
1348	ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1349				querystd, std_id);
1350	if (ret < 0)
1351		vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1352
1353	return ret;
1354}
1355
1356/**
1357 * vpif_g_std() - get STD handler
1358 * @file: file ptr
1359 * @priv: file handle
1360 * @std_id: ptr to std id
1361 */
1362static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1363{
1364	struct vpif_fh *fh = priv;
1365	struct channel_obj *ch = fh->channel;
1366
1367	vpif_dbg(2, debug, "vpif_g_std\n");
1368
1369	*std = ch->video.stdid;
1370	return 0;
1371}
1372
1373/**
1374 * vpif_s_std() - set STD handler
1375 * @file: file ptr
1376 * @priv: file handle
1377 * @std_id: ptr to std id
1378 */
1379static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1380{
1381	struct vpif_fh *fh = priv;
1382	struct channel_obj *ch = fh->channel;
1383	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1384	int ret = 0;
1385
1386	vpif_dbg(2, debug, "vpif_s_std\n");
1387
1388	if (common->started) {
1389		vpif_err("streaming in progress\n");
1390		return -EBUSY;
1391	}
1392
1393	if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1394	    (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1395		if (!fh->initialized) {
1396			vpif_dbg(1, debug, "Channel Busy\n");
1397			return -EBUSY;
1398		}
1399	}
1400
1401	ret = v4l2_prio_check(&ch->prio, fh->prio);
1402	if (0 != ret)
1403		return ret;
1404
1405	fh->initialized = 1;
1406
1407	/* Call encoder subdevice function to set the standard */
1408	ch->video.stdid = *std_id;
1409	ch->video.dv_preset = V4L2_DV_INVALID;
1410	memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1411
1412	/* Get the information about the standard */
1413	if (vpif_update_std_info(ch)) {
1414		vpif_err("Error getting the standard info\n");
1415		return -EINVAL;
1416	}
1417
1418	/* Configure the default format information */
1419	vpif_config_format(ch);
1420
1421	/* set standard in the sub device */
1422	ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1423				s_std, *std_id);
1424	if (ret < 0)
1425		vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1426	return ret;
1427}
1428
1429/**
1430 * vpif_enum_input() - ENUMINPUT handler
1431 * @file: file ptr
1432 * @priv: file handle
1433 * @input: ptr to input structure
1434 */
1435static int vpif_enum_input(struct file *file, void *priv,
1436				struct v4l2_input *input)
1437{
1438
1439	struct vpif_capture_config *config = vpif_dev->platform_data;
1440	struct vpif_capture_chan_config *chan_cfg;
1441	struct vpif_fh *fh = priv;
1442	struct channel_obj *ch = fh->channel;
1443
1444	chan_cfg = &config->chan_config[ch->channel_id];
1445
1446	if (input->index >= chan_cfg->input_count) {
1447		vpif_dbg(1, debug, "Invalid input index\n");
1448		return -EINVAL;
1449	}
1450
1451	memcpy(input, &chan_cfg->inputs[input->index].input,
1452		sizeof(*input));
1453	return 0;
1454}
1455
1456/**
1457 * vpif_g_input() - Get INPUT handler
1458 * @file: file ptr
1459 * @priv: file handle
1460 * @index: ptr to input index
1461 */
1462static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1463{
1464	struct vpif_fh *fh = priv;
1465	struct channel_obj *ch = fh->channel;
1466	struct video_obj *vid_ch = &ch->video;
1467
1468	*index = vid_ch->input_idx;
1469
1470	return 0;
1471}
1472
1473/**
1474 * vpif_s_input() - Set INPUT handler
1475 * @file: file ptr
1476 * @priv: file handle
1477 * @index: input index
1478 */
1479static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1480{
1481	struct vpif_capture_config *config = vpif_dev->platform_data;
1482	struct vpif_capture_chan_config *chan_cfg;
1483	struct vpif_fh *fh = priv;
1484	struct channel_obj *ch = fh->channel;
1485	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1486	struct video_obj *vid_ch = &ch->video;
1487	struct vpif_subdev_info *subdev_info;
1488	int ret = 0, sd_index = 0;
1489	u32 input = 0, output = 0;
1490
1491	chan_cfg = &config->chan_config[ch->channel_id];
1492
1493	if (common->started) {
1494		vpif_err("Streaming in progress\n");
1495		return -EBUSY;
1496	}
1497
1498	if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1499	    (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1500		if (!fh->initialized) {
1501			vpif_dbg(1, debug, "Channel Busy\n");
1502			return -EBUSY;
1503		}
1504	}
1505
1506	ret = v4l2_prio_check(&ch->prio, fh->prio);
1507	if (0 != ret)
1508		return ret;
1509
1510	fh->initialized = 1;
1511	subdev_info = vpif_map_sub_device_to_input(ch, config, index,
1512						   &sd_index);
1513	if (NULL == subdev_info) {
1514		vpif_dbg(1, debug,
1515			"couldn't lookup sub device for the input index\n");
1516		return -EINVAL;
1517	}
1518
1519	/* first setup input path from sub device to vpif */
1520	if (config->setup_input_path) {
1521		ret = config->setup_input_path(ch->channel_id,
1522					       subdev_info->name);
1523		if (ret < 0) {
1524			vpif_dbg(1, debug, "couldn't setup input path for the"
1525				" sub device %s, for input index %d\n",
1526				subdev_info->name, index);
1527			return ret;
1528		}
1529	}
1530
1531	if (subdev_info->can_route) {
1532		input = subdev_info->input;
1533		output = subdev_info->output;
1534		ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
1535					input, output, 0);
1536		if (ret < 0) {
1537			vpif_dbg(1, debug, "Failed to set input\n");
1538			return ret;
1539		}
1540	}
1541	vid_ch->input_idx = index;
1542	ch->curr_subdev_info = subdev_info;
1543	ch->curr_sd_index = sd_index;
1544	/* copy interface parameters to vpif */
1545	ch->vpifparams.iface = subdev_info->vpif_if;
1546
1547	/* update tvnorms from the sub device input info */
1548	ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1549	return ret;
1550}
1551
1552/**
1553 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1554 * @file: file ptr
1555 * @priv: file handle
1556 * @index: input index
1557 */
1558static int vpif_enum_fmt_vid_cap(struct file *file, void  *priv,
1559					struct v4l2_fmtdesc *fmt)
1560{
1561	struct vpif_fh *fh = priv;
1562	struct channel_obj *ch = fh->channel;
1563
1564	if (fmt->index != 0) {
1565		vpif_dbg(1, debug, "Invalid format index\n");
1566		return -EINVAL;
1567	}
1568
1569	/* Fill in the information about format */
1570	if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1571		fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1572		strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1573		fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1574	} else {
1575		fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1576		strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1577		fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1578	}
1579	return 0;
1580}
1581
1582/**
1583 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1584 * @file: file ptr
1585 * @priv: file handle
1586 * @fmt: ptr to v4l2 format structure
1587 */
1588static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1589				struct v4l2_format *fmt)
1590{
1591	struct vpif_fh *fh = priv;
1592	struct channel_obj *ch = fh->channel;
1593	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1594
1595	return vpif_check_format(ch, pixfmt, 1);
1596}
1597
1598
1599/**
1600 * vpif_g_fmt_vid_cap() - Set INPUT handler
1601 * @file: file ptr
1602 * @priv: file handle
1603 * @fmt: ptr to v4l2 format structure
1604 */
1605static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1606				struct v4l2_format *fmt)
1607{
1608	struct vpif_fh *fh = priv;
1609	struct channel_obj *ch = fh->channel;
1610	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1611
1612	/* Check the validity of the buffer type */
1613	if (common->fmt.type != fmt->type)
1614		return -EINVAL;
1615
1616	/* Fill in the information about format */
1617	*fmt = common->fmt;
1618	return 0;
1619}
1620
1621/**
1622 * vpif_s_fmt_vid_cap() - Set FMT handler
1623 * @file: file ptr
1624 * @priv: file handle
1625 * @fmt: ptr to v4l2 format structure
1626 */
1627static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1628				struct v4l2_format *fmt)
1629{
1630	struct vpif_fh *fh = priv;
1631	struct channel_obj *ch = fh->channel;
1632	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1633	struct v4l2_pix_format *pixfmt;
1634	int ret = 0;
1635
1636	vpif_dbg(2, debug, "%s\n", __func__);
1637
1638	/* If streaming is started, return error */
1639	if (common->started) {
1640		vpif_dbg(1, debug, "Streaming is started\n");
1641		return -EBUSY;
1642	}
1643
1644	if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1645	    (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1646		if (!fh->initialized) {
1647			vpif_dbg(1, debug, "Channel Busy\n");
1648			return -EBUSY;
1649		}
1650	}
1651
1652	ret = v4l2_prio_check(&ch->prio, fh->prio);
1653	if (0 != ret)
1654		return ret;
1655
1656	fh->initialized = 1;
1657
1658	pixfmt = &fmt->fmt.pix;
1659	/* Check for valid field format */
1660	ret = vpif_check_format(ch, pixfmt, 0);
1661
1662	if (ret)
1663		return ret;
1664	/* store the format in the channel object */
1665	common->fmt = *fmt;
1666	return 0;
1667}
1668
1669/**
1670 * vpif_querycap() - QUERYCAP handler
1671 * @file: file ptr
1672 * @priv: file handle
1673 * @cap: ptr to v4l2_capability structure
1674 */
1675static int vpif_querycap(struct file *file, void  *priv,
1676				struct v4l2_capability *cap)
1677{
1678	struct vpif_capture_config *config = vpif_dev->platform_data;
1679
1680	cap->version = VPIF_CAPTURE_VERSION_CODE;
1681	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1682	strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
1683	strlcpy(cap->bus_info, "DM646x Platform", sizeof(cap->bus_info));
1684	strlcpy(cap->card, config->card_name, sizeof(cap->card));
1685
1686	return 0;
1687}
1688
1689/**
1690 * vpif_g_priority() - get priority handler
1691 * @file: file ptr
1692 * @priv: file handle
1693 * @prio: ptr to v4l2_priority structure
1694 */
1695static int vpif_g_priority(struct file *file, void *priv,
1696			   enum v4l2_priority *prio)
1697{
1698	struct vpif_fh *fh = priv;
1699	struct channel_obj *ch = fh->channel;
1700
1701	*prio = v4l2_prio_max(&ch->prio);
1702
1703	return 0;
1704}
1705
1706/**
1707 * vpif_s_priority() - set priority handler
1708 * @file: file ptr
1709 * @priv: file handle
1710 * @prio: ptr to v4l2_priority structure
1711 */
1712static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1713{
1714	struct vpif_fh *fh = priv;
1715	struct channel_obj *ch = fh->channel;
1716
1717	return v4l2_prio_change(&ch->prio, &fh->prio, p);
1718}
1719
1720/**
1721 * vpif_cropcap() - cropcap handler
1722 * @file: file ptr
1723 * @priv: file handle
1724 * @crop: ptr to v4l2_cropcap structure
1725 */
1726static int vpif_cropcap(struct file *file, void *priv,
1727			struct v4l2_cropcap *crop)
1728{
1729	struct vpif_fh *fh = priv;
1730	struct channel_obj *ch = fh->channel;
1731	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1732
1733	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1734		return -EINVAL;
1735
1736	crop->bounds.left = 0;
1737	crop->bounds.top = 0;
1738	crop->bounds.height = common->height;
1739	crop->bounds.width = common->width;
1740	crop->defrect = crop->bounds;
1741	return 0;
1742}
1743
1744/**
1745 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1746 * @file: file ptr
1747 * @priv: file handle
1748 * @preset: input preset
1749 */
1750static int vpif_enum_dv_presets(struct file *file, void *priv,
1751		struct v4l2_dv_enum_preset *preset)
1752{
1753	struct vpif_fh *fh = priv;
1754	struct channel_obj *ch = fh->channel;
1755
1756	return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1757			video, enum_dv_presets, preset);
1758}
1759
1760/**
1761 * vpif_query_dv_presets() - QUERY_DV_PRESET handler
1762 * @file: file ptr
1763 * @priv: file handle
1764 * @preset: input preset
1765 */
1766static int vpif_query_dv_preset(struct file *file, void *priv,
1767		struct v4l2_dv_preset *preset)
1768{
1769	struct vpif_fh *fh = priv;
1770	struct channel_obj *ch = fh->channel;
1771
1772	return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1773		       video, query_dv_preset, preset);
1774}
1775/**
1776 * vpif_s_dv_presets() - S_DV_PRESETS handler
1777 * @file: file ptr
1778 * @priv: file handle
1779 * @preset: input preset
1780 */
1781static int vpif_s_dv_preset(struct file *file, void *priv,
1782		struct v4l2_dv_preset *preset)
1783{
1784	struct vpif_fh *fh = priv;
1785	struct channel_obj *ch = fh->channel;
1786	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1787	int ret = 0;
1788
1789	if (common->started) {
1790		vpif_dbg(1, debug, "streaming in progress\n");
1791		return -EBUSY;
1792	}
1793
1794	if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1795	    (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1796		if (!fh->initialized) {
1797			vpif_dbg(1, debug, "Channel Busy\n");
1798			return -EBUSY;
1799		}
1800	}
1801
1802	ret = v4l2_prio_check(&ch->prio, fh->prio);
1803	if (ret)
1804		return ret;
1805
1806	fh->initialized = 1;
1807
1808	/* Call encoder subdevice function to set the standard */
1809	if (mutex_lock_interruptible(&common->lock))
1810		return -ERESTARTSYS;
1811
1812	ch->video.dv_preset = preset->preset;
1813	ch->video.stdid = V4L2_STD_UNKNOWN;
1814	memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1815
1816	/* Get the information about the standard */
1817	if (vpif_update_std_info(ch)) {
1818		vpif_dbg(1, debug, "Error getting the standard info\n");
1819		ret = -EINVAL;
1820	} else {
1821		/* Configure the default format information */
1822		vpif_config_format(ch);
1823
1824		ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1825				video, s_dv_preset, preset);
1826	}
1827
1828	mutex_unlock(&common->lock);
1829
1830	return ret;
1831}
1832/**
1833 * vpif_g_dv_presets() - G_DV_PRESETS handler
1834 * @file: file ptr
1835 * @priv: file handle
1836 * @preset: input preset
1837 */
1838static int vpif_g_dv_preset(struct file *file, void *priv,
1839		struct v4l2_dv_preset *preset)
1840{
1841	struct vpif_fh *fh = priv;
1842	struct channel_obj *ch = fh->channel;
1843
1844	preset->preset = ch->video.dv_preset;
1845
1846	return 0;
1847}
1848
1849/**
1850 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1851 * @file: file ptr
1852 * @priv: file handle
1853 * @timings: digital video timings
1854 */
1855static int vpif_s_dv_timings(struct file *file, void *priv,
1856		struct v4l2_dv_timings *timings)
1857{
1858	struct vpif_fh *fh = priv;
1859	struct channel_obj *ch = fh->channel;
1860	struct vpif_params *vpifparams = &ch->vpifparams;
1861	struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1862	struct video_obj *vid_ch = &ch->video;
1863	struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1864	int ret;
1865
1866	if (timings->type != V4L2_DV_BT_656_1120) {
1867		vpif_dbg(2, debug, "Timing type not defined\n");
1868		return -EINVAL;
1869	}
1870
1871	/* Configure subdevice timings, if any */
1872	ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1873			video, s_dv_timings, timings);
1874	if (ret == -ENOIOCTLCMD) {
1875		vpif_dbg(2, debug, "Custom DV timings not supported by "
1876				"subdevice\n");
1877		return -EINVAL;
1878	}
1879	if (ret < 0) {
1880		vpif_dbg(2, debug, "Error setting custom DV timings\n");
1881		return ret;
1882	}
1883
1884	if (!(timings->bt.width && timings->bt.height &&
1885				(timings->bt.hbackporch ||
1886				 timings->bt.hfrontporch ||
1887				 timings->bt.hsync) &&
1888				timings->bt.vfrontporch &&
1889				(timings->bt.vbackporch ||
1890				 timings->bt.vsync))) {
1891		vpif_dbg(2, debug, "Timings for width, height, "
1892				"horizontal back porch, horizontal sync, "
1893				"horizontal front porch, vertical back porch, "
1894				"vertical sync and vertical back porch "
1895				"must be defined\n");
1896		return -EINVAL;
1897	}
1898
1899	*bt = timings->bt;
1900
1901	/* Configure video port timings */
1902
1903	std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1904		bt->hsync - 8;
1905	std_info->sav2eav = bt->width;
1906
1907	std_info->l1 = 1;
1908	std_info->l3 = bt->vsync + bt->vbackporch + 1;
1909
1910	if (bt->interlaced) {
1911		if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1912			std_info->vsize = bt->height * 2 +
1913				bt->vfrontporch + bt->vsync + bt->vbackporch +
1914				bt->il_vfrontporch + bt->il_vsync +
1915				bt->il_vbackporch;
1916			std_info->l5 = std_info->vsize/2 -
1917				(bt->vfrontporch - 1);
1918			std_info->l7 = std_info->vsize/2 + 1;
1919			std_info->l9 = std_info->l7 + bt->il_vsync +
1920				bt->il_vbackporch + 1;
1921			std_info->l11 = std_info->vsize -
1922				(bt->il_vfrontporch - 1);
1923		} else {
1924			vpif_dbg(2, debug, "Required timing values for "
1925					"interlaced BT format missing\n");
1926			return -EINVAL;
1927		}
1928	} else {
1929		std_info->vsize = bt->height + bt->vfrontporch +
1930			bt->vsync + bt->vbackporch;
1931		std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1932	}
1933	strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1934	std_info->width = bt->width;
1935	std_info->height = bt->height;
1936	std_info->frm_fmt = bt->interlaced ? 0 : 1;
1937	std_info->ycmux_mode = 0;
1938	std_info->capture_format = 0;
1939	std_info->vbi_supported = 0;
1940	std_info->hd_sd = 1;
1941	std_info->stdid = 0;
1942	std_info->dv_preset = V4L2_DV_INVALID;
1943
1944	vid_ch->stdid = 0;
1945	vid_ch->dv_preset = V4L2_DV_INVALID;
1946	return 0;
1947}
1948
1949/**
1950 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1951 * @file: file ptr
1952 * @priv: file handle
1953 * @timings: digital video timings
1954 */
1955static int vpif_g_dv_timings(struct file *file, void *priv,
1956		struct v4l2_dv_timings *timings)
1957{
1958	struct vpif_fh *fh = priv;
1959	struct channel_obj *ch = fh->channel;
1960	struct video_obj *vid_ch = &ch->video;
1961	struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1962
1963	timings->bt = *bt;
1964
1965	return 0;
1966}
1967
1968/*
1969 * vpif_g_chip_ident() - Identify the chip
1970 * @file: file ptr
1971 * @priv: file handle
1972 * @chip: chip identity
1973 *
1974 * Returns zero or -EINVAL if read operations fails.
1975 */
1976static int vpif_g_chip_ident(struct file *file, void *priv,
1977		struct v4l2_dbg_chip_ident *chip)
1978{
1979	chip->ident = V4L2_IDENT_NONE;
1980	chip->revision = 0;
1981	if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1982			chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1983		vpif_dbg(2, debug, "match_type is invalid.\n");
1984		return -EINVAL;
1985	}
1986
1987	return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1988			g_chip_ident, chip);
1989}
1990
1991#ifdef CONFIG_VIDEO_ADV_DEBUG
1992/*
1993 * vpif_dbg_g_register() - Read register
1994 * @file: file ptr
1995 * @priv: file handle
1996 * @reg: register to be read
1997 *
1998 * Debugging only
1999 * Returns zero or -EINVAL if read operations fails.
2000 */
2001static int vpif_dbg_g_register(struct file *file, void *priv,
2002		struct v4l2_dbg_register *reg){
2003	struct vpif_fh *fh = priv;
2004	struct channel_obj *ch = fh->channel;
2005
2006	return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2007			g_register, reg);
2008}
2009
2010/*
2011 * vpif_dbg_s_register() - Write to register
2012 * @file: file ptr
2013 * @priv: file handle
2014 * @reg: register to be modified
2015 *
2016 * Debugging only
2017 * Returns zero or -EINVAL if write operations fails.
2018 */
2019static int vpif_dbg_s_register(struct file *file, void *priv,
2020		struct v4l2_dbg_register *reg){
2021	struct vpif_fh *fh = priv;
2022	struct channel_obj *ch = fh->channel;
2023
2024	return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2025			s_register, reg);
2026}
2027#endif
2028
2029/*
2030 * vpif_log_status() - Status information
2031 * @file: file ptr
2032 * @priv: file handle
2033 *
2034 * Returns zero.
2035 */
2036static int vpif_log_status(struct file *filep, void *priv)
2037{
2038	/* status for sub devices */
2039	v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
2040
2041	return 0;
2042}
2043
2044/* vpif capture ioctl operations */
2045static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
2046	.vidioc_querycap        	= vpif_querycap,
2047	.vidioc_g_priority		= vpif_g_priority,
2048	.vidioc_s_priority		= vpif_s_priority,
2049	.vidioc_enum_fmt_vid_cap	= vpif_enum_fmt_vid_cap,
2050	.vidioc_g_fmt_vid_cap  		= vpif_g_fmt_vid_cap,
2051	.vidioc_s_fmt_vid_cap		= vpif_s_fmt_vid_cap,
2052	.vidioc_try_fmt_vid_cap		= vpif_try_fmt_vid_cap,
2053	.vidioc_enum_input		= vpif_enum_input,
2054	.vidioc_s_input			= vpif_s_input,
2055	.vidioc_g_input			= vpif_g_input,
2056	.vidioc_reqbufs         	= vpif_reqbufs,
2057	.vidioc_querybuf        	= vpif_querybuf,
2058	.vidioc_querystd		= vpif_querystd,
2059	.vidioc_s_std           	= vpif_s_std,
2060	.vidioc_g_std			= vpif_g_std,
2061	.vidioc_qbuf            	= vpif_qbuf,
2062	.vidioc_dqbuf           	= vpif_dqbuf,
2063	.vidioc_streamon        	= vpif_streamon,
2064	.vidioc_streamoff       	= vpif_streamoff,
2065	.vidioc_cropcap         	= vpif_cropcap,
2066	.vidioc_enum_dv_presets         = vpif_enum_dv_presets,
2067	.vidioc_s_dv_preset             = vpif_s_dv_preset,
2068	.vidioc_g_dv_preset             = vpif_g_dv_preset,
2069	.vidioc_query_dv_preset         = vpif_query_dv_preset,
2070	.vidioc_s_dv_timings            = vpif_s_dv_timings,
2071	.vidioc_g_dv_timings            = vpif_g_dv_timings,
2072	.vidioc_g_chip_ident		= vpif_g_chip_ident,
2073#ifdef CONFIG_VIDEO_ADV_DEBUG
2074	.vidioc_g_register		= vpif_dbg_g_register,
2075	.vidioc_s_register		= vpif_dbg_s_register,
2076#endif
2077	.vidioc_log_status		= vpif_log_status,
2078};
2079
2080/* vpif file operations */
2081static struct v4l2_file_operations vpif_fops = {
2082	.owner = THIS_MODULE,
2083	.open = vpif_open,
2084	.release = vpif_release,
2085	.unlocked_ioctl = video_ioctl2,
2086	.mmap = vpif_mmap,
2087	.poll = vpif_poll
2088};
2089
2090/* vpif video template */
2091static struct video_device vpif_video_template = {
2092	.name		= "vpif",
2093	.fops		= &vpif_fops,
2094	.minor		= -1,
2095	.ioctl_ops	= &vpif_ioctl_ops,
2096};
2097
2098/**
2099 * initialize_vpif() - Initialize vpif data structures
2100 *
2101 * Allocate memory for data structures and initialize them
2102 */
2103static int initialize_vpif(void)
2104{
2105	int err = 0, i, j;
2106	int free_channel_objects_index;
2107
2108	/* Default number of buffers should be 3 */
2109	if ((ch0_numbuffers > 0) &&
2110	    (ch0_numbuffers < config_params.min_numbuffers))
2111		ch0_numbuffers = config_params.min_numbuffers;
2112	if ((ch1_numbuffers > 0) &&
2113	    (ch1_numbuffers < config_params.min_numbuffers))
2114		ch1_numbuffers = config_params.min_numbuffers;
2115
2116	/* Set buffer size to min buffers size if it is invalid */
2117	if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2118		ch0_bufsize =
2119		    config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2120	if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2121		ch1_bufsize =
2122		    config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2123
2124	config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2125	config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2126	if (ch0_numbuffers) {
2127		config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2128		    = ch0_bufsize;
2129	}
2130	if (ch1_numbuffers) {
2131		config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2132		    = ch1_bufsize;
2133	}
2134
2135	/* Allocate memory for six channel objects */
2136	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2137		vpif_obj.dev[i] =
2138		    kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2139		/* If memory allocation fails, return error */
2140		if (!vpif_obj.dev[i]) {
2141			free_channel_objects_index = i;
2142			err = -ENOMEM;
2143			goto vpif_init_free_channel_objects;
2144		}
2145	}
2146	return 0;
2147
2148vpif_init_free_channel_objects:
2149	for (j = 0; j < free_channel_objects_index; j++)
2150		kfree(vpif_obj.dev[j]);
2151	return err;
2152}
2153
2154/**
2155 * vpif_probe : This function probes the vpif capture driver
2156 * @pdev: platform device pointer
2157 *
2158 * This creates device entries by register itself to the V4L2 driver and
2159 * initializes fields of each channel objects
2160 */
2161static __init int vpif_probe(struct platform_device *pdev)
2162{
2163	struct vpif_subdev_info *subdevdata;
2164	struct vpif_capture_config *config;
2165	int i, j, k, m, q, err;
2166	struct i2c_adapter *i2c_adap;
2167	struct channel_obj *ch;
2168	struct common_obj *common;
2169	struct video_device *vfd;
2170	struct resource *res;
2171	int subdev_count;
2172
2173	vpif_dev = &pdev->dev;
2174
2175	err = initialize_vpif();
2176	if (err) {
2177		v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2178		return err;
2179	}
2180
2181	k = 0;
2182	while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2183		for (i = res->start; i <= res->end; i++) {
2184			if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
2185					"DM646x_Capture",
2186				(void *)(&vpif_obj.dev[k]->channel_id))) {
2187				err = -EBUSY;
2188				i--;
2189				goto vpif_int_err;
2190			}
2191		}
2192		k++;
2193	}
2194
2195	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2196		/* Get the pointer to the channel object */
2197		ch = vpif_obj.dev[i];
2198		/* Allocate memory for video device */
2199		vfd = video_device_alloc();
2200		if (NULL == vfd) {
2201			for (j = 0; j < i; j++) {
2202				ch = vpif_obj.dev[j];
2203				video_device_release(ch->video_dev);
2204			}
2205			err = -ENOMEM;
2206			goto vpif_dev_alloc_err;
2207		}
2208
2209		/* Initialize field of video device */
2210		*vfd = vpif_video_template;
2211		vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2212		vfd->release = video_device_release;
2213		snprintf(vfd->name, sizeof(vfd->name),
2214			 "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
2215			 (VPIF_CAPTURE_VERSION_CODE >> 16) & 0xff,
2216			 (VPIF_CAPTURE_VERSION_CODE >> 8) & 0xff,
2217			 (VPIF_CAPTURE_VERSION_CODE) & 0xff);
2218		/* Set video_dev to the video device */
2219		ch->video_dev = vfd;
2220	}
2221
2222	for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2223		ch = vpif_obj.dev[j];
2224		ch->channel_id = j;
2225		common = &(ch->common[VPIF_VIDEO_INDEX]);
2226		spin_lock_init(&common->irqlock);
2227		mutex_init(&common->lock);
2228		ch->video_dev->lock = &common->lock;
2229		/* Initialize prio member of channel object */
2230		v4l2_prio_init(&ch->prio);
2231		err = video_register_device(ch->video_dev,
2232					    VFL_TYPE_GRABBER, (j ? 1 : 0));
2233		if (err)
2234			goto probe_out;
2235
2236		video_set_drvdata(ch->video_dev, ch);
2237
2238	}
2239
2240	i2c_adap = i2c_get_adapter(1);
2241	config = pdev->dev.platform_data;
2242
2243	subdev_count = config->subdev_count;
2244	vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2245				GFP_KERNEL);
2246	if (vpif_obj.sd == NULL) {
2247		vpif_err("unable to allocate memory for subdevice pointers\n");
2248		err = -ENOMEM;
2249		goto probe_out;
2250	}
2251
2252	err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2253	if (err) {
2254		v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2255		goto probe_subdev_out;
2256	}
2257
2258	for (i = 0; i < subdev_count; i++) {
2259		subdevdata = &config->subdev_info[i];
2260		vpif_obj.sd[i] =
2261			v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2262						  i2c_adap,
2263						  &subdevdata->board_info,
2264						  NULL);
2265
2266		if (!vpif_obj.sd[i]) {
2267			vpif_err("Error registering v4l2 subdevice\n");
2268			goto probe_subdev_out;
2269		}
2270		v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2271			  subdevdata->name);
2272
2273		if (vpif_obj.sd[i])
2274			vpif_obj.sd[i]->grp_id = 1 << i;
2275	}
2276
2277	v4l2_info(&vpif_obj.v4l2_dev,
2278			"DM646x VPIF capture driver initialized\n");
2279	return 0;
2280
2281probe_subdev_out:
2282	/* free sub devices memory */
2283	kfree(vpif_obj.sd);
2284
2285	j = VPIF_CAPTURE_MAX_DEVICES;
2286probe_out:
2287	v4l2_device_unregister(&vpif_obj.v4l2_dev);
2288	for (k = 0; k < j; k++) {
2289		/* Get the pointer to the channel object */
2290		ch = vpif_obj.dev[k];
2291		/* Unregister video device */
2292		video_unregister_device(ch->video_dev);
2293	}
2294
2295vpif_dev_alloc_err:
2296	k = VPIF_CAPTURE_MAX_DEVICES-1;
2297	res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2298	i = res->end;
2299
2300vpif_int_err:
2301	for (q = k; q >= 0; q--) {
2302		for (m = i; m >= (int)res->start; m--)
2303			free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2304
2305		res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2306		if (res)
2307			i = res->end;
2308	}
2309	return err;
2310}
2311
2312/**
2313 * vpif_remove() - driver remove handler
2314 * @device: ptr to platform device structure
2315 *
2316 * The vidoe device is unregistered
2317 */
2318static int vpif_remove(struct platform_device *device)
2319{
2320	int i;
2321	struct channel_obj *ch;
2322
2323	v4l2_device_unregister(&vpif_obj.v4l2_dev);
2324
2325	/* un-register device */
2326	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2327		/* Get the pointer to the channel object */
2328		ch = vpif_obj.dev[i];
2329		/* Unregister video device */
2330		video_unregister_device(ch->video_dev);
2331	}
2332	return 0;
2333}
2334
2335/**
2336 * vpif_suspend: vpif device suspend
2337 *
2338 * TODO: Add suspend code here
2339 */
2340static int
2341vpif_suspend(struct device *dev)
2342{
2343	return -1;
2344}
2345
2346/**
2347 * vpif_resume: vpif device suspend
2348 *
2349 * TODO: Add resume code here
2350 */
2351static int
2352vpif_resume(struct device *dev)
2353{
2354	return -1;
2355}
2356
2357static const struct dev_pm_ops vpif_dev_pm_ops = {
2358	.suspend = vpif_suspend,
2359	.resume = vpif_resume,
2360};
2361
2362static __refdata struct platform_driver vpif_driver = {
2363	.driver	= {
2364		.name	= "vpif_capture",
2365		.owner	= THIS_MODULE,
2366		.pm = &vpif_dev_pm_ops,
2367	},
2368	.probe = vpif_probe,
2369	.remove = vpif_remove,
2370};
2371
2372/**
2373 * vpif_init: initialize the vpif driver
2374 *
2375 * This function registers device and driver to the kernel, requests irq
2376 * handler and allocates memory
2377 * for channel objects
2378 */
2379static __init int vpif_init(void)
2380{
2381	return platform_driver_register(&vpif_driver);
2382}
2383
2384/**
2385 * vpif_cleanup : This function clean up the vpif capture resources
2386 *
2387 * This will un-registers device and driver to the kernel, frees
2388 * requested irq handler and de-allocates memory allocated for channel
2389 * objects.
2390 */
2391static void vpif_cleanup(void)
2392{
2393	struct platform_device *pdev;
2394	struct resource *res;
2395	int irq_num;
2396	int i = 0;
2397
2398	pdev = container_of(vpif_dev, struct platform_device, dev);
2399	while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2400		for (irq_num = res->start; irq_num <= res->end; irq_num++)
2401			free_irq(irq_num,
2402				 (void *)(&vpif_obj.dev[i]->channel_id));
2403		i++;
2404	}
2405
2406	platform_driver_unregister(&vpif_driver);
2407
2408	kfree(vpif_obj.sd);
2409	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2410		kfree(vpif_obj.dev[i]);
2411}
2412
2413/* Function for module initialization and cleanup */
2414module_init(vpif_init);
2415module_exit(vpif_cleanup);
2416