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