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