]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/media/omap4iss/iss_video.c
Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney...
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / media / omap4iss / iss_video.c
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
33 static struct iss_format_info formats[] = {
34 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
35 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
36 V4L2_PIX_FMT_GREY, 8, "Greyscale 8 bpp", },
37 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
38 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
39 V4L2_PIX_FMT_Y10, 10, "Greyscale 10 bpp", },
40 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
41 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
42 V4L2_PIX_FMT_Y12, 12, "Greyscale 12 bpp", },
43 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
44 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
45 V4L2_PIX_FMT_SBGGR8, 8, "BGGR Bayer 8 bpp", },
46 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
47 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
48 V4L2_PIX_FMT_SGBRG8, 8, "GBRG Bayer 8 bpp", },
49 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
50 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
51 V4L2_PIX_FMT_SGRBG8, 8, "GRBG Bayer 8 bpp", },
52 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
53 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
54 V4L2_PIX_FMT_SRGGB8, 8, "RGGB Bayer 8 bpp", },
55 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
56 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
57 V4L2_PIX_FMT_SGRBG10DPCM8, 8, "GRBG Bayer 10 bpp DPCM8", },
58 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
59 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
60 V4L2_PIX_FMT_SBGGR10, 10, "BGGR Bayer 10 bpp", },
61 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
62 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
63 V4L2_PIX_FMT_SGBRG10, 10, "GBRG Bayer 10 bpp", },
64 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
65 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
66 V4L2_PIX_FMT_SGRBG10, 10, "GRBG Bayer 10 bpp", },
67 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
68 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
69 V4L2_PIX_FMT_SRGGB10, 10, "RGGB Bayer 10 bpp", },
70 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
71 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
72 V4L2_PIX_FMT_SBGGR12, 12, "BGGR Bayer 12 bpp", },
73 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
74 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
75 V4L2_PIX_FMT_SGBRG12, 12, "GBRG Bayer 12 bpp", },
76 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
77 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
78 V4L2_PIX_FMT_SGRBG12, 12, "GRBG Bayer 12 bpp", },
79 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
80 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
81 V4L2_PIX_FMT_SRGGB12, 12, "RGGB Bayer 12 bpp", },
82 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
83 MEDIA_BUS_FMT_UYVY8_1X16, 0,
84 V4L2_PIX_FMT_UYVY, 16, "YUV 4:2:2 (UYVY)", },
85 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
86 MEDIA_BUS_FMT_YUYV8_1X16, 0,
87 V4L2_PIX_FMT_YUYV, 16, "YUV 4:2:2 (YUYV)", },
88 { MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8,
89 MEDIA_BUS_FMT_YUYV8_1_5X8, 0,
90 V4L2_PIX_FMT_NV12, 8, "YUV 4:2:0 (NV12)", },
91 };
92
93 const struct iss_format_info *
94 omap4iss_video_format_info(u32 code)
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 */
118 static 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
165 static 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
174 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
175 if (formats[i].pixelformat == pix->pixelformat)
176 break;
177 }
178
179 if (WARN_ON(i == ARRAY_SIZE(formats)))
180 return;
181
182 mbus->code = formats[i].code;
183 mbus->colorspace = pix->colorspace;
184 mbus->field = pix->field;
185 }
186
187 static struct v4l2_subdev *
188 iss_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. */
205 static struct iss_video *
206 iss_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
234 static int
235 __iss_video_get_format(struct iss_video *video,
236 struct v4l2_mbus_framefmt *format)
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
247 memset(&fmt, 0, sizeof(fmt));
248 fmt.pad = pad;
249 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
250
251 mutex_lock(&video->mutex);
252 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
253 mutex_unlock(&video->mutex);
254
255 if (ret)
256 return ret;
257
258 *format = fmt.format;
259 return 0;
260 }
261
262 static int
263 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
264 {
265 struct v4l2_mbus_framefmt format;
266 struct v4l2_pix_format pixfmt;
267 int ret;
268
269 ret = __iss_video_get_format(video, &format);
270 if (ret < 0)
271 return ret;
272
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)
281 return -EINVAL;
282
283 return ret;
284 }
285
286 /* -----------------------------------------------------------------------------
287 * Video queue operations
288 */
289
290 static int iss_video_queue_setup(struct vb2_queue *vq,
291 const struct v4l2_format *fmt,
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
307 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
308
309 return 0;
310 }
311
312 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
313 {
314 struct iss_buffer *buffer = container_of(vb, struct iss_buffer, vb);
315
316 if (buffer->iss_addr)
317 buffer->iss_addr = 0;
318 }
319
320 static 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)) {
333 dev_dbg(video->iss->dev,
334 "Buffer address must be aligned to 32 bytes boundary.\n");
335 return -EINVAL;
336 }
337
338 vb2_set_plane_payload(vb, 0, size);
339 buffer->iss_addr = addr;
340 return 0;
341 }
342
343 static 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);
349 unsigned long flags;
350 bool empty;
351
352 spin_lock_irqsave(&video->qlock, flags);
353
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 */
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
365 empty = list_empty(&video->dmaqueue);
366 list_add_tail(&buffer->list, &video->dmaqueue);
367
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
395 static const struct vb2_ops iss_video_vb2ops = {
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 */
417 struct 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;
423 struct timespec ts;
424
425 spin_lock_irqsave(&video->qlock, flags);
426 if (WARN_ON(list_empty(&video->dmaqueue))) {
427 spin_unlock_irqrestore(&video->qlock, flags);
428 return NULL;
429 }
430
431 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
432 list);
433 list_del(&buf->list);
434 spin_unlock_irqrestore(&video->qlock, flags);
435
436 ktime_get_ts(&ts);
437 buf->vb.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
438 buf->vb.v4l2_buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
439
440 /* Do frame number propagation only if this is the output video node.
441 * Frame number either comes from the CSI receivers or it gets
442 * incremented here if H3A is not active.
443 * Note: There is no guarantee that the output buffer will finish
444 * first, so the input number might lag behind by 1 in some cases.
445 */
446 if (video == pipe->output && !pipe->do_propagation)
447 buf->vb.v4l2_buf.sequence =
448 atomic_inc_return(&pipe->frame_number);
449 else
450 buf->vb.v4l2_buf.sequence = atomic_read(&pipe->frame_number);
451
452 vb2_buffer_done(&buf->vb, pipe->error ?
453 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
454 pipe->error = false;
455
456 spin_lock_irqsave(&video->qlock, flags);
457 if (list_empty(&video->dmaqueue)) {
458 spin_unlock_irqrestore(&video->qlock, flags);
459 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
460 state = ISS_PIPELINE_QUEUE_OUTPUT
461 | ISS_PIPELINE_STREAM;
462 else
463 state = ISS_PIPELINE_QUEUE_INPUT
464 | ISS_PIPELINE_STREAM;
465
466 spin_lock_irqsave(&pipe->lock, flags);
467 pipe->state &= ~state;
468 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
469 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
470 spin_unlock_irqrestore(&pipe->lock, flags);
471 return NULL;
472 }
473
474 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
475 spin_lock(&pipe->lock);
476 pipe->state &= ~ISS_PIPELINE_STREAM;
477 spin_unlock(&pipe->lock);
478 }
479
480 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
481 list);
482 spin_unlock_irqrestore(&video->qlock, flags);
483 buf->vb.state = VB2_BUF_STATE_ACTIVE;
484 return buf;
485 }
486
487 /*
488 * omap4iss_video_cancel_stream - Cancel stream on a video node
489 * @video: ISS video object
490 *
491 * Cancelling a stream mark all buffers on the video node as erroneous and makes
492 * sure no new buffer can be queued.
493 */
494 void omap4iss_video_cancel_stream(struct iss_video *video)
495 {
496 unsigned long flags;
497
498 spin_lock_irqsave(&video->qlock, flags);
499
500 while (!list_empty(&video->dmaqueue)) {
501 struct iss_buffer *buf;
502
503 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
504 list);
505 list_del(&buf->list);
506 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
507 }
508
509 vb2_queue_error(video->queue);
510 video->error = true;
511
512 spin_unlock_irqrestore(&video->qlock, flags);
513 }
514
515 /* -----------------------------------------------------------------------------
516 * V4L2 ioctls
517 */
518
519 static int
520 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
521 {
522 struct iss_video *video = video_drvdata(file);
523
524 strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
525 strlcpy(cap->card, video->video.name, sizeof(cap->card));
526 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
527
528 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
529 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
530 else
531 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
532
533 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
534 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
535
536 return 0;
537 }
538
539 static int
540 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
541 {
542 struct iss_video *video = video_drvdata(file);
543 struct v4l2_mbus_framefmt format;
544 unsigned int index = f->index;
545 unsigned int i;
546 int ret;
547
548 if (f->type != video->type)
549 return -EINVAL;
550
551 ret = __iss_video_get_format(video, &format);
552 if (ret < 0)
553 return ret;
554
555 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
556 const struct iss_format_info *info = &formats[i];
557
558 if (format.code != info->code)
559 continue;
560
561 if (index == 0) {
562 f->pixelformat = info->pixelformat;
563 strlcpy(f->description, info->description,
564 sizeof(f->description));
565 return 0;
566 }
567
568 index--;
569 }
570
571 return -EINVAL;
572 }
573
574 static int
575 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
576 {
577 struct iss_video_fh *vfh = to_iss_video_fh(fh);
578 struct iss_video *video = video_drvdata(file);
579
580 if (format->type != video->type)
581 return -EINVAL;
582
583 mutex_lock(&video->mutex);
584 *format = vfh->format;
585 mutex_unlock(&video->mutex);
586
587 return 0;
588 }
589
590 static int
591 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
592 {
593 struct iss_video_fh *vfh = to_iss_video_fh(fh);
594 struct iss_video *video = video_drvdata(file);
595 struct v4l2_mbus_framefmt fmt;
596
597 if (format->type != video->type)
598 return -EINVAL;
599
600 mutex_lock(&video->mutex);
601
602 /* Fill the bytesperline and sizeimage fields by converting to media bus
603 * format and back to pixel format.
604 */
605 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
606 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
607
608 vfh->format = *format;
609
610 mutex_unlock(&video->mutex);
611 return 0;
612 }
613
614 static int
615 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
616 {
617 struct iss_video *video = video_drvdata(file);
618 struct v4l2_subdev_format fmt;
619 struct v4l2_subdev *subdev;
620 u32 pad;
621 int ret;
622
623 if (format->type != video->type)
624 return -EINVAL;
625
626 subdev = iss_video_remote_subdev(video, &pad);
627 if (subdev == NULL)
628 return -EINVAL;
629
630 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
631
632 fmt.pad = pad;
633 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
634 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
635 if (ret)
636 return ret;
637
638 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
639 return 0;
640 }
641
642 static int
643 iss_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
644 {
645 struct iss_video *video = video_drvdata(file);
646 struct v4l2_subdev *subdev;
647 int ret;
648
649 subdev = iss_video_remote_subdev(video, NULL);
650 if (subdev == NULL)
651 return -EINVAL;
652
653 mutex_lock(&video->mutex);
654 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
655 mutex_unlock(&video->mutex);
656
657 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
658 }
659
660 static int
661 iss_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
662 {
663 struct iss_video *video = video_drvdata(file);
664 struct v4l2_subdev_format format;
665 struct v4l2_subdev *subdev;
666 u32 pad;
667 int ret;
668
669 subdev = iss_video_remote_subdev(video, &pad);
670 if (subdev == NULL)
671 return -EINVAL;
672
673 /* Try the get crop operation first and fallback to get format if not
674 * implemented.
675 */
676 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
677 if (ret != -ENOIOCTLCMD)
678 return ret;
679
680 format.pad = pad;
681 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
682 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
683 if (ret < 0)
684 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
685
686 crop->c.left = 0;
687 crop->c.top = 0;
688 crop->c.width = format.format.width;
689 crop->c.height = format.format.height;
690
691 return 0;
692 }
693
694 static int
695 iss_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
696 {
697 struct iss_video *video = video_drvdata(file);
698 struct v4l2_subdev *subdev;
699 int ret;
700
701 subdev = iss_video_remote_subdev(video, NULL);
702 if (subdev == NULL)
703 return -EINVAL;
704
705 mutex_lock(&video->mutex);
706 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
707 mutex_unlock(&video->mutex);
708
709 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
710 }
711
712 static int
713 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
714 {
715 struct iss_video_fh *vfh = to_iss_video_fh(fh);
716 struct iss_video *video = video_drvdata(file);
717
718 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
719 video->type != a->type)
720 return -EINVAL;
721
722 memset(a, 0, sizeof(*a));
723 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
724 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
725 a->parm.output.timeperframe = vfh->timeperframe;
726
727 return 0;
728 }
729
730 static int
731 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
732 {
733 struct iss_video_fh *vfh = to_iss_video_fh(fh);
734 struct iss_video *video = video_drvdata(file);
735
736 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
737 video->type != a->type)
738 return -EINVAL;
739
740 if (a->parm.output.timeperframe.denominator == 0)
741 a->parm.output.timeperframe.denominator = 1;
742
743 vfh->timeperframe = a->parm.output.timeperframe;
744
745 return 0;
746 }
747
748 static int
749 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
750 {
751 struct iss_video_fh *vfh = to_iss_video_fh(fh);
752
753 return vb2_reqbufs(&vfh->queue, rb);
754 }
755
756 static int
757 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
758 {
759 struct iss_video_fh *vfh = to_iss_video_fh(fh);
760
761 return vb2_querybuf(&vfh->queue, b);
762 }
763
764 static int
765 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
766 {
767 struct iss_video_fh *vfh = to_iss_video_fh(fh);
768
769 return vb2_qbuf(&vfh->queue, b);
770 }
771
772 static int
773 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
774 {
775 struct iss_video_fh *vfh = to_iss_video_fh(fh);
776
777 return vb2_expbuf(&vfh->queue, e);
778 }
779
780 static int
781 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
782 {
783 struct iss_video_fh *vfh = to_iss_video_fh(fh);
784
785 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
786 }
787
788 /*
789 * Stream management
790 *
791 * Every ISS pipeline has a single input and a single output. The input can be
792 * either a sensor or a video node. The output is always a video node.
793 *
794 * As every pipeline has an output video node, the ISS video objects at the
795 * pipeline output stores the pipeline state. It tracks the streaming state of
796 * both the input and output, as well as the availability of buffers.
797 *
798 * In sensor-to-memory mode, frames are always available at the pipeline input.
799 * Starting the sensor usually requires I2C transfers and must be done in
800 * interruptible context. The pipeline is started and stopped synchronously
801 * to the stream on/off commands. All modules in the pipeline will get their
802 * subdev set stream handler called. The module at the end of the pipeline must
803 * delay starting the hardware until buffers are available at its output.
804 *
805 * In memory-to-memory mode, starting/stopping the stream requires
806 * synchronization between the input and output. ISS modules can't be stopped
807 * in the middle of a frame, and at least some of the modules seem to become
808 * busy as soon as they're started, even if they don't receive a frame start
809 * event. For that reason frames need to be processed in single-shot mode. The
810 * driver needs to wait until a frame is completely processed and written to
811 * memory before restarting the pipeline for the next frame. Pipelined
812 * processing might be possible but requires more testing.
813 *
814 * Stream start must be delayed until buffers are available at both the input
815 * and output. The pipeline must be started in the videobuf queue callback with
816 * the buffers queue spinlock held. The modules subdev set stream operation must
817 * not sleep.
818 */
819 static int
820 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
821 {
822 struct iss_video_fh *vfh = to_iss_video_fh(fh);
823 struct iss_video *video = video_drvdata(file);
824 struct media_entity_graph graph;
825 struct media_entity *entity;
826 enum iss_pipeline_state state;
827 struct iss_pipeline *pipe;
828 struct iss_video *far_end;
829 unsigned long flags;
830 int ret;
831
832 if (type != video->type)
833 return -EINVAL;
834
835 mutex_lock(&video->stream_lock);
836
837 /* Start streaming on the pipeline. No link touching an entity in the
838 * pipeline can be activated or deactivated once streaming is started.
839 */
840 pipe = video->video.entity.pipe
841 ? to_iss_pipeline(&video->video.entity) : &video->pipe;
842 pipe->external = NULL;
843 pipe->external_rate = 0;
844 pipe->external_bpp = 0;
845 pipe->entities = 0;
846
847 if (video->iss->pdata->set_constraints)
848 video->iss->pdata->set_constraints(video->iss, true);
849
850 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
851 if (ret < 0)
852 goto err_media_entity_pipeline_start;
853
854 entity = &video->video.entity;
855 media_entity_graph_walk_start(&graph, entity);
856 while ((entity = media_entity_graph_walk_next(&graph)))
857 pipe->entities |= 1 << entity->id;
858
859 /* Verify that the currently configured format matches the output of
860 * the connected subdev.
861 */
862 ret = iss_video_check_format(video, vfh);
863 if (ret < 0)
864 goto err_iss_video_check_format;
865
866 video->bpl_padding = ret;
867 video->bpl_value = vfh->format.fmt.pix.bytesperline;
868
869 /* Find the ISS video node connected at the far end of the pipeline and
870 * update the pipeline.
871 */
872 far_end = iss_video_far_end(video);
873
874 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
875 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
876 pipe->input = far_end;
877 pipe->output = video;
878 } else {
879 if (far_end == NULL) {
880 ret = -EPIPE;
881 goto err_iss_video_check_format;
882 }
883
884 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
885 pipe->input = video;
886 pipe->output = far_end;
887 }
888
889 spin_lock_irqsave(&pipe->lock, flags);
890 pipe->state &= ~ISS_PIPELINE_STREAM;
891 pipe->state |= state;
892 spin_unlock_irqrestore(&pipe->lock, flags);
893
894 /* Set the maximum time per frame as the value requested by userspace.
895 * This is a soft limit that can be overridden if the hardware doesn't
896 * support the request limit.
897 */
898 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
899 pipe->max_timeperframe = vfh->timeperframe;
900
901 video->queue = &vfh->queue;
902 INIT_LIST_HEAD(&video->dmaqueue);
903 video->error = false;
904 atomic_set(&pipe->frame_number, -1);
905
906 ret = vb2_streamon(&vfh->queue, type);
907 if (ret < 0)
908 goto err_iss_video_check_format;
909
910 /* In sensor-to-memory mode, the stream can be started synchronously
911 * to the stream on command. In memory-to-memory mode, it will be
912 * started when buffers are queued on both the input and output.
913 */
914 if (pipe->input == NULL) {
915 unsigned long flags;
916
917 ret = omap4iss_pipeline_set_stream(pipe,
918 ISS_PIPELINE_STREAM_CONTINUOUS);
919 if (ret < 0)
920 goto err_omap4iss_set_stream;
921 spin_lock_irqsave(&video->qlock, flags);
922 if (list_empty(&video->dmaqueue))
923 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
924 spin_unlock_irqrestore(&video->qlock, flags);
925 }
926
927 mutex_unlock(&video->stream_lock);
928 return 0;
929
930 err_omap4iss_set_stream:
931 vb2_streamoff(&vfh->queue, type);
932 err_iss_video_check_format:
933 media_entity_pipeline_stop(&video->video.entity);
934 err_media_entity_pipeline_start:
935 if (video->iss->pdata->set_constraints)
936 video->iss->pdata->set_constraints(video->iss, false);
937 video->queue = NULL;
938
939 mutex_unlock(&video->stream_lock);
940 return ret;
941 }
942
943 static int
944 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
945 {
946 struct iss_video_fh *vfh = to_iss_video_fh(fh);
947 struct iss_video *video = video_drvdata(file);
948 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
949 enum iss_pipeline_state state;
950 unsigned long flags;
951
952 if (type != video->type)
953 return -EINVAL;
954
955 mutex_lock(&video->stream_lock);
956
957 if (!vb2_is_streaming(&vfh->queue))
958 goto done;
959
960 /* Update the pipeline state. */
961 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
962 state = ISS_PIPELINE_STREAM_OUTPUT
963 | ISS_PIPELINE_QUEUE_OUTPUT;
964 else
965 state = ISS_PIPELINE_STREAM_INPUT
966 | ISS_PIPELINE_QUEUE_INPUT;
967
968 spin_lock_irqsave(&pipe->lock, flags);
969 pipe->state &= ~state;
970 spin_unlock_irqrestore(&pipe->lock, flags);
971
972 /* Stop the stream. */
973 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
974 vb2_streamoff(&vfh->queue, type);
975 video->queue = NULL;
976
977 if (video->iss->pdata->set_constraints)
978 video->iss->pdata->set_constraints(video->iss, false);
979 media_entity_pipeline_stop(&video->video.entity);
980
981 done:
982 mutex_unlock(&video->stream_lock);
983 return 0;
984 }
985
986 static int
987 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
988 {
989 if (input->index > 0)
990 return -EINVAL;
991
992 strlcpy(input->name, "camera", sizeof(input->name));
993 input->type = V4L2_INPUT_TYPE_CAMERA;
994
995 return 0;
996 }
997
998 static int
999 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1000 {
1001 *input = 0;
1002
1003 return 0;
1004 }
1005
1006 static int
1007 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1008 {
1009 return input == 0 ? 0 : -EINVAL;
1010 }
1011
1012 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1013 .vidioc_querycap = iss_video_querycap,
1014 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
1015 .vidioc_g_fmt_vid_cap = iss_video_get_format,
1016 .vidioc_s_fmt_vid_cap = iss_video_set_format,
1017 .vidioc_try_fmt_vid_cap = iss_video_try_format,
1018 .vidioc_g_fmt_vid_out = iss_video_get_format,
1019 .vidioc_s_fmt_vid_out = iss_video_set_format,
1020 .vidioc_try_fmt_vid_out = iss_video_try_format,
1021 .vidioc_cropcap = iss_video_cropcap,
1022 .vidioc_g_crop = iss_video_get_crop,
1023 .vidioc_s_crop = iss_video_set_crop,
1024 .vidioc_g_parm = iss_video_get_param,
1025 .vidioc_s_parm = iss_video_set_param,
1026 .vidioc_reqbufs = iss_video_reqbufs,
1027 .vidioc_querybuf = iss_video_querybuf,
1028 .vidioc_qbuf = iss_video_qbuf,
1029 .vidioc_expbuf = iss_video_expbuf,
1030 .vidioc_dqbuf = iss_video_dqbuf,
1031 .vidioc_streamon = iss_video_streamon,
1032 .vidioc_streamoff = iss_video_streamoff,
1033 .vidioc_enum_input = iss_video_enum_input,
1034 .vidioc_g_input = iss_video_g_input,
1035 .vidioc_s_input = iss_video_s_input,
1036 };
1037
1038 /* -----------------------------------------------------------------------------
1039 * V4L2 file operations
1040 */
1041
1042 static int iss_video_open(struct file *file)
1043 {
1044 struct iss_video *video = video_drvdata(file);
1045 struct iss_video_fh *handle;
1046 struct vb2_queue *q;
1047 int ret = 0;
1048
1049 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1050 if (handle == NULL)
1051 return -ENOMEM;
1052
1053 v4l2_fh_init(&handle->vfh, &video->video);
1054 v4l2_fh_add(&handle->vfh);
1055
1056 /* If this is the first user, initialise the pipeline. */
1057 if (omap4iss_get(video->iss) == NULL) {
1058 ret = -EBUSY;
1059 goto done;
1060 }
1061
1062 ret = omap4iss_pipeline_pm_use(&video->video.entity, 1);
1063 if (ret < 0) {
1064 omap4iss_put(video->iss);
1065 goto done;
1066 }
1067
1068 video->alloc_ctx = vb2_dma_contig_init_ctx(video->iss->dev);
1069 if (IS_ERR(video->alloc_ctx)) {
1070 ret = PTR_ERR(video->alloc_ctx);
1071 omap4iss_put(video->iss);
1072 goto done;
1073 }
1074
1075 q = &handle->queue;
1076
1077 q->type = video->type;
1078 q->io_modes = VB2_MMAP | VB2_DMABUF;
1079 q->drv_priv = handle;
1080 q->ops = &iss_video_vb2ops;
1081 q->mem_ops = &vb2_dma_contig_memops;
1082 q->buf_struct_size = sizeof(struct iss_buffer);
1083 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1084
1085 ret = vb2_queue_init(q);
1086 if (ret) {
1087 omap4iss_put(video->iss);
1088 goto done;
1089 }
1090
1091 memset(&handle->format, 0, sizeof(handle->format));
1092 handle->format.type = video->type;
1093 handle->timeperframe.denominator = 1;
1094
1095 handle->video = video;
1096 file->private_data = &handle->vfh;
1097
1098 done:
1099 if (ret < 0) {
1100 v4l2_fh_del(&handle->vfh);
1101 kfree(handle);
1102 }
1103
1104 return ret;
1105 }
1106
1107 static int iss_video_release(struct file *file)
1108 {
1109 struct iss_video *video = video_drvdata(file);
1110 struct v4l2_fh *vfh = file->private_data;
1111 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1112
1113 /* Disable streaming and free the buffers queue resources. */
1114 iss_video_streamoff(file, vfh, video->type);
1115
1116 omap4iss_pipeline_pm_use(&video->video.entity, 0);
1117
1118 /* Release the videobuf2 queue */
1119 vb2_queue_release(&handle->queue);
1120
1121 /* Release the file handle. */
1122 v4l2_fh_del(vfh);
1123 kfree(handle);
1124 file->private_data = NULL;
1125
1126 omap4iss_put(video->iss);
1127
1128 return 0;
1129 }
1130
1131 static unsigned int iss_video_poll(struct file *file, poll_table *wait)
1132 {
1133 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1134
1135 return vb2_poll(&vfh->queue, file, wait);
1136 }
1137
1138 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1139 {
1140 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1141
1142 return vb2_mmap(&vfh->queue, vma);
1143 }
1144
1145 static struct v4l2_file_operations iss_video_fops = {
1146 .owner = THIS_MODULE,
1147 .unlocked_ioctl = video_ioctl2,
1148 .open = iss_video_open,
1149 .release = iss_video_release,
1150 .poll = iss_video_poll,
1151 .mmap = iss_video_mmap,
1152 };
1153
1154 /* -----------------------------------------------------------------------------
1155 * ISS video core
1156 */
1157
1158 static const struct iss_video_operations iss_video_dummy_ops = {
1159 };
1160
1161 int omap4iss_video_init(struct iss_video *video, const char *name)
1162 {
1163 const char *direction;
1164 int ret;
1165
1166 switch (video->type) {
1167 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1168 direction = "output";
1169 video->pad.flags = MEDIA_PAD_FL_SINK;
1170 break;
1171 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1172 direction = "input";
1173 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1174 break;
1175
1176 default:
1177 return -EINVAL;
1178 }
1179
1180 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1181 if (ret < 0)
1182 return ret;
1183
1184 spin_lock_init(&video->qlock);
1185 mutex_init(&video->mutex);
1186 atomic_set(&video->active, 0);
1187
1188 spin_lock_init(&video->pipe.lock);
1189 mutex_init(&video->stream_lock);
1190
1191 /* Initialize the video device. */
1192 if (video->ops == NULL)
1193 video->ops = &iss_video_dummy_ops;
1194
1195 video->video.fops = &iss_video_fops;
1196 snprintf(video->video.name, sizeof(video->video.name),
1197 "OMAP4 ISS %s %s", name, direction);
1198 video->video.vfl_type = VFL_TYPE_GRABBER;
1199 video->video.release = video_device_release_empty;
1200 video->video.ioctl_ops = &iss_video_ioctl_ops;
1201 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1202
1203 video_set_drvdata(&video->video, video);
1204
1205 return 0;
1206 }
1207
1208 void omap4iss_video_cleanup(struct iss_video *video)
1209 {
1210 media_entity_cleanup(&video->video.entity);
1211 mutex_destroy(&video->stream_lock);
1212 mutex_destroy(&video->mutex);
1213 }
1214
1215 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1216 {
1217 int ret;
1218
1219 video->video.v4l2_dev = vdev;
1220
1221 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1222 if (ret < 0)
1223 dev_err(video->iss->dev,
1224 "%s: could not register video device (%d)\n",
1225 __func__, ret);
1226
1227 return ret;
1228 }
1229
1230 void omap4iss_video_unregister(struct iss_video *video)
1231 {
1232 video_unregister_device(&video->video);
1233 }