Commit ab2e8d5d authored by Paul Kocialkowski's avatar Paul Kocialkowski Committed by Mauro Carvalho Chehab

media: sun6i-csi: Tidy up video code

Some code cleanups, renames, variable lowerings and moving things around for
better organization. No functional change intended.
Signed-off-by: default avatarPaul Kocialkowski <paul.kocialkowski@bootlin.com>
Reviewed-by: default avatarMaxime Ripard <maxime@cerno.tech>
Signed-off-by: default avatarHans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@kernel.org>
parent cad7f35c
...@@ -595,7 +595,7 @@ static int sun6i_csi_link_entity(struct sun6i_csi_device *csi_dev, ...@@ -595,7 +595,7 @@ static int sun6i_csi_link_entity(struct sun6i_csi_device *csi_dev,
src_pad_index = ret; src_pad_index = ret;
sink = &csi_dev->video.vdev.entity; sink = &csi_dev->video.video_dev.entity;
sink_pad = &csi_dev->video.pad; sink_pad = &csi_dev->video.pad;
dev_dbg(csi_dev->dev, "creating %s:%u -> %s:%u link\n", dev_dbg(csi_dev->dev, "creating %s:%u -> %s:%u link\n",
...@@ -706,7 +706,7 @@ static int sun6i_csi_v4l2_setup(struct sun6i_csi_device *csi_dev) ...@@ -706,7 +706,7 @@ static int sun6i_csi_v4l2_setup(struct sun6i_csi_device *csi_dev)
/* Video */ /* Video */
ret = sun6i_video_init(&csi_dev->video, csi_dev, SUN6I_CSI_NAME); ret = sun6i_video_setup(&csi_dev->video, csi_dev);
if (ret) if (ret)
goto error_v4l2_device; goto error_v4l2_device;
......
...@@ -24,14 +24,34 @@ ...@@ -24,14 +24,34 @@
#define MAX_HEIGHT (4800) #define MAX_HEIGHT (4800)
struct sun6i_csi_buffer { struct sun6i_csi_buffer {
struct vb2_v4l2_buffer vb; struct vb2_v4l2_buffer v4l2_buffer;
struct list_head list; struct list_head list;
dma_addr_t dma_addr; dma_addr_t dma_addr;
bool queued_to_csi; bool queued_to_csi;
}; };
static const u32 supported_pixformats[] = { /* Helpers */
static struct v4l2_subdev *
sun6i_video_remote_subdev(struct sun6i_video *video, u32 *pad)
{
struct media_pad *remote;
remote = media_pad_remote_pad_first(&video->pad);
if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
return NULL;
if (pad)
*pad = remote->index;
return media_entity_to_v4l2_subdev(remote->entity);
}
/* Format */
static const u32 sun6i_video_formats[] = {
V4L2_PIX_FMT_SBGGR8, V4L2_PIX_FMT_SBGGR8,
V4L2_PIX_FMT_SGBRG8, V4L2_PIX_FMT_SGBRG8,
V4L2_PIX_FMT_SGRBG8, V4L2_PIX_FMT_SGRBG8,
...@@ -61,77 +81,80 @@ static const u32 supported_pixformats[] = { ...@@ -61,77 +81,80 @@ static const u32 supported_pixformats[] = {
V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_JPEG,
}; };
static bool is_pixformat_valid(unsigned int pixformat) static bool sun6i_video_format_check(u32 format)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < ARRAY_SIZE(supported_pixformats); i++) for (i = 0; i < ARRAY_SIZE(sun6i_video_formats); i++)
if (supported_pixformats[i] == pixformat) if (sun6i_video_formats[i] == format)
return true; return true;
return false; return false;
} }
static struct v4l2_subdev * /* Queue */
sun6i_video_remote_subdev(struct sun6i_video *video, u32 *pad)
{
struct media_pad *remote;
remote = media_pad_remote_pad_first(&video->pad);
if (!remote || !is_media_entity_v4l2_subdev(remote->entity)) static int sun6i_video_queue_setup(struct vb2_queue *queue,
return NULL; unsigned int *buffers_count,
unsigned int *planes_count,
if (pad)
*pad = remote->index;
return media_entity_to_v4l2_subdev(remote->entity);
}
static int sun6i_video_queue_setup(struct vb2_queue *vq,
unsigned int *nbuffers,
unsigned int *nplanes,
unsigned int sizes[], unsigned int sizes[],
struct device *alloc_devs[]) struct device *alloc_devs[])
{ {
struct sun6i_video *video = vb2_get_drv_priv(vq); struct sun6i_video *video = vb2_get_drv_priv(queue);
unsigned int size = video->fmt.fmt.pix.sizeimage; unsigned int size = video->format.fmt.pix.sizeimage;
if (*nplanes) if (*planes_count)
return sizes[0] < size ? -EINVAL : 0; return sizes[0] < size ? -EINVAL : 0;
*nplanes = 1; *planes_count = 1;
sizes[0] = size; sizes[0] = size;
return 0; return 0;
} }
static int sun6i_video_buffer_prepare(struct vb2_buffer *vb) static int sun6i_video_buffer_prepare(struct vb2_buffer *buffer)
{ {
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); struct sun6i_video *video = vb2_get_drv_priv(buffer->vb2_queue);
struct sun6i_csi_buffer *buf = struct sun6i_csi_device *csi_dev = video->csi_dev;
container_of(vbuf, struct sun6i_csi_buffer, vb); struct v4l2_device *v4l2_dev = &csi_dev->v4l2.v4l2_dev;
struct sun6i_video *video = vb2_get_drv_priv(vb->vb2_queue); struct vb2_v4l2_buffer *v4l2_buffer = to_vb2_v4l2_buffer(buffer);
unsigned long size = video->fmt.fmt.pix.sizeimage; struct sun6i_csi_buffer *csi_buffer =
container_of(v4l2_buffer, struct sun6i_csi_buffer, v4l2_buffer);
if (vb2_plane_size(vb, 0) < size) { unsigned long size = video->format.fmt.pix.sizeimage;
v4l2_err(video->vdev.v4l2_dev, "buffer too small (%lu < %lu)\n",
vb2_plane_size(vb, 0), size); if (vb2_plane_size(buffer, 0) < size) {
v4l2_err(v4l2_dev, "buffer too small (%lu < %lu)\n",
vb2_plane_size(buffer, 0), size);
return -EINVAL; return -EINVAL;
} }
vb2_set_plane_payload(vb, 0, size); vb2_set_plane_payload(buffer, 0, size);
buf->dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
vbuf->field = video->fmt.fmt.pix.field; csi_buffer->dma_addr = vb2_dma_contig_plane_dma_addr(buffer, 0);
v4l2_buffer->field = video->format.fmt.pix.field;
return 0; return 0;
} }
static int sun6i_video_start_streaming(struct vb2_queue *vq, unsigned int count) static void sun6i_video_buffer_queue(struct vb2_buffer *buffer)
{
struct sun6i_video *video = vb2_get_drv_priv(buffer->vb2_queue);
struct vb2_v4l2_buffer *v4l2_buffer = to_vb2_v4l2_buffer(buffer);
struct sun6i_csi_buffer *csi_buffer =
container_of(v4l2_buffer, struct sun6i_csi_buffer, v4l2_buffer);
unsigned long flags;
spin_lock_irqsave(&video->dma_queue_lock, flags);
csi_buffer->queued_to_csi = false;
list_add_tail(&csi_buffer->list, &video->dma_queue);
spin_unlock_irqrestore(&video->dma_queue_lock, flags);
}
static int sun6i_video_start_streaming(struct vb2_queue *queue,
unsigned int count)
{ {
struct sun6i_video *video = vb2_get_drv_priv(vq); struct sun6i_video *video = vb2_get_drv_priv(queue);
struct video_device *video_dev = &video->video_dev;
struct sun6i_csi_buffer *buf; struct sun6i_csi_buffer *buf;
struct sun6i_csi_buffer *next_buf; struct sun6i_csi_buffer *next_buf;
struct sun6i_csi_config config; struct sun6i_csi_config config;
...@@ -141,30 +164,30 @@ static int sun6i_video_start_streaming(struct vb2_queue *vq, unsigned int count) ...@@ -141,30 +164,30 @@ static int sun6i_video_start_streaming(struct vb2_queue *vq, unsigned int count)
video->sequence = 0; video->sequence = 0;
ret = video_device_pipeline_alloc_start(&video->vdev); ret = video_device_pipeline_alloc_start(video_dev);
if (ret < 0) if (ret < 0)
goto clear_dma_queue; goto error_dma_queue_flush;
if (video->mbus_code == 0) { if (video->mbus_code == 0) {
ret = -EINVAL; ret = -EINVAL;
goto stop_media_pipeline; goto error_media_pipeline;
} }
subdev = sun6i_video_remote_subdev(video, NULL); subdev = sun6i_video_remote_subdev(video, NULL);
if (!subdev) { if (!subdev) {
ret = -EINVAL; ret = -EINVAL;
goto stop_media_pipeline; goto error_media_pipeline;
} }
config.pixelformat = video->fmt.fmt.pix.pixelformat; config.pixelformat = video->format.fmt.pix.pixelformat;
config.code = video->mbus_code; config.code = video->mbus_code;
config.field = video->fmt.fmt.pix.field; config.field = video->format.fmt.pix.field;
config.width = video->fmt.fmt.pix.width; config.width = video->format.fmt.pix.width;
config.height = video->fmt.fmt.pix.height; config.height = video->format.fmt.pix.height;
ret = sun6i_csi_update_config(video->csi_dev, &config); ret = sun6i_csi_update_config(video->csi_dev, &config);
if (ret < 0) if (ret < 0)
goto stop_media_pipeline; goto error_media_pipeline;
spin_lock_irqsave(&video->dma_queue_lock, flags); spin_lock_irqsave(&video->dma_queue_lock, flags);
...@@ -200,27 +223,30 @@ static int sun6i_video_start_streaming(struct vb2_queue *vq, unsigned int count) ...@@ -200,27 +223,30 @@ static int sun6i_video_start_streaming(struct vb2_queue *vq, unsigned int count)
ret = v4l2_subdev_call(subdev, video, s_stream, 1); ret = v4l2_subdev_call(subdev, video, s_stream, 1);
if (ret && ret != -ENOIOCTLCMD) if (ret && ret != -ENOIOCTLCMD)
goto stop_csi_stream; goto error_stream;
return 0; return 0;
stop_csi_stream: error_stream:
sun6i_csi_set_stream(video->csi_dev, false); sun6i_csi_set_stream(video->csi_dev, false);
stop_media_pipeline:
video_device_pipeline_stop(&video->vdev); error_media_pipeline:
clear_dma_queue: video_device_pipeline_stop(video_dev);
error_dma_queue_flush:
spin_lock_irqsave(&video->dma_queue_lock, flags); spin_lock_irqsave(&video->dma_queue_lock, flags);
list_for_each_entry(buf, &video->dma_queue, list) list_for_each_entry(buf, &video->dma_queue, list)
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED); vb2_buffer_done(&buf->v4l2_buffer.vb2_buf,
VB2_BUF_STATE_QUEUED);
INIT_LIST_HEAD(&video->dma_queue); INIT_LIST_HEAD(&video->dma_queue);
spin_unlock_irqrestore(&video->dma_queue_lock, flags); spin_unlock_irqrestore(&video->dma_queue_lock, flags);
return ret; return ret;
} }
static void sun6i_video_stop_streaming(struct vb2_queue *vq) static void sun6i_video_stop_streaming(struct vb2_queue *queue)
{ {
struct sun6i_video *video = vb2_get_drv_priv(vq); struct sun6i_video *video = vb2_get_drv_priv(queue);
struct v4l2_subdev *subdev; struct v4l2_subdev *subdev;
unsigned long flags; unsigned long flags;
struct sun6i_csi_buffer *buf; struct sun6i_csi_buffer *buf;
...@@ -231,35 +257,21 @@ static void sun6i_video_stop_streaming(struct vb2_queue *vq) ...@@ -231,35 +257,21 @@ static void sun6i_video_stop_streaming(struct vb2_queue *vq)
sun6i_csi_set_stream(video->csi_dev, false); sun6i_csi_set_stream(video->csi_dev, false);
video_device_pipeline_stop(&video->vdev); video_device_pipeline_stop(&video->video_dev);
/* Release all active buffers */ /* Release all active buffers */
spin_lock_irqsave(&video->dma_queue_lock, flags); spin_lock_irqsave(&video->dma_queue_lock, flags);
list_for_each_entry(buf, &video->dma_queue, list) list_for_each_entry(buf, &video->dma_queue, list)
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); vb2_buffer_done(&buf->v4l2_buffer.vb2_buf, VB2_BUF_STATE_ERROR);
INIT_LIST_HEAD(&video->dma_queue); INIT_LIST_HEAD(&video->dma_queue);
spin_unlock_irqrestore(&video->dma_queue_lock, flags); spin_unlock_irqrestore(&video->dma_queue_lock, flags);
} }
static void sun6i_video_buffer_queue(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct sun6i_csi_buffer *buf =
container_of(vbuf, struct sun6i_csi_buffer, vb);
struct sun6i_video *video = vb2_get_drv_priv(vb->vb2_queue);
unsigned long flags;
spin_lock_irqsave(&video->dma_queue_lock, flags);
buf->queued_to_csi = false;
list_add_tail(&buf->list, &video->dma_queue);
spin_unlock_irqrestore(&video->dma_queue_lock, flags);
}
void sun6i_video_frame_done(struct sun6i_video *video) void sun6i_video_frame_done(struct sun6i_video *video)
{ {
struct sun6i_csi_buffer *buf; struct sun6i_csi_buffer *buf;
struct sun6i_csi_buffer *next_buf; struct sun6i_csi_buffer *next_buf;
struct vb2_v4l2_buffer *vbuf; struct vb2_v4l2_buffer *v4l2_buffer;
spin_lock(&video->dma_queue_lock); spin_lock(&video->dma_queue_lock);
...@@ -267,7 +279,7 @@ void sun6i_video_frame_done(struct sun6i_video *video) ...@@ -267,7 +279,7 @@ void sun6i_video_frame_done(struct sun6i_video *video)
struct sun6i_csi_buffer, list); struct sun6i_csi_buffer, list);
if (list_is_last(&buf->list, &video->dma_queue)) { if (list_is_last(&buf->list, &video->dma_queue)) {
dev_dbg(video->csi_dev->dev, "Frame dropped!\n"); dev_dbg(video->csi_dev->dev, "Frame dropped!\n");
goto unlock; goto complete;
} }
next_buf = list_next_entry(buf, list); next_buf = list_next_entry(buf, list);
...@@ -280,14 +292,14 @@ void sun6i_video_frame_done(struct sun6i_video *video) ...@@ -280,14 +292,14 @@ void sun6i_video_frame_done(struct sun6i_video *video)
next_buf->queued_to_csi = true; next_buf->queued_to_csi = true;
sun6i_csi_update_buf_addr(video->csi_dev, next_buf->dma_addr); sun6i_csi_update_buf_addr(video->csi_dev, next_buf->dma_addr);
dev_dbg(video->csi_dev->dev, "Frame dropped!\n"); dev_dbg(video->csi_dev->dev, "Frame dropped!\n");
goto unlock; goto complete;
} }
list_del(&buf->list); list_del(&buf->list);
vbuf = &buf->vb; v4l2_buffer = &buf->v4l2_buffer;
vbuf->vb2_buf.timestamp = ktime_get_ns(); v4l2_buffer->vb2_buf.timestamp = ktime_get_ns();
vbuf->sequence = video->sequence; v4l2_buffer->sequence = video->sequence;
vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE); vb2_buffer_done(&v4l2_buffer->vb2_buf, VB2_BUF_STATE_DONE);
/* Prepare buffer for next frame but one. */ /* Prepare buffer for next frame but one. */
if (!list_is_last(&next_buf->list, &video->dma_queue)) { if (!list_is_last(&next_buf->list, &video->dma_queue)) {
...@@ -298,165 +310,173 @@ void sun6i_video_frame_done(struct sun6i_video *video) ...@@ -298,165 +310,173 @@ void sun6i_video_frame_done(struct sun6i_video *video)
dev_dbg(video->csi_dev->dev, "Next frame will be dropped!\n"); dev_dbg(video->csi_dev->dev, "Next frame will be dropped!\n");
} }
unlock: complete:
video->sequence++; video->sequence++;
spin_unlock(&video->dma_queue_lock); spin_unlock(&video->dma_queue_lock);
} }
static const struct vb2_ops sun6i_csi_vb2_ops = { static const struct vb2_ops sun6i_video_queue_ops = {
.queue_setup = sun6i_video_queue_setup, .queue_setup = sun6i_video_queue_setup,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
.buf_prepare = sun6i_video_buffer_prepare, .buf_prepare = sun6i_video_buffer_prepare,
.buf_queue = sun6i_video_buffer_queue,
.start_streaming = sun6i_video_start_streaming, .start_streaming = sun6i_video_start_streaming,
.stop_streaming = sun6i_video_stop_streaming, .stop_streaming = sun6i_video_stop_streaming,
.buf_queue = sun6i_video_buffer_queue, .wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
}; };
static int vidioc_querycap(struct file *file, void *priv, /* V4L2 Device */
struct v4l2_capability *cap)
static int sun6i_video_querycap(struct file *file, void *private,
struct v4l2_capability *capability)
{ {
struct sun6i_video *video = video_drvdata(file); struct sun6i_video *video = video_drvdata(file);
struct sun6i_csi_device *csi_dev = video->csi_dev;
struct video_device *video_dev = &video->video_dev;
strscpy(cap->driver, "sun6i-video", sizeof(cap->driver)); strscpy(capability->driver, SUN6I_CSI_NAME, sizeof(capability->driver));
strscpy(cap->card, video->vdev.name, sizeof(cap->card)); strscpy(capability->card, video_dev->name, sizeof(capability->card));
snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", snprintf(capability->bus_info, sizeof(capability->bus_info),
video->csi_dev->dev->of_node->name); "platform:%s", dev_name(csi_dev->dev));
return 0; return 0;
} }
static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, static int sun6i_video_enum_fmt(struct file *file, void *private,
struct v4l2_fmtdesc *f) struct v4l2_fmtdesc *fmtdesc)
{ {
u32 index = f->index; u32 index = fmtdesc->index;
if (index >= ARRAY_SIZE(supported_pixformats)) if (index >= ARRAY_SIZE(sun6i_video_formats))
return -EINVAL; return -EINVAL;
f->pixelformat = supported_pixformats[index]; fmtdesc->pixelformat = sun6i_video_formats[index];
return 0; return 0;
} }
static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, static int sun6i_video_g_fmt(struct file *file, void *private,
struct v4l2_format *fmt) struct v4l2_format *format)
{ {
struct sun6i_video *video = video_drvdata(file); struct sun6i_video *video = video_drvdata(file);
*fmt = video->fmt; *format = video->format;
return 0; return 0;
} }
static int sun6i_video_try_fmt(struct sun6i_video *video, static int sun6i_video_format_try(struct sun6i_video *video,
struct v4l2_format *f) struct v4l2_format *format)
{ {
struct v4l2_pix_format *pixfmt = &f->fmt.pix; struct v4l2_pix_format *pix_format = &format->fmt.pix;
int bpp; int bpp;
if (!is_pixformat_valid(pixfmt->pixelformat)) if (!sun6i_video_format_check(pix_format->pixelformat))
pixfmt->pixelformat = supported_pixformats[0]; pix_format->pixelformat = sun6i_video_formats[0];
v4l_bound_align_image(&pixfmt->width, MIN_WIDTH, MAX_WIDTH, 1, v4l_bound_align_image(&pix_format->width, MIN_WIDTH, MAX_WIDTH, 1,
&pixfmt->height, MIN_HEIGHT, MAX_WIDTH, 1, 1); &pix_format->height, MIN_HEIGHT, MAX_WIDTH, 1, 1);
bpp = sun6i_csi_get_bpp(pixfmt->pixelformat); bpp = sun6i_csi_get_bpp(pix_format->pixelformat);
pixfmt->bytesperline = (pixfmt->width * bpp) >> 3; pix_format->bytesperline = (pix_format->width * bpp) >> 3;
pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height; pix_format->sizeimage = pix_format->bytesperline * pix_format->height;
if (pixfmt->field == V4L2_FIELD_ANY) if (pix_format->field == V4L2_FIELD_ANY)
pixfmt->field = V4L2_FIELD_NONE; pix_format->field = V4L2_FIELD_NONE;
if (pixfmt->pixelformat == V4L2_PIX_FMT_JPEG) if (pix_format->pixelformat == V4L2_PIX_FMT_JPEG)
pixfmt->colorspace = V4L2_COLORSPACE_JPEG; pix_format->colorspace = V4L2_COLORSPACE_JPEG;
else else
pixfmt->colorspace = V4L2_COLORSPACE_SRGB; pix_format->colorspace = V4L2_COLORSPACE_SRGB;
pixfmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; pix_format->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
pixfmt->quantization = V4L2_QUANTIZATION_DEFAULT; pix_format->quantization = V4L2_QUANTIZATION_DEFAULT;
pixfmt->xfer_func = V4L2_XFER_FUNC_DEFAULT; pix_format->xfer_func = V4L2_XFER_FUNC_DEFAULT;
return 0; return 0;
} }
static int sun6i_video_set_fmt(struct sun6i_video *video, struct v4l2_format *f) static int sun6i_video_format_set(struct sun6i_video *video,
struct v4l2_format *format)
{ {
int ret; int ret;
ret = sun6i_video_try_fmt(video, f); ret = sun6i_video_format_try(video, format);
if (ret) if (ret)
return ret; return ret;
video->fmt = *f; video->format = *format;
return 0; return 0;
} }
static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, static int sun6i_video_s_fmt(struct file *file, void *private,
struct v4l2_format *f) struct v4l2_format *format)
{ {
struct sun6i_video *video = video_drvdata(file); struct sun6i_video *video = video_drvdata(file);
if (vb2_is_busy(&video->vb2_vidq)) if (vb2_is_busy(&video->queue))
return -EBUSY; return -EBUSY;
return sun6i_video_set_fmt(video, f); return sun6i_video_format_set(video, format);
} }
static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, static int sun6i_video_try_fmt(struct file *file, void *private,
struct v4l2_format *f) struct v4l2_format *format)
{ {
struct sun6i_video *video = video_drvdata(file); struct sun6i_video *video = video_drvdata(file);
return sun6i_video_try_fmt(video, f); return sun6i_video_format_try(video, format);
} }
static int vidioc_enum_input(struct file *file, void *fh, static int sun6i_video_enum_input(struct file *file, void *private,
struct v4l2_input *inp) struct v4l2_input *input)
{ {
if (inp->index != 0) if (input->index != 0)
return -EINVAL; return -EINVAL;
strscpy(inp->name, "camera", sizeof(inp->name)); input->type = V4L2_INPUT_TYPE_CAMERA;
inp->type = V4L2_INPUT_TYPE_CAMERA; strscpy(input->name, "Camera", sizeof(input->name));
return 0; return 0;
} }
static int vidioc_g_input(struct file *file, void *fh, unsigned int *i) static int sun6i_video_g_input(struct file *file, void *private,
unsigned int *index)
{ {
*i = 0; *index = 0;
return 0; return 0;
} }
static int vidioc_s_input(struct file *file, void *fh, unsigned int i) static int sun6i_video_s_input(struct file *file, void *private,
unsigned int index)
{ {
if (i != 0) if (index != 0)
return -EINVAL; return -EINVAL;
return 0; return 0;
} }
static const struct v4l2_ioctl_ops sun6i_video_ioctl_ops = { static const struct v4l2_ioctl_ops sun6i_video_ioctl_ops = {
.vidioc_querycap = vidioc_querycap, .vidioc_querycap = sun6i_video_querycap,
.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, .vidioc_enum_fmt_vid_cap = sun6i_video_enum_fmt,
.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, .vidioc_g_fmt_vid_cap = sun6i_video_g_fmt,
.vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, .vidioc_s_fmt_vid_cap = sun6i_video_s_fmt,
.vidioc_try_fmt_vid_cap = sun6i_video_try_fmt,
.vidioc_enum_input = vidioc_enum_input, .vidioc_enum_input = sun6i_video_enum_input,
.vidioc_s_input = vidioc_s_input, .vidioc_g_input = sun6i_video_g_input,
.vidioc_g_input = vidioc_g_input, .vidioc_s_input = sun6i_video_s_input,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
.vidioc_reqbufs = vb2_ioctl_reqbufs, .vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_querybuf = vb2_ioctl_querybuf, .vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_expbuf = vb2_ioctl_expbuf, .vidioc_expbuf = vb2_ioctl_expbuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf, .vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
.vidioc_streamon = vb2_ioctl_streamon, .vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff, .vidioc_streamoff = vb2_ioctl_streamoff,
...@@ -465,9 +485,8 @@ static const struct v4l2_ioctl_ops sun6i_video_ioctl_ops = { ...@@ -465,9 +485,8 @@ static const struct v4l2_ioctl_ops sun6i_video_ioctl_ops = {
.vidioc_unsubscribe_event = v4l2_event_unsubscribe, .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
}; };
/* ----------------------------------------------------------------------------- /* V4L2 File */
* V4L2 file operations
*/
static int sun6i_video_open(struct file *file) static int sun6i_video_open(struct file *file)
{ {
struct sun6i_video *video = video_drvdata(file); struct sun6i_video *video = video_drvdata(file);
...@@ -478,44 +497,46 @@ static int sun6i_video_open(struct file *file) ...@@ -478,44 +497,46 @@ static int sun6i_video_open(struct file *file)
ret = v4l2_fh_open(file); ret = v4l2_fh_open(file);
if (ret < 0) if (ret < 0)
goto unlock; goto error_lock;
ret = v4l2_pipeline_pm_get(&video->vdev.entity); ret = v4l2_pipeline_pm_get(&video->video_dev.entity);
if (ret < 0) if (ret < 0)
goto fh_release; goto error_v4l2_fh;
/* check if already powered */
if (!v4l2_fh_is_singular_file(file))
goto unlock;
ret = sun6i_csi_set_power(video->csi_dev, true); /* Power on at first open. */
if (ret < 0) if (v4l2_fh_is_singular_file(file)) {
goto fh_release; ret = sun6i_csi_set_power(video->csi_dev, true);
if (ret < 0)
goto error_v4l2_fh;
}
mutex_unlock(&video->lock); mutex_unlock(&video->lock);
return 0; return 0;
fh_release: error_v4l2_fh:
v4l2_fh_release(file); v4l2_fh_release(file);
unlock:
error_lock:
mutex_unlock(&video->lock); mutex_unlock(&video->lock);
return ret; return ret;
} }
static int sun6i_video_close(struct file *file) static int sun6i_video_close(struct file *file)
{ {
struct sun6i_video *video = video_drvdata(file); struct sun6i_video *video = video_drvdata(file);
bool last_fh; bool last_close;
mutex_lock(&video->lock); mutex_lock(&video->lock);
last_fh = v4l2_fh_is_singular_file(file); last_close = v4l2_fh_is_singular_file(file);
_vb2_fop_release(file, NULL); _vb2_fop_release(file, NULL);
v4l2_pipeline_pm_put(&video->video_dev.entity);
v4l2_pipeline_pm_put(&video->vdev.entity); /* Power off at last close. */
if (last_close)
if (last_fh)
sun6i_csi_set_power(video->csi_dev, false); sun6i_csi_set_power(video->csi_dev, false);
mutex_unlock(&video->lock); mutex_unlock(&video->lock);
...@@ -532,9 +553,8 @@ static const struct v4l2_file_operations sun6i_video_fops = { ...@@ -532,9 +553,8 @@ static const struct v4l2_file_operations sun6i_video_fops = {
.poll = vb2_fop_poll .poll = vb2_fop_poll
}; };
/* ----------------------------------------------------------------------------- /* Media Entity */
* Media Operations
*/
static int sun6i_video_link_validate_get_format(struct media_pad *pad, static int sun6i_video_link_validate_get_format(struct media_pad *pad,
struct v4l2_subdev_format *fmt) struct v4l2_subdev_format *fmt)
{ {
...@@ -571,20 +591,20 @@ static int sun6i_video_link_validate(struct media_link *link) ...@@ -571,20 +591,20 @@ static int sun6i_video_link_validate(struct media_link *link)
return ret; return ret;
if (!sun6i_csi_is_format_supported(video->csi_dev, if (!sun6i_csi_is_format_supported(video->csi_dev,
video->fmt.fmt.pix.pixelformat, video->format.fmt.pix.pixelformat,
source_fmt.format.code)) { source_fmt.format.code)) {
dev_err(video->csi_dev->dev, dev_err(video->csi_dev->dev,
"Unsupported pixformat: 0x%x with mbus code: 0x%x!\n", "Unsupported pixformat: 0x%x with mbus code: 0x%x!\n",
video->fmt.fmt.pix.pixelformat, video->format.fmt.pix.pixelformat,
source_fmt.format.code); source_fmt.format.code);
return -EPIPE; return -EPIPE;
} }
if (source_fmt.format.width != video->fmt.fmt.pix.width || if (source_fmt.format.width != video->format.fmt.pix.width ||
source_fmt.format.height != video->fmt.fmt.pix.height) { source_fmt.format.height != video->format.fmt.pix.height) {
dev_err(video->csi_dev->dev, dev_err(video->csi_dev->dev,
"Wrong width or height %ux%u (%ux%u expected)\n", "Wrong width or height %ux%u (%ux%u expected)\n",
video->fmt.fmt.pix.width, video->fmt.fmt.pix.height, video->format.fmt.pix.width, video->format.fmt.pix.height,
source_fmt.format.width, source_fmt.format.height); source_fmt.format.width, source_fmt.format.height);
return -EPIPE; return -EPIPE;
} }
...@@ -598,90 +618,109 @@ static const struct media_entity_operations sun6i_video_media_ops = { ...@@ -598,90 +618,109 @@ static const struct media_entity_operations sun6i_video_media_ops = {
.link_validate = sun6i_video_link_validate .link_validate = sun6i_video_link_validate
}; };
int sun6i_video_init(struct sun6i_video *video, /* Video */
struct sun6i_csi_device *csi_dev, const char *name)
int sun6i_video_setup(struct sun6i_video *video,
struct sun6i_csi_device *csi_dev)
{ {
struct sun6i_csi_v4l2 *v4l2 = &csi_dev->v4l2; struct v4l2_device *v4l2_dev = &csi_dev->v4l2.v4l2_dev;
struct video_device *vdev = &video->vdev; struct video_device *video_dev = &video->video_dev;
struct vb2_queue *vidq = &video->vb2_vidq; struct vb2_queue *queue = &video->queue;
struct v4l2_format fmt = { 0 }; struct media_pad *pad = &video->pad;
struct v4l2_format format = { 0 };
struct v4l2_pix_format *pix_format = &format.fmt.pix;
int ret; int ret;
video->csi_dev = csi_dev; video->csi_dev = csi_dev;
/* Initialize the media entity... */ /* Media Entity */
video->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
vdev->entity.ops = &sun6i_video_media_ops; video_dev->entity.ops = &sun6i_video_media_ops;
ret = media_entity_pads_init(&vdev->entity, 1, &video->pad);
/* Media Pad */
pad->flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
ret = media_entity_pads_init(&video_dev->entity, 1, pad);
if (ret < 0) if (ret < 0)
return ret; return ret;
mutex_init(&video->lock); /* DMA queue */
INIT_LIST_HEAD(&video->dma_queue); INIT_LIST_HEAD(&video->dma_queue);
spin_lock_init(&video->dma_queue_lock); spin_lock_init(&video->dma_queue_lock);
video->sequence = 0; video->sequence = 0;
/* Setup default format */ /* Queue */
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.pixelformat = supported_pixformats[0]; mutex_init(&video->lock);
fmt.fmt.pix.width = 1280;
fmt.fmt.pix.height = 720; queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.field = V4L2_FIELD_NONE; queue->io_modes = VB2_MMAP | VB2_DMABUF;
sun6i_video_set_fmt(video, &fmt); queue->buf_struct_size = sizeof(struct sun6i_csi_buffer);
queue->ops = &sun6i_video_queue_ops;
/* Initialize videobuf2 queue */ queue->mem_ops = &vb2_dma_contig_memops;
vidq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
vidq->io_modes = VB2_MMAP | VB2_DMABUF; queue->lock = &video->lock;
vidq->drv_priv = video; queue->dev = csi_dev->dev;
vidq->buf_struct_size = sizeof(struct sun6i_csi_buffer); queue->drv_priv = video;
vidq->ops = &sun6i_csi_vb2_ops;
vidq->mem_ops = &vb2_dma_contig_memops; /* Make sure non-dropped frame. */
vidq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; queue->min_buffers_needed = 3;
vidq->lock = &video->lock;
/* Make sure non-dropped frame */ ret = vb2_queue_init(queue);
vidq->min_buffers_needed = 3;
vidq->dev = csi_dev->dev;
ret = vb2_queue_init(vidq);
if (ret) { if (ret) {
v4l2_err(&v4l2->v4l2_dev, "vb2_queue_init failed: %d\n", v4l2_err(v4l2_dev, "failed to initialize vb2 queue: %d\n", ret);
ret); goto error_media_entity;
goto clean_entity;
} }
/* Register video device */ /* V4L2 Format */
strscpy(vdev->name, name, sizeof(vdev->name));
vdev->release = video_device_release_empty; format.type = queue->type;
vdev->fops = &sun6i_video_fops; pix_format->pixelformat = sun6i_video_formats[0];
vdev->ioctl_ops = &sun6i_video_ioctl_ops; pix_format->width = 1280;
vdev->vfl_type = VFL_TYPE_VIDEO; pix_format->height = 720;
vdev->vfl_dir = VFL_DIR_RX; pix_format->field = V4L2_FIELD_NONE;
vdev->v4l2_dev = &v4l2->v4l2_dev;
vdev->queue = vidq; sun6i_video_format_set(video, &format);
vdev->lock = &video->lock;
vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE; /* Video Device */
video_set_drvdata(vdev, video);
strscpy(video_dev->name, SUN6I_CSI_NAME, sizeof(video_dev->name));
ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); video_dev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
video_dev->vfl_dir = VFL_DIR_RX;
video_dev->release = video_device_release_empty;
video_dev->fops = &sun6i_video_fops;
video_dev->ioctl_ops = &sun6i_video_ioctl_ops;
video_dev->v4l2_dev = v4l2_dev;
video_dev->queue = queue;
video_dev->lock = &video->lock;
video_set_drvdata(video_dev, video);
ret = video_register_device(video_dev, VFL_TYPE_VIDEO, -1);
if (ret < 0) { if (ret < 0) {
v4l2_err(&v4l2->v4l2_dev, v4l2_err(v4l2_dev, "failed to register video device: %d\n",
"video_register_device failed: %d\n", ret); ret);
goto clean_entity; goto error_media_entity;
} }
return 0; return 0;
clean_entity: error_media_entity:
media_entity_cleanup(&video->vdev.entity); media_entity_cleanup(&video_dev->entity);
mutex_destroy(&video->lock); mutex_destroy(&video->lock);
return ret; return ret;
} }
void sun6i_video_cleanup(struct sun6i_video *video) void sun6i_video_cleanup(struct sun6i_video *video)
{ {
vb2_video_unregister_device(&video->vdev); struct video_device *video_dev = &video->video_dev;
media_entity_cleanup(&video->vdev.entity);
vb2_video_unregister_device(video_dev);
media_entity_cleanup(&video_dev->entity);
mutex_destroy(&video->lock); mutex_destroy(&video->lock);
} }
...@@ -15,22 +15,22 @@ struct sun6i_csi_device; ...@@ -15,22 +15,22 @@ struct sun6i_csi_device;
struct sun6i_video { struct sun6i_video {
struct sun6i_csi_device *csi_dev; struct sun6i_csi_device *csi_dev;
struct video_device vdev;
struct media_pad pad;
struct mutex lock; struct video_device video_dev;
struct vb2_queue queue;
struct mutex lock; /* Queue lock. */
struct media_pad pad;
struct vb2_queue vb2_vidq;
spinlock_t dma_queue_lock;
struct list_head dma_queue; struct list_head dma_queue;
spinlock_t dma_queue_lock; /* DMA queue lock. */
unsigned int sequence; struct v4l2_format format;
struct v4l2_format fmt;
u32 mbus_code; u32 mbus_code;
unsigned int sequence;
}; };
int sun6i_video_init(struct sun6i_video *video, int sun6i_video_setup(struct sun6i_video *video,
struct sun6i_csi_device *csi_dev, const char *name); struct sun6i_csi_device *csi_dev);
void sun6i_video_cleanup(struct sun6i_video *video); void sun6i_video_cleanup(struct sun6i_video *video);
void sun6i_video_frame_done(struct sun6i_video *video); void sun6i_video_frame_done(struct sun6i_video *video);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment