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