blob: 6bb4dd264b714e412c30958a52eea71a787abb45 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Laurent Pinchartad614ac2011-02-12 18:05:06 -03002/*
3 * ispvideo.c
4 *
5 * TI OMAP3 ISP - Generic video node
6 *
7 * Copyright (C) 2009-2010 Nokia Corporation
8 *
9 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 * Sakari Ailus <sakari.ailus@iki.fi>
Laurent Pinchartad614ac2011-02-12 18:05:06 -030011 */
12
13#include <asm/cacheflush.h>
14#include <linux/clk.h>
15#include <linux/mm.h>
Dmitry Artamonow025521f2011-11-20 12:54:26 -030016#include <linux/module.h>
Laurent Pinchartad614ac2011-02-12 18:05:06 -030017#include <linux/pagemap.h>
18#include <linux/scatterlist.h>
19#include <linux/sched.h>
20#include <linux/slab.h>
21#include <linux/vmalloc.h>
Sakari Ailus506a47e2016-02-21 13:25:10 -030022
Laurent Pinchartad614ac2011-02-12 18:05:06 -030023#include <media/v4l2-dev.h>
24#include <media/v4l2-ioctl.h>
Sakari Ailus506a47e2016-02-21 13:25:10 -030025#include <media/v4l2-mc.h>
Laurent Pinchartfbac1402014-03-09 20:36:15 -030026#include <media/videobuf2-dma-contig.h>
Laurent Pinchartad614ac2011-02-12 18:05:06 -030027
28#include "ispvideo.h"
29#include "isp.h"
30
31
32/* -----------------------------------------------------------------------------
33 * Helper functions
34 */
35
Sakari Ailus8f4f2982011-12-28 06:28:41 -030036/*
37 * NOTE: When adding new media bus codes, always remember to add
38 * corresponding in-memory formats to the table below!!!
39 */
Laurent Pinchartad614ac2011-02-12 18:05:06 -030040static struct isp_format_info formats[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030041 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
42 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030043 V4L2_PIX_FMT_GREY, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030044 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
45 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030046 V4L2_PIX_FMT_Y10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030047 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
48 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030049 V4L2_PIX_FMT_Y12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030050 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
51 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030052 V4L2_PIX_FMT_SBGGR8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030053 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
54 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030055 V4L2_PIX_FMT_SGBRG8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030056 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
57 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030058 V4L2_PIX_FMT_SGRBG8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030059 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
60 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030061 V4L2_PIX_FMT_SRGGB8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030062 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
63 MEDIA_BUS_FMT_SBGGR10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030064 V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030065 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
66 MEDIA_BUS_FMT_SGBRG10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030067 V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030068 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
69 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030070 V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030071 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
72 MEDIA_BUS_FMT_SRGGB10_1X10, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -030073 V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030074 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
75 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030076 V4L2_PIX_FMT_SBGGR10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030077 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
78 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030079 V4L2_PIX_FMT_SGBRG10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030080 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
81 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030082 V4L2_PIX_FMT_SGRBG10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030083 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
84 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030085 V4L2_PIX_FMT_SRGGB10, 10, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030086 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
87 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030088 V4L2_PIX_FMT_SBGGR12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030089 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
90 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030091 V4L2_PIX_FMT_SGBRG12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030092 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
93 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030094 V4L2_PIX_FMT_SGRBG12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030095 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
96 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
Laurent Pinchart1697e492011-08-31 11:57:12 -030097 V4L2_PIX_FMT_SRGGB12, 12, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030098 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
99 MEDIA_BUS_FMT_UYVY8_1X16, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -0300100 V4L2_PIX_FMT_UYVY, 16, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300101 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
102 MEDIA_BUS_FMT_YUYV8_1X16, 0,
Laurent Pinchart1697e492011-08-31 11:57:12 -0300103 V4L2_PIX_FMT_YUYV, 16, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300104 { MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_UYVY8_2X8,
105 MEDIA_BUS_FMT_UYVY8_2X8, 0,
Laurent Pinchart0810fd92012-05-24 11:55:00 -0300106 V4L2_PIX_FMT_UYVY, 8, 2, },
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300107 { MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YUYV8_2X8,
108 MEDIA_BUS_FMT_YUYV8_2X8, 0,
Laurent Pinchart0810fd92012-05-24 11:55:00 -0300109 V4L2_PIX_FMT_YUYV, 8, 2, },
Laurent Pinchartc51364c2011-08-31 11:03:53 -0300110 /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
111 * module and avoid NULL pointer dereferences.
112 */
113 { 0, }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300114};
115
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300116const struct isp_format_info *omap3isp_video_format_info(u32 code)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300117{
118 unsigned int i;
119
120 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
121 if (formats[i].code == code)
122 return &formats[i];
123 }
124
125 return NULL;
126}
127
128/*
129 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
130 * @video: ISP video instance
131 * @mbus: v4l2_mbus_framefmt format (input)
132 * @pix: v4l2_pix_format format (output)
133 *
134 * Fill the output pix structure with information from the input mbus format.
135 * The bytesperline and sizeimage fields are computed from the requested bytes
136 * per line value in the pix format and information from the video instance.
137 *
138 * Return the number of padding bytes at end of line.
139 */
140static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
141 const struct v4l2_mbus_framefmt *mbus,
142 struct v4l2_pix_format *pix)
143{
144 unsigned int bpl = pix->bytesperline;
145 unsigned int min_bpl;
146 unsigned int i;
147
148 memset(pix, 0, sizeof(*pix));
149 pix->width = mbus->width;
150 pix->height = mbus->height;
151
152 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
153 if (formats[i].code == mbus->code)
154 break;
155 }
156
157 if (WARN_ON(i == ARRAY_SIZE(formats)))
158 return 0;
159
Laurent Pinchart1697e492011-08-31 11:57:12 -0300160 min_bpl = pix->width * formats[i].bpp;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300161
162 /* Clamp the requested bytes per line value. If the maximum bytes per
163 * line value is zero, the module doesn't support user configurable line
164 * sizes. Override the requested value with the minimum in that case.
165 */
166 if (video->bpl_max)
167 bpl = clamp(bpl, min_bpl, video->bpl_max);
168 else
169 bpl = min_bpl;
170
171 if (!video->bpl_zero_padding || bpl != min_bpl)
172 bpl = ALIGN(bpl, video->bpl_alignment);
173
174 pix->pixelformat = formats[i].pixelformat;
175 pix->bytesperline = bpl;
176 pix->sizeimage = pix->bytesperline * pix->height;
177 pix->colorspace = mbus->colorspace;
178 pix->field = mbus->field;
179
180 return bpl - min_bpl;
181}
182
183static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
184 struct v4l2_mbus_framefmt *mbus)
185{
186 unsigned int i;
187
188 memset(mbus, 0, sizeof(*mbus));
189 mbus->width = pix->width;
190 mbus->height = pix->height;
191
Laurent Pinchartc3cd2572011-11-28 08:25:30 -0300192 /* Skip the last format in the loop so that it will be selected if no
193 * match is found.
194 */
195 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300196 if (formats[i].pixelformat == pix->pixelformat)
197 break;
198 }
199
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300200 mbus->code = formats[i].code;
201 mbus->colorspace = pix->colorspace;
202 mbus->field = pix->field;
203}
204
205static struct v4l2_subdev *
206isp_video_remote_subdev(struct isp_video *video, u32 *pad)
207{
208 struct media_pad *remote;
209
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300210 remote = media_entity_remote_pad(&video->pad);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300211
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -0300212 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300213 return NULL;
214
215 if (pad)
216 *pad = remote->index;
217
218 return media_entity_to_v4l2_subdev(remote->entity);
219}
220
221/* Return a pointer to the ISP video instance at the far end of the pipeline. */
Sakari Ailusae5df812012-03-05 20:22:41 -0300222static int isp_video_get_graph_data(struct isp_video *video,
223 struct isp_pipeline *pipe)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300224{
Sakari Ailus20b85222016-11-21 14:48:30 -0200225 struct media_graph graph;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300226 struct media_entity *entity = &video->video.entity;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300227 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300228 struct isp_video *far_end = NULL;
Sakari Ailus28461452015-12-16 11:32:24 -0200229 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300230
231 mutex_lock(&mdev->graph_mutex);
Sakari Ailus79ac44c2016-11-28 08:42:44 -0200232 ret = media_graph_walk_init(&graph, mdev);
Sakari Ailus28461452015-12-16 11:32:24 -0200233 if (ret) {
234 mutex_unlock(&mdev->graph_mutex);
235 return ret;
236 }
237
Sakari Ailus20b85222016-11-21 14:48:30 -0200238 media_graph_walk_start(&graph, entity);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300239
Sakari Ailus20b85222016-11-21 14:48:30 -0200240 while ((entity = media_graph_walk_next(&graph))) {
Sakari Ailusae5df812012-03-05 20:22:41 -0300241 struct isp_video *__video;
242
Sakari Ailus17d3d402015-12-16 11:32:30 -0200243 media_entity_enum_set(&pipe->ent_enum, entity);
Sakari Ailusae5df812012-03-05 20:22:41 -0300244
245 if (far_end != NULL)
246 continue;
247
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300248 if (entity == &video->video.entity)
249 continue;
250
Laurent Pinchart45b46872016-02-29 08:45:45 -0300251 if (!is_media_entity_v4l2_video_device(entity))
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300252 continue;
253
Sakari Ailusae5df812012-03-05 20:22:41 -0300254 __video = to_isp_video(media_entity_to_video_device(entity));
255 if (__video->type != video->type)
256 far_end = __video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300257 }
258
259 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusae5df812012-03-05 20:22:41 -0300260
Sakari Ailus20b85222016-11-21 14:48:30 -0200261 media_graph_walk_cleanup(&graph);
Sakari Ailus28461452015-12-16 11:32:24 -0200262
Sakari Ailusae5df812012-03-05 20:22:41 -0300263 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
264 pipe->input = far_end;
265 pipe->output = video;
266 } else {
267 if (far_end == NULL)
268 return -EPIPE;
269
270 pipe->input = video;
271 pipe->output = far_end;
272 }
273
274 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300275}
276
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300277static int
278__isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
279{
280 struct v4l2_subdev_format fmt;
281 struct v4l2_subdev *subdev;
282 u32 pad;
283 int ret;
284
285 subdev = isp_video_remote_subdev(video, &pad);
286 if (subdev == NULL)
287 return -EINVAL;
288
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300289 fmt.pad = pad;
290 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300291
Laurent Pinchart52646822013-11-04 06:52:10 -0300292 mutex_lock(&video->mutex);
293 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300294 mutex_unlock(&video->mutex);
295
296 if (ret)
297 return ret;
298
299 format->type = video->type;
300 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
301}
302
303static int
304isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
305{
306 struct v4l2_format format;
307 int ret;
308
309 memcpy(&format, &vfh->format, sizeof(format));
310 ret = __isp_video_get_format(video, &format);
311 if (ret < 0)
312 return ret;
313
314 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
315 vfh->format.fmt.pix.height != format.fmt.pix.height ||
316 vfh->format.fmt.pix.width != format.fmt.pix.width ||
317 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
Laurent Pinchartf7abbe92014-05-19 12:14:42 -0300318 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage ||
319 vfh->format.fmt.pix.field != format.fmt.pix.field)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300320 return -EINVAL;
321
Laurent Pinchartf7abbe92014-05-19 12:14:42 -0300322 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300323}
324
325/* -----------------------------------------------------------------------------
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300326 * Video queue operations
327 */
328
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300329static int isp_video_queue_setup(struct vb2_queue *queue,
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300330 unsigned int *count, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300331 unsigned int sizes[], struct device *alloc_devs[])
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300332{
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300333 struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300334 struct isp_video *video = vfh->video;
335
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300336 *num_planes = 1;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300337
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300338 sizes[0] = vfh->format.fmt.pix.sizeimage;
339 if (sizes[0] == 0)
340 return -EINVAL;
341
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300342 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
343
344 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300345}
346
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300347static int isp_video_buffer_prepare(struct vb2_buffer *buf)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300348{
Junghak Sung2d700712015-09-22 10:30:30 -0300349 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300350 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300351 struct isp_buffer *buffer = to_isp_buffer(vbuf);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300352 struct isp_video *video = vfh->video;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300353 dma_addr_t addr;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300354
Laurent Pinchart661112c2013-12-09 11:36:51 -0300355 /* Refuse to prepare the buffer is the video node has registered an
356 * error. We don't need to take any lock here as the operation is
357 * inherently racy. The authoritative check will be performed in the
358 * queue handler, which can't return an error, this check is just a best
359 * effort to notify userspace as early as possible.
360 */
361 if (unlikely(video->error))
362 return -EIO;
363
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300364 addr = vb2_dma_contig_plane_dma_addr(buf, 0);
365 if (!IS_ALIGNED(addr, 32)) {
366 dev_dbg(video->isp->dev,
367 "Buffer address must be aligned to 32 bytes boundary.\n");
368 return -EINVAL;
369 }
370
Junghak Sung2d700712015-09-22 10:30:30 -0300371 vb2_set_plane_payload(&buffer->vb.vb2_buf, 0,
372 vfh->format.fmt.pix.sizeimage);
Laurent Pinchart21d85822014-03-09 20:17:12 -0300373 buffer->dma = addr;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300374
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300375 return 0;
376}
377
378/*
379 * isp_video_buffer_queue - Add buffer to streaming queue
380 * @buf: Video buffer
381 *
382 * In memory-to-memory mode, start streaming on the pipeline if buffers are
383 * queued on both the input and the output, if the pipeline isn't already busy.
384 * If the pipeline is busy, it will be restarted in the output module interrupt
385 * handler.
386 */
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300387static void isp_video_buffer_queue(struct vb2_buffer *buf)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300388{
Junghak Sung2d700712015-09-22 10:30:30 -0300389 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300390 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300391 struct isp_buffer *buffer = to_isp_buffer(vbuf);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300392 struct isp_video *video = vfh->video;
393 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
394 enum isp_pipeline_state state;
395 unsigned long flags;
396 unsigned int empty;
397 unsigned int start;
398
Laurent Pincharte8feb872014-03-09 20:57:53 -0300399 spin_lock_irqsave(&video->irqlock, flags);
400
Laurent Pinchart661112c2013-12-09 11:36:51 -0300401 if (unlikely(video->error)) {
Junghak Sung2d700712015-09-22 10:30:30 -0300402 vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Laurent Pincharte8feb872014-03-09 20:57:53 -0300403 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300404 return;
405 }
406
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300407 empty = list_empty(&video->dmaqueue);
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300408 list_add_tail(&buffer->irqlist, &video->dmaqueue);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300409
Laurent Pincharte8feb872014-03-09 20:57:53 -0300410 spin_unlock_irqrestore(&video->irqlock, flags);
411
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300412 if (empty) {
413 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
414 state = ISP_PIPELINE_QUEUE_OUTPUT;
415 else
416 state = ISP_PIPELINE_QUEUE_INPUT;
417
418 spin_lock_irqsave(&pipe->lock, flags);
419 pipe->state |= state;
420 video->ops->queue(video, buffer);
421 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
422
423 start = isp_pipeline_ready(pipe);
424 if (start)
425 pipe->state |= ISP_PIPELINE_STREAM;
426 spin_unlock_irqrestore(&pipe->lock, flags);
427
428 if (start)
429 omap3isp_pipeline_set_stream(pipe,
430 ISP_PIPELINE_STREAM_SINGLESHOT);
431 }
432}
433
Sakari Ailus87e062d2014-09-18 18:57:49 -0300434/*
435 * omap3isp_video_return_buffers - Return all queued buffers to videobuf2
436 * @video: ISP video object
437 * @state: new state for the returned buffers
438 *
439 * Return all buffers queued on the video node to videobuf2 in the given state.
440 * The buffer state should be VB2_BUF_STATE_QUEUED if called due to an error
441 * when starting the stream, or VB2_BUF_STATE_ERROR otherwise.
442 *
443 * The function must be called with the video irqlock held.
444 */
445static void omap3isp_video_return_buffers(struct isp_video *video,
446 enum vb2_buffer_state state)
447{
448 while (!list_empty(&video->dmaqueue)) {
449 struct isp_buffer *buf;
450
451 buf = list_first_entry(&video->dmaqueue,
452 struct isp_buffer, irqlist);
453 list_del(&buf->irqlist);
454 vb2_buffer_done(&buf->vb.vb2_buf, state);
455 }
456}
457
Sakari Ailus35c5f632014-09-18 18:57:48 -0300458static int isp_video_start_streaming(struct vb2_queue *queue,
459 unsigned int count)
460{
461 struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
462 struct isp_video *video = vfh->video;
463 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
464 unsigned long flags;
465 int ret;
466
467 /* In sensor-to-memory mode, the stream can be started synchronously
468 * to the stream on command. In memory-to-memory mode, it will be
469 * started when buffers are queued on both the input and output.
470 */
471 if (pipe->input)
472 return 0;
473
474 ret = omap3isp_pipeline_set_stream(pipe,
475 ISP_PIPELINE_STREAM_CONTINUOUS);
Sakari Ailus87e062d2014-09-18 18:57:49 -0300476 if (ret < 0) {
477 spin_lock_irqsave(&video->irqlock, flags);
478 omap3isp_video_return_buffers(video, VB2_BUF_STATE_QUEUED);
479 spin_unlock_irqrestore(&video->irqlock, flags);
Sakari Ailus35c5f632014-09-18 18:57:48 -0300480 return ret;
Sakari Ailus87e062d2014-09-18 18:57:49 -0300481 }
Sakari Ailus35c5f632014-09-18 18:57:48 -0300482
483 spin_lock_irqsave(&video->irqlock, flags);
484 if (list_empty(&video->dmaqueue))
485 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
486 spin_unlock_irqrestore(&video->irqlock, flags);
487
488 return 0;
489}
490
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300491static const struct vb2_ops isp_video_queue_ops = {
492 .queue_setup = isp_video_queue_setup,
493 .buf_prepare = isp_video_buffer_prepare,
494 .buf_queue = isp_video_buffer_queue,
Sakari Ailus35c5f632014-09-18 18:57:48 -0300495 .start_streaming = isp_video_start_streaming,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300496};
497
498/*
499 * omap3isp_video_buffer_next - Complete the current buffer and return the next
500 * @video: ISP video object
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300501 *
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300502 * Remove the current video buffer from the DMA queue and fill its timestamp and
503 * field count before handing it back to videobuf2.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300504 *
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300505 * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
506 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
507 * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300508 *
509 * The DMA queue is expected to contain at least one buffer.
510 *
511 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
512 * empty.
513 */
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300514struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300515{
516 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300517 enum vb2_buffer_state vb_state;
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300518 struct isp_buffer *buf;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300519 unsigned long flags;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300520
Laurent Pincharte8feb872014-03-09 20:57:53 -0300521 spin_lock_irqsave(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300522 if (WARN_ON(list_empty(&video->dmaqueue))) {
Laurent Pincharte8feb872014-03-09 20:57:53 -0300523 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300524 return NULL;
525 }
526
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300527 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300528 irqlist);
529 list_del(&buf->irqlist);
Laurent Pincharte8feb872014-03-09 20:57:53 -0300530 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300531
Junghak Sungd6dd6452015-11-03 08:16:37 -0200532 buf->vb.vb2_buf.timestamp = ktime_get_ns();
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300533
534 /* Do frame number propagation only if this is the output video node.
535 * Frame number either comes from the CSI receivers or it gets
536 * incremented here if H3A is not active.
537 * Note: There is no guarantee that the output buffer will finish
538 * first, so the input number might lag behind by 1 in some cases.
539 */
540 if (video == pipe->output && !pipe->do_propagation)
Junghak Sung2d700712015-09-22 10:30:30 -0300541 buf->vb.sequence =
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300542 atomic_inc_return(&pipe->frame_number);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300543 else
Junghak Sung2d700712015-09-22 10:30:30 -0300544 buf->vb.sequence = atomic_read(&pipe->frame_number);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300545
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -0300546 if (pipe->field != V4L2_FIELD_NONE)
Junghak Sung2d700712015-09-22 10:30:30 -0300547 buf->vb.sequence /= 2;
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -0300548
Junghak Sung2d700712015-09-22 10:30:30 -0300549 buf->vb.field = pipe->field;
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -0300550
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300551 /* Report pipeline errors to userspace on the capture device side. */
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300552 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300553 vb_state = VB2_BUF_STATE_ERROR;
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300554 pipe->error = false;
555 } else {
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300556 vb_state = VB2_BUF_STATE_DONE;
Laurent Pinchart875e2e32011-12-07 08:34:50 -0300557 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300558
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300559 vb2_buffer_done(&buf->vb.vb2_buf, vb_state);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300560
561 spin_lock_irqsave(&video->irqlock, flags);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300562
563 if (list_empty(&video->dmaqueue)) {
Lad, Prabhakar18ba53f2015-02-25 12:58:14 -0300564 enum isp_pipeline_state state;
565
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300566 spin_unlock_irqrestore(&video->irqlock, flags);
567
568 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300569 state = ISP_PIPELINE_QUEUE_OUTPUT
570 | ISP_PIPELINE_STREAM;
571 else
572 state = ISP_PIPELINE_QUEUE_INPUT
573 | ISP_PIPELINE_STREAM;
574
575 spin_lock_irqsave(&pipe->lock, flags);
576 pipe->state &= ~state;
577 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
578 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
579 spin_unlock_irqrestore(&pipe->lock, flags);
580 return NULL;
581 }
582
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300583 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
584 spin_lock(&pipe->lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300585 pipe->state &= ~ISP_PIPELINE_STREAM;
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300586 spin_unlock(&pipe->lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300587 }
588
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300589 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300590 irqlist);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300591
592 spin_unlock_irqrestore(&video->irqlock, flags);
593
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300594 return buf;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300595}
596
597/*
Laurent Pinchart661112c2013-12-09 11:36:51 -0300598 * omap3isp_video_cancel_stream - Cancel stream on a video node
599 * @video: ISP video object
600 *
Sakari Ailus87e062d2014-09-18 18:57:49 -0300601 * Cancelling a stream returns all buffers queued on the video node to videobuf2
602 * in the erroneous state and makes sure no new buffer can be queued.
Laurent Pinchart661112c2013-12-09 11:36:51 -0300603 */
604void omap3isp_video_cancel_stream(struct isp_video *video)
605{
Laurent Pinchart661112c2013-12-09 11:36:51 -0300606 unsigned long flags;
607
Laurent Pincharte8feb872014-03-09 20:57:53 -0300608 spin_lock_irqsave(&video->irqlock, flags);
Sakari Ailus87e062d2014-09-18 18:57:49 -0300609 omap3isp_video_return_buffers(video, VB2_BUF_STATE_ERROR);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300610 video->error = true;
Laurent Pincharte8feb872014-03-09 20:57:53 -0300611 spin_unlock_irqrestore(&video->irqlock, flags);
Laurent Pinchart661112c2013-12-09 11:36:51 -0300612}
613
614/*
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300615 * omap3isp_video_resume - Perform resume operation on the buffers
616 * @video: ISP video object
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300617 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300618 *
619 * This function is intended to be used on suspend/resume scenario. It
620 * requests video queue layer to discard buffers marked as DONE if it's in
621 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
622 * if there's any.
623 */
624void omap3isp_video_resume(struct isp_video *video, int continuous)
625{
626 struct isp_buffer *buf = NULL;
627
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300628 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
629 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300630 vb2_discard_done(video->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300631 mutex_unlock(&video->queue_lock);
632 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300633
634 if (!list_empty(&video->dmaqueue)) {
635 buf = list_first_entry(&video->dmaqueue,
Laurent Pincharteb2c00d2014-03-09 20:57:53 -0300636 struct isp_buffer, irqlist);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300637 video->ops->queue(video, buf);
638 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
639 } else {
640 if (continuous)
641 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
642 }
643}
644
645/* -----------------------------------------------------------------------------
646 * V4L2 ioctls
647 */
648
649static int
650isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
651{
652 struct isp_video *video = video_drvdata(file);
653
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400654 strscpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
655 strscpy(cap->card, video->video.name, sizeof(cap->card));
656 strscpy(cap->bus_info, "media", sizeof(cap->bus_info));
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300657
Sakari Ailus2c0108e2015-01-01 18:13:54 -0300658 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
659 | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
660
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300661 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Sakari Ailus2c0108e2015-01-01 18:13:54 -0300662 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300663 else
Sakari Ailus2c0108e2015-01-01 18:13:54 -0300664 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300665
666 return 0;
667}
668
669static int
670isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
671{
672 struct isp_video_fh *vfh = to_isp_video_fh(fh);
673 struct isp_video *video = video_drvdata(file);
674
675 if (format->type != video->type)
676 return -EINVAL;
677
678 mutex_lock(&video->mutex);
679 *format = vfh->format;
680 mutex_unlock(&video->mutex);
681
682 return 0;
683}
684
685static int
686isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
687{
688 struct isp_video_fh *vfh = to_isp_video_fh(fh);
689 struct isp_video *video = video_drvdata(file);
690 struct v4l2_mbus_framefmt fmt;
691
692 if (format->type != video->type)
693 return -EINVAL;
694
Laurent Pinchartbcb4e0e2014-05-19 19:40:04 -0300695 /* Replace unsupported field orders with sane defaults. */
696 switch (format->fmt.pix.field) {
697 case V4L2_FIELD_NONE:
698 /* Progressive is supported everywhere. */
699 break;
700 case V4L2_FIELD_ALTERNATE:
701 /* ALTERNATE is not supported on output nodes. */
702 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
703 format->fmt.pix.field = V4L2_FIELD_NONE;
704 break;
705 case V4L2_FIELD_INTERLACED:
706 /* The ISP has no concept of video standard, select the
707 * top-bottom order when the unqualified interlaced order is
708 * requested.
709 */
710 format->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
711 /* Fall-through */
712 case V4L2_FIELD_INTERLACED_TB:
713 case V4L2_FIELD_INTERLACED_BT:
714 /* Interlaced orders are only supported at the CCDC output. */
715 if (video != &video->isp->isp_ccdc.video_out)
716 format->fmt.pix.field = V4L2_FIELD_NONE;
717 break;
718 case V4L2_FIELD_TOP:
719 case V4L2_FIELD_BOTTOM:
720 case V4L2_FIELD_SEQ_TB:
721 case V4L2_FIELD_SEQ_BT:
722 default:
723 /* All other field orders are currently unsupported, default to
724 * progressive.
725 */
Laurent Pinchart48a8a0c2014-05-19 10:49:58 -0300726 format->fmt.pix.field = V4L2_FIELD_NONE;
Laurent Pinchartbcb4e0e2014-05-19 19:40:04 -0300727 break;
728 }
Laurent Pinchart48a8a0c2014-05-19 10:49:58 -0300729
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300730 /* Fill the bytesperline and sizeimage fields by converting to media bus
731 * format and back to pixel format.
732 */
733 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
734 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
735
Laurent Pinchart25c5cc92014-05-19 10:33:15 -0300736 mutex_lock(&video->mutex);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300737 vfh->format = *format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300738 mutex_unlock(&video->mutex);
Laurent Pinchart25c5cc92014-05-19 10:33:15 -0300739
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300740 return 0;
741}
742
743static int
744isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
745{
746 struct isp_video *video = video_drvdata(file);
747 struct v4l2_subdev_format fmt;
748 struct v4l2_subdev *subdev;
749 u32 pad;
750 int ret;
751
752 if (format->type != video->type)
753 return -EINVAL;
754
755 subdev = isp_video_remote_subdev(video, &pad);
756 if (subdev == NULL)
757 return -EINVAL;
758
759 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
760
761 fmt.pad = pad;
762 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
763 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
764 if (ret)
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300765 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300766
767 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
768 return 0;
769}
770
771static int
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200772isp_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300773{
774 struct isp_video *video = video_drvdata(file);
775 struct v4l2_subdev_format format;
776 struct v4l2_subdev *subdev;
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200777 struct v4l2_subdev_selection sdsel = {
778 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
779 .target = sel->target,
780 };
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300781 u32 pad;
782 int ret;
783
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200784 switch (sel->target) {
785 case V4L2_SEL_TGT_CROP:
786 case V4L2_SEL_TGT_CROP_BOUNDS:
787 case V4L2_SEL_TGT_CROP_DEFAULT:
788 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
789 return -EINVAL;
790 break;
791 case V4L2_SEL_TGT_COMPOSE:
792 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
793 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
794 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
795 return -EINVAL;
796 break;
797 default:
798 return -EINVAL;
799 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300800 subdev = isp_video_remote_subdev(video, &pad);
801 if (subdev == NULL)
802 return -EINVAL;
803
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200804 /* Try the get selection operation first and fallback to get format if not
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300805 * implemented.
806 */
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200807 sdsel.pad = pad;
808 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
809 if (!ret)
810 sel->r = sdsel.r;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300811 if (ret != -ENOIOCTLCMD)
812 return ret;
813
814 format.pad = pad;
815 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
816 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
817 if (ret < 0)
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300818 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300819
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200820 sel->r.left = 0;
821 sel->r.top = 0;
822 sel->r.width = format.format.width;
823 sel->r.height = format.format.height;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300824
825 return 0;
826}
827
828static int
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200829isp_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300830{
831 struct isp_video *video = video_drvdata(file);
832 struct v4l2_subdev *subdev;
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200833 struct v4l2_subdev_selection sdsel = {
834 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
835 .target = sel->target,
836 .flags = sel->flags,
837 .r = sel->r,
838 };
839 u32 pad;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300840 int ret;
841
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200842 switch (sel->target) {
843 case V4L2_SEL_TGT_CROP:
844 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
845 return -EINVAL;
846 break;
847 case V4L2_SEL_TGT_COMPOSE:
848 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
849 return -EINVAL;
850 break;
851 default:
852 return -EINVAL;
853 }
854 subdev = isp_video_remote_subdev(video, &pad);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300855 if (subdev == NULL)
856 return -EINVAL;
857
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200858 sdsel.pad = pad;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300859 mutex_lock(&video->mutex);
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200860 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300861 mutex_unlock(&video->mutex);
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200862 if (!ret)
863 sel->r = sdsel.r;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300864
Wanlong Gaoa694f1b2012-08-27 04:23:15 -0300865 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300866}
867
868static int
869isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
870{
871 struct isp_video_fh *vfh = to_isp_video_fh(fh);
872 struct isp_video *video = video_drvdata(file);
873
874 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
875 video->type != a->type)
876 return -EINVAL;
877
878 memset(a, 0, sizeof(*a));
879 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
880 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
881 a->parm.output.timeperframe = vfh->timeperframe;
882
883 return 0;
884}
885
886static int
887isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
888{
889 struct isp_video_fh *vfh = to_isp_video_fh(fh);
890 struct isp_video *video = video_drvdata(file);
891
892 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
893 video->type != a->type)
894 return -EINVAL;
895
896 if (a->parm.output.timeperframe.denominator == 0)
897 a->parm.output.timeperframe.denominator = 1;
898
899 vfh->timeperframe = a->parm.output.timeperframe;
900
901 return 0;
902}
903
904static int
905isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
906{
907 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300908 struct isp_video *video = video_drvdata(file);
909 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300910
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300911 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300912 ret = vb2_reqbufs(&vfh->queue, rb);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300913 mutex_unlock(&video->queue_lock);
914
915 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300916}
917
918static int
919isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
920{
921 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300922 struct isp_video *video = video_drvdata(file);
923 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300924
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300925 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300926 ret = vb2_querybuf(&vfh->queue, b);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300927 mutex_unlock(&video->queue_lock);
928
929 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300930}
931
932static int
933isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
934{
935 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300936 struct isp_video *video = video_drvdata(file);
937 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300938
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300939 mutex_lock(&video->queue_lock);
Hans Verkuil394dc582018-05-30 02:46:22 -0400940 ret = vb2_qbuf(&vfh->queue, video->video.v4l2_dev->mdev, b);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300941 mutex_unlock(&video->queue_lock);
942
943 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300944}
945
946static int
947isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
948{
949 struct isp_video_fh *vfh = to_isp_video_fh(fh);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300950 struct isp_video *video = video_drvdata(file);
951 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300952
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300953 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -0300954 ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
Laurent Pinchart988d54c2014-03-09 20:57:53 -0300955 mutex_unlock(&video->queue_lock);
956
957 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300958}
959
Sakari Ailusccddd912012-01-17 12:29:38 -0300960static int isp_video_check_external_subdevs(struct isp_video *video,
961 struct isp_pipeline *pipe)
962{
963 struct isp_device *isp = video->isp;
964 struct media_entity *ents[] = {
965 &isp->isp_csi2a.subdev.entity,
966 &isp->isp_csi2c.subdev.entity,
967 &isp->isp_ccp2.subdev.entity,
968 &isp->isp_ccdc.subdev.entity
969 };
970 struct media_pad *source_pad;
971 struct media_entity *source = NULL;
972 struct media_entity *sink;
973 struct v4l2_subdev_format fmt;
974 struct v4l2_ext_controls ctrls;
975 struct v4l2_ext_control ctrl;
976 unsigned int i;
Laurent Pinchart7c486c02014-02-14 21:45:50 -0300977 int ret;
Sakari Ailusccddd912012-01-17 12:29:38 -0300978
Laurent Pinchart4b2f1ed2014-02-14 21:40:48 -0300979 /* Memory-to-memory pipelines have no external subdev. */
980 if (pipe->input != NULL)
981 return 0;
982
Sakari Ailusccddd912012-01-17 12:29:38 -0300983 for (i = 0; i < ARRAY_SIZE(ents); i++) {
984 /* Is the entity part of the pipeline? */
Sakari Ailus17d3d402015-12-16 11:32:30 -0200985 if (!media_entity_enum_test(&pipe->ent_enum, ents[i]))
Sakari Ailusccddd912012-01-17 12:29:38 -0300986 continue;
987
988 /* ISP entities have always sink pad == 0. Find source. */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300989 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
Sakari Ailusccddd912012-01-17 12:29:38 -0300990 if (source_pad == NULL)
991 continue;
992
993 source = source_pad->entity;
994 sink = ents[i];
995 break;
996 }
997
998 if (!source) {
999 dev_warn(isp->dev, "can't find source, failing now\n");
Laurent Pinchart7c486c02014-02-14 21:45:50 -03001000 return -EINVAL;
Sakari Ailusccddd912012-01-17 12:29:38 -03001001 }
1002
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001003 if (!is_media_entity_v4l2_subdev(source))
Sakari Ailusccddd912012-01-17 12:29:38 -03001004 return 0;
1005
1006 pipe->external = media_entity_to_v4l2_subdev(source);
1007
1008 fmt.pad = source_pad->index;
1009 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1010 ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
1011 pad, get_fmt, NULL, &fmt);
1012 if (unlikely(ret < 0)) {
1013 dev_warn(isp->dev, "get_fmt returned null!\n");
1014 return ret;
1015 }
1016
Laurent Pinchart1697e492011-08-31 11:57:12 -03001017 pipe->external_width =
1018 omap3isp_video_format_info(fmt.format.code)->width;
Sakari Ailusccddd912012-01-17 12:29:38 -03001019
1020 memset(&ctrls, 0, sizeof(ctrls));
1021 memset(&ctrl, 0, sizeof(ctrl));
1022
1023 ctrl.id = V4L2_CID_PIXEL_RATE;
1024
1025 ctrls.count = 1;
1026 ctrls.controls = &ctrl;
1027
Hans Verkuilc41e9cf2018-05-21 04:54:42 -04001028 ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, NULL, &ctrls);
Sakari Ailusccddd912012-01-17 12:29:38 -03001029 if (ret < 0) {
1030 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
1031 pipe->external->name);
1032 return ret;
1033 }
1034
1035 pipe->external_rate = ctrl.value64;
1036
Sakari Ailus17d3d402015-12-16 11:32:30 -02001037 if (media_entity_enum_test(&pipe->ent_enum,
1038 &isp->isp_ccdc.subdev.entity)) {
Sakari Ailusa6d7a622012-02-25 20:42:07 -03001039 unsigned int rate = UINT_MAX;
1040 /*
1041 * Check that maximum allowed CCDC pixel rate isn't
1042 * exceeded by the pixel rate.
1043 */
1044 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
1045 if (pipe->external_rate > rate)
1046 return -ENOSPC;
1047 }
1048
Sakari Ailusccddd912012-01-17 12:29:38 -03001049 return 0;
1050}
1051
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001052/*
1053 * Stream management
1054 *
1055 * Every ISP pipeline has a single input and a single output. The input can be
1056 * either a sensor or a video node. The output is always a video node.
1057 *
1058 * As every pipeline has an output video node, the ISP video objects at the
1059 * pipeline output stores the pipeline state. It tracks the streaming state of
1060 * both the input and output, as well as the availability of buffers.
1061 *
1062 * In sensor-to-memory mode, frames are always available at the pipeline input.
1063 * Starting the sensor usually requires I2C transfers and must be done in
1064 * interruptible context. The pipeline is started and stopped synchronously
1065 * to the stream on/off commands. All modules in the pipeline will get their
1066 * subdev set stream handler called. The module at the end of the pipeline must
1067 * delay starting the hardware until buffers are available at its output.
1068 *
1069 * In memory-to-memory mode, starting/stopping the stream requires
1070 * synchronization between the input and output. ISP modules can't be stopped
1071 * in the middle of a frame, and at least some of the modules seem to become
1072 * busy as soon as they're started, even if they don't receive a frame start
1073 * event. For that reason frames need to be processed in single-shot mode. The
1074 * driver needs to wait until a frame is completely processed and written to
1075 * memory before restarting the pipeline for the next frame. Pipelined
1076 * processing might be possible but requires more testing.
1077 *
1078 * Stream start must be delayed until buffers are available at both the input
1079 * and output. The pipeline must be started in the videobuf queue callback with
1080 * the buffers queue spinlock held. The modules subdev set stream operation must
1081 * not sleep.
1082 */
1083static int
1084isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1085{
1086 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1087 struct isp_video *video = video_drvdata(file);
1088 enum isp_pipeline_state state;
1089 struct isp_pipeline *pipe;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001090 unsigned long flags;
1091 int ret;
1092
1093 if (type != video->type)
1094 return -EINVAL;
1095
1096 mutex_lock(&video->stream_lock);
1097
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001098 /* Start streaming on the pipeline. No link touching an entity in the
1099 * pipeline can be activated or deactivated once streaming is started.
1100 */
1101 pipe = video->video.entity.pipe
1102 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
Sakari Ailusb0cd79e2012-01-16 18:59:02 -03001103
Sakari Ailus17d3d402015-12-16 11:32:30 -02001104 ret = media_entity_enum_init(&pipe->ent_enum, &video->isp->media_dev);
1105 if (ret)
1106 goto err_enum_init;
Sakari Ailusae5df812012-03-05 20:22:41 -03001107
Laurent Pinchart78c66fb2015-05-20 04:08:30 -03001108 /* TODO: Implement PM QoS */
Sakari Ailusb0cd79e2012-01-16 18:59:02 -03001109 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1110 pipe->max_rate = pipe->l3_ick;
1111
Sakari Ailus20b85222016-11-21 14:48:30 -02001112 ret = media_pipeline_start(&video->video.entity, &pipe->pipe);
Sakari Ailusda392572011-10-10 17:05:24 -03001113 if (ret < 0)
1114 goto err_pipeline_start;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001115
1116 /* Verify that the currently configured format matches the output of
1117 * the connected subdev.
1118 */
1119 ret = isp_video_check_format(video, vfh);
1120 if (ret < 0)
Sakari Ailusda392572011-10-10 17:05:24 -03001121 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001122
1123 video->bpl_padding = ret;
1124 video->bpl_value = vfh->format.fmt.pix.bytesperline;
1125
Sakari Ailusae5df812012-03-05 20:22:41 -03001126 ret = isp_video_get_graph_data(video, pipe);
1127 if (ret < 0)
1128 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001129
Sakari Ailusae5df812012-03-05 20:22:41 -03001130 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001131 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
Sakari Ailusae5df812012-03-05 20:22:41 -03001132 else
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001133 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001134
Sakari Ailusccddd912012-01-17 12:29:38 -03001135 ret = isp_video_check_external_subdevs(video, pipe);
1136 if (ret < 0)
1137 goto err_check_format;
1138
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001139 pipe->error = false;
1140
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001141 spin_lock_irqsave(&pipe->lock, flags);
1142 pipe->state &= ~ISP_PIPELINE_STREAM;
1143 pipe->state |= state;
1144 spin_unlock_irqrestore(&pipe->lock, flags);
1145
1146 /* Set the maximum time per frame as the value requested by userspace.
1147 * This is a soft limit that can be overridden if the hardware doesn't
1148 * support the request limit.
1149 */
1150 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1151 pipe->max_timeperframe = vfh->timeperframe;
1152
1153 video->queue = &vfh->queue;
1154 INIT_LIST_HEAD(&video->dmaqueue);
1155 atomic_set(&pipe->frame_number, -1);
Laurent Pinchart9a36d8e2014-05-19 16:37:38 -03001156 pipe->field = vfh->format.fmt.pix.field;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001157
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001158 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001159 ret = vb2_streamon(&vfh->queue, type);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001160 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001161 if (ret < 0)
Sakari Ailusda392572011-10-10 17:05:24 -03001162 goto err_check_format;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001163
Sakari Ailusda392572011-10-10 17:05:24 -03001164 mutex_unlock(&video->stream_lock);
Sakari Ailus17d3d402015-12-16 11:32:30 -02001165
Sakari Ailusda392572011-10-10 17:05:24 -03001166 return 0;
1167
Sakari Ailusda392572011-10-10 17:05:24 -03001168err_check_format:
Sakari Ailus20b85222016-11-21 14:48:30 -02001169 media_pipeline_stop(&video->video.entity);
Sakari Ailusda392572011-10-10 17:05:24 -03001170err_pipeline_start:
Laurent Pinchart78c66fb2015-05-20 04:08:30 -03001171 /* TODO: Implement PM QoS */
Sakari Ailusda392572011-10-10 17:05:24 -03001172 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1173 * will get triggered the next time the CCDC is powered up will try to
1174 * access buffers that might have been freed but still present in the
1175 * DMA queue. This can easily get triggered if the above
1176 * omap3isp_pipeline_set_stream() call fails on a system with a
1177 * free-running sensor.
1178 */
1179 INIT_LIST_HEAD(&video->dmaqueue);
1180 video->queue = NULL;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001181
Sakari Ailus17d3d402015-12-16 11:32:30 -02001182 media_entity_enum_cleanup(&pipe->ent_enum);
1183
1184err_enum_init:
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001185 mutex_unlock(&video->stream_lock);
Sakari Ailus17d3d402015-12-16 11:32:30 -02001186
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001187 return ret;
1188}
1189
1190static int
1191isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1192{
1193 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1194 struct isp_video *video = video_drvdata(file);
1195 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1196 enum isp_pipeline_state state;
1197 unsigned int streaming;
1198 unsigned long flags;
1199
1200 if (type != video->type)
1201 return -EINVAL;
1202
1203 mutex_lock(&video->stream_lock);
1204
1205 /* Make sure we're not streaming yet. */
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001206 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001207 streaming = vb2_is_streaming(&vfh->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001208 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001209
1210 if (!streaming)
1211 goto done;
1212
1213 /* Update the pipeline state. */
1214 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1215 state = ISP_PIPELINE_STREAM_OUTPUT
1216 | ISP_PIPELINE_QUEUE_OUTPUT;
1217 else
1218 state = ISP_PIPELINE_STREAM_INPUT
1219 | ISP_PIPELINE_QUEUE_INPUT;
1220
1221 spin_lock_irqsave(&pipe->lock, flags);
1222 pipe->state &= ~state;
1223 spin_unlock_irqrestore(&pipe->lock, flags);
1224
1225 /* Stop the stream. */
1226 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
Laurent Pinchart08344492014-04-20 20:59:03 -03001227 omap3isp_video_cancel_stream(video);
1228
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001229 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001230 vb2_streamoff(&vfh->queue, type);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001231 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001232 video->queue = NULL;
Laurent Pinchart661112c2013-12-09 11:36:51 -03001233 video->error = false;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001234
Laurent Pinchart78c66fb2015-05-20 04:08:30 -03001235 /* TODO: Implement PM QoS */
Sakari Ailus20b85222016-11-21 14:48:30 -02001236 media_pipeline_stop(&video->video.entity);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001237
Sakari Ailus17d3d402015-12-16 11:32:30 -02001238 media_entity_enum_cleanup(&pipe->ent_enum);
1239
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001240done:
1241 mutex_unlock(&video->stream_lock);
1242 return 0;
1243}
1244
1245static int
1246isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1247{
1248 if (input->index > 0)
1249 return -EINVAL;
1250
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001251 strscpy(input->name, "camera", sizeof(input->name));
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001252 input->type = V4L2_INPUT_TYPE_CAMERA;
1253
1254 return 0;
1255}
1256
1257static int
1258isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1259{
1260 *input = 0;
1261
1262 return 0;
1263}
1264
1265static int
1266isp_video_s_input(struct file *file, void *fh, unsigned int input)
1267{
1268 return input == 0 ? 0 : -EINVAL;
1269}
1270
1271static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1272 .vidioc_querycap = isp_video_querycap,
1273 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1274 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1275 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1276 .vidioc_g_fmt_vid_out = isp_video_get_format,
1277 .vidioc_s_fmt_vid_out = isp_video_set_format,
1278 .vidioc_try_fmt_vid_out = isp_video_try_format,
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001279 .vidioc_g_selection = isp_video_get_selection,
1280 .vidioc_s_selection = isp_video_set_selection,
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001281 .vidioc_g_parm = isp_video_get_param,
1282 .vidioc_s_parm = isp_video_set_param,
1283 .vidioc_reqbufs = isp_video_reqbufs,
1284 .vidioc_querybuf = isp_video_querybuf,
1285 .vidioc_qbuf = isp_video_qbuf,
1286 .vidioc_dqbuf = isp_video_dqbuf,
1287 .vidioc_streamon = isp_video_streamon,
1288 .vidioc_streamoff = isp_video_streamoff,
1289 .vidioc_enum_input = isp_video_enum_input,
1290 .vidioc_g_input = isp_video_g_input,
1291 .vidioc_s_input = isp_video_s_input,
1292};
1293
1294/* -----------------------------------------------------------------------------
1295 * V4L2 file operations
1296 */
1297
1298static int isp_video_open(struct file *file)
1299{
1300 struct isp_video *video = video_drvdata(file);
1301 struct isp_video_fh *handle;
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001302 struct vb2_queue *queue;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001303 int ret = 0;
1304
1305 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1306 if (handle == NULL)
1307 return -ENOMEM;
1308
1309 v4l2_fh_init(&handle->vfh, &video->video);
1310 v4l2_fh_add(&handle->vfh);
1311
1312 /* If this is the first user, initialise the pipeline. */
1313 if (omap3isp_get(video->isp) == NULL) {
1314 ret = -EBUSY;
1315 goto done;
1316 }
1317
Sakari Ailus506a47e2016-02-21 13:25:10 -03001318 ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001319 if (ret < 0) {
1320 omap3isp_put(video->isp);
1321 goto done;
1322 }
1323
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001324 queue = &handle->queue;
1325 queue->type = video->type;
1326 queue->io_modes = VB2_MMAP | VB2_USERPTR;
1327 queue->drv_priv = handle;
1328 queue->ops = &isp_video_queue_ops;
1329 queue->mem_ops = &vb2_dma_contig_memops;
1330 queue->buf_struct_size = sizeof(struct isp_buffer);
1331 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Hans Verkuil1ad70ce2016-02-15 13:41:51 -02001332 queue->dev = video->isp->dev;
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001333
1334 ret = vb2_queue_init(&handle->queue);
1335 if (ret < 0) {
1336 omap3isp_put(video->isp);
1337 goto done;
1338 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001339
1340 memset(&handle->format, 0, sizeof(handle->format));
1341 handle->format.type = video->type;
1342 handle->timeperframe.denominator = 1;
1343
1344 handle->video = video;
1345 file->private_data = &handle->vfh;
1346
1347done:
1348 if (ret < 0) {
1349 v4l2_fh_del(&handle->vfh);
Shailendra Verma1deb54d2016-11-25 02:44:32 -02001350 v4l2_fh_exit(&handle->vfh);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001351 kfree(handle);
1352 }
1353
1354 return ret;
1355}
1356
1357static int isp_video_release(struct file *file)
1358{
1359 struct isp_video *video = video_drvdata(file);
1360 struct v4l2_fh *vfh = file->private_data;
1361 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1362
1363 /* Disable streaming and free the buffers queue resources. */
1364 isp_video_streamoff(file, vfh, video->type);
1365
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001366 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001367 vb2_queue_release(&handle->queue);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001368 mutex_unlock(&video->queue_lock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001369
Sakari Ailus506a47e2016-02-21 13:25:10 -03001370 v4l2_pipeline_pm_use(&video->video.entity, 0);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001371
1372 /* Release the file handle. */
1373 v4l2_fh_del(vfh);
Shailendra Verma1deb54d2016-11-25 02:44:32 -02001374 v4l2_fh_exit(vfh);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001375 kfree(handle);
1376 file->private_data = NULL;
1377
1378 omap3isp_put(video->isp);
1379
1380 return 0;
1381}
1382
Al Viroc23e0cb2017-07-03 03:02:56 -04001383static __poll_t isp_video_poll(struct file *file, poll_table *wait)
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001384{
1385 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001386 struct isp_video *video = video_drvdata(file);
Al Viroc23e0cb2017-07-03 03:02:56 -04001387 __poll_t ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001388
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001389 mutex_lock(&video->queue_lock);
Laurent Pinchartfbac1402014-03-09 20:36:15 -03001390 ret = vb2_poll(&vfh->queue, file, wait);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001391 mutex_unlock(&video->queue_lock);
1392
1393 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001394}
1395
1396static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1397{
1398 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1399
Laurent Pinchart9be293692015-02-24 07:39:49 -03001400 return vb2_mmap(&vfh->queue, vma);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001401}
1402
Bhumika Goyalcdc9c742017-08-26 02:12:07 -04001403static const struct v4l2_file_operations isp_video_fops = {
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001404 .owner = THIS_MODULE,
1405 .unlocked_ioctl = video_ioctl2,
1406 .open = isp_video_open,
1407 .release = isp_video_release,
1408 .poll = isp_video_poll,
1409 .mmap = isp_video_mmap,
1410};
1411
1412/* -----------------------------------------------------------------------------
1413 * ISP video core
1414 */
1415
1416static const struct isp_video_operations isp_video_dummy_ops = {
1417};
1418
1419int omap3isp_video_init(struct isp_video *video, const char *name)
1420{
1421 const char *direction;
1422 int ret;
1423
1424 switch (video->type) {
1425 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1426 direction = "output";
Sakari Ailus8dad9362013-10-02 20:17:52 -03001427 video->pad.flags = MEDIA_PAD_FL_SINK
1428 | MEDIA_PAD_FL_MUST_CONNECT;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001429 break;
1430 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1431 direction = "input";
Sakari Ailus8dad9362013-10-02 20:17:52 -03001432 video->pad.flags = MEDIA_PAD_FL_SOURCE
1433 | MEDIA_PAD_FL_MUST_CONNECT;
Hans Verkuil954f3402012-09-05 06:05:50 -03001434 video->video.vfl_dir = VFL_DIR_TX;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001435 break;
1436
1437 default:
1438 return -EINVAL;
1439 }
1440
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001441 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
Hans Verkuil1ad70ce2016-02-15 13:41:51 -02001442 if (ret < 0)
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001443 return ret;
1444
1445 mutex_init(&video->mutex);
1446 atomic_set(&video->active, 0);
1447
1448 spin_lock_init(&video->pipe.lock);
1449 mutex_init(&video->stream_lock);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001450 mutex_init(&video->queue_lock);
Laurent Pincharte8feb872014-03-09 20:57:53 -03001451 spin_lock_init(&video->irqlock);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001452
1453 /* Initialize the video device. */
1454 if (video->ops == NULL)
1455 video->ops = &isp_video_dummy_ops;
1456
1457 video->video.fops = &isp_video_fops;
1458 snprintf(video->video.name, sizeof(video->video.name),
1459 "OMAP3 ISP %s %s", name, direction);
1460 video->video.vfl_type = VFL_TYPE_GRABBER;
1461 video->video.release = video_device_release_empty;
1462 video->video.ioctl_ops = &isp_video_ioctl_ops;
1463 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1464
1465 video_set_drvdata(&video->video, video);
1466
1467 return 0;
1468}
1469
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001470void omap3isp_video_cleanup(struct isp_video *video)
1471{
1472 media_entity_cleanup(&video->video.entity);
Laurent Pinchart988d54c2014-03-09 20:57:53 -03001473 mutex_destroy(&video->queue_lock);
Laurent Pincharted33ac82011-09-22 17:09:26 -03001474 mutex_destroy(&video->stream_lock);
1475 mutex_destroy(&video->mutex);
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001476}
1477
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001478int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1479{
1480 int ret;
1481
1482 video->video.v4l2_dev = vdev;
1483
1484 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1485 if (ret < 0)
Laurent Pinchart4feca392012-10-22 08:28:51 -03001486 dev_err(video->isp->dev,
1487 "%s: could not register video device (%d)\n",
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001488 __func__, ret);
1489
1490 return ret;
1491}
1492
1493void omap3isp_video_unregister(struct isp_video *video)
1494{
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001495 if (video_is_registered(&video->video))
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001496 video_unregister_device(&video->video);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001497}