blob: 25a8210f0a6d5c9de9eb958f8d8a613b80e53a38 [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>
Sakari Ailus506a47e2016-02-21 13:25:10 -030025
Laurent Pinchartad614ac2011-02-12 18:05:06 -030026#include <media/v4l2-dev.h>
27#include <media/v4l2-ioctl.h>
Sakari Ailus506a47e2016-02-21 13:25:10 -030028#include <media/v4l2-mc.h>
Laurent Pinchartfbac1402014-03-09 20:36:15 -030029#include <media/videobuf2-dma-contig.h>
Laurent Pinchartad614ac2011-02-12 18:05:06 -030030
31#include "ispvideo.h"
32#include "isp.h"
33
34
35/* -----------------------------------------------------------------------------
36 * Helper functions
37 */
38
Sakari Ailus8f4f2982011-12-28 06:28:41 -030039/*
40 * NOTE: When adding new media bus codes, always remember to add
41 * corresponding in-memory formats to the table below!!!
42 */
Laurent Pinchartad614ac2011-02-12 18:05:06 -030043static struct isp_format_info formats[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030044 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
45 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030046 V4L2_PIX_FMT_GREY, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030047 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
48 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030049 V4L2_PIX_FMT_Y10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030050 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
51 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030052 V4L2_PIX_FMT_Y12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030053 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
54 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030055 V4L2_PIX_FMT_SBGGR8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030056 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
57 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030058 V4L2_PIX_FMT_SGBRG8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030059 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
60 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030061 V4L2_PIX_FMT_SGRBG8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030062 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
63 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030064 V4L2_PIX_FMT_SRGGB8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030065 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
66 MEDIA_BUS_FMT_SBGGR10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030067 V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030068 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
69 MEDIA_BUS_FMT_SGBRG10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030070 V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030071 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
72 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030073 V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030074 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
75 MEDIA_BUS_FMT_SRGGB10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030076 V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030077 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
78 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030079 V4L2_PIX_FMT_SBGGR10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030080 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
81 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030082 V4L2_PIX_FMT_SGBRG10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030083 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
84 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030085 V4L2_PIX_FMT_SGRBG10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030086 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
87 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030088 V4L2_PIX_FMT_SRGGB10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030089 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
90 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030091 V4L2_PIX_FMT_SBGGR12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030092 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
93 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030094 V4L2_PIX_FMT_SGBRG12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030095 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
96 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030097 V4L2_PIX_FMT_SGRBG12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030098 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
99 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -0300100 V4L2_PIX_FMT_SRGGB12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300101 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
102 MEDIA_BUS_FMT_UYVY8_1X16, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -0300103 V4L2_PIX_FMT_UYVY, 16, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300104 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
105 MEDIA_BUS_FMT_YUYV8_1X16, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -0300106 V4L2_PIX_FMT_YUYV, 16, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300107 { MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_UYVY8_2X8,
108 MEDIA_BUS_FMT_UYVY8_2X8, 0,
Laurent Pinchart0810fd92012-05-24 11:55:00 -0300109 V4L2_PIX_FMT_UYVY, 8, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300110 { MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YUYV8_2X8,
111 MEDIA_BUS_FMT_YUYV8_2X8, 0,
Laurent Pinchart0810fd92012-05-24 11:55:00 -0300112 V4L2_PIX_FMT_YUYV, 8, 2, },
Laurent Pinchartc51364c2011-08-31 11:03:53 -0300113 /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
114 * module and avoid NULL pointer dereferences.
115 */
116 { 0, }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300117};
118
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300119const struct isp_format_info *omap3isp_video_format_info(u32 code)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300120{
121 unsigned int i;
122
123 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
124 if (formats[i].code == code)
125 return &formats[i];
126 }
127
128 return NULL;
129}
130
131/*
132 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
133 * @video: ISP video instance
134 * @mbus: v4l2_mbus_framefmt format (input)
135 * @pix: v4l2_pix_format format (output)
136 *
137 * Fill the output pix structure with information from the input mbus format.
138 * The bytesperline and sizeimage fields are computed from the requested bytes
139 * per line value in the pix format and information from the video instance.
140 *
141 * Return the number of padding bytes at end of line.
142 */
143static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
144 const struct v4l2_mbus_framefmt *mbus,
145 struct v4l2_pix_format *pix)
146{
147 unsigned int bpl = pix->bytesperline;
148 unsigned int min_bpl;
149 unsigned int i;
150
151 memset(pix, 0, sizeof(*pix));
152 pix->width = mbus->width;
153 pix->height = mbus->height;
154
155 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
156 if (formats[i].code == mbus->code)
157 break;
158 }
159
160 if (WARN_ON(i == ARRAY_SIZE(formats)))
161 return 0;
162
Laurent Pinchart1697e492011-08-31 11:57:12 -0300163 min_bpl = pix->width * formats[i].bpp;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300164
165 /* Clamp the requested bytes per line value. If the maximum bytes per
166 * line value is zero, the module doesn't support user configurable line
167 * sizes. Override the requested value with the minimum in that case.
168 */
169 if (video->bpl_max)
170 bpl = clamp(bpl, min_bpl, video->bpl_max);
171 else
172 bpl = min_bpl;
173
174 if (!video->bpl_zero_padding || bpl != min_bpl)
175 bpl = ALIGN(bpl, video->bpl_alignment);
176
177 pix->pixelformat = formats[i].pixelformat;
178 pix->bytesperline = bpl;
179 pix->sizeimage = pix->bytesperline * pix->height;
180 pix->colorspace = mbus->colorspace;
181 pix->field = mbus->field;
182
183 return bpl - min_bpl;
184}
185
186static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
187 struct v4l2_mbus_framefmt *mbus)
188{
189 unsigned int i;
190
191 memset(mbus, 0, sizeof(*mbus));
192 mbus->width = pix->width;
193 mbus->height = pix->height;
194
Laurent Pinchartc3cd2572011-11-28 08:25:30 -0300195 /* Skip the last format in the loop so that it will be selected if no
196 * match is found.
197 */
198 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300199 if (formats[i].pixelformat == pix->pixelformat)
200 break;
201 }
202
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300203 mbus->code = formats[i].code;
204 mbus->colorspace = pix->colorspace;
205 mbus->field = pix->field;
206}
207
208static struct v4l2_subdev *
209isp_video_remote_subdev(struct isp_video *video, u32 *pad)
210{
211 struct media_pad *remote;
212
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300213 remote = media_entity_remote_pad(&video->pad);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300214
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -0300215 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300216 return NULL;
217
218 if (pad)
219 *pad = remote->index;
220
221 return media_entity_to_v4l2_subdev(remote->entity);
222}
223
224/* Return a pointer to the ISP video instance at the far end of the pipeline. */
Sakari Ailusae5df812012-03-05 20:22:41 -0300225static int isp_video_get_graph_data(struct isp_video *video,
226 struct isp_pipeline *pipe)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300227{
Sakari Ailus20b85222016-11-21 14:48:30 -0200228 struct media_graph graph;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300229 struct media_entity *entity = &video->video.entity;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300230 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300231 struct isp_video *far_end = NULL;
Sakari Ailus28461452015-12-16 11:32:24 -0200232 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300233
234 mutex_lock(&mdev->graph_mutex);
Sakari Ailus79ac44c2016-11-28 08:42:44 -0200235 ret = media_graph_walk_init(&graph, mdev);
Sakari Ailus28461452015-12-16 11:32:24 -0200236 if (ret) {
237 mutex_unlock(&mdev->graph_mutex);
238 return ret;
239 }
240
Sakari Ailus20b85222016-11-21 14:48:30 -0200241 media_graph_walk_start(&graph, entity);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300242
Sakari Ailus20b85222016-11-21 14:48:30 -0200243 while ((entity = media_graph_walk_next(&graph))) {
Sakari Ailusae5df812012-03-05 20:22:41 -0300244 struct isp_video *__video;
245
Sakari Ailus17d3d402015-12-16 11:32:30 -0200246 media_entity_enum_set(&pipe->ent_enum, entity);
Sakari Ailusae5df812012-03-05 20:22:41 -0300247
248 if (far_end != NULL)
249 continue;
250
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300251 if (entity == &video->video.entity)
252 continue;
253
Laurent Pinchart45b46872016-02-29 08:45:45 -0300254 if (!is_media_entity_v4l2_video_device(entity))
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300255 continue;
256
Sakari Ailusae5df812012-03-05 20:22:41 -0300257 __video = to_isp_video(media_entity_to_video_device(entity));
258 if (__video->type != video->type)
259 far_end = __video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300260 }
261
262 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusae5df812012-03-05 20:22:41 -0300263
Sakari Ailus20b85222016-11-21 14:48:30 -0200264 media_graph_walk_cleanup(&graph);
Sakari Ailus28461452015-12-16 11:32:24 -0200265
Sakari Ailusae5df812012-03-05 20:22:41 -0300266 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;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300278}
279
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300280static 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
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300292 fmt.pad = pad;
293 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300294
Laurent Pinchart52646822013-11-04 06:52:10 -0300295 mutex_lock(&video->mutex);
296 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300297 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 ||
Laurent Pinchartf7abbe92014-05-19 12:14:42 -0300321 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage ||
322 vfh->format.fmt.pix.field != format.fmt.pix.field)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300323 return -EINVAL;
324
Laurent Pinchartf7abbe92014-05-19 12:14:42 -0300325 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300326}
327
328/* -----------------------------------------------------------------------------
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300329 * Video queue operations
330 */
331
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300332static int isp_video_queue_setup(struct vb2_queue *queue,
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300333 unsigned int *count, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300334 unsigned int sizes[], struct device *alloc_devs[])
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300335{
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300336 struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300337 struct isp_video *video = vfh->video;
338
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300339 *num_planes = 1;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300340
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300341 sizes[0] = vfh->format.fmt.pix.sizeimage;
342 if (sizes[0] == 0)
343 return -EINVAL;
344
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300345 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
346
347 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300348}
349
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300350static int isp_video_buffer_prepare(struct vb2_buffer *buf)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300351{
Junghak Sung2d700712015-09-22 10:30:30 -0300352 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300353 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300354 struct isp_buffer *buffer = to_isp_buffer(vbuf);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300355 struct isp_video *video = vfh->video;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300356 dma_addr_t addr;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300357
Laurent Pinchart661112c2013-12-09 11:36:51 -0300358 /* Refuse to prepare the buffer is the video node has registered an
359 * error. We don't need to take any lock here as the operation is
360 * inherently racy. The authoritative check will be performed in the
361 * queue handler, which can't return an error, this check is just a best
362 * effort to notify userspace as early as possible.
363 */
364 if (unlikely(video->error))
365 return -EIO;
366
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300367 addr = vb2_dma_contig_plane_dma_addr(buf, 0);
368 if (!IS_ALIGNED(addr, 32)) {
369 dev_dbg(video->isp->dev,
370 "Buffer address must be aligned to 32 bytes boundary.\n");
371 return -EINVAL;
372 }
373
Junghak Sung2d700712015-09-22 10:30:30 -0300374 vb2_set_plane_payload(&buffer->vb.vb2_buf, 0,
375 vfh->format.fmt.pix.sizeimage);
Laurent Pinchart21d85822014-03-09 20:17:12 -0300376 buffer->dma = addr;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300377
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300378 return 0;
379}
380
381/*
382 * isp_video_buffer_queue - Add buffer to streaming queue
383 * @buf: Video buffer
384 *
385 * In memory-to-memory mode, start streaming on the pipeline if buffers are
386 * queued on both the input and the output, if the pipeline isn't already busy.
387 * If the pipeline is busy, it will be restarted in the output module interrupt
388 * handler.
389 */
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300390static void isp_video_buffer_queue(struct vb2_buffer *buf)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300391{
Junghak Sung2d700712015-09-22 10:30:30 -0300392 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300393 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300394 struct isp_buffer *buffer = to_isp_buffer(vbuf);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300395 struct isp_video *video = vfh->video;
396 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
397 enum isp_pipeline_state state;
398 unsigned long flags;
399 unsigned int empty;
400 unsigned int start;
401
Laurent Pincharte8feb872014-03-09 20:57:53 -0300402 spin_lock_irqsave(&video->irqlock, flags);
403
Laurent Pinchart661112c2013-12-09 11:36:51 -0300404 if (unlikely(video->error)) {
Junghak Sung2d700712015-09-22 10:30:30 -0300405 vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Laurent Pincharte8feb872014-03-09 20:57:53 -0300406 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300407 return;
408 }
409
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300410 empty = list_empty(&video->dmaqueue);
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300411 list_add_tail(&buffer->irqlist, &video->dmaqueue);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300412
Laurent Pincharte8feb872014-03-09 20:57:53 -0300413 spin_unlock_irqrestore(&video->irqlock, flags);
414
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300415 if (empty) {
416 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
417 state = ISP_PIPELINE_QUEUE_OUTPUT;
418 else
419 state = ISP_PIPELINE_QUEUE_INPUT;
420
421 spin_lock_irqsave(&pipe->lock, flags);
422 pipe->state |= state;
423 video->ops->queue(video, buffer);
424 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
425
426 start = isp_pipeline_ready(pipe);
427 if (start)
428 pipe->state |= ISP_PIPELINE_STREAM;
429 spin_unlock_irqrestore(&pipe->lock, flags);
430
431 if (start)
432 omap3isp_pipeline_set_stream(pipe,
433 ISP_PIPELINE_STREAM_SINGLESHOT);
434 }
435}
436
Sakari Ailus87e062d2014-09-18 18:57:49 -0300437/*
438 * omap3isp_video_return_buffers - Return all queued buffers to videobuf2
439 * @video: ISP video object
440 * @state: new state for the returned buffers
441 *
442 * Return all buffers queued on the video node to videobuf2 in the given state.
443 * The buffer state should be VB2_BUF_STATE_QUEUED if called due to an error
444 * when starting the stream, or VB2_BUF_STATE_ERROR otherwise.
445 *
446 * The function must be called with the video irqlock held.
447 */
448static void omap3isp_video_return_buffers(struct isp_video *video,
449 enum vb2_buffer_state state)
450{
451 while (!list_empty(&video->dmaqueue)) {
452 struct isp_buffer *buf;
453
454 buf = list_first_entry(&video->dmaqueue,
455 struct isp_buffer, irqlist);
456 list_del(&buf->irqlist);
457 vb2_buffer_done(&buf->vb.vb2_buf, state);
458 }
459}
460
Sakari Ailus35c5f632014-09-18 18:57:48 -0300461static int isp_video_start_streaming(struct vb2_queue *queue,
462 unsigned int count)
463{
464 struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
465 struct isp_video *video = vfh->video;
466 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
467 unsigned long flags;
468 int ret;
469
470 /* In sensor-to-memory mode, the stream can be started synchronously
471 * to the stream on command. In memory-to-memory mode, it will be
472 * started when buffers are queued on both the input and output.
473 */
474 if (pipe->input)
475 return 0;
476
477 ret = omap3isp_pipeline_set_stream(pipe,
478 ISP_PIPELINE_STREAM_CONTINUOUS);
Sakari Ailus87e062d2014-09-18 18:57:49 -0300479 if (ret < 0) {
480 spin_lock_irqsave(&video->irqlock, flags);
481 omap3isp_video_return_buffers(video, VB2_BUF_STATE_QUEUED);
482 spin_unlock_irqrestore(&video->irqlock, flags);
Sakari Ailus35c5f632014-09-18 18:57:48 -0300483 return ret;
Sakari Ailus87e062d2014-09-18 18:57:49 -0300484 }
Sakari Ailus35c5f632014-09-18 18:57:48 -0300485
486 spin_lock_irqsave(&video->irqlock, flags);
487 if (list_empty(&video->dmaqueue))
488 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
489 spin_unlock_irqrestore(&video->irqlock, flags);
490
491 return 0;
492}
493
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300494static const struct vb2_ops isp_video_queue_ops = {
495 .queue_setup = isp_video_queue_setup,
496 .buf_prepare = isp_video_buffer_prepare,
497 .buf_queue = isp_video_buffer_queue,
Sakari Ailus35c5f632014-09-18 18:57:48 -0300498 .start_streaming = isp_video_start_streaming,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300499};
500
501/*
502 * omap3isp_video_buffer_next - Complete the current buffer and return the next
503 * @video: ISP video object
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300504 *
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300505 * Remove the current video buffer from the DMA queue and fill its timestamp and
506 * field count before handing it back to videobuf2.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300507 *
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300508 * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
509 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
510 * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300511 *
512 * The DMA queue is expected to contain at least one buffer.
513 *
514 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
515 * empty.
516 */
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300517struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300518{
519 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300520 enum vb2_buffer_state vb_state;
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300521 struct isp_buffer *buf;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300522 unsigned long flags;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300523
Laurent Pincharte8feb872014-03-09 20:57:53 -0300524 spin_lock_irqsave(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300525 if (WARN_ON(list_empty(&video->dmaqueue))) {
Laurent Pincharte8feb872014-03-09 20:57:53 -0300526 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300527 return NULL;
528 }
529
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300530 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300531 irqlist);
532 list_del(&buf->irqlist);
Laurent Pincharte8feb872014-03-09 20:57:53 -0300533 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300534
Junghak Sungd6dd6452015-11-03 08:16:37 -0200535 buf->vb.vb2_buf.timestamp = ktime_get_ns();
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300536
537 /* Do frame number propagation only if this is the output video node.
538 * Frame number either comes from the CSI receivers or it gets
539 * incremented here if H3A is not active.
540 * Note: There is no guarantee that the output buffer will finish
541 * first, so the input number might lag behind by 1 in some cases.
542 */
543 if (video == pipe->output && !pipe->do_propagation)
Junghak Sung2d700712015-09-22 10:30:30 -0300544 buf->vb.sequence =
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300545 atomic_inc_return(&pipe->frame_number);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300546 else
Junghak Sung2d700712015-09-22 10:30:30 -0300547 buf->vb.sequence = atomic_read(&pipe->frame_number);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300548
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -0300549 if (pipe->field != V4L2_FIELD_NONE)
Junghak Sung2d700712015-09-22 10:30:30 -0300550 buf->vb.sequence /= 2;
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -0300551
Junghak Sung2d700712015-09-22 10:30:30 -0300552 buf->vb.field = pipe->field;
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -0300553
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300554 /* Report pipeline errors to userspace on the capture device side. */
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300555 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300556 vb_state = VB2_BUF_STATE_ERROR;
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300557 pipe->error = false;
558 } else {
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300559 vb_state = VB2_BUF_STATE_DONE;
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300560 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300561
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300562 vb2_buffer_done(&buf->vb.vb2_buf, vb_state);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300563
564 spin_lock_irqsave(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300565
566 if (list_empty(&video->dmaqueue)) {
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300567 enum isp_pipeline_state state;
568
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300569 spin_unlock_irqrestore(&video->irqlock, flags);
570
571 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300572 state = ISP_PIPELINE_QUEUE_OUTPUT
573 | ISP_PIPELINE_STREAM;
574 else
575 state = ISP_PIPELINE_QUEUE_INPUT
576 | ISP_PIPELINE_STREAM;
577
578 spin_lock_irqsave(&pipe->lock, flags);
579 pipe->state &= ~state;
580 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
581 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
582 spin_unlock_irqrestore(&pipe->lock, flags);
583 return NULL;
584 }
585
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300586 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
587 spin_lock(&pipe->lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300588 pipe->state &= ~ISP_PIPELINE_STREAM;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300589 spin_unlock(&pipe->lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300590 }
591
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300592 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300593 irqlist);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300594
595 spin_unlock_irqrestore(&video->irqlock, flags);
596
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300597 return buf;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300598}
599
600/*
Laurent Pinchart661112c2013-12-09 11:36:51 -0300601 * omap3isp_video_cancel_stream - Cancel stream on a video node
602 * @video: ISP video object
603 *
Sakari Ailus87e062d2014-09-18 18:57:49 -0300604 * Cancelling a stream returns all buffers queued on the video node to videobuf2
605 * in the erroneous state and makes sure no new buffer can be queued.
Laurent Pinchart661112c2013-12-09 11:36:51 -0300606 */
607void omap3isp_video_cancel_stream(struct isp_video *video)
608{
Laurent Pinchart661112c2013-12-09 11:36:51 -0300609 unsigned long flags;
610
Laurent Pincharte8feb872014-03-09 20:57:53 -0300611 spin_lock_irqsave(&video->irqlock, flags);
Sakari Ailus87e062d2014-09-18 18:57:49 -0300612 omap3isp_video_return_buffers(video, VB2_BUF_STATE_ERROR);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300613 video->error = true;
Laurent Pincharte8feb872014-03-09 20:57:53 -0300614 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300615}
616
617/*
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300618 * omap3isp_video_resume - Perform resume operation on the buffers
619 * @video: ISP video object
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300620 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300621 *
622 * This function is intended to be used on suspend/resume scenario. It
623 * requests video queue layer to discard buffers marked as DONE if it's in
624 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
625 * if there's any.
626 */
627void omap3isp_video_resume(struct isp_video *video, int continuous)
628{
629 struct isp_buffer *buf = NULL;
630
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300631 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
632 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300633 vb2_discard_done(video->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300634 mutex_unlock(&video->queue_lock);
635 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300636
637 if (!list_empty(&video->dmaqueue)) {
638 buf = list_first_entry(&video->dmaqueue,
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300639 struct isp_buffer, irqlist);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300640 video->ops->queue(video, buf);
641 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
642 } else {
643 if (continuous)
644 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
645 }
646}
647
648/* -----------------------------------------------------------------------------
649 * V4L2 ioctls
650 */
651
652static int
653isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
654{
655 struct isp_video *video = video_drvdata(file);
656
657 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
658 strlcpy(cap->card, video->video.name, sizeof(cap->card));
659 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300660
Sakari Ailus2c0108e2015-01-01 18:13:54 -0300661 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
662 | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
663
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300664 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Sakari Ailus2c0108e2015-01-01 18:13:54 -0300665 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300666 else
Sakari Ailus2c0108e2015-01-01 18:13:54 -0300667 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300668
669 return 0;
670}
671
672static int
673isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
674{
675 struct isp_video_fh *vfh = to_isp_video_fh(fh);
676 struct isp_video *video = video_drvdata(file);
677
678 if (format->type != video->type)
679 return -EINVAL;
680
681 mutex_lock(&video->mutex);
682 *format = vfh->format;
683 mutex_unlock(&video->mutex);
684
685 return 0;
686}
687
688static int
689isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
690{
691 struct isp_video_fh *vfh = to_isp_video_fh(fh);
692 struct isp_video *video = video_drvdata(file);
693 struct v4l2_mbus_framefmt fmt;
694
695 if (format->type != video->type)
696 return -EINVAL;
697
Laurent Pinchartbcb4e0e2014-05-19 19:40:04 -0300698 /* Replace unsupported field orders with sane defaults. */
699 switch (format->fmt.pix.field) {
700 case V4L2_FIELD_NONE:
701 /* Progressive is supported everywhere. */
702 break;
703 case V4L2_FIELD_ALTERNATE:
704 /* ALTERNATE is not supported on output nodes. */
705 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
706 format->fmt.pix.field = V4L2_FIELD_NONE;
707 break;
708 case V4L2_FIELD_INTERLACED:
709 /* The ISP has no concept of video standard, select the
710 * top-bottom order when the unqualified interlaced order is
711 * requested.
712 */
713 format->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
714 /* Fall-through */
715 case V4L2_FIELD_INTERLACED_TB:
716 case V4L2_FIELD_INTERLACED_BT:
717 /* Interlaced orders are only supported at the CCDC output. */
718 if (video != &video->isp->isp_ccdc.video_out)
719 format->fmt.pix.field = V4L2_FIELD_NONE;
720 break;
721 case V4L2_FIELD_TOP:
722 case V4L2_FIELD_BOTTOM:
723 case V4L2_FIELD_SEQ_TB:
724 case V4L2_FIELD_SEQ_BT:
725 default:
726 /* All other field orders are currently unsupported, default to
727 * progressive.
728 */
Laurent Pinchart48a8a0c2014-05-19 10:49:58 -0300729 format->fmt.pix.field = V4L2_FIELD_NONE;
Laurent Pinchartbcb4e0e2014-05-19 19:40:04 -0300730 break;
731 }
Laurent Pinchart48a8a0c2014-05-19 10:49:58 -0300732
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300733 /* Fill the bytesperline and sizeimage fields by converting to media bus
734 * format and back to pixel format.
735 */
736 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
737 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
738
Laurent Pinchart25c5cc92014-05-19 10:33:15 -0300739 mutex_lock(&video->mutex);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300740 vfh->format = *format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300741 mutex_unlock(&video->mutex);
Laurent Pinchart25c5cc92014-05-19 10:33:15 -0300742
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300743 return 0;
744}
745
746static int
747isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
748{
749 struct isp_video *video = video_drvdata(file);
750 struct v4l2_subdev_format fmt;
751 struct v4l2_subdev *subdev;
752 u32 pad;
753 int ret;
754
755 if (format->type != video->type)
756 return -EINVAL;
757
758 subdev = isp_video_remote_subdev(video, &pad);
759 if (subdev == NULL)
760 return -EINVAL;
761
762 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
763
764 fmt.pad = pad;
765 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
766 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
767 if (ret)
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300768 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300769
770 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
771 return 0;
772}
773
774static int
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200775isp_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300776{
777 struct isp_video *video = video_drvdata(file);
778 struct v4l2_subdev_format format;
779 struct v4l2_subdev *subdev;
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200780 struct v4l2_subdev_selection sdsel = {
781 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
782 .target = sel->target,
783 };
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300784 u32 pad;
785 int ret;
786
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200787 switch (sel->target) {
788 case V4L2_SEL_TGT_CROP:
789 case V4L2_SEL_TGT_CROP_BOUNDS:
790 case V4L2_SEL_TGT_CROP_DEFAULT:
791 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
792 return -EINVAL;
793 break;
794 case V4L2_SEL_TGT_COMPOSE:
795 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
796 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
797 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
798 return -EINVAL;
799 break;
800 default:
801 return -EINVAL;
802 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300803 subdev = isp_video_remote_subdev(video, &pad);
804 if (subdev == NULL)
805 return -EINVAL;
806
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200807 /* Try the get selection operation first and fallback to get format if not
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300808 * implemented.
809 */
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200810 sdsel.pad = pad;
811 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
812 if (!ret)
813 sel->r = sdsel.r;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300814 if (ret != -ENOIOCTLCMD)
815 return ret;
816
817 format.pad = pad;
818 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
819 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
820 if (ret < 0)
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300821 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300822
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200823 sel->r.left = 0;
824 sel->r.top = 0;
825 sel->r.width = format.format.width;
826 sel->r.height = format.format.height;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300827
828 return 0;
829}
830
831static int
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200832isp_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300833{
834 struct isp_video *video = video_drvdata(file);
835 struct v4l2_subdev *subdev;
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200836 struct v4l2_subdev_selection sdsel = {
837 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
838 .target = sel->target,
839 .flags = sel->flags,
840 .r = sel->r,
841 };
842 u32 pad;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300843 int ret;
844
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200845 switch (sel->target) {
846 case V4L2_SEL_TGT_CROP:
847 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
848 return -EINVAL;
849 break;
850 case V4L2_SEL_TGT_COMPOSE:
851 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
852 return -EINVAL;
853 break;
854 default:
855 return -EINVAL;
856 }
857 subdev = isp_video_remote_subdev(video, &pad);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300858 if (subdev == NULL)
859 return -EINVAL;
860
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200861 sdsel.pad = pad;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300862 mutex_lock(&video->mutex);
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200863 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300864 mutex_unlock(&video->mutex);
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200865 if (!ret)
866 sel->r = sdsel.r;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300867
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300868 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300869}
870
871static int
872isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
873{
874 struct isp_video_fh *vfh = to_isp_video_fh(fh);
875 struct isp_video *video = video_drvdata(file);
876
877 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
878 video->type != a->type)
879 return -EINVAL;
880
881 memset(a, 0, sizeof(*a));
882 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
883 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
884 a->parm.output.timeperframe = vfh->timeperframe;
885
886 return 0;
887}
888
889static int
890isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
891{
892 struct isp_video_fh *vfh = to_isp_video_fh(fh);
893 struct isp_video *video = video_drvdata(file);
894
895 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
896 video->type != a->type)
897 return -EINVAL;
898
899 if (a->parm.output.timeperframe.denominator == 0)
900 a->parm.output.timeperframe.denominator = 1;
901
902 vfh->timeperframe = a->parm.output.timeperframe;
903
904 return 0;
905}
906
907static int
908isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
909{
910 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300911 struct isp_video *video = video_drvdata(file);
912 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300913
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300914 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300915 ret = vb2_reqbufs(&vfh->queue, rb);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300916 mutex_unlock(&video->queue_lock);
917
918 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300919}
920
921static int
922isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
923{
924 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300925 struct isp_video *video = video_drvdata(file);
926 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300927
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300928 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300929 ret = vb2_querybuf(&vfh->queue, b);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300930 mutex_unlock(&video->queue_lock);
931
932 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300933}
934
935static int
936isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
937{
938 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300939 struct isp_video *video = video_drvdata(file);
940 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300941
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300942 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300943 ret = vb2_qbuf(&vfh->queue, b);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300944 mutex_unlock(&video->queue_lock);
945
946 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300947}
948
949static int
950isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
951{
952 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300953 struct isp_video *video = video_drvdata(file);
954 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300955
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300956 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300957 ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300958 mutex_unlock(&video->queue_lock);
959
960 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300961}
962
Sakari Ailusccddd912012-01-17 12:29:38 -0300963static int isp_video_check_external_subdevs(struct isp_video *video,
964 struct isp_pipeline *pipe)
965{
966 struct isp_device *isp = video->isp;
967 struct media_entity *ents[] = {
968 &isp->isp_csi2a.subdev.entity,
969 &isp->isp_csi2c.subdev.entity,
970 &isp->isp_ccp2.subdev.entity,
971 &isp->isp_ccdc.subdev.entity
972 };
973 struct media_pad *source_pad;
974 struct media_entity *source = NULL;
975 struct media_entity *sink;
976 struct v4l2_subdev_format fmt;
977 struct v4l2_ext_controls ctrls;
978 struct v4l2_ext_control ctrl;
979 unsigned int i;
Laurent Pinchart7c486c02014-02-14 21:45:50 -0300980 int ret;
Sakari Ailusccddd912012-01-17 12:29:38 -0300981
Laurent Pinchart4b2f1ed2014-02-14 21:40:48 -0300982 /* Memory-to-memory pipelines have no external subdev. */
983 if (pipe->input != NULL)
984 return 0;
985
Sakari Ailusccddd912012-01-17 12:29:38 -0300986 for (i = 0; i < ARRAY_SIZE(ents); i++) {
987 /* Is the entity part of the pipeline? */
Sakari Ailus17d3d402015-12-16 11:32:30 -0200988 if (!media_entity_enum_test(&pipe->ent_enum, ents[i]))
Sakari Ailusccddd912012-01-17 12:29:38 -0300989 continue;
990
991 /* ISP entities have always sink pad == 0. Find source. */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300992 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
Sakari Ailusccddd912012-01-17 12:29:38 -0300993 if (source_pad == NULL)
994 continue;
995
996 source = source_pad->entity;
997 sink = ents[i];
998 break;
999 }
1000
1001 if (!source) {
1002 dev_warn(isp->dev, "can't find source, failing now\n");
Laurent Pinchart7c486c02014-02-14 21:45:50 -03001003 return -EINVAL;
Sakari Ailusccddd912012-01-17 12:29:38 -03001004 }
1005
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001006 if (!is_media_entity_v4l2_subdev(source))
Sakari Ailusccddd912012-01-17 12:29:38 -03001007 return 0;
1008
1009 pipe->external = media_entity_to_v4l2_subdev(source);
1010
1011 fmt.pad = source_pad->index;
1012 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1013 ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
1014 pad, get_fmt, NULL, &fmt);
1015 if (unlikely(ret < 0)) {
1016 dev_warn(isp->dev, "get_fmt returned null!\n");
1017 return ret;
1018 }
1019
Laurent Pinchart1697e492011-08-31 11:57:12 -03001020 pipe->external_width =
1021 omap3isp_video_format_info(fmt.format.code)->width;
Sakari Ailusccddd912012-01-17 12:29:38 -03001022
1023 memset(&ctrls, 0, sizeof(ctrls));
1024 memset(&ctrl, 0, sizeof(ctrl));
1025
1026 ctrl.id = V4L2_CID_PIXEL_RATE;
1027
1028 ctrls.count = 1;
1029 ctrls.controls = &ctrl;
1030
1031 ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
1032 if (ret < 0) {
1033 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
1034 pipe->external->name);
1035 return ret;
1036 }
1037
1038 pipe->external_rate = ctrl.value64;
1039
Sakari Ailus17d3d402015-12-16 11:32:30 -02001040 if (media_entity_enum_test(&pipe->ent_enum,
1041 &isp->isp_ccdc.subdev.entity)) {
Sakari Ailusa6d7a622012-02-25 20:42:07 -03001042 unsigned int rate = UINT_MAX;
1043 /*
1044 * Check that maximum allowed CCDC pixel rate isn't
1045 * exceeded by the pixel rate.
1046 */
1047 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
1048 if (pipe->external_rate > rate)
1049 return -ENOSPC;
1050 }
1051
Sakari Ailusccddd912012-01-17 12:29:38 -03001052 return 0;
1053}
1054
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001055/*
1056 * Stream management
1057 *
1058 * Every ISP pipeline has a single input and a single output. The input can be
1059 * either a sensor or a video node. The output is always a video node.
1060 *
1061 * As every pipeline has an output video node, the ISP video objects at the
1062 * pipeline output stores the pipeline state. It tracks the streaming state of
1063 * both the input and output, as well as the availability of buffers.
1064 *
1065 * In sensor-to-memory mode, frames are always available at the pipeline input.
1066 * Starting the sensor usually requires I2C transfers and must be done in
1067 * interruptible context. The pipeline is started and stopped synchronously
1068 * to the stream on/off commands. All modules in the pipeline will get their
1069 * subdev set stream handler called. The module at the end of the pipeline must
1070 * delay starting the hardware until buffers are available at its output.
1071 *
1072 * In memory-to-memory mode, starting/stopping the stream requires
1073 * synchronization between the input and output. ISP modules can't be stopped
1074 * in the middle of a frame, and at least some of the modules seem to become
1075 * busy as soon as they're started, even if they don't receive a frame start
1076 * event. For that reason frames need to be processed in single-shot mode. The
1077 * driver needs to wait until a frame is completely processed and written to
1078 * memory before restarting the pipeline for the next frame. Pipelined
1079 * processing might be possible but requires more testing.
1080 *
1081 * Stream start must be delayed until buffers are available at both the input
1082 * and output. The pipeline must be started in the videobuf queue callback with
1083 * the buffers queue spinlock held. The modules subdev set stream operation must
1084 * not sleep.
1085 */
1086static int
1087isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1088{
1089 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1090 struct isp_video *video = video_drvdata(file);
1091 enum isp_pipeline_state state;
1092 struct isp_pipeline *pipe;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001093 unsigned long flags;
1094 int ret;
1095
1096 if (type != video->type)
1097 return -EINVAL;
1098
1099 mutex_lock(&video->stream_lock);
1100
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001101 /* Start streaming on the pipeline. No link touching an entity in the
1102 * pipeline can be activated or deactivated once streaming is started.
1103 */
1104 pipe = video->video.entity.pipe
1105 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
Sakari Ailusb0cd79e2012-01-16 18:59:02 -03001106
Sakari Ailus17d3d402015-12-16 11:32:30 -02001107 ret = media_entity_enum_init(&pipe->ent_enum, &video->isp->media_dev);
1108 if (ret)
1109 goto err_enum_init;
Sakari Ailusae5df812012-03-05 20:22:41 -03001110
Laurent Pinchart78c66fb2015-05-20 04:08:30 -03001111 /* TODO: Implement PM QoS */
Sakari Ailusb0cd79e2012-01-16 18:59:02 -03001112 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1113 pipe->max_rate = pipe->l3_ick;
1114
Sakari Ailus20b85222016-11-21 14:48:30 -02001115 ret = media_pipeline_start(&video->video.entity, &pipe->pipe);
Sakari Ailusda392572011-10-10 17:05:24 -03001116 if (ret < 0)
1117 goto err_pipeline_start;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001118
1119 /* Verify that the currently configured format matches the output of
1120 * the connected subdev.
1121 */
1122 ret = isp_video_check_format(video, vfh);
1123 if (ret < 0)
Sakari Ailusda392572011-10-10 17:05:24 -03001124 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001125
1126 video->bpl_padding = ret;
1127 video->bpl_value = vfh->format.fmt.pix.bytesperline;
1128
Sakari Ailusae5df812012-03-05 20:22:41 -03001129 ret = isp_video_get_graph_data(video, pipe);
1130 if (ret < 0)
1131 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001132
Sakari Ailusae5df812012-03-05 20:22:41 -03001133 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001134 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
Sakari Ailusae5df812012-03-05 20:22:41 -03001135 else
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001136 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001137
Sakari Ailusccddd912012-01-17 12:29:38 -03001138 ret = isp_video_check_external_subdevs(video, pipe);
1139 if (ret < 0)
1140 goto err_check_format;
1141
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001142 pipe->error = false;
1143
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001144 spin_lock_irqsave(&pipe->lock, flags);
1145 pipe->state &= ~ISP_PIPELINE_STREAM;
1146 pipe->state |= state;
1147 spin_unlock_irqrestore(&pipe->lock, flags);
1148
1149 /* Set the maximum time per frame as the value requested by userspace.
1150 * This is a soft limit that can be overridden if the hardware doesn't
1151 * support the request limit.
1152 */
1153 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1154 pipe->max_timeperframe = vfh->timeperframe;
1155
1156 video->queue = &vfh->queue;
1157 INIT_LIST_HEAD(&video->dmaqueue);
1158 atomic_set(&pipe->frame_number, -1);
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -03001159 pipe->field = vfh->format.fmt.pix.field;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001160
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001161 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001162 ret = vb2_streamon(&vfh->queue, type);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001163 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001164 if (ret < 0)
Sakari Ailusda392572011-10-10 17:05:24 -03001165 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001166
Sakari Ailusda392572011-10-10 17:05:24 -03001167 mutex_unlock(&video->stream_lock);
Sakari Ailus17d3d402015-12-16 11:32:30 -02001168
Sakari Ailusda392572011-10-10 17:05:24 -03001169 return 0;
1170
Sakari Ailusda392572011-10-10 17:05:24 -03001171err_check_format:
Sakari Ailus20b85222016-11-21 14:48:30 -02001172 media_pipeline_stop(&video->video.entity);
Sakari Ailusda392572011-10-10 17:05:24 -03001173err_pipeline_start:
Laurent Pinchart78c66fb2015-05-20 04:08:30 -03001174 /* TODO: Implement PM QoS */
Sakari Ailusda392572011-10-10 17:05:24 -03001175 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1176 * will get triggered the next time the CCDC is powered up will try to
1177 * access buffers that might have been freed but still present in the
1178 * DMA queue. This can easily get triggered if the above
1179 * omap3isp_pipeline_set_stream() call fails on a system with a
1180 * free-running sensor.
1181 */
1182 INIT_LIST_HEAD(&video->dmaqueue);
1183 video->queue = NULL;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001184
Sakari Ailus17d3d402015-12-16 11:32:30 -02001185 media_entity_enum_cleanup(&pipe->ent_enum);
1186
1187err_enum_init:
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001188 mutex_unlock(&video->stream_lock);
Sakari Ailus17d3d402015-12-16 11:32:30 -02001189
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001190 return ret;
1191}
1192
1193static int
1194isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1195{
1196 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1197 struct isp_video *video = video_drvdata(file);
1198 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1199 enum isp_pipeline_state state;
1200 unsigned int streaming;
1201 unsigned long flags;
1202
1203 if (type != video->type)
1204 return -EINVAL;
1205
1206 mutex_lock(&video->stream_lock);
1207
1208 /* Make sure we're not streaming yet. */
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001209 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001210 streaming = vb2_is_streaming(&vfh->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001211 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001212
1213 if (!streaming)
1214 goto done;
1215
1216 /* Update the pipeline state. */
1217 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1218 state = ISP_PIPELINE_STREAM_OUTPUT
1219 | ISP_PIPELINE_QUEUE_OUTPUT;
1220 else
1221 state = ISP_PIPELINE_STREAM_INPUT
1222 | ISP_PIPELINE_QUEUE_INPUT;
1223
1224 spin_lock_irqsave(&pipe->lock, flags);
1225 pipe->state &= ~state;
1226 spin_unlock_irqrestore(&pipe->lock, flags);
1227
1228 /* Stop the stream. */
1229 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
Laurent Pinchart08344492014-04-20 20:59:03 -03001230 omap3isp_video_cancel_stream(video);
1231
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001232 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001233 vb2_streamoff(&vfh->queue, type);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001234 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001235 video->queue = NULL;
Laurent Pinchart661112c2013-12-09 11:36:51 -03001236 video->error = false;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001237
Laurent Pinchart78c66fb2015-05-20 04:08:30 -03001238 /* TODO: Implement PM QoS */
Sakari Ailus20b85222016-11-21 14:48:30 -02001239 media_pipeline_stop(&video->video.entity);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001240
Sakari Ailus17d3d402015-12-16 11:32:30 -02001241 media_entity_enum_cleanup(&pipe->ent_enum);
1242
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001243done:
1244 mutex_unlock(&video->stream_lock);
1245 return 0;
1246}
1247
1248static int
1249isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1250{
1251 if (input->index > 0)
1252 return -EINVAL;
1253
1254 strlcpy(input->name, "camera", sizeof(input->name));
1255 input->type = V4L2_INPUT_TYPE_CAMERA;
1256
1257 return 0;
1258}
1259
1260static int
1261isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1262{
1263 *input = 0;
1264
1265 return 0;
1266}
1267
1268static int
1269isp_video_s_input(struct file *file, void *fh, unsigned int input)
1270{
1271 return input == 0 ? 0 : -EINVAL;
1272}
1273
1274static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1275 .vidioc_querycap = isp_video_querycap,
1276 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1277 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1278 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1279 .vidioc_g_fmt_vid_out = isp_video_get_format,
1280 .vidioc_s_fmt_vid_out = isp_video_set_format,
1281 .vidioc_try_fmt_vid_out = isp_video_try_format,
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001282 .vidioc_g_selection = isp_video_get_selection,
1283 .vidioc_s_selection = isp_video_set_selection,
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001284 .vidioc_g_parm = isp_video_get_param,
1285 .vidioc_s_parm = isp_video_set_param,
1286 .vidioc_reqbufs = isp_video_reqbufs,
1287 .vidioc_querybuf = isp_video_querybuf,
1288 .vidioc_qbuf = isp_video_qbuf,
1289 .vidioc_dqbuf = isp_video_dqbuf,
1290 .vidioc_streamon = isp_video_streamon,
1291 .vidioc_streamoff = isp_video_streamoff,
1292 .vidioc_enum_input = isp_video_enum_input,
1293 .vidioc_g_input = isp_video_g_input,
1294 .vidioc_s_input = isp_video_s_input,
1295};
1296
1297/* -----------------------------------------------------------------------------
1298 * V4L2 file operations
1299 */
1300
1301static int isp_video_open(struct file *file)
1302{
1303 struct isp_video *video = video_drvdata(file);
1304 struct isp_video_fh *handle;
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001305 struct vb2_queue *queue;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001306 int ret = 0;
1307
1308 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1309 if (handle == NULL)
1310 return -ENOMEM;
1311
1312 v4l2_fh_init(&handle->vfh, &video->video);
1313 v4l2_fh_add(&handle->vfh);
1314
1315 /* If this is the first user, initialise the pipeline. */
1316 if (omap3isp_get(video->isp) == NULL) {
1317 ret = -EBUSY;
1318 goto done;
1319 }
1320
Sakari Ailus506a47e2016-02-21 13:25:10 -03001321 ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001322 if (ret < 0) {
1323 omap3isp_put(video->isp);
1324 goto done;
1325 }
1326
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001327 queue = &handle->queue;
1328 queue->type = video->type;
1329 queue->io_modes = VB2_MMAP | VB2_USERPTR;
1330 queue->drv_priv = handle;
1331 queue->ops = &isp_video_queue_ops;
1332 queue->mem_ops = &vb2_dma_contig_memops;
1333 queue->buf_struct_size = sizeof(struct isp_buffer);
1334 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Hans Verkuil1ad70ce2016-02-15 13:41:51 -02001335 queue->dev = video->isp->dev;
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001336
1337 ret = vb2_queue_init(&handle->queue);
1338 if (ret < 0) {
1339 omap3isp_put(video->isp);
1340 goto done;
1341 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001342
1343 memset(&handle->format, 0, sizeof(handle->format));
1344 handle->format.type = video->type;
1345 handle->timeperframe.denominator = 1;
1346
1347 handle->video = video;
1348 file->private_data = &handle->vfh;
1349
1350done:
1351 if (ret < 0) {
1352 v4l2_fh_del(&handle->vfh);
1353 kfree(handle);
1354 }
1355
1356 return ret;
1357}
1358
1359static int isp_video_release(struct file *file)
1360{
1361 struct isp_video *video = video_drvdata(file);
1362 struct v4l2_fh *vfh = file->private_data;
1363 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1364
1365 /* Disable streaming and free the buffers queue resources. */
1366 isp_video_streamoff(file, vfh, video->type);
1367
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001368 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001369 vb2_queue_release(&handle->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001370 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001371
Sakari Ailus506a47e2016-02-21 13:25:10 -03001372 v4l2_pipeline_pm_use(&video->video.entity, 0);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001373
1374 /* Release the file handle. */
1375 v4l2_fh_del(vfh);
1376 kfree(handle);
1377 file->private_data = NULL;
1378
1379 omap3isp_put(video->isp);
1380
1381 return 0;
1382}
1383
1384static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1385{
1386 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001387 struct isp_video *video = video_drvdata(file);
1388 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001389
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001390 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001391 ret = vb2_poll(&vfh->queue, file, wait);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001392 mutex_unlock(&video->queue_lock);
1393
1394 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001395}
1396
1397static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1398{
1399 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1400
Laurent Pinchart9be293692015-02-24 07:39:49 -03001401 return vb2_mmap(&vfh->queue, vma);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001402}
1403
1404static struct v4l2_file_operations isp_video_fops = {
1405 .owner = THIS_MODULE,
1406 .unlocked_ioctl = video_ioctl2,
1407 .open = isp_video_open,
1408 .release = isp_video_release,
1409 .poll = isp_video_poll,
1410 .mmap = isp_video_mmap,
1411};
1412
1413/* -----------------------------------------------------------------------------
1414 * ISP video core
1415 */
1416
1417static const struct isp_video_operations isp_video_dummy_ops = {
1418};
1419
1420int omap3isp_video_init(struct isp_video *video, const char *name)
1421{
1422 const char *direction;
1423 int ret;
1424
1425 switch (video->type) {
1426 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1427 direction = "output";
Sakari Ailus8dad9362013-10-02 20:17:52 -03001428 video->pad.flags = MEDIA_PAD_FL_SINK
1429 | MEDIA_PAD_FL_MUST_CONNECT;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001430 break;
1431 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1432 direction = "input";
Sakari Ailus8dad9362013-10-02 20:17:52 -03001433 video->pad.flags = MEDIA_PAD_FL_SOURCE
1434 | MEDIA_PAD_FL_MUST_CONNECT;
Hans Verkuil954f3402012-09-05 06:05:50 -03001435 video->video.vfl_dir = VFL_DIR_TX;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001436 break;
1437
1438 default:
1439 return -EINVAL;
1440 }
1441
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001442 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
Hans Verkuil1ad70ce2016-02-15 13:41:51 -02001443 if (ret < 0)
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001444 return ret;
1445
1446 mutex_init(&video->mutex);
1447 atomic_set(&video->active, 0);
1448
1449 spin_lock_init(&video->pipe.lock);
1450 mutex_init(&video->stream_lock);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001451 mutex_init(&video->queue_lock);
Laurent Pincharte8feb872014-03-09 20:57:53 -03001452 spin_lock_init(&video->irqlock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001453
1454 /* Initialize the video device. */
1455 if (video->ops == NULL)
1456 video->ops = &isp_video_dummy_ops;
1457
1458 video->video.fops = &isp_video_fops;
1459 snprintf(video->video.name, sizeof(video->video.name),
1460 "OMAP3 ISP %s %s", name, direction);
1461 video->video.vfl_type = VFL_TYPE_GRABBER;
1462 video->video.release = video_device_release_empty;
1463 video->video.ioctl_ops = &isp_video_ioctl_ops;
1464 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1465
1466 video_set_drvdata(&video->video, video);
1467
1468 return 0;
1469}
1470
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001471void omap3isp_video_cleanup(struct isp_video *video)
1472{
1473 media_entity_cleanup(&video->video.entity);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001474 mutex_destroy(&video->queue_lock);
Laurent Pincharted33ac82011-09-22 17:09:26 -03001475 mutex_destroy(&video->stream_lock);
1476 mutex_destroy(&video->mutex);
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001477}
1478
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001479int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1480{
1481 int ret;
1482
1483 video->video.v4l2_dev = vdev;
1484
1485 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1486 if (ret < 0)
Laurent Pinchart4feca392012-10-22 08:28:51 -03001487 dev_err(video->isp->dev,
1488 "%s: could not register video device (%d)\n",
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001489 __func__, ret);
1490
1491 return ret;
1492}
1493
1494void omap3isp_video_unregister(struct isp_video *video)
1495{
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001496 if (video_is_registered(&video->video))
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001497 video_unregister_device(&video->video);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001498}