[go: nahoru, domu]

1/*
2 * ispvideo.c
3 *
4 * TI OMAP3 ISP - Generic video node
5 *
6 * Copyright (C) 2009-2010 Nokia Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 *	     Sakari Ailus <sakari.ailus@iki.fi>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <asm/cacheflush.h>
17#include <linux/clk.h>
18#include <linux/mm.h>
19#include <linux/module.h>
20#include <linux/pagemap.h>
21#include <linux/scatterlist.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <media/v4l2-dev.h>
26#include <media/v4l2-ioctl.h>
27#include <media/videobuf2-dma-contig.h>
28
29#include "ispvideo.h"
30#include "isp.h"
31
32
33/* -----------------------------------------------------------------------------
34 * Helper functions
35 */
36
37/*
38 * NOTE: When adding new media bus codes, always remember to add
39 * corresponding in-memory formats to the table below!!!
40 */
41static struct isp_format_info formats[] = {
42	{ V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
43	  V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
44	  V4L2_PIX_FMT_GREY, 8, 1, },
45	{ V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
46	  V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
47	  V4L2_PIX_FMT_Y10, 10, 2, },
48	{ V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
49	  V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
50	  V4L2_PIX_FMT_Y12, 12, 2, },
51	{ V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
52	  V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
53	  V4L2_PIX_FMT_SBGGR8, 8, 1, },
54	{ V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
55	  V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
56	  V4L2_PIX_FMT_SGBRG8, 8, 1, },
57	{ V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
58	  V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
59	  V4L2_PIX_FMT_SGRBG8, 8, 1, },
60	{ V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
61	  V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
62	  V4L2_PIX_FMT_SRGGB8, 8, 1, },
63	{ V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8,
64	  V4L2_MBUS_FMT_SBGGR10_1X10, 0,
65	  V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
66	{ V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8,
67	  V4L2_MBUS_FMT_SGBRG10_1X10, 0,
68	  V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
69	{ V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
70	  V4L2_MBUS_FMT_SGRBG10_1X10, 0,
71	  V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
72	{ V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8,
73	  V4L2_MBUS_FMT_SRGGB10_1X10, 0,
74	  V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
75	{ V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
76	  V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
77	  V4L2_PIX_FMT_SBGGR10, 10, 2, },
78	{ V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
79	  V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
80	  V4L2_PIX_FMT_SGBRG10, 10, 2, },
81	{ V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
82	  V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
83	  V4L2_PIX_FMT_SGRBG10, 10, 2, },
84	{ V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
85	  V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
86	  V4L2_PIX_FMT_SRGGB10, 10, 2, },
87	{ V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
88	  V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
89	  V4L2_PIX_FMT_SBGGR12, 12, 2, },
90	{ V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
91	  V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
92	  V4L2_PIX_FMT_SGBRG12, 12, 2, },
93	{ V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
94	  V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
95	  V4L2_PIX_FMT_SGRBG12, 12, 2, },
96	{ V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
97	  V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
98	  V4L2_PIX_FMT_SRGGB12, 12, 2, },
99	{ V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
100	  V4L2_MBUS_FMT_UYVY8_1X16, 0,
101	  V4L2_PIX_FMT_UYVY, 16, 2, },
102	{ V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
103	  V4L2_MBUS_FMT_YUYV8_1X16, 0,
104	  V4L2_PIX_FMT_YUYV, 16, 2, },
105	{ V4L2_MBUS_FMT_UYVY8_2X8, V4L2_MBUS_FMT_UYVY8_2X8,
106	  V4L2_MBUS_FMT_UYVY8_2X8, 0,
107	  V4L2_PIX_FMT_UYVY, 8, 2, },
108	{ V4L2_MBUS_FMT_YUYV8_2X8, V4L2_MBUS_FMT_YUYV8_2X8,
109	  V4L2_MBUS_FMT_YUYV8_2X8, 0,
110	  V4L2_PIX_FMT_YUYV, 8, 2, },
111	/* Empty entry to catch the unsupported pixel code (0) used by the CCDC
112	 * module and avoid NULL pointer dereferences.
113	 */
114	{ 0, }
115};
116
117const struct isp_format_info *
118omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
119{
120	unsigned int i;
121
122	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
123		if (formats[i].code == code)
124			return &formats[i];
125	}
126
127	return NULL;
128}
129
130/*
131 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
132 * @video: ISP video instance
133 * @mbus: v4l2_mbus_framefmt format (input)
134 * @pix: v4l2_pix_format format (output)
135 *
136 * Fill the output pix structure with information from the input mbus format.
137 * The bytesperline and sizeimage fields are computed from the requested bytes
138 * per line value in the pix format and information from the video instance.
139 *
140 * Return the number of padding bytes at end of line.
141 */
142static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
143					  const struct v4l2_mbus_framefmt *mbus,
144					  struct v4l2_pix_format *pix)
145{
146	unsigned int bpl = pix->bytesperline;
147	unsigned int min_bpl;
148	unsigned int i;
149
150	memset(pix, 0, sizeof(*pix));
151	pix->width = mbus->width;
152	pix->height = mbus->height;
153
154	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
155		if (formats[i].code == mbus->code)
156			break;
157	}
158
159	if (WARN_ON(i == ARRAY_SIZE(formats)))
160		return 0;
161
162	min_bpl = pix->width * formats[i].bpp;
163
164	/* Clamp the requested bytes per line value. If the maximum bytes per
165	 * line value is zero, the module doesn't support user configurable line
166	 * sizes. Override the requested value with the minimum in that case.
167	 */
168	if (video->bpl_max)
169		bpl = clamp(bpl, min_bpl, video->bpl_max);
170	else
171		bpl = min_bpl;
172
173	if (!video->bpl_zero_padding || bpl != min_bpl)
174		bpl = ALIGN(bpl, video->bpl_alignment);
175
176	pix->pixelformat = formats[i].pixelformat;
177	pix->bytesperline = bpl;
178	pix->sizeimage = pix->bytesperline * pix->height;
179	pix->colorspace = mbus->colorspace;
180	pix->field = mbus->field;
181
182	return bpl - min_bpl;
183}
184
185static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
186				  struct v4l2_mbus_framefmt *mbus)
187{
188	unsigned int i;
189
190	memset(mbus, 0, sizeof(*mbus));
191	mbus->width = pix->width;
192	mbus->height = pix->height;
193
194	/* Skip the last format in the loop so that it will be selected if no
195	 * match is found.
196	 */
197	for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
198		if (formats[i].pixelformat == pix->pixelformat)
199			break;
200	}
201
202	mbus->code = formats[i].code;
203	mbus->colorspace = pix->colorspace;
204	mbus->field = pix->field;
205}
206
207static struct v4l2_subdev *
208isp_video_remote_subdev(struct isp_video *video, u32 *pad)
209{
210	struct media_pad *remote;
211
212	remote = media_entity_remote_pad(&video->pad);
213
214	if (remote == NULL ||
215	    media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
216		return NULL;
217
218	if (pad)
219		*pad = remote->index;
220
221	return media_entity_to_v4l2_subdev(remote->entity);
222}
223
224/* Return a pointer to the ISP video instance at the far end of the pipeline. */
225static int isp_video_get_graph_data(struct isp_video *video,
226				    struct isp_pipeline *pipe)
227{
228	struct media_entity_graph graph;
229	struct media_entity *entity = &video->video.entity;
230	struct media_device *mdev = entity->parent;
231	struct isp_video *far_end = NULL;
232
233	mutex_lock(&mdev->graph_mutex);
234	media_entity_graph_walk_start(&graph, entity);
235
236	while ((entity = media_entity_graph_walk_next(&graph))) {
237		struct isp_video *__video;
238
239		pipe->entities |= 1 << entity->id;
240
241		if (far_end != NULL)
242			continue;
243
244		if (entity == &video->video.entity)
245			continue;
246
247		if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
248			continue;
249
250		__video = to_isp_video(media_entity_to_video_device(entity));
251		if (__video->type != video->type)
252			far_end = __video;
253	}
254
255	mutex_unlock(&mdev->graph_mutex);
256
257	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
258		pipe->input = far_end;
259		pipe->output = video;
260	} else {
261		if (far_end == NULL)
262			return -EPIPE;
263
264		pipe->input = video;
265		pipe->output = far_end;
266	}
267
268	return 0;
269}
270
271static int
272__isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
273{
274	struct v4l2_subdev_format fmt;
275	struct v4l2_subdev *subdev;
276	u32 pad;
277	int ret;
278
279	subdev = isp_video_remote_subdev(video, &pad);
280	if (subdev == NULL)
281		return -EINVAL;
282
283	fmt.pad = pad;
284	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
285
286	mutex_lock(&video->mutex);
287	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
288	mutex_unlock(&video->mutex);
289
290	if (ret)
291		return ret;
292
293	format->type = video->type;
294	return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
295}
296
297static int
298isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
299{
300	struct v4l2_format format;
301	int ret;
302
303	memcpy(&format, &vfh->format, sizeof(format));
304	ret = __isp_video_get_format(video, &format);
305	if (ret < 0)
306		return ret;
307
308	if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
309	    vfh->format.fmt.pix.height != format.fmt.pix.height ||
310	    vfh->format.fmt.pix.width != format.fmt.pix.width ||
311	    vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
312	    vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage ||
313	    vfh->format.fmt.pix.field != format.fmt.pix.field)
314		return -EINVAL;
315
316	return 0;
317}
318
319/* -----------------------------------------------------------------------------
320 * Video queue operations
321 */
322
323static int isp_video_queue_setup(struct vb2_queue *queue,
324				 const struct v4l2_format *fmt,
325				 unsigned int *count, unsigned int *num_planes,
326				 unsigned int sizes[], void *alloc_ctxs[])
327{
328	struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
329	struct isp_video *video = vfh->video;
330
331	*num_planes = 1;
332
333	sizes[0] = vfh->format.fmt.pix.sizeimage;
334	if (sizes[0] == 0)
335		return -EINVAL;
336
337	alloc_ctxs[0] = video->alloc_ctx;
338
339	*count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
340
341	return 0;
342}
343
344static int isp_video_buffer_prepare(struct vb2_buffer *buf)
345{
346	struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
347	struct isp_buffer *buffer = to_isp_buffer(buf);
348	struct isp_video *video = vfh->video;
349	dma_addr_t addr;
350
351	/* Refuse to prepare the buffer is the video node has registered an
352	 * error. We don't need to take any lock here as the operation is
353	 * inherently racy. The authoritative check will be performed in the
354	 * queue handler, which can't return an error, this check is just a best
355	 * effort to notify userspace as early as possible.
356	 */
357	if (unlikely(video->error))
358		return -EIO;
359
360	addr = vb2_dma_contig_plane_dma_addr(buf, 0);
361	if (!IS_ALIGNED(addr, 32)) {
362		dev_dbg(video->isp->dev,
363			"Buffer address must be aligned to 32 bytes boundary.\n");
364		return -EINVAL;
365	}
366
367	vb2_set_plane_payload(&buffer->vb, 0, vfh->format.fmt.pix.sizeimage);
368	buffer->dma = addr;
369
370	return 0;
371}
372
373/*
374 * isp_video_buffer_queue - Add buffer to streaming queue
375 * @buf: Video buffer
376 *
377 * In memory-to-memory mode, start streaming on the pipeline if buffers are
378 * queued on both the input and the output, if the pipeline isn't already busy.
379 * If the pipeline is busy, it will be restarted in the output module interrupt
380 * handler.
381 */
382static void isp_video_buffer_queue(struct vb2_buffer *buf)
383{
384	struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
385	struct isp_buffer *buffer = to_isp_buffer(buf);
386	struct isp_video *video = vfh->video;
387	struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
388	enum isp_pipeline_state state;
389	unsigned long flags;
390	unsigned int empty;
391	unsigned int start;
392
393	spin_lock_irqsave(&video->irqlock, flags);
394
395	if (unlikely(video->error)) {
396		vb2_buffer_done(&buffer->vb, VB2_BUF_STATE_ERROR);
397		spin_unlock_irqrestore(&video->irqlock, flags);
398		return;
399	}
400
401	empty = list_empty(&video->dmaqueue);
402	list_add_tail(&buffer->irqlist, &video->dmaqueue);
403
404	spin_unlock_irqrestore(&video->irqlock, flags);
405
406	if (empty) {
407		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
408			state = ISP_PIPELINE_QUEUE_OUTPUT;
409		else
410			state = ISP_PIPELINE_QUEUE_INPUT;
411
412		spin_lock_irqsave(&pipe->lock, flags);
413		pipe->state |= state;
414		video->ops->queue(video, buffer);
415		video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
416
417		start = isp_pipeline_ready(pipe);
418		if (start)
419			pipe->state |= ISP_PIPELINE_STREAM;
420		spin_unlock_irqrestore(&pipe->lock, flags);
421
422		if (start)
423			omap3isp_pipeline_set_stream(pipe,
424						ISP_PIPELINE_STREAM_SINGLESHOT);
425	}
426}
427
428static const struct vb2_ops isp_video_queue_ops = {
429	.queue_setup = isp_video_queue_setup,
430	.buf_prepare = isp_video_buffer_prepare,
431	.buf_queue = isp_video_buffer_queue,
432};
433
434/*
435 * omap3isp_video_buffer_next - Complete the current buffer and return the next
436 * @video: ISP video object
437 *
438 * Remove the current video buffer from the DMA queue and fill its timestamp and
439 * field count before handing it back to videobuf2.
440 *
441 * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
442 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
443 * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
444 *
445 * The DMA queue is expected to contain at least one buffer.
446 *
447 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
448 * empty.
449 */
450struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
451{
452	struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
453	enum isp_pipeline_state state;
454	struct isp_buffer *buf;
455	unsigned long flags;
456	struct timespec ts;
457
458	spin_lock_irqsave(&video->irqlock, flags);
459	if (WARN_ON(list_empty(&video->dmaqueue))) {
460		spin_unlock_irqrestore(&video->irqlock, flags);
461		return NULL;
462	}
463
464	buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
465			       irqlist);
466	list_del(&buf->irqlist);
467	spin_unlock_irqrestore(&video->irqlock, flags);
468
469	ktime_get_ts(&ts);
470	buf->vb.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
471	buf->vb.v4l2_buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
472
473	/* Do frame number propagation only if this is the output video node.
474	 * Frame number either comes from the CSI receivers or it gets
475	 * incremented here if H3A is not active.
476	 * Note: There is no guarantee that the output buffer will finish
477	 * first, so the input number might lag behind by 1 in some cases.
478	 */
479	if (video == pipe->output && !pipe->do_propagation)
480		buf->vb.v4l2_buf.sequence =
481			atomic_inc_return(&pipe->frame_number);
482	else
483		buf->vb.v4l2_buf.sequence = atomic_read(&pipe->frame_number);
484
485	if (pipe->field != V4L2_FIELD_NONE)
486		buf->vb.v4l2_buf.sequence /= 2;
487
488	buf->vb.v4l2_buf.field = pipe->field;
489
490	/* Report pipeline errors to userspace on the capture device side. */
491	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
492		state = VB2_BUF_STATE_ERROR;
493		pipe->error = false;
494	} else {
495		state = VB2_BUF_STATE_DONE;
496	}
497
498	vb2_buffer_done(&buf->vb, state);
499
500	spin_lock_irqsave(&video->irqlock, flags);
501
502	if (list_empty(&video->dmaqueue)) {
503		spin_unlock_irqrestore(&video->irqlock, flags);
504
505		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
506			state = ISP_PIPELINE_QUEUE_OUTPUT
507			      | ISP_PIPELINE_STREAM;
508		else
509			state = ISP_PIPELINE_QUEUE_INPUT
510			      | ISP_PIPELINE_STREAM;
511
512		spin_lock_irqsave(&pipe->lock, flags);
513		pipe->state &= ~state;
514		if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
515			video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
516		spin_unlock_irqrestore(&pipe->lock, flags);
517		return NULL;
518	}
519
520	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
521		spin_lock(&pipe->lock);
522		pipe->state &= ~ISP_PIPELINE_STREAM;
523		spin_unlock(&pipe->lock);
524	}
525
526	buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
527			       irqlist);
528	buf->vb.state = VB2_BUF_STATE_ACTIVE;
529
530	spin_unlock_irqrestore(&video->irqlock, flags);
531
532	return buf;
533}
534
535/*
536 * omap3isp_video_cancel_stream - Cancel stream on a video node
537 * @video: ISP video object
538 *
539 * Cancelling a stream mark all buffers on the video node as erroneous and makes
540 * sure no new buffer can be queued.
541 */
542void omap3isp_video_cancel_stream(struct isp_video *video)
543{
544	unsigned long flags;
545
546	spin_lock_irqsave(&video->irqlock, flags);
547
548	while (!list_empty(&video->dmaqueue)) {
549		struct isp_buffer *buf;
550
551		buf = list_first_entry(&video->dmaqueue,
552				       struct isp_buffer, irqlist);
553		list_del(&buf->irqlist);
554		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
555	}
556
557	video->error = true;
558
559	spin_unlock_irqrestore(&video->irqlock, flags);
560}
561
562/*
563 * omap3isp_video_resume - Perform resume operation on the buffers
564 * @video: ISP video object
565 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
566 *
567 * This function is intended to be used on suspend/resume scenario. It
568 * requests video queue layer to discard buffers marked as DONE if it's in
569 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
570 * if there's any.
571 */
572void omap3isp_video_resume(struct isp_video *video, int continuous)
573{
574	struct isp_buffer *buf = NULL;
575
576	if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
577		mutex_lock(&video->queue_lock);
578		vb2_discard_done(video->queue);
579		mutex_unlock(&video->queue_lock);
580	}
581
582	if (!list_empty(&video->dmaqueue)) {
583		buf = list_first_entry(&video->dmaqueue,
584				       struct isp_buffer, irqlist);
585		video->ops->queue(video, buf);
586		video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
587	} else {
588		if (continuous)
589			video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
590	}
591}
592
593/* -----------------------------------------------------------------------------
594 * V4L2 ioctls
595 */
596
597static int
598isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
599{
600	struct isp_video *video = video_drvdata(file);
601
602	strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
603	strlcpy(cap->card, video->video.name, sizeof(cap->card));
604	strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
605
606	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
607		cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
608	else
609		cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
610
611	return 0;
612}
613
614static int
615isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
616{
617	struct isp_video_fh *vfh = to_isp_video_fh(fh);
618	struct isp_video *video = video_drvdata(file);
619
620	if (format->type != video->type)
621		return -EINVAL;
622
623	mutex_lock(&video->mutex);
624	*format = vfh->format;
625	mutex_unlock(&video->mutex);
626
627	return 0;
628}
629
630static int
631isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
632{
633	struct isp_video_fh *vfh = to_isp_video_fh(fh);
634	struct isp_video *video = video_drvdata(file);
635	struct v4l2_mbus_framefmt fmt;
636
637	if (format->type != video->type)
638		return -EINVAL;
639
640	/* Replace unsupported field orders with sane defaults. */
641	switch (format->fmt.pix.field) {
642	case V4L2_FIELD_NONE:
643		/* Progressive is supported everywhere. */
644		break;
645	case V4L2_FIELD_ALTERNATE:
646		/* ALTERNATE is not supported on output nodes. */
647		if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
648			format->fmt.pix.field = V4L2_FIELD_NONE;
649		break;
650	case V4L2_FIELD_INTERLACED:
651		/* The ISP has no concept of video standard, select the
652		 * top-bottom order when the unqualified interlaced order is
653		 * requested.
654		 */
655		format->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
656		/* Fall-through */
657	case V4L2_FIELD_INTERLACED_TB:
658	case V4L2_FIELD_INTERLACED_BT:
659		/* Interlaced orders are only supported at the CCDC output. */
660		if (video != &video->isp->isp_ccdc.video_out)
661			format->fmt.pix.field = V4L2_FIELD_NONE;
662		break;
663	case V4L2_FIELD_TOP:
664	case V4L2_FIELD_BOTTOM:
665	case V4L2_FIELD_SEQ_TB:
666	case V4L2_FIELD_SEQ_BT:
667	default:
668		/* All other field orders are currently unsupported, default to
669		 * progressive.
670		 */
671		format->fmt.pix.field = V4L2_FIELD_NONE;
672		break;
673	}
674
675	/* Fill the bytesperline and sizeimage fields by converting to media bus
676	 * format and back to pixel format.
677	 */
678	isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
679	isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
680
681	mutex_lock(&video->mutex);
682	vfh->format = *format;
683	mutex_unlock(&video->mutex);
684
685	return 0;
686}
687
688static int
689isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
690{
691	struct isp_video *video = video_drvdata(file);
692	struct v4l2_subdev_format fmt;
693	struct v4l2_subdev *subdev;
694	u32 pad;
695	int ret;
696
697	if (format->type != video->type)
698		return -EINVAL;
699
700	subdev = isp_video_remote_subdev(video, &pad);
701	if (subdev == NULL)
702		return -EINVAL;
703
704	isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
705
706	fmt.pad = pad;
707	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
708	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
709	if (ret)
710		return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
711
712	isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
713	return 0;
714}
715
716static int
717isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
718{
719	struct isp_video *video = video_drvdata(file);
720	struct v4l2_subdev *subdev;
721	int ret;
722
723	subdev = isp_video_remote_subdev(video, NULL);
724	if (subdev == NULL)
725		return -EINVAL;
726
727	mutex_lock(&video->mutex);
728	ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
729	mutex_unlock(&video->mutex);
730
731	return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
732}
733
734static int
735isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
736{
737	struct isp_video *video = video_drvdata(file);
738	struct v4l2_subdev_format format;
739	struct v4l2_subdev *subdev;
740	u32 pad;
741	int ret;
742
743	subdev = isp_video_remote_subdev(video, &pad);
744	if (subdev == NULL)
745		return -EINVAL;
746
747	/* Try the get crop operation first and fallback to get format if not
748	 * implemented.
749	 */
750	ret = v4l2_subdev_call(subdev, video, g_crop, crop);
751	if (ret != -ENOIOCTLCMD)
752		return ret;
753
754	format.pad = pad;
755	format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
756	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
757	if (ret < 0)
758		return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
759
760	crop->c.left = 0;
761	crop->c.top = 0;
762	crop->c.width = format.format.width;
763	crop->c.height = format.format.height;
764
765	return 0;
766}
767
768static int
769isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
770{
771	struct isp_video *video = video_drvdata(file);
772	struct v4l2_subdev *subdev;
773	int ret;
774
775	subdev = isp_video_remote_subdev(video, NULL);
776	if (subdev == NULL)
777		return -EINVAL;
778
779	mutex_lock(&video->mutex);
780	ret = v4l2_subdev_call(subdev, video, s_crop, crop);
781	mutex_unlock(&video->mutex);
782
783	return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
784}
785
786static int
787isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
788{
789	struct isp_video_fh *vfh = to_isp_video_fh(fh);
790	struct isp_video *video = video_drvdata(file);
791
792	if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
793	    video->type != a->type)
794		return -EINVAL;
795
796	memset(a, 0, sizeof(*a));
797	a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
798	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
799	a->parm.output.timeperframe = vfh->timeperframe;
800
801	return 0;
802}
803
804static int
805isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
806{
807	struct isp_video_fh *vfh = to_isp_video_fh(fh);
808	struct isp_video *video = video_drvdata(file);
809
810	if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
811	    video->type != a->type)
812		return -EINVAL;
813
814	if (a->parm.output.timeperframe.denominator == 0)
815		a->parm.output.timeperframe.denominator = 1;
816
817	vfh->timeperframe = a->parm.output.timeperframe;
818
819	return 0;
820}
821
822static int
823isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
824{
825	struct isp_video_fh *vfh = to_isp_video_fh(fh);
826	struct isp_video *video = video_drvdata(file);
827	int ret;
828
829	mutex_lock(&video->queue_lock);
830	ret = vb2_reqbufs(&vfh->queue, rb);
831	mutex_unlock(&video->queue_lock);
832
833	return ret;
834}
835
836static int
837isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
838{
839	struct isp_video_fh *vfh = to_isp_video_fh(fh);
840	struct isp_video *video = video_drvdata(file);
841	int ret;
842
843	mutex_lock(&video->queue_lock);
844	ret = vb2_querybuf(&vfh->queue, b);
845	mutex_unlock(&video->queue_lock);
846
847	return ret;
848}
849
850static int
851isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
852{
853	struct isp_video_fh *vfh = to_isp_video_fh(fh);
854	struct isp_video *video = video_drvdata(file);
855	int ret;
856
857	mutex_lock(&video->queue_lock);
858	ret = vb2_qbuf(&vfh->queue, b);
859	mutex_unlock(&video->queue_lock);
860
861	return ret;
862}
863
864static int
865isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
866{
867	struct isp_video_fh *vfh = to_isp_video_fh(fh);
868	struct isp_video *video = video_drvdata(file);
869	int ret;
870
871	mutex_lock(&video->queue_lock);
872	ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
873	mutex_unlock(&video->queue_lock);
874
875	return ret;
876}
877
878static int isp_video_check_external_subdevs(struct isp_video *video,
879					    struct isp_pipeline *pipe)
880{
881	struct isp_device *isp = video->isp;
882	struct media_entity *ents[] = {
883		&isp->isp_csi2a.subdev.entity,
884		&isp->isp_csi2c.subdev.entity,
885		&isp->isp_ccp2.subdev.entity,
886		&isp->isp_ccdc.subdev.entity
887	};
888	struct media_pad *source_pad;
889	struct media_entity *source = NULL;
890	struct media_entity *sink;
891	struct v4l2_subdev_format fmt;
892	struct v4l2_ext_controls ctrls;
893	struct v4l2_ext_control ctrl;
894	unsigned int i;
895	int ret;
896
897	/* Memory-to-memory pipelines have no external subdev. */
898	if (pipe->input != NULL)
899		return 0;
900
901	for (i = 0; i < ARRAY_SIZE(ents); i++) {
902		/* Is the entity part of the pipeline? */
903		if (!(pipe->entities & (1 << ents[i]->id)))
904			continue;
905
906		/* ISP entities have always sink pad == 0. Find source. */
907		source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
908		if (source_pad == NULL)
909			continue;
910
911		source = source_pad->entity;
912		sink = ents[i];
913		break;
914	}
915
916	if (!source) {
917		dev_warn(isp->dev, "can't find source, failing now\n");
918		return -EINVAL;
919	}
920
921	if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
922		return 0;
923
924	pipe->external = media_entity_to_v4l2_subdev(source);
925
926	fmt.pad = source_pad->index;
927	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
928	ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
929			       pad, get_fmt, NULL, &fmt);
930	if (unlikely(ret < 0)) {
931		dev_warn(isp->dev, "get_fmt returned null!\n");
932		return ret;
933	}
934
935	pipe->external_width =
936		omap3isp_video_format_info(fmt.format.code)->width;
937
938	memset(&ctrls, 0, sizeof(ctrls));
939	memset(&ctrl, 0, sizeof(ctrl));
940
941	ctrl.id = V4L2_CID_PIXEL_RATE;
942
943	ctrls.count = 1;
944	ctrls.controls = &ctrl;
945
946	ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
947	if (ret < 0) {
948		dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
949			 pipe->external->name);
950		return ret;
951	}
952
953	pipe->external_rate = ctrl.value64;
954
955	if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
956		unsigned int rate = UINT_MAX;
957		/*
958		 * Check that maximum allowed CCDC pixel rate isn't
959		 * exceeded by the pixel rate.
960		 */
961		omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
962		if (pipe->external_rate > rate)
963			return -ENOSPC;
964	}
965
966	return 0;
967}
968
969/*
970 * Stream management
971 *
972 * Every ISP pipeline has a single input and a single output. The input can be
973 * either a sensor or a video node. The output is always a video node.
974 *
975 * As every pipeline has an output video node, the ISP video objects at the
976 * pipeline output stores the pipeline state. It tracks the streaming state of
977 * both the input and output, as well as the availability of buffers.
978 *
979 * In sensor-to-memory mode, frames are always available at the pipeline input.
980 * Starting the sensor usually requires I2C transfers and must be done in
981 * interruptible context. The pipeline is started and stopped synchronously
982 * to the stream on/off commands. All modules in the pipeline will get their
983 * subdev set stream handler called. The module at the end of the pipeline must
984 * delay starting the hardware until buffers are available at its output.
985 *
986 * In memory-to-memory mode, starting/stopping the stream requires
987 * synchronization between the input and output. ISP modules can't be stopped
988 * in the middle of a frame, and at least some of the modules seem to become
989 * busy as soon as they're started, even if they don't receive a frame start
990 * event. For that reason frames need to be processed in single-shot mode. The
991 * driver needs to wait until a frame is completely processed and written to
992 * memory before restarting the pipeline for the next frame. Pipelined
993 * processing might be possible but requires more testing.
994 *
995 * Stream start must be delayed until buffers are available at both the input
996 * and output. The pipeline must be started in the videobuf queue callback with
997 * the buffers queue spinlock held. The modules subdev set stream operation must
998 * not sleep.
999 */
1000static int
1001isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1002{
1003	struct isp_video_fh *vfh = to_isp_video_fh(fh);
1004	struct isp_video *video = video_drvdata(file);
1005	enum isp_pipeline_state state;
1006	struct isp_pipeline *pipe;
1007	unsigned long flags;
1008	int ret;
1009
1010	if (type != video->type)
1011		return -EINVAL;
1012
1013	mutex_lock(&video->stream_lock);
1014
1015	/* Start streaming on the pipeline. No link touching an entity in the
1016	 * pipeline can be activated or deactivated once streaming is started.
1017	 */
1018	pipe = video->video.entity.pipe
1019	     ? to_isp_pipeline(&video->video.entity) : &video->pipe;
1020
1021	pipe->entities = 0;
1022
1023	if (video->isp->pdata->set_constraints)
1024		video->isp->pdata->set_constraints(video->isp, true);
1025	pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1026	pipe->max_rate = pipe->l3_ick;
1027
1028	ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
1029	if (ret < 0)
1030		goto err_pipeline_start;
1031
1032	/* Verify that the currently configured format matches the output of
1033	 * the connected subdev.
1034	 */
1035	ret = isp_video_check_format(video, vfh);
1036	if (ret < 0)
1037		goto err_check_format;
1038
1039	video->bpl_padding = ret;
1040	video->bpl_value = vfh->format.fmt.pix.bytesperline;
1041
1042	ret = isp_video_get_graph_data(video, pipe);
1043	if (ret < 0)
1044		goto err_check_format;
1045
1046	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1047		state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
1048	else
1049		state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1050
1051	ret = isp_video_check_external_subdevs(video, pipe);
1052	if (ret < 0)
1053		goto err_check_format;
1054
1055	pipe->error = false;
1056
1057	spin_lock_irqsave(&pipe->lock, flags);
1058	pipe->state &= ~ISP_PIPELINE_STREAM;
1059	pipe->state |= state;
1060	spin_unlock_irqrestore(&pipe->lock, flags);
1061
1062	/* Set the maximum time per frame as the value requested by userspace.
1063	 * This is a soft limit that can be overridden if the hardware doesn't
1064	 * support the request limit.
1065	 */
1066	if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1067		pipe->max_timeperframe = vfh->timeperframe;
1068
1069	video->queue = &vfh->queue;
1070	INIT_LIST_HEAD(&video->dmaqueue);
1071	atomic_set(&pipe->frame_number, -1);
1072	pipe->field = vfh->format.fmt.pix.field;
1073
1074	mutex_lock(&video->queue_lock);
1075	ret = vb2_streamon(&vfh->queue, type);
1076	mutex_unlock(&video->queue_lock);
1077	if (ret < 0)
1078		goto err_check_format;
1079
1080	/* In sensor-to-memory mode, the stream can be started synchronously
1081	 * to the stream on command. In memory-to-memory mode, it will be
1082	 * started when buffers are queued on both the input and output.
1083	 */
1084	if (pipe->input == NULL) {
1085		ret = omap3isp_pipeline_set_stream(pipe,
1086					      ISP_PIPELINE_STREAM_CONTINUOUS);
1087		if (ret < 0)
1088			goto err_set_stream;
1089		spin_lock_irqsave(&video->irqlock, flags);
1090		if (list_empty(&video->dmaqueue))
1091			video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1092		spin_unlock_irqrestore(&video->irqlock, flags);
1093	}
1094
1095	mutex_unlock(&video->stream_lock);
1096	return 0;
1097
1098err_set_stream:
1099	mutex_lock(&video->queue_lock);
1100	vb2_streamoff(&vfh->queue, type);
1101	mutex_unlock(&video->queue_lock);
1102err_check_format:
1103	media_entity_pipeline_stop(&video->video.entity);
1104err_pipeline_start:
1105	if (video->isp->pdata->set_constraints)
1106		video->isp->pdata->set_constraints(video->isp, false);
1107	/* The DMA queue must be emptied here, otherwise CCDC interrupts that
1108	 * will get triggered the next time the CCDC is powered up will try to
1109	 * access buffers that might have been freed but still present in the
1110	 * DMA queue. This can easily get triggered if the above
1111	 * omap3isp_pipeline_set_stream() call fails on a system with a
1112	 * free-running sensor.
1113	 */
1114	INIT_LIST_HEAD(&video->dmaqueue);
1115	video->queue = NULL;
1116
1117	mutex_unlock(&video->stream_lock);
1118	return ret;
1119}
1120
1121static int
1122isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1123{
1124	struct isp_video_fh *vfh = to_isp_video_fh(fh);
1125	struct isp_video *video = video_drvdata(file);
1126	struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1127	enum isp_pipeline_state state;
1128	unsigned int streaming;
1129	unsigned long flags;
1130
1131	if (type != video->type)
1132		return -EINVAL;
1133
1134	mutex_lock(&video->stream_lock);
1135
1136	/* Make sure we're not streaming yet. */
1137	mutex_lock(&video->queue_lock);
1138	streaming = vb2_is_streaming(&vfh->queue);
1139	mutex_unlock(&video->queue_lock);
1140
1141	if (!streaming)
1142		goto done;
1143
1144	/* Update the pipeline state. */
1145	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1146		state = ISP_PIPELINE_STREAM_OUTPUT
1147		      | ISP_PIPELINE_QUEUE_OUTPUT;
1148	else
1149		state = ISP_PIPELINE_STREAM_INPUT
1150		      | ISP_PIPELINE_QUEUE_INPUT;
1151
1152	spin_lock_irqsave(&pipe->lock, flags);
1153	pipe->state &= ~state;
1154	spin_unlock_irqrestore(&pipe->lock, flags);
1155
1156	/* Stop the stream. */
1157	omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1158	omap3isp_video_cancel_stream(video);
1159
1160	mutex_lock(&video->queue_lock);
1161	vb2_streamoff(&vfh->queue, type);
1162	mutex_unlock(&video->queue_lock);
1163	video->queue = NULL;
1164	video->error = false;
1165
1166	if (video->isp->pdata->set_constraints)
1167		video->isp->pdata->set_constraints(video->isp, false);
1168	media_entity_pipeline_stop(&video->video.entity);
1169
1170done:
1171	mutex_unlock(&video->stream_lock);
1172	return 0;
1173}
1174
1175static int
1176isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1177{
1178	if (input->index > 0)
1179		return -EINVAL;
1180
1181	strlcpy(input->name, "camera", sizeof(input->name));
1182	input->type = V4L2_INPUT_TYPE_CAMERA;
1183
1184	return 0;
1185}
1186
1187static int
1188isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1189{
1190	*input = 0;
1191
1192	return 0;
1193}
1194
1195static int
1196isp_video_s_input(struct file *file, void *fh, unsigned int input)
1197{
1198	return input == 0 ? 0 : -EINVAL;
1199}
1200
1201static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1202	.vidioc_querycap		= isp_video_querycap,
1203	.vidioc_g_fmt_vid_cap		= isp_video_get_format,
1204	.vidioc_s_fmt_vid_cap		= isp_video_set_format,
1205	.vidioc_try_fmt_vid_cap		= isp_video_try_format,
1206	.vidioc_g_fmt_vid_out		= isp_video_get_format,
1207	.vidioc_s_fmt_vid_out		= isp_video_set_format,
1208	.vidioc_try_fmt_vid_out		= isp_video_try_format,
1209	.vidioc_cropcap			= isp_video_cropcap,
1210	.vidioc_g_crop			= isp_video_get_crop,
1211	.vidioc_s_crop			= isp_video_set_crop,
1212	.vidioc_g_parm			= isp_video_get_param,
1213	.vidioc_s_parm			= isp_video_set_param,
1214	.vidioc_reqbufs			= isp_video_reqbufs,
1215	.vidioc_querybuf		= isp_video_querybuf,
1216	.vidioc_qbuf			= isp_video_qbuf,
1217	.vidioc_dqbuf			= isp_video_dqbuf,
1218	.vidioc_streamon		= isp_video_streamon,
1219	.vidioc_streamoff		= isp_video_streamoff,
1220	.vidioc_enum_input		= isp_video_enum_input,
1221	.vidioc_g_input			= isp_video_g_input,
1222	.vidioc_s_input			= isp_video_s_input,
1223};
1224
1225/* -----------------------------------------------------------------------------
1226 * V4L2 file operations
1227 */
1228
1229static int isp_video_open(struct file *file)
1230{
1231	struct isp_video *video = video_drvdata(file);
1232	struct isp_video_fh *handle;
1233	struct vb2_queue *queue;
1234	int ret = 0;
1235
1236	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1237	if (handle == NULL)
1238		return -ENOMEM;
1239
1240	v4l2_fh_init(&handle->vfh, &video->video);
1241	v4l2_fh_add(&handle->vfh);
1242
1243	/* If this is the first user, initialise the pipeline. */
1244	if (omap3isp_get(video->isp) == NULL) {
1245		ret = -EBUSY;
1246		goto done;
1247	}
1248
1249	ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1250	if (ret < 0) {
1251		omap3isp_put(video->isp);
1252		goto done;
1253	}
1254
1255	queue = &handle->queue;
1256	queue->type = video->type;
1257	queue->io_modes = VB2_MMAP | VB2_USERPTR;
1258	queue->drv_priv = handle;
1259	queue->ops = &isp_video_queue_ops;
1260	queue->mem_ops = &vb2_dma_contig_memops;
1261	queue->buf_struct_size = sizeof(struct isp_buffer);
1262	queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1263
1264	ret = vb2_queue_init(&handle->queue);
1265	if (ret < 0) {
1266		omap3isp_put(video->isp);
1267		goto done;
1268	}
1269
1270	memset(&handle->format, 0, sizeof(handle->format));
1271	handle->format.type = video->type;
1272	handle->timeperframe.denominator = 1;
1273
1274	handle->video = video;
1275	file->private_data = &handle->vfh;
1276
1277done:
1278	if (ret < 0) {
1279		v4l2_fh_del(&handle->vfh);
1280		kfree(handle);
1281	}
1282
1283	return ret;
1284}
1285
1286static int isp_video_release(struct file *file)
1287{
1288	struct isp_video *video = video_drvdata(file);
1289	struct v4l2_fh *vfh = file->private_data;
1290	struct isp_video_fh *handle = to_isp_video_fh(vfh);
1291
1292	/* Disable streaming and free the buffers queue resources. */
1293	isp_video_streamoff(file, vfh, video->type);
1294
1295	mutex_lock(&video->queue_lock);
1296	vb2_queue_release(&handle->queue);
1297	mutex_unlock(&video->queue_lock);
1298
1299	omap3isp_pipeline_pm_use(&video->video.entity, 0);
1300
1301	/* Release the file handle. */
1302	v4l2_fh_del(vfh);
1303	kfree(handle);
1304	file->private_data = NULL;
1305
1306	omap3isp_put(video->isp);
1307
1308	return 0;
1309}
1310
1311static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1312{
1313	struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1314	struct isp_video *video = video_drvdata(file);
1315	int ret;
1316
1317	mutex_lock(&video->queue_lock);
1318	ret = vb2_poll(&vfh->queue, file, wait);
1319	mutex_unlock(&video->queue_lock);
1320
1321	return ret;
1322}
1323
1324static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1325{
1326	struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1327	struct isp_video *video = video_drvdata(file);
1328	int ret;
1329
1330	mutex_lock(&video->queue_lock);
1331	ret = vb2_mmap(&vfh->queue, vma);
1332	mutex_unlock(&video->queue_lock);
1333
1334	return ret;
1335}
1336
1337static struct v4l2_file_operations isp_video_fops = {
1338	.owner = THIS_MODULE,
1339	.unlocked_ioctl = video_ioctl2,
1340	.open = isp_video_open,
1341	.release = isp_video_release,
1342	.poll = isp_video_poll,
1343	.mmap = isp_video_mmap,
1344};
1345
1346/* -----------------------------------------------------------------------------
1347 * ISP video core
1348 */
1349
1350static const struct isp_video_operations isp_video_dummy_ops = {
1351};
1352
1353int omap3isp_video_init(struct isp_video *video, const char *name)
1354{
1355	const char *direction;
1356	int ret;
1357
1358	switch (video->type) {
1359	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1360		direction = "output";
1361		video->pad.flags = MEDIA_PAD_FL_SINK
1362				   | MEDIA_PAD_FL_MUST_CONNECT;
1363		break;
1364	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1365		direction = "input";
1366		video->pad.flags = MEDIA_PAD_FL_SOURCE
1367				   | MEDIA_PAD_FL_MUST_CONNECT;
1368		video->video.vfl_dir = VFL_DIR_TX;
1369		break;
1370
1371	default:
1372		return -EINVAL;
1373	}
1374
1375	video->alloc_ctx = vb2_dma_contig_init_ctx(video->isp->dev);
1376	if (IS_ERR(video->alloc_ctx))
1377		return PTR_ERR(video->alloc_ctx);
1378
1379	ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1380	if (ret < 0) {
1381		vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1382		return ret;
1383	}
1384
1385	mutex_init(&video->mutex);
1386	atomic_set(&video->active, 0);
1387
1388	spin_lock_init(&video->pipe.lock);
1389	mutex_init(&video->stream_lock);
1390	mutex_init(&video->queue_lock);
1391	spin_lock_init(&video->irqlock);
1392
1393	/* Initialize the video device. */
1394	if (video->ops == NULL)
1395		video->ops = &isp_video_dummy_ops;
1396
1397	video->video.fops = &isp_video_fops;
1398	snprintf(video->video.name, sizeof(video->video.name),
1399		 "OMAP3 ISP %s %s", name, direction);
1400	video->video.vfl_type = VFL_TYPE_GRABBER;
1401	video->video.release = video_device_release_empty;
1402	video->video.ioctl_ops = &isp_video_ioctl_ops;
1403	video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1404
1405	video_set_drvdata(&video->video, video);
1406
1407	return 0;
1408}
1409
1410void omap3isp_video_cleanup(struct isp_video *video)
1411{
1412	vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1413	media_entity_cleanup(&video->video.entity);
1414	mutex_destroy(&video->queue_lock);
1415	mutex_destroy(&video->stream_lock);
1416	mutex_destroy(&video->mutex);
1417}
1418
1419int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1420{
1421	int ret;
1422
1423	video->video.v4l2_dev = vdev;
1424
1425	ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1426	if (ret < 0)
1427		dev_err(video->isp->dev,
1428			"%s: could not register video device (%d)\n",
1429			__func__, ret);
1430
1431	return ret;
1432}
1433
1434void omap3isp_video_unregister(struct isp_video *video)
1435{
1436	if (video_is_registered(&video->video))
1437		video_unregister_device(&video->video);
1438}
1439