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