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