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