1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
6 * Samsung EXYNOS5 SoC series G-Scaler driver
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/bug.h>
14 #include <linux/interrupt.h>
15 #include <linux/workqueue.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/list.h>
20 #include <linux/slab.h>
21 #include <linux/clk.h>
23 #include <media/v4l2-ioctl.h>
27 static int gsc_m2m_ctx_stop_req(struct gsc_ctx *ctx)
29 struct gsc_ctx *curr_ctx;
30 struct gsc_dev *gsc = ctx->gsc_dev;
33 curr_ctx = v4l2_m2m_get_curr_priv(gsc->m2m.m2m_dev);
34 if (!gsc_m2m_pending(gsc) || (curr_ctx != ctx))
37 gsc_ctx_state_lock_set(GSC_CTX_STOP_REQ, ctx);
38 ret = wait_event_timeout(gsc->irq_queue,
39 !gsc_ctx_state_is_set(GSC_CTX_STOP_REQ, ctx),
40 GSC_SHUTDOWN_TIMEOUT);
42 return ret == 0 ? -ETIMEDOUT : ret;
45 static void __gsc_m2m_job_abort(struct gsc_ctx *ctx)
49 ret = gsc_m2m_ctx_stop_req(ctx);
50 if ((ret == -ETIMEDOUT) || (ctx->state & GSC_CTX_ABORT)) {
51 gsc_ctx_state_lock_clear(GSC_CTX_STOP_REQ | GSC_CTX_ABORT, ctx);
52 gsc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
56 static int gsc_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
58 struct gsc_ctx *ctx = q->drv_priv;
60 return pm_runtime_resume_and_get(&ctx->gsc_dev->pdev->dev);
63 static void __gsc_m2m_cleanup_queue(struct gsc_ctx *ctx)
65 struct vb2_v4l2_buffer *src_vb, *dst_vb;
67 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
68 src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
69 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
72 while (v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) > 0) {
73 dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
74 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
78 static void gsc_m2m_stop_streaming(struct vb2_queue *q)
80 struct gsc_ctx *ctx = q->drv_priv;
82 __gsc_m2m_job_abort(ctx);
84 __gsc_m2m_cleanup_queue(ctx);
86 pm_runtime_put(&ctx->gsc_dev->pdev->dev);
89 void gsc_m2m_job_finish(struct gsc_ctx *ctx, int vb_state)
91 struct vb2_v4l2_buffer *src_vb, *dst_vb;
93 if (!ctx || !ctx->m2m_ctx)
96 src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
97 dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
99 if (src_vb && dst_vb) {
100 dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
101 dst_vb->timecode = src_vb->timecode;
102 dst_vb->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
105 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
107 v4l2_m2m_buf_done(src_vb, vb_state);
108 v4l2_m2m_buf_done(dst_vb, vb_state);
110 v4l2_m2m_job_finish(ctx->gsc_dev->m2m.m2m_dev,
115 static void gsc_m2m_job_abort(void *priv)
117 __gsc_m2m_job_abort((struct gsc_ctx *)priv);
120 static int gsc_get_bufs(struct gsc_ctx *ctx)
122 struct gsc_frame *s_frame, *d_frame;
123 struct vb2_v4l2_buffer *src_vb, *dst_vb;
126 s_frame = &ctx->s_frame;
127 d_frame = &ctx->d_frame;
129 src_vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
130 ret = gsc_prepare_addr(ctx, &src_vb->vb2_buf, s_frame, &s_frame->addr);
134 dst_vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
135 ret = gsc_prepare_addr(ctx, &dst_vb->vb2_buf, d_frame, &d_frame->addr);
139 dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
144 static void gsc_m2m_device_run(void *priv)
146 struct gsc_ctx *ctx = priv;
152 if (WARN(!ctx, "null hardware context\n"))
156 spin_lock_irqsave(&gsc->slock, flags);
158 set_bit(ST_M2M_PEND, &gsc->state);
160 /* Reconfigure hardware if the context has changed. */
161 if (gsc->m2m.ctx != ctx) {
162 pr_debug("gsc->m2m.ctx = 0x%p, current_ctx = 0x%p",
164 ctx->state |= GSC_PARAMS;
168 is_set = ctx->state & GSC_CTX_STOP_REQ;
170 ctx->state &= ~GSC_CTX_STOP_REQ;
171 ctx->state |= GSC_CTX_ABORT;
172 wake_up(&gsc->irq_queue);
176 ret = gsc_get_bufs(ctx);
178 pr_err("Wrong address");
182 gsc_set_prefbuf(gsc, &ctx->s_frame);
183 gsc_hw_set_input_addr(gsc, &ctx->s_frame.addr, GSC_M2M_BUF_NUM);
184 gsc_hw_set_output_addr(gsc, &ctx->d_frame.addr, GSC_M2M_BUF_NUM);
186 if (ctx->state & GSC_PARAMS) {
187 gsc_hw_set_input_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
188 gsc_hw_set_output_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
189 gsc_hw_set_frm_done_irq_mask(gsc, false);
190 gsc_hw_set_gsc_irq_enable(gsc, true);
192 if (gsc_set_scaler_info(ctx)) {
193 pr_err("Scaler setup error");
197 gsc_hw_set_input_path(ctx);
198 gsc_hw_set_in_size(ctx);
199 gsc_hw_set_in_image_format(ctx);
201 gsc_hw_set_output_path(ctx);
202 gsc_hw_set_out_size(ctx);
203 gsc_hw_set_out_image_format(ctx);
205 gsc_hw_set_prescaler(ctx);
206 gsc_hw_set_mainscaler(ctx);
207 gsc_hw_set_rotation(ctx);
208 gsc_hw_set_global_alpha(ctx);
211 /* update shadow registers */
212 gsc_hw_set_sfr_update(ctx);
214 ctx->state &= ~GSC_PARAMS;
215 gsc_hw_enable_control(gsc, true);
217 spin_unlock_irqrestore(&gsc->slock, flags);
221 ctx->state &= ~GSC_PARAMS;
222 spin_unlock_irqrestore(&gsc->slock, flags);
225 static int gsc_m2m_queue_setup(struct vb2_queue *vq,
226 unsigned int *num_buffers, unsigned int *num_planes,
227 unsigned int sizes[], struct device *alloc_devs[])
229 struct gsc_ctx *ctx = vb2_get_drv_priv(vq);
230 struct gsc_frame *frame;
233 frame = ctx_get_frame(ctx, vq->type);
235 return PTR_ERR(frame);
240 *num_planes = frame->fmt->num_planes;
241 for (i = 0; i < frame->fmt->num_planes; i++)
242 sizes[i] = frame->payload[i];
246 static int gsc_m2m_buf_prepare(struct vb2_buffer *vb)
248 struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
249 struct gsc_frame *frame;
252 frame = ctx_get_frame(ctx, vb->vb2_queue->type);
254 return PTR_ERR(frame);
256 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) {
257 for (i = 0; i < frame->fmt->num_planes; i++)
258 vb2_set_plane_payload(vb, i, frame->payload[i]);
264 static void gsc_m2m_buf_queue(struct vb2_buffer *vb)
266 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
267 struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
269 pr_debug("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
272 v4l2_m2m_buf_queue(ctx->m2m_ctx, vbuf);
275 static const struct vb2_ops gsc_m2m_qops = {
276 .queue_setup = gsc_m2m_queue_setup,
277 .buf_prepare = gsc_m2m_buf_prepare,
278 .buf_queue = gsc_m2m_buf_queue,
279 .wait_prepare = vb2_ops_wait_prepare,
280 .wait_finish = vb2_ops_wait_finish,
281 .stop_streaming = gsc_m2m_stop_streaming,
282 .start_streaming = gsc_m2m_start_streaming,
285 static int gsc_m2m_querycap(struct file *file, void *fh,
286 struct v4l2_capability *cap)
288 strscpy(cap->driver, GSC_MODULE_NAME, sizeof(cap->driver));
289 strscpy(cap->card, GSC_MODULE_NAME " gscaler", sizeof(cap->card));
293 static int gsc_m2m_enum_fmt(struct file *file, void *priv,
294 struct v4l2_fmtdesc *f)
296 return gsc_enum_fmt(f);
299 static int gsc_m2m_g_fmt_mplane(struct file *file, void *fh,
300 struct v4l2_format *f)
302 struct gsc_ctx *ctx = fh_to_ctx(fh);
304 return gsc_g_fmt_mplane(ctx, f);
307 static int gsc_m2m_try_fmt_mplane(struct file *file, void *fh,
308 struct v4l2_format *f)
310 struct gsc_ctx *ctx = fh_to_ctx(fh);
312 return gsc_try_fmt_mplane(ctx, f);
315 static int gsc_m2m_s_fmt_mplane(struct file *file, void *fh,
316 struct v4l2_format *f)
318 struct gsc_ctx *ctx = fh_to_ctx(fh);
319 struct vb2_queue *vq;
320 struct gsc_frame *frame;
321 struct v4l2_pix_format_mplane *pix;
324 ret = gsc_m2m_try_fmt_mplane(file, fh, f);
328 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
330 if (vb2_is_streaming(vq)) {
331 pr_err("queue (%d) busy", f->type);
335 if (V4L2_TYPE_IS_OUTPUT(f->type))
336 frame = &ctx->s_frame;
338 frame = &ctx->d_frame;
340 pix = &f->fmt.pix_mp;
341 frame->fmt = find_fmt(&pix->pixelformat, NULL, 0);
342 frame->colorspace = pix->colorspace;
346 for (i = 0; i < frame->fmt->num_planes; i++)
347 frame->payload[i] = pix->plane_fmt[i].sizeimage;
349 gsc_set_frame_size(frame, pix->width, pix->height);
351 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
352 gsc_ctx_state_lock_set(GSC_PARAMS | GSC_DST_FMT, ctx);
354 gsc_ctx_state_lock_set(GSC_PARAMS | GSC_SRC_FMT, ctx);
356 pr_debug("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
361 static int gsc_m2m_reqbufs(struct file *file, void *fh,
362 struct v4l2_requestbuffers *reqbufs)
364 struct gsc_ctx *ctx = fh_to_ctx(fh);
365 struct gsc_dev *gsc = ctx->gsc_dev;
368 max_cnt = (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
369 gsc->variant->in_buf_cnt : gsc->variant->out_buf_cnt;
370 if (reqbufs->count > max_cnt)
373 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
376 static int gsc_m2m_expbuf(struct file *file, void *fh,
377 struct v4l2_exportbuffer *eb)
379 struct gsc_ctx *ctx = fh_to_ctx(fh);
380 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
383 static int gsc_m2m_querybuf(struct file *file, void *fh,
384 struct v4l2_buffer *buf)
386 struct gsc_ctx *ctx = fh_to_ctx(fh);
387 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
390 static int gsc_m2m_qbuf(struct file *file, void *fh,
391 struct v4l2_buffer *buf)
393 struct gsc_ctx *ctx = fh_to_ctx(fh);
394 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
397 static int gsc_m2m_dqbuf(struct file *file, void *fh,
398 struct v4l2_buffer *buf)
400 struct gsc_ctx *ctx = fh_to_ctx(fh);
401 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
404 static int gsc_m2m_streamon(struct file *file, void *fh,
405 enum v4l2_buf_type type)
407 struct gsc_ctx *ctx = fh_to_ctx(fh);
409 /* The source and target color format need to be set */
410 if (V4L2_TYPE_IS_OUTPUT(type)) {
411 if (!gsc_ctx_state_is_set(GSC_SRC_FMT, ctx))
413 } else if (!gsc_ctx_state_is_set(GSC_DST_FMT, ctx)) {
417 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
420 static int gsc_m2m_streamoff(struct file *file, void *fh,
421 enum v4l2_buf_type type)
423 struct gsc_ctx *ctx = fh_to_ctx(fh);
424 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
427 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
428 static int is_rectangle_enclosed(struct v4l2_rect *a, struct v4l2_rect *b)
430 if (a->left < b->left || a->top < b->top)
433 if (a->left + a->width > b->left + b->width)
436 if (a->top + a->height > b->top + b->height)
442 static int gsc_m2m_g_selection(struct file *file, void *fh,
443 struct v4l2_selection *s)
445 struct gsc_frame *frame;
446 struct gsc_ctx *ctx = fh_to_ctx(fh);
448 if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
449 (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
452 frame = ctx_get_frame(ctx, s->type);
454 return PTR_ERR(frame);
457 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
458 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
459 case V4L2_SEL_TGT_CROP_BOUNDS:
460 case V4L2_SEL_TGT_CROP_DEFAULT:
463 s->r.width = frame->f_width;
464 s->r.height = frame->f_height;
467 case V4L2_SEL_TGT_COMPOSE:
468 case V4L2_SEL_TGT_CROP:
469 s->r.left = frame->crop.left;
470 s->r.top = frame->crop.top;
471 s->r.width = frame->crop.width;
472 s->r.height = frame->crop.height;
479 static int gsc_m2m_s_selection(struct file *file, void *fh,
480 struct v4l2_selection *s)
482 struct gsc_frame *frame;
483 struct gsc_ctx *ctx = fh_to_ctx(fh);
484 struct gsc_variant *variant = ctx->gsc_dev->variant;
485 struct v4l2_selection sel = *s;
488 if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
489 (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
492 ret = gsc_try_selection(ctx, &sel);
496 if (s->flags & V4L2_SEL_FLAG_LE &&
497 !is_rectangle_enclosed(&sel.r, &s->r))
500 if (s->flags & V4L2_SEL_FLAG_GE &&
501 !is_rectangle_enclosed(&s->r, &sel.r))
507 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
508 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
509 case V4L2_SEL_TGT_COMPOSE:
510 frame = &ctx->s_frame;
513 case V4L2_SEL_TGT_CROP_BOUNDS:
514 case V4L2_SEL_TGT_CROP:
515 case V4L2_SEL_TGT_CROP_DEFAULT:
516 frame = &ctx->d_frame;
523 /* Check to see if scaling ratio is within supported range */
524 if (gsc_ctx_state_is_set(GSC_DST_FMT | GSC_SRC_FMT, ctx)) {
525 if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
526 ret = gsc_check_scaler_ratio(variant, sel.r.width,
527 sel.r.height, ctx->d_frame.crop.width,
528 ctx->d_frame.crop.height,
529 ctx->gsc_ctrls.rotate->val, ctx->out_path);
531 ret = gsc_check_scaler_ratio(variant,
532 ctx->s_frame.crop.width,
533 ctx->s_frame.crop.height, sel.r.width,
534 sel.r.height, ctx->gsc_ctrls.rotate->val,
539 pr_err("Out of scaler range");
546 gsc_ctx_state_lock_set(GSC_PARAMS, ctx);
550 static const struct v4l2_ioctl_ops gsc_m2m_ioctl_ops = {
551 .vidioc_querycap = gsc_m2m_querycap,
552 .vidioc_enum_fmt_vid_cap = gsc_m2m_enum_fmt,
553 .vidioc_enum_fmt_vid_out = gsc_m2m_enum_fmt,
554 .vidioc_g_fmt_vid_cap_mplane = gsc_m2m_g_fmt_mplane,
555 .vidioc_g_fmt_vid_out_mplane = gsc_m2m_g_fmt_mplane,
556 .vidioc_try_fmt_vid_cap_mplane = gsc_m2m_try_fmt_mplane,
557 .vidioc_try_fmt_vid_out_mplane = gsc_m2m_try_fmt_mplane,
558 .vidioc_s_fmt_vid_cap_mplane = gsc_m2m_s_fmt_mplane,
559 .vidioc_s_fmt_vid_out_mplane = gsc_m2m_s_fmt_mplane,
560 .vidioc_reqbufs = gsc_m2m_reqbufs,
561 .vidioc_expbuf = gsc_m2m_expbuf,
562 .vidioc_querybuf = gsc_m2m_querybuf,
563 .vidioc_qbuf = gsc_m2m_qbuf,
564 .vidioc_dqbuf = gsc_m2m_dqbuf,
565 .vidioc_streamon = gsc_m2m_streamon,
566 .vidioc_streamoff = gsc_m2m_streamoff,
567 .vidioc_g_selection = gsc_m2m_g_selection,
568 .vidioc_s_selection = gsc_m2m_s_selection
571 static int queue_init(void *priv, struct vb2_queue *src_vq,
572 struct vb2_queue *dst_vq)
574 struct gsc_ctx *ctx = priv;
577 memset(src_vq, 0, sizeof(*src_vq));
578 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
579 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
580 src_vq->drv_priv = ctx;
581 src_vq->ops = &gsc_m2m_qops;
582 src_vq->mem_ops = &vb2_dma_contig_memops;
583 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
584 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
585 src_vq->lock = &ctx->gsc_dev->lock;
586 src_vq->dev = &ctx->gsc_dev->pdev->dev;
588 ret = vb2_queue_init(src_vq);
592 memset(dst_vq, 0, sizeof(*dst_vq));
593 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
594 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
595 dst_vq->drv_priv = ctx;
596 dst_vq->ops = &gsc_m2m_qops;
597 dst_vq->mem_ops = &vb2_dma_contig_memops;
598 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
599 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
600 dst_vq->lock = &ctx->gsc_dev->lock;
601 dst_vq->dev = &ctx->gsc_dev->pdev->dev;
603 return vb2_queue_init(dst_vq);
606 static int gsc_m2m_open(struct file *file)
608 struct gsc_dev *gsc = video_drvdata(file);
609 struct gsc_ctx *ctx = NULL;
612 pr_debug("pid: %d, state: 0x%lx", task_pid_nr(current), gsc->state);
614 if (mutex_lock_interruptible(&gsc->lock))
617 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
623 v4l2_fh_init(&ctx->fh, gsc->m2m.vfd);
624 ret = gsc_ctrls_create(ctx);
628 /* Use separate control handler per file handle */
629 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
630 file->private_data = &ctx->fh;
631 v4l2_fh_add(&ctx->fh);
634 /* Default color format */
635 ctx->s_frame.fmt = get_format(0);
636 ctx->d_frame.fmt = get_format(0);
637 /* Setup the device context for mem2mem mode. */
638 ctx->state = GSC_CTX_M2M;
640 ctx->in_path = GSC_DMA;
641 ctx->out_path = GSC_DMA;
643 ctx->m2m_ctx = v4l2_m2m_ctx_init(gsc->m2m.m2m_dev, ctx, queue_init);
644 if (IS_ERR(ctx->m2m_ctx)) {
645 pr_err("Failed to initialize m2m context");
646 ret = PTR_ERR(ctx->m2m_ctx);
650 if (gsc->m2m.refcnt++ == 0)
651 set_bit(ST_M2M_OPEN, &gsc->state);
653 pr_debug("gsc m2m driver is opened, ctx(0x%p)", ctx);
655 mutex_unlock(&gsc->lock);
659 gsc_ctrls_delete(ctx);
660 v4l2_fh_del(&ctx->fh);
662 v4l2_fh_exit(&ctx->fh);
665 mutex_unlock(&gsc->lock);
669 static int gsc_m2m_release(struct file *file)
671 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
672 struct gsc_dev *gsc = ctx->gsc_dev;
674 pr_debug("pid: %d, state: 0x%lx, refcnt= %d",
675 task_pid_nr(current), gsc->state, gsc->m2m.refcnt);
677 mutex_lock(&gsc->lock);
679 v4l2_m2m_ctx_release(ctx->m2m_ctx);
680 gsc_ctrls_delete(ctx);
681 v4l2_fh_del(&ctx->fh);
682 v4l2_fh_exit(&ctx->fh);
684 if (--gsc->m2m.refcnt <= 0)
685 clear_bit(ST_M2M_OPEN, &gsc->state);
688 mutex_unlock(&gsc->lock);
692 static __poll_t gsc_m2m_poll(struct file *file,
693 struct poll_table_struct *wait)
695 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
696 struct gsc_dev *gsc = ctx->gsc_dev;
699 if (mutex_lock_interruptible(&gsc->lock))
702 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
703 mutex_unlock(&gsc->lock);
708 static int gsc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
710 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
711 struct gsc_dev *gsc = ctx->gsc_dev;
714 if (mutex_lock_interruptible(&gsc->lock))
717 ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
718 mutex_unlock(&gsc->lock);
723 static const struct v4l2_file_operations gsc_m2m_fops = {
724 .owner = THIS_MODULE,
725 .open = gsc_m2m_open,
726 .release = gsc_m2m_release,
727 .poll = gsc_m2m_poll,
728 .unlocked_ioctl = video_ioctl2,
729 .mmap = gsc_m2m_mmap,
732 static const struct v4l2_m2m_ops gsc_m2m_ops = {
733 .device_run = gsc_m2m_device_run,
734 .job_abort = gsc_m2m_job_abort,
737 int gsc_register_m2m_device(struct gsc_dev *gsc)
739 struct platform_device *pdev;
747 gsc->vdev.fops = &gsc_m2m_fops;
748 gsc->vdev.ioctl_ops = &gsc_m2m_ioctl_ops;
749 gsc->vdev.release = video_device_release_empty;
750 gsc->vdev.lock = &gsc->lock;
751 gsc->vdev.vfl_dir = VFL_DIR_M2M;
752 gsc->vdev.v4l2_dev = &gsc->v4l2_dev;
753 gsc->vdev.device_caps = V4L2_CAP_STREAMING |
754 V4L2_CAP_VIDEO_M2M_MPLANE;
755 snprintf(gsc->vdev.name, sizeof(gsc->vdev.name), "%s.%d:m2m",
756 GSC_MODULE_NAME, gsc->id);
758 video_set_drvdata(&gsc->vdev, gsc);
760 gsc->m2m.vfd = &gsc->vdev;
761 gsc->m2m.m2m_dev = v4l2_m2m_init(&gsc_m2m_ops);
762 if (IS_ERR(gsc->m2m.m2m_dev)) {
763 dev_err(&pdev->dev, "failed to initialize v4l2-m2m device\n");
764 return PTR_ERR(gsc->m2m.m2m_dev);
767 ret = video_register_device(&gsc->vdev, VFL_TYPE_VIDEO, -1);
770 "%s(): failed to register video device\n", __func__);
771 goto err_m2m_release;
774 pr_debug("gsc m2m driver registered as /dev/video%d", gsc->vdev.num);
778 v4l2_m2m_release(gsc->m2m.m2m_dev);
783 void gsc_unregister_m2m_device(struct gsc_dev *gsc)
786 v4l2_m2m_release(gsc->m2m.m2m_dev);
787 video_unregister_device(&gsc->vdev);