blob: b463fe172d164a0f14400c42f13f33aa9dbc82dd [file] [log] [blame]
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001/*
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.
Laurent Pinchartad614ac2011-02-12 18:05:06 -030014 */
15
16#include <asm/cacheflush.h>
17#include <linux/clk.h>
18#include <linux/mm.h>
Dmitry Artamonow025521f2011-11-20 12:54:26 -030019#include <linux/module.h>
Laurent Pinchartad614ac2011-02-12 18:05:06 -030020#include <linux/pagemap.h>
21#include <linux/scatterlist.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <media/v4l2-dev.h>
26#include <media/v4l2-ioctl.h>
Laurent Pinchartfbac1402014-03-09 20:36:15 -030027#include <media/videobuf2-dma-contig.h>
Laurent Pinchartad614ac2011-02-12 18:05:06 -030028
29#include "ispvideo.h"
30#include "isp.h"
31
32
33/* -----------------------------------------------------------------------------
34 * Helper functions
35 */
36
Sakari Ailus8f4f2982011-12-28 06:28:41 -030037/*
38 * NOTE: When adding new media bus codes, always remember to add
39 * corresponding in-memory formats to the table below!!!
40 */
Laurent Pinchartad614ac2011-02-12 18:05:06 -030041static struct isp_format_info formats[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030042 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
43 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030044 V4L2_PIX_FMT_GREY, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030045 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
46 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030047 V4L2_PIX_FMT_Y10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030048 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
49 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030050 V4L2_PIX_FMT_Y12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030051 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
52 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030053 V4L2_PIX_FMT_SBGGR8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030054 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
55 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030056 V4L2_PIX_FMT_SGBRG8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030057 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
58 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030059 V4L2_PIX_FMT_SGRBG8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030060 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
61 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030062 V4L2_PIX_FMT_SRGGB8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030063 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
64 MEDIA_BUS_FMT_SBGGR10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030065 V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030066 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
67 MEDIA_BUS_FMT_SGBRG10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030068 V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030069 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
70 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030071 V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030072 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
73 MEDIA_BUS_FMT_SRGGB10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030074 V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030075 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
76 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030077 V4L2_PIX_FMT_SBGGR10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030078 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
79 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030080 V4L2_PIX_FMT_SGBRG10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030081 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
82 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030083 V4L2_PIX_FMT_SGRBG10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030084 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
85 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030086 V4L2_PIX_FMT_SRGGB10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030087 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
88 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030089 V4L2_PIX_FMT_SBGGR12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030090 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
91 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030092 V4L2_PIX_FMT_SGBRG12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030093 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
94 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030095 V4L2_PIX_FMT_SGRBG12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030096 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
97 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030098 V4L2_PIX_FMT_SRGGB12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030099 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
100 MEDIA_BUS_FMT_UYVY8_1X16, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -0300101 V4L2_PIX_FMT_UYVY, 16, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300102 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
103 MEDIA_BUS_FMT_YUYV8_1X16, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -0300104 V4L2_PIX_FMT_YUYV, 16, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300105 { MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_UYVY8_2X8,
106 MEDIA_BUS_FMT_UYVY8_2X8, 0,
Laurent Pinchart0810fd92012-05-24 11:55:00 -0300107 V4L2_PIX_FMT_UYVY, 8, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300108 { MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YUYV8_2X8,
109 MEDIA_BUS_FMT_YUYV8_2X8, 0,
Laurent Pinchart0810fd92012-05-24 11:55:00 -0300110 V4L2_PIX_FMT_YUYV, 8, 2, },
Laurent Pinchartc51364c2011-08-31 11:03:53 -0300111 /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
112 * module and avoid NULL pointer dereferences.
113 */
114 { 0, }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300115};
116
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300117const struct isp_format_info *omap3isp_video_format_info(u32 code)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300118{
119 unsigned int i;
120
121 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
122 if (formats[i].code == code)
123 return &formats[i];
124 }
125
126 return NULL;
127}
128
129/*
130 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
131 * @video: ISP video instance
132 * @mbus: v4l2_mbus_framefmt format (input)
133 * @pix: v4l2_pix_format format (output)
134 *
135 * Fill the output pix structure with information from the input mbus format.
136 * The bytesperline and sizeimage fields are computed from the requested bytes
137 * per line value in the pix format and information from the video instance.
138 *
139 * Return the number of padding bytes at end of line.
140 */
141static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
142 const struct v4l2_mbus_framefmt *mbus,
143 struct v4l2_pix_format *pix)
144{
145 unsigned int bpl = pix->bytesperline;
146 unsigned int min_bpl;
147 unsigned int i;
148
149 memset(pix, 0, sizeof(*pix));
150 pix->width = mbus->width;
151 pix->height = mbus->height;
152
153 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
154 if (formats[i].code == mbus->code)
155 break;
156 }
157
158 if (WARN_ON(i == ARRAY_SIZE(formats)))
159 return 0;
160
Laurent Pinchart1697e492011-08-31 11:57:12 -0300161 min_bpl = pix->width * formats[i].bpp;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300162
163 /* Clamp the requested bytes per line value. If the maximum bytes per
164 * line value is zero, the module doesn't support user configurable line
165 * sizes. Override the requested value with the minimum in that case.
166 */
167 if (video->bpl_max)
168 bpl = clamp(bpl, min_bpl, video->bpl_max);
169 else
170 bpl = min_bpl;
171
172 if (!video->bpl_zero_padding || bpl != min_bpl)
173 bpl = ALIGN(bpl, video->bpl_alignment);
174
175 pix->pixelformat = formats[i].pixelformat;
176 pix->bytesperline = bpl;
177 pix->sizeimage = pix->bytesperline * pix->height;
178 pix->colorspace = mbus->colorspace;
179 pix->field = mbus->field;
180
181 return bpl - min_bpl;
182}
183
184static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
185 struct v4l2_mbus_framefmt *mbus)
186{
187 unsigned int i;
188
189 memset(mbus, 0, sizeof(*mbus));
190 mbus->width = pix->width;
191 mbus->height = pix->height;
192
Laurent Pinchartc3cd2572011-11-28 08:25:30 -0300193 /* Skip the last format in the loop so that it will be selected if no
194 * match is found.
195 */
196 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300197 if (formats[i].pixelformat == pix->pixelformat)
198 break;
199 }
200
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300201 mbus->code = formats[i].code;
202 mbus->colorspace = pix->colorspace;
203 mbus->field = pix->field;
204}
205
206static struct v4l2_subdev *
207isp_video_remote_subdev(struct isp_video *video, u32 *pad)
208{
209 struct media_pad *remote;
210
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300211 remote = media_entity_remote_pad(&video->pad);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300212
213 if (remote == NULL ||
214 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
215 return NULL;
216
217 if (pad)
218 *pad = remote->index;
219
220 return media_entity_to_v4l2_subdev(remote->entity);
221}
222
223/* Return a pointer to the ISP video instance at the far end of the pipeline. */
Sakari Ailusae5df812012-03-05 20:22:41 -0300224static int isp_video_get_graph_data(struct isp_video *video,
225 struct isp_pipeline *pipe)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300226{
227 struct media_entity_graph graph;
228 struct media_entity *entity = &video->video.entity;
229 struct media_device *mdev = entity->parent;
230 struct isp_video *far_end = NULL;
231
232 mutex_lock(&mdev->graph_mutex);
233 media_entity_graph_walk_start(&graph, entity);
234
235 while ((entity = media_entity_graph_walk_next(&graph))) {
Sakari Ailusae5df812012-03-05 20:22:41 -0300236 struct isp_video *__video;
237
238 pipe->entities |= 1 << entity->id;
239
240 if (far_end != NULL)
241 continue;
242
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300243 if (entity == &video->video.entity)
244 continue;
245
246 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
247 continue;
248
Sakari Ailusae5df812012-03-05 20:22:41 -0300249 __video = to_isp_video(media_entity_to_video_device(entity));
250 if (__video->type != video->type)
251 far_end = __video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300252 }
253
254 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusae5df812012-03-05 20:22:41 -0300255
256 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
257 pipe->input = far_end;
258 pipe->output = video;
259 } else {
260 if (far_end == NULL)
261 return -EPIPE;
262
263 pipe->input = video;
264 pipe->output = far_end;
265 }
266
267 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300268}
269
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300270static int
271__isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
272{
273 struct v4l2_subdev_format fmt;
274 struct v4l2_subdev *subdev;
275 u32 pad;
276 int ret;
277
278 subdev = isp_video_remote_subdev(video, &pad);
279 if (subdev == NULL)
280 return -EINVAL;
281
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300282 fmt.pad = pad;
283 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300284
Laurent Pinchart52646822013-11-04 06:52:10 -0300285 mutex_lock(&video->mutex);
286 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300287 mutex_unlock(&video->mutex);
288
289 if (ret)
290 return ret;
291
292 format->type = video->type;
293 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
294}
295
296static int
297isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
298{
299 struct v4l2_format format;
300 int ret;
301
302 memcpy(&format, &vfh->format, sizeof(format));
303 ret = __isp_video_get_format(video, &format);
304 if (ret < 0)
305 return ret;
306
307 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
308 vfh->format.fmt.pix.height != format.fmt.pix.height ||
309 vfh->format.fmt.pix.width != format.fmt.pix.width ||
310 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
Laurent Pinchartf7abbe92014-05-19 12:14:42 -0300311 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage ||
312 vfh->format.fmt.pix.field != format.fmt.pix.field)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300313 return -EINVAL;
314
Laurent Pinchartf7abbe92014-05-19 12:14:42 -0300315 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300316}
317
318/* -----------------------------------------------------------------------------
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300319 * Video queue operations
320 */
321
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300322static int isp_video_queue_setup(struct vb2_queue *queue,
323 const struct v4l2_format *fmt,
324 unsigned int *count, unsigned int *num_planes,
325 unsigned int sizes[], void *alloc_ctxs[])
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300326{
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300327 struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300328 struct isp_video *video = vfh->video;
329
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300330 *num_planes = 1;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300331
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300332 sizes[0] = vfh->format.fmt.pix.sizeimage;
333 if (sizes[0] == 0)
334 return -EINVAL;
335
336 alloc_ctxs[0] = video->alloc_ctx;
337
338 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
339
340 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300341}
342
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300343static int isp_video_buffer_prepare(struct vb2_buffer *buf)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300344{
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300345 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300346 struct isp_buffer *buffer = to_isp_buffer(buf);
347 struct isp_video *video = vfh->video;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300348 dma_addr_t addr;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300349
Laurent Pinchart661112c2013-12-09 11:36:51 -0300350 /* Refuse to prepare the buffer is the video node has registered an
351 * error. We don't need to take any lock here as the operation is
352 * inherently racy. The authoritative check will be performed in the
353 * queue handler, which can't return an error, this check is just a best
354 * effort to notify userspace as early as possible.
355 */
356 if (unlikely(video->error))
357 return -EIO;
358
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300359 addr = vb2_dma_contig_plane_dma_addr(buf, 0);
360 if (!IS_ALIGNED(addr, 32)) {
361 dev_dbg(video->isp->dev,
362 "Buffer address must be aligned to 32 bytes boundary.\n");
363 return -EINVAL;
364 }
365
366 vb2_set_plane_payload(&buffer->vb, 0, vfh->format.fmt.pix.sizeimage);
Laurent Pinchart21d85822014-03-09 20:17:12 -0300367 buffer->dma = addr;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300368
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300369 return 0;
370}
371
372/*
373 * isp_video_buffer_queue - Add buffer to streaming queue
374 * @buf: Video buffer
375 *
376 * In memory-to-memory mode, start streaming on the pipeline if buffers are
377 * queued on both the input and the output, if the pipeline isn't already busy.
378 * If the pipeline is busy, it will be restarted in the output module interrupt
379 * handler.
380 */
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300381static void isp_video_buffer_queue(struct vb2_buffer *buf)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300382{
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300383 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300384 struct isp_buffer *buffer = to_isp_buffer(buf);
385 struct isp_video *video = vfh->video;
386 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
387 enum isp_pipeline_state state;
388 unsigned long flags;
389 unsigned int empty;
390 unsigned int start;
391
Laurent Pincharte8feb872014-03-09 20:57:53 -0300392 spin_lock_irqsave(&video->irqlock, flags);
393
Laurent Pinchart661112c2013-12-09 11:36:51 -0300394 if (unlikely(video->error)) {
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300395 vb2_buffer_done(&buffer->vb, VB2_BUF_STATE_ERROR);
Laurent Pincharte8feb872014-03-09 20:57:53 -0300396 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300397 return;
398 }
399
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300400 empty = list_empty(&video->dmaqueue);
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300401 list_add_tail(&buffer->irqlist, &video->dmaqueue);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300402
Laurent Pincharte8feb872014-03-09 20:57:53 -0300403 spin_unlock_irqrestore(&video->irqlock, flags);
404
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300405 if (empty) {
406 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
407 state = ISP_PIPELINE_QUEUE_OUTPUT;
408 else
409 state = ISP_PIPELINE_QUEUE_INPUT;
410
411 spin_lock_irqsave(&pipe->lock, flags);
412 pipe->state |= state;
413 video->ops->queue(video, buffer);
414 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
415
416 start = isp_pipeline_ready(pipe);
417 if (start)
418 pipe->state |= ISP_PIPELINE_STREAM;
419 spin_unlock_irqrestore(&pipe->lock, flags);
420
421 if (start)
422 omap3isp_pipeline_set_stream(pipe,
423 ISP_PIPELINE_STREAM_SINGLESHOT);
424 }
425}
426
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300427static const struct vb2_ops isp_video_queue_ops = {
428 .queue_setup = isp_video_queue_setup,
429 .buf_prepare = isp_video_buffer_prepare,
430 .buf_queue = isp_video_buffer_queue,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300431};
432
433/*
434 * omap3isp_video_buffer_next - Complete the current buffer and return the next
435 * @video: ISP video object
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300436 *
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300437 * Remove the current video buffer from the DMA queue and fill its timestamp and
438 * field count before handing it back to videobuf2.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300439 *
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300440 * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
441 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
442 * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300443 *
444 * The DMA queue is expected to contain at least one buffer.
445 *
446 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
447 * empty.
448 */
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300449struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300450{
451 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300452 enum isp_pipeline_state state;
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300453 struct isp_buffer *buf;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300454 unsigned long flags;
455 struct timespec ts;
456
Laurent Pincharte8feb872014-03-09 20:57:53 -0300457 spin_lock_irqsave(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300458 if (WARN_ON(list_empty(&video->dmaqueue))) {
Laurent Pincharte8feb872014-03-09 20:57:53 -0300459 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300460 return NULL;
461 }
462
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300463 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300464 irqlist);
465 list_del(&buf->irqlist);
Laurent Pincharte8feb872014-03-09 20:57:53 -0300466 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300467
468 ktime_get_ts(&ts);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300469 buf->vb.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
470 buf->vb.v4l2_buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300471
472 /* Do frame number propagation only if this is the output video node.
473 * Frame number either comes from the CSI receivers or it gets
474 * incremented here if H3A is not active.
475 * Note: There is no guarantee that the output buffer will finish
476 * first, so the input number might lag behind by 1 in some cases.
477 */
478 if (video == pipe->output && !pipe->do_propagation)
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300479 buf->vb.v4l2_buf.sequence =
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300480 atomic_inc_return(&pipe->frame_number);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300481 else
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300482 buf->vb.v4l2_buf.sequence = atomic_read(&pipe->frame_number);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300483
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -0300484 if (pipe->field != V4L2_FIELD_NONE)
485 buf->vb.v4l2_buf.sequence /= 2;
486
487 buf->vb.v4l2_buf.field = pipe->field;
488
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300489 /* Report pipeline errors to userspace on the capture device side. */
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300490 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
491 state = VB2_BUF_STATE_ERROR;
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300492 pipe->error = false;
493 } else {
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300494 state = VB2_BUF_STATE_DONE;
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300495 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300496
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300497 vb2_buffer_done(&buf->vb, state);
498
499 spin_lock_irqsave(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300500
501 if (list_empty(&video->dmaqueue)) {
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300502 spin_unlock_irqrestore(&video->irqlock, flags);
503
504 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300505 state = ISP_PIPELINE_QUEUE_OUTPUT
506 | ISP_PIPELINE_STREAM;
507 else
508 state = ISP_PIPELINE_QUEUE_INPUT
509 | ISP_PIPELINE_STREAM;
510
511 spin_lock_irqsave(&pipe->lock, flags);
512 pipe->state &= ~state;
513 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
514 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
515 spin_unlock_irqrestore(&pipe->lock, flags);
516 return NULL;
517 }
518
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300519 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
520 spin_lock(&pipe->lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300521 pipe->state &= ~ISP_PIPELINE_STREAM;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300522 spin_unlock(&pipe->lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300523 }
524
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300525 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300526 irqlist);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300527 buf->vb.state = VB2_BUF_STATE_ACTIVE;
528
529 spin_unlock_irqrestore(&video->irqlock, flags);
530
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300531 return buf;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300532}
533
534/*
Laurent Pinchart661112c2013-12-09 11:36:51 -0300535 * omap3isp_video_cancel_stream - Cancel stream on a video node
536 * @video: ISP video object
537 *
538 * Cancelling a stream mark all buffers on the video node as erroneous and makes
539 * sure no new buffer can be queued.
540 */
541void omap3isp_video_cancel_stream(struct isp_video *video)
542{
Laurent Pinchart661112c2013-12-09 11:36:51 -0300543 unsigned long flags;
544
Laurent Pincharte8feb872014-03-09 20:57:53 -0300545 spin_lock_irqsave(&video->irqlock, flags);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300546
547 while (!list_empty(&video->dmaqueue)) {
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300548 struct isp_buffer *buf;
Laurent Pinchart661112c2013-12-09 11:36:51 -0300549
550 buf = list_first_entry(&video->dmaqueue,
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300551 struct isp_buffer, irqlist);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300552 list_del(&buf->irqlist);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300553 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300554 }
555
556 video->error = true;
557
Laurent Pincharte8feb872014-03-09 20:57:53 -0300558 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300559}
560
561/*
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300562 * omap3isp_video_resume - Perform resume operation on the buffers
563 * @video: ISP video object
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300564 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300565 *
566 * This function is intended to be used on suspend/resume scenario. It
567 * requests video queue layer to discard buffers marked as DONE if it's in
568 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
569 * if there's any.
570 */
571void omap3isp_video_resume(struct isp_video *video, int continuous)
572{
573 struct isp_buffer *buf = NULL;
574
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300575 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
576 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300577 vb2_discard_done(video->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300578 mutex_unlock(&video->queue_lock);
579 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300580
581 if (!list_empty(&video->dmaqueue)) {
582 buf = list_first_entry(&video->dmaqueue,
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300583 struct isp_buffer, irqlist);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300584 video->ops->queue(video, buf);
585 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
586 } else {
587 if (continuous)
588 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
589 }
590}
591
592/* -----------------------------------------------------------------------------
593 * V4L2 ioctls
594 */
595
596static int
597isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
598{
599 struct isp_video *video = video_drvdata(file);
600
601 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
602 strlcpy(cap->card, video->video.name, sizeof(cap->card));
603 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300604
605 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
606 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
607 else
608 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
609
610 return 0;
611}
612
613static int
614isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
615{
616 struct isp_video_fh *vfh = to_isp_video_fh(fh);
617 struct isp_video *video = video_drvdata(file);
618
619 if (format->type != video->type)
620 return -EINVAL;
621
622 mutex_lock(&video->mutex);
623 *format = vfh->format;
624 mutex_unlock(&video->mutex);
625
626 return 0;
627}
628
629static int
630isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
631{
632 struct isp_video_fh *vfh = to_isp_video_fh(fh);
633 struct isp_video *video = video_drvdata(file);
634 struct v4l2_mbus_framefmt fmt;
635
636 if (format->type != video->type)
637 return -EINVAL;
638
Laurent Pinchartbcb4e0e2014-05-19 19:40:04 -0300639 /* Replace unsupported field orders with sane defaults. */
640 switch (format->fmt.pix.field) {
641 case V4L2_FIELD_NONE:
642 /* Progressive is supported everywhere. */
643 break;
644 case V4L2_FIELD_ALTERNATE:
645 /* ALTERNATE is not supported on output nodes. */
646 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
647 format->fmt.pix.field = V4L2_FIELD_NONE;
648 break;
649 case V4L2_FIELD_INTERLACED:
650 /* The ISP has no concept of video standard, select the
651 * top-bottom order when the unqualified interlaced order is
652 * requested.
653 */
654 format->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
655 /* Fall-through */
656 case V4L2_FIELD_INTERLACED_TB:
657 case V4L2_FIELD_INTERLACED_BT:
658 /* Interlaced orders are only supported at the CCDC output. */
659 if (video != &video->isp->isp_ccdc.video_out)
660 format->fmt.pix.field = V4L2_FIELD_NONE;
661 break;
662 case V4L2_FIELD_TOP:
663 case V4L2_FIELD_BOTTOM:
664 case V4L2_FIELD_SEQ_TB:
665 case V4L2_FIELD_SEQ_BT:
666 default:
667 /* All other field orders are currently unsupported, default to
668 * progressive.
669 */
Laurent Pinchart48a8a0c2014-05-19 10:49:58 -0300670 format->fmt.pix.field = V4L2_FIELD_NONE;
Laurent Pinchartbcb4e0e2014-05-19 19:40:04 -0300671 break;
672 }
Laurent Pinchart48a8a0c2014-05-19 10:49:58 -0300673
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300674 /* Fill the bytesperline and sizeimage fields by converting to media bus
675 * format and back to pixel format.
676 */
677 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
678 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
679
Laurent Pinchart25c5cc92014-05-19 10:33:15 -0300680 mutex_lock(&video->mutex);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300681 vfh->format = *format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300682 mutex_unlock(&video->mutex);
Laurent Pinchart25c5cc92014-05-19 10:33:15 -0300683
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300684 return 0;
685}
686
687static int
688isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
689{
690 struct isp_video *video = video_drvdata(file);
691 struct v4l2_subdev_format fmt;
692 struct v4l2_subdev *subdev;
693 u32 pad;
694 int ret;
695
696 if (format->type != video->type)
697 return -EINVAL;
698
699 subdev = isp_video_remote_subdev(video, &pad);
700 if (subdev == NULL)
701 return -EINVAL;
702
703 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
704
705 fmt.pad = pad;
706 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
707 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
708 if (ret)
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300709 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300710
711 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
712 return 0;
713}
714
715static int
716isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
717{
718 struct isp_video *video = video_drvdata(file);
719 struct v4l2_subdev *subdev;
720 int ret;
721
722 subdev = isp_video_remote_subdev(video, NULL);
723 if (subdev == NULL)
724 return -EINVAL;
725
726 mutex_lock(&video->mutex);
727 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
728 mutex_unlock(&video->mutex);
729
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300730 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300731}
732
733static int
734isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
735{
736 struct isp_video *video = video_drvdata(file);
737 struct v4l2_subdev_format format;
738 struct v4l2_subdev *subdev;
739 u32 pad;
740 int ret;
741
742 subdev = isp_video_remote_subdev(video, &pad);
743 if (subdev == NULL)
744 return -EINVAL;
745
746 /* Try the get crop operation first and fallback to get format if not
747 * implemented.
748 */
749 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
750 if (ret != -ENOIOCTLCMD)
751 return ret;
752
753 format.pad = pad;
754 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
755 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
756 if (ret < 0)
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300757 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300758
759 crop->c.left = 0;
760 crop->c.top = 0;
761 crop->c.width = format.format.width;
762 crop->c.height = format.format.height;
763
764 return 0;
765}
766
767static int
Laurent Pinchart1fa48cd2012-10-12 11:28:40 -0300768isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300769{
770 struct isp_video *video = video_drvdata(file);
771 struct v4l2_subdev *subdev;
772 int ret;
773
774 subdev = isp_video_remote_subdev(video, NULL);
775 if (subdev == NULL)
776 return -EINVAL;
777
778 mutex_lock(&video->mutex);
779 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
780 mutex_unlock(&video->mutex);
781
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300782 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300783}
784
785static int
786isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
787{
788 struct isp_video_fh *vfh = to_isp_video_fh(fh);
789 struct isp_video *video = video_drvdata(file);
790
791 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
792 video->type != a->type)
793 return -EINVAL;
794
795 memset(a, 0, sizeof(*a));
796 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
797 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
798 a->parm.output.timeperframe = vfh->timeperframe;
799
800 return 0;
801}
802
803static int
804isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
805{
806 struct isp_video_fh *vfh = to_isp_video_fh(fh);
807 struct isp_video *video = video_drvdata(file);
808
809 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
810 video->type != a->type)
811 return -EINVAL;
812
813 if (a->parm.output.timeperframe.denominator == 0)
814 a->parm.output.timeperframe.denominator = 1;
815
816 vfh->timeperframe = a->parm.output.timeperframe;
817
818 return 0;
819}
820
821static int
822isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
823{
824 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300825 struct isp_video *video = video_drvdata(file);
826 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300827
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300828 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300829 ret = vb2_reqbufs(&vfh->queue, rb);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300830 mutex_unlock(&video->queue_lock);
831
832 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300833}
834
835static int
836isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
837{
838 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300839 struct isp_video *video = video_drvdata(file);
840 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300841
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300842 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300843 ret = vb2_querybuf(&vfh->queue, b);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300844 mutex_unlock(&video->queue_lock);
845
846 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300847}
848
849static int
850isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
851{
852 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300853 struct isp_video *video = video_drvdata(file);
854 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300855
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300856 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300857 ret = vb2_qbuf(&vfh->queue, b);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300858 mutex_unlock(&video->queue_lock);
859
860 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300861}
862
863static int
864isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
865{
866 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300867 struct isp_video *video = video_drvdata(file);
868 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300869
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300870 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300871 ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300872 mutex_unlock(&video->queue_lock);
873
874 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300875}
876
Sakari Ailusccddd912012-01-17 12:29:38 -0300877static int isp_video_check_external_subdevs(struct isp_video *video,
878 struct isp_pipeline *pipe)
879{
880 struct isp_device *isp = video->isp;
881 struct media_entity *ents[] = {
882 &isp->isp_csi2a.subdev.entity,
883 &isp->isp_csi2c.subdev.entity,
884 &isp->isp_ccp2.subdev.entity,
885 &isp->isp_ccdc.subdev.entity
886 };
887 struct media_pad *source_pad;
888 struct media_entity *source = NULL;
889 struct media_entity *sink;
890 struct v4l2_subdev_format fmt;
891 struct v4l2_ext_controls ctrls;
892 struct v4l2_ext_control ctrl;
893 unsigned int i;
Laurent Pinchart7c486c02014-02-14 21:45:50 -0300894 int ret;
Sakari Ailusccddd912012-01-17 12:29:38 -0300895
Laurent Pinchart4b2f1ed2014-02-14 21:40:48 -0300896 /* Memory-to-memory pipelines have no external subdev. */
897 if (pipe->input != NULL)
898 return 0;
899
Sakari Ailusccddd912012-01-17 12:29:38 -0300900 for (i = 0; i < ARRAY_SIZE(ents); i++) {
901 /* Is the entity part of the pipeline? */
902 if (!(pipe->entities & (1 << ents[i]->id)))
903 continue;
904
905 /* ISP entities have always sink pad == 0. Find source. */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300906 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
Sakari Ailusccddd912012-01-17 12:29:38 -0300907 if (source_pad == NULL)
908 continue;
909
910 source = source_pad->entity;
911 sink = ents[i];
912 break;
913 }
914
915 if (!source) {
916 dev_warn(isp->dev, "can't find source, failing now\n");
Laurent Pinchart7c486c02014-02-14 21:45:50 -0300917 return -EINVAL;
Sakari Ailusccddd912012-01-17 12:29:38 -0300918 }
919
920 if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
921 return 0;
922
923 pipe->external = media_entity_to_v4l2_subdev(source);
924
925 fmt.pad = source_pad->index;
926 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
927 ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
928 pad, get_fmt, NULL, &fmt);
929 if (unlikely(ret < 0)) {
930 dev_warn(isp->dev, "get_fmt returned null!\n");
931 return ret;
932 }
933
Laurent Pinchart1697e492011-08-31 11:57:12 -0300934 pipe->external_width =
935 omap3isp_video_format_info(fmt.format.code)->width;
Sakari Ailusccddd912012-01-17 12:29:38 -0300936
937 memset(&ctrls, 0, sizeof(ctrls));
938 memset(&ctrl, 0, sizeof(ctrl));
939
940 ctrl.id = V4L2_CID_PIXEL_RATE;
941
942 ctrls.count = 1;
943 ctrls.controls = &ctrl;
944
945 ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
946 if (ret < 0) {
947 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
948 pipe->external->name);
949 return ret;
950 }
951
952 pipe->external_rate = ctrl.value64;
953
Sakari Ailusa6d7a622012-02-25 20:42:07 -0300954 if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
955 unsigned int rate = UINT_MAX;
956 /*
957 * Check that maximum allowed CCDC pixel rate isn't
958 * exceeded by the pixel rate.
959 */
960 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
961 if (pipe->external_rate > rate)
962 return -ENOSPC;
963 }
964
Sakari Ailusccddd912012-01-17 12:29:38 -0300965 return 0;
966}
967
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300968/*
969 * Stream management
970 *
971 * Every ISP pipeline has a single input and a single output. The input can be
972 * either a sensor or a video node. The output is always a video node.
973 *
974 * As every pipeline has an output video node, the ISP video objects at the
975 * pipeline output stores the pipeline state. It tracks the streaming state of
976 * both the input and output, as well as the availability of buffers.
977 *
978 * In sensor-to-memory mode, frames are always available at the pipeline input.
979 * Starting the sensor usually requires I2C transfers and must be done in
980 * interruptible context. The pipeline is started and stopped synchronously
981 * to the stream on/off commands. All modules in the pipeline will get their
982 * subdev set stream handler called. The module at the end of the pipeline must
983 * delay starting the hardware until buffers are available at its output.
984 *
985 * In memory-to-memory mode, starting/stopping the stream requires
986 * synchronization between the input and output. ISP modules can't be stopped
987 * in the middle of a frame, and at least some of the modules seem to become
988 * busy as soon as they're started, even if they don't receive a frame start
989 * event. For that reason frames need to be processed in single-shot mode. The
990 * driver needs to wait until a frame is completely processed and written to
991 * memory before restarting the pipeline for the next frame. Pipelined
992 * processing might be possible but requires more testing.
993 *
994 * Stream start must be delayed until buffers are available at both the input
995 * and output. The pipeline must be started in the videobuf queue callback with
996 * the buffers queue spinlock held. The modules subdev set stream operation must
997 * not sleep.
998 */
999static int
1000isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1001{
1002 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1003 struct isp_video *video = video_drvdata(file);
1004 enum isp_pipeline_state state;
1005 struct isp_pipeline *pipe;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001006 unsigned long flags;
1007 int ret;
1008
1009 if (type != video->type)
1010 return -EINVAL;
1011
1012 mutex_lock(&video->stream_lock);
1013
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001014 /* Start streaming on the pipeline. No link touching an entity in the
1015 * pipeline can be activated or deactivated once streaming is started.
1016 */
1017 pipe = video->video.entity.pipe
1018 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
Sakari Ailusb0cd79e2012-01-16 18:59:02 -03001019
Sakari Ailusae5df812012-03-05 20:22:41 -03001020 pipe->entities = 0;
1021
Sakari Ailusb0cd79e2012-01-16 18:59:02 -03001022 if (video->isp->pdata->set_constraints)
1023 video->isp->pdata->set_constraints(video->isp, true);
1024 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1025 pipe->max_rate = pipe->l3_ick;
1026
Sakari Ailusda392572011-10-10 17:05:24 -03001027 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
1028 if (ret < 0)
1029 goto err_pipeline_start;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001030
1031 /* Verify that the currently configured format matches the output of
1032 * the connected subdev.
1033 */
1034 ret = isp_video_check_format(video, vfh);
1035 if (ret < 0)
Sakari Ailusda392572011-10-10 17:05:24 -03001036 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001037
1038 video->bpl_padding = ret;
1039 video->bpl_value = vfh->format.fmt.pix.bytesperline;
1040
Sakari Ailusae5df812012-03-05 20:22:41 -03001041 ret = isp_video_get_graph_data(video, pipe);
1042 if (ret < 0)
1043 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001044
Sakari Ailusae5df812012-03-05 20:22:41 -03001045 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001046 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
Sakari Ailusae5df812012-03-05 20:22:41 -03001047 else
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001048 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001049
Sakari Ailusccddd912012-01-17 12:29:38 -03001050 ret = isp_video_check_external_subdevs(video, pipe);
1051 if (ret < 0)
1052 goto err_check_format;
1053
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001054 pipe->error = false;
1055
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001056 spin_lock_irqsave(&pipe->lock, flags);
1057 pipe->state &= ~ISP_PIPELINE_STREAM;
1058 pipe->state |= state;
1059 spin_unlock_irqrestore(&pipe->lock, flags);
1060
1061 /* Set the maximum time per frame as the value requested by userspace.
1062 * This is a soft limit that can be overridden if the hardware doesn't
1063 * support the request limit.
1064 */
1065 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1066 pipe->max_timeperframe = vfh->timeperframe;
1067
1068 video->queue = &vfh->queue;
1069 INIT_LIST_HEAD(&video->dmaqueue);
1070 atomic_set(&pipe->frame_number, -1);
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -03001071 pipe->field = vfh->format.fmt.pix.field;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001072
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001073 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001074 ret = vb2_streamon(&vfh->queue, type);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001075 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001076 if (ret < 0)
Sakari Ailusda392572011-10-10 17:05:24 -03001077 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001078
1079 /* In sensor-to-memory mode, the stream can be started synchronously
1080 * to the stream on command. In memory-to-memory mode, it will be
1081 * started when buffers are queued on both the input and output.
1082 */
1083 if (pipe->input == NULL) {
1084 ret = omap3isp_pipeline_set_stream(pipe,
1085 ISP_PIPELINE_STREAM_CONTINUOUS);
1086 if (ret < 0)
Sakari Ailusda392572011-10-10 17:05:24 -03001087 goto err_set_stream;
Laurent Pincharte8feb872014-03-09 20:57:53 -03001088 spin_lock_irqsave(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001089 if (list_empty(&video->dmaqueue))
1090 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
Laurent Pincharte8feb872014-03-09 20:57:53 -03001091 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001092 }
1093
Sakari Ailusda392572011-10-10 17:05:24 -03001094 mutex_unlock(&video->stream_lock);
1095 return 0;
1096
1097err_set_stream:
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001098 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001099 vb2_streamoff(&vfh->queue, type);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001100 mutex_unlock(&video->queue_lock);
Sakari Ailusda392572011-10-10 17:05:24 -03001101err_check_format:
1102 media_entity_pipeline_stop(&video->video.entity);
1103err_pipeline_start:
1104 if (video->isp->pdata->set_constraints)
1105 video->isp->pdata->set_constraints(video->isp, false);
1106 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1107 * will get triggered the next time the CCDC is powered up will try to
1108 * access buffers that might have been freed but still present in the
1109 * DMA queue. This can easily get triggered if the above
1110 * omap3isp_pipeline_set_stream() call fails on a system with a
1111 * free-running sensor.
1112 */
1113 INIT_LIST_HEAD(&video->dmaqueue);
1114 video->queue = NULL;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001115
1116 mutex_unlock(&video->stream_lock);
1117 return ret;
1118}
1119
1120static int
1121isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1122{
1123 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1124 struct isp_video *video = video_drvdata(file);
1125 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1126 enum isp_pipeline_state state;
1127 unsigned int streaming;
1128 unsigned long flags;
1129
1130 if (type != video->type)
1131 return -EINVAL;
1132
1133 mutex_lock(&video->stream_lock);
1134
1135 /* Make sure we're not streaming yet. */
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001136 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001137 streaming = vb2_is_streaming(&vfh->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001138 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001139
1140 if (!streaming)
1141 goto done;
1142
1143 /* Update the pipeline state. */
1144 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1145 state = ISP_PIPELINE_STREAM_OUTPUT
1146 | ISP_PIPELINE_QUEUE_OUTPUT;
1147 else
1148 state = ISP_PIPELINE_STREAM_INPUT
1149 | ISP_PIPELINE_QUEUE_INPUT;
1150
1151 spin_lock_irqsave(&pipe->lock, flags);
1152 pipe->state &= ~state;
1153 spin_unlock_irqrestore(&pipe->lock, flags);
1154
1155 /* Stop the stream. */
1156 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
Laurent Pinchart08344492014-04-20 20:59:03 -03001157 omap3isp_video_cancel_stream(video);
1158
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001159 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001160 vb2_streamoff(&vfh->queue, type);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001161 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001162 video->queue = NULL;
Laurent Pinchart661112c2013-12-09 11:36:51 -03001163 video->error = false;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001164
Laurent Pinchart4b0ec192011-03-03 10:05:22 -03001165 if (video->isp->pdata->set_constraints)
1166 video->isp->pdata->set_constraints(video->isp, false);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001167 media_entity_pipeline_stop(&video->video.entity);
1168
1169done:
1170 mutex_unlock(&video->stream_lock);
1171 return 0;
1172}
1173
1174static int
1175isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1176{
1177 if (input->index > 0)
1178 return -EINVAL;
1179
1180 strlcpy(input->name, "camera", sizeof(input->name));
1181 input->type = V4L2_INPUT_TYPE_CAMERA;
1182
1183 return 0;
1184}
1185
1186static int
1187isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1188{
1189 *input = 0;
1190
1191 return 0;
1192}
1193
1194static int
1195isp_video_s_input(struct file *file, void *fh, unsigned int input)
1196{
1197 return input == 0 ? 0 : -EINVAL;
1198}
1199
1200static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1201 .vidioc_querycap = isp_video_querycap,
1202 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1203 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1204 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1205 .vidioc_g_fmt_vid_out = isp_video_get_format,
1206 .vidioc_s_fmt_vid_out = isp_video_set_format,
1207 .vidioc_try_fmt_vid_out = isp_video_try_format,
1208 .vidioc_cropcap = isp_video_cropcap,
1209 .vidioc_g_crop = isp_video_get_crop,
1210 .vidioc_s_crop = isp_video_set_crop,
1211 .vidioc_g_parm = isp_video_get_param,
1212 .vidioc_s_parm = isp_video_set_param,
1213 .vidioc_reqbufs = isp_video_reqbufs,
1214 .vidioc_querybuf = isp_video_querybuf,
1215 .vidioc_qbuf = isp_video_qbuf,
1216 .vidioc_dqbuf = isp_video_dqbuf,
1217 .vidioc_streamon = isp_video_streamon,
1218 .vidioc_streamoff = isp_video_streamoff,
1219 .vidioc_enum_input = isp_video_enum_input,
1220 .vidioc_g_input = isp_video_g_input,
1221 .vidioc_s_input = isp_video_s_input,
1222};
1223
1224/* -----------------------------------------------------------------------------
1225 * V4L2 file operations
1226 */
1227
1228static int isp_video_open(struct file *file)
1229{
1230 struct isp_video *video = video_drvdata(file);
1231 struct isp_video_fh *handle;
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001232 struct vb2_queue *queue;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001233 int ret = 0;
1234
1235 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1236 if (handle == NULL)
1237 return -ENOMEM;
1238
1239 v4l2_fh_init(&handle->vfh, &video->video);
1240 v4l2_fh_add(&handle->vfh);
1241
1242 /* If this is the first user, initialise the pipeline. */
1243 if (omap3isp_get(video->isp) == NULL) {
1244 ret = -EBUSY;
1245 goto done;
1246 }
1247
1248 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1249 if (ret < 0) {
1250 omap3isp_put(video->isp);
1251 goto done;
1252 }
1253
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001254 queue = &handle->queue;
1255 queue->type = video->type;
1256 queue->io_modes = VB2_MMAP | VB2_USERPTR;
1257 queue->drv_priv = handle;
1258 queue->ops = &isp_video_queue_ops;
1259 queue->mem_ops = &vb2_dma_contig_memops;
1260 queue->buf_struct_size = sizeof(struct isp_buffer);
1261 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1262
1263 ret = vb2_queue_init(&handle->queue);
1264 if (ret < 0) {
1265 omap3isp_put(video->isp);
1266 goto done;
1267 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001268
1269 memset(&handle->format, 0, sizeof(handle->format));
1270 handle->format.type = video->type;
1271 handle->timeperframe.denominator = 1;
1272
1273 handle->video = video;
1274 file->private_data = &handle->vfh;
1275
1276done:
1277 if (ret < 0) {
1278 v4l2_fh_del(&handle->vfh);
1279 kfree(handle);
1280 }
1281
1282 return ret;
1283}
1284
1285static int isp_video_release(struct file *file)
1286{
1287 struct isp_video *video = video_drvdata(file);
1288 struct v4l2_fh *vfh = file->private_data;
1289 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1290
1291 /* Disable streaming and free the buffers queue resources. */
1292 isp_video_streamoff(file, vfh, video->type);
1293
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001294 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001295 vb2_queue_release(&handle->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001296 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001297
1298 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1299
1300 /* Release the file handle. */
1301 v4l2_fh_del(vfh);
1302 kfree(handle);
1303 file->private_data = NULL;
1304
1305 omap3isp_put(video->isp);
1306
1307 return 0;
1308}
1309
1310static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1311{
1312 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001313 struct isp_video *video = video_drvdata(file);
1314 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001315
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001316 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001317 ret = vb2_poll(&vfh->queue, file, wait);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001318 mutex_unlock(&video->queue_lock);
1319
1320 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001321}
1322
1323static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1324{
1325 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001326 struct isp_video *video = video_drvdata(file);
1327 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001328
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001329 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001330 ret = vb2_mmap(&vfh->queue, vma);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001331 mutex_unlock(&video->queue_lock);
1332
1333 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001334}
1335
1336static struct v4l2_file_operations isp_video_fops = {
1337 .owner = THIS_MODULE,
1338 .unlocked_ioctl = video_ioctl2,
1339 .open = isp_video_open,
1340 .release = isp_video_release,
1341 .poll = isp_video_poll,
1342 .mmap = isp_video_mmap,
1343};
1344
1345/* -----------------------------------------------------------------------------
1346 * ISP video core
1347 */
1348
1349static const struct isp_video_operations isp_video_dummy_ops = {
1350};
1351
1352int omap3isp_video_init(struct isp_video *video, const char *name)
1353{
1354 const char *direction;
1355 int ret;
1356
1357 switch (video->type) {
1358 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1359 direction = "output";
Sakari Ailus8dad9362013-10-02 20:17:52 -03001360 video->pad.flags = MEDIA_PAD_FL_SINK
1361 | MEDIA_PAD_FL_MUST_CONNECT;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001362 break;
1363 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1364 direction = "input";
Sakari Ailus8dad9362013-10-02 20:17:52 -03001365 video->pad.flags = MEDIA_PAD_FL_SOURCE
1366 | MEDIA_PAD_FL_MUST_CONNECT;
Hans Verkuil954f3402012-09-05 06:05:50 -03001367 video->video.vfl_dir = VFL_DIR_TX;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001368 break;
1369
1370 default:
1371 return -EINVAL;
1372 }
1373
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001374 video->alloc_ctx = vb2_dma_contig_init_ctx(video->isp->dev);
1375 if (IS_ERR(video->alloc_ctx))
1376 return PTR_ERR(video->alloc_ctx);
1377
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001378 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001379 if (ret < 0) {
1380 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001381 return ret;
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001382 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001383
1384 mutex_init(&video->mutex);
1385 atomic_set(&video->active, 0);
1386
1387 spin_lock_init(&video->pipe.lock);
1388 mutex_init(&video->stream_lock);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001389 mutex_init(&video->queue_lock);
Laurent Pincharte8feb872014-03-09 20:57:53 -03001390 spin_lock_init(&video->irqlock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001391
1392 /* Initialize the video device. */
1393 if (video->ops == NULL)
1394 video->ops = &isp_video_dummy_ops;
1395
1396 video->video.fops = &isp_video_fops;
1397 snprintf(video->video.name, sizeof(video->video.name),
1398 "OMAP3 ISP %s %s", name, direction);
1399 video->video.vfl_type = VFL_TYPE_GRABBER;
1400 video->video.release = video_device_release_empty;
1401 video->video.ioctl_ops = &isp_video_ioctl_ops;
1402 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1403
1404 video_set_drvdata(&video->video, video);
1405
1406 return 0;
1407}
1408
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001409void omap3isp_video_cleanup(struct isp_video *video)
1410{
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001411 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001412 media_entity_cleanup(&video->video.entity);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001413 mutex_destroy(&video->queue_lock);
Laurent Pincharted33ac82011-09-22 17:09:26 -03001414 mutex_destroy(&video->stream_lock);
1415 mutex_destroy(&video->mutex);
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001416}
1417
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001418int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1419{
1420 int ret;
1421
1422 video->video.v4l2_dev = vdev;
1423
1424 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1425 if (ret < 0)
Laurent Pinchart4feca392012-10-22 08:28:51 -03001426 dev_err(video->isp->dev,
1427 "%s: could not register video device (%d)\n",
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001428 __func__, ret);
1429
1430 return ret;
1431}
1432
1433void omap3isp_video_unregister(struct isp_video *video)
1434{
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001435 if (video_is_registered(&video->video))
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001436 video_unregister_device(&video->video);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001437}