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