1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * TI OMAP4 ISS V4L2 Driver - Generic video node
4  *
5  * Copyright (C) 2012 Texas Instruments, Inc.
6  *
7  * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/module.h>
17 
18 #include <media/v4l2-dev.h>
19 #include <media/v4l2-ioctl.h>
20 #include <media/v4l2-mc.h>
21 
22 #include "iss_video.h"
23 #include "iss.h"
24 
25 /* -----------------------------------------------------------------------------
26  * Helper functions
27  */
28 
29 static struct iss_format_info formats[] = {
30 	{ MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
31 	  MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
32 	  V4L2_PIX_FMT_GREY, 8, },
33 	{ MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
34 	  MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
35 	  V4L2_PIX_FMT_Y10, 10, },
36 	{ MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
37 	  MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
38 	  V4L2_PIX_FMT_Y12, 12, },
39 	{ MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
40 	  MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
41 	  V4L2_PIX_FMT_SBGGR8, 8, },
42 	{ MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
43 	  MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
44 	  V4L2_PIX_FMT_SGBRG8, 8, },
45 	{ MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
46 	  MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
47 	  V4L2_PIX_FMT_SGRBG8, 8, },
48 	{ MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
49 	  MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
50 	  V4L2_PIX_FMT_SRGGB8, 8, },
51 	{ MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
52 	  MEDIA_BUS_FMT_SGRBG10_1X10, 0,
53 	  V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
54 	{ MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
55 	  MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
56 	  V4L2_PIX_FMT_SBGGR10, 10, },
57 	{ MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
58 	  MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
59 	  V4L2_PIX_FMT_SGBRG10, 10, },
60 	{ MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
61 	  MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
62 	  V4L2_PIX_FMT_SGRBG10, 10, },
63 	{ MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
64 	  MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
65 	  V4L2_PIX_FMT_SRGGB10, 10, },
66 	{ MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
67 	  MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
68 	  V4L2_PIX_FMT_SBGGR12, 12, },
69 	{ MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
70 	  MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
71 	  V4L2_PIX_FMT_SGBRG12, 12, },
72 	{ MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
73 	  MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
74 	  V4L2_PIX_FMT_SGRBG12, 12, },
75 	{ MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
76 	  MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
77 	  V4L2_PIX_FMT_SRGGB12, 12, },
78 	{ MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
79 	  MEDIA_BUS_FMT_UYVY8_1X16, 0,
80 	  V4L2_PIX_FMT_UYVY, 16, },
81 	{ MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
82 	  MEDIA_BUS_FMT_YUYV8_1X16, 0,
83 	  V4L2_PIX_FMT_YUYV, 16, },
84 	{ MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8,
85 	  MEDIA_BUS_FMT_YUYV8_1_5X8, 0,
86 	  V4L2_PIX_FMT_NV12, 8, },
87 };
88 
89 const struct iss_format_info *
90 omap4iss_video_format_info(u32 code)
91 {
92 	unsigned int i;
93 
94 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
95 		if (formats[i].code == code)
96 			return &formats[i];
97 	}
98 
99 	return NULL;
100 }
101 
102 /*
103  * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
104  * @video: ISS video instance
105  * @mbus: v4l2_mbus_framefmt format (input)
106  * @pix: v4l2_pix_format format (output)
107  *
108  * Fill the output pix structure with information from the input mbus format.
109  * The bytesperline and sizeimage fields are computed from the requested bytes
110  * per line value in the pix format and information from the video instance.
111  *
112  * Return the number of padding bytes at end of line.
113  */
114 static unsigned int iss_video_mbus_to_pix(const struct iss_video *video,
115 					  const struct v4l2_mbus_framefmt *mbus,
116 					  struct v4l2_pix_format *pix)
117 {
118 	unsigned int bpl = pix->bytesperline;
119 	unsigned int min_bpl;
120 	unsigned int i;
121 
122 	memset(pix, 0, sizeof(*pix));
123 	pix->width = mbus->width;
124 	pix->height = mbus->height;
125 
126 	/*
127 	 * Skip the last format in the loop so that it will be selected if no
128 	 * match is found.
129 	 */
130 	for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
131 		if (formats[i].code == mbus->code)
132 			break;
133 	}
134 
135 	min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
136 
137 	/*
138 	 * Clamp the requested bytes per line value. If the maximum bytes per
139 	 * line value is zero, the module doesn't support user configurable line
140 	 * sizes. Override the requested value with the minimum in that case.
141 	 */
142 	if (video->bpl_max)
143 		bpl = clamp(bpl, min_bpl, video->bpl_max);
144 	else
145 		bpl = min_bpl;
146 
147 	if (!video->bpl_zero_padding || bpl != min_bpl)
148 		bpl = ALIGN(bpl, video->bpl_alignment);
149 
150 	pix->pixelformat = formats[i].pixelformat;
151 	pix->bytesperline = bpl;
152 	pix->sizeimage = pix->bytesperline * pix->height;
153 	pix->colorspace = mbus->colorspace;
154 	pix->field = mbus->field;
155 
156 	/* FIXME: Special case for NV12! We should make this nicer... */
157 	if (pix->pixelformat == V4L2_PIX_FMT_NV12)
158 		pix->sizeimage += (pix->bytesperline * pix->height) / 2;
159 
160 	return bpl - min_bpl;
161 }
162 
163 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
164 				  struct v4l2_mbus_framefmt *mbus)
165 {
166 	unsigned int i;
167 
168 	memset(mbus, 0, sizeof(*mbus));
169 	mbus->width = pix->width;
170 	mbus->height = pix->height;
171 
172 	/*
173 	 * Skip the last format in the loop so that it will be selected if no
174 	 * match is found.
175 	 */
176 	for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
177 		if (formats[i].pixelformat == pix->pixelformat)
178 			break;
179 	}
180 
181 	mbus->code = formats[i].code;
182 	mbus->colorspace = pix->colorspace;
183 	mbus->field = pix->field;
184 }
185 
186 static struct v4l2_subdev *
187 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
188 {
189 	struct media_pad *remote;
190 
191 	remote = media_pad_remote_pad_first(&video->pad);
192 
193 	if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
194 		return NULL;
195 
196 	if (pad)
197 		*pad = remote->index;
198 
199 	return media_entity_to_v4l2_subdev(remote->entity);
200 }
201 
202 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
203 static struct iss_video *
204 iss_video_far_end(struct iss_video *video, struct iss_pipeline *pipe)
205 {
206 	struct media_pipeline_entity_iter iter;
207 	struct media_entity *entity;
208 	struct iss_video *far_end = NULL;
209 	int ret;
210 
211 	ret = media_pipeline_entity_iter_init(&pipe->pipe, &iter);
212 	if (ret)
213 		return ERR_PTR(-ENOMEM);
214 
215 	media_pipeline_for_each_entity(&pipe->pipe, &iter, entity) {
216 		struct iss_video *other;
217 
218 		if (entity == &video->video.entity)
219 			continue;
220 
221 		if (!is_media_entity_v4l2_video_device(entity))
222 			continue;
223 
224 		other = to_iss_video(media_entity_to_video_device(entity));
225 		if (other->type != video->type) {
226 			far_end = other;
227 			break;
228 		}
229 	}
230 
231 	media_pipeline_entity_iter_cleanup(&iter);
232 
233 	return far_end;
234 }
235 
236 static int
237 __iss_video_get_format(struct iss_video *video,
238 		       struct v4l2_mbus_framefmt *format)
239 {
240 	struct v4l2_subdev_format fmt;
241 	struct v4l2_subdev *subdev;
242 	u32 pad;
243 	int ret;
244 
245 	subdev = iss_video_remote_subdev(video, &pad);
246 	if (!subdev)
247 		return -EINVAL;
248 
249 	memset(&fmt, 0, sizeof(fmt));
250 	fmt.pad = pad;
251 	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
252 
253 	mutex_lock(&video->mutex);
254 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
255 	mutex_unlock(&video->mutex);
256 
257 	if (ret)
258 		return ret;
259 
260 	*format = fmt.format;
261 	return 0;
262 }
263 
264 static int
265 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
266 {
267 	struct v4l2_mbus_framefmt format;
268 	struct v4l2_pix_format pixfmt;
269 	int ret;
270 
271 	ret = __iss_video_get_format(video, &format);
272 	if (ret < 0)
273 		return ret;
274 
275 	pixfmt.bytesperline = 0;
276 	ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
277 
278 	if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
279 	    vfh->format.fmt.pix.height != pixfmt.height ||
280 	    vfh->format.fmt.pix.width != pixfmt.width ||
281 	    vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
282 	    vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
283 		return -EINVAL;
284 
285 	return ret;
286 }
287 
288 /* -----------------------------------------------------------------------------
289  * Video queue operations
290  */
291 
292 static int iss_video_queue_setup(struct vb2_queue *vq,
293 				 unsigned int *count, unsigned int *num_planes,
294 				 unsigned int sizes[],
295 				 struct device *alloc_devs[])
296 {
297 	struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
298 	struct iss_video *video = vfh->video;
299 
300 	/* Revisit multi-planar support for NV12 */
301 	*num_planes = 1;
302 
303 	sizes[0] = vfh->format.fmt.pix.sizeimage;
304 	if (sizes[0] == 0)
305 		return -EINVAL;
306 
307 	*count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
308 
309 	return 0;
310 }
311 
312 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
313 {
314 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
315 	struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
316 
317 	if (buffer->iss_addr)
318 		buffer->iss_addr = 0;
319 }
320 
321 static int iss_video_buf_prepare(struct vb2_buffer *vb)
322 {
323 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
324 	struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
325 	struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
326 	struct iss_video *video = vfh->video;
327 	unsigned long size = vfh->format.fmt.pix.sizeimage;
328 	dma_addr_t addr;
329 
330 	if (vb2_plane_size(vb, 0) < size)
331 		return -ENOBUFS;
332 
333 	addr = vb2_dma_contig_plane_dma_addr(vb, 0);
334 	if (!IS_ALIGNED(addr, 32)) {
335 		dev_dbg(video->iss->dev,
336 			"Buffer address must be aligned to 32 bytes boundary.\n");
337 		return -EINVAL;
338 	}
339 
340 	vb2_set_plane_payload(vb, 0, size);
341 	buffer->iss_addr = addr;
342 	return 0;
343 }
344 
345 static void iss_video_buf_queue(struct vb2_buffer *vb)
346 {
347 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
348 	struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
349 	struct iss_video *video = vfh->video;
350 	struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
351 	struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
352 	unsigned long flags;
353 	bool empty;
354 
355 	spin_lock_irqsave(&video->qlock, flags);
356 
357 	/*
358 	 * Mark the buffer is faulty and give it back to the queue immediately
359 	 * if the video node has registered an error. vb2 will perform the same
360 	 * check when preparing the buffer, but that is inherently racy, so we
361 	 * need to handle the race condition with an authoritative check here.
362 	 */
363 	if (unlikely(video->error)) {
364 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
365 		spin_unlock_irqrestore(&video->qlock, flags);
366 		return;
367 	}
368 
369 	empty = list_empty(&video->dmaqueue);
370 	list_add_tail(&buffer->list, &video->dmaqueue);
371 
372 	spin_unlock_irqrestore(&video->qlock, flags);
373 
374 	if (empty) {
375 		enum iss_pipeline_state state;
376 		unsigned int start;
377 
378 		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
379 			state = ISS_PIPELINE_QUEUE_OUTPUT;
380 		else
381 			state = ISS_PIPELINE_QUEUE_INPUT;
382 
383 		spin_lock_irqsave(&pipe->lock, flags);
384 		pipe->state |= state;
385 		video->ops->queue(video, buffer);
386 		video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
387 
388 		start = iss_pipeline_ready(pipe);
389 		if (start)
390 			pipe->state |= ISS_PIPELINE_STREAM;
391 		spin_unlock_irqrestore(&pipe->lock, flags);
392 
393 		if (start)
394 			omap4iss_pipeline_set_stream(pipe,
395 						     ISS_PIPELINE_STREAM_SINGLESHOT);
396 	}
397 }
398 
399 static const struct vb2_ops iss_video_vb2ops = {
400 	.queue_setup	= iss_video_queue_setup,
401 	.buf_prepare	= iss_video_buf_prepare,
402 	.buf_queue	= iss_video_buf_queue,
403 	.buf_cleanup	= iss_video_buf_cleanup,
404 };
405 
406 /*
407  * omap4iss_video_buffer_next - Complete the current buffer and return the next
408  * @video: ISS video object
409  *
410  * Remove the current video buffer from the DMA queue and fill its timestamp,
411  * field count and state fields before waking up its completion handler.
412  *
413  * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
414  * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
415  *
416  * The DMA queue is expected to contain at least one buffer.
417  *
418  * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
419  * empty.
420  */
421 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
422 {
423 	struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
424 	enum iss_pipeline_state state;
425 	struct iss_buffer *buf;
426 	unsigned long flags;
427 
428 	spin_lock_irqsave(&video->qlock, flags);
429 	if (WARN_ON(list_empty(&video->dmaqueue))) {
430 		spin_unlock_irqrestore(&video->qlock, flags);
431 		return NULL;
432 	}
433 
434 	buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
435 			       list);
436 	list_del(&buf->list);
437 	spin_unlock_irqrestore(&video->qlock, flags);
438 
439 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
440 
441 	/*
442 	 * Do frame number propagation only if this is the output video node.
443 	 * Frame number either comes from the CSI receivers or it gets
444 	 * incremented here if H3A is not active.
445 	 * Note: There is no guarantee that the output buffer will finish
446 	 * first, so the input number might lag behind by 1 in some cases.
447 	 */
448 	if (video == pipe->output && !pipe->do_propagation)
449 		buf->vb.sequence =
450 			atomic_inc_return(&pipe->frame_number);
451 	else
452 		buf->vb.sequence = atomic_read(&pipe->frame_number);
453 
454 	vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
455 			VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
456 	pipe->error = false;
457 
458 	spin_lock_irqsave(&video->qlock, flags);
459 	if (list_empty(&video->dmaqueue)) {
460 		spin_unlock_irqrestore(&video->qlock, flags);
461 		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
462 			state = ISS_PIPELINE_QUEUE_OUTPUT
463 			      | ISS_PIPELINE_STREAM;
464 		else
465 			state = ISS_PIPELINE_QUEUE_INPUT
466 			      | ISS_PIPELINE_STREAM;
467 
468 		spin_lock_irqsave(&pipe->lock, flags);
469 		pipe->state &= ~state;
470 		if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
471 			video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
472 		spin_unlock_irqrestore(&pipe->lock, flags);
473 		return NULL;
474 	}
475 
476 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
477 		spin_lock(&pipe->lock);
478 		pipe->state &= ~ISS_PIPELINE_STREAM;
479 		spin_unlock(&pipe->lock);
480 	}
481 
482 	buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
483 			       list);
484 	spin_unlock_irqrestore(&video->qlock, flags);
485 	buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
486 	return buf;
487 }
488 
489 /*
490  * omap4iss_video_cancel_stream - Cancel stream on a video node
491  * @video: ISS video object
492  *
493  * Cancelling a stream mark all buffers on the video node as erroneous and makes
494  * sure no new buffer can be queued.
495  */
496 void omap4iss_video_cancel_stream(struct iss_video *video)
497 {
498 	unsigned long flags;
499 
500 	spin_lock_irqsave(&video->qlock, flags);
501 
502 	while (!list_empty(&video->dmaqueue)) {
503 		struct iss_buffer *buf;
504 
505 		buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
506 				       list);
507 		list_del(&buf->list);
508 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
509 	}
510 
511 	vb2_queue_error(video->queue);
512 	video->error = true;
513 
514 	spin_unlock_irqrestore(&video->qlock, flags);
515 }
516 
517 /* -----------------------------------------------------------------------------
518  * V4L2 ioctls
519  */
520 
521 static int
522 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
523 {
524 	struct iss_video *video = video_drvdata(file);
525 
526 	strscpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
527 	strscpy(cap->card, video->video.name, sizeof(cap->card));
528 	strscpy(cap->bus_info, "media", sizeof(cap->bus_info));
529 	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
530 			  | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
531 
532 	return 0;
533 }
534 
535 static int
536 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
537 {
538 	struct iss_video *video = video_drvdata(file);
539 	struct v4l2_mbus_framefmt format;
540 	unsigned int index = f->index;
541 	unsigned int i;
542 	int ret;
543 
544 	if (f->type != video->type)
545 		return -EINVAL;
546 
547 	ret = __iss_video_get_format(video, &format);
548 	if (ret < 0)
549 		return ret;
550 
551 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
552 		const struct iss_format_info *info = &formats[i];
553 
554 		if (format.code != info->code)
555 			continue;
556 
557 		if (index == 0) {
558 			f->pixelformat = info->pixelformat;
559 			return 0;
560 		}
561 
562 		index--;
563 	}
564 
565 	return -EINVAL;
566 }
567 
568 static int
569 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
570 {
571 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
572 	struct iss_video *video = video_drvdata(file);
573 
574 	if (format->type != video->type)
575 		return -EINVAL;
576 
577 	mutex_lock(&video->mutex);
578 	*format = vfh->format;
579 	mutex_unlock(&video->mutex);
580 
581 	return 0;
582 }
583 
584 static int
585 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
586 {
587 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
588 	struct iss_video *video = video_drvdata(file);
589 	struct v4l2_mbus_framefmt fmt;
590 
591 	if (format->type != video->type)
592 		return -EINVAL;
593 
594 	mutex_lock(&video->mutex);
595 
596 	/*
597 	 * Fill the bytesperline and sizeimage fields by converting to media bus
598 	 * format and back to pixel format.
599 	 */
600 	iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
601 	iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
602 
603 	vfh->format = *format;
604 
605 	mutex_unlock(&video->mutex);
606 	return 0;
607 }
608 
609 static int
610 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
611 {
612 	struct iss_video *video = video_drvdata(file);
613 	struct v4l2_subdev_format fmt;
614 	struct v4l2_subdev *subdev;
615 	u32 pad;
616 	int ret;
617 
618 	if (format->type != video->type)
619 		return -EINVAL;
620 
621 	subdev = iss_video_remote_subdev(video, &pad);
622 	if (!subdev)
623 		return -EINVAL;
624 
625 	iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
626 
627 	fmt.pad = pad;
628 	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
629 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
630 	if (ret)
631 		return ret;
632 
633 	iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
634 	return 0;
635 }
636 
637 static int
638 iss_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
639 {
640 	struct iss_video *video = video_drvdata(file);
641 	struct v4l2_subdev_format format;
642 	struct v4l2_subdev *subdev;
643 	struct v4l2_subdev_selection sdsel = {
644 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
645 		.target = sel->target,
646 	};
647 	u32 pad;
648 	int ret;
649 
650 	switch (sel->target) {
651 	case V4L2_SEL_TGT_CROP:
652 	case V4L2_SEL_TGT_CROP_BOUNDS:
653 	case V4L2_SEL_TGT_CROP_DEFAULT:
654 		if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
655 			return -EINVAL;
656 		break;
657 	case V4L2_SEL_TGT_COMPOSE:
658 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
659 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
660 		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
661 			return -EINVAL;
662 		break;
663 	default:
664 		return -EINVAL;
665 	}
666 	subdev = iss_video_remote_subdev(video, &pad);
667 	if (!subdev)
668 		return -EINVAL;
669 
670 	/*
671 	 * Try the get selection operation first and fallback to get format if
672 	 * not implemented.
673 	 */
674 	sdsel.pad = pad;
675 	ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
676 	if (!ret)
677 		sel->r = sdsel.r;
678 	if (ret != -ENOIOCTLCMD)
679 		return ret;
680 
681 	format.pad = pad;
682 	format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
683 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
684 	if (ret < 0)
685 		return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
686 
687 	sel->r.left = 0;
688 	sel->r.top = 0;
689 	sel->r.width = format.format.width;
690 	sel->r.height = format.format.height;
691 
692 	return 0;
693 }
694 
695 static int
696 iss_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
697 {
698 	struct iss_video *video = video_drvdata(file);
699 	struct v4l2_subdev *subdev;
700 	struct v4l2_subdev_selection sdsel = {
701 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
702 		.target = sel->target,
703 		.flags = sel->flags,
704 		.r = sel->r,
705 	};
706 	u32 pad;
707 	int ret;
708 
709 	switch (sel->target) {
710 	case V4L2_SEL_TGT_CROP:
711 		if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
712 			return -EINVAL;
713 		break;
714 	case V4L2_SEL_TGT_COMPOSE:
715 		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
716 			return -EINVAL;
717 		break;
718 	default:
719 		return -EINVAL;
720 	}
721 	subdev = iss_video_remote_subdev(video, &pad);
722 	if (!subdev)
723 		return -EINVAL;
724 
725 	sdsel.pad = pad;
726 	mutex_lock(&video->mutex);
727 	ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
728 	mutex_unlock(&video->mutex);
729 	if (!ret)
730 		sel->r = sdsel.r;
731 
732 	return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
733 }
734 
735 static int
736 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
737 {
738 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
739 	struct iss_video *video = video_drvdata(file);
740 
741 	if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
742 	    video->type != a->type)
743 		return -EINVAL;
744 
745 	memset(a, 0, sizeof(*a));
746 	a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
747 	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
748 	a->parm.output.timeperframe = vfh->timeperframe;
749 
750 	return 0;
751 }
752 
753 static int
754 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
755 {
756 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
757 	struct iss_video *video = video_drvdata(file);
758 
759 	if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
760 	    video->type != a->type)
761 		return -EINVAL;
762 
763 	if (a->parm.output.timeperframe.denominator == 0)
764 		a->parm.output.timeperframe.denominator = 1;
765 
766 	vfh->timeperframe = a->parm.output.timeperframe;
767 
768 	return 0;
769 }
770 
771 static int
772 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
773 {
774 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
775 
776 	return vb2_reqbufs(&vfh->queue, rb);
777 }
778 
779 static int
780 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
781 {
782 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
783 
784 	return vb2_querybuf(&vfh->queue, b);
785 }
786 
787 static int
788 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
789 {
790 	struct iss_video *video = video_drvdata(file);
791 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
792 
793 	return vb2_qbuf(&vfh->queue, video->video.v4l2_dev->mdev, b);
794 }
795 
796 static int
797 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
798 {
799 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
800 
801 	return vb2_expbuf(&vfh->queue, e);
802 }
803 
804 static int
805 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
806 {
807 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
808 
809 	return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
810 }
811 
812 /*
813  * Stream management
814  *
815  * Every ISS pipeline has a single input and a single output. The input can be
816  * either a sensor or a video node. The output is always a video node.
817  *
818  * As every pipeline has an output video node, the ISS video objects at the
819  * pipeline output stores the pipeline state. It tracks the streaming state of
820  * both the input and output, as well as the availability of buffers.
821  *
822  * In sensor-to-memory mode, frames are always available at the pipeline input.
823  * Starting the sensor usually requires I2C transfers and must be done in
824  * interruptible context. The pipeline is started and stopped synchronously
825  * to the stream on/off commands. All modules in the pipeline will get their
826  * subdev set stream handler called. The module at the end of the pipeline must
827  * delay starting the hardware until buffers are available at its output.
828  *
829  * In memory-to-memory mode, starting/stopping the stream requires
830  * synchronization between the input and output. ISS modules can't be stopped
831  * in the middle of a frame, and at least some of the modules seem to become
832  * busy as soon as they're started, even if they don't receive a frame start
833  * event. For that reason frames need to be processed in single-shot mode. The
834  * driver needs to wait until a frame is completely processed and written to
835  * memory before restarting the pipeline for the next frame. Pipelined
836  * processing might be possible but requires more testing.
837  *
838  * Stream start must be delayed until buffers are available at both the input
839  * and output. The pipeline must be started in the vb2 queue callback with
840  * the buffers queue spinlock held. The modules subdev set stream operation must
841  * not sleep.
842  */
843 static int
844 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
845 {
846 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
847 	struct iss_video *video = video_drvdata(file);
848 	struct media_device *mdev = video->video.entity.graph_obj.mdev;
849 	struct media_pipeline_pad_iter iter;
850 	enum iss_pipeline_state state;
851 	struct iss_pipeline *pipe;
852 	struct iss_video *far_end;
853 	struct media_pad *pad;
854 	unsigned long flags;
855 	int ret;
856 
857 	if (type != video->type)
858 		return -EINVAL;
859 
860 	mutex_lock(&video->stream_lock);
861 
862 	/*
863 	 * Start streaming on the pipeline. No link touching an entity in the
864 	 * pipeline can be activated or deactivated once streaming is started.
865 	 */
866 	pipe = to_iss_pipeline(&video->video.entity) ? : &video->pipe;
867 	pipe->external = NULL;
868 	pipe->external_rate = 0;
869 	pipe->external_bpp = 0;
870 
871 	ret = media_entity_enum_init(&pipe->ent_enum, mdev);
872 	if (ret)
873 		goto err_entity_enum_init;
874 
875 	if (video->iss->pdata->set_constraints)
876 		video->iss->pdata->set_constraints(video->iss, true);
877 
878 	ret = video_device_pipeline_start(&video->video, &pipe->pipe);
879 	if (ret < 0)
880 		goto err_media_pipeline_start;
881 
882 	media_pipeline_for_each_pad(&pipe->pipe, &iter, pad)
883 		media_entity_enum_set(&pipe->ent_enum, pad->entity);
884 
885 	/*
886 	 * Verify that the currently configured format matches the output of
887 	 * the connected subdev.
888 	 */
889 	ret = iss_video_check_format(video, vfh);
890 	if (ret < 0)
891 		goto err_iss_video_check_format;
892 
893 	video->bpl_padding = ret;
894 	video->bpl_value = vfh->format.fmt.pix.bytesperline;
895 
896 	/*
897 	 * Find the ISS video node connected at the far end of the pipeline and
898 	 * update the pipeline.
899 	 */
900 	far_end = iss_video_far_end(video, pipe);
901 	if (IS_ERR(far_end)) {
902 		ret = PTR_ERR(far_end);
903 		goto err_iss_video_check_format;
904 	}
905 
906 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
907 		state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
908 		pipe->input = far_end;
909 		pipe->output = video;
910 	} else {
911 		if (!far_end) {
912 			ret = -EPIPE;
913 			goto err_iss_video_check_format;
914 		}
915 
916 		state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
917 		pipe->input = video;
918 		pipe->output = far_end;
919 	}
920 
921 	spin_lock_irqsave(&pipe->lock, flags);
922 	pipe->state &= ~ISS_PIPELINE_STREAM;
923 	pipe->state |= state;
924 	spin_unlock_irqrestore(&pipe->lock, flags);
925 
926 	/*
927 	 * Set the maximum time per frame as the value requested by userspace.
928 	 * This is a soft limit that can be overridden if the hardware doesn't
929 	 * support the request limit.
930 	 */
931 	if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
932 		pipe->max_timeperframe = vfh->timeperframe;
933 
934 	video->queue = &vfh->queue;
935 	INIT_LIST_HEAD(&video->dmaqueue);
936 	video->error = false;
937 	atomic_set(&pipe->frame_number, -1);
938 
939 	ret = vb2_streamon(&vfh->queue, type);
940 	if (ret < 0)
941 		goto err_iss_video_check_format;
942 
943 	/*
944 	 * In sensor-to-memory mode, the stream can be started synchronously
945 	 * to the stream on command. In memory-to-memory mode, it will be
946 	 * started when buffers are queued on both the input and output.
947 	 */
948 	if (!pipe->input) {
949 		unsigned long flags;
950 
951 		ret = omap4iss_pipeline_set_stream(pipe,
952 						   ISS_PIPELINE_STREAM_CONTINUOUS);
953 		if (ret < 0)
954 			goto err_omap4iss_set_stream;
955 		spin_lock_irqsave(&video->qlock, flags);
956 		if (list_empty(&video->dmaqueue))
957 			video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
958 		spin_unlock_irqrestore(&video->qlock, flags);
959 	}
960 
961 	mutex_unlock(&video->stream_lock);
962 
963 	return 0;
964 
965 err_omap4iss_set_stream:
966 	vb2_streamoff(&vfh->queue, type);
967 err_iss_video_check_format:
968 	video_device_pipeline_stop(&video->video);
969 err_media_pipeline_start:
970 	if (video->iss->pdata->set_constraints)
971 		video->iss->pdata->set_constraints(video->iss, false);
972 	video->queue = NULL;
973 
974 err_entity_enum_init:
975 	media_entity_enum_cleanup(&pipe->ent_enum);
976 
977 	mutex_unlock(&video->stream_lock);
978 
979 	return ret;
980 }
981 
982 static int
983 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
984 {
985 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
986 	struct iss_video *video = video_drvdata(file);
987 	struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
988 	enum iss_pipeline_state state;
989 	unsigned long flags;
990 
991 	if (type != video->type)
992 		return -EINVAL;
993 
994 	mutex_lock(&video->stream_lock);
995 
996 	if (!vb2_is_streaming(&vfh->queue))
997 		goto done;
998 
999 	/* Update the pipeline state. */
1000 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1001 		state = ISS_PIPELINE_STREAM_OUTPUT
1002 		      | ISS_PIPELINE_QUEUE_OUTPUT;
1003 	else
1004 		state = ISS_PIPELINE_STREAM_INPUT
1005 		      | ISS_PIPELINE_QUEUE_INPUT;
1006 
1007 	spin_lock_irqsave(&pipe->lock, flags);
1008 	pipe->state &= ~state;
1009 	spin_unlock_irqrestore(&pipe->lock, flags);
1010 
1011 	/* Stop the stream. */
1012 	omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
1013 	vb2_streamoff(&vfh->queue, type);
1014 	video->queue = NULL;
1015 
1016 	media_entity_enum_cleanup(&pipe->ent_enum);
1017 
1018 	if (video->iss->pdata->set_constraints)
1019 		video->iss->pdata->set_constraints(video->iss, false);
1020 	video_device_pipeline_stop(&video->video);
1021 
1022 done:
1023 	mutex_unlock(&video->stream_lock);
1024 	return 0;
1025 }
1026 
1027 static int
1028 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1029 {
1030 	if (input->index > 0)
1031 		return -EINVAL;
1032 
1033 	strscpy(input->name, "camera", sizeof(input->name));
1034 	input->type = V4L2_INPUT_TYPE_CAMERA;
1035 
1036 	return 0;
1037 }
1038 
1039 static int
1040 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1041 {
1042 	*input = 0;
1043 
1044 	return 0;
1045 }
1046 
1047 static int
1048 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1049 {
1050 	return input == 0 ? 0 : -EINVAL;
1051 }
1052 
1053 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1054 	.vidioc_querycap		= iss_video_querycap,
1055 	.vidioc_enum_fmt_vid_cap        = iss_video_enum_format,
1056 	.vidioc_g_fmt_vid_cap		= iss_video_get_format,
1057 	.vidioc_s_fmt_vid_cap		= iss_video_set_format,
1058 	.vidioc_try_fmt_vid_cap		= iss_video_try_format,
1059 	.vidioc_g_fmt_vid_out		= iss_video_get_format,
1060 	.vidioc_s_fmt_vid_out		= iss_video_set_format,
1061 	.vidioc_try_fmt_vid_out		= iss_video_try_format,
1062 	.vidioc_g_selection		= iss_video_get_selection,
1063 	.vidioc_s_selection		= iss_video_set_selection,
1064 	.vidioc_g_parm			= iss_video_get_param,
1065 	.vidioc_s_parm			= iss_video_set_param,
1066 	.vidioc_reqbufs			= iss_video_reqbufs,
1067 	.vidioc_querybuf		= iss_video_querybuf,
1068 	.vidioc_qbuf			= iss_video_qbuf,
1069 	.vidioc_expbuf			= iss_video_expbuf,
1070 	.vidioc_dqbuf			= iss_video_dqbuf,
1071 	.vidioc_streamon		= iss_video_streamon,
1072 	.vidioc_streamoff		= iss_video_streamoff,
1073 	.vidioc_enum_input		= iss_video_enum_input,
1074 	.vidioc_g_input			= iss_video_g_input,
1075 	.vidioc_s_input			= iss_video_s_input,
1076 };
1077 
1078 /* -----------------------------------------------------------------------------
1079  * V4L2 file operations
1080  */
1081 
1082 static int iss_video_open(struct file *file)
1083 {
1084 	struct iss_video *video = video_drvdata(file);
1085 	struct iss_video_fh *handle;
1086 	struct vb2_queue *q;
1087 	int ret = 0;
1088 
1089 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1090 	if (!handle)
1091 		return -ENOMEM;
1092 
1093 	v4l2_fh_init(&handle->vfh, &video->video);
1094 	v4l2_fh_add(&handle->vfh);
1095 
1096 	/* If this is the first user, initialise the pipeline. */
1097 	if (!omap4iss_get(video->iss)) {
1098 		ret = -EBUSY;
1099 		goto done;
1100 	}
1101 
1102 	ret = v4l2_pipeline_pm_get(&video->video.entity);
1103 	if (ret < 0) {
1104 		omap4iss_put(video->iss);
1105 		goto done;
1106 	}
1107 
1108 	q = &handle->queue;
1109 
1110 	q->type = video->type;
1111 	q->io_modes = VB2_MMAP | VB2_DMABUF;
1112 	q->drv_priv = handle;
1113 	q->ops = &iss_video_vb2ops;
1114 	q->mem_ops = &vb2_dma_contig_memops;
1115 	q->buf_struct_size = sizeof(struct iss_buffer);
1116 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1117 	q->dev = video->iss->dev;
1118 
1119 	ret = vb2_queue_init(q);
1120 	if (ret) {
1121 		omap4iss_put(video->iss);
1122 		goto done;
1123 	}
1124 
1125 	memset(&handle->format, 0, sizeof(handle->format));
1126 	handle->format.type = video->type;
1127 	handle->timeperframe.denominator = 1;
1128 
1129 	handle->video = video;
1130 	file->private_data = &handle->vfh;
1131 
1132 done:
1133 	if (ret < 0) {
1134 		v4l2_fh_del(&handle->vfh);
1135 		v4l2_fh_exit(&handle->vfh);
1136 		kfree(handle);
1137 	}
1138 
1139 	return ret;
1140 }
1141 
1142 static int iss_video_release(struct file *file)
1143 {
1144 	struct iss_video *video = video_drvdata(file);
1145 	struct v4l2_fh *vfh = file->private_data;
1146 	struct iss_video_fh *handle = to_iss_video_fh(vfh);
1147 
1148 	/* Disable streaming and free the buffers queue resources. */
1149 	iss_video_streamoff(file, vfh, video->type);
1150 
1151 	v4l2_pipeline_pm_put(&video->video.entity);
1152 
1153 	/* Release the videobuf2 queue */
1154 	vb2_queue_release(&handle->queue);
1155 
1156 	v4l2_fh_del(vfh);
1157 	v4l2_fh_exit(vfh);
1158 	kfree(handle);
1159 	file->private_data = NULL;
1160 
1161 	omap4iss_put(video->iss);
1162 
1163 	return 0;
1164 }
1165 
1166 static __poll_t iss_video_poll(struct file *file, poll_table *wait)
1167 {
1168 	struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1169 
1170 	return vb2_poll(&vfh->queue, file, wait);
1171 }
1172 
1173 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1174 {
1175 	struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1176 
1177 	return vb2_mmap(&vfh->queue, vma);
1178 }
1179 
1180 static const struct v4l2_file_operations iss_video_fops = {
1181 	.owner = THIS_MODULE,
1182 	.unlocked_ioctl = video_ioctl2,
1183 	.open = iss_video_open,
1184 	.release = iss_video_release,
1185 	.poll = iss_video_poll,
1186 	.mmap = iss_video_mmap,
1187 };
1188 
1189 /* -----------------------------------------------------------------------------
1190  * ISS video core
1191  */
1192 
1193 static const struct iss_video_operations iss_video_dummy_ops = {
1194 };
1195 
1196 int omap4iss_video_init(struct iss_video *video, const char *name)
1197 {
1198 	const char *direction;
1199 	int ret;
1200 
1201 	switch (video->type) {
1202 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1203 		direction = "output";
1204 		video->pad.flags = MEDIA_PAD_FL_SINK;
1205 		break;
1206 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1207 		direction = "input";
1208 		video->pad.flags = MEDIA_PAD_FL_SOURCE;
1209 		break;
1210 
1211 	default:
1212 		return -EINVAL;
1213 	}
1214 
1215 	ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1216 	if (ret < 0)
1217 		return ret;
1218 
1219 	spin_lock_init(&video->qlock);
1220 	mutex_init(&video->mutex);
1221 	atomic_set(&video->active, 0);
1222 
1223 	spin_lock_init(&video->pipe.lock);
1224 	mutex_init(&video->stream_lock);
1225 
1226 	/* Initialize the video device. */
1227 	if (!video->ops)
1228 		video->ops = &iss_video_dummy_ops;
1229 
1230 	video->video.fops = &iss_video_fops;
1231 	snprintf(video->video.name, sizeof(video->video.name),
1232 		 "OMAP4 ISS %s %s", name, direction);
1233 	video->video.vfl_type = VFL_TYPE_VIDEO;
1234 	video->video.release = video_device_release_empty;
1235 	video->video.ioctl_ops = &iss_video_ioctl_ops;
1236 	video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1237 
1238 	video_set_drvdata(&video->video, video);
1239 
1240 	return 0;
1241 }
1242 
1243 void omap4iss_video_cleanup(struct iss_video *video)
1244 {
1245 	media_entity_cleanup(&video->video.entity);
1246 	mutex_destroy(&video->stream_lock);
1247 	mutex_destroy(&video->mutex);
1248 }
1249 
1250 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1251 {
1252 	int ret;
1253 
1254 	video->video.v4l2_dev = vdev;
1255 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1256 		video->video.device_caps = V4L2_CAP_VIDEO_CAPTURE;
1257 	else
1258 		video->video.device_caps = V4L2_CAP_VIDEO_OUTPUT;
1259 	video->video.device_caps |= V4L2_CAP_STREAMING;
1260 
1261 	ret = video_register_device(&video->video, VFL_TYPE_VIDEO, -1);
1262 	if (ret < 0)
1263 		dev_err(video->iss->dev,
1264 			"could not register video device (%d)\n", ret);
1265 
1266 	return ret;
1267 }
1268 
1269 void omap4iss_video_unregister(struct iss_video *video)
1270 {
1271 	video_unregister_device(&video->video);
1272 }
1273