]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/media/platform/exynos-gsc/gsc-m2m.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-eoan-kernel.git] / drivers / media / platform / exynos-gsc / gsc-m2m.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
5d718338
SK
2/*
3 * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
6 * Samsung EXYNOS5 SoC series G-Scaler driver
5d718338
SK
7 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
5d718338
SK
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>
19#include <linux/io.h>
20#include <linux/slab.h>
21#include <linux/clk.h>
22
23#include <media/v4l2-ioctl.h>
24
25#include "gsc-core.h"
26
27static int gsc_m2m_ctx_stop_req(struct gsc_ctx *ctx)
28{
29 struct gsc_ctx *curr_ctx;
30 struct gsc_dev *gsc = ctx->gsc_dev;
31 int ret;
32
33 curr_ctx = v4l2_m2m_get_curr_priv(gsc->m2m.m2m_dev);
34 if (!gsc_m2m_pending(gsc) || (curr_ctx != ctx))
35 return 0;
36
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);
41
42 return ret == 0 ? -ETIMEDOUT : ret;
43}
44
d9315160
SAB
45static void __gsc_m2m_job_abort(struct gsc_ctx *ctx)
46{
47 int ret;
48
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);
53 }
54}
55
5d718338
SK
56static int gsc_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
57{
58 struct gsc_ctx *ctx = q->drv_priv;
59 int ret;
60
61 ret = pm_runtime_get_sync(&ctx->gsc_dev->pdev->dev);
62 return ret > 0 ? 0 : ret;
63}
64
76ca0824
JMC
65static void __gsc_m2m_cleanup_queue(struct gsc_ctx *ctx)
66{
67 struct vb2_v4l2_buffer *src_vb, *dst_vb;
68
69 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
70 src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
71 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
72 }
73
74 while (v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) > 0) {
75 dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
76 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
77 }
78}
79
e37559b2 80static void gsc_m2m_stop_streaming(struct vb2_queue *q)
5d718338
SK
81{
82 struct gsc_ctx *ctx = q->drv_priv;
5d718338 83
d9315160 84 __gsc_m2m_job_abort(ctx);
5d718338 85
76ca0824
JMC
86 __gsc_m2m_cleanup_queue(ctx);
87
5d718338 88 pm_runtime_put(&ctx->gsc_dev->pdev->dev);
5d718338
SK
89}
90
91void gsc_m2m_job_finish(struct gsc_ctx *ctx, int vb_state)
92{
2d700715 93 struct vb2_v4l2_buffer *src_vb, *dst_vb;
5d718338
SK
94
95 if (!ctx || !ctx->m2m_ctx)
96 return;
97
98 src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
99 dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
100
101 if (src_vb && dst_vb) {
d6dd645e 102 dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
2d700715
JS
103 dst_vb->timecode = src_vb->timecode;
104 dst_vb->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
105 dst_vb->flags |=
106 src_vb->flags
309f4d62 107 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
9c303ec6 108
5d718338
SK
109 v4l2_m2m_buf_done(src_vb, vb_state);
110 v4l2_m2m_buf_done(dst_vb, vb_state);
111
112 v4l2_m2m_job_finish(ctx->gsc_dev->m2m.m2m_dev,
113 ctx->m2m_ctx);
114 }
115}
116
5d718338
SK
117static void gsc_m2m_job_abort(void *priv)
118{
d9315160 119 __gsc_m2m_job_abort((struct gsc_ctx *)priv);
5d718338
SK
120}
121
f60e160e 122static int gsc_get_bufs(struct gsc_ctx *ctx)
5d718338
SK
123{
124 struct gsc_frame *s_frame, *d_frame;
2d700715 125 struct vb2_v4l2_buffer *src_vb, *dst_vb;
5d718338
SK
126 int ret;
127
128 s_frame = &ctx->s_frame;
129 d_frame = &ctx->d_frame;
130
f60e160e 131 src_vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
2d700715 132 ret = gsc_prepare_addr(ctx, &src_vb->vb2_buf, s_frame, &s_frame->addr);
f60e160e
SAB
133 if (ret)
134 return ret;
135
136 dst_vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2d700715 137 ret = gsc_prepare_addr(ctx, &dst_vb->vb2_buf, d_frame, &d_frame->addr);
5d718338
SK
138 if (ret)
139 return ret;
140
d6dd645e 141 dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
f60e160e
SAB
142
143 return 0;
5d718338
SK
144}
145
146static void gsc_m2m_device_run(void *priv)
147{
148 struct gsc_ctx *ctx = priv;
149 struct gsc_dev *gsc;
150 unsigned long flags;
4bd0e030 151 int ret;
5d718338
SK
152 bool is_set = false;
153
154 if (WARN(!ctx, "null hardware context\n"))
155 return;
156
157 gsc = ctx->gsc_dev;
158 spin_lock_irqsave(&gsc->slock, flags);
159
160 set_bit(ST_M2M_PEND, &gsc->state);
161
162 /* Reconfigure hardware if the context has changed. */
163 if (gsc->m2m.ctx != ctx) {
164 pr_debug("gsc->m2m.ctx = 0x%p, current_ctx = 0x%p",
165 gsc->m2m.ctx, ctx);
166 ctx->state |= GSC_PARAMS;
167 gsc->m2m.ctx = ctx;
168 }
169
d9315160 170 is_set = ctx->state & GSC_CTX_STOP_REQ;
5d718338 171 if (is_set) {
d9315160
SAB
172 ctx->state &= ~GSC_CTX_STOP_REQ;
173 ctx->state |= GSC_CTX_ABORT;
5d718338
SK
174 wake_up(&gsc->irq_queue);
175 goto put_device;
176 }
177
f60e160e 178 ret = gsc_get_bufs(ctx);
5d718338
SK
179 if (ret) {
180 pr_err("Wrong address");
181 goto put_device;
182 }
183
184 gsc_set_prefbuf(gsc, &ctx->s_frame);
185 gsc_hw_set_input_addr(gsc, &ctx->s_frame.addr, GSC_M2M_BUF_NUM);
186 gsc_hw_set_output_addr(gsc, &ctx->d_frame.addr, GSC_M2M_BUF_NUM);
187
188 if (ctx->state & GSC_PARAMS) {
189 gsc_hw_set_input_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
190 gsc_hw_set_output_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
191 gsc_hw_set_frm_done_irq_mask(gsc, false);
192 gsc_hw_set_gsc_irq_enable(gsc, true);
193
194 if (gsc_set_scaler_info(ctx)) {
195 pr_err("Scaler setup error");
196 goto put_device;
197 }
198
199 gsc_hw_set_input_path(ctx);
200 gsc_hw_set_in_size(ctx);
201 gsc_hw_set_in_image_format(ctx);
202
203 gsc_hw_set_output_path(ctx);
204 gsc_hw_set_out_size(ctx);
205 gsc_hw_set_out_image_format(ctx);
206
207 gsc_hw_set_prescaler(ctx);
208 gsc_hw_set_mainscaler(ctx);
209 gsc_hw_set_rotation(ctx);
210 gsc_hw_set_global_alpha(ctx);
211 }
212
213 /* update shadow registers */
214 gsc_hw_set_sfr_update(ctx);
215
216 ctx->state &= ~GSC_PARAMS;
217 gsc_hw_enable_control(gsc, true);
218
219 spin_unlock_irqrestore(&gsc->slock, flags);
220 return;
221
222put_device:
223 ctx->state &= ~GSC_PARAMS;
224 spin_unlock_irqrestore(&gsc->slock, flags);
225}
226
227static int gsc_m2m_queue_setup(struct vb2_queue *vq,
5d718338 228 unsigned int *num_buffers, unsigned int *num_planes,
36c0f8b3 229 unsigned int sizes[], struct device *alloc_devs[])
5d718338
SK
230{
231 struct gsc_ctx *ctx = vb2_get_drv_priv(vq);
232 struct gsc_frame *frame;
233 int i;
234
235 frame = ctx_get_frame(ctx, vq->type);
236 if (IS_ERR(frame))
237 return PTR_ERR(frame);
238
239 if (!frame->fmt)
240 return -EINVAL;
241
242 *num_planes = frame->fmt->num_planes;
c781e4a5 243 for (i = 0; i < frame->fmt->num_planes; i++)
5d718338 244 sizes[i] = frame->payload[i];
5d718338
SK
245 return 0;
246}
247
248static int gsc_m2m_buf_prepare(struct vb2_buffer *vb)
249{
250 struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
251 struct gsc_frame *frame;
252 int i;
253
254 frame = ctx_get_frame(ctx, vb->vb2_queue->type);
255 if (IS_ERR(frame))
256 return PTR_ERR(frame);
257
258 if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
259 for (i = 0; i < frame->fmt->num_planes; i++)
260 vb2_set_plane_payload(vb, i, frame->payload[i]);
261 }
262
263 return 0;
264}
265
266static void gsc_m2m_buf_queue(struct vb2_buffer *vb)
267{
2d700715 268 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
5d718338
SK
269 struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
270
271 pr_debug("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
272
273 if (ctx->m2m_ctx)
2d700715 274 v4l2_m2m_buf_queue(ctx->m2m_ctx, vbuf);
5d718338
SK
275}
276
b7b361f0 277static const struct vb2_ops gsc_m2m_qops = {
5d718338
SK
278 .queue_setup = gsc_m2m_queue_setup,
279 .buf_prepare = gsc_m2m_buf_prepare,
280 .buf_queue = gsc_m2m_buf_queue,
0637f054
PL
281 .wait_prepare = vb2_ops_wait_prepare,
282 .wait_finish = vb2_ops_wait_finish,
5d718338
SK
283 .stop_streaming = gsc_m2m_stop_streaming,
284 .start_streaming = gsc_m2m_start_streaming,
285};
286
287static int gsc_m2m_querycap(struct file *file, void *fh,
288 struct v4l2_capability *cap)
289{
290 struct gsc_ctx *ctx = fh_to_ctx(fh);
291 struct gsc_dev *gsc = ctx->gsc_dev;
292
c0decac1
MCC
293 strscpy(cap->driver, GSC_MODULE_NAME, sizeof(cap->driver));
294 strscpy(cap->card, GSC_MODULE_NAME " gscaler", sizeof(cap->card));
3a5a2ac0
JMC
295 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
296 dev_name(&gsc->pdev->dev));
442dd067 297 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
5d718338
SK
298 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
299 return 0;
300}
301
302static int gsc_m2m_enum_fmt_mplane(struct file *file, void *priv,
303 struct v4l2_fmtdesc *f)
304{
305 return gsc_enum_fmt_mplane(f);
306}
307
308static int gsc_m2m_g_fmt_mplane(struct file *file, void *fh,
309 struct v4l2_format *f)
310{
311 struct gsc_ctx *ctx = fh_to_ctx(fh);
312
313 return gsc_g_fmt_mplane(ctx, f);
314}
315
316static int gsc_m2m_try_fmt_mplane(struct file *file, void *fh,
317 struct v4l2_format *f)
318{
319 struct gsc_ctx *ctx = fh_to_ctx(fh);
320
321 return gsc_try_fmt_mplane(ctx, f);
322}
323
324static int gsc_m2m_s_fmt_mplane(struct file *file, void *fh,
325 struct v4l2_format *f)
326{
327 struct gsc_ctx *ctx = fh_to_ctx(fh);
328 struct vb2_queue *vq;
329 struct gsc_frame *frame;
330 struct v4l2_pix_format_mplane *pix;
331 int i, ret = 0;
332
333 ret = gsc_m2m_try_fmt_mplane(file, fh, f);
334 if (ret)
335 return ret;
336
337 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
338
339 if (vb2_is_streaming(vq)) {
340 pr_err("queue (%d) busy", f->type);
341 return -EBUSY;
342 }
343
344 if (V4L2_TYPE_IS_OUTPUT(f->type))
345 frame = &ctx->s_frame;
346 else
347 frame = &ctx->d_frame;
348
349 pix = &f->fmt.pix_mp;
350 frame->fmt = find_fmt(&pix->pixelformat, NULL, 0);
351 frame->colorspace = pix->colorspace;
352 if (!frame->fmt)
353 return -EINVAL;
354
355 for (i = 0; i < frame->fmt->num_planes; i++)
356 frame->payload[i] = pix->plane_fmt[i].sizeimage;
357
358 gsc_set_frame_size(frame, pix->width, pix->height);
359
360 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
361 gsc_ctx_state_lock_set(GSC_PARAMS | GSC_DST_FMT, ctx);
362 else
363 gsc_ctx_state_lock_set(GSC_PARAMS | GSC_SRC_FMT, ctx);
364
365 pr_debug("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
366
367 return 0;
368}
369
370static int gsc_m2m_reqbufs(struct file *file, void *fh,
371 struct v4l2_requestbuffers *reqbufs)
372{
373 struct gsc_ctx *ctx = fh_to_ctx(fh);
374 struct gsc_dev *gsc = ctx->gsc_dev;
5d718338
SK
375 u32 max_cnt;
376
377 max_cnt = (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
378 gsc->variant->in_buf_cnt : gsc->variant->out_buf_cnt;
daba4dfb 379 if (reqbufs->count > max_cnt)
5d718338 380 return -EINVAL;
5d718338 381
5d718338
SK
382 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
383}
384
371a664e
SAB
385static int gsc_m2m_expbuf(struct file *file, void *fh,
386 struct v4l2_exportbuffer *eb)
387{
388 struct gsc_ctx *ctx = fh_to_ctx(fh);
389 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
390}
391
5d718338
SK
392static int gsc_m2m_querybuf(struct file *file, void *fh,
393 struct v4l2_buffer *buf)
394{
395 struct gsc_ctx *ctx = fh_to_ctx(fh);
396 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
397}
398
399static int gsc_m2m_qbuf(struct file *file, void *fh,
400 struct v4l2_buffer *buf)
401{
402 struct gsc_ctx *ctx = fh_to_ctx(fh);
403 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
404}
405
406static int gsc_m2m_dqbuf(struct file *file, void *fh,
407 struct v4l2_buffer *buf)
408{
409 struct gsc_ctx *ctx = fh_to_ctx(fh);
410 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
411}
412
413static int gsc_m2m_streamon(struct file *file, void *fh,
414 enum v4l2_buf_type type)
415{
416 struct gsc_ctx *ctx = fh_to_ctx(fh);
417
418 /* The source and target color format need to be set */
419 if (V4L2_TYPE_IS_OUTPUT(type)) {
420 if (!gsc_ctx_state_is_set(GSC_SRC_FMT, ctx))
421 return -EINVAL;
422 } else if (!gsc_ctx_state_is_set(GSC_DST_FMT, ctx)) {
423 return -EINVAL;
424 }
425
426 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
427}
428
429static int gsc_m2m_streamoff(struct file *file, void *fh,
430 enum v4l2_buf_type type)
431{
432 struct gsc_ctx *ctx = fh_to_ctx(fh);
433 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
434}
435
436/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
437static int is_rectangle_enclosed(struct v4l2_rect *a, struct v4l2_rect *b)
438{
439 if (a->left < b->left || a->top < b->top)
440 return 0;
441
442 if (a->left + a->width > b->left + b->width)
443 return 0;
444
445 if (a->top + a->height > b->top + b->height)
446 return 0;
447
448 return 1;
449}
450
451static int gsc_m2m_g_selection(struct file *file, void *fh,
452 struct v4l2_selection *s)
453{
454 struct gsc_frame *frame;
455 struct gsc_ctx *ctx = fh_to_ctx(fh);
456
eaec420f
HV
457 if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
458 (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
5d718338
SK
459 return -EINVAL;
460
461 frame = ctx_get_frame(ctx, s->type);
462 if (IS_ERR(frame))
463 return PTR_ERR(frame);
464
465 switch (s->target) {
466 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
467 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
468 case V4L2_SEL_TGT_CROP_BOUNDS:
469 case V4L2_SEL_TGT_CROP_DEFAULT:
470 s->r.left = 0;
471 s->r.top = 0;
472 s->r.width = frame->f_width;
473 s->r.height = frame->f_height;
474 return 0;
475
476 case V4L2_SEL_TGT_COMPOSE:
477 case V4L2_SEL_TGT_CROP:
478 s->r.left = frame->crop.left;
479 s->r.top = frame->crop.top;
480 s->r.width = frame->crop.width;
481 s->r.height = frame->crop.height;
482 return 0;
483 }
484
485 return -EINVAL;
486}
487
488static int gsc_m2m_s_selection(struct file *file, void *fh,
489 struct v4l2_selection *s)
490{
491 struct gsc_frame *frame;
492 struct gsc_ctx *ctx = fh_to_ctx(fh);
5d718338 493 struct gsc_variant *variant = ctx->gsc_dev->variant;
9ad763d0 494 struct v4l2_selection sel = *s;
5d718338
SK
495 int ret;
496
eaec420f
HV
497 if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
498 (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
5d718338
SK
499 return -EINVAL;
500
9ad763d0 501 ret = gsc_try_selection(ctx, &sel);
5d718338
SK
502 if (ret)
503 return ret;
504
505 if (s->flags & V4L2_SEL_FLAG_LE &&
9ad763d0 506 !is_rectangle_enclosed(&sel.r, &s->r))
5d718338
SK
507 return -ERANGE;
508
509 if (s->flags & V4L2_SEL_FLAG_GE &&
9ad763d0 510 !is_rectangle_enclosed(&s->r, &sel.r))
5d718338
SK
511 return -ERANGE;
512
9ad763d0 513 s->r = sel.r;
5d718338
SK
514
515 switch (s->target) {
516 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
517 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
518 case V4L2_SEL_TGT_COMPOSE:
519 frame = &ctx->s_frame;
520 break;
521
522 case V4L2_SEL_TGT_CROP_BOUNDS:
523 case V4L2_SEL_TGT_CROP:
524 case V4L2_SEL_TGT_CROP_DEFAULT:
525 frame = &ctx->d_frame;
526 break;
527
528 default:
529 return -EINVAL;
530 }
531
532 /* Check to see if scaling ratio is within supported range */
533 if (gsc_ctx_state_is_set(GSC_DST_FMT | GSC_SRC_FMT, ctx)) {
534 if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
9ad763d0
HV
535 ret = gsc_check_scaler_ratio(variant, sel.r.width,
536 sel.r.height, ctx->d_frame.crop.width,
5d718338
SK
537 ctx->d_frame.crop.height,
538 ctx->gsc_ctrls.rotate->val, ctx->out_path);
539 } else {
540 ret = gsc_check_scaler_ratio(variant,
541 ctx->s_frame.crop.width,
9ad763d0
HV
542 ctx->s_frame.crop.height, sel.r.width,
543 sel.r.height, ctx->gsc_ctrls.rotate->val,
5d718338
SK
544 ctx->out_path);
545 }
546
547 if (ret) {
548 pr_err("Out of scaler range");
549 return -EINVAL;
550 }
551 }
552
9ad763d0 553 frame->crop = sel.r;
5d718338
SK
554
555 gsc_ctx_state_lock_set(GSC_PARAMS, ctx);
556 return 0;
557}
558
559static const struct v4l2_ioctl_ops gsc_m2m_ioctl_ops = {
560 .vidioc_querycap = gsc_m2m_querycap,
561 .vidioc_enum_fmt_vid_cap_mplane = gsc_m2m_enum_fmt_mplane,
562 .vidioc_enum_fmt_vid_out_mplane = gsc_m2m_enum_fmt_mplane,
563 .vidioc_g_fmt_vid_cap_mplane = gsc_m2m_g_fmt_mplane,
564 .vidioc_g_fmt_vid_out_mplane = gsc_m2m_g_fmt_mplane,
565 .vidioc_try_fmt_vid_cap_mplane = gsc_m2m_try_fmt_mplane,
566 .vidioc_try_fmt_vid_out_mplane = gsc_m2m_try_fmt_mplane,
567 .vidioc_s_fmt_vid_cap_mplane = gsc_m2m_s_fmt_mplane,
568 .vidioc_s_fmt_vid_out_mplane = gsc_m2m_s_fmt_mplane,
569 .vidioc_reqbufs = gsc_m2m_reqbufs,
371a664e 570 .vidioc_expbuf = gsc_m2m_expbuf,
5d718338
SK
571 .vidioc_querybuf = gsc_m2m_querybuf,
572 .vidioc_qbuf = gsc_m2m_qbuf,
573 .vidioc_dqbuf = gsc_m2m_dqbuf,
574 .vidioc_streamon = gsc_m2m_streamon,
575 .vidioc_streamoff = gsc_m2m_streamoff,
576 .vidioc_g_selection = gsc_m2m_g_selection,
577 .vidioc_s_selection = gsc_m2m_s_selection
578};
579
580static int queue_init(void *priv, struct vb2_queue *src_vq,
581 struct vb2_queue *dst_vq)
582{
583 struct gsc_ctx *ctx = priv;
584 int ret;
585
586 memset(src_vq, 0, sizeof(*src_vq));
587 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
371a664e 588 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
5d718338
SK
589 src_vq->drv_priv = ctx;
590 src_vq->ops = &gsc_m2m_qops;
591 src_vq->mem_ops = &vb2_dma_contig_memops;
592 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
ade48681 593 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
0637f054 594 src_vq->lock = &ctx->gsc_dev->lock;
c781e4a5 595 src_vq->dev = &ctx->gsc_dev->pdev->dev;
5d718338
SK
596
597 ret = vb2_queue_init(src_vq);
598 if (ret)
599 return ret;
600
601 memset(dst_vq, 0, sizeof(*dst_vq));
602 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
371a664e 603 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
5d718338
SK
604 dst_vq->drv_priv = ctx;
605 dst_vq->ops = &gsc_m2m_qops;
606 dst_vq->mem_ops = &vb2_dma_contig_memops;
607 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
ade48681 608 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
0637f054 609 dst_vq->lock = &ctx->gsc_dev->lock;
c781e4a5 610 dst_vq->dev = &ctx->gsc_dev->pdev->dev;
5d718338
SK
611
612 return vb2_queue_init(dst_vq);
613}
614
615static int gsc_m2m_open(struct file *file)
616{
617 struct gsc_dev *gsc = video_drvdata(file);
618 struct gsc_ctx *ctx = NULL;
619 int ret;
620
621 pr_debug("pid: %d, state: 0x%lx", task_pid_nr(current), gsc->state);
622
623 if (mutex_lock_interruptible(&gsc->lock))
624 return -ERESTARTSYS;
625
2c8cc13f 626 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
5d718338
SK
627 if (!ctx) {
628 ret = -ENOMEM;
629 goto unlock;
630 }
631
632 v4l2_fh_init(&ctx->fh, gsc->m2m.vfd);
633 ret = gsc_ctrls_create(ctx);
634 if (ret)
635 goto error_fh;
636
637 /* Use separate control handler per file handle */
638 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
639 file->private_data = &ctx->fh;
640 v4l2_fh_add(&ctx->fh);
641
642 ctx->gsc_dev = gsc;
643 /* Default color format */
644 ctx->s_frame.fmt = get_format(0);
645 ctx->d_frame.fmt = get_format(0);
646 /* Setup the device context for mem2mem mode. */
647 ctx->state = GSC_CTX_M2M;
648 ctx->flags = 0;
649 ctx->in_path = GSC_DMA;
650 ctx->out_path = GSC_DMA;
651
652 ctx->m2m_ctx = v4l2_m2m_ctx_init(gsc->m2m.m2m_dev, ctx, queue_init);
653 if (IS_ERR(ctx->m2m_ctx)) {
654 pr_err("Failed to initialize m2m context");
655 ret = PTR_ERR(ctx->m2m_ctx);
656 goto error_ctrls;
657 }
658
659 if (gsc->m2m.refcnt++ == 0)
660 set_bit(ST_M2M_OPEN, &gsc->state);
661
662 pr_debug("gsc m2m driver is opened, ctx(0x%p)", ctx);
663
664 mutex_unlock(&gsc->lock);
665 return 0;
666
667error_ctrls:
668 gsc_ctrls_delete(ctx);
5d718338 669 v4l2_fh_del(&ctx->fh);
3a07a827 670error_fh:
5d718338
SK
671 v4l2_fh_exit(&ctx->fh);
672 kfree(ctx);
673unlock:
674 mutex_unlock(&gsc->lock);
675 return ret;
676}
677
678static int gsc_m2m_release(struct file *file)
679{
680 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
681 struct gsc_dev *gsc = ctx->gsc_dev;
682
683 pr_debug("pid: %d, state: 0x%lx, refcnt= %d",
684 task_pid_nr(current), gsc->state, gsc->m2m.refcnt);
685
98680180 686 mutex_lock(&gsc->lock);
5d718338
SK
687
688 v4l2_m2m_ctx_release(ctx->m2m_ctx);
689 gsc_ctrls_delete(ctx);
690 v4l2_fh_del(&ctx->fh);
691 v4l2_fh_exit(&ctx->fh);
692
693 if (--gsc->m2m.refcnt <= 0)
694 clear_bit(ST_M2M_OPEN, &gsc->state);
695 kfree(ctx);
696
697 mutex_unlock(&gsc->lock);
698 return 0;
699}
700
c23e0cb8 701static __poll_t gsc_m2m_poll(struct file *file,
5d718338
SK
702 struct poll_table_struct *wait)
703{
704 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
705 struct gsc_dev *gsc = ctx->gsc_dev;
c23e0cb8 706 __poll_t ret;
5d718338
SK
707
708 if (mutex_lock_interruptible(&gsc->lock))
541b647a 709 return EPOLLERR;
5d718338
SK
710
711 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
712 mutex_unlock(&gsc->lock);
713
714 return ret;
715}
716
717static int gsc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
718{
719 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
720 struct gsc_dev *gsc = ctx->gsc_dev;
721 int ret;
722
723 if (mutex_lock_interruptible(&gsc->lock))
724 return -ERESTARTSYS;
725
726 ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
727 mutex_unlock(&gsc->lock);
728
729 return ret;
730}
731
732static const struct v4l2_file_operations gsc_m2m_fops = {
733 .owner = THIS_MODULE,
734 .open = gsc_m2m_open,
735 .release = gsc_m2m_release,
736 .poll = gsc_m2m_poll,
737 .unlocked_ioctl = video_ioctl2,
738 .mmap = gsc_m2m_mmap,
739};
740
729ce68c 741static const struct v4l2_m2m_ops gsc_m2m_ops = {
5d718338
SK
742 .device_run = gsc_m2m_device_run,
743 .job_abort = gsc_m2m_job_abort,
744};
745
746int gsc_register_m2m_device(struct gsc_dev *gsc)
747{
748 struct platform_device *pdev;
749 int ret;
750
751 if (!gsc)
752 return -ENODEV;
753
754 pdev = gsc->pdev;
755
756 gsc->vdev.fops = &gsc_m2m_fops;
757 gsc->vdev.ioctl_ops = &gsc_m2m_ioctl_ops;
758 gsc->vdev.release = video_device_release_empty;
759 gsc->vdev.lock = &gsc->lock;
24fc681a 760 gsc->vdev.vfl_dir = VFL_DIR_M2M;
d0b1c313 761 gsc->vdev.v4l2_dev = &gsc->v4l2_dev;
5d718338
SK
762 snprintf(gsc->vdev.name, sizeof(gsc->vdev.name), "%s.%d:m2m",
763 GSC_MODULE_NAME, gsc->id);
764
765 video_set_drvdata(&gsc->vdev, gsc);
766
767 gsc->m2m.vfd = &gsc->vdev;
768 gsc->m2m.m2m_dev = v4l2_m2m_init(&gsc_m2m_ops);
769 if (IS_ERR(gsc->m2m.m2m_dev)) {
770 dev_err(&pdev->dev, "failed to initialize v4l2-m2m device\n");
f4ca5030 771 return PTR_ERR(gsc->m2m.m2m_dev);
5d718338
SK
772 }
773
774 ret = video_register_device(&gsc->vdev, VFL_TYPE_GRABBER, -1);
775 if (ret) {
776 dev_err(&pdev->dev,
777 "%s(): failed to register video device\n", __func__);
f4ca5030 778 goto err_m2m_release;
5d718338
SK
779 }
780
781 pr_debug("gsc m2m driver registered as /dev/video%d", gsc->vdev.num);
782 return 0;
783
f4ca5030 784err_m2m_release:
5d718338 785 v4l2_m2m_release(gsc->m2m.m2m_dev);
5d718338
SK
786
787 return ret;
788}
789
790void gsc_unregister_m2m_device(struct gsc_dev *gsc)
791{
115a16ba 792 if (gsc) {
5d718338 793 v4l2_m2m_release(gsc->m2m.m2m_dev);
115a16ba
JMC
794 video_unregister_device(&gsc->vdev);
795 }
5d718338 796}