4
* TI OMAP3 ISP - Generic video node
6
* Copyright (C) 2009-2010 Nokia Corporation
8
* Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9
* Sakari Ailus <sakari.ailus@iki.fi>
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.
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.
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
26
#include <asm/cacheflush.h>
27
#include <linux/clk.h>
29
#include <linux/pagemap.h>
30
#include <linux/scatterlist.h>
31
#include <linux/sched.h>
32
#include <linux/slab.h>
33
#include <linux/vmalloc.h>
34
#include <media/v4l2-dev.h>
35
#include <media/v4l2-ioctl.h>
36
#include <plat/iommu.h>
37
#include <plat/iovmm.h>
38
#include <plat/omap-pm.h>
44
/* -----------------------------------------------------------------------------
48
static struct isp_format_info formats[] = {
49
{ V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
50
V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
51
V4L2_PIX_FMT_GREY, 8, },
52
{ V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
53
V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
54
V4L2_PIX_FMT_Y10, 10, },
55
{ V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
56
V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
57
V4L2_PIX_FMT_Y12, 12, },
58
{ V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
59
V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
60
V4L2_PIX_FMT_SBGGR8, 8, },
61
{ V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
62
V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
63
V4L2_PIX_FMT_SGBRG8, 8, },
64
{ V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
65
V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
66
V4L2_PIX_FMT_SGRBG8, 8, },
67
{ V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
68
V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
69
V4L2_PIX_FMT_SRGGB8, 8, },
70
{ V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
71
V4L2_MBUS_FMT_SGRBG10_1X10, 0,
72
V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
73
{ V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
74
V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
75
V4L2_PIX_FMT_SBGGR10, 10, },
76
{ V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
77
V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
78
V4L2_PIX_FMT_SGBRG10, 10, },
79
{ V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
80
V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
81
V4L2_PIX_FMT_SGRBG10, 10, },
82
{ V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
83
V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
84
V4L2_PIX_FMT_SRGGB10, 10, },
85
{ V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
86
V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
87
V4L2_PIX_FMT_SBGGR12, 12, },
88
{ V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
89
V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
90
V4L2_PIX_FMT_SGBRG12, 12, },
91
{ V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
92
V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
93
V4L2_PIX_FMT_SGRBG12, 12, },
94
{ V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
95
V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
96
V4L2_PIX_FMT_SRGGB12, 12, },
97
{ V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
98
V4L2_MBUS_FMT_UYVY8_1X16, 0,
99
V4L2_PIX_FMT_UYVY, 16, },
100
{ V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
101
V4L2_MBUS_FMT_YUYV8_1X16, 0,
102
V4L2_PIX_FMT_YUYV, 16, },
105
const struct isp_format_info *
106
omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
110
for (i = 0; i < ARRAY_SIZE(formats); ++i) {
111
if (formats[i].code == code)
119
* Decide whether desired output pixel code can be obtained with
120
* the lane shifter by shifting the input pixel code.
121
* @in: input pixelcode to shifter
122
* @out: output pixelcode from shifter
123
* @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
125
* return true if the combination is possible
126
* return false otherwise
128
static bool isp_video_is_shiftable(enum v4l2_mbus_pixelcode in,
129
enum v4l2_mbus_pixelcode out,
130
unsigned int additional_shift)
132
const struct isp_format_info *in_info, *out_info;
137
in_info = omap3isp_video_format_info(in);
138
out_info = omap3isp_video_format_info(out);
140
if ((in_info->flavor == 0) || (out_info->flavor == 0))
143
if (in_info->flavor != out_info->flavor)
146
return in_info->bpp - out_info->bpp + additional_shift <= 6;
150
* isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
151
* @video: ISP video instance
152
* @mbus: v4l2_mbus_framefmt format (input)
153
* @pix: v4l2_pix_format format (output)
155
* Fill the output pix structure with information from the input mbus format.
156
* The bytesperline and sizeimage fields are computed from the requested bytes
157
* per line value in the pix format and information from the video instance.
159
* Return the number of padding bytes at end of line.
161
static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
162
const struct v4l2_mbus_framefmt *mbus,
163
struct v4l2_pix_format *pix)
165
unsigned int bpl = pix->bytesperline;
166
unsigned int min_bpl;
169
memset(pix, 0, sizeof(*pix));
170
pix->width = mbus->width;
171
pix->height = mbus->height;
173
for (i = 0; i < ARRAY_SIZE(formats); ++i) {
174
if (formats[i].code == mbus->code)
178
if (WARN_ON(i == ARRAY_SIZE(formats)))
181
min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
183
/* Clamp the requested bytes per line value. If the maximum bytes per
184
* line value is zero, the module doesn't support user configurable line
185
* sizes. Override the requested value with the minimum in that case.
188
bpl = clamp(bpl, min_bpl, video->bpl_max);
192
if (!video->bpl_zero_padding || bpl != min_bpl)
193
bpl = ALIGN(bpl, video->bpl_alignment);
195
pix->pixelformat = formats[i].pixelformat;
196
pix->bytesperline = bpl;
197
pix->sizeimage = pix->bytesperline * pix->height;
198
pix->colorspace = mbus->colorspace;
199
pix->field = mbus->field;
201
return bpl - min_bpl;
204
static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
205
struct v4l2_mbus_framefmt *mbus)
209
memset(mbus, 0, sizeof(*mbus));
210
mbus->width = pix->width;
211
mbus->height = pix->height;
213
for (i = 0; i < ARRAY_SIZE(formats); ++i) {
214
if (formats[i].pixelformat == pix->pixelformat)
218
if (WARN_ON(i == ARRAY_SIZE(formats)))
221
mbus->code = formats[i].code;
222
mbus->colorspace = pix->colorspace;
223
mbus->field = pix->field;
226
static struct v4l2_subdev *
227
isp_video_remote_subdev(struct isp_video *video, u32 *pad)
229
struct media_pad *remote;
231
remote = media_entity_remote_source(&video->pad);
233
if (remote == NULL ||
234
media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
238
*pad = remote->index;
240
return media_entity_to_v4l2_subdev(remote->entity);
243
/* Return a pointer to the ISP video instance at the far end of the pipeline. */
244
static struct isp_video *
245
isp_video_far_end(struct isp_video *video)
247
struct media_entity_graph graph;
248
struct media_entity *entity = &video->video.entity;
249
struct media_device *mdev = entity->parent;
250
struct isp_video *far_end = NULL;
252
mutex_lock(&mdev->graph_mutex);
253
media_entity_graph_walk_start(&graph, entity);
255
while ((entity = media_entity_graph_walk_next(&graph))) {
256
if (entity == &video->video.entity)
259
if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
262
far_end = to_isp_video(media_entity_to_video_device(entity));
263
if (far_end->type != video->type)
269
mutex_unlock(&mdev->graph_mutex);
274
* Validate a pipeline by checking both ends of all links for format
277
* Compute the minimum time per frame value as the maximum of time per frame
278
* limits reported by every block in the pipeline.
280
* Return 0 if all formats match, or -EPIPE if at least one link is found with
281
* different formats on its two ends.
283
static int isp_video_validate_pipeline(struct isp_pipeline *pipe)
285
struct isp_device *isp = pipe->output->isp;
286
struct v4l2_subdev_format fmt_source;
287
struct v4l2_subdev_format fmt_sink;
288
struct media_pad *pad;
289
struct v4l2_subdev *subdev;
292
pipe->max_rate = pipe->l3_ick;
294
subdev = isp_video_remote_subdev(pipe->output, NULL);
299
unsigned int shifter_link;
300
/* Retrieve the sink format */
301
pad = &subdev->entity.pads[0];
302
if (!(pad->flags & MEDIA_PAD_FL_SINK))
305
fmt_sink.pad = pad->index;
306
fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
307
ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_sink);
308
if (ret < 0 && ret != -ENOIOCTLCMD)
311
/* Update the maximum frame rate */
312
if (subdev == &isp->isp_res.subdev)
313
omap3isp_resizer_max_rate(&isp->isp_res,
316
/* Check ccdc maximum data rate when data comes from sensor
317
* TODO: Include ccdc rate in pipe->max_rate and compare the
318
* total pipe rate with the input data rate from sensor.
320
if (subdev == &isp->isp_ccdc.subdev && pipe->input == NULL) {
321
unsigned int rate = UINT_MAX;
323
omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
324
if (isp->isp_ccdc.vpcfg.pixelclk > rate)
328
/* If sink pad is on CCDC, the link has the lane shifter
329
* in the middle of it. */
330
shifter_link = subdev == &isp->isp_ccdc.subdev;
332
/* Retrieve the source format */
333
pad = media_entity_remote_source(pad);
335
media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
338
subdev = media_entity_to_v4l2_subdev(pad->entity);
340
fmt_source.pad = pad->index;
341
fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
342
ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
343
if (ret < 0 && ret != -ENOIOCTLCMD)
346
/* Check if the two ends match */
347
if (fmt_source.format.width != fmt_sink.format.width ||
348
fmt_source.format.height != fmt_sink.format.height)
352
unsigned int parallel_shift = 0;
353
if (isp->isp_ccdc.input == CCDC_INPUT_PARALLEL) {
354
struct isp_parallel_platform_data *pdata =
355
&((struct isp_v4l2_subdevs_group *)
356
subdev->host_priv)->bus.parallel;
357
parallel_shift = pdata->data_lane_shift * 2;
359
if (!isp_video_is_shiftable(fmt_source.format.code,
360
fmt_sink.format.code,
363
} else if (fmt_source.format.code != fmt_sink.format.code)
371
__isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
373
struct v4l2_subdev_format fmt;
374
struct v4l2_subdev *subdev;
378
subdev = isp_video_remote_subdev(video, &pad);
382
mutex_lock(&video->mutex);
385
fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
386
ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
387
if (ret == -ENOIOCTLCMD)
390
mutex_unlock(&video->mutex);
395
format->type = video->type;
396
return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
400
isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
402
struct v4l2_format format;
405
memcpy(&format, &vfh->format, sizeof(format));
406
ret = __isp_video_get_format(video, &format);
410
if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
411
vfh->format.fmt.pix.height != format.fmt.pix.height ||
412
vfh->format.fmt.pix.width != format.fmt.pix.width ||
413
vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
414
vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
420
/* -----------------------------------------------------------------------------
424
#define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
427
* ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
428
* @dev: Device pointer specific to the OMAP3 ISP.
429
* @sglist: Pointer to source Scatter gather list to allocate.
430
* @sglen: Number of elements of the scatter-gatter list.
432
* Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
433
* we ran out of memory.
436
ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
438
struct sg_table *sgt;
441
sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
445
sgt->sgl = (struct scatterlist *)sglist;
447
sgt->orig_nents = sglen;
449
da = iommu_vmap(isp->iommu, 0, sgt, IOMMU_FLAG);
450
if (IS_ERR_VALUE(da))
457
* ispmmu_vunmap - Unmap a device address from the ISP MMU
458
* @dev: Device pointer specific to the OMAP3 ISP.
459
* @da: Device address generated from a ispmmu_vmap call.
461
static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
463
struct sg_table *sgt;
465
sgt = iommu_vunmap(isp->iommu, (u32)da);
469
/* -----------------------------------------------------------------------------
470
* Video queue operations
473
static void isp_video_queue_prepare(struct isp_video_queue *queue,
474
unsigned int *nbuffers, unsigned int *size)
476
struct isp_video_fh *vfh =
477
container_of(queue, struct isp_video_fh, queue);
478
struct isp_video *video = vfh->video;
480
*size = vfh->format.fmt.pix.sizeimage;
484
*nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
487
static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
489
struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
490
struct isp_buffer *buffer = to_isp_buffer(buf);
491
struct isp_video *video = vfh->video;
493
if (buffer->isp_addr) {
494
ispmmu_vunmap(video->isp, buffer->isp_addr);
495
buffer->isp_addr = 0;
499
static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
501
struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
502
struct isp_buffer *buffer = to_isp_buffer(buf);
503
struct isp_video *video = vfh->video;
506
addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
507
if (IS_ERR_VALUE(addr))
510
if (!IS_ALIGNED(addr, 32)) {
511
dev_dbg(video->isp->dev, "Buffer address must be "
512
"aligned to 32 bytes boundary.\n");
513
ispmmu_vunmap(video->isp, buffer->isp_addr);
517
buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
518
buffer->isp_addr = addr;
523
* isp_video_buffer_queue - Add buffer to streaming queue
526
* In memory-to-memory mode, start streaming on the pipeline if buffers are
527
* queued on both the input and the output, if the pipeline isn't already busy.
528
* If the pipeline is busy, it will be restarted in the output module interrupt
531
static void isp_video_buffer_queue(struct isp_video_buffer *buf)
533
struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
534
struct isp_buffer *buffer = to_isp_buffer(buf);
535
struct isp_video *video = vfh->video;
536
struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
537
enum isp_pipeline_state state;
542
empty = list_empty(&video->dmaqueue);
543
list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
546
if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
547
state = ISP_PIPELINE_QUEUE_OUTPUT;
549
state = ISP_PIPELINE_QUEUE_INPUT;
551
spin_lock_irqsave(&pipe->lock, flags);
552
pipe->state |= state;
553
video->ops->queue(video, buffer);
554
video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
556
start = isp_pipeline_ready(pipe);
558
pipe->state |= ISP_PIPELINE_STREAM;
559
spin_unlock_irqrestore(&pipe->lock, flags);
562
omap3isp_pipeline_set_stream(pipe,
563
ISP_PIPELINE_STREAM_SINGLESHOT);
567
static const struct isp_video_queue_operations isp_video_queue_ops = {
568
.queue_prepare = &isp_video_queue_prepare,
569
.buffer_prepare = &isp_video_buffer_prepare,
570
.buffer_queue = &isp_video_buffer_queue,
571
.buffer_cleanup = &isp_video_buffer_cleanup,
575
* omap3isp_video_buffer_next - Complete the current buffer and return the next
576
* @video: ISP video object
577
* @error: Whether an error occurred during capture
579
* Remove the current video buffer from the DMA queue and fill its timestamp,
580
* field count and state fields before waking up its completion handler.
582
* The buffer state is set to VIDEOBUF_DONE if no error occurred (@error is 0)
583
* or VIDEOBUF_ERROR otherwise (@error is non-zero).
585
* The DMA queue is expected to contain at least one buffer.
587
* Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
590
struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video,
593
struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
594
struct isp_video_queue *queue = video->queue;
595
enum isp_pipeline_state state;
596
struct isp_video_buffer *buf;
600
spin_lock_irqsave(&queue->irqlock, flags);
601
if (WARN_ON(list_empty(&video->dmaqueue))) {
602
spin_unlock_irqrestore(&queue->irqlock, flags);
606
buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
608
list_del(&buf->irqlist);
609
spin_unlock_irqrestore(&queue->irqlock, flags);
612
buf->vbuf.timestamp.tv_sec = ts.tv_sec;
613
buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
615
/* Do frame number propagation only if this is the output video node.
616
* Frame number either comes from the CSI receivers or it gets
617
* incremented here if H3A is not active.
618
* Note: There is no guarantee that the output buffer will finish
619
* first, so the input number might lag behind by 1 in some cases.
621
if (video == pipe->output && !pipe->do_propagation)
622
buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
624
buf->vbuf.sequence = atomic_read(&pipe->frame_number);
626
buf->state = error ? ISP_BUF_STATE_ERROR : ISP_BUF_STATE_DONE;
630
if (list_empty(&video->dmaqueue)) {
631
if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
632
state = ISP_PIPELINE_QUEUE_OUTPUT
633
| ISP_PIPELINE_STREAM;
635
state = ISP_PIPELINE_QUEUE_INPUT
636
| ISP_PIPELINE_STREAM;
638
spin_lock_irqsave(&pipe->lock, flags);
639
pipe->state &= ~state;
640
if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
641
video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
642
spin_unlock_irqrestore(&pipe->lock, flags);
646
if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
647
spin_lock_irqsave(&pipe->lock, flags);
648
pipe->state &= ~ISP_PIPELINE_STREAM;
649
spin_unlock_irqrestore(&pipe->lock, flags);
652
buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
654
buf->state = ISP_BUF_STATE_ACTIVE;
655
return to_isp_buffer(buf);
659
* omap3isp_video_resume - Perform resume operation on the buffers
660
* @video: ISP video object
661
* @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
663
* This function is intended to be used on suspend/resume scenario. It
664
* requests video queue layer to discard buffers marked as DONE if it's in
665
* continuous mode and requests ISP modules to queue again the ACTIVE buffer
668
void omap3isp_video_resume(struct isp_video *video, int continuous)
670
struct isp_buffer *buf = NULL;
672
if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
673
omap3isp_video_queue_discard_done(video->queue);
675
if (!list_empty(&video->dmaqueue)) {
676
buf = list_first_entry(&video->dmaqueue,
677
struct isp_buffer, buffer.irqlist);
678
video->ops->queue(video, buf);
679
video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
682
video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
686
/* -----------------------------------------------------------------------------
691
isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
693
struct isp_video *video = video_drvdata(file);
695
strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
696
strlcpy(cap->card, video->video.name, sizeof(cap->card));
697
strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
698
cap->version = ISP_VIDEO_DRIVER_VERSION;
700
if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
701
cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
703
cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
709
isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
711
struct isp_video_fh *vfh = to_isp_video_fh(fh);
712
struct isp_video *video = video_drvdata(file);
714
if (format->type != video->type)
717
mutex_lock(&video->mutex);
718
*format = vfh->format;
719
mutex_unlock(&video->mutex);
725
isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
727
struct isp_video_fh *vfh = to_isp_video_fh(fh);
728
struct isp_video *video = video_drvdata(file);
729
struct v4l2_mbus_framefmt fmt;
731
if (format->type != video->type)
734
mutex_lock(&video->mutex);
736
/* Fill the bytesperline and sizeimage fields by converting to media bus
737
* format and back to pixel format.
739
isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
740
isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
742
vfh->format = *format;
744
mutex_unlock(&video->mutex);
749
isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
751
struct isp_video *video = video_drvdata(file);
752
struct v4l2_subdev_format fmt;
753
struct v4l2_subdev *subdev;
757
if (format->type != video->type)
760
subdev = isp_video_remote_subdev(video, &pad);
764
isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
767
fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
768
ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
770
return ret == -ENOIOCTLCMD ? -EINVAL : ret;
772
isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
777
isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
779
struct isp_video *video = video_drvdata(file);
780
struct v4l2_subdev *subdev;
783
subdev = isp_video_remote_subdev(video, NULL);
787
mutex_lock(&video->mutex);
788
ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
789
mutex_unlock(&video->mutex);
791
return ret == -ENOIOCTLCMD ? -EINVAL : ret;
795
isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
797
struct isp_video *video = video_drvdata(file);
798
struct v4l2_subdev_format format;
799
struct v4l2_subdev *subdev;
803
subdev = isp_video_remote_subdev(video, &pad);
807
/* Try the get crop operation first and fallback to get format if not
810
ret = v4l2_subdev_call(subdev, video, g_crop, crop);
811
if (ret != -ENOIOCTLCMD)
815
format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
816
ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
818
return ret == -ENOIOCTLCMD ? -EINVAL : ret;
822
crop->c.width = format.format.width;
823
crop->c.height = format.format.height;
829
isp_video_set_crop(struct file *file, void *fh, struct v4l2_crop *crop)
831
struct isp_video *video = video_drvdata(file);
832
struct v4l2_subdev *subdev;
835
subdev = isp_video_remote_subdev(video, NULL);
839
mutex_lock(&video->mutex);
840
ret = v4l2_subdev_call(subdev, video, s_crop, crop);
841
mutex_unlock(&video->mutex);
843
return ret == -ENOIOCTLCMD ? -EINVAL : ret;
847
isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
849
struct isp_video_fh *vfh = to_isp_video_fh(fh);
850
struct isp_video *video = video_drvdata(file);
852
if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
853
video->type != a->type)
856
memset(a, 0, sizeof(*a));
857
a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
858
a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
859
a->parm.output.timeperframe = vfh->timeperframe;
865
isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
867
struct isp_video_fh *vfh = to_isp_video_fh(fh);
868
struct isp_video *video = video_drvdata(file);
870
if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
871
video->type != a->type)
874
if (a->parm.output.timeperframe.denominator == 0)
875
a->parm.output.timeperframe.denominator = 1;
877
vfh->timeperframe = a->parm.output.timeperframe;
883
isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
885
struct isp_video_fh *vfh = to_isp_video_fh(fh);
887
return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
891
isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
893
struct isp_video_fh *vfh = to_isp_video_fh(fh);
895
return omap3isp_video_queue_querybuf(&vfh->queue, b);
899
isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
901
struct isp_video_fh *vfh = to_isp_video_fh(fh);
903
return omap3isp_video_queue_qbuf(&vfh->queue, b);
907
isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
909
struct isp_video_fh *vfh = to_isp_video_fh(fh);
911
return omap3isp_video_queue_dqbuf(&vfh->queue, b,
912
file->f_flags & O_NONBLOCK);
918
* Every ISP pipeline has a single input and a single output. The input can be
919
* either a sensor or a video node. The output is always a video node.
921
* As every pipeline has an output video node, the ISP video objects at the
922
* pipeline output stores the pipeline state. It tracks the streaming state of
923
* both the input and output, as well as the availability of buffers.
925
* In sensor-to-memory mode, frames are always available at the pipeline input.
926
* Starting the sensor usually requires I2C transfers and must be done in
927
* interruptible context. The pipeline is started and stopped synchronously
928
* to the stream on/off commands. All modules in the pipeline will get their
929
* subdev set stream handler called. The module at the end of the pipeline must
930
* delay starting the hardware until buffers are available at its output.
932
* In memory-to-memory mode, starting/stopping the stream requires
933
* synchronization between the input and output. ISP modules can't be stopped
934
* in the middle of a frame, and at least some of the modules seem to become
935
* busy as soon as they're started, even if they don't receive a frame start
936
* event. For that reason frames need to be processed in single-shot mode. The
937
* driver needs to wait until a frame is completely processed and written to
938
* memory before restarting the pipeline for the next frame. Pipelined
939
* processing might be possible but requires more testing.
941
* Stream start must be delayed until buffers are available at both the input
942
* and output. The pipeline must be started in the videobuf queue callback with
943
* the buffers queue spinlock held. The modules subdev set stream operation must
947
isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
949
struct isp_video_fh *vfh = to_isp_video_fh(fh);
950
struct isp_video *video = video_drvdata(file);
951
enum isp_pipeline_state state;
952
struct isp_pipeline *pipe;
953
struct isp_video *far_end;
957
if (type != video->type)
960
mutex_lock(&video->stream_lock);
962
if (video->streaming) {
963
mutex_unlock(&video->stream_lock);
967
/* Start streaming on the pipeline. No link touching an entity in the
968
* pipeline can be activated or deactivated once streaming is started.
970
pipe = video->video.entity.pipe
971
? to_isp_pipeline(&video->video.entity) : &video->pipe;
972
media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
974
/* Verify that the currently configured format matches the output of
975
* the connected subdev.
977
ret = isp_video_check_format(video, vfh);
981
video->bpl_padding = ret;
982
video->bpl_value = vfh->format.fmt.pix.bytesperline;
984
/* Find the ISP video node connected at the far end of the pipeline and
985
* update the pipeline.
987
far_end = isp_video_far_end(video);
989
if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
990
state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
991
pipe->input = far_end;
992
pipe->output = video;
994
if (far_end == NULL) {
999
state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1000
pipe->input = video;
1001
pipe->output = far_end;
1004
if (video->isp->pdata->set_constraints)
1005
video->isp->pdata->set_constraints(video->isp, true);
1006
pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1008
/* Validate the pipeline and update its state. */
1009
ret = isp_video_validate_pipeline(pipe);
1013
spin_lock_irqsave(&pipe->lock, flags);
1014
pipe->state &= ~ISP_PIPELINE_STREAM;
1015
pipe->state |= state;
1016
spin_unlock_irqrestore(&pipe->lock, flags);
1018
/* Set the maximum time per frame as the value requested by userspace.
1019
* This is a soft limit that can be overridden if the hardware doesn't
1020
* support the request limit.
1022
if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1023
pipe->max_timeperframe = vfh->timeperframe;
1025
video->queue = &vfh->queue;
1026
INIT_LIST_HEAD(&video->dmaqueue);
1027
atomic_set(&pipe->frame_number, -1);
1029
ret = omap3isp_video_queue_streamon(&vfh->queue);
1033
/* In sensor-to-memory mode, the stream can be started synchronously
1034
* to the stream on command. In memory-to-memory mode, it will be
1035
* started when buffers are queued on both the input and output.
1037
if (pipe->input == NULL) {
1038
ret = omap3isp_pipeline_set_stream(pipe,
1039
ISP_PIPELINE_STREAM_CONTINUOUS);
1042
spin_lock_irqsave(&video->queue->irqlock, flags);
1043
if (list_empty(&video->dmaqueue))
1044
video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1045
spin_unlock_irqrestore(&video->queue->irqlock, flags);
1050
omap3isp_video_queue_streamoff(&vfh->queue);
1051
if (video->isp->pdata->set_constraints)
1052
video->isp->pdata->set_constraints(video->isp, false);
1053
media_entity_pipeline_stop(&video->video.entity);
1054
video->queue = NULL;
1058
video->streaming = 1;
1060
mutex_unlock(&video->stream_lock);
1065
isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1067
struct isp_video_fh *vfh = to_isp_video_fh(fh);
1068
struct isp_video *video = video_drvdata(file);
1069
struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1070
enum isp_pipeline_state state;
1071
unsigned int streaming;
1072
unsigned long flags;
1074
if (type != video->type)
1077
mutex_lock(&video->stream_lock);
1079
/* Make sure we're not streaming yet. */
1080
mutex_lock(&vfh->queue.lock);
1081
streaming = vfh->queue.streaming;
1082
mutex_unlock(&vfh->queue.lock);
1087
/* Update the pipeline state. */
1088
if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1089
state = ISP_PIPELINE_STREAM_OUTPUT
1090
| ISP_PIPELINE_QUEUE_OUTPUT;
1092
state = ISP_PIPELINE_STREAM_INPUT
1093
| ISP_PIPELINE_QUEUE_INPUT;
1095
spin_lock_irqsave(&pipe->lock, flags);
1096
pipe->state &= ~state;
1097
spin_unlock_irqrestore(&pipe->lock, flags);
1099
/* Stop the stream. */
1100
omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1101
omap3isp_video_queue_streamoff(&vfh->queue);
1102
video->queue = NULL;
1103
video->streaming = 0;
1105
if (video->isp->pdata->set_constraints)
1106
video->isp->pdata->set_constraints(video->isp, false);
1107
media_entity_pipeline_stop(&video->video.entity);
1110
mutex_unlock(&video->stream_lock);
1115
isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1117
if (input->index > 0)
1120
strlcpy(input->name, "camera", sizeof(input->name));
1121
input->type = V4L2_INPUT_TYPE_CAMERA;
1127
isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1135
isp_video_s_input(struct file *file, void *fh, unsigned int input)
1137
return input == 0 ? 0 : -EINVAL;
1140
static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1141
.vidioc_querycap = isp_video_querycap,
1142
.vidioc_g_fmt_vid_cap = isp_video_get_format,
1143
.vidioc_s_fmt_vid_cap = isp_video_set_format,
1144
.vidioc_try_fmt_vid_cap = isp_video_try_format,
1145
.vidioc_g_fmt_vid_out = isp_video_get_format,
1146
.vidioc_s_fmt_vid_out = isp_video_set_format,
1147
.vidioc_try_fmt_vid_out = isp_video_try_format,
1148
.vidioc_cropcap = isp_video_cropcap,
1149
.vidioc_g_crop = isp_video_get_crop,
1150
.vidioc_s_crop = isp_video_set_crop,
1151
.vidioc_g_parm = isp_video_get_param,
1152
.vidioc_s_parm = isp_video_set_param,
1153
.vidioc_reqbufs = isp_video_reqbufs,
1154
.vidioc_querybuf = isp_video_querybuf,
1155
.vidioc_qbuf = isp_video_qbuf,
1156
.vidioc_dqbuf = isp_video_dqbuf,
1157
.vidioc_streamon = isp_video_streamon,
1158
.vidioc_streamoff = isp_video_streamoff,
1159
.vidioc_enum_input = isp_video_enum_input,
1160
.vidioc_g_input = isp_video_g_input,
1161
.vidioc_s_input = isp_video_s_input,
1164
/* -----------------------------------------------------------------------------
1165
* V4L2 file operations
1168
static int isp_video_open(struct file *file)
1170
struct isp_video *video = video_drvdata(file);
1171
struct isp_video_fh *handle;
1174
handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1178
v4l2_fh_init(&handle->vfh, &video->video);
1179
v4l2_fh_add(&handle->vfh);
1181
/* If this is the first user, initialise the pipeline. */
1182
if (omap3isp_get(video->isp) == NULL) {
1187
ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1189
omap3isp_put(video->isp);
1193
omap3isp_video_queue_init(&handle->queue, video->type,
1194
&isp_video_queue_ops, video->isp->dev,
1195
sizeof(struct isp_buffer));
1197
memset(&handle->format, 0, sizeof(handle->format));
1198
handle->format.type = video->type;
1199
handle->timeperframe.denominator = 1;
1201
handle->video = video;
1202
file->private_data = &handle->vfh;
1206
v4l2_fh_del(&handle->vfh);
1213
static int isp_video_release(struct file *file)
1215
struct isp_video *video = video_drvdata(file);
1216
struct v4l2_fh *vfh = file->private_data;
1217
struct isp_video_fh *handle = to_isp_video_fh(vfh);
1219
/* Disable streaming and free the buffers queue resources. */
1220
isp_video_streamoff(file, vfh, video->type);
1222
mutex_lock(&handle->queue.lock);
1223
omap3isp_video_queue_cleanup(&handle->queue);
1224
mutex_unlock(&handle->queue.lock);
1226
omap3isp_pipeline_pm_use(&video->video.entity, 0);
1228
/* Release the file handle. */
1231
file->private_data = NULL;
1233
omap3isp_put(video->isp);
1238
static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1240
struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1241
struct isp_video_queue *queue = &vfh->queue;
1243
return omap3isp_video_queue_poll(queue, file, wait);
1246
static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1248
struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1250
return omap3isp_video_queue_mmap(&vfh->queue, vma);
1253
static struct v4l2_file_operations isp_video_fops = {
1254
.owner = THIS_MODULE,
1255
.unlocked_ioctl = video_ioctl2,
1256
.open = isp_video_open,
1257
.release = isp_video_release,
1258
.poll = isp_video_poll,
1259
.mmap = isp_video_mmap,
1262
/* -----------------------------------------------------------------------------
1266
static const struct isp_video_operations isp_video_dummy_ops = {
1269
int omap3isp_video_init(struct isp_video *video, const char *name)
1271
const char *direction;
1274
switch (video->type) {
1275
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1276
direction = "output";
1277
video->pad.flags = MEDIA_PAD_FL_SINK;
1279
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1280
direction = "input";
1281
video->pad.flags = MEDIA_PAD_FL_SOURCE;
1288
ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1292
mutex_init(&video->mutex);
1293
atomic_set(&video->active, 0);
1295
spin_lock_init(&video->pipe.lock);
1296
mutex_init(&video->stream_lock);
1298
/* Initialize the video device. */
1299
if (video->ops == NULL)
1300
video->ops = &isp_video_dummy_ops;
1302
video->video.fops = &isp_video_fops;
1303
snprintf(video->video.name, sizeof(video->video.name),
1304
"OMAP3 ISP %s %s", name, direction);
1305
video->video.vfl_type = VFL_TYPE_GRABBER;
1306
video->video.release = video_device_release_empty;
1307
video->video.ioctl_ops = &isp_video_ioctl_ops;
1308
video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1310
video_set_drvdata(&video->video, video);
1315
int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1319
video->video.v4l2_dev = vdev;
1321
ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1323
printk(KERN_ERR "%s: could not register video device (%d)\n",
1329
void omap3isp_video_unregister(struct isp_video *video)
1331
if (video_is_registered(&video->video)) {
1332
media_entity_cleanup(&video->video.entity);
1333
video_unregister_device(&video->video);