1/*
2 * Copyright (C) 2009 Texas Instruments Inc
3 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 *
19 * TODO : add support for VBI & HBI data service
20 *	  add static buffer allocation
21 */
22
23#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/platform_device.h>
26#include <linux/slab.h>
27
28#include <media/v4l2-ioctl.h>
29
30#include "vpif.h"
31#include "vpif_capture.h"
32
33MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34MODULE_LICENSE("GPL");
35MODULE_VERSION(VPIF_CAPTURE_VERSION);
36
37#define vpif_err(fmt, arg...)	v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38#define vpif_dbg(level, debug, fmt, arg...)	\
39		v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40
41static int debug = 1;
42
43module_param(debug, int, 0644);
44
45MODULE_PARM_DESC(debug, "Debug level 0-1");
46
47#define VPIF_DRIVER_NAME	"vpif_capture"
48
49/* global variables */
50static struct vpif_device vpif_obj = { {NULL} };
51static struct device *vpif_dev;
52static void vpif_calculate_offsets(struct channel_obj *ch);
53static void vpif_config_addr(struct channel_obj *ch, int muxmode);
54
55static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
56
57/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
58static int ycmux_mode;
59
60static inline
61struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
62{
63	return container_of(vb, struct vpif_cap_buffer, vb);
64}
65
66/**
67 * vpif_buffer_prepare :  callback function for buffer prepare
68 * @vb: ptr to vb2_buffer
69 *
70 * This is the callback function for buffer prepare when vb2_qbuf()
71 * function is called. The buffer is prepared and user space virtual address
72 * or user address is converted into  physical address
73 */
74static int vpif_buffer_prepare(struct vb2_buffer *vb)
75{
76	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
77	struct vb2_queue *q = vb->vb2_queue;
78	struct channel_obj *ch = vb2_get_drv_priv(q);
79	struct common_obj *common;
80	unsigned long addr;
81
82	vpif_dbg(2, debug, "vpif_buffer_prepare\n");
83
84	common = &ch->common[VPIF_VIDEO_INDEX];
85
86	vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
87	if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
88		return -EINVAL;
89
90	vbuf->field = common->fmt.fmt.pix.field;
91
92	addr = vb2_dma_contig_plane_dma_addr(vb, 0);
93	if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
94		!IS_ALIGNED((addr + common->ybtm_off), 8) ||
95		!IS_ALIGNED((addr + common->ctop_off), 8) ||
96		!IS_ALIGNED((addr + common->cbtm_off), 8)) {
97		vpif_dbg(1, debug, "offset is not aligned\n");
98		return -EINVAL;
99	}
100
101	return 0;
102}
103
104/**
105 * vpif_buffer_queue_setup : Callback function for buffer setup.
106 * @vq: vb2_queue ptr
107 * @fmt: v4l2 format
108 * @nbuffers: ptr to number of buffers requested by application
109 * @nplanes:: contains number of distinct video planes needed to hold a frame
110 * @sizes[]: contains the size (in bytes) of each plane.
111 * @alloc_ctxs: ptr to allocation context
112 *
113 * This callback function is called when reqbuf() is called to adjust
114 * the buffer count and buffer size
115 */
116static int vpif_buffer_queue_setup(struct vb2_queue *vq,
117				const void *parg,
118				unsigned int *nbuffers, unsigned int *nplanes,
119				unsigned int sizes[], void *alloc_ctxs[])
120{
121	const struct v4l2_format *fmt = parg;
122	struct channel_obj *ch = vb2_get_drv_priv(vq);
123	struct common_obj *common;
124
125	common = &ch->common[VPIF_VIDEO_INDEX];
126
127	vpif_dbg(2, debug, "vpif_buffer_setup\n");
128
129	if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
130		return -EINVAL;
131
132	if (vq->num_buffers + *nbuffers < 3)
133		*nbuffers = 3 - vq->num_buffers;
134
135	*nplanes = 1;
136	sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
137	alloc_ctxs[0] = common->alloc_ctx;
138
139	/* Calculate the offset for Y and C data in the buffer */
140	vpif_calculate_offsets(ch);
141
142	return 0;
143}
144
145/**
146 * vpif_buffer_queue : Callback function to add buffer to DMA queue
147 * @vb: ptr to vb2_buffer
148 */
149static void vpif_buffer_queue(struct vb2_buffer *vb)
150{
151	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
152	struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
153	struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
154	struct common_obj *common;
155	unsigned long flags;
156
157	common = &ch->common[VPIF_VIDEO_INDEX];
158
159	vpif_dbg(2, debug, "vpif_buffer_queue\n");
160
161	spin_lock_irqsave(&common->irqlock, flags);
162	/* add the buffer to the DMA queue */
163	list_add_tail(&buf->list, &common->dma_queue);
164	spin_unlock_irqrestore(&common->irqlock, flags);
165}
166
167/**
168 * vpif_start_streaming : Starts the DMA engine for streaming
169 * @vb: ptr to vb2_buffer
170 * @count: number of buffers
171 */
172static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
173{
174	struct vpif_capture_config *vpif_config_data =
175					vpif_dev->platform_data;
176	struct channel_obj *ch = vb2_get_drv_priv(vq);
177	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
178	struct vpif_params *vpif = &ch->vpifparams;
179	struct vpif_cap_buffer *buf, *tmp;
180	unsigned long addr, flags;
181	int ret;
182
183	spin_lock_irqsave(&common->irqlock, flags);
184
185	/* Initialize field_id */
186	ch->field_id = 0;
187
188	/* configure 1 or 2 channel mode */
189	if (vpif_config_data->setup_input_channel_mode) {
190		ret = vpif_config_data->
191			setup_input_channel_mode(vpif->std_info.ycmux_mode);
192		if (ret < 0) {
193			vpif_dbg(1, debug, "can't set vpif channel mode\n");
194			goto err;
195		}
196	}
197
198	ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
199	if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
200		vpif_dbg(1, debug, "stream on failed in subdev\n");
201		goto err;
202	}
203
204	/* Call vpif_set_params function to set the parameters and addresses */
205	ret = vpif_set_video_params(vpif, ch->channel_id);
206	if (ret < 0) {
207		vpif_dbg(1, debug, "can't set video params\n");
208		goto err;
209	}
210
211	ycmux_mode = ret;
212	vpif_config_addr(ch, ret);
213
214	/* Get the next frame from the buffer queue */
215	common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
216				    struct vpif_cap_buffer, list);
217	/* Remove buffer from the buffer queue */
218	list_del(&common->cur_frm->list);
219	spin_unlock_irqrestore(&common->irqlock, flags);
220
221	addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
222
223	common->set_addr(addr + common->ytop_off,
224			 addr + common->ybtm_off,
225			 addr + common->ctop_off,
226			 addr + common->cbtm_off);
227
228	/**
229	 * Set interrupt for both the fields in VPIF Register enable channel in
230	 * VPIF register
231	 */
232	channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
233	if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
234		channel0_intr_assert();
235		channel0_intr_enable(1);
236		enable_channel0(1);
237	}
238	if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
239		ycmux_mode == 2) {
240		channel1_intr_assert();
241		channel1_intr_enable(1);
242		enable_channel1(1);
243	}
244
245	return 0;
246
247err:
248	list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
249		list_del(&buf->list);
250		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
251	}
252	spin_unlock_irqrestore(&common->irqlock, flags);
253
254	return ret;
255}
256
257/**
258 * vpif_stop_streaming : Stop the DMA engine
259 * @vq: ptr to vb2_queue
260 *
261 * This callback stops the DMA engine and any remaining buffers
262 * in the DMA queue are released.
263 */
264static void vpif_stop_streaming(struct vb2_queue *vq)
265{
266	struct channel_obj *ch = vb2_get_drv_priv(vq);
267	struct common_obj *common;
268	unsigned long flags;
269	int ret;
270
271	common = &ch->common[VPIF_VIDEO_INDEX];
272
273	/* Disable channel as per its device type and channel id */
274	if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
275		enable_channel0(0);
276		channel0_intr_enable(0);
277	}
278	if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
279		ycmux_mode == 2) {
280		enable_channel1(0);
281		channel1_intr_enable(0);
282	}
283
284	ycmux_mode = 0;
285
286	ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
287	if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
288		vpif_dbg(1, debug, "stream off failed in subdev\n");
289
290	/* release all active buffers */
291	spin_lock_irqsave(&common->irqlock, flags);
292	if (common->cur_frm == common->next_frm) {
293		vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
294				VB2_BUF_STATE_ERROR);
295	} else {
296		if (common->cur_frm != NULL)
297			vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
298					VB2_BUF_STATE_ERROR);
299		if (common->next_frm != NULL)
300			vb2_buffer_done(&common->next_frm->vb.vb2_buf,
301					VB2_BUF_STATE_ERROR);
302	}
303
304	while (!list_empty(&common->dma_queue)) {
305		common->next_frm = list_entry(common->dma_queue.next,
306						struct vpif_cap_buffer, list);
307		list_del(&common->next_frm->list);
308		vb2_buffer_done(&common->next_frm->vb.vb2_buf,
309				VB2_BUF_STATE_ERROR);
310	}
311	spin_unlock_irqrestore(&common->irqlock, flags);
312}
313
314static struct vb2_ops video_qops = {
315	.queue_setup		= vpif_buffer_queue_setup,
316	.buf_prepare		= vpif_buffer_prepare,
317	.start_streaming	= vpif_start_streaming,
318	.stop_streaming		= vpif_stop_streaming,
319	.buf_queue		= vpif_buffer_queue,
320	.wait_prepare		= vb2_ops_wait_prepare,
321	.wait_finish		= vb2_ops_wait_finish,
322};
323
324/**
325 * vpif_process_buffer_complete: process a completed buffer
326 * @common: ptr to common channel object
327 *
328 * This function time stamp the buffer and mark it as DONE. It also
329 * wake up any process waiting on the QUEUE and set the next buffer
330 * as current
331 */
332static void vpif_process_buffer_complete(struct common_obj *common)
333{
334	v4l2_get_timestamp(&common->cur_frm->vb.timestamp);
335	vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
336	/* Make curFrm pointing to nextFrm */
337	common->cur_frm = common->next_frm;
338}
339
340/**
341 * vpif_schedule_next_buffer: set next buffer address for capture
342 * @common : ptr to common channel object
343 *
344 * This function will get next buffer from the dma queue and
345 * set the buffer address in the vpif register for capture.
346 * the buffer is marked active
347 */
348static void vpif_schedule_next_buffer(struct common_obj *common)
349{
350	unsigned long addr = 0;
351
352	spin_lock(&common->irqlock);
353	common->next_frm = list_entry(common->dma_queue.next,
354				     struct vpif_cap_buffer, list);
355	/* Remove that buffer from the buffer queue */
356	list_del(&common->next_frm->list);
357	spin_unlock(&common->irqlock);
358	addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
359
360	/* Set top and bottom field addresses in VPIF registers */
361	common->set_addr(addr + common->ytop_off,
362			 addr + common->ybtm_off,
363			 addr + common->ctop_off,
364			 addr + common->cbtm_off);
365}
366
367/**
368 * vpif_channel_isr : ISR handler for vpif capture
369 * @irq: irq number
370 * @dev_id: dev_id ptr
371 *
372 * It changes status of the captured buffer, takes next buffer from the queue
373 * and sets its address in VPIF registers
374 */
375static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
376{
377	struct vpif_device *dev = &vpif_obj;
378	struct common_obj *common;
379	struct channel_obj *ch;
380	int channel_id = 0;
381	int fid = -1, i;
382
383	channel_id = *(int *)(dev_id);
384	if (!vpif_intr_status(channel_id))
385		return IRQ_NONE;
386
387	ch = dev->dev[channel_id];
388
389	for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
390		common = &ch->common[i];
391		/* skip If streaming is not started in this channel */
392		/* Check the field format */
393		if (1 == ch->vpifparams.std_info.frm_fmt) {
394			/* Progressive mode */
395			spin_lock(&common->irqlock);
396			if (list_empty(&common->dma_queue)) {
397				spin_unlock(&common->irqlock);
398				continue;
399			}
400			spin_unlock(&common->irqlock);
401
402			if (!channel_first_int[i][channel_id])
403				vpif_process_buffer_complete(common);
404
405			channel_first_int[i][channel_id] = 0;
406
407			vpif_schedule_next_buffer(common);
408
409
410			channel_first_int[i][channel_id] = 0;
411		} else {
412			/**
413			 * Interlaced mode. If it is first interrupt, ignore
414			 * it
415			 */
416			if (channel_first_int[i][channel_id]) {
417				channel_first_int[i][channel_id] = 0;
418				continue;
419			}
420			if (0 == i) {
421				ch->field_id ^= 1;
422				/* Get field id from VPIF registers */
423				fid = vpif_channel_getfid(ch->channel_id);
424				if (fid != ch->field_id) {
425					/**
426					 * If field id does not match stored
427					 * field id, make them in sync
428					 */
429					if (0 == fid)
430						ch->field_id = fid;
431					return IRQ_HANDLED;
432				}
433			}
434			/* device field id and local field id are in sync */
435			if (0 == fid) {
436				/* this is even field */
437				if (common->cur_frm == common->next_frm)
438					continue;
439
440				/* mark the current buffer as done */
441				vpif_process_buffer_complete(common);
442			} else if (1 == fid) {
443				/* odd field */
444				spin_lock(&common->irqlock);
445				if (list_empty(&common->dma_queue) ||
446				    (common->cur_frm != common->next_frm)) {
447					spin_unlock(&common->irqlock);
448					continue;
449				}
450				spin_unlock(&common->irqlock);
451
452				vpif_schedule_next_buffer(common);
453			}
454		}
455	}
456	return IRQ_HANDLED;
457}
458
459/**
460 * vpif_update_std_info() - update standard related info
461 * @ch: ptr to channel object
462 *
463 * For a given standard selected by application, update values
464 * in the device data structures
465 */
466static int vpif_update_std_info(struct channel_obj *ch)
467{
468	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
469	struct vpif_params *vpifparams = &ch->vpifparams;
470	const struct vpif_channel_config_params *config;
471	struct vpif_channel_config_params *std_info = &vpifparams->std_info;
472	struct video_obj *vid_ch = &ch->video;
473	int index;
474
475	vpif_dbg(2, debug, "vpif_update_std_info\n");
476
477	for (index = 0; index < vpif_ch_params_count; index++) {
478		config = &vpif_ch_params[index];
479		if (config->hd_sd == 0) {
480			vpif_dbg(2, debug, "SD format\n");
481			if (config->stdid & vid_ch->stdid) {
482				memcpy(std_info, config, sizeof(*config));
483				break;
484			}
485		} else {
486			vpif_dbg(2, debug, "HD format\n");
487			if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
488				sizeof(vid_ch->dv_timings))) {
489				memcpy(std_info, config, sizeof(*config));
490				break;
491			}
492		}
493	}
494
495	/* standard not found */
496	if (index == vpif_ch_params_count)
497		return -EINVAL;
498
499	common->fmt.fmt.pix.width = std_info->width;
500	common->width = std_info->width;
501	common->fmt.fmt.pix.height = std_info->height;
502	common->height = std_info->height;
503	common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
504	common->fmt.fmt.pix.bytesperline = std_info->width;
505	vpifparams->video_params.hpitch = std_info->width;
506	vpifparams->video_params.storage_mode = std_info->frm_fmt;
507
508	if (vid_ch->stdid)
509		common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
510	else
511		common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
512
513	if (ch->vpifparams.std_info.frm_fmt)
514		common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
515	else
516		common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
517
518	if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
519		common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
520	else
521		common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
522
523	common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
524
525	return 0;
526}
527
528/**
529 * vpif_calculate_offsets : This function calculates buffers offsets
530 * @ch : ptr to channel object
531 *
532 * This function calculates buffer offsets for Y and C in the top and
533 * bottom field
534 */
535static void vpif_calculate_offsets(struct channel_obj *ch)
536{
537	unsigned int hpitch, sizeimage;
538	struct video_obj *vid_ch = &(ch->video);
539	struct vpif_params *vpifparams = &ch->vpifparams;
540	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
541	enum v4l2_field field = common->fmt.fmt.pix.field;
542
543	vpif_dbg(2, debug, "vpif_calculate_offsets\n");
544
545	if (V4L2_FIELD_ANY == field) {
546		if (vpifparams->std_info.frm_fmt)
547			vid_ch->buf_field = V4L2_FIELD_NONE;
548		else
549			vid_ch->buf_field = V4L2_FIELD_INTERLACED;
550	} else
551		vid_ch->buf_field = common->fmt.fmt.pix.field;
552
553	sizeimage = common->fmt.fmt.pix.sizeimage;
554
555	hpitch = common->fmt.fmt.pix.bytesperline;
556
557	if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
558	    (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
559		/* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
560		common->ytop_off = 0;
561		common->ybtm_off = hpitch;
562		common->ctop_off = sizeimage / 2;
563		common->cbtm_off = sizeimage / 2 + hpitch;
564	} else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
565		/* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
566		common->ytop_off = 0;
567		common->ybtm_off = sizeimage / 4;
568		common->ctop_off = sizeimage / 2;
569		common->cbtm_off = common->ctop_off + sizeimage / 4;
570	} else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
571		/* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
572		common->ybtm_off = 0;
573		common->ytop_off = sizeimage / 4;
574		common->cbtm_off = sizeimage / 2;
575		common->ctop_off = common->cbtm_off + sizeimage / 4;
576	}
577	if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
578	    (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
579		vpifparams->video_params.storage_mode = 1;
580	else
581		vpifparams->video_params.storage_mode = 0;
582
583	if (1 == vpifparams->std_info.frm_fmt)
584		vpifparams->video_params.hpitch =
585		    common->fmt.fmt.pix.bytesperline;
586	else {
587		if ((field == V4L2_FIELD_ANY)
588		    || (field == V4L2_FIELD_INTERLACED))
589			vpifparams->video_params.hpitch =
590			    common->fmt.fmt.pix.bytesperline * 2;
591		else
592			vpifparams->video_params.hpitch =
593			    common->fmt.fmt.pix.bytesperline;
594	}
595
596	ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
597}
598
599/**
600 * vpif_get_default_field() - Get default field type based on interface
601 * @vpif_params - ptr to vpif params
602 */
603static inline enum v4l2_field vpif_get_default_field(
604				struct vpif_interface *iface)
605{
606	return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
607						V4L2_FIELD_INTERLACED;
608}
609
610/**
611 * vpif_config_addr() - function to configure buffer address in vpif
612 * @ch - channel ptr
613 * @muxmode - channel mux mode
614 */
615static void vpif_config_addr(struct channel_obj *ch, int muxmode)
616{
617	struct common_obj *common;
618
619	vpif_dbg(2, debug, "vpif_config_addr\n");
620
621	common = &(ch->common[VPIF_VIDEO_INDEX]);
622
623	if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
624		common->set_addr = ch1_set_videobuf_addr;
625	else if (2 == muxmode)
626		common->set_addr = ch0_set_videobuf_addr_yc_nmux;
627	else
628		common->set_addr = ch0_set_videobuf_addr;
629}
630
631/**
632 * vpif_input_to_subdev() - Maps input to sub device
633 * @vpif_cfg - global config ptr
634 * @chan_cfg - channel config ptr
635 * @input_index - Given input index from application
636 *
637 * lookup the sub device information for a given input index.
638 * we report all the inputs to application. inputs table also
639 * has sub device name for the each input
640 */
641static int vpif_input_to_subdev(
642		struct vpif_capture_config *vpif_cfg,
643		struct vpif_capture_chan_config *chan_cfg,
644		int input_index)
645{
646	struct vpif_subdev_info *subdev_info;
647	const char *subdev_name;
648	int i;
649
650	vpif_dbg(2, debug, "vpif_input_to_subdev\n");
651
652	subdev_name = chan_cfg->inputs[input_index].subdev_name;
653	if (subdev_name == NULL)
654		return -1;
655
656	/* loop through the sub device list to get the sub device info */
657	for (i = 0; i < vpif_cfg->subdev_count; i++) {
658		subdev_info = &vpif_cfg->subdev_info[i];
659		if (!strcmp(subdev_info->name, subdev_name))
660			return i;
661	}
662	return -1;
663}
664
665/**
666 * vpif_set_input() - Select an input
667 * @vpif_cfg - global config ptr
668 * @ch - channel
669 * @_index - Given input index from application
670 *
671 * Select the given input.
672 */
673static int vpif_set_input(
674		struct vpif_capture_config *vpif_cfg,
675		struct channel_obj *ch,
676		int index)
677{
678	struct vpif_capture_chan_config *chan_cfg =
679			&vpif_cfg->chan_config[ch->channel_id];
680	struct vpif_subdev_info *subdev_info = NULL;
681	struct v4l2_subdev *sd = NULL;
682	u32 input = 0, output = 0;
683	int sd_index;
684	int ret;
685
686	sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
687	if (sd_index >= 0) {
688		sd = vpif_obj.sd[sd_index];
689		subdev_info = &vpif_cfg->subdev_info[sd_index];
690	}
691
692	/* first setup input path from sub device to vpif */
693	if (sd && vpif_cfg->setup_input_path) {
694		ret = vpif_cfg->setup_input_path(ch->channel_id,
695				       subdev_info->name);
696		if (ret < 0) {
697			vpif_dbg(1, debug, "couldn't setup input path for the" \
698			" sub device %s, for input index %d\n",
699			subdev_info->name, index);
700			return ret;
701		}
702	}
703
704	if (sd) {
705		input = chan_cfg->inputs[index].input_route;
706		output = chan_cfg->inputs[index].output_route;
707		ret = v4l2_subdev_call(sd, video, s_routing,
708				input, output, 0);
709		if (ret < 0 && ret != -ENOIOCTLCMD) {
710			vpif_dbg(1, debug, "Failed to set input\n");
711			return ret;
712		}
713	}
714	ch->input_idx = index;
715	ch->sd = sd;
716	/* copy interface parameters to vpif */
717	ch->vpifparams.iface = chan_cfg->vpif_if;
718
719	/* update tvnorms from the sub device input info */
720	ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
721	return 0;
722}
723
724/**
725 * vpif_querystd() - querystd handler
726 * @file: file ptr
727 * @priv: file handle
728 * @std_id: ptr to std id
729 *
730 * This function is called to detect standard at the selected input
731 */
732static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
733{
734	struct video_device *vdev = video_devdata(file);
735	struct channel_obj *ch = video_get_drvdata(vdev);
736	int ret = 0;
737
738	vpif_dbg(2, debug, "vpif_querystd\n");
739
740	/* Call querystd function of decoder device */
741	ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
742
743	if (ret == -ENOIOCTLCMD || ret == -ENODEV)
744		return -ENODATA;
745	if (ret) {
746		vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
747		return ret;
748	}
749
750	return 0;
751}
752
753/**
754 * vpif_g_std() - get STD handler
755 * @file: file ptr
756 * @priv: file handle
757 * @std_id: ptr to std id
758 */
759static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
760{
761	struct vpif_capture_config *config = vpif_dev->platform_data;
762	struct video_device *vdev = video_devdata(file);
763	struct channel_obj *ch = video_get_drvdata(vdev);
764	struct vpif_capture_chan_config *chan_cfg;
765	struct v4l2_input input;
766
767	vpif_dbg(2, debug, "vpif_g_std\n");
768
769	if (config->chan_config[ch->channel_id].inputs == NULL)
770		return -ENODATA;
771
772	chan_cfg = &config->chan_config[ch->channel_id];
773	input = chan_cfg->inputs[ch->input_idx].input;
774	if (input.capabilities != V4L2_IN_CAP_STD)
775		return -ENODATA;
776
777	*std = ch->video.stdid;
778	return 0;
779}
780
781/**
782 * vpif_s_std() - set STD handler
783 * @file: file ptr
784 * @priv: file handle
785 * @std_id: ptr to std id
786 */
787static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
788{
789	struct vpif_capture_config *config = vpif_dev->platform_data;
790	struct video_device *vdev = video_devdata(file);
791	struct channel_obj *ch = video_get_drvdata(vdev);
792	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
793	struct vpif_capture_chan_config *chan_cfg;
794	struct v4l2_input input;
795	int ret;
796
797	vpif_dbg(2, debug, "vpif_s_std\n");
798
799	if (config->chan_config[ch->channel_id].inputs == NULL)
800		return -ENODATA;
801
802	chan_cfg = &config->chan_config[ch->channel_id];
803	input = chan_cfg->inputs[ch->input_idx].input;
804	if (input.capabilities != V4L2_IN_CAP_STD)
805		return -ENODATA;
806
807	if (vb2_is_busy(&common->buffer_queue))
808		return -EBUSY;
809
810	/* Call encoder subdevice function to set the standard */
811	ch->video.stdid = std_id;
812	memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
813
814	/* Get the information about the standard */
815	if (vpif_update_std_info(ch)) {
816		vpif_err("Error getting the standard info\n");
817		return -EINVAL;
818	}
819
820	/* set standard in the sub device */
821	ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
822	if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
823		vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
824		return ret;
825	}
826	return 0;
827}
828
829/**
830 * vpif_enum_input() - ENUMINPUT handler
831 * @file: file ptr
832 * @priv: file handle
833 * @input: ptr to input structure
834 */
835static int vpif_enum_input(struct file *file, void *priv,
836				struct v4l2_input *input)
837{
838
839	struct vpif_capture_config *config = vpif_dev->platform_data;
840	struct video_device *vdev = video_devdata(file);
841	struct channel_obj *ch = video_get_drvdata(vdev);
842	struct vpif_capture_chan_config *chan_cfg;
843
844	chan_cfg = &config->chan_config[ch->channel_id];
845
846	if (input->index >= chan_cfg->input_count)
847		return -EINVAL;
848
849	memcpy(input, &chan_cfg->inputs[input->index].input,
850		sizeof(*input));
851	return 0;
852}
853
854/**
855 * vpif_g_input() - Get INPUT handler
856 * @file: file ptr
857 * @priv: file handle
858 * @index: ptr to input index
859 */
860static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
861{
862	struct video_device *vdev = video_devdata(file);
863	struct channel_obj *ch = video_get_drvdata(vdev);
864
865	*index = ch->input_idx;
866	return 0;
867}
868
869/**
870 * vpif_s_input() - Set INPUT handler
871 * @file: file ptr
872 * @priv: file handle
873 * @index: input index
874 */
875static int vpif_s_input(struct file *file, void *priv, unsigned int index)
876{
877	struct vpif_capture_config *config = vpif_dev->platform_data;
878	struct video_device *vdev = video_devdata(file);
879	struct channel_obj *ch = video_get_drvdata(vdev);
880	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
881	struct vpif_capture_chan_config *chan_cfg;
882
883	chan_cfg = &config->chan_config[ch->channel_id];
884
885	if (index >= chan_cfg->input_count)
886		return -EINVAL;
887
888	if (vb2_is_busy(&common->buffer_queue))
889		return -EBUSY;
890
891	return vpif_set_input(config, ch, index);
892}
893
894/**
895 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
896 * @file: file ptr
897 * @priv: file handle
898 * @index: input index
899 */
900static int vpif_enum_fmt_vid_cap(struct file *file, void  *priv,
901					struct v4l2_fmtdesc *fmt)
902{
903	struct video_device *vdev = video_devdata(file);
904	struct channel_obj *ch = video_get_drvdata(vdev);
905
906	if (fmt->index != 0) {
907		vpif_dbg(1, debug, "Invalid format index\n");
908		return -EINVAL;
909	}
910
911	/* Fill in the information about format */
912	if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
913		fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
914		strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
915		fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
916	} else {
917		fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
918		strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
919		fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
920	}
921	return 0;
922}
923
924/**
925 * vpif_try_fmt_vid_cap() - TRY_FMT handler
926 * @file: file ptr
927 * @priv: file handle
928 * @fmt: ptr to v4l2 format structure
929 */
930static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
931				struct v4l2_format *fmt)
932{
933	struct video_device *vdev = video_devdata(file);
934	struct channel_obj *ch = video_get_drvdata(vdev);
935	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
936	struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
937	struct vpif_params *vpif_params = &ch->vpifparams;
938
939	/*
940	 * to supress v4l-compliance warnings silently correct
941	 * the pixelformat
942	 */
943	if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
944		if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
945			pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
946	} else {
947		if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
948			pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
949	}
950
951	common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
952
953	vpif_update_std_info(ch);
954
955	pixfmt->field = common->fmt.fmt.pix.field;
956	pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
957	pixfmt->bytesperline = common->fmt.fmt.pix.width;
958	pixfmt->width = common->fmt.fmt.pix.width;
959	pixfmt->height = common->fmt.fmt.pix.height;
960	pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
961	pixfmt->priv = 0;
962
963	return 0;
964}
965
966
967/**
968 * vpif_g_fmt_vid_cap() - Set INPUT handler
969 * @file: file ptr
970 * @priv: file handle
971 * @fmt: ptr to v4l2 format structure
972 */
973static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
974				struct v4l2_format *fmt)
975{
976	struct video_device *vdev = video_devdata(file);
977	struct channel_obj *ch = video_get_drvdata(vdev);
978	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
979
980	/* Check the validity of the buffer type */
981	if (common->fmt.type != fmt->type)
982		return -EINVAL;
983
984	/* Fill in the information about format */
985	*fmt = common->fmt;
986	return 0;
987}
988
989/**
990 * vpif_s_fmt_vid_cap() - Set FMT handler
991 * @file: file ptr
992 * @priv: file handle
993 * @fmt: ptr to v4l2 format structure
994 */
995static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
996				struct v4l2_format *fmt)
997{
998	struct video_device *vdev = video_devdata(file);
999	struct channel_obj *ch = video_get_drvdata(vdev);
1000	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1001	int ret;
1002
1003	vpif_dbg(2, debug, "%s\n", __func__);
1004
1005	if (vb2_is_busy(&common->buffer_queue))
1006		return -EBUSY;
1007
1008	ret = vpif_try_fmt_vid_cap(file, priv, fmt);
1009	if (ret)
1010		return ret;
1011
1012	/* store the format in the channel object */
1013	common->fmt = *fmt;
1014	return 0;
1015}
1016
1017/**
1018 * vpif_querycap() - QUERYCAP handler
1019 * @file: file ptr
1020 * @priv: file handle
1021 * @cap: ptr to v4l2_capability structure
1022 */
1023static int vpif_querycap(struct file *file, void  *priv,
1024				struct v4l2_capability *cap)
1025{
1026	struct vpif_capture_config *config = vpif_dev->platform_data;
1027
1028	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1029	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1030	strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1031	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1032		 dev_name(vpif_dev));
1033	strlcpy(cap->card, config->card_name, sizeof(cap->card));
1034
1035	return 0;
1036}
1037
1038/**
1039 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1040 * @file: file ptr
1041 * @priv: file handle
1042 * @timings: input timings
1043 */
1044static int
1045vpif_enum_dv_timings(struct file *file, void *priv,
1046		     struct v4l2_enum_dv_timings *timings)
1047{
1048	struct vpif_capture_config *config = vpif_dev->platform_data;
1049	struct video_device *vdev = video_devdata(file);
1050	struct channel_obj *ch = video_get_drvdata(vdev);
1051	struct vpif_capture_chan_config *chan_cfg;
1052	struct v4l2_input input;
1053	int ret;
1054
1055	if (config->chan_config[ch->channel_id].inputs == NULL)
1056		return -ENODATA;
1057
1058	chan_cfg = &config->chan_config[ch->channel_id];
1059	input = chan_cfg->inputs[ch->input_idx].input;
1060	if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1061		return -ENODATA;
1062
1063	timings->pad = 0;
1064
1065	ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
1066	if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1067		return -EINVAL;
1068
1069	return ret;
1070}
1071
1072/**
1073 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1074 * @file: file ptr
1075 * @priv: file handle
1076 * @timings: input timings
1077 */
1078static int
1079vpif_query_dv_timings(struct file *file, void *priv,
1080		      struct v4l2_dv_timings *timings)
1081{
1082	struct vpif_capture_config *config = vpif_dev->platform_data;
1083	struct video_device *vdev = video_devdata(file);
1084	struct channel_obj *ch = video_get_drvdata(vdev);
1085	struct vpif_capture_chan_config *chan_cfg;
1086	struct v4l2_input input;
1087	int ret;
1088
1089	if (config->chan_config[ch->channel_id].inputs == NULL)
1090		return -ENODATA;
1091
1092	chan_cfg = &config->chan_config[ch->channel_id];
1093	input = chan_cfg->inputs[ch->input_idx].input;
1094	if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1095		return -ENODATA;
1096
1097	ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1098	if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1099		return -ENODATA;
1100
1101	return ret;
1102}
1103
1104/**
1105 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1106 * @file: file ptr
1107 * @priv: file handle
1108 * @timings: digital video timings
1109 */
1110static int vpif_s_dv_timings(struct file *file, void *priv,
1111		struct v4l2_dv_timings *timings)
1112{
1113	struct vpif_capture_config *config = vpif_dev->platform_data;
1114	struct video_device *vdev = video_devdata(file);
1115	struct channel_obj *ch = video_get_drvdata(vdev);
1116	struct vpif_params *vpifparams = &ch->vpifparams;
1117	struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1118	struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1119	struct video_obj *vid_ch = &ch->video;
1120	struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1121	struct vpif_capture_chan_config *chan_cfg;
1122	struct v4l2_input input;
1123	int ret;
1124
1125	if (config->chan_config[ch->channel_id].inputs == NULL)
1126		return -ENODATA;
1127
1128	chan_cfg = &config->chan_config[ch->channel_id];
1129	input = chan_cfg->inputs[ch->input_idx].input;
1130	if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1131		return -ENODATA;
1132
1133	if (timings->type != V4L2_DV_BT_656_1120) {
1134		vpif_dbg(2, debug, "Timing type not defined\n");
1135		return -EINVAL;
1136	}
1137
1138	if (vb2_is_busy(&common->buffer_queue))
1139		return -EBUSY;
1140
1141	/* Configure subdevice timings, if any */
1142	ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1143	if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1144		ret = 0;
1145	if (ret < 0) {
1146		vpif_dbg(2, debug, "Error setting custom DV timings\n");
1147		return ret;
1148	}
1149
1150	if (!(timings->bt.width && timings->bt.height &&
1151				(timings->bt.hbackporch ||
1152				 timings->bt.hfrontporch ||
1153				 timings->bt.hsync) &&
1154				timings->bt.vfrontporch &&
1155				(timings->bt.vbackporch ||
1156				 timings->bt.vsync))) {
1157		vpif_dbg(2, debug, "Timings for width, height, "
1158				"horizontal back porch, horizontal sync, "
1159				"horizontal front porch, vertical back porch, "
1160				"vertical sync and vertical back porch "
1161				"must be defined\n");
1162		return -EINVAL;
1163	}
1164
1165	vid_ch->dv_timings = *timings;
1166
1167	/* Configure video port timings */
1168
1169	std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1170	std_info->sav2eav = bt->width;
1171
1172	std_info->l1 = 1;
1173	std_info->l3 = bt->vsync + bt->vbackporch + 1;
1174
1175	std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1176	if (bt->interlaced) {
1177		if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1178			std_info->l5 = std_info->vsize/2 -
1179				(bt->vfrontporch - 1);
1180			std_info->l7 = std_info->vsize/2 + 1;
1181			std_info->l9 = std_info->l7 + bt->il_vsync +
1182				bt->il_vbackporch + 1;
1183			std_info->l11 = std_info->vsize -
1184				(bt->il_vfrontporch - 1);
1185		} else {
1186			vpif_dbg(2, debug, "Required timing values for "
1187					"interlaced BT format missing\n");
1188			return -EINVAL;
1189		}
1190	} else {
1191		std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1192	}
1193	strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1194	std_info->width = bt->width;
1195	std_info->height = bt->height;
1196	std_info->frm_fmt = bt->interlaced ? 0 : 1;
1197	std_info->ycmux_mode = 0;
1198	std_info->capture_format = 0;
1199	std_info->vbi_supported = 0;
1200	std_info->hd_sd = 1;
1201	std_info->stdid = 0;
1202
1203	vid_ch->stdid = 0;
1204	return 0;
1205}
1206
1207/**
1208 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1209 * @file: file ptr
1210 * @priv: file handle
1211 * @timings: digital video timings
1212 */
1213static int vpif_g_dv_timings(struct file *file, void *priv,
1214		struct v4l2_dv_timings *timings)
1215{
1216	struct vpif_capture_config *config = vpif_dev->platform_data;
1217	struct video_device *vdev = video_devdata(file);
1218	struct channel_obj *ch = video_get_drvdata(vdev);
1219	struct video_obj *vid_ch = &ch->video;
1220	struct vpif_capture_chan_config *chan_cfg;
1221	struct v4l2_input input;
1222
1223	if (config->chan_config[ch->channel_id].inputs == NULL)
1224		return -ENODATA;
1225
1226	chan_cfg = &config->chan_config[ch->channel_id];
1227	input = chan_cfg->inputs[ch->input_idx].input;
1228	if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1229		return -ENODATA;
1230
1231	*timings = vid_ch->dv_timings;
1232
1233	return 0;
1234}
1235
1236/*
1237 * vpif_log_status() - Status information
1238 * @file: file ptr
1239 * @priv: file handle
1240 *
1241 * Returns zero.
1242 */
1243static int vpif_log_status(struct file *filep, void *priv)
1244{
1245	/* status for sub devices */
1246	v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1247
1248	return 0;
1249}
1250
1251/* vpif capture ioctl operations */
1252static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1253	.vidioc_querycap		= vpif_querycap,
1254	.vidioc_enum_fmt_vid_cap	= vpif_enum_fmt_vid_cap,
1255	.vidioc_g_fmt_vid_cap		= vpif_g_fmt_vid_cap,
1256	.vidioc_s_fmt_vid_cap		= vpif_s_fmt_vid_cap,
1257	.vidioc_try_fmt_vid_cap		= vpif_try_fmt_vid_cap,
1258
1259	.vidioc_enum_input		= vpif_enum_input,
1260	.vidioc_s_input			= vpif_s_input,
1261	.vidioc_g_input			= vpif_g_input,
1262
1263	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
1264	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
1265	.vidioc_querybuf		= vb2_ioctl_querybuf,
1266	.vidioc_qbuf			= vb2_ioctl_qbuf,
1267	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1268	.vidioc_expbuf			= vb2_ioctl_expbuf,
1269	.vidioc_streamon		= vb2_ioctl_streamon,
1270	.vidioc_streamoff		= vb2_ioctl_streamoff,
1271
1272	.vidioc_querystd		= vpif_querystd,
1273	.vidioc_s_std			= vpif_s_std,
1274	.vidioc_g_std			= vpif_g_std,
1275
1276	.vidioc_enum_dv_timings		= vpif_enum_dv_timings,
1277	.vidioc_query_dv_timings	= vpif_query_dv_timings,
1278	.vidioc_s_dv_timings		= vpif_s_dv_timings,
1279	.vidioc_g_dv_timings		= vpif_g_dv_timings,
1280
1281	.vidioc_log_status		= vpif_log_status,
1282};
1283
1284/* vpif file operations */
1285static struct v4l2_file_operations vpif_fops = {
1286	.owner = THIS_MODULE,
1287	.open = v4l2_fh_open,
1288	.release = vb2_fop_release,
1289	.unlocked_ioctl = video_ioctl2,
1290	.mmap = vb2_fop_mmap,
1291	.poll = vb2_fop_poll
1292};
1293
1294/**
1295 * initialize_vpif() - Initialize vpif data structures
1296 *
1297 * Allocate memory for data structures and initialize them
1298 */
1299static int initialize_vpif(void)
1300{
1301	int err, i, j;
1302	int free_channel_objects_index;
1303
1304	/* Allocate memory for six channel objects */
1305	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1306		vpif_obj.dev[i] =
1307		    kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1308		/* If memory allocation fails, return error */
1309		if (!vpif_obj.dev[i]) {
1310			free_channel_objects_index = i;
1311			err = -ENOMEM;
1312			goto vpif_init_free_channel_objects;
1313		}
1314	}
1315	return 0;
1316
1317vpif_init_free_channel_objects:
1318	for (j = 0; j < free_channel_objects_index; j++)
1319		kfree(vpif_obj.dev[j]);
1320	return err;
1321}
1322
1323static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1324			    struct v4l2_subdev *subdev,
1325			    struct v4l2_async_subdev *asd)
1326{
1327	int i;
1328
1329	for (i = 0; i < vpif_obj.config->subdev_count; i++)
1330		if (!strcmp(vpif_obj.config->subdev_info[i].name,
1331			    subdev->name)) {
1332			vpif_obj.sd[i] = subdev;
1333			return 0;
1334		}
1335
1336	return -EINVAL;
1337}
1338
1339static int vpif_probe_complete(void)
1340{
1341	struct common_obj *common;
1342	struct video_device *vdev;
1343	struct channel_obj *ch;
1344	struct vb2_queue *q;
1345	int j, err, k;
1346
1347	for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1348		ch = vpif_obj.dev[j];
1349		ch->channel_id = j;
1350		common = &(ch->common[VPIF_VIDEO_INDEX]);
1351		spin_lock_init(&common->irqlock);
1352		mutex_init(&common->lock);
1353
1354		/* select input 0 */
1355		err = vpif_set_input(vpif_obj.config, ch, 0);
1356		if (err)
1357			goto probe_out;
1358
1359		/* set initial format */
1360		ch->video.stdid = V4L2_STD_525_60;
1361		memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1362		vpif_update_std_info(ch);
1363
1364		/* Initialize vb2 queue */
1365		q = &common->buffer_queue;
1366		q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1367		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1368		q->drv_priv = ch;
1369		q->ops = &video_qops;
1370		q->mem_ops = &vb2_dma_contig_memops;
1371		q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1372		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1373		q->min_buffers_needed = 1;
1374		q->lock = &common->lock;
1375
1376		err = vb2_queue_init(q);
1377		if (err) {
1378			vpif_err("vpif_capture: vb2_queue_init() failed\n");
1379			goto probe_out;
1380		}
1381
1382		common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1383		if (IS_ERR(common->alloc_ctx)) {
1384			vpif_err("Failed to get the context\n");
1385			err = PTR_ERR(common->alloc_ctx);
1386			goto probe_out;
1387		}
1388
1389		INIT_LIST_HEAD(&common->dma_queue);
1390
1391		/* Initialize the video_device structure */
1392		vdev = &ch->video_dev;
1393		strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1394		vdev->release = video_device_release_empty;
1395		vdev->fops = &vpif_fops;
1396		vdev->ioctl_ops = &vpif_ioctl_ops;
1397		vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1398		vdev->vfl_dir = VFL_DIR_RX;
1399		vdev->queue = q;
1400		vdev->lock = &common->lock;
1401		video_set_drvdata(&ch->video_dev, ch);
1402		err = video_register_device(vdev,
1403					    VFL_TYPE_GRABBER, (j ? 1 : 0));
1404		if (err)
1405			goto probe_out;
1406	}
1407
1408	v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1409	return 0;
1410
1411probe_out:
1412	for (k = 0; k < j; k++) {
1413		/* Get the pointer to the channel object */
1414		ch = vpif_obj.dev[k];
1415		common = &ch->common[k];
1416		vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1417		/* Unregister video device */
1418		video_unregister_device(&ch->video_dev);
1419	}
1420	kfree(vpif_obj.sd);
1421	v4l2_device_unregister(&vpif_obj.v4l2_dev);
1422
1423	return err;
1424}
1425
1426static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1427{
1428	return vpif_probe_complete();
1429}
1430
1431/**
1432 * vpif_probe : This function probes the vpif capture driver
1433 * @pdev: platform device pointer
1434 *
1435 * This creates device entries by register itself to the V4L2 driver and
1436 * initializes fields of each channel objects
1437 */
1438static __init int vpif_probe(struct platform_device *pdev)
1439{
1440	struct vpif_subdev_info *subdevdata;
1441	struct i2c_adapter *i2c_adap;
1442	struct resource *res;
1443	int subdev_count;
1444	int res_idx = 0;
1445	int i, err;
1446
1447	vpif_dev = &pdev->dev;
1448
1449	err = initialize_vpif();
1450	if (err) {
1451		v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1452		return err;
1453	}
1454
1455	err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1456	if (err) {
1457		v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1458		return err;
1459	}
1460
1461	while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1462		err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1463					IRQF_SHARED, VPIF_DRIVER_NAME,
1464					(void *)(&vpif_obj.dev[res_idx]->
1465					channel_id));
1466		if (err) {
1467			err = -EINVAL;
1468			goto vpif_unregister;
1469		}
1470		res_idx++;
1471	}
1472
1473	vpif_obj.config = pdev->dev.platform_data;
1474
1475	subdev_count = vpif_obj.config->subdev_count;
1476	vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1477				GFP_KERNEL);
1478	if (vpif_obj.sd == NULL) {
1479		vpif_err("unable to allocate memory for subdevice pointers\n");
1480		err = -ENOMEM;
1481		goto vpif_unregister;
1482	}
1483
1484	if (!vpif_obj.config->asd_sizes) {
1485		i2c_adap = i2c_get_adapter(1);
1486		for (i = 0; i < subdev_count; i++) {
1487			subdevdata = &vpif_obj.config->subdev_info[i];
1488			vpif_obj.sd[i] =
1489				v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1490							  i2c_adap,
1491							  &subdevdata->
1492							  board_info,
1493							  NULL);
1494
1495			if (!vpif_obj.sd[i]) {
1496				vpif_err("Error registering v4l2 subdevice\n");
1497				err = -ENODEV;
1498				goto probe_subdev_out;
1499			}
1500			v4l2_info(&vpif_obj.v4l2_dev,
1501				  "registered sub device %s\n",
1502				   subdevdata->name);
1503		}
1504		vpif_probe_complete();
1505	} else {
1506		vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1507		vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1508		vpif_obj.notifier.bound = vpif_async_bound;
1509		vpif_obj.notifier.complete = vpif_async_complete;
1510		err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1511						   &vpif_obj.notifier);
1512		if (err) {
1513			vpif_err("Error registering async notifier\n");
1514			err = -EINVAL;
1515			goto probe_subdev_out;
1516		}
1517	}
1518
1519	return 0;
1520
1521probe_subdev_out:
1522	/* free sub devices memory */
1523	kfree(vpif_obj.sd);
1524vpif_unregister:
1525	v4l2_device_unregister(&vpif_obj.v4l2_dev);
1526
1527	return err;
1528}
1529
1530/**
1531 * vpif_remove() - driver remove handler
1532 * @device: ptr to platform device structure
1533 *
1534 * The vidoe device is unregistered
1535 */
1536static int vpif_remove(struct platform_device *device)
1537{
1538	struct common_obj *common;
1539	struct channel_obj *ch;
1540	int i;
1541
1542	v4l2_device_unregister(&vpif_obj.v4l2_dev);
1543
1544	kfree(vpif_obj.sd);
1545	/* un-register device */
1546	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1547		/* Get the pointer to the channel object */
1548		ch = vpif_obj.dev[i];
1549		common = &ch->common[VPIF_VIDEO_INDEX];
1550		vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1551		/* Unregister video device */
1552		video_unregister_device(&ch->video_dev);
1553		kfree(vpif_obj.dev[i]);
1554	}
1555	return 0;
1556}
1557
1558#ifdef CONFIG_PM_SLEEP
1559/**
1560 * vpif_suspend: vpif device suspend
1561 */
1562static int vpif_suspend(struct device *dev)
1563{
1564
1565	struct common_obj *common;
1566	struct channel_obj *ch;
1567	int i;
1568
1569	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1570		/* Get the pointer to the channel object */
1571		ch = vpif_obj.dev[i];
1572		common = &ch->common[VPIF_VIDEO_INDEX];
1573
1574		if (!vb2_start_streaming_called(&common->buffer_queue))
1575			continue;
1576
1577		mutex_lock(&common->lock);
1578		/* Disable channel */
1579		if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1580			enable_channel0(0);
1581			channel0_intr_enable(0);
1582		}
1583		if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1584			ycmux_mode == 2) {
1585			enable_channel1(0);
1586			channel1_intr_enable(0);
1587		}
1588		mutex_unlock(&common->lock);
1589	}
1590
1591	return 0;
1592}
1593
1594/*
1595 * vpif_resume: vpif device suspend
1596 */
1597static int vpif_resume(struct device *dev)
1598{
1599	struct common_obj *common;
1600	struct channel_obj *ch;
1601	int i;
1602
1603	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1604		/* Get the pointer to the channel object */
1605		ch = vpif_obj.dev[i];
1606		common = &ch->common[VPIF_VIDEO_INDEX];
1607
1608		if (!vb2_start_streaming_called(&common->buffer_queue))
1609			continue;
1610
1611		mutex_lock(&common->lock);
1612		/* Enable channel */
1613		if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1614			enable_channel0(1);
1615			channel0_intr_enable(1);
1616		}
1617		if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1618			ycmux_mode == 2) {
1619			enable_channel1(1);
1620			channel1_intr_enable(1);
1621		}
1622		mutex_unlock(&common->lock);
1623	}
1624
1625	return 0;
1626}
1627#endif
1628
1629static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1630
1631static __refdata struct platform_driver vpif_driver = {
1632	.driver	= {
1633		.name	= VPIF_DRIVER_NAME,
1634		.pm	= &vpif_pm_ops,
1635	},
1636	.probe = vpif_probe,
1637	.remove = vpif_remove,
1638};
1639
1640module_platform_driver(vpif_driver);
1641