2 * Samsung S5P/EXYNOS4 SoC series FIMC (video postprocessor) driver
4 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 2 of the License,
10 * or (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/bug.h>
18 #include <linux/interrupt.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/clk.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/videobuf2-v4l2.h>
28 #include <media/videobuf2-dma-contig.h>
31 #include "fimc-core.h"
33 #include "media-dev.h"
35 static unsigned int get_m2m_fmt_flags(unsigned int stream_type
)
37 if (stream_type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
38 return FMT_FLAGS_M2M_IN
;
40 return FMT_FLAGS_M2M_OUT
;
43 void fimc_m2m_job_finish(struct fimc_ctx
*ctx
, int vb_state
)
45 struct vb2_v4l2_buffer
*src_vb
, *dst_vb
;
47 if (!ctx
|| !ctx
->fh
.m2m_ctx
)
50 src_vb
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
51 dst_vb
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
54 v4l2_m2m_buf_done(src_vb
, vb_state
);
56 v4l2_m2m_buf_done(dst_vb
, vb_state
);
58 v4l2_m2m_job_finish(ctx
->fimc_dev
->m2m
.m2m_dev
,
62 /* Complete the transaction which has been scheduled for execution. */
63 static void fimc_m2m_shutdown(struct fimc_ctx
*ctx
)
65 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
67 if (!fimc_m2m_pending(fimc
))
70 fimc_ctx_state_set(FIMC_CTX_SHUT
, ctx
);
72 wait_event_timeout(fimc
->irq_queue
,
73 !fimc_ctx_state_is_set(FIMC_CTX_SHUT
, ctx
),
74 FIMC_SHUTDOWN_TIMEOUT
);
77 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
79 struct fimc_ctx
*ctx
= q
->drv_priv
;
82 ret
= pm_runtime_get_sync(&ctx
->fimc_dev
->pdev
->dev
);
83 return ret
> 0 ? 0 : ret
;
86 static void stop_streaming(struct vb2_queue
*q
)
88 struct fimc_ctx
*ctx
= q
->drv_priv
;
91 fimc_m2m_shutdown(ctx
);
92 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_ERROR
);
93 pm_runtime_put(&ctx
->fimc_dev
->pdev
->dev
);
96 static void fimc_device_run(void *priv
)
98 struct vb2_v4l2_buffer
*src_vb
, *dst_vb
;
99 struct fimc_ctx
*ctx
= priv
;
100 struct fimc_frame
*sf
, *df
;
101 struct fimc_dev
*fimc
;
105 if (WARN(!ctx
, "Null context\n"))
108 fimc
= ctx
->fimc_dev
;
109 spin_lock_irqsave(&fimc
->slock
, flags
);
111 set_bit(ST_M2M_PEND
, &fimc
->state
);
115 if (ctx
->state
& FIMC_PARAMS
) {
116 /* Prepare the DMA offsets for scaler */
117 fimc_prepare_dma_offset(ctx
, sf
);
118 fimc_prepare_dma_offset(ctx
, df
);
121 src_vb
= v4l2_m2m_next_src_buf(ctx
->fh
.m2m_ctx
);
122 ret
= fimc_prepare_addr(ctx
, &src_vb
->vb2_buf
, sf
, &sf
->paddr
);
126 dst_vb
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
127 ret
= fimc_prepare_addr(ctx
, &dst_vb
->vb2_buf
, df
, &df
->paddr
);
131 dst_vb
->vb2_buf
.timestamp
= src_vb
->vb2_buf
.timestamp
;
132 dst_vb
->flags
&= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
134 src_vb
->flags
& V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
136 /* Reconfigure hardware if the context has changed. */
137 if (fimc
->m2m
.ctx
!= ctx
) {
138 ctx
->state
|= FIMC_PARAMS
;
142 if (ctx
->state
& FIMC_PARAMS
) {
143 fimc_set_yuv_order(ctx
);
144 fimc_hw_set_input_path(ctx
);
145 fimc_hw_set_in_dma(ctx
);
146 ret
= fimc_set_scaler_info(ctx
);
149 fimc_hw_set_prescaler(ctx
);
150 fimc_hw_set_mainscaler(ctx
);
151 fimc_hw_set_target_format(ctx
);
152 fimc_hw_set_rotation(ctx
);
153 fimc_hw_set_effect(ctx
);
154 fimc_hw_set_out_dma(ctx
);
155 if (fimc
->drv_data
->alpha_color
)
156 fimc_hw_set_rgb_alpha(ctx
);
157 fimc_hw_set_output_path(ctx
);
159 fimc_hw_set_input_addr(fimc
, &sf
->paddr
);
160 fimc_hw_set_output_addr(fimc
, &df
->paddr
, -1);
162 fimc_activate_capture(ctx
);
163 ctx
->state
&= (FIMC_CTX_M2M
| FIMC_CTX_CAP
);
164 fimc_hw_activate_input_dma(fimc
, true);
167 spin_unlock_irqrestore(&fimc
->slock
, flags
);
170 static void fimc_job_abort(void *priv
)
172 fimc_m2m_shutdown(priv
);
175 static int fimc_queue_setup(struct vb2_queue
*vq
,
176 unsigned int *num_buffers
, unsigned int *num_planes
,
177 unsigned int sizes
[], struct device
*alloc_devs
[])
179 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
180 struct fimc_frame
*f
;
183 f
= ctx_get_frame(ctx
, vq
->type
);
187 * Return number of non-contiguous planes (plane buffers)
188 * depending on the configured color format.
193 *num_planes
= f
->fmt
->memplanes
;
194 for (i
= 0; i
< f
->fmt
->memplanes
; i
++)
195 sizes
[i
] = f
->payload
[i
];
199 static int fimc_buf_prepare(struct vb2_buffer
*vb
)
201 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
202 struct fimc_frame
*frame
;
205 frame
= ctx_get_frame(ctx
, vb
->vb2_queue
->type
);
207 return PTR_ERR(frame
);
209 for (i
= 0; i
< frame
->fmt
->memplanes
; i
++)
210 vb2_set_plane_payload(vb
, i
, frame
->payload
[i
]);
215 static void fimc_buf_queue(struct vb2_buffer
*vb
)
217 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
218 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
219 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
222 static const struct vb2_ops fimc_qops
= {
223 .queue_setup
= fimc_queue_setup
,
224 .buf_prepare
= fimc_buf_prepare
,
225 .buf_queue
= fimc_buf_queue
,
226 .wait_prepare
= vb2_ops_wait_prepare
,
227 .wait_finish
= vb2_ops_wait_finish
,
228 .stop_streaming
= stop_streaming
,
229 .start_streaming
= start_streaming
,
233 * V4L2 ioctl handlers
235 static int fimc_m2m_querycap(struct file
*file
, void *fh
,
236 struct v4l2_capability
*cap
)
238 struct fimc_dev
*fimc
= video_drvdata(file
);
239 unsigned int caps
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_M2M_MPLANE
;
241 __fimc_vidioc_querycap(&fimc
->pdev
->dev
, cap
, caps
);
245 static int fimc_m2m_enum_fmt_mplane(struct file
*file
, void *priv
,
246 struct v4l2_fmtdesc
*f
)
248 struct fimc_fmt
*fmt
;
250 fmt
= fimc_find_format(NULL
, NULL
, get_m2m_fmt_flags(f
->type
),
255 strscpy(f
->description
, fmt
->name
, sizeof(f
->description
));
256 f
->pixelformat
= fmt
->fourcc
;
260 static int fimc_m2m_g_fmt_mplane(struct file
*file
, void *fh
,
261 struct v4l2_format
*f
)
263 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
264 struct fimc_frame
*frame
= ctx_get_frame(ctx
, f
->type
);
267 return PTR_ERR(frame
);
269 __fimc_get_format(frame
, f
);
273 static int fimc_try_fmt_mplane(struct fimc_ctx
*ctx
, struct v4l2_format
*f
)
275 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
276 const struct fimc_variant
*variant
= fimc
->variant
;
277 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
278 struct fimc_fmt
*fmt
;
279 u32 max_w
, mod_x
, mod_y
;
281 if (!IS_M2M(f
->type
))
284 fmt
= fimc_find_format(&pix
->pixelformat
, NULL
,
285 get_m2m_fmt_flags(f
->type
), 0);
286 if (WARN(fmt
== NULL
, "Pixel format lookup failed"))
289 if (pix
->field
== V4L2_FIELD_ANY
)
290 pix
->field
= V4L2_FIELD_NONE
;
291 else if (pix
->field
!= V4L2_FIELD_NONE
)
294 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
295 max_w
= variant
->pix_limit
->scaler_dis_w
;
296 mod_x
= ffs(variant
->min_inp_pixsize
) - 1;
298 max_w
= variant
->pix_limit
->out_rot_dis_w
;
299 mod_x
= ffs(variant
->min_out_pixsize
) - 1;
302 if (tiled_fmt(fmt
)) {
303 mod_x
= 6; /* 64 x 32 pixels tile */
306 if (variant
->min_vsize_align
== 1)
307 mod_y
= fimc_fmt_is_rgb(fmt
->color
) ? 0 : 1;
309 mod_y
= ffs(variant
->min_vsize_align
) - 1;
312 v4l_bound_align_image(&pix
->width
, 16, max_w
, mod_x
,
313 &pix
->height
, 8, variant
->pix_limit
->scaler_dis_w
, mod_y
, 0);
315 fimc_adjust_mplane_format(fmt
, pix
->width
, pix
->height
, &f
->fmt
.pix_mp
);
319 static int fimc_m2m_try_fmt_mplane(struct file
*file
, void *fh
,
320 struct v4l2_format
*f
)
322 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
323 return fimc_try_fmt_mplane(ctx
, f
);
326 static void __set_frame_format(struct fimc_frame
*frame
, struct fimc_fmt
*fmt
,
327 struct v4l2_pix_format_mplane
*pixm
)
331 for (i
= 0; i
< fmt
->memplanes
; i
++) {
332 frame
->bytesperline
[i
] = pixm
->plane_fmt
[i
].bytesperline
;
333 frame
->payload
[i
] = pixm
->plane_fmt
[i
].sizeimage
;
336 frame
->f_width
= pixm
->width
;
337 frame
->f_height
= pixm
->height
;
338 frame
->o_width
= pixm
->width
;
339 frame
->o_height
= pixm
->height
;
340 frame
->width
= pixm
->width
;
341 frame
->height
= pixm
->height
;
347 static int fimc_m2m_s_fmt_mplane(struct file
*file
, void *fh
,
348 struct v4l2_format
*f
)
350 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
351 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
352 struct fimc_fmt
*fmt
;
353 struct vb2_queue
*vq
;
354 struct fimc_frame
*frame
;
357 ret
= fimc_try_fmt_mplane(ctx
, f
);
361 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
363 if (vb2_is_busy(vq
)) {
364 v4l2_err(&fimc
->m2m
.vfd
, "queue (%d) busy\n", f
->type
);
368 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
369 frame
= &ctx
->s_frame
;
371 frame
= &ctx
->d_frame
;
373 fmt
= fimc_find_format(&f
->fmt
.pix_mp
.pixelformat
, NULL
,
374 get_m2m_fmt_flags(f
->type
), 0);
378 __set_frame_format(frame
, fmt
, &f
->fmt
.pix_mp
);
380 /* Update RGB Alpha control state and value range */
381 fimc_alpha_ctrl_update(ctx
);
386 static int fimc_m2m_g_selection(struct file
*file
, void *fh
,
387 struct v4l2_selection
*s
)
389 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
390 struct fimc_frame
*frame
;
392 frame
= ctx_get_frame(ctx
, s
->type
);
394 return PTR_ERR(frame
);
397 case V4L2_SEL_TGT_CROP
:
398 case V4L2_SEL_TGT_CROP_DEFAULT
:
399 case V4L2_SEL_TGT_CROP_BOUNDS
:
400 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
403 case V4L2_SEL_TGT_COMPOSE
:
404 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
405 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
406 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
414 case V4L2_SEL_TGT_CROP
:
415 case V4L2_SEL_TGT_COMPOSE
:
416 s
->r
.left
= frame
->offs_h
;
417 s
->r
.top
= frame
->offs_v
;
418 s
->r
.width
= frame
->width
;
419 s
->r
.height
= frame
->height
;
421 case V4L2_SEL_TGT_CROP_DEFAULT
:
422 case V4L2_SEL_TGT_CROP_BOUNDS
:
423 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
424 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
427 s
->r
.width
= frame
->o_width
;
428 s
->r
.height
= frame
->o_height
;
436 static int fimc_m2m_try_selection(struct fimc_ctx
*ctx
,
437 struct v4l2_selection
*s
)
439 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
440 struct fimc_frame
*f
;
441 u32 min_size
, halign
, depth
= 0;
444 if (s
->r
.top
< 0 || s
->r
.left
< 0) {
445 v4l2_err(&fimc
->m2m
.vfd
,
446 "doesn't support negative values for top & left\n");
449 if (s
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
451 if (s
->target
!= V4L2_SEL_TGT_COMPOSE
)
453 } else if (s
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
455 if (s
->target
!= V4L2_SEL_TGT_CROP
)
461 min_size
= (f
== &ctx
->s_frame
) ?
462 fimc
->variant
->min_inp_pixsize
: fimc
->variant
->min_out_pixsize
;
464 /* Get pixel alignment constraints. */
465 if (fimc
->variant
->min_vsize_align
== 1)
466 halign
= fimc_fmt_is_rgb(f
->fmt
->color
) ? 0 : 1;
468 halign
= ffs(fimc
->variant
->min_vsize_align
) - 1;
470 for (i
= 0; i
< f
->fmt
->memplanes
; i
++)
471 depth
+= f
->fmt
->depth
[i
];
473 v4l_bound_align_image(&s
->r
.width
, min_size
, f
->o_width
,
475 &s
->r
.height
, min_size
, f
->o_height
,
476 halign
, 64/(ALIGN(depth
, 8)));
478 /* adjust left/top if cropping rectangle is out of bounds */
479 if (s
->r
.left
+ s
->r
.width
> f
->o_width
)
480 s
->r
.left
= f
->o_width
- s
->r
.width
;
481 if (s
->r
.top
+ s
->r
.height
> f
->o_height
)
482 s
->r
.top
= f
->o_height
- s
->r
.height
;
484 s
->r
.left
= round_down(s
->r
.left
, min_size
);
485 s
->r
.top
= round_down(s
->r
.top
, fimc
->variant
->hor_offs_align
);
487 dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
488 s
->r
.left
, s
->r
.top
, s
->r
.width
, s
->r
.height
,
489 f
->f_width
, f
->f_height
);
494 static int fimc_m2m_s_selection(struct file
*file
, void *fh
,
495 struct v4l2_selection
*s
)
497 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
498 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
499 struct fimc_frame
*f
;
502 ret
= fimc_m2m_try_selection(ctx
, s
);
506 f
= (s
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) ?
507 &ctx
->s_frame
: &ctx
->d_frame
;
509 /* Check to see if scaling ratio is within supported range */
510 if (s
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
511 ret
= fimc_check_scaler_ratio(ctx
, s
->r
.width
,
512 s
->r
.height
, ctx
->d_frame
.width
,
513 ctx
->d_frame
.height
, ctx
->rotation
);
515 ret
= fimc_check_scaler_ratio(ctx
, ctx
->s_frame
.width
,
516 ctx
->s_frame
.height
, s
->r
.width
,
517 s
->r
.height
, ctx
->rotation
);
520 v4l2_err(&fimc
->m2m
.vfd
, "Out of scaler range\n");
524 f
->offs_h
= s
->r
.left
;
525 f
->offs_v
= s
->r
.top
;
526 f
->width
= s
->r
.width
;
527 f
->height
= s
->r
.height
;
529 fimc_ctx_state_set(FIMC_PARAMS
, ctx
);
534 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops
= {
535 .vidioc_querycap
= fimc_m2m_querycap
,
536 .vidioc_enum_fmt_vid_cap_mplane
= fimc_m2m_enum_fmt_mplane
,
537 .vidioc_enum_fmt_vid_out_mplane
= fimc_m2m_enum_fmt_mplane
,
538 .vidioc_g_fmt_vid_cap_mplane
= fimc_m2m_g_fmt_mplane
,
539 .vidioc_g_fmt_vid_out_mplane
= fimc_m2m_g_fmt_mplane
,
540 .vidioc_try_fmt_vid_cap_mplane
= fimc_m2m_try_fmt_mplane
,
541 .vidioc_try_fmt_vid_out_mplane
= fimc_m2m_try_fmt_mplane
,
542 .vidioc_s_fmt_vid_cap_mplane
= fimc_m2m_s_fmt_mplane
,
543 .vidioc_s_fmt_vid_out_mplane
= fimc_m2m_s_fmt_mplane
,
544 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
545 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
546 .vidioc_qbuf
= v4l2_m2m_ioctl_qbuf
,
547 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
548 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
549 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
550 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
551 .vidioc_g_selection
= fimc_m2m_g_selection
,
552 .vidioc_s_selection
= fimc_m2m_s_selection
,
556 static int queue_init(void *priv
, struct vb2_queue
*src_vq
,
557 struct vb2_queue
*dst_vq
)
559 struct fimc_ctx
*ctx
= priv
;
562 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
563 src_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
564 src_vq
->drv_priv
= ctx
;
565 src_vq
->ops
= &fimc_qops
;
566 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
567 src_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
568 src_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
569 src_vq
->lock
= &ctx
->fimc_dev
->lock
;
570 src_vq
->dev
= &ctx
->fimc_dev
->pdev
->dev
;
572 ret
= vb2_queue_init(src_vq
);
576 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
577 dst_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
578 dst_vq
->drv_priv
= ctx
;
579 dst_vq
->ops
= &fimc_qops
;
580 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
581 dst_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
582 dst_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
583 dst_vq
->lock
= &ctx
->fimc_dev
->lock
;
584 dst_vq
->dev
= &ctx
->fimc_dev
->pdev
->dev
;
586 return vb2_queue_init(dst_vq
);
589 static int fimc_m2m_set_default_format(struct fimc_ctx
*ctx
)
591 struct v4l2_pix_format_mplane pixm
= {
592 .pixelformat
= V4L2_PIX_FMT_RGB32
,
596 .bytesperline
= 800 * 4,
597 .sizeimage
= 800 * 4 * 600,
600 struct fimc_fmt
*fmt
;
602 fmt
= fimc_find_format(&pixm
.pixelformat
, NULL
, FMT_FLAGS_M2M
, 0);
606 __set_frame_format(&ctx
->s_frame
, fmt
, &pixm
);
607 __set_frame_format(&ctx
->d_frame
, fmt
, &pixm
);
612 static int fimc_m2m_open(struct file
*file
)
614 struct fimc_dev
*fimc
= video_drvdata(file
);
615 struct fimc_ctx
*ctx
;
618 pr_debug("pid: %d, state: %#lx\n", task_pid_nr(current
), fimc
->state
);
620 if (mutex_lock_interruptible(&fimc
->lock
))
623 * Don't allow simultaneous open() of the mem-to-mem and the
624 * capture video node that belong to same FIMC IP instance.
626 if (test_bit(ST_CAPT_BUSY
, &fimc
->state
))
629 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
634 v4l2_fh_init(&ctx
->fh
, &fimc
->m2m
.vfd
);
635 ctx
->fimc_dev
= fimc
;
637 /* Default color format */
638 ctx
->s_frame
.fmt
= fimc_get_format(0);
639 ctx
->d_frame
.fmt
= fimc_get_format(0);
641 ret
= fimc_ctrls_create(ctx
);
645 /* Use separate control handler per file handle */
646 ctx
->fh
.ctrl_handler
= &ctx
->ctrls
.handler
;
647 file
->private_data
= &ctx
->fh
;
648 v4l2_fh_add(&ctx
->fh
);
650 /* Setup the device context for memory-to-memory mode */
651 ctx
->state
= FIMC_CTX_M2M
;
653 ctx
->in_path
= FIMC_IO_DMA
;
654 ctx
->out_path
= FIMC_IO_DMA
;
655 ctx
->scaler
.enabled
= 1;
657 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(fimc
->m2m
.m2m_dev
, ctx
, queue_init
);
658 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
659 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
663 if (fimc
->m2m
.refcnt
++ == 0)
664 set_bit(ST_M2M_RUN
, &fimc
->state
);
666 ret
= fimc_m2m_set_default_format(ctx
);
670 mutex_unlock(&fimc
->lock
);
674 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
676 fimc_ctrls_delete(ctx
);
677 v4l2_fh_del(&ctx
->fh
);
679 v4l2_fh_exit(&ctx
->fh
);
682 mutex_unlock(&fimc
->lock
);
686 static int fimc_m2m_release(struct file
*file
)
688 struct fimc_ctx
*ctx
= fh_to_ctx(file
->private_data
);
689 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
691 dbg("pid: %d, state: 0x%lx, refcnt= %d",
692 task_pid_nr(current
), fimc
->state
, fimc
->m2m
.refcnt
);
694 mutex_lock(&fimc
->lock
);
696 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
697 fimc_ctrls_delete(ctx
);
698 v4l2_fh_del(&ctx
->fh
);
699 v4l2_fh_exit(&ctx
->fh
);
701 if (--fimc
->m2m
.refcnt
<= 0)
702 clear_bit(ST_M2M_RUN
, &fimc
->state
);
705 mutex_unlock(&fimc
->lock
);
709 static const struct v4l2_file_operations fimc_m2m_fops
= {
710 .owner
= THIS_MODULE
,
711 .open
= fimc_m2m_open
,
712 .release
= fimc_m2m_release
,
713 .poll
= v4l2_m2m_fop_poll
,
714 .unlocked_ioctl
= video_ioctl2
,
715 .mmap
= v4l2_m2m_fop_mmap
,
718 static const struct v4l2_m2m_ops m2m_ops
= {
719 .device_run
= fimc_device_run
,
720 .job_abort
= fimc_job_abort
,
723 int fimc_register_m2m_device(struct fimc_dev
*fimc
,
724 struct v4l2_device
*v4l2_dev
)
726 struct video_device
*vfd
= &fimc
->m2m
.vfd
;
729 fimc
->v4l2_dev
= v4l2_dev
;
731 memset(vfd
, 0, sizeof(*vfd
));
732 vfd
->fops
= &fimc_m2m_fops
;
733 vfd
->ioctl_ops
= &fimc_m2m_ioctl_ops
;
734 vfd
->v4l2_dev
= v4l2_dev
;
736 vfd
->release
= video_device_release_empty
;
737 vfd
->lock
= &fimc
->lock
;
738 vfd
->vfl_dir
= VFL_DIR_M2M
;
739 set_bit(V4L2_FL_QUIRK_INVERTED_CROP
, &vfd
->flags
);
741 snprintf(vfd
->name
, sizeof(vfd
->name
), "fimc.%d.m2m", fimc
->id
);
742 video_set_drvdata(vfd
, fimc
);
744 fimc
->m2m
.m2m_dev
= v4l2_m2m_init(&m2m_ops
);
745 if (IS_ERR(fimc
->m2m
.m2m_dev
)) {
746 v4l2_err(v4l2_dev
, "failed to initialize v4l2-m2m device\n");
747 return PTR_ERR(fimc
->m2m
.m2m_dev
);
750 ret
= media_entity_pads_init(&vfd
->entity
, 0, NULL
);
754 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
758 v4l2_info(v4l2_dev
, "Registered %s as /dev/%s\n",
759 vfd
->name
, video_device_node_name(vfd
));
763 media_entity_cleanup(&vfd
->entity
);
765 v4l2_m2m_release(fimc
->m2m
.m2m_dev
);
769 void fimc_unregister_m2m_device(struct fimc_dev
*fimc
)
774 if (fimc
->m2m
.m2m_dev
)
775 v4l2_m2m_release(fimc
->m2m
.m2m_dev
);
777 if (video_is_registered(&fimc
->m2m
.vfd
)) {
778 video_unregister_device(&fimc
->m2m
.vfd
);
779 media_entity_cleanup(&fimc
->m2m
.vfd
.entity
);