1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
6 * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
7 * Copyright (C) 2018 Bootlin
9 * Based on the vim2m driver, that is:
11 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
12 * Pawel Osciak, <pawel@osciak.com>
13 * Marek Szyprowski, <m.szyprowski@samsung.com>
16 #include <media/videobuf2-dma-contig.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-ioctl.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-mem2mem.h>
23 #include "cedrus_video.h"
24 #include "cedrus_dec.h"
25 #include "cedrus_hw.h"
27 #define CEDRUS_DECODE_SRC BIT(0)
28 #define CEDRUS_DECODE_DST BIT(1)
30 #define CEDRUS_MIN_WIDTH 16U
31 #define CEDRUS_MIN_HEIGHT 16U
32 #define CEDRUS_MAX_WIDTH 3840U
33 #define CEDRUS_MAX_HEIGHT 2160U
35 static struct cedrus_format cedrus_formats[] = {
37 .pixelformat = V4L2_PIX_FMT_MPEG2_SLICE,
38 .directions = CEDRUS_DECODE_SRC,
41 .pixelformat = V4L2_PIX_FMT_H264_SLICE_RAW,
42 .directions = CEDRUS_DECODE_SRC,
45 .pixelformat = V4L2_PIX_FMT_SUNXI_TILED_NV12,
46 .directions = CEDRUS_DECODE_DST,
49 .pixelformat = V4L2_PIX_FMT_NV12,
50 .directions = CEDRUS_DECODE_DST,
51 .capabilities = CEDRUS_CAPABILITY_UNTILED,
55 #define CEDRUS_FORMATS_COUNT ARRAY_SIZE(cedrus_formats)
57 static inline struct cedrus_ctx *cedrus_file2ctx(struct file *file)
59 return container_of(file->private_data, struct cedrus_ctx, fh);
62 static struct cedrus_format *cedrus_find_format(u32 pixelformat, u32 directions,
63 unsigned int capabilities)
65 struct cedrus_format *fmt;
68 for (i = 0; i < CEDRUS_FORMATS_COUNT; i++) {
69 fmt = &cedrus_formats[i];
71 if (fmt->capabilities && (fmt->capabilities & capabilities) !=
75 if (fmt->pixelformat == pixelformat &&
76 (fmt->directions & directions) != 0)
80 if (i == CEDRUS_FORMATS_COUNT)
83 return &cedrus_formats[i];
86 static bool cedrus_check_format(u32 pixelformat, u32 directions,
87 unsigned int capabilities)
89 return cedrus_find_format(pixelformat, directions, capabilities);
92 static void cedrus_prepare_format(struct v4l2_pix_format *pix_fmt)
94 unsigned int width = pix_fmt->width;
95 unsigned int height = pix_fmt->height;
96 unsigned int sizeimage = pix_fmt->sizeimage;
97 unsigned int bytesperline = pix_fmt->bytesperline;
99 pix_fmt->field = V4L2_FIELD_NONE;
101 /* Limit to hardware min/max. */
102 width = clamp(width, CEDRUS_MIN_WIDTH, CEDRUS_MAX_WIDTH);
103 height = clamp(height, CEDRUS_MIN_HEIGHT, CEDRUS_MAX_HEIGHT);
105 switch (pix_fmt->pixelformat) {
106 case V4L2_PIX_FMT_MPEG2_SLICE:
107 case V4L2_PIX_FMT_H264_SLICE_RAW:
108 /* Zero bytes per line for encoded source. */
113 case V4L2_PIX_FMT_SUNXI_TILED_NV12:
114 /* 32-aligned stride. */
115 bytesperline = ALIGN(width, 32);
117 /* 32-aligned height. */
118 height = ALIGN(height, 32);
120 /* Luma plane size. */
121 sizeimage = bytesperline * height;
123 /* Chroma plane size. */
124 sizeimage += bytesperline * height / 2;
128 case V4L2_PIX_FMT_NV12:
129 /* 16-aligned stride. */
130 bytesperline = ALIGN(width, 16);
132 /* 16-aligned height. */
133 height = ALIGN(height, 16);
135 /* Luma plane size. */
136 sizeimage = bytesperline * height;
138 /* Chroma plane size. */
139 sizeimage += bytesperline * height / 2;
144 pix_fmt->width = width;
145 pix_fmt->height = height;
147 pix_fmt->bytesperline = bytesperline;
148 pix_fmt->sizeimage = sizeimage;
151 static int cedrus_querycap(struct file *file, void *priv,
152 struct v4l2_capability *cap)
154 strscpy(cap->driver, CEDRUS_NAME, sizeof(cap->driver));
155 strscpy(cap->card, CEDRUS_NAME, sizeof(cap->card));
156 snprintf(cap->bus_info, sizeof(cap->bus_info),
157 "platform:%s", CEDRUS_NAME);
162 static int cedrus_enum_fmt(struct file *file, struct v4l2_fmtdesc *f,
165 struct cedrus_ctx *ctx = cedrus_file2ctx(file);
166 struct cedrus_dev *dev = ctx->dev;
167 unsigned int capabilities = dev->capabilities;
168 struct cedrus_format *fmt;
169 unsigned int i, index;
171 /* Index among formats that match the requested direction. */
174 for (i = 0; i < CEDRUS_FORMATS_COUNT; i++) {
175 fmt = &cedrus_formats[i];
177 if (fmt->capabilities && (fmt->capabilities & capabilities) !=
181 if (!(cedrus_formats[i].directions & direction))
184 if (index == f->index)
190 /* Matched format. */
191 if (i < CEDRUS_FORMATS_COUNT) {
192 f->pixelformat = cedrus_formats[i].pixelformat;
200 static int cedrus_enum_fmt_vid_cap(struct file *file, void *priv,
201 struct v4l2_fmtdesc *f)
203 return cedrus_enum_fmt(file, f, CEDRUS_DECODE_DST);
206 static int cedrus_enum_fmt_vid_out(struct file *file, void *priv,
207 struct v4l2_fmtdesc *f)
209 return cedrus_enum_fmt(file, f, CEDRUS_DECODE_SRC);
212 static int cedrus_g_fmt_vid_cap(struct file *file, void *priv,
213 struct v4l2_format *f)
215 struct cedrus_ctx *ctx = cedrus_file2ctx(file);
217 /* Fall back to dummy default by lack of hardware configuration. */
218 if (!ctx->dst_fmt.width || !ctx->dst_fmt.height) {
219 f->fmt.pix.pixelformat = V4L2_PIX_FMT_SUNXI_TILED_NV12;
220 cedrus_prepare_format(&f->fmt.pix);
225 f->fmt.pix = ctx->dst_fmt;
230 static int cedrus_g_fmt_vid_out(struct file *file, void *priv,
231 struct v4l2_format *f)
233 struct cedrus_ctx *ctx = cedrus_file2ctx(file);
235 /* Fall back to dummy default by lack of hardware configuration. */
236 if (!ctx->dst_fmt.width || !ctx->dst_fmt.height) {
237 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG2_SLICE;
238 f->fmt.pix.sizeimage = SZ_1K;
239 cedrus_prepare_format(&f->fmt.pix);
244 f->fmt.pix = ctx->src_fmt;
249 static int cedrus_try_fmt_vid_cap(struct file *file, void *priv,
250 struct v4l2_format *f)
252 struct cedrus_ctx *ctx = cedrus_file2ctx(file);
253 struct cedrus_dev *dev = ctx->dev;
254 struct v4l2_pix_format *pix_fmt = &f->fmt.pix;
256 if (!cedrus_check_format(pix_fmt->pixelformat, CEDRUS_DECODE_DST,
260 cedrus_prepare_format(pix_fmt);
265 static int cedrus_try_fmt_vid_out(struct file *file, void *priv,
266 struct v4l2_format *f)
268 struct cedrus_ctx *ctx = cedrus_file2ctx(file);
269 struct cedrus_dev *dev = ctx->dev;
270 struct v4l2_pix_format *pix_fmt = &f->fmt.pix;
272 if (!cedrus_check_format(pix_fmt->pixelformat, CEDRUS_DECODE_SRC,
276 /* Source image size has to be provided by userspace. */
277 if (pix_fmt->sizeimage == 0)
280 cedrus_prepare_format(pix_fmt);
285 static int cedrus_s_fmt_vid_cap(struct file *file, void *priv,
286 struct v4l2_format *f)
288 struct cedrus_ctx *ctx = cedrus_file2ctx(file);
289 struct cedrus_dev *dev = ctx->dev;
290 struct vb2_queue *vq;
293 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
297 ret = cedrus_try_fmt_vid_cap(file, priv, f);
301 ctx->dst_fmt = f->fmt.pix;
303 cedrus_dst_format_set(dev, &ctx->dst_fmt);
308 static int cedrus_s_fmt_vid_out(struct file *file, void *priv,
309 struct v4l2_format *f)
311 struct cedrus_ctx *ctx = cedrus_file2ctx(file);
312 struct vb2_queue *vq;
315 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
319 ret = cedrus_try_fmt_vid_out(file, priv, f);
323 ctx->src_fmt = f->fmt.pix;
325 /* Propagate colorspace information to capture. */
326 ctx->dst_fmt.colorspace = f->fmt.pix.colorspace;
327 ctx->dst_fmt.xfer_func = f->fmt.pix.xfer_func;
328 ctx->dst_fmt.ycbcr_enc = f->fmt.pix.ycbcr_enc;
329 ctx->dst_fmt.quantization = f->fmt.pix.quantization;
334 const struct v4l2_ioctl_ops cedrus_ioctl_ops = {
335 .vidioc_querycap = cedrus_querycap,
337 .vidioc_enum_fmt_vid_cap = cedrus_enum_fmt_vid_cap,
338 .vidioc_g_fmt_vid_cap = cedrus_g_fmt_vid_cap,
339 .vidioc_try_fmt_vid_cap = cedrus_try_fmt_vid_cap,
340 .vidioc_s_fmt_vid_cap = cedrus_s_fmt_vid_cap,
342 .vidioc_enum_fmt_vid_out = cedrus_enum_fmt_vid_out,
343 .vidioc_g_fmt_vid_out = cedrus_g_fmt_vid_out,
344 .vidioc_try_fmt_vid_out = cedrus_try_fmt_vid_out,
345 .vidioc_s_fmt_vid_out = cedrus_s_fmt_vid_out,
347 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
348 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
349 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
350 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
351 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
352 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
353 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
355 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
356 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
358 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
359 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
362 static int cedrus_queue_setup(struct vb2_queue *vq, unsigned int *nbufs,
363 unsigned int *nplanes, unsigned int sizes[],
364 struct device *alloc_devs[])
366 struct cedrus_ctx *ctx = vb2_get_drv_priv(vq);
367 struct cedrus_dev *dev = ctx->dev;
368 struct v4l2_pix_format *pix_fmt;
371 if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
372 directions = CEDRUS_DECODE_SRC;
373 pix_fmt = &ctx->src_fmt;
375 directions = CEDRUS_DECODE_DST;
376 pix_fmt = &ctx->dst_fmt;
379 if (!cedrus_check_format(pix_fmt->pixelformat, directions,
384 if (sizes[0] < pix_fmt->sizeimage)
387 sizes[0] = pix_fmt->sizeimage;
394 static void cedrus_queue_cleanup(struct vb2_queue *vq, u32 state)
396 struct cedrus_ctx *ctx = vb2_get_drv_priv(vq);
397 struct vb2_v4l2_buffer *vbuf;
400 if (V4L2_TYPE_IS_OUTPUT(vq->type))
401 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
403 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
408 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
410 v4l2_m2m_buf_done(vbuf, state);
414 static int cedrus_buf_init(struct vb2_buffer *vb)
416 struct vb2_queue *vq = vb->vb2_queue;
417 struct cedrus_ctx *ctx = vb2_get_drv_priv(vq);
419 if (!V4L2_TYPE_IS_OUTPUT(vq->type))
420 ctx->dst_bufs[vb->index] = vb;
425 static void cedrus_buf_cleanup(struct vb2_buffer *vb)
427 struct vb2_queue *vq = vb->vb2_queue;
428 struct cedrus_ctx *ctx = vb2_get_drv_priv(vq);
430 if (!V4L2_TYPE_IS_OUTPUT(vq->type))
431 ctx->dst_bufs[vb->index] = NULL;
434 static int cedrus_buf_out_validate(struct vb2_buffer *vb)
436 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
438 vbuf->field = V4L2_FIELD_NONE;
442 static int cedrus_buf_prepare(struct vb2_buffer *vb)
444 struct vb2_queue *vq = vb->vb2_queue;
445 struct cedrus_ctx *ctx = vb2_get_drv_priv(vq);
446 struct v4l2_pix_format *pix_fmt;
448 if (V4L2_TYPE_IS_OUTPUT(vq->type))
449 pix_fmt = &ctx->src_fmt;
451 pix_fmt = &ctx->dst_fmt;
453 if (vb2_plane_size(vb, 0) < pix_fmt->sizeimage)
456 vb2_set_plane_payload(vb, 0, pix_fmt->sizeimage);
461 static int cedrus_start_streaming(struct vb2_queue *vq, unsigned int count)
463 struct cedrus_ctx *ctx = vb2_get_drv_priv(vq);
464 struct cedrus_dev *dev = ctx->dev;
467 switch (ctx->src_fmt.pixelformat) {
468 case V4L2_PIX_FMT_MPEG2_SLICE:
469 ctx->current_codec = CEDRUS_CODEC_MPEG2;
472 case V4L2_PIX_FMT_H264_SLICE_RAW:
473 ctx->current_codec = CEDRUS_CODEC_H264;
480 if (V4L2_TYPE_IS_OUTPUT(vq->type) &&
481 dev->dec_ops[ctx->current_codec]->start)
482 ret = dev->dec_ops[ctx->current_codec]->start(ctx);
485 cedrus_queue_cleanup(vq, VB2_BUF_STATE_QUEUED);
490 static void cedrus_stop_streaming(struct vb2_queue *vq)
492 struct cedrus_ctx *ctx = vb2_get_drv_priv(vq);
493 struct cedrus_dev *dev = ctx->dev;
495 if (V4L2_TYPE_IS_OUTPUT(vq->type) &&
496 dev->dec_ops[ctx->current_codec]->stop)
497 dev->dec_ops[ctx->current_codec]->stop(ctx);
499 cedrus_queue_cleanup(vq, VB2_BUF_STATE_ERROR);
502 static void cedrus_buf_queue(struct vb2_buffer *vb)
504 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
505 struct cedrus_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
507 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
510 static void cedrus_buf_request_complete(struct vb2_buffer *vb)
512 struct cedrus_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
514 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
517 static struct vb2_ops cedrus_qops = {
518 .queue_setup = cedrus_queue_setup,
519 .buf_prepare = cedrus_buf_prepare,
520 .buf_init = cedrus_buf_init,
521 .buf_cleanup = cedrus_buf_cleanup,
522 .buf_queue = cedrus_buf_queue,
523 .buf_out_validate = cedrus_buf_out_validate,
524 .buf_request_complete = cedrus_buf_request_complete,
525 .start_streaming = cedrus_start_streaming,
526 .stop_streaming = cedrus_stop_streaming,
527 .wait_prepare = vb2_ops_wait_prepare,
528 .wait_finish = vb2_ops_wait_finish,
531 int cedrus_queue_init(void *priv, struct vb2_queue *src_vq,
532 struct vb2_queue *dst_vq)
534 struct cedrus_ctx *ctx = priv;
537 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
538 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
539 src_vq->drv_priv = ctx;
540 src_vq->buf_struct_size = sizeof(struct cedrus_buffer);
541 src_vq->min_buffers_needed = 1;
542 src_vq->ops = &cedrus_qops;
543 src_vq->mem_ops = &vb2_dma_contig_memops;
544 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
545 src_vq->lock = &ctx->dev->dev_mutex;
546 src_vq->dev = ctx->dev->dev;
547 src_vq->supports_requests = true;
548 src_vq->requires_requests = true;
550 ret = vb2_queue_init(src_vq);
554 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
555 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
556 dst_vq->drv_priv = ctx;
557 dst_vq->buf_struct_size = sizeof(struct cedrus_buffer);
558 dst_vq->min_buffers_needed = 1;
559 dst_vq->ops = &cedrus_qops;
560 dst_vq->mem_ops = &vb2_dma_contig_memops;
561 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
562 dst_vq->lock = &ctx->dev->dev_mutex;
563 dst_vq->dev = ctx->dev->dev;
565 return vb2_queue_init(dst_vq);