Commit | Line | Data |
---|---|---|
fc96d58c SA |
1 | /* |
2 | * TI OMAP4 ISS V4L2 Driver - Generic video node | |
3 | * | |
4 | * Copyright (C) 2012 Texas Instruments, Inc. | |
5 | * | |
6 | * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | */ | |
13 | ||
14 | #include <asm/cacheflush.h> | |
15 | #include <linux/clk.h> | |
16 | #include <linux/mm.h> | |
17 | #include <linux/pagemap.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/vmalloc.h> | |
21 | #include <linux/module.h> | |
22 | #include <media/v4l2-dev.h> | |
23 | #include <media/v4l2-ioctl.h> | |
24 | ||
25 | #include "iss_video.h" | |
26 | #include "iss.h" | |
27 | ||
fc96d58c SA |
28 | /* ----------------------------------------------------------------------------- |
29 | * Helper functions | |
30 | */ | |
31 | ||
32 | static struct iss_format_info formats[] = { | |
3336f07a BB |
33 | { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8, |
34 | MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8, | |
a1d4eab0 | 35 | V4L2_PIX_FMT_GREY, 8, "Greyscale 8 bpp", }, |
3336f07a BB |
36 | { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10, |
37 | MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8, | |
a1d4eab0 | 38 | V4L2_PIX_FMT_Y10, 10, "Greyscale 10 bpp", }, |
3336f07a BB |
39 | { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10, |
40 | MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8, | |
a1d4eab0 | 41 | V4L2_PIX_FMT_Y12, 12, "Greyscale 12 bpp", }, |
3336f07a BB |
42 | { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8, |
43 | MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8, | |
a1d4eab0 | 44 | V4L2_PIX_FMT_SBGGR8, 8, "BGGR Bayer 8 bpp", }, |
3336f07a BB |
45 | { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8, |
46 | MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8, | |
a1d4eab0 | 47 | V4L2_PIX_FMT_SGBRG8, 8, "GBRG Bayer 8 bpp", }, |
3336f07a BB |
48 | { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8, |
49 | MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8, | |
a1d4eab0 | 50 | V4L2_PIX_FMT_SGRBG8, 8, "GRBG Bayer 8 bpp", }, |
3336f07a BB |
51 | { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8, |
52 | MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8, | |
a1d4eab0 | 53 | V4L2_PIX_FMT_SRGGB8, 8, "RGGB Bayer 8 bpp", }, |
3336f07a BB |
54 | { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, |
55 | MEDIA_BUS_FMT_SGRBG10_1X10, 0, | |
a1d4eab0 | 56 | V4L2_PIX_FMT_SGRBG10DPCM8, 8, "GRBG Bayer 10 bpp DPCM8", }, |
3336f07a BB |
57 | { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, |
58 | MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8, | |
a1d4eab0 | 59 | V4L2_PIX_FMT_SBGGR10, 10, "BGGR Bayer 10 bpp", }, |
3336f07a BB |
60 | { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10, |
61 | MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8, | |
a1d4eab0 | 62 | V4L2_PIX_FMT_SGBRG10, 10, "GBRG Bayer 10 bpp", }, |
3336f07a BB |
63 | { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, |
64 | MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8, | |
a1d4eab0 | 65 | V4L2_PIX_FMT_SGRBG10, 10, "GRBG Bayer 10 bpp", }, |
3336f07a BB |
66 | { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10, |
67 | MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8, | |
a1d4eab0 | 68 | V4L2_PIX_FMT_SRGGB10, 10, "RGGB Bayer 10 bpp", }, |
3336f07a BB |
69 | { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10, |
70 | MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8, | |
a1d4eab0 | 71 | V4L2_PIX_FMT_SBGGR12, 12, "BGGR Bayer 12 bpp", }, |
3336f07a BB |
72 | { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10, |
73 | MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8, | |
a1d4eab0 | 74 | V4L2_PIX_FMT_SGBRG12, 12, "GBRG Bayer 12 bpp", }, |
3336f07a BB |
75 | { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10, |
76 | MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8, | |
a1d4eab0 | 77 | V4L2_PIX_FMT_SGRBG12, 12, "GRBG Bayer 12 bpp", }, |
3336f07a BB |
78 | { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10, |
79 | MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8, | |
a1d4eab0 | 80 | V4L2_PIX_FMT_SRGGB12, 12, "RGGB Bayer 12 bpp", }, |
3336f07a BB |
81 | { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16, |
82 | MEDIA_BUS_FMT_UYVY8_1X16, 0, | |
a1d4eab0 | 83 | V4L2_PIX_FMT_UYVY, 16, "YUV 4:2:2 (UYVY)", }, |
3336f07a BB |
84 | { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16, |
85 | MEDIA_BUS_FMT_YUYV8_1X16, 0, | |
a1d4eab0 | 86 | V4L2_PIX_FMT_YUYV, 16, "YUV 4:2:2 (YUYV)", }, |
3336f07a BB |
87 | { MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8, |
88 | MEDIA_BUS_FMT_YUYV8_1_5X8, 0, | |
a1d4eab0 | 89 | V4L2_PIX_FMT_NV12, 8, "YUV 4:2:0 (NV12)", }, |
fc96d58c SA |
90 | }; |
91 | ||
92 | const struct iss_format_info * | |
3336f07a | 93 | omap4iss_video_format_info(u32 code) |
fc96d58c SA |
94 | { |
95 | unsigned int i; | |
96 | ||
97 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { | |
98 | if (formats[i].code == code) | |
99 | return &formats[i]; | |
100 | } | |
101 | ||
102 | return NULL; | |
103 | } | |
104 | ||
105 | /* | |
106 | * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format | |
107 | * @video: ISS video instance | |
108 | * @mbus: v4l2_mbus_framefmt format (input) | |
109 | * @pix: v4l2_pix_format format (output) | |
110 | * | |
111 | * Fill the output pix structure with information from the input mbus format. | |
112 | * The bytesperline and sizeimage fields are computed from the requested bytes | |
113 | * per line value in the pix format and information from the video instance. | |
114 | * | |
115 | * Return the number of padding bytes at end of line. | |
116 | */ | |
117 | static unsigned int iss_video_mbus_to_pix(const struct iss_video *video, | |
118 | const struct v4l2_mbus_framefmt *mbus, | |
119 | struct v4l2_pix_format *pix) | |
120 | { | |
121 | unsigned int bpl = pix->bytesperline; | |
122 | unsigned int min_bpl; | |
123 | unsigned int i; | |
124 | ||
125 | memset(pix, 0, sizeof(*pix)); | |
126 | pix->width = mbus->width; | |
127 | pix->height = mbus->height; | |
128 | ||
129 | /* Skip the last format in the loop so that it will be selected if no | |
130 | * match is found. | |
131 | */ | |
132 | for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) { | |
133 | if (formats[i].code == mbus->code) | |
134 | break; | |
135 | } | |
136 | ||
137 | min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8; | |
138 | ||
139 | /* Clamp the requested bytes per line value. If the maximum bytes per | |
140 | * line value is zero, the module doesn't support user configurable line | |
141 | * sizes. Override the requested value with the minimum in that case. | |
142 | */ | |
143 | if (video->bpl_max) | |
144 | bpl = clamp(bpl, min_bpl, video->bpl_max); | |
145 | else | |
146 | bpl = min_bpl; | |
147 | ||
148 | if (!video->bpl_zero_padding || bpl != min_bpl) | |
149 | bpl = ALIGN(bpl, video->bpl_alignment); | |
150 | ||
151 | pix->pixelformat = formats[i].pixelformat; | |
152 | pix->bytesperline = bpl; | |
153 | pix->sizeimage = pix->bytesperline * pix->height; | |
154 | pix->colorspace = mbus->colorspace; | |
155 | pix->field = mbus->field; | |
156 | ||
157 | /* FIXME: Special case for NV12! We should make this nicer... */ | |
158 | if (pix->pixelformat == V4L2_PIX_FMT_NV12) | |
159 | pix->sizeimage += (pix->bytesperline * pix->height) / 2; | |
160 | ||
161 | return bpl - min_bpl; | |
162 | } | |
163 | ||
164 | static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix, | |
165 | struct v4l2_mbus_framefmt *mbus) | |
166 | { | |
167 | unsigned int i; | |
168 | ||
169 | memset(mbus, 0, sizeof(*mbus)); | |
170 | mbus->width = pix->width; | |
171 | mbus->height = pix->height; | |
172 | ||
ac0eb480 LP |
173 | /* Skip the last format in the loop so that it will be selected if no |
174 | * match is found. | |
175 | */ | |
176 | for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) { | |
fc96d58c SA |
177 | if (formats[i].pixelformat == pix->pixelformat) |
178 | break; | |
179 | } | |
180 | ||
fc96d58c SA |
181 | mbus->code = formats[i].code; |
182 | mbus->colorspace = pix->colorspace; | |
183 | mbus->field = pix->field; | |
184 | } | |
185 | ||
186 | static struct v4l2_subdev * | |
187 | iss_video_remote_subdev(struct iss_video *video, u32 *pad) | |
188 | { | |
189 | struct media_pad *remote; | |
190 | ||
191 | remote = media_entity_remote_pad(&video->pad); | |
192 | ||
9058fc92 | 193 | if (!remote || |
fc96d58c SA |
194 | media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
195 | return NULL; | |
196 | ||
197 | if (pad) | |
198 | *pad = remote->index; | |
199 | ||
200 | return media_entity_to_v4l2_subdev(remote->entity); | |
201 | } | |
202 | ||
203 | /* Return a pointer to the ISS video instance at the far end of the pipeline. */ | |
204 | static struct iss_video * | |
205 | iss_video_far_end(struct iss_video *video) | |
206 | { | |
207 | struct media_entity_graph graph; | |
208 | struct media_entity *entity = &video->video.entity; | |
d10c9894 | 209 | struct media_device *mdev = entity->graph_obj.mdev; |
fc96d58c SA |
210 | struct iss_video *far_end = NULL; |
211 | ||
212 | mutex_lock(&mdev->graph_mutex); | |
213 | media_entity_graph_walk_start(&graph, entity); | |
214 | ||
215 | while ((entity = media_entity_graph_walk_next(&graph))) { | |
216 | if (entity == &video->video.entity) | |
217 | continue; | |
218 | ||
219 | if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE) | |
220 | continue; | |
221 | ||
222 | far_end = to_iss_video(media_entity_to_video_device(entity)); | |
223 | if (far_end->type != video->type) | |
224 | break; | |
225 | ||
226 | far_end = NULL; | |
227 | } | |
228 | ||
229 | mutex_unlock(&mdev->graph_mutex); | |
230 | return far_end; | |
231 | } | |
232 | ||
233 | static int | |
cc3c2ac2 LP |
234 | __iss_video_get_format(struct iss_video *video, |
235 | struct v4l2_mbus_framefmt *format) | |
fc96d58c SA |
236 | { |
237 | struct v4l2_subdev_format fmt; | |
238 | struct v4l2_subdev *subdev; | |
239 | u32 pad; | |
240 | int ret; | |
241 | ||
242 | subdev = iss_video_remote_subdev(video, &pad); | |
9058fc92 | 243 | if (!subdev) |
fc96d58c SA |
244 | return -EINVAL; |
245 | ||
cc3c2ac2 | 246 | memset(&fmt, 0, sizeof(fmt)); |
fc96d58c SA |
247 | fmt.pad = pad; |
248 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
fc96d58c | 249 | |
c3dbc70d LP |
250 | mutex_lock(&video->mutex); |
251 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); | |
fc96d58c SA |
252 | mutex_unlock(&video->mutex); |
253 | ||
254 | if (ret) | |
255 | return ret; | |
256 | ||
cc3c2ac2 LP |
257 | *format = fmt.format; |
258 | return 0; | |
fc96d58c SA |
259 | } |
260 | ||
261 | static int | |
262 | iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh) | |
263 | { | |
cc3c2ac2 LP |
264 | struct v4l2_mbus_framefmt format; |
265 | struct v4l2_pix_format pixfmt; | |
fc96d58c SA |
266 | int ret; |
267 | ||
fc96d58c SA |
268 | ret = __iss_video_get_format(video, &format); |
269 | if (ret < 0) | |
270 | return ret; | |
271 | ||
cc3c2ac2 LP |
272 | pixfmt.bytesperline = 0; |
273 | ret = iss_video_mbus_to_pix(video, &format, &pixfmt); | |
274 | ||
275 | if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat || | |
276 | vfh->format.fmt.pix.height != pixfmt.height || | |
277 | vfh->format.fmt.pix.width != pixfmt.width || | |
278 | vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline || | |
279 | vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage) | |
fc96d58c SA |
280 | return -EINVAL; |
281 | ||
282 | return ret; | |
283 | } | |
284 | ||
285 | /* ----------------------------------------------------------------------------- | |
286 | * Video queue operations | |
287 | */ | |
288 | ||
a0fe029c | 289 | static int iss_video_queue_setup(struct vb2_queue *vq, |
fc96d58c SA |
290 | unsigned int *count, unsigned int *num_planes, |
291 | unsigned int sizes[], void *alloc_ctxs[]) | |
292 | { | |
293 | struct iss_video_fh *vfh = vb2_get_drv_priv(vq); | |
294 | struct iss_video *video = vfh->video; | |
295 | ||
296 | /* Revisit multi-planar support for NV12 */ | |
297 | *num_planes = 1; | |
298 | ||
299 | sizes[0] = vfh->format.fmt.pix.sizeimage; | |
300 | if (sizes[0] == 0) | |
301 | return -EINVAL; | |
302 | ||
303 | alloc_ctxs[0] = video->alloc_ctx; | |
304 | ||
a0fe029c | 305 | *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0])); |
fc96d58c SA |
306 | |
307 | return 0; | |
308 | } | |
309 | ||
310 | static void iss_video_buf_cleanup(struct vb2_buffer *vb) | |
311 | { | |
2d700715 JS |
312 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
313 | struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb); | |
fc96d58c SA |
314 | |
315 | if (buffer->iss_addr) | |
316 | buffer->iss_addr = 0; | |
317 | } | |
318 | ||
319 | static int iss_video_buf_prepare(struct vb2_buffer *vb) | |
320 | { | |
2d700715 | 321 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
fc96d58c | 322 | struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue); |
2d700715 | 323 | struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb); |
fc96d58c SA |
324 | struct iss_video *video = vfh->video; |
325 | unsigned long size = vfh->format.fmt.pix.sizeimage; | |
326 | dma_addr_t addr; | |
327 | ||
328 | if (vb2_plane_size(vb, 0) < size) | |
329 | return -ENOBUFS; | |
330 | ||
331 | addr = vb2_dma_contig_plane_dma_addr(vb, 0); | |
332 | if (!IS_ALIGNED(addr, 32)) { | |
499226fb LP |
333 | dev_dbg(video->iss->dev, |
334 | "Buffer address must be aligned to 32 bytes boundary.\n"); | |
fc96d58c SA |
335 | return -EINVAL; |
336 | } | |
337 | ||
338 | vb2_set_plane_payload(vb, 0, size); | |
339 | buffer->iss_addr = addr; | |
340 | return 0; | |
341 | } | |
342 | ||
343 | static void iss_video_buf_queue(struct vb2_buffer *vb) | |
344 | { | |
2d700715 | 345 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
fc96d58c SA |
346 | struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue); |
347 | struct iss_video *video = vfh->video; | |
2d700715 | 348 | struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb); |
fc96d58c | 349 | struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity); |
fc96d58c | 350 | unsigned long flags; |
112da085 | 351 | bool empty; |
fc96d58c SA |
352 | |
353 | spin_lock_irqsave(&video->qlock, flags); | |
112da085 | 354 | |
cb766937 LP |
355 | /* Mark the buffer is faulty and give it back to the queue immediately |
356 | * if the video node has registered an error. vb2 will perform the same | |
357 | * check when preparing the buffer, but that is inherently racy, so we | |
358 | * need to handle the race condition with an authoritative check here. | |
359 | */ | |
112da085 LP |
360 | if (unlikely(video->error)) { |
361 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); | |
362 | spin_unlock_irqrestore(&video->qlock, flags); | |
363 | return; | |
364 | } | |
365 | ||
fc96d58c SA |
366 | empty = list_empty(&video->dmaqueue); |
367 | list_add_tail(&buffer->list, &video->dmaqueue); | |
112da085 | 368 | |
fc96d58c SA |
369 | spin_unlock_irqrestore(&video->qlock, flags); |
370 | ||
371 | if (empty) { | |
372 | enum iss_pipeline_state state; | |
373 | unsigned int start; | |
374 | ||
375 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
376 | state = ISS_PIPELINE_QUEUE_OUTPUT; | |
377 | else | |
378 | state = ISS_PIPELINE_QUEUE_INPUT; | |
379 | ||
380 | spin_lock_irqsave(&pipe->lock, flags); | |
381 | pipe->state |= state; | |
382 | video->ops->queue(video, buffer); | |
383 | video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED; | |
384 | ||
385 | start = iss_pipeline_ready(pipe); | |
386 | if (start) | |
387 | pipe->state |= ISS_PIPELINE_STREAM; | |
388 | spin_unlock_irqrestore(&pipe->lock, flags); | |
389 | ||
390 | if (start) | |
391 | omap4iss_pipeline_set_stream(pipe, | |
392 | ISS_PIPELINE_STREAM_SINGLESHOT); | |
393 | } | |
394 | } | |
395 | ||
fbac1400 | 396 | static const struct vb2_ops iss_video_vb2ops = { |
fc96d58c SA |
397 | .queue_setup = iss_video_queue_setup, |
398 | .buf_prepare = iss_video_buf_prepare, | |
399 | .buf_queue = iss_video_buf_queue, | |
400 | .buf_cleanup = iss_video_buf_cleanup, | |
401 | }; | |
402 | ||
403 | /* | |
404 | * omap4iss_video_buffer_next - Complete the current buffer and return the next | |
405 | * @video: ISS video object | |
406 | * | |
407 | * Remove the current video buffer from the DMA queue and fill its timestamp, | |
408 | * field count and state fields before waking up its completion handler. | |
409 | * | |
410 | * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no | |
411 | * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise. | |
412 | * | |
413 | * The DMA queue is expected to contain at least one buffer. | |
414 | * | |
415 | * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is | |
416 | * empty. | |
417 | */ | |
418 | struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video) | |
419 | { | |
420 | struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity); | |
421 | enum iss_pipeline_state state; | |
422 | struct iss_buffer *buf; | |
423 | unsigned long flags; | |
fc96d58c SA |
424 | |
425 | spin_lock_irqsave(&video->qlock, flags); | |
426 | if (WARN_ON(list_empty(&video->dmaqueue))) { | |
427 | spin_unlock_irqrestore(&video->qlock, flags); | |
428 | return NULL; | |
429 | } | |
430 | ||
431 | buf = list_first_entry(&video->dmaqueue, struct iss_buffer, | |
432 | list); | |
433 | list_del(&buf->list); | |
434 | spin_unlock_irqrestore(&video->qlock, flags); | |
435 | ||
d6dd645e | 436 | buf->vb.vb2_buf.timestamp = ktime_get_ns(); |
fc96d58c SA |
437 | |
438 | /* Do frame number propagation only if this is the output video node. | |
439 | * Frame number either comes from the CSI receivers or it gets | |
440 | * incremented here if H3A is not active. | |
441 | * Note: There is no guarantee that the output buffer will finish | |
442 | * first, so the input number might lag behind by 1 in some cases. | |
443 | */ | |
444 | if (video == pipe->output && !pipe->do_propagation) | |
2d700715 | 445 | buf->vb.sequence = |
a0fe029c | 446 | atomic_inc_return(&pipe->frame_number); |
fc96d58c | 447 | else |
2d700715 | 448 | buf->vb.sequence = atomic_read(&pipe->frame_number); |
fc96d58c | 449 | |
2d700715 | 450 | vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ? |
a0fe029c | 451 | VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); |
fc96d58c SA |
452 | pipe->error = false; |
453 | ||
454 | spin_lock_irqsave(&video->qlock, flags); | |
455 | if (list_empty(&video->dmaqueue)) { | |
456 | spin_unlock_irqrestore(&video->qlock, flags); | |
457 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
458 | state = ISS_PIPELINE_QUEUE_OUTPUT | |
459 | | ISS_PIPELINE_STREAM; | |
460 | else | |
461 | state = ISS_PIPELINE_QUEUE_INPUT | |
462 | | ISS_PIPELINE_STREAM; | |
463 | ||
464 | spin_lock_irqsave(&pipe->lock, flags); | |
465 | pipe->state &= ~state; | |
466 | if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS) | |
467 | video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN; | |
468 | spin_unlock_irqrestore(&pipe->lock, flags); | |
469 | return NULL; | |
470 | } | |
471 | ||
9058fc92 | 472 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) { |
f7d40eea | 473 | spin_lock(&pipe->lock); |
fc96d58c | 474 | pipe->state &= ~ISS_PIPELINE_STREAM; |
f7d40eea | 475 | spin_unlock(&pipe->lock); |
fc96d58c SA |
476 | } |
477 | ||
478 | buf = list_first_entry(&video->dmaqueue, struct iss_buffer, | |
479 | list); | |
480 | spin_unlock_irqrestore(&video->qlock, flags); | |
2d700715 | 481 | buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE; |
fc96d58c SA |
482 | return buf; |
483 | } | |
484 | ||
112da085 LP |
485 | /* |
486 | * omap4iss_video_cancel_stream - Cancel stream on a video node | |
487 | * @video: ISS video object | |
488 | * | |
489 | * Cancelling a stream mark all buffers on the video node as erroneous and makes | |
490 | * sure no new buffer can be queued. | |
491 | */ | |
492 | void omap4iss_video_cancel_stream(struct iss_video *video) | |
493 | { | |
494 | unsigned long flags; | |
495 | ||
496 | spin_lock_irqsave(&video->qlock, flags); | |
497 | ||
498 | while (!list_empty(&video->dmaqueue)) { | |
499 | struct iss_buffer *buf; | |
500 | ||
501 | buf = list_first_entry(&video->dmaqueue, struct iss_buffer, | |
502 | list); | |
503 | list_del(&buf->list); | |
2d700715 | 504 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
112da085 LP |
505 | } |
506 | ||
cb766937 | 507 | vb2_queue_error(video->queue); |
112da085 LP |
508 | video->error = true; |
509 | ||
510 | spin_unlock_irqrestore(&video->qlock, flags); | |
511 | } | |
512 | ||
fc96d58c SA |
513 | /* ----------------------------------------------------------------------------- |
514 | * V4L2 ioctls | |
515 | */ | |
516 | ||
517 | static int | |
518 | iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap) | |
519 | { | |
520 | struct iss_video *video = video_drvdata(file); | |
521 | ||
522 | strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver)); | |
523 | strlcpy(cap->card, video->video.name, sizeof(cap->card)); | |
524 | strlcpy(cap->bus_info, "media", sizeof(cap->bus_info)); | |
525 | ||
526 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
35c71be8 | 527 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
fc96d58c | 528 | else |
35c71be8 LP |
529 | cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; |
530 | ||
531 | cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING | |
532 | | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT; | |
fc96d58c SA |
533 | |
534 | return 0; | |
535 | } | |
536 | ||
0b1d4249 LP |
537 | static int |
538 | iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f) | |
539 | { | |
540 | struct iss_video *video = video_drvdata(file); | |
541 | struct v4l2_mbus_framefmt format; | |
542 | unsigned int index = f->index; | |
543 | unsigned int i; | |
544 | int ret; | |
545 | ||
546 | if (f->type != video->type) | |
547 | return -EINVAL; | |
548 | ||
549 | ret = __iss_video_get_format(video, &format); | |
550 | if (ret < 0) | |
551 | return ret; | |
552 | ||
553 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { | |
554 | const struct iss_format_info *info = &formats[i]; | |
555 | ||
556 | if (format.code != info->code) | |
557 | continue; | |
558 | ||
559 | if (index == 0) { | |
560 | f->pixelformat = info->pixelformat; | |
561 | strlcpy(f->description, info->description, | |
562 | sizeof(f->description)); | |
563 | return 0; | |
564 | } | |
565 | ||
566 | index--; | |
567 | } | |
568 | ||
569 | return -EINVAL; | |
570 | } | |
571 | ||
fc96d58c SA |
572 | static int |
573 | iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format) | |
574 | { | |
575 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
576 | struct iss_video *video = video_drvdata(file); | |
577 | ||
578 | if (format->type != video->type) | |
579 | return -EINVAL; | |
580 | ||
581 | mutex_lock(&video->mutex); | |
582 | *format = vfh->format; | |
583 | mutex_unlock(&video->mutex); | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
588 | static int | |
589 | iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format) | |
590 | { | |
591 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
592 | struct iss_video *video = video_drvdata(file); | |
593 | struct v4l2_mbus_framefmt fmt; | |
594 | ||
595 | if (format->type != video->type) | |
596 | return -EINVAL; | |
597 | ||
598 | mutex_lock(&video->mutex); | |
599 | ||
600 | /* Fill the bytesperline and sizeimage fields by converting to media bus | |
601 | * format and back to pixel format. | |
602 | */ | |
603 | iss_video_pix_to_mbus(&format->fmt.pix, &fmt); | |
604 | iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix); | |
605 | ||
606 | vfh->format = *format; | |
607 | ||
608 | mutex_unlock(&video->mutex); | |
609 | return 0; | |
610 | } | |
611 | ||
612 | static int | |
613 | iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format) | |
614 | { | |
615 | struct iss_video *video = video_drvdata(file); | |
616 | struct v4l2_subdev_format fmt; | |
617 | struct v4l2_subdev *subdev; | |
618 | u32 pad; | |
619 | int ret; | |
620 | ||
621 | if (format->type != video->type) | |
622 | return -EINVAL; | |
623 | ||
624 | subdev = iss_video_remote_subdev(video, &pad); | |
9058fc92 | 625 | if (!subdev) |
fc96d58c SA |
626 | return -EINVAL; |
627 | ||
628 | iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format); | |
629 | ||
630 | fmt.pad = pad; | |
631 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
632 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); | |
633 | if (ret) | |
e43484e4 | 634 | return ret; |
fc96d58c SA |
635 | |
636 | iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix); | |
637 | return 0; | |
638 | } | |
639 | ||
fc96d58c SA |
640 | static int |
641 | iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a) | |
642 | { | |
643 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
644 | struct iss_video *video = video_drvdata(file); | |
645 | ||
646 | if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT || | |
647 | video->type != a->type) | |
648 | return -EINVAL; | |
649 | ||
650 | memset(a, 0, sizeof(*a)); | |
651 | a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
652 | a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; | |
653 | a->parm.output.timeperframe = vfh->timeperframe; | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
658 | static int | |
659 | iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a) | |
660 | { | |
661 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
662 | struct iss_video *video = video_drvdata(file); | |
663 | ||
664 | if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT || | |
665 | video->type != a->type) | |
666 | return -EINVAL; | |
667 | ||
668 | if (a->parm.output.timeperframe.denominator == 0) | |
669 | a->parm.output.timeperframe.denominator = 1; | |
670 | ||
671 | vfh->timeperframe = a->parm.output.timeperframe; | |
672 | ||
673 | return 0; | |
674 | } | |
675 | ||
676 | static int | |
677 | iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb) | |
678 | { | |
679 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
680 | ||
681 | return vb2_reqbufs(&vfh->queue, rb); | |
682 | } | |
683 | ||
684 | static int | |
685 | iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b) | |
686 | { | |
687 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
688 | ||
689 | return vb2_querybuf(&vfh->queue, b); | |
690 | } | |
691 | ||
692 | static int | |
693 | iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b) | |
694 | { | |
695 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
696 | ||
697 | return vb2_qbuf(&vfh->queue, b); | |
698 | } | |
699 | ||
3e90f789 LP |
700 | static int |
701 | iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e) | |
702 | { | |
703 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
704 | ||
705 | return vb2_expbuf(&vfh->queue, e); | |
706 | } | |
707 | ||
fc96d58c SA |
708 | static int |
709 | iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b) | |
710 | { | |
711 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
712 | ||
713 | return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK); | |
714 | } | |
715 | ||
716 | /* | |
717 | * Stream management | |
718 | * | |
719 | * Every ISS pipeline has a single input and a single output. The input can be | |
720 | * either a sensor or a video node. The output is always a video node. | |
721 | * | |
722 | * As every pipeline has an output video node, the ISS video objects at the | |
723 | * pipeline output stores the pipeline state. It tracks the streaming state of | |
724 | * both the input and output, as well as the availability of buffers. | |
725 | * | |
726 | * In sensor-to-memory mode, frames are always available at the pipeline input. | |
727 | * Starting the sensor usually requires I2C transfers and must be done in | |
728 | * interruptible context. The pipeline is started and stopped synchronously | |
729 | * to the stream on/off commands. All modules in the pipeline will get their | |
730 | * subdev set stream handler called. The module at the end of the pipeline must | |
731 | * delay starting the hardware until buffers are available at its output. | |
732 | * | |
733 | * In memory-to-memory mode, starting/stopping the stream requires | |
734 | * synchronization between the input and output. ISS modules can't be stopped | |
735 | * in the middle of a frame, and at least some of the modules seem to become | |
736 | * busy as soon as they're started, even if they don't receive a frame start | |
737 | * event. For that reason frames need to be processed in single-shot mode. The | |
738 | * driver needs to wait until a frame is completely processed and written to | |
739 | * memory before restarting the pipeline for the next frame. Pipelined | |
740 | * processing might be possible but requires more testing. | |
741 | * | |
742 | * Stream start must be delayed until buffers are available at both the input | |
743 | * and output. The pipeline must be started in the videobuf queue callback with | |
744 | * the buffers queue spinlock held. The modules subdev set stream operation must | |
745 | * not sleep. | |
746 | */ | |
747 | static int | |
748 | iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type) | |
749 | { | |
750 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
751 | struct iss_video *video = video_drvdata(file); | |
f3632ba8 LP |
752 | struct media_entity_graph graph; |
753 | struct media_entity *entity; | |
fc96d58c SA |
754 | enum iss_pipeline_state state; |
755 | struct iss_pipeline *pipe; | |
756 | struct iss_video *far_end; | |
757 | unsigned long flags; | |
758 | int ret; | |
759 | ||
760 | if (type != video->type) | |
761 | return -EINVAL; | |
762 | ||
763 | mutex_lock(&video->stream_lock); | |
764 | ||
fc96d58c SA |
765 | /* Start streaming on the pipeline. No link touching an entity in the |
766 | * pipeline can be activated or deactivated once streaming is started. | |
767 | */ | |
768 | pipe = video->video.entity.pipe | |
769 | ? to_iss_pipeline(&video->video.entity) : &video->pipe; | |
770 | pipe->external = NULL; | |
771 | pipe->external_rate = 0; | |
772 | pipe->external_bpp = 0; | |
f3632ba8 | 773 | pipe->entities = 0; |
fc96d58c SA |
774 | |
775 | if (video->iss->pdata->set_constraints) | |
776 | video->iss->pdata->set_constraints(video->iss, true); | |
777 | ||
778 | ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe); | |
779 | if (ret < 0) | |
780 | goto err_media_entity_pipeline_start; | |
781 | ||
f3632ba8 LP |
782 | entity = &video->video.entity; |
783 | media_entity_graph_walk_start(&graph, entity); | |
784 | while ((entity = media_entity_graph_walk_next(&graph))) | |
1302d39c | 785 | pipe->entities |= 1 << media_entity_id(entity); |
f3632ba8 | 786 | |
fc96d58c SA |
787 | /* Verify that the currently configured format matches the output of |
788 | * the connected subdev. | |
789 | */ | |
790 | ret = iss_video_check_format(video, vfh); | |
791 | if (ret < 0) | |
792 | goto err_iss_video_check_format; | |
793 | ||
794 | video->bpl_padding = ret; | |
795 | video->bpl_value = vfh->format.fmt.pix.bytesperline; | |
796 | ||
797 | /* Find the ISS video node connected at the far end of the pipeline and | |
798 | * update the pipeline. | |
799 | */ | |
800 | far_end = iss_video_far_end(video); | |
801 | ||
802 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { | |
803 | state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT; | |
804 | pipe->input = far_end; | |
805 | pipe->output = video; | |
806 | } else { | |
9058fc92 | 807 | if (!far_end) { |
fc96d58c SA |
808 | ret = -EPIPE; |
809 | goto err_iss_video_check_format; | |
810 | } | |
811 | ||
812 | state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT; | |
813 | pipe->input = video; | |
814 | pipe->output = far_end; | |
815 | } | |
816 | ||
817 | spin_lock_irqsave(&pipe->lock, flags); | |
818 | pipe->state &= ~ISS_PIPELINE_STREAM; | |
819 | pipe->state |= state; | |
820 | spin_unlock_irqrestore(&pipe->lock, flags); | |
821 | ||
822 | /* Set the maximum time per frame as the value requested by userspace. | |
823 | * This is a soft limit that can be overridden if the hardware doesn't | |
824 | * support the request limit. | |
825 | */ | |
826 | if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) | |
827 | pipe->max_timeperframe = vfh->timeperframe; | |
828 | ||
829 | video->queue = &vfh->queue; | |
830 | INIT_LIST_HEAD(&video->dmaqueue); | |
112da085 | 831 | video->error = false; |
fc96d58c SA |
832 | atomic_set(&pipe->frame_number, -1); |
833 | ||
834 | ret = vb2_streamon(&vfh->queue, type); | |
835 | if (ret < 0) | |
836 | goto err_iss_video_check_format; | |
837 | ||
838 | /* In sensor-to-memory mode, the stream can be started synchronously | |
839 | * to the stream on command. In memory-to-memory mode, it will be | |
840 | * started when buffers are queued on both the input and output. | |
841 | */ | |
9058fc92 | 842 | if (!pipe->input) { |
fc96d58c | 843 | unsigned long flags; |
00ef07e3 | 844 | |
fc96d58c SA |
845 | ret = omap4iss_pipeline_set_stream(pipe, |
846 | ISS_PIPELINE_STREAM_CONTINUOUS); | |
847 | if (ret < 0) | |
848 | goto err_omap4iss_set_stream; | |
849 | spin_lock_irqsave(&video->qlock, flags); | |
850 | if (list_empty(&video->dmaqueue)) | |
851 | video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN; | |
852 | spin_unlock_irqrestore(&video->qlock, flags); | |
853 | } | |
854 | ||
2b16b44a LP |
855 | mutex_unlock(&video->stream_lock); |
856 | return 0; | |
857 | ||
fc96d58c | 858 | err_omap4iss_set_stream: |
2b16b44a | 859 | vb2_streamoff(&vfh->queue, type); |
fc96d58c | 860 | err_iss_video_check_format: |
2b16b44a | 861 | media_entity_pipeline_stop(&video->video.entity); |
fc96d58c | 862 | err_media_entity_pipeline_start: |
2b16b44a LP |
863 | if (video->iss->pdata->set_constraints) |
864 | video->iss->pdata->set_constraints(video->iss, false); | |
865 | video->queue = NULL; | |
fc96d58c | 866 | |
fc96d58c SA |
867 | mutex_unlock(&video->stream_lock); |
868 | return ret; | |
869 | } | |
870 | ||
871 | static int | |
872 | iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) | |
873 | { | |
874 | struct iss_video_fh *vfh = to_iss_video_fh(fh); | |
875 | struct iss_video *video = video_drvdata(file); | |
876 | struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity); | |
877 | enum iss_pipeline_state state; | |
878 | unsigned long flags; | |
879 | ||
880 | if (type != video->type) | |
881 | return -EINVAL; | |
882 | ||
883 | mutex_lock(&video->stream_lock); | |
884 | ||
885 | if (!vb2_is_streaming(&vfh->queue)) | |
886 | goto done; | |
887 | ||
888 | /* Update the pipeline state. */ | |
889 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
890 | state = ISS_PIPELINE_STREAM_OUTPUT | |
891 | | ISS_PIPELINE_QUEUE_OUTPUT; | |
892 | else | |
893 | state = ISS_PIPELINE_STREAM_INPUT | |
894 | | ISS_PIPELINE_QUEUE_INPUT; | |
895 | ||
896 | spin_lock_irqsave(&pipe->lock, flags); | |
897 | pipe->state &= ~state; | |
898 | spin_unlock_irqrestore(&pipe->lock, flags); | |
899 | ||
900 | /* Stop the stream. */ | |
901 | omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED); | |
902 | vb2_streamoff(&vfh->queue, type); | |
903 | video->queue = NULL; | |
fc96d58c SA |
904 | |
905 | if (video->iss->pdata->set_constraints) | |
906 | video->iss->pdata->set_constraints(video->iss, false); | |
907 | media_entity_pipeline_stop(&video->video.entity); | |
908 | ||
909 | done: | |
910 | mutex_unlock(&video->stream_lock); | |
911 | return 0; | |
912 | } | |
913 | ||
914 | static int | |
915 | iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input) | |
916 | { | |
917 | if (input->index > 0) | |
918 | return -EINVAL; | |
919 | ||
920 | strlcpy(input->name, "camera", sizeof(input->name)); | |
921 | input->type = V4L2_INPUT_TYPE_CAMERA; | |
922 | ||
923 | return 0; | |
924 | } | |
925 | ||
926 | static int | |
927 | iss_video_g_input(struct file *file, void *fh, unsigned int *input) | |
928 | { | |
929 | *input = 0; | |
930 | ||
931 | return 0; | |
932 | } | |
933 | ||
16422f55 LP |
934 | static int |
935 | iss_video_s_input(struct file *file, void *fh, unsigned int input) | |
936 | { | |
937 | return input == 0 ? 0 : -EINVAL; | |
938 | } | |
939 | ||
fc96d58c SA |
940 | static const struct v4l2_ioctl_ops iss_video_ioctl_ops = { |
941 | .vidioc_querycap = iss_video_querycap, | |
0b1d4249 | 942 | .vidioc_enum_fmt_vid_cap = iss_video_enum_format, |
fc96d58c SA |
943 | .vidioc_g_fmt_vid_cap = iss_video_get_format, |
944 | .vidioc_s_fmt_vid_cap = iss_video_set_format, | |
945 | .vidioc_try_fmt_vid_cap = iss_video_try_format, | |
946 | .vidioc_g_fmt_vid_out = iss_video_get_format, | |
947 | .vidioc_s_fmt_vid_out = iss_video_set_format, | |
948 | .vidioc_try_fmt_vid_out = iss_video_try_format, | |
fc96d58c SA |
949 | .vidioc_g_parm = iss_video_get_param, |
950 | .vidioc_s_parm = iss_video_set_param, | |
951 | .vidioc_reqbufs = iss_video_reqbufs, | |
952 | .vidioc_querybuf = iss_video_querybuf, | |
953 | .vidioc_qbuf = iss_video_qbuf, | |
3e90f789 | 954 | .vidioc_expbuf = iss_video_expbuf, |
fc96d58c SA |
955 | .vidioc_dqbuf = iss_video_dqbuf, |
956 | .vidioc_streamon = iss_video_streamon, | |
957 | .vidioc_streamoff = iss_video_streamoff, | |
958 | .vidioc_enum_input = iss_video_enum_input, | |
959 | .vidioc_g_input = iss_video_g_input, | |
16422f55 | 960 | .vidioc_s_input = iss_video_s_input, |
fc96d58c SA |
961 | }; |
962 | ||
963 | /* ----------------------------------------------------------------------------- | |
964 | * V4L2 file operations | |
965 | */ | |
966 | ||
967 | static int iss_video_open(struct file *file) | |
968 | { | |
969 | struct iss_video *video = video_drvdata(file); | |
970 | struct iss_video_fh *handle; | |
971 | struct vb2_queue *q; | |
972 | int ret = 0; | |
973 | ||
974 | handle = kzalloc(sizeof(*handle), GFP_KERNEL); | |
9058fc92 | 975 | if (!handle) |
fc96d58c SA |
976 | return -ENOMEM; |
977 | ||
978 | v4l2_fh_init(&handle->vfh, &video->video); | |
979 | v4l2_fh_add(&handle->vfh); | |
980 | ||
981 | /* If this is the first user, initialise the pipeline. */ | |
9058fc92 | 982 | if (!omap4iss_get(video->iss)) { |
fc96d58c SA |
983 | ret = -EBUSY; |
984 | goto done; | |
985 | } | |
986 | ||
987 | ret = omap4iss_pipeline_pm_use(&video->video.entity, 1); | |
988 | if (ret < 0) { | |
989 | omap4iss_put(video->iss); | |
990 | goto done; | |
991 | } | |
992 | ||
993 | video->alloc_ctx = vb2_dma_contig_init_ctx(video->iss->dev); | |
994 | if (IS_ERR(video->alloc_ctx)) { | |
995 | ret = PTR_ERR(video->alloc_ctx); | |
996 | omap4iss_put(video->iss); | |
997 | goto done; | |
998 | } | |
999 | ||
1000 | q = &handle->queue; | |
1001 | ||
1002 | q->type = video->type; | |
3e90f789 | 1003 | q->io_modes = VB2_MMAP | VB2_DMABUF; |
fc96d58c SA |
1004 | q->drv_priv = handle; |
1005 | q->ops = &iss_video_vb2ops; | |
1006 | q->mem_ops = &vb2_dma_contig_memops; | |
1007 | q->buf_struct_size = sizeof(struct iss_buffer); | |
ade48681 | 1008 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
fc96d58c SA |
1009 | |
1010 | ret = vb2_queue_init(q); | |
1011 | if (ret) { | |
1012 | omap4iss_put(video->iss); | |
1013 | goto done; | |
1014 | } | |
1015 | ||
1016 | memset(&handle->format, 0, sizeof(handle->format)); | |
1017 | handle->format.type = video->type; | |
1018 | handle->timeperframe.denominator = 1; | |
1019 | ||
1020 | handle->video = video; | |
1021 | file->private_data = &handle->vfh; | |
1022 | ||
1023 | done: | |
1024 | if (ret < 0) { | |
1025 | v4l2_fh_del(&handle->vfh); | |
1026 | kfree(handle); | |
1027 | } | |
1028 | ||
1029 | return ret; | |
1030 | } | |
1031 | ||
1032 | static int iss_video_release(struct file *file) | |
1033 | { | |
1034 | struct iss_video *video = video_drvdata(file); | |
1035 | struct v4l2_fh *vfh = file->private_data; | |
1036 | struct iss_video_fh *handle = to_iss_video_fh(vfh); | |
1037 | ||
1038 | /* Disable streaming and free the buffers queue resources. */ | |
1039 | iss_video_streamoff(file, vfh, video->type); | |
1040 | ||
1041 | omap4iss_pipeline_pm_use(&video->video.entity, 0); | |
1042 | ||
1043 | /* Release the videobuf2 queue */ | |
1044 | vb2_queue_release(&handle->queue); | |
1045 | ||
1046 | /* Release the file handle. */ | |
1047 | v4l2_fh_del(vfh); | |
1048 | kfree(handle); | |
1049 | file->private_data = NULL; | |
1050 | ||
1051 | omap4iss_put(video->iss); | |
1052 | ||
1053 | return 0; | |
1054 | } | |
1055 | ||
1056 | static unsigned int iss_video_poll(struct file *file, poll_table *wait) | |
1057 | { | |
1058 | struct iss_video_fh *vfh = to_iss_video_fh(file->private_data); | |
1059 | ||
1060 | return vb2_poll(&vfh->queue, file, wait); | |
1061 | } | |
1062 | ||
1063 | static int iss_video_mmap(struct file *file, struct vm_area_struct *vma) | |
1064 | { | |
1065 | struct iss_video_fh *vfh = to_iss_video_fh(file->private_data); | |
1066 | ||
29ee6261 | 1067 | return vb2_mmap(&vfh->queue, vma); |
fc96d58c SA |
1068 | } |
1069 | ||
1070 | static struct v4l2_file_operations iss_video_fops = { | |
1071 | .owner = THIS_MODULE, | |
1072 | .unlocked_ioctl = video_ioctl2, | |
1073 | .open = iss_video_open, | |
1074 | .release = iss_video_release, | |
1075 | .poll = iss_video_poll, | |
1076 | .mmap = iss_video_mmap, | |
1077 | }; | |
1078 | ||
1079 | /* ----------------------------------------------------------------------------- | |
1080 | * ISS video core | |
1081 | */ | |
1082 | ||
1083 | static const struct iss_video_operations iss_video_dummy_ops = { | |
1084 | }; | |
1085 | ||
1086 | int omap4iss_video_init(struct iss_video *video, const char *name) | |
1087 | { | |
1088 | const char *direction; | |
1089 | int ret; | |
1090 | ||
1091 | switch (video->type) { | |
1092 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: | |
1093 | direction = "output"; | |
1094 | video->pad.flags = MEDIA_PAD_FL_SINK; | |
1095 | break; | |
1096 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: | |
1097 | direction = "input"; | |
1098 | video->pad.flags = MEDIA_PAD_FL_SOURCE; | |
1099 | break; | |
1100 | ||
1101 | default: | |
1102 | return -EINVAL; | |
1103 | } | |
1104 | ||
18095107 | 1105 | ret = media_entity_init(&video->video.entity, 1, &video->pad); |
fc96d58c SA |
1106 | if (ret < 0) |
1107 | return ret; | |
1108 | ||
2b44ac9e | 1109 | spin_lock_init(&video->qlock); |
fc96d58c SA |
1110 | mutex_init(&video->mutex); |
1111 | atomic_set(&video->active, 0); | |
1112 | ||
1113 | spin_lock_init(&video->pipe.lock); | |
1114 | mutex_init(&video->stream_lock); | |
1115 | ||
1116 | /* Initialize the video device. */ | |
9058fc92 | 1117 | if (!video->ops) |
fc96d58c SA |
1118 | video->ops = &iss_video_dummy_ops; |
1119 | ||
1120 | video->video.fops = &iss_video_fops; | |
1121 | snprintf(video->video.name, sizeof(video->video.name), | |
1122 | "OMAP4 ISS %s %s", name, direction); | |
1123 | video->video.vfl_type = VFL_TYPE_GRABBER; | |
1124 | video->video.release = video_device_release_empty; | |
1125 | video->video.ioctl_ops = &iss_video_ioctl_ops; | |
1126 | video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED; | |
1127 | ||
1128 | video_set_drvdata(&video->video, video); | |
1129 | ||
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | void omap4iss_video_cleanup(struct iss_video *video) | |
1134 | { | |
1135 | media_entity_cleanup(&video->video.entity); | |
1136 | mutex_destroy(&video->stream_lock); | |
1137 | mutex_destroy(&video->mutex); | |
1138 | } | |
1139 | ||
1140 | int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev) | |
1141 | { | |
1142 | int ret; | |
1143 | ||
1144 | video->video.v4l2_dev = vdev; | |
1145 | ||
1146 | ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1); | |
1147 | if (ret < 0) | |
4cd89e91 | 1148 | dev_err(video->iss->dev, |
8c6ccbeb | 1149 | "could not register video device (%d)\n", ret); |
fc96d58c SA |
1150 | |
1151 | return ret; | |
1152 | } | |
1153 | ||
1154 | void omap4iss_video_unregister(struct iss_video *video) | |
1155 | { | |
2b7f0b64 | 1156 | video_unregister_device(&video->video); |
fc96d58c | 1157 | } |