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