Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / s5p-fimc / fimc-m2m.c
1 /*
2  * Samsung S5P/EXYNOS4 SoC series FIMC (video postprocessor) driver
3  *
4  * Copyright (C) 2012 Samsung Electronics Co., Ltd.
5  * Sylwester Nawrocki, <s.nawrocki@samsung.com>
6  *
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.
11  */
12
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>
23 #include <linux/io.h>
24 #include <linux/slab.h>
25 #include <linux/clk.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
29
30 #include "fimc-core.h"
31 #include "fimc-reg.h"
32 #include "fimc-mdevice.h"
33
34
35 static unsigned int get_m2m_fmt_flags(unsigned int stream_type)
36 {
37         if (stream_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
38                 return FMT_FLAGS_M2M_IN;
39         else
40                 return FMT_FLAGS_M2M_OUT;
41 }
42
43 void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
44 {
45         struct vb2_buffer *src_vb, *dst_vb;
46
47         if (!ctx || !ctx->m2m_ctx)
48                 return;
49
50         src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
51         dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
52
53         if (src_vb && dst_vb) {
54                 v4l2_m2m_buf_done(src_vb, vb_state);
55                 v4l2_m2m_buf_done(dst_vb, vb_state);
56                 v4l2_m2m_job_finish(ctx->fimc_dev->m2m.m2m_dev,
57                                     ctx->m2m_ctx);
58         }
59 }
60
61 /* Complete the transaction which has been scheduled for execution. */
62 static int fimc_m2m_shutdown(struct fimc_ctx *ctx)
63 {
64         struct fimc_dev *fimc = ctx->fimc_dev;
65         int ret;
66
67         if (!fimc_m2m_pending(fimc))
68                 return 0;
69
70         fimc_ctx_state_set(FIMC_CTX_SHUT, ctx);
71
72         ret = wait_event_timeout(fimc->irq_queue,
73                            !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx),
74                            FIMC_SHUTDOWN_TIMEOUT);
75
76         return ret == 0 ? -ETIMEDOUT : ret;
77 }
78
79 static int start_streaming(struct vb2_queue *q, unsigned int count)
80 {
81         struct fimc_ctx *ctx = q->drv_priv;
82         int ret;
83
84         ret = pm_runtime_get_sync(&ctx->fimc_dev->pdev->dev);
85         return ret > 0 ? 0 : ret;
86 }
87
88 static int stop_streaming(struct vb2_queue *q)
89 {
90         struct fimc_ctx *ctx = q->drv_priv;
91         int ret;
92
93         ret = fimc_m2m_shutdown(ctx);
94         if (ret == -ETIMEDOUT)
95                 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
96
97         pm_runtime_put(&ctx->fimc_dev->pdev->dev);
98         return 0;
99 }
100
101 static void fimc_device_run(void *priv)
102 {
103         struct vb2_buffer *vb = NULL;
104         struct fimc_ctx *ctx = priv;
105         struct fimc_frame *sf, *df;
106         struct fimc_dev *fimc;
107         unsigned long flags;
108         u32 ret;
109
110         if (WARN(!ctx, "Null context\n"))
111                 return;
112
113         fimc = ctx->fimc_dev;
114         spin_lock_irqsave(&fimc->slock, flags);
115
116         set_bit(ST_M2M_PEND, &fimc->state);
117         sf = &ctx->s_frame;
118         df = &ctx->d_frame;
119
120         if (ctx->state & FIMC_PARAMS) {
121                 /* Prepare the DMA offsets for scaler */
122                 fimc_prepare_dma_offset(ctx, sf);
123                 fimc_prepare_dma_offset(ctx, df);
124         }
125
126         vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
127         ret = fimc_prepare_addr(ctx, vb, sf, &sf->paddr);
128         if (ret)
129                 goto dma_unlock;
130
131         vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
132         ret = fimc_prepare_addr(ctx, vb, df, &df->paddr);
133         if (ret)
134                 goto dma_unlock;
135
136         /* Reconfigure hardware if the context has changed. */
137         if (fimc->m2m.ctx != ctx) {
138                 ctx->state |= FIMC_PARAMS;
139                 fimc->m2m.ctx = ctx;
140         }
141
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);
147                 if (ret)
148                         goto dma_unlock;
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->variant->has_alpha)
156                         fimc_hw_set_rgb_alpha(ctx);
157                 fimc_hw_set_output_path(ctx);
158         }
159         fimc_hw_set_input_addr(fimc, &sf->paddr);
160         fimc_hw_set_output_addr(fimc, &df->paddr, -1);
161
162         fimc_activate_capture(ctx);
163         ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
164                        FIMC_SRC_FMT | FIMC_DST_FMT);
165         fimc_hw_activate_input_dma(fimc, true);
166
167 dma_unlock:
168         spin_unlock_irqrestore(&fimc->slock, flags);
169 }
170
171 static void fimc_job_abort(void *priv)
172 {
173         fimc_m2m_shutdown(priv);
174 }
175
176 static int fimc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
177                             unsigned int *num_buffers, unsigned int *num_planes,
178                             unsigned int sizes[], void *allocators[])
179 {
180         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
181         struct fimc_frame *f;
182         int i;
183
184         f = ctx_get_frame(ctx, vq->type);
185         if (IS_ERR(f))
186                 return PTR_ERR(f);
187         /*
188          * Return number of non-contigous planes (plane buffers)
189          * depending on the configured color format.
190          */
191         if (!f->fmt)
192                 return -EINVAL;
193
194         *num_planes = f->fmt->memplanes;
195         for (i = 0; i < f->fmt->memplanes; i++) {
196                 sizes[i] = (f->f_width * f->f_height * f->fmt->depth[i]) / 8;
197                 allocators[i] = ctx->fimc_dev->alloc_ctx;
198         }
199         return 0;
200 }
201
202 static int fimc_buf_prepare(struct vb2_buffer *vb)
203 {
204         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
205         struct fimc_frame *frame;
206         int i;
207
208         frame = ctx_get_frame(ctx, vb->vb2_queue->type);
209         if (IS_ERR(frame))
210                 return PTR_ERR(frame);
211
212         for (i = 0; i < frame->fmt->memplanes; i++)
213                 vb2_set_plane_payload(vb, i, frame->payload[i]);
214
215         return 0;
216 }
217
218 static void fimc_buf_queue(struct vb2_buffer *vb)
219 {
220         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
221
222         dbg("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
223
224         if (ctx->m2m_ctx)
225                 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
226 }
227
228 static void fimc_lock(struct vb2_queue *vq)
229 {
230         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
231         mutex_lock(&ctx->fimc_dev->lock);
232 }
233
234 static void fimc_unlock(struct vb2_queue *vq)
235 {
236         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
237         mutex_unlock(&ctx->fimc_dev->lock);
238 }
239
240 static struct vb2_ops fimc_qops = {
241         .queue_setup     = fimc_queue_setup,
242         .buf_prepare     = fimc_buf_prepare,
243         .buf_queue       = fimc_buf_queue,
244         .wait_prepare    = fimc_unlock,
245         .wait_finish     = fimc_lock,
246         .stop_streaming  = stop_streaming,
247         .start_streaming = start_streaming,
248 };
249
250 /*
251  * V4L2 ioctl handlers
252  */
253 static int fimc_m2m_querycap(struct file *file, void *fh,
254                              struct v4l2_capability *cap)
255 {
256         struct fimc_ctx *ctx = fh_to_ctx(fh);
257         struct fimc_dev *fimc = ctx->fimc_dev;
258
259         strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
260         strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
261         cap->bus_info[0] = 0;
262         cap->capabilities = V4L2_CAP_STREAMING |
263                 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
264
265         return 0;
266 }
267
268 static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
269                                     struct v4l2_fmtdesc *f)
270 {
271         struct fimc_fmt *fmt;
272
273         fmt = fimc_find_format(NULL, NULL, get_m2m_fmt_flags(f->type),
274                                f->index);
275         if (!fmt)
276                 return -EINVAL;
277
278         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
279         f->pixelformat = fmt->fourcc;
280         return 0;
281 }
282
283 static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
284                                  struct v4l2_format *f)
285 {
286         struct fimc_ctx *ctx = fh_to_ctx(fh);
287         struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
288
289         if (IS_ERR(frame))
290                 return PTR_ERR(frame);
291
292         return fimc_fill_format(frame, f);
293 }
294
295 static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
296 {
297         struct fimc_dev *fimc = ctx->fimc_dev;
298         struct fimc_variant *variant = fimc->variant;
299         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
300         struct fimc_fmt *fmt;
301         u32 max_w, mod_x, mod_y;
302
303         if (!IS_M2M(f->type))
304                 return -EINVAL;
305
306         dbg("w: %d, h: %d", pix->width, pix->height);
307
308         fmt = fimc_find_format(&pix->pixelformat, NULL,
309                                get_m2m_fmt_flags(f->type), 0);
310         if (WARN(fmt == NULL, "Pixel format lookup failed"))
311                 return -EINVAL;
312
313         if (pix->field == V4L2_FIELD_ANY)
314                 pix->field = V4L2_FIELD_NONE;
315         else if (pix->field != V4L2_FIELD_NONE)
316                 return -EINVAL;
317
318         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
319                 max_w = variant->pix_limit->scaler_dis_w;
320                 mod_x = ffs(variant->min_inp_pixsize) - 1;
321         } else {
322                 max_w = variant->pix_limit->out_rot_dis_w;
323                 mod_x = ffs(variant->min_out_pixsize) - 1;
324         }
325
326         if (tiled_fmt(fmt)) {
327                 mod_x = 6; /* 64 x 32 pixels tile */
328                 mod_y = 5;
329         } else {
330                 if (variant->min_vsize_align == 1)
331                         mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
332                 else
333                         mod_y = ffs(variant->min_vsize_align) - 1;
334         }
335
336         v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
337                 &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
338
339         fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
340         return 0;
341 }
342
343 static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
344                                    struct v4l2_format *f)
345 {
346         struct fimc_ctx *ctx = fh_to_ctx(fh);
347
348         return fimc_try_fmt_mplane(ctx, f);
349 }
350
351 static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
352                                  struct v4l2_format *f)
353 {
354         struct fimc_ctx *ctx = fh_to_ctx(fh);
355         struct fimc_dev *fimc = ctx->fimc_dev;
356         struct vb2_queue *vq;
357         struct fimc_frame *frame;
358         struct v4l2_pix_format_mplane *pix;
359         int i, ret = 0;
360
361         ret = fimc_try_fmt_mplane(ctx, f);
362         if (ret)
363                 return ret;
364
365         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
366
367         if (vb2_is_busy(vq)) {
368                 v4l2_err(fimc->m2m.vfd, "queue (%d) busy\n", f->type);
369                 return -EBUSY;
370         }
371
372         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
373                 frame = &ctx->s_frame;
374         else
375                 frame = &ctx->d_frame;
376
377         pix = &f->fmt.pix_mp;
378         frame->fmt = fimc_find_format(&pix->pixelformat, NULL,
379                                       get_m2m_fmt_flags(f->type), 0);
380         if (!frame->fmt)
381                 return -EINVAL;
382
383         /* Update RGB Alpha control state and value range */
384         fimc_alpha_ctrl_update(ctx);
385
386         for (i = 0; i < frame->fmt->colplanes; i++) {
387                 frame->payload[i] =
388                         (pix->width * pix->height * frame->fmt->depth[i]) / 8;
389         }
390
391         fimc_fill_frame(frame, f);
392
393         ctx->scaler.enabled = 1;
394
395         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
396                 fimc_ctx_state_set(FIMC_PARAMS | FIMC_DST_FMT, ctx);
397         else
398                 fimc_ctx_state_set(FIMC_PARAMS | FIMC_SRC_FMT, ctx);
399
400         dbg("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
401
402         return 0;
403 }
404
405 static int fimc_m2m_reqbufs(struct file *file, void *fh,
406                             struct v4l2_requestbuffers *reqbufs)
407 {
408         struct fimc_ctx *ctx = fh_to_ctx(fh);
409
410         return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
411 }
412
413 static int fimc_m2m_querybuf(struct file *file, void *fh,
414                              struct v4l2_buffer *buf)
415 {
416         struct fimc_ctx *ctx = fh_to_ctx(fh);
417
418         return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
419 }
420
421 static int fimc_m2m_qbuf(struct file *file, void *fh,
422                          struct v4l2_buffer *buf)
423 {
424         struct fimc_ctx *ctx = fh_to_ctx(fh);
425
426         return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
427 }
428
429 static int fimc_m2m_dqbuf(struct file *file, void *fh,
430                           struct v4l2_buffer *buf)
431 {
432         struct fimc_ctx *ctx = fh_to_ctx(fh);
433
434         return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
435 }
436
437 static int fimc_m2m_streamon(struct file *file, void *fh,
438                              enum v4l2_buf_type type)
439 {
440         struct fimc_ctx *ctx = fh_to_ctx(fh);
441
442         /* The source and target color format need to be set */
443         if (V4L2_TYPE_IS_OUTPUT(type)) {
444                 if (!fimc_ctx_state_is_set(FIMC_SRC_FMT, ctx))
445                         return -EINVAL;
446         } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT, ctx)) {
447                 return -EINVAL;
448         }
449
450         return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
451 }
452
453 static int fimc_m2m_streamoff(struct file *file, void *fh,
454                             enum v4l2_buf_type type)
455 {
456         struct fimc_ctx *ctx = fh_to_ctx(fh);
457
458         return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
459 }
460
461 static int fimc_m2m_cropcap(struct file *file, void *fh,
462                             struct v4l2_cropcap *cr)
463 {
464         struct fimc_ctx *ctx = fh_to_ctx(fh);
465         struct fimc_frame *frame;
466
467         frame = ctx_get_frame(ctx, cr->type);
468         if (IS_ERR(frame))
469                 return PTR_ERR(frame);
470
471         cr->bounds.left = 0;
472         cr->bounds.top = 0;
473         cr->bounds.width = frame->o_width;
474         cr->bounds.height = frame->o_height;
475         cr->defrect = cr->bounds;
476
477         return 0;
478 }
479
480 static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
481 {
482         struct fimc_ctx *ctx = fh_to_ctx(fh);
483         struct fimc_frame *frame;
484
485         frame = ctx_get_frame(ctx, cr->type);
486         if (IS_ERR(frame))
487                 return PTR_ERR(frame);
488
489         cr->c.left = frame->offs_h;
490         cr->c.top = frame->offs_v;
491         cr->c.width = frame->width;
492         cr->c.height = frame->height;
493
494         return 0;
495 }
496
497 static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
498 {
499         struct fimc_dev *fimc = ctx->fimc_dev;
500         struct fimc_frame *f;
501         u32 min_size, halign, depth = 0;
502         int i;
503
504         if (cr->c.top < 0 || cr->c.left < 0) {
505                 v4l2_err(fimc->m2m.vfd,
506                         "doesn't support negative values for top & left\n");
507                 return -EINVAL;
508         }
509         if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
510                 f = &ctx->d_frame;
511         else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
512                 f = &ctx->s_frame;
513         else
514                 return -EINVAL;
515
516         min_size = (f == &ctx->s_frame) ?
517                 fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
518
519         /* Get pixel alignment constraints. */
520         if (fimc->variant->min_vsize_align == 1)
521                 halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
522         else
523                 halign = ffs(fimc->variant->min_vsize_align) - 1;
524
525         for (i = 0; i < f->fmt->colplanes; i++)
526                 depth += f->fmt->depth[i];
527
528         v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
529                               ffs(min_size) - 1,
530                               &cr->c.height, min_size, f->o_height,
531                               halign, 64/(ALIGN(depth, 8)));
532
533         /* adjust left/top if cropping rectangle is out of bounds */
534         if (cr->c.left + cr->c.width > f->o_width)
535                 cr->c.left = f->o_width - cr->c.width;
536         if (cr->c.top + cr->c.height > f->o_height)
537                 cr->c.top = f->o_height - cr->c.height;
538
539         cr->c.left = round_down(cr->c.left, min_size);
540         cr->c.top  = round_down(cr->c.top, fimc->variant->hor_offs_align);
541
542         dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
543             cr->c.left, cr->c.top, cr->c.width, cr->c.height,
544             f->f_width, f->f_height);
545
546         return 0;
547 }
548
549 static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
550 {
551         struct fimc_ctx *ctx = fh_to_ctx(fh);
552         struct fimc_dev *fimc = ctx->fimc_dev;
553         struct fimc_frame *f;
554         int ret;
555
556         ret = fimc_m2m_try_crop(ctx, cr);
557         if (ret)
558                 return ret;
559
560         f = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
561                 &ctx->s_frame : &ctx->d_frame;
562
563         /* Check to see if scaling ratio is within supported range */
564         if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
565                 if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
566                         ret = fimc_check_scaler_ratio(ctx, cr->c.width,
567                                         cr->c.height, ctx->d_frame.width,
568                                         ctx->d_frame.height, ctx->rotation);
569                 } else {
570                         ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
571                                         ctx->s_frame.height, cr->c.width,
572                                         cr->c.height, ctx->rotation);
573                 }
574                 if (ret) {
575                         v4l2_err(fimc->m2m.vfd, "Out of scaler range\n");
576                         return -EINVAL;
577                 }
578         }
579
580         f->offs_h = cr->c.left;
581         f->offs_v = cr->c.top;
582         f->width  = cr->c.width;
583         f->height = cr->c.height;
584
585         fimc_ctx_state_set(FIMC_PARAMS, ctx);
586
587         return 0;
588 }
589
590 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
591         .vidioc_querycap                = fimc_m2m_querycap,
592         .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
593         .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
594         .vidioc_g_fmt_vid_cap_mplane    = fimc_m2m_g_fmt_mplane,
595         .vidioc_g_fmt_vid_out_mplane    = fimc_m2m_g_fmt_mplane,
596         .vidioc_try_fmt_vid_cap_mplane  = fimc_m2m_try_fmt_mplane,
597         .vidioc_try_fmt_vid_out_mplane  = fimc_m2m_try_fmt_mplane,
598         .vidioc_s_fmt_vid_cap_mplane    = fimc_m2m_s_fmt_mplane,
599         .vidioc_s_fmt_vid_out_mplane    = fimc_m2m_s_fmt_mplane,
600         .vidioc_reqbufs                 = fimc_m2m_reqbufs,
601         .vidioc_querybuf                = fimc_m2m_querybuf,
602         .vidioc_qbuf                    = fimc_m2m_qbuf,
603         .vidioc_dqbuf                   = fimc_m2m_dqbuf,
604         .vidioc_streamon                = fimc_m2m_streamon,
605         .vidioc_streamoff               = fimc_m2m_streamoff,
606         .vidioc_g_crop                  = fimc_m2m_g_crop,
607         .vidioc_s_crop                  = fimc_m2m_s_crop,
608         .vidioc_cropcap                 = fimc_m2m_cropcap
609
610 };
611
612 static int queue_init(void *priv, struct vb2_queue *src_vq,
613                       struct vb2_queue *dst_vq)
614 {
615         struct fimc_ctx *ctx = priv;
616         int ret;
617
618         memset(src_vq, 0, sizeof(*src_vq));
619         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
620         src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
621         src_vq->drv_priv = ctx;
622         src_vq->ops = &fimc_qops;
623         src_vq->mem_ops = &vb2_dma_contig_memops;
624         src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
625
626         ret = vb2_queue_init(src_vq);
627         if (ret)
628                 return ret;
629
630         memset(dst_vq, 0, sizeof(*dst_vq));
631         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
632         dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
633         dst_vq->drv_priv = ctx;
634         dst_vq->ops = &fimc_qops;
635         dst_vq->mem_ops = &vb2_dma_contig_memops;
636         dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
637
638         return vb2_queue_init(dst_vq);
639 }
640
641 static int fimc_m2m_open(struct file *file)
642 {
643         struct fimc_dev *fimc = video_drvdata(file);
644         struct fimc_ctx *ctx;
645         int ret;
646
647         dbg("pid: %d, state: 0x%lx, refcnt: %d",
648             task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
649
650         /*
651          * Return if the corresponding video capture node
652          * is already opened.
653          */
654         if (fimc->vid_cap.refcnt > 0)
655                 return -EBUSY;
656
657         ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
658         if (!ctx)
659                 return -ENOMEM;
660         v4l2_fh_init(&ctx->fh, fimc->m2m.vfd);
661         ctx->fimc_dev = fimc;
662
663         /* Default color format */
664         ctx->s_frame.fmt = fimc_get_format(0);
665         ctx->d_frame.fmt = fimc_get_format(0);
666
667         ret = fimc_ctrls_create(ctx);
668         if (ret)
669                 goto error_fh;
670
671         /* Use separate control handler per file handle */
672         ctx->fh.ctrl_handler = &ctx->ctrls.handler;
673         file->private_data = &ctx->fh;
674         v4l2_fh_add(&ctx->fh);
675
676         /* Setup the device context for memory-to-memory mode */
677         ctx->state = FIMC_CTX_M2M;
678         ctx->flags = 0;
679         ctx->in_path = FIMC_IO_DMA;
680         ctx->out_path = FIMC_IO_DMA;
681
682         ctx->m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
683         if (IS_ERR(ctx->m2m_ctx)) {
684                 ret = PTR_ERR(ctx->m2m_ctx);
685                 goto error_c;
686         }
687
688         if (fimc->m2m.refcnt++ == 0)
689                 set_bit(ST_M2M_RUN, &fimc->state);
690         return 0;
691
692 error_c:
693         fimc_ctrls_delete(ctx);
694 error_fh:
695         v4l2_fh_del(&ctx->fh);
696         v4l2_fh_exit(&ctx->fh);
697         kfree(ctx);
698         return ret;
699 }
700
701 static int fimc_m2m_release(struct file *file)
702 {
703         struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
704         struct fimc_dev *fimc = ctx->fimc_dev;
705
706         dbg("pid: %d, state: 0x%lx, refcnt= %d",
707                 task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
708
709         v4l2_m2m_ctx_release(ctx->m2m_ctx);
710         fimc_ctrls_delete(ctx);
711         v4l2_fh_del(&ctx->fh);
712         v4l2_fh_exit(&ctx->fh);
713
714         if (--fimc->m2m.refcnt <= 0)
715                 clear_bit(ST_M2M_RUN, &fimc->state);
716         kfree(ctx);
717         return 0;
718 }
719
720 static unsigned int fimc_m2m_poll(struct file *file,
721                                   struct poll_table_struct *wait)
722 {
723         struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
724
725         return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
726 }
727
728
729 static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
730 {
731         struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
732
733         return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
734 }
735
736 static const struct v4l2_file_operations fimc_m2m_fops = {
737         .owner          = THIS_MODULE,
738         .open           = fimc_m2m_open,
739         .release        = fimc_m2m_release,
740         .poll           = fimc_m2m_poll,
741         .unlocked_ioctl = video_ioctl2,
742         .mmap           = fimc_m2m_mmap,
743 };
744
745 static struct v4l2_m2m_ops m2m_ops = {
746         .device_run     = fimc_device_run,
747         .job_abort      = fimc_job_abort,
748 };
749
750 int fimc_register_m2m_device(struct fimc_dev *fimc,
751                              struct v4l2_device *v4l2_dev)
752 {
753         struct video_device *vfd;
754         struct platform_device *pdev;
755         int ret = 0;
756
757         if (!fimc)
758                 return -ENODEV;
759
760         pdev = fimc->pdev;
761         fimc->v4l2_dev = v4l2_dev;
762
763         vfd = video_device_alloc();
764         if (!vfd) {
765                 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
766                 return -ENOMEM;
767         }
768
769         vfd->fops = &fimc_m2m_fops;
770         vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
771         vfd->v4l2_dev = v4l2_dev;
772         vfd->minor = -1;
773         vfd->release = video_device_release;
774         vfd->lock = &fimc->lock;
775         /* Locking in file operations other than ioctl should be done
776            by the driver, not the V4L2 core.
777            This driver needs auditing so that this flag can be removed. */
778         set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
779
780         snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.m2m", fimc->id);
781         video_set_drvdata(vfd, fimc);
782
783         fimc->m2m.vfd = vfd;
784         fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
785         if (IS_ERR(fimc->m2m.m2m_dev)) {
786                 v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
787                 ret = PTR_ERR(fimc->m2m.m2m_dev);
788                 goto err_init;
789         }
790
791         ret = media_entity_init(&vfd->entity, 0, NULL, 0);
792         if (ret)
793                 goto err_me;
794
795         ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
796         if (ret)
797                 goto err_vd;
798
799         v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
800                   vfd->name, video_device_node_name(vfd));
801         return 0;
802
803 err_vd:
804         media_entity_cleanup(&vfd->entity);
805 err_me:
806         v4l2_m2m_release(fimc->m2m.m2m_dev);
807 err_init:
808         video_device_release(fimc->m2m.vfd);
809         return ret;
810 }
811
812 void fimc_unregister_m2m_device(struct fimc_dev *fimc)
813 {
814         if (!fimc)
815                 return;
816
817         if (fimc->m2m.m2m_dev)
818                 v4l2_m2m_release(fimc->m2m.m2m_dev);
819         if (fimc->m2m.vfd) {
820                 media_entity_cleanup(&fimc->m2m.vfd->entity);
821                 /* Can also be called if video device wasn't registered */
822                 video_unregister_device(fimc->m2m.vfd);
823         }
824 }