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