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