1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * i.MX IPUv3 IC PP mem2mem CSC/Scaler driver
5 * Copyright (C) 2011 Pengutronix, Sascha Hauer
6 * Copyright (C) 2018 Pengutronix, Philipp Zabel
8 #include <linux/module.h>
9 #include <linux/delay.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <video/imx-ipu-v3.h>
14 #include <video/imx-ipu-image-convert.h>
16 #include <media/media-device.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-mem2mem.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-dma-contig.h>
24 #include "imx-media.h"
26 #define fh_to_ctx(__fh) container_of(__fh, struct ipu_csc_scaler_ctx, fh)
33 struct ipu_csc_scaler_priv {
34 struct imx_media_video_dev vdev;
36 struct v4l2_m2m_dev *m2m_dev;
39 struct imx_media_dev *md;
41 struct mutex mutex; /* mem2mem device mutex */
44 #define vdev_to_priv(v) container_of(v, struct ipu_csc_scaler_priv, vdev)
46 /* Per-queue, driver-specific private data */
47 struct ipu_csc_scaler_q_data {
48 struct v4l2_pix_format cur_fmt;
49 struct v4l2_rect rect;
52 struct ipu_csc_scaler_ctx {
53 struct ipu_csc_scaler_priv *priv;
56 struct ipu_csc_scaler_q_data q_data[2];
57 struct ipu_image_convert_ctx *icc;
59 struct v4l2_ctrl_handler ctrl_hdlr;
63 enum ipu_rotate_mode rot_mode;
64 unsigned int sequence;
67 static struct ipu_csc_scaler_q_data *get_q_data(struct ipu_csc_scaler_ctx *ctx,
68 enum v4l2_buf_type type)
70 if (V4L2_TYPE_IS_OUTPUT(type))
71 return &ctx->q_data[V4L2_M2M_SRC];
73 return &ctx->q_data[V4L2_M2M_DST];
80 static void job_abort(void *_ctx)
82 struct ipu_csc_scaler_ctx *ctx = _ctx;
85 ipu_image_convert_abort(ctx->icc);
88 static void ipu_ic_pp_complete(struct ipu_image_convert_run *run, void *_ctx)
90 struct ipu_csc_scaler_ctx *ctx = _ctx;
91 struct ipu_csc_scaler_priv *priv = ctx->priv;
92 struct vb2_v4l2_buffer *src_buf, *dst_buf;
94 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
95 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
97 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
99 src_buf->sequence = ctx->sequence++;
100 dst_buf->sequence = src_buf->sequence;
102 v4l2_m2m_buf_done(src_buf, run->status ? VB2_BUF_STATE_ERROR :
104 v4l2_m2m_buf_done(dst_buf, run->status ? VB2_BUF_STATE_ERROR :
107 v4l2_m2m_job_finish(priv->m2m_dev, ctx->fh.m2m_ctx);
111 static void device_run(void *_ctx)
113 struct ipu_csc_scaler_ctx *ctx = _ctx;
114 struct ipu_csc_scaler_priv *priv = ctx->priv;
115 struct vb2_v4l2_buffer *src_buf, *dst_buf;
116 struct ipu_image_convert_run *run;
119 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
120 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
122 run = kzalloc(sizeof(*run), GFP_KERNEL);
127 run->in_phys = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
128 run->out_phys = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
130 ret = ipu_image_convert_queue(run);
132 v4l2_err(ctx->priv->vdev.vfd->v4l2_dev,
133 "%s: failed to queue: %d\n", __func__, ret);
140 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
141 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
142 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
143 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
144 v4l2_m2m_job_finish(priv->m2m_dev, ctx->fh.m2m_ctx);
150 static int ipu_csc_scaler_querycap(struct file *file, void *priv,
151 struct v4l2_capability *cap)
153 strscpy(cap->driver, "imx-media-csc-scaler", sizeof(cap->driver));
154 strscpy(cap->card, "imx-media-csc-scaler", sizeof(cap->card));
155 strscpy(cap->bus_info, "platform:imx-media-csc-scaler",
156 sizeof(cap->bus_info));
161 static int ipu_csc_scaler_enum_fmt(struct file *file, void *fh,
162 struct v4l2_fmtdesc *f)
167 ret = imx_media_enum_format(&fourcc, f->index, CS_SEL_ANY);
171 f->pixelformat = fourcc;
176 static int ipu_csc_scaler_g_fmt(struct file *file, void *priv,
177 struct v4l2_format *f)
179 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
180 struct ipu_csc_scaler_q_data *q_data;
182 q_data = get_q_data(ctx, f->type);
184 f->fmt.pix = q_data->cur_fmt;
189 static int ipu_csc_scaler_try_fmt(struct file *file, void *priv,
190 struct v4l2_format *f)
192 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
193 struct ipu_csc_scaler_q_data *q_data = get_q_data(ctx, f->type);
194 struct ipu_image test_in, test_out;
195 enum v4l2_field field;
197 field = f->fmt.pix.field;
198 if (field == V4L2_FIELD_ANY)
199 field = V4L2_FIELD_NONE;
200 else if (field != V4L2_FIELD_NONE)
203 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
204 struct ipu_csc_scaler_q_data *q_data_in =
205 get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
207 test_out.pix = f->fmt.pix;
208 test_in.pix = q_data_in->cur_fmt;
210 struct ipu_csc_scaler_q_data *q_data_out =
211 get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
213 test_in.pix = f->fmt.pix;
214 test_out.pix = q_data_out->cur_fmt;
217 ipu_image_convert_adjust(&test_in, &test_out, ctx->rot_mode);
219 f->fmt.pix = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
220 test_out.pix : test_in.pix;
222 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
223 f->fmt.pix.colorspace = q_data->cur_fmt.colorspace;
224 f->fmt.pix.ycbcr_enc = q_data->cur_fmt.ycbcr_enc;
225 f->fmt.pix.xfer_func = q_data->cur_fmt.xfer_func;
226 f->fmt.pix.quantization = q_data->cur_fmt.quantization;
227 } else if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT) {
228 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
229 f->fmt.pix.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
230 f->fmt.pix.xfer_func = V4L2_XFER_FUNC_DEFAULT;
231 f->fmt.pix.quantization = V4L2_QUANTIZATION_DEFAULT;
237 static int ipu_csc_scaler_s_fmt(struct file *file, void *priv,
238 struct v4l2_format *f)
240 struct ipu_csc_scaler_q_data *q_data;
241 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
242 struct vb2_queue *vq;
245 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
246 if (vb2_is_busy(vq)) {
247 v4l2_err(ctx->priv->vdev.vfd->v4l2_dev, "%s: queue busy\n",
252 q_data = get_q_data(ctx, f->type);
254 ret = ipu_csc_scaler_try_fmt(file, priv, f);
258 q_data->cur_fmt.width = f->fmt.pix.width;
259 q_data->cur_fmt.height = f->fmt.pix.height;
260 q_data->cur_fmt.pixelformat = f->fmt.pix.pixelformat;
261 q_data->cur_fmt.field = f->fmt.pix.field;
262 q_data->cur_fmt.bytesperline = f->fmt.pix.bytesperline;
263 q_data->cur_fmt.sizeimage = f->fmt.pix.sizeimage;
265 /* Reset cropping/composing rectangle */
266 q_data->rect.left = 0;
267 q_data->rect.top = 0;
268 q_data->rect.width = q_data->cur_fmt.width;
269 q_data->rect.height = q_data->cur_fmt.height;
271 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
272 /* Set colorimetry on the output queue */
273 q_data->cur_fmt.colorspace = f->fmt.pix.colorspace;
274 q_data->cur_fmt.ycbcr_enc = f->fmt.pix.ycbcr_enc;
275 q_data->cur_fmt.xfer_func = f->fmt.pix.xfer_func;
276 q_data->cur_fmt.quantization = f->fmt.pix.quantization;
277 /* Propagate colorimetry to the capture queue */
278 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
279 q_data->cur_fmt.colorspace = f->fmt.pix.colorspace;
280 q_data->cur_fmt.ycbcr_enc = f->fmt.pix.ycbcr_enc;
281 q_data->cur_fmt.xfer_func = f->fmt.pix.xfer_func;
282 q_data->cur_fmt.quantization = f->fmt.pix.quantization;
286 * TODO: Setting colorimetry on the capture queue is currently not
287 * supported by the V4L2 API
293 static int ipu_csc_scaler_g_selection(struct file *file, void *priv,
294 struct v4l2_selection *s)
296 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
297 struct ipu_csc_scaler_q_data *q_data;
300 case V4L2_SEL_TGT_CROP:
301 case V4L2_SEL_TGT_CROP_DEFAULT:
302 case V4L2_SEL_TGT_CROP_BOUNDS:
303 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
305 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
307 case V4L2_SEL_TGT_COMPOSE:
308 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
309 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
310 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
312 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
318 if (s->target == V4L2_SEL_TGT_CROP ||
319 s->target == V4L2_SEL_TGT_COMPOSE) {
324 s->r.width = q_data->cur_fmt.width;
325 s->r.height = q_data->cur_fmt.height;
331 static int ipu_csc_scaler_s_selection(struct file *file, void *priv,
332 struct v4l2_selection *s)
334 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
335 struct ipu_csc_scaler_q_data *q_data;
338 case V4L2_SEL_TGT_CROP:
339 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
342 case V4L2_SEL_TGT_COMPOSE:
343 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
350 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
351 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
354 q_data = get_q_data(ctx, s->type);
356 /* The input's frame width to the IC must be a multiple of 8 pixels
357 * When performing resizing the frame width must be multiple of burst
358 * size - 8 or 16 pixels as defined by CB#_BURST_16 parameter.
360 if (s->flags & V4L2_SEL_FLAG_GE)
361 s->r.width = round_up(s->r.width, 8);
362 if (s->flags & V4L2_SEL_FLAG_LE)
363 s->r.width = round_down(s->r.width, 8);
364 s->r.width = clamp_t(unsigned int, s->r.width, 8,
365 round_down(q_data->cur_fmt.width, 8));
366 s->r.height = clamp_t(unsigned int, s->r.height, 1,
367 q_data->cur_fmt.height);
368 s->r.left = clamp_t(unsigned int, s->r.left, 0,
369 q_data->cur_fmt.width - s->r.width);
370 s->r.top = clamp_t(unsigned int, s->r.top, 0,
371 q_data->cur_fmt.height - s->r.height);
373 /* V4L2_SEL_FLAG_KEEP_CONFIG is only valid for subdevices */
379 static const struct v4l2_ioctl_ops ipu_csc_scaler_ioctl_ops = {
380 .vidioc_querycap = ipu_csc_scaler_querycap,
382 .vidioc_enum_fmt_vid_cap = ipu_csc_scaler_enum_fmt,
383 .vidioc_g_fmt_vid_cap = ipu_csc_scaler_g_fmt,
384 .vidioc_try_fmt_vid_cap = ipu_csc_scaler_try_fmt,
385 .vidioc_s_fmt_vid_cap = ipu_csc_scaler_s_fmt,
387 .vidioc_enum_fmt_vid_out = ipu_csc_scaler_enum_fmt,
388 .vidioc_g_fmt_vid_out = ipu_csc_scaler_g_fmt,
389 .vidioc_try_fmt_vid_out = ipu_csc_scaler_try_fmt,
390 .vidioc_s_fmt_vid_out = ipu_csc_scaler_s_fmt,
392 .vidioc_g_selection = ipu_csc_scaler_g_selection,
393 .vidioc_s_selection = ipu_csc_scaler_s_selection,
395 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
396 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
398 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
399 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
400 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
401 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
402 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
404 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
405 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
407 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
408 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
415 static int ipu_csc_scaler_queue_setup(struct vb2_queue *vq,
416 unsigned int *nbuffers,
417 unsigned int *nplanes,
418 unsigned int sizes[],
419 struct device *alloc_devs[])
421 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vq);
422 struct ipu_csc_scaler_q_data *q_data;
423 unsigned int size, count = *nbuffers;
425 q_data = get_q_data(ctx, vq->type);
427 size = q_data->cur_fmt.sizeimage;
432 return sizes[0] < size ? -EINVAL : 0;
437 dev_dbg(ctx->priv->dev, "get %d buffer(s) of size %d each.\n",
443 static int ipu_csc_scaler_buf_prepare(struct vb2_buffer *vb)
445 struct vb2_queue *vq = vb->vb2_queue;
446 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
447 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vq);
448 struct ipu_csc_scaler_q_data *q_data;
451 dev_dbg(ctx->priv->dev, "type: %d\n", vq->type);
453 if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
454 if (vbuf->field == V4L2_FIELD_ANY)
455 vbuf->field = V4L2_FIELD_NONE;
456 if (vbuf->field != V4L2_FIELD_NONE) {
457 dev_dbg(ctx->priv->dev, "%s: field isn't supported\n",
463 q_data = get_q_data(ctx, vq->type);
464 size = q_data->cur_fmt.sizeimage;
466 if (vb2_plane_size(vb, 0) < size) {
467 dev_dbg(ctx->priv->dev,
468 "%s: data will not fit into plane (%lu < %lu)\n",
469 __func__, vb2_plane_size(vb, 0), size);
473 vb2_set_plane_payload(vb, 0, size);
478 static void ipu_csc_scaler_buf_queue(struct vb2_buffer *vb)
480 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
482 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
485 static void ipu_image_from_q_data(struct ipu_image *im,
486 struct ipu_csc_scaler_q_data *q_data)
488 struct v4l2_pix_format *fmt = &q_data->cur_fmt;
491 if (fmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
492 im->pix.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
493 if (fmt->quantization == V4L2_QUANTIZATION_DEFAULT)
494 im->pix.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
495 im->rect = q_data->rect;
498 static int ipu_csc_scaler_start_streaming(struct vb2_queue *q,
501 const enum ipu_ic_task ic_task = IC_TASK_POST_PROCESSOR;
502 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(q);
503 struct ipu_csc_scaler_priv *priv = ctx->priv;
504 struct ipu_soc *ipu = priv->md->ipu[0];
505 struct ipu_csc_scaler_q_data *q_data;
506 struct vb2_queue *other_q;
507 struct ipu_image in, out;
509 other_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
510 (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
511 V4L2_BUF_TYPE_VIDEO_OUTPUT :
512 V4L2_BUF_TYPE_VIDEO_CAPTURE);
513 if (!vb2_is_streaming(other_q))
517 v4l2_warn(ctx->priv->vdev.vfd->v4l2_dev, "removing old ICC\n");
518 ipu_image_convert_unprepare(ctx->icc);
521 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
522 ipu_image_from_q_data(&in, q_data);
524 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
525 ipu_image_from_q_data(&out, q_data);
527 ctx->icc = ipu_image_convert_prepare(ipu, ic_task, &in, &out,
529 ipu_ic_pp_complete, ctx);
530 if (IS_ERR(ctx->icc)) {
531 struct vb2_v4l2_buffer *buf;
532 int ret = PTR_ERR(ctx->icc);
535 v4l2_err(ctx->priv->vdev.vfd->v4l2_dev, "%s: error %d\n",
537 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
538 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
539 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
540 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
547 static void ipu_csc_scaler_stop_streaming(struct vb2_queue *q)
549 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(q);
550 struct vb2_v4l2_buffer *buf;
553 ipu_image_convert_unprepare(ctx->icc);
559 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
560 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
561 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
563 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
564 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
568 static const struct vb2_ops ipu_csc_scaler_qops = {
569 .queue_setup = ipu_csc_scaler_queue_setup,
570 .buf_prepare = ipu_csc_scaler_buf_prepare,
571 .buf_queue = ipu_csc_scaler_buf_queue,
572 .wait_prepare = vb2_ops_wait_prepare,
573 .wait_finish = vb2_ops_wait_finish,
574 .start_streaming = ipu_csc_scaler_start_streaming,
575 .stop_streaming = ipu_csc_scaler_stop_streaming,
578 static int ipu_csc_scaler_queue_init(void *priv, struct vb2_queue *src_vq,
579 struct vb2_queue *dst_vq)
581 struct ipu_csc_scaler_ctx *ctx = priv;
584 memset(src_vq, 0, sizeof(*src_vq));
585 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
586 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
587 src_vq->drv_priv = ctx;
588 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
589 src_vq->ops = &ipu_csc_scaler_qops;
590 src_vq->mem_ops = &vb2_dma_contig_memops;
591 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
592 src_vq->lock = &ctx->priv->mutex;
593 src_vq->dev = ctx->priv->dev;
595 ret = vb2_queue_init(src_vq);
599 memset(dst_vq, 0, sizeof(*dst_vq));
600 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
601 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
602 dst_vq->drv_priv = ctx;
603 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
604 dst_vq->ops = &ipu_csc_scaler_qops;
605 dst_vq->mem_ops = &vb2_dma_contig_memops;
606 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
607 dst_vq->lock = &ctx->priv->mutex;
608 dst_vq->dev = ctx->priv->dev;
610 return vb2_queue_init(dst_vq);
613 static int ipu_csc_scaler_s_ctrl(struct v4l2_ctrl *ctrl)
615 struct ipu_csc_scaler_ctx *ctx = container_of(ctrl->handler,
616 struct ipu_csc_scaler_ctx,
618 enum ipu_rotate_mode rot_mode;
623 rotate = ctx->rotate;
634 case V4L2_CID_ROTATE:
641 ret = ipu_degrees_to_rot_mode(&rot_mode, rotate, hflip, vflip);
645 if (rot_mode != ctx->rot_mode) {
646 struct v4l2_pix_format *in_fmt, *out_fmt;
647 struct ipu_image test_in, test_out;
649 in_fmt = &ctx->q_data[V4L2_M2M_SRC].cur_fmt;
650 out_fmt = &ctx->q_data[V4L2_M2M_DST].cur_fmt;
652 test_in.pix = *in_fmt;
653 test_out.pix = *out_fmt;
655 if (ipu_rot_mode_is_irt(rot_mode) !=
656 ipu_rot_mode_is_irt(ctx->rot_mode)) {
657 /* Switch width & height to keep aspect ratio intact */
658 test_out.pix.width = out_fmt->height;
659 test_out.pix.height = out_fmt->width;
662 ipu_image_convert_adjust(&test_in, &test_out, ctx->rot_mode);
664 /* Check if output format needs to be changed */
665 if (test_in.pix.width != in_fmt->width ||
666 test_in.pix.height != in_fmt->height ||
667 test_in.pix.bytesperline != in_fmt->bytesperline ||
668 test_in.pix.sizeimage != in_fmt->sizeimage) {
669 struct vb2_queue *out_q;
671 out_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
672 V4L2_BUF_TYPE_VIDEO_OUTPUT);
673 if (vb2_is_busy(out_q))
677 /* Check if capture format needs to be changed */
678 if (test_out.pix.width != out_fmt->width ||
679 test_out.pix.height != out_fmt->height ||
680 test_out.pix.bytesperline != out_fmt->bytesperline ||
681 test_out.pix.sizeimage != out_fmt->sizeimage) {
682 struct vb2_queue *cap_q;
684 cap_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
685 V4L2_BUF_TYPE_VIDEO_CAPTURE);
686 if (vb2_is_busy(cap_q))
690 *in_fmt = test_in.pix;
691 *out_fmt = test_out.pix;
693 ctx->rot_mode = rot_mode;
694 ctx->rotate = rotate;
702 static const struct v4l2_ctrl_ops ipu_csc_scaler_ctrl_ops = {
703 .s_ctrl = ipu_csc_scaler_s_ctrl,
706 static int ipu_csc_scaler_init_controls(struct ipu_csc_scaler_ctx *ctx)
708 struct v4l2_ctrl_handler *hdlr = &ctx->ctrl_hdlr;
710 v4l2_ctrl_handler_init(hdlr, 3);
712 v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_HFLIP,
714 v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_VFLIP,
716 v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_ROTATE,
720 v4l2_ctrl_handler_free(hdlr);
724 v4l2_ctrl_handler_setup(hdlr);
728 #define DEFAULT_WIDTH 720
729 #define DEFAULT_HEIGHT 576
730 static const struct ipu_csc_scaler_q_data ipu_csc_scaler_q_data_default = {
732 .width = DEFAULT_WIDTH,
733 .height = DEFAULT_HEIGHT,
734 .pixelformat = V4L2_PIX_FMT_YUV420,
735 .field = V4L2_FIELD_NONE,
736 .bytesperline = DEFAULT_WIDTH,
737 .sizeimage = DEFAULT_WIDTH * DEFAULT_HEIGHT * 3 / 2,
738 .colorspace = V4L2_COLORSPACE_SRGB,
741 .width = DEFAULT_WIDTH,
742 .height = DEFAULT_HEIGHT,
749 static int ipu_csc_scaler_open(struct file *file)
751 struct ipu_csc_scaler_priv *priv = video_drvdata(file);
752 struct ipu_csc_scaler_ctx *ctx = NULL;
755 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
759 ctx->rot_mode = IPU_ROTATE_NONE;
761 v4l2_fh_init(&ctx->fh, video_devdata(file));
762 file->private_data = &ctx->fh;
763 v4l2_fh_add(&ctx->fh);
766 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(priv->m2m_dev, ctx,
767 &ipu_csc_scaler_queue_init);
768 if (IS_ERR(ctx->fh.m2m_ctx)) {
769 ret = PTR_ERR(ctx->fh.m2m_ctx);
773 ret = ipu_csc_scaler_init_controls(ctx);
777 ctx->fh.ctrl_handler = &ctx->ctrl_hdlr;
779 ctx->q_data[V4L2_M2M_SRC] = ipu_csc_scaler_q_data_default;
780 ctx->q_data[V4L2_M2M_DST] = ipu_csc_scaler_q_data_default;
782 dev_dbg(priv->dev, "Created instance %p, m2m_ctx: %p\n", ctx,
788 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
790 v4l2_fh_del(&ctx->fh);
791 v4l2_fh_exit(&ctx->fh);
796 static int ipu_csc_scaler_release(struct file *file)
798 struct ipu_csc_scaler_priv *priv = video_drvdata(file);
799 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(file->private_data);
801 dev_dbg(priv->dev, "Releasing instance %p\n", ctx);
803 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
804 v4l2_fh_del(&ctx->fh);
805 v4l2_fh_exit(&ctx->fh);
811 static const struct v4l2_file_operations ipu_csc_scaler_fops = {
812 .owner = THIS_MODULE,
813 .open = ipu_csc_scaler_open,
814 .release = ipu_csc_scaler_release,
815 .poll = v4l2_m2m_fop_poll,
816 .unlocked_ioctl = video_ioctl2,
817 .mmap = v4l2_m2m_fop_mmap,
820 static struct v4l2_m2m_ops m2m_ops = {
821 .device_run = device_run,
822 .job_abort = job_abort,
825 static void ipu_csc_scaler_video_device_release(struct video_device *vdev)
827 struct ipu_csc_scaler_priv *priv = video_get_drvdata(vdev);
829 v4l2_m2m_release(priv->m2m_dev);
830 video_device_release(vdev);
834 static const struct video_device ipu_csc_scaler_videodev_template = {
835 .name = "ipu_ic_pp csc/scaler",
836 .fops = &ipu_csc_scaler_fops,
837 .ioctl_ops = &ipu_csc_scaler_ioctl_ops,
839 .release = ipu_csc_scaler_video_device_release,
840 .vfl_dir = VFL_DIR_M2M,
841 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
844 int imx_media_csc_scaler_device_register(struct imx_media_video_dev *vdev)
846 struct ipu_csc_scaler_priv *priv = vdev_to_priv(vdev);
847 struct video_device *vfd = vdev->vfd;
850 vfd->v4l2_dev = &priv->md->v4l2_dev;
852 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
854 v4l2_err(vfd->v4l2_dev, "Failed to register video device\n");
858 v4l2_info(vfd->v4l2_dev, "Registered %s as /dev/%s\n", vfd->name,
859 video_device_node_name(vfd));
864 void imx_media_csc_scaler_device_unregister(struct imx_media_video_dev *vdev)
866 struct ipu_csc_scaler_priv *priv = vdev_to_priv(vdev);
867 struct video_device *vfd = priv->vdev.vfd;
869 mutex_lock(&priv->mutex);
871 video_unregister_device(vfd);
873 mutex_unlock(&priv->mutex);
876 struct imx_media_video_dev *
877 imx_media_csc_scaler_device_init(struct imx_media_dev *md)
879 struct ipu_csc_scaler_priv *priv;
880 struct video_device *vfd;
883 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
885 return ERR_PTR(-ENOMEM);
888 priv->dev = md->md.dev;
890 mutex_init(&priv->mutex);
892 vfd = video_device_alloc();
898 *vfd = ipu_csc_scaler_videodev_template;
899 vfd->lock = &priv->mutex;
900 priv->vdev.vfd = vfd;
902 INIT_LIST_HEAD(&priv->vdev.list);
904 video_set_drvdata(vfd, priv);
906 priv->m2m_dev = v4l2_m2m_init(&m2m_ops);
907 if (IS_ERR(priv->m2m_dev)) {
908 ret = PTR_ERR(priv->m2m_dev);
909 v4l2_err(&md->v4l2_dev, "Failed to init mem2mem device: %d\n",
917 video_set_drvdata(vfd, NULL);
923 MODULE_DESCRIPTION("i.MX IPUv3 mem2mem scaler/CSC driver");
924 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
925 MODULE_LICENSE("GPL");