[media] exynos4-is: Use mem-to-mem ioctl helpers
[cascardo/linux.git] / drivers / media / platform / exynos4-is / fimc-m2m.c
1 /*
2  * Samsung S5P/EXYNOS4 SoC series FIMC (video postprocessor) driver
3  *
4  * Copyright (C) 2012 - 2013 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 "common.h"
31 #include "fimc-core.h"
32 #include "fimc-reg.h"
33 #include "media-dev.h"
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->fh.m2m_ctx)
48                 return;
49
50         src_vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
51         dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.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->fh.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 *src_vb, *dst_vb;
104         struct fimc_ctx *ctx = priv;
105         struct fimc_frame *sf, *df;
106         struct fimc_dev *fimc;
107         unsigned long flags;
108         int 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         src_vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
127         ret = fimc_prepare_addr(ctx, src_vb, sf, &sf->paddr);
128         if (ret)
129                 goto dma_unlock;
130
131         dst_vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
132         ret = fimc_prepare_addr(ctx, dst_vb, df, &df->paddr);
133         if (ret)
134                 goto dma_unlock;
135
136         dst_vb->v4l2_buf.timestamp = src_vb->v4l2_buf.timestamp;
137
138         /* Reconfigure hardware if the context has changed. */
139         if (fimc->m2m.ctx != ctx) {
140                 ctx->state |= FIMC_PARAMS;
141                 fimc->m2m.ctx = ctx;
142         }
143
144         if (ctx->state & FIMC_PARAMS) {
145                 fimc_set_yuv_order(ctx);
146                 fimc_hw_set_input_path(ctx);
147                 fimc_hw_set_in_dma(ctx);
148                 ret = fimc_set_scaler_info(ctx);
149                 if (ret)
150                         goto dma_unlock;
151                 fimc_hw_set_prescaler(ctx);
152                 fimc_hw_set_mainscaler(ctx);
153                 fimc_hw_set_target_format(ctx);
154                 fimc_hw_set_rotation(ctx);
155                 fimc_hw_set_effect(ctx);
156                 fimc_hw_set_out_dma(ctx);
157                 if (fimc->drv_data->alpha_color)
158                         fimc_hw_set_rgb_alpha(ctx);
159                 fimc_hw_set_output_path(ctx);
160         }
161         fimc_hw_set_input_addr(fimc, &sf->paddr);
162         fimc_hw_set_output_addr(fimc, &df->paddr, -1);
163
164         fimc_activate_capture(ctx);
165         ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP);
166         fimc_hw_activate_input_dma(fimc, true);
167
168 dma_unlock:
169         spin_unlock_irqrestore(&fimc->slock, flags);
170 }
171
172 static void fimc_job_abort(void *priv)
173 {
174         fimc_m2m_shutdown(priv);
175 }
176
177 static int fimc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
178                             unsigned int *num_buffers, unsigned int *num_planes,
179                             unsigned int sizes[], void *allocators[])
180 {
181         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
182         struct fimc_frame *f;
183         int i;
184
185         f = ctx_get_frame(ctx, vq->type);
186         if (IS_ERR(f))
187                 return PTR_ERR(f);
188         /*
189          * Return number of non-contigous planes (plane buffers)
190          * depending on the configured color format.
191          */
192         if (!f->fmt)
193                 return -EINVAL;
194
195         *num_planes = f->fmt->memplanes;
196         for (i = 0; i < f->fmt->memplanes; i++) {
197                 sizes[i] = (f->f_width * f->f_height * f->fmt->depth[i]) / 8;
198                 allocators[i] = ctx->fimc_dev->alloc_ctx;
199         }
200         return 0;
201 }
202
203 static int fimc_buf_prepare(struct vb2_buffer *vb)
204 {
205         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
206         struct fimc_frame *frame;
207         int i;
208
209         frame = ctx_get_frame(ctx, vb->vb2_queue->type);
210         if (IS_ERR(frame))
211                 return PTR_ERR(frame);
212
213         for (i = 0; i < frame->fmt->memplanes; i++)
214                 vb2_set_plane_payload(vb, i, frame->payload[i]);
215
216         return 0;
217 }
218
219 static void fimc_buf_queue(struct vb2_buffer *vb)
220 {
221         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
222         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
223 }
224
225 static struct vb2_ops fimc_qops = {
226         .queue_setup     = fimc_queue_setup,
227         .buf_prepare     = fimc_buf_prepare,
228         .buf_queue       = fimc_buf_queue,
229         .wait_prepare    = vb2_ops_wait_prepare,
230         .wait_finish     = vb2_ops_wait_finish,
231         .stop_streaming  = stop_streaming,
232         .start_streaming = start_streaming,
233 };
234
235 /*
236  * V4L2 ioctl handlers
237  */
238 static int fimc_m2m_querycap(struct file *file, void *fh,
239                                      struct v4l2_capability *cap)
240 {
241         struct fimc_dev *fimc = video_drvdata(file);
242         unsigned int caps;
243
244         /*
245          * This is only a mem-to-mem video device. The capture and output
246          * device capability flags are left only for backward compatibility
247          * and are scheduled for removal.
248          */
249         caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE |
250                 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
251
252         __fimc_vidioc_querycap(&fimc->pdev->dev, cap, caps);
253         return 0;
254 }
255
256 static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
257                                     struct v4l2_fmtdesc *f)
258 {
259         struct fimc_fmt *fmt;
260
261         fmt = fimc_find_format(NULL, NULL, get_m2m_fmt_flags(f->type),
262                                f->index);
263         if (!fmt)
264                 return -EINVAL;
265
266         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
267         f->pixelformat = fmt->fourcc;
268         return 0;
269 }
270
271 static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
272                                  struct v4l2_format *f)
273 {
274         struct fimc_ctx *ctx = fh_to_ctx(fh);
275         struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
276
277         if (IS_ERR(frame))
278                 return PTR_ERR(frame);
279
280         __fimc_get_format(frame, f);
281         return 0;
282 }
283
284 static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
285 {
286         struct fimc_dev *fimc = ctx->fimc_dev;
287         const struct fimc_variant *variant = fimc->variant;
288         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
289         struct fimc_fmt *fmt;
290         u32 max_w, mod_x, mod_y;
291
292         if (!IS_M2M(f->type))
293                 return -EINVAL;
294
295         fmt = fimc_find_format(&pix->pixelformat, NULL,
296                                get_m2m_fmt_flags(f->type), 0);
297         if (WARN(fmt == NULL, "Pixel format lookup failed"))
298                 return -EINVAL;
299
300         if (pix->field == V4L2_FIELD_ANY)
301                 pix->field = V4L2_FIELD_NONE;
302         else if (pix->field != V4L2_FIELD_NONE)
303                 return -EINVAL;
304
305         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
306                 max_w = variant->pix_limit->scaler_dis_w;
307                 mod_x = ffs(variant->min_inp_pixsize) - 1;
308         } else {
309                 max_w = variant->pix_limit->out_rot_dis_w;
310                 mod_x = ffs(variant->min_out_pixsize) - 1;
311         }
312
313         if (tiled_fmt(fmt)) {
314                 mod_x = 6; /* 64 x 32 pixels tile */
315                 mod_y = 5;
316         } else {
317                 if (variant->min_vsize_align == 1)
318                         mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
319                 else
320                         mod_y = ffs(variant->min_vsize_align) - 1;
321         }
322
323         v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
324                 &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
325
326         fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
327         return 0;
328 }
329
330 static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
331                                    struct v4l2_format *f)
332 {
333         struct fimc_ctx *ctx = fh_to_ctx(fh);
334         return fimc_try_fmt_mplane(ctx, f);
335 }
336
337 static void __set_frame_format(struct fimc_frame *frame, struct fimc_fmt *fmt,
338                                struct v4l2_pix_format_mplane *pixm)
339 {
340         int i;
341
342         for (i = 0; i < fmt->colplanes; i++) {
343                 frame->bytesperline[i] = pixm->plane_fmt[i].bytesperline;
344                 frame->payload[i] = pixm->plane_fmt[i].sizeimage;
345         }
346
347         frame->f_width = pixm->width;
348         frame->f_height = pixm->height;
349         frame->o_width = pixm->width;
350         frame->o_height = pixm->height;
351         frame->width = pixm->width;
352         frame->height = pixm->height;
353         frame->offs_h = 0;
354         frame->offs_v = 0;
355         frame->fmt = fmt;
356 }
357
358 static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
359                                  struct v4l2_format *f)
360 {
361         struct fimc_ctx *ctx = fh_to_ctx(fh);
362         struct fimc_dev *fimc = ctx->fimc_dev;
363         struct fimc_fmt *fmt;
364         struct vb2_queue *vq;
365         struct fimc_frame *frame;
366         int ret;
367
368         ret = fimc_try_fmt_mplane(ctx, f);
369         if (ret)
370                 return ret;
371
372         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
373
374         if (vb2_is_busy(vq)) {
375                 v4l2_err(&fimc->m2m.vfd, "queue (%d) busy\n", f->type);
376                 return -EBUSY;
377         }
378
379         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
380                 frame = &ctx->s_frame;
381         else
382                 frame = &ctx->d_frame;
383
384         fmt = fimc_find_format(&f->fmt.pix_mp.pixelformat, NULL,
385                                get_m2m_fmt_flags(f->type), 0);
386         if (!fmt)
387                 return -EINVAL;
388
389         __set_frame_format(frame, fmt, &f->fmt.pix_mp);
390
391         /* Update RGB Alpha control state and value range */
392         fimc_alpha_ctrl_update(ctx);
393
394         return 0;
395 }
396
397 static int fimc_m2m_cropcap(struct file *file, void *fh,
398                             struct v4l2_cropcap *cr)
399 {
400         struct fimc_ctx *ctx = fh_to_ctx(fh);
401         struct fimc_frame *frame;
402
403         frame = ctx_get_frame(ctx, cr->type);
404         if (IS_ERR(frame))
405                 return PTR_ERR(frame);
406
407         cr->bounds.left = 0;
408         cr->bounds.top = 0;
409         cr->bounds.width = frame->o_width;
410         cr->bounds.height = frame->o_height;
411         cr->defrect = cr->bounds;
412
413         return 0;
414 }
415
416 static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
417 {
418         struct fimc_ctx *ctx = fh_to_ctx(fh);
419         struct fimc_frame *frame;
420
421         frame = ctx_get_frame(ctx, cr->type);
422         if (IS_ERR(frame))
423                 return PTR_ERR(frame);
424
425         cr->c.left = frame->offs_h;
426         cr->c.top = frame->offs_v;
427         cr->c.width = frame->width;
428         cr->c.height = frame->height;
429
430         return 0;
431 }
432
433 static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
434 {
435         struct fimc_dev *fimc = ctx->fimc_dev;
436         struct fimc_frame *f;
437         u32 min_size, halign, depth = 0;
438         int i;
439
440         if (cr->c.top < 0 || cr->c.left < 0) {
441                 v4l2_err(&fimc->m2m.vfd,
442                         "doesn't support negative values for top & left\n");
443                 return -EINVAL;
444         }
445         if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
446                 f = &ctx->d_frame;
447         else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
448                 f = &ctx->s_frame;
449         else
450                 return -EINVAL;
451
452         min_size = (f == &ctx->s_frame) ?
453                 fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
454
455         /* Get pixel alignment constraints. */
456         if (fimc->variant->min_vsize_align == 1)
457                 halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
458         else
459                 halign = ffs(fimc->variant->min_vsize_align) - 1;
460
461         for (i = 0; i < f->fmt->colplanes; i++)
462                 depth += f->fmt->depth[i];
463
464         v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
465                               ffs(min_size) - 1,
466                               &cr->c.height, min_size, f->o_height,
467                               halign, 64/(ALIGN(depth, 8)));
468
469         /* adjust left/top if cropping rectangle is out of bounds */
470         if (cr->c.left + cr->c.width > f->o_width)
471                 cr->c.left = f->o_width - cr->c.width;
472         if (cr->c.top + cr->c.height > f->o_height)
473                 cr->c.top = f->o_height - cr->c.height;
474
475         cr->c.left = round_down(cr->c.left, min_size);
476         cr->c.top  = round_down(cr->c.top, fimc->variant->hor_offs_align);
477
478         dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
479             cr->c.left, cr->c.top, cr->c.width, cr->c.height,
480             f->f_width, f->f_height);
481
482         return 0;
483 }
484
485 static int fimc_m2m_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
486 {
487         struct fimc_ctx *ctx = fh_to_ctx(fh);
488         struct fimc_dev *fimc = ctx->fimc_dev;
489         struct v4l2_crop cr = *crop;
490         struct fimc_frame *f;
491         int ret;
492
493         ret = fimc_m2m_try_crop(ctx, &cr);
494         if (ret)
495                 return ret;
496
497         f = (cr.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
498                 &ctx->s_frame : &ctx->d_frame;
499
500         /* Check to see if scaling ratio is within supported range */
501         if (cr.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
502                 ret = fimc_check_scaler_ratio(ctx, cr.c.width,
503                                 cr.c.height, ctx->d_frame.width,
504                                 ctx->d_frame.height, ctx->rotation);
505         } else {
506                 ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
507                                 ctx->s_frame.height, cr.c.width,
508                                 cr.c.height, ctx->rotation);
509         }
510         if (ret) {
511                 v4l2_err(&fimc->m2m.vfd, "Out of scaler range\n");
512                 return -EINVAL;
513         }
514
515         f->offs_h = cr.c.left;
516         f->offs_v = cr.c.top;
517         f->width  = cr.c.width;
518         f->height = cr.c.height;
519
520         fimc_ctx_state_set(FIMC_PARAMS, ctx);
521
522         return 0;
523 }
524
525 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
526         .vidioc_querycap                = fimc_m2m_querycap,
527         .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
528         .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
529         .vidioc_g_fmt_vid_cap_mplane    = fimc_m2m_g_fmt_mplane,
530         .vidioc_g_fmt_vid_out_mplane    = fimc_m2m_g_fmt_mplane,
531         .vidioc_try_fmt_vid_cap_mplane  = fimc_m2m_try_fmt_mplane,
532         .vidioc_try_fmt_vid_out_mplane  = fimc_m2m_try_fmt_mplane,
533         .vidioc_s_fmt_vid_cap_mplane    = fimc_m2m_s_fmt_mplane,
534         .vidioc_s_fmt_vid_out_mplane    = fimc_m2m_s_fmt_mplane,
535         .vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
536         .vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
537         .vidioc_qbuf                    = v4l2_m2m_ioctl_qbuf,
538         .vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
539         .vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
540         .vidioc_streamon                = v4l2_m2m_ioctl_streamon,
541         .vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
542         .vidioc_g_crop                  = fimc_m2m_g_crop,
543         .vidioc_s_crop                  = fimc_m2m_s_crop,
544         .vidioc_cropcap                 = fimc_m2m_cropcap
545
546 };
547
548 static int queue_init(void *priv, struct vb2_queue *src_vq,
549                       struct vb2_queue *dst_vq)
550 {
551         struct fimc_ctx *ctx = priv;
552         int ret;
553
554         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
555         src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
556         src_vq->drv_priv = ctx;
557         src_vq->ops = &fimc_qops;
558         src_vq->mem_ops = &vb2_dma_contig_memops;
559         src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
560         src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
561         src_vq->lock = &ctx->fimc_dev->lock;
562
563         ret = vb2_queue_init(src_vq);
564         if (ret)
565                 return ret;
566
567         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
568         dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
569         dst_vq->drv_priv = ctx;
570         dst_vq->ops = &fimc_qops;
571         dst_vq->mem_ops = &vb2_dma_contig_memops;
572         dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
573         dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
574         dst_vq->lock = &ctx->fimc_dev->lock;
575
576         return vb2_queue_init(dst_vq);
577 }
578
579 static int fimc_m2m_set_default_format(struct fimc_ctx *ctx)
580 {
581         struct v4l2_pix_format_mplane pixm = {
582                 .pixelformat    = V4L2_PIX_FMT_RGB32,
583                 .width          = 800,
584                 .height         = 600,
585                 .plane_fmt[0]   = {
586                         .bytesperline = 800 * 4,
587                         .sizeimage = 800 * 4 * 600,
588                 },
589         };
590         struct fimc_fmt *fmt;
591
592         fmt = fimc_find_format(&pixm.pixelformat, NULL, FMT_FLAGS_M2M, 0);
593         if (!fmt)
594                 return -EINVAL;
595
596         __set_frame_format(&ctx->s_frame, fmt, &pixm);
597         __set_frame_format(&ctx->d_frame, fmt, &pixm);
598
599         return 0;
600 }
601
602 static int fimc_m2m_open(struct file *file)
603 {
604         struct fimc_dev *fimc = video_drvdata(file);
605         struct fimc_ctx *ctx;
606         int ret = -EBUSY;
607
608         pr_debug("pid: %d, state: %#lx\n", task_pid_nr(current), fimc->state);
609
610         if (mutex_lock_interruptible(&fimc->lock))
611                 return -ERESTARTSYS;
612         /*
613          * Don't allow simultaneous open() of the mem-to-mem and the
614          * capture video node that belong to same FIMC IP instance.
615          */
616         if (test_bit(ST_CAPT_BUSY, &fimc->state))
617                 goto unlock;
618
619         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
620         if (!ctx) {
621                 ret = -ENOMEM;
622                 goto unlock;
623         }
624         v4l2_fh_init(&ctx->fh, &fimc->m2m.vfd);
625         ctx->fimc_dev = fimc;
626
627         /* Default color format */
628         ctx->s_frame.fmt = fimc_get_format(0);
629         ctx->d_frame.fmt = fimc_get_format(0);
630
631         ret = fimc_ctrls_create(ctx);
632         if (ret)
633                 goto error_fh;
634
635         /* Use separate control handler per file handle */
636         ctx->fh.ctrl_handler = &ctx->ctrls.handler;
637         file->private_data = &ctx->fh;
638         v4l2_fh_add(&ctx->fh);
639
640         /* Setup the device context for memory-to-memory mode */
641         ctx->state = FIMC_CTX_M2M;
642         ctx->flags = 0;
643         ctx->in_path = FIMC_IO_DMA;
644         ctx->out_path = FIMC_IO_DMA;
645         ctx->scaler.enabled = 1;
646
647         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
648         if (IS_ERR(ctx->fh.m2m_ctx)) {
649                 ret = PTR_ERR(ctx->fh.m2m_ctx);
650                 goto error_c;
651         }
652
653         if (fimc->m2m.refcnt++ == 0)
654                 set_bit(ST_M2M_RUN, &fimc->state);
655
656         ret = fimc_m2m_set_default_format(ctx);
657         if (ret < 0)
658                 goto error_m2m_ctx;
659
660         mutex_unlock(&fimc->lock);
661         return 0;
662
663 error_m2m_ctx:
664         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
665 error_c:
666         fimc_ctrls_delete(ctx);
667 error_fh:
668         v4l2_fh_del(&ctx->fh);
669         v4l2_fh_exit(&ctx->fh);
670         kfree(ctx);
671 unlock:
672         mutex_unlock(&fimc->lock);
673         return ret;
674 }
675
676 static int fimc_m2m_release(struct file *file)
677 {
678         struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
679         struct fimc_dev *fimc = ctx->fimc_dev;
680
681         dbg("pid: %d, state: 0x%lx, refcnt= %d",
682                 task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
683
684         mutex_lock(&fimc->lock);
685
686         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
687         fimc_ctrls_delete(ctx);
688         v4l2_fh_del(&ctx->fh);
689         v4l2_fh_exit(&ctx->fh);
690
691         if (--fimc->m2m.refcnt <= 0)
692                 clear_bit(ST_M2M_RUN, &fimc->state);
693         kfree(ctx);
694
695         mutex_unlock(&fimc->lock);
696         return 0;
697 }
698
699 static const struct v4l2_file_operations fimc_m2m_fops = {
700         .owner          = THIS_MODULE,
701         .open           = fimc_m2m_open,
702         .release        = fimc_m2m_release,
703         .poll           = v4l2_m2m_fop_poll,
704         .unlocked_ioctl = video_ioctl2,
705         .mmap           = v4l2_m2m_fop_mmap,
706 };
707
708 static struct v4l2_m2m_ops m2m_ops = {
709         .device_run     = fimc_device_run,
710         .job_abort      = fimc_job_abort,
711 };
712
713 int fimc_register_m2m_device(struct fimc_dev *fimc,
714                              struct v4l2_device *v4l2_dev)
715 {
716         struct video_device *vfd = &fimc->m2m.vfd;
717         int ret;
718
719         fimc->v4l2_dev = v4l2_dev;
720
721         memset(vfd, 0, sizeof(*vfd));
722         vfd->fops = &fimc_m2m_fops;
723         vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
724         vfd->v4l2_dev = v4l2_dev;
725         vfd->minor = -1;
726         vfd->release = video_device_release_empty;
727         vfd->lock = &fimc->lock;
728         vfd->vfl_dir = VFL_DIR_M2M;
729
730         snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.m2m", fimc->id);
731         video_set_drvdata(vfd, fimc);
732
733         fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
734         if (IS_ERR(fimc->m2m.m2m_dev)) {
735                 v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
736                 return PTR_ERR(fimc->m2m.m2m_dev);
737         }
738
739         ret = media_entity_init(&vfd->entity, 0, NULL, 0);
740         if (ret)
741                 goto err_me;
742
743         ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
744         if (ret)
745                 goto err_vd;
746
747         v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
748                   vfd->name, video_device_node_name(vfd));
749         return 0;
750
751 err_vd:
752         media_entity_cleanup(&vfd->entity);
753 err_me:
754         v4l2_m2m_release(fimc->m2m.m2m_dev);
755         return ret;
756 }
757
758 void fimc_unregister_m2m_device(struct fimc_dev *fimc)
759 {
760         if (!fimc)
761                 return;
762
763         if (fimc->m2m.m2m_dev)
764                 v4l2_m2m_release(fimc->m2m.m2m_dev);
765
766         if (video_is_registered(&fimc->m2m.vfd)) {
767                 video_unregister_device(&fimc->m2m.vfd);
768                 media_entity_cleanup(&fimc->m2m.vfd.entity);
769         }
770 }