13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
20 #include <linux/device.h>
22 #include <linux/list.h>
24 #include <linux/slab.h>
31 static int gsc_m2m_ctx_stop_req(
struct gsc_ctx *
ctx)
49 static int gsc_m2m_start_streaming(
struct vb2_queue *
q,
unsigned int count)
54 ret = pm_runtime_get_sync(&ctx->
gsc_dev->pdev->dev);
55 return ret > 0 ? 0 :
ret;
58 static int gsc_m2m_stop_streaming(
struct vb2_queue *
q)
63 ret = gsc_m2m_ctx_stop_req(ctx);
67 pm_runtime_put(&ctx->
gsc_dev->pdev->dev);
79 src_vb = v4l2_m2m_src_buf_remove(ctx->
m2m_ctx);
80 dst_vb = v4l2_m2m_dst_buf_remove(ctx->
m2m_ctx);
82 if (src_vb && dst_vb) {
83 v4l2_m2m_buf_done(src_vb, vb_state);
84 v4l2_m2m_buf_done(dst_vb, vb_state);
92 static void gsc_m2m_job_abort(
void *
priv)
97 ret = gsc_m2m_ctx_stop_req(ctx);
102 static int gsc_fill_addr(
struct gsc_ctx *ctx)
111 vb = v4l2_m2m_next_src_buf(ctx->
m2m_ctx);
116 vb = v4l2_m2m_next_dst_buf(ctx->
m2m_ctx);
120 static void gsc_m2m_device_run(
void *priv)
128 if (
WARN(!ctx,
"null hardware context\n"))
137 if (gsc->
m2m.ctx != ctx) {
138 pr_debug(
"gsc->m2m.ctx = 0x%p, current_ctx = 0x%p",
151 ret = gsc_fill_addr(ctx);
168 pr_err(
"Scaler setup error");
190 gsc_hw_enable_control(gsc,
true);
192 spin_unlock_irqrestore(&gsc->
slock, flags);
197 spin_unlock_irqrestore(&gsc->
slock, flags);
200 static int gsc_m2m_queue_setup(
struct vb2_queue *vq,
202 unsigned int *num_buffers,
unsigned int *
num_planes,
203 unsigned int sizes[],
void *allocators[])
205 struct gsc_ctx *ctx = vb2_get_drv_priv(vq);
209 frame = ctx_get_frame(ctx, vq->
type);
211 return PTR_ERR(frame);
216 *num_planes = frame->
fmt->num_planes;
217 for (i = 0; i < frame->
fmt->num_planes; i++) {
219 allocators[
i] = ctx->
gsc_dev->alloc_ctx;
224 static int gsc_m2m_buf_prepare(
struct vb2_buffer *vb)
230 frame = ctx_get_frame(ctx, vb->
vb2_queue->type);
232 return PTR_ERR(frame);
235 for (i = 0; i < frame->
fmt->num_planes; i++)
236 vb2_set_plane_payload(vb, i, frame->
payload[i]);
242 static void gsc_m2m_buf_queue(
struct vb2_buffer *vb)
252 static struct vb2_ops gsc_m2m_qops = {
253 .queue_setup = gsc_m2m_queue_setup,
254 .buf_prepare = gsc_m2m_buf_prepare,
255 .buf_queue = gsc_m2m_buf_queue,
256 .wait_prepare = gsc_unlock,
257 .wait_finish = gsc_lock,
258 .stop_streaming = gsc_m2m_stop_streaming,
259 .start_streaming = gsc_m2m_start_streaming,
262 static int gsc_m2m_querycap(
struct file *
file,
void *fh,
278 static int gsc_m2m_enum_fmt_mplane(
struct file *
file,
void *priv,
284 static int gsc_m2m_g_fmt_mplane(
struct file *
file,
void *fh,
292 static int gsc_m2m_try_fmt_mplane(
struct file *
file,
void *
fh,
300 static int gsc_m2m_s_fmt_mplane(
struct file *
file,
void *
fh,
309 ret = gsc_m2m_try_fmt_mplane(file, fh, f);
315 if (vb2_is_streaming(vq)) {
331 for (i = 0; i < frame->
fmt->num_planes; i++)
346 static int gsc_m2m_reqbufs(
struct file *file,
void *fh,
356 if (reqbufs->
count > max_cnt) {
358 }
else if (reqbufs->
count == 0) {
365 frame = ctx_get_frame(ctx, reqbufs->
type);
370 static int gsc_m2m_querybuf(
struct file *file,
void *fh,
377 static int gsc_m2m_qbuf(
struct file *file,
void *fh,
384 static int gsc_m2m_dqbuf(
struct file *file,
void *fh,
391 static int gsc_m2m_streamon(
struct file *file,
void *fh,
400 }
else if (!gsc_ctx_state_is_set(
GSC_DST_FMT, ctx)) {
407 static int gsc_m2m_streamoff(
struct file *file,
void *fh,
429 static int gsc_m2m_g_selection(
struct file *file,
void *fh,
439 frame = ctx_get_frame(ctx, s->
type);
441 return PTR_ERR(frame);
456 s->
r.left = frame->
crop.left;
457 s->
r.top = frame->
crop.top;
458 s->
r.width = frame->
crop.width;
459 s->
r.height = frame->
crop.height;
466 static int gsc_m2m_s_selection(
struct file *file,
void *fh,
487 !is_rectangle_enclosed(&
cr.c, &s->
r))
491 !is_rectangle_enclosed(&s->
r, &
cr.c))
529 pr_err(
"Out of scaler range");
541 .vidioc_querycap = gsc_m2m_querycap,
542 .vidioc_enum_fmt_vid_cap_mplane = gsc_m2m_enum_fmt_mplane,
543 .vidioc_enum_fmt_vid_out_mplane = gsc_m2m_enum_fmt_mplane,
544 .vidioc_g_fmt_vid_cap_mplane = gsc_m2m_g_fmt_mplane,
545 .vidioc_g_fmt_vid_out_mplane = gsc_m2m_g_fmt_mplane,
546 .vidioc_try_fmt_vid_cap_mplane = gsc_m2m_try_fmt_mplane,
547 .vidioc_try_fmt_vid_out_mplane = gsc_m2m_try_fmt_mplane,
548 .vidioc_s_fmt_vid_cap_mplane = gsc_m2m_s_fmt_mplane,
549 .vidioc_s_fmt_vid_out_mplane = gsc_m2m_s_fmt_mplane,
550 .vidioc_reqbufs = gsc_m2m_reqbufs,
551 .vidioc_querybuf = gsc_m2m_querybuf,
552 .vidioc_qbuf = gsc_m2m_qbuf,
553 .vidioc_dqbuf = gsc_m2m_dqbuf,
554 .vidioc_streamon = gsc_m2m_streamon,
555 .vidioc_streamoff = gsc_m2m_streamoff,
556 .vidioc_g_selection = gsc_m2m_g_selection,
557 .vidioc_s_selection = gsc_m2m_s_selection
560 static int queue_init(
void *priv,
struct vb2_queue *src_vq,
566 memset(src_vq, 0,
sizeof(*src_vq));
570 src_vq->
ops = &gsc_m2m_qops;
578 memset(dst_vq, 0,
sizeof(*dst_vq));
582 dst_vq->
ops = &gsc_m2m_qops;
589 static int gsc_m2m_open(
struct file *file)
591 struct gsc_dev *gsc = video_drvdata(file);
628 pr_err(
"Failed to initialize m2m context");
633 if (gsc->
m2m.refcnt++ == 0)
636 pr_debug(
"gsc m2m driver is opened, ctx(0x%p)", ctx);
652 static int gsc_m2m_release(
struct file *file)
657 pr_debug(
"pid: %d, state: 0x%lx, refcnt= %d",
667 if (--gsc->
m2m.refcnt <= 0)
675 static unsigned int gsc_m2m_poll(
struct file *file,
691 static int gsc_m2m_mmap(
struct file *file,
struct vm_area_struct *vma)
708 .open = gsc_m2m_open,
709 .release = gsc_m2m_release,
710 .poll = gsc_m2m_poll,
712 .mmap = gsc_m2m_mmap,
716 .device_run = gsc_m2m_device_run,
717 .job_abort = gsc_m2m_job_abort,
730 gsc->
vdev.fops = &gsc_m2m_fops;
731 gsc->
vdev.ioctl_ops = &gsc_m2m_ioctl_ops;
738 video_set_drvdata(&gsc->
vdev, gsc);
742 if (IS_ERR(gsc->
m2m.m2m_dev)) {
743 dev_err(&pdev->
dev,
"failed to initialize v4l2-m2m device\n");
744 ret = PTR_ERR(gsc->
m2m.m2m_dev);
751 "%s(): failed to register video device\n", __func__);
755 pr_debug(
"gsc m2m driver registered as /dev/video%d", gsc->
vdev.num);