]> Pileus Git - ~andy/linux/blob - drivers/media/video/s5p-fimc/fimc-capture.c
[media] s5p-fimc: Convert to use media pipeline operations
[~andy/linux] / drivers / media / video / s5p-fimc / fimc-capture.c
1 /*
2  * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
3  *
4  * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
5  * Author: 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 version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
22
23 #include <linux/videodev2.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-mem2mem.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
29
30 #include "fimc-mdevice.h"
31 #include "fimc-core.h"
32
33 static int fimc_init_capture(struct fimc_dev *fimc)
34 {
35         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
36         struct fimc_sensor_info *sensor;
37         unsigned long flags;
38         int ret = 0;
39
40         if (fimc->pipeline.sensor == NULL || ctx == NULL)
41                 return -ENXIO;
42         if (ctx->s_frame.fmt == NULL)
43                 return -EINVAL;
44
45         sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor);
46
47         spin_lock_irqsave(&fimc->slock, flags);
48         fimc_prepare_dma_offset(ctx, &ctx->d_frame);
49         fimc_set_yuv_order(ctx);
50
51         fimc_hw_set_camera_polarity(fimc, sensor->pdata);
52         fimc_hw_set_camera_type(fimc, sensor->pdata);
53         fimc_hw_set_camera_source(fimc, sensor->pdata);
54         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
55
56         ret = fimc_set_scaler_info(ctx);
57         if (!ret) {
58                 fimc_hw_set_input_path(ctx);
59                 fimc_hw_set_prescaler(ctx);
60                 fimc_hw_set_mainscaler(ctx);
61                 fimc_hw_set_target_format(ctx);
62                 fimc_hw_set_rotation(ctx);
63                 fimc_hw_set_effect(ctx);
64                 fimc_hw_set_output_path(ctx);
65                 fimc_hw_set_out_dma(ctx);
66         }
67         spin_unlock_irqrestore(&fimc->slock, flags);
68         return ret;
69 }
70
71 static int fimc_capture_state_cleanup(struct fimc_dev *fimc)
72 {
73         struct fimc_vid_cap *cap = &fimc->vid_cap;
74         struct fimc_vid_buffer *buf;
75         unsigned long flags;
76
77         spin_lock_irqsave(&fimc->slock, flags);
78         fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
79                          1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM |
80                          1 << ST_CAPT_ISP_STREAM);
81
82         fimc->vid_cap.active_buf_cnt = 0;
83
84         /* Release buffers that were enqueued in the driver by videobuf2. */
85         while (!list_empty(&cap->pending_buf_q)) {
86                 buf = pending_queue_pop(cap);
87                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
88         }
89
90         while (!list_empty(&cap->active_buf_q)) {
91                 buf = active_queue_pop(cap);
92                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
93         }
94
95         spin_unlock_irqrestore(&fimc->slock, flags);
96
97         if (test_bit(ST_CAPT_ISP_STREAM, &fimc->state))
98                 return fimc_pipeline_s_stream(fimc, 0);
99         else
100                 return 0;
101 }
102
103 static int fimc_stop_capture(struct fimc_dev *fimc)
104 {
105         struct fimc_vid_cap *cap = &fimc->vid_cap;
106         unsigned long flags;
107
108         if (!fimc_capture_active(fimc))
109                 return 0;
110
111         spin_lock_irqsave(&fimc->slock, flags);
112         set_bit(ST_CAPT_SHUT, &fimc->state);
113         fimc_deactivate_capture(fimc);
114         spin_unlock_irqrestore(&fimc->slock, flags);
115
116         wait_event_timeout(fimc->irq_queue,
117                            !test_bit(ST_CAPT_SHUT, &fimc->state),
118                            FIMC_SHUTDOWN_TIMEOUT);
119
120         return fimc_capture_state_cleanup(fimc);
121 }
122
123
124 static int start_streaming(struct vb2_queue *q, unsigned int count)
125 {
126         struct fimc_ctx *ctx = q->drv_priv;
127         struct fimc_dev *fimc = ctx->fimc_dev;
128         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
129         int min_bufs;
130         int ret;
131
132         fimc_hw_reset(fimc);
133         vid_cap->frame_count = 0;
134
135         ret = fimc_init_capture(fimc);
136         if (ret)
137                 goto error;
138
139         set_bit(ST_CAPT_PEND, &fimc->state);
140
141         min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
142
143         if (vid_cap->active_buf_cnt >= min_bufs &&
144             !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
145                 fimc_activate_capture(ctx);
146
147                 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
148                         fimc_pipeline_s_stream(fimc, 1);
149         }
150
151         return 0;
152 error:
153         fimc_capture_state_cleanup(fimc);
154         return ret;
155 }
156
157 static int stop_streaming(struct vb2_queue *q)
158 {
159         struct fimc_ctx *ctx = q->drv_priv;
160         struct fimc_dev *fimc = ctx->fimc_dev;
161
162         if (!fimc_capture_active(fimc))
163                 return -EINVAL;
164
165         return fimc_stop_capture(fimc);
166 }
167
168 int fimc_capture_suspend(struct fimc_dev *fimc)
169 {
170         return -EBUSY;
171 }
172
173 int fimc_capture_resume(struct fimc_dev *fimc)
174 {
175         return 0;
176 }
177
178 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
179 {
180         if (!fr || plane >= fr->fmt->memplanes)
181                 return 0;
182         return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
183 }
184
185 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
186                        unsigned int *num_planes, unsigned int sizes[],
187                        void *allocators[])
188 {
189         struct fimc_ctx *ctx = vq->drv_priv;
190         struct fimc_fmt *fmt = ctx->d_frame.fmt;
191         int i;
192
193         if (!fmt)
194                 return -EINVAL;
195
196         *num_planes = fmt->memplanes;
197
198         for (i = 0; i < fmt->memplanes; i++) {
199                 sizes[i] = get_plane_size(&ctx->d_frame, i);
200                 allocators[i] = ctx->fimc_dev->alloc_ctx;
201         }
202
203         return 0;
204 }
205
206 static int buffer_prepare(struct vb2_buffer *vb)
207 {
208         struct vb2_queue *vq = vb->vb2_queue;
209         struct fimc_ctx *ctx = vq->drv_priv;
210         int i;
211
212         if (ctx->d_frame.fmt == NULL)
213                 return -EINVAL;
214
215         for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
216                 unsigned long size = ctx->d_frame.payload[i];
217
218                 if (vb2_plane_size(vb, i) < size) {
219                         v4l2_err(ctx->fimc_dev->vid_cap.vfd,
220                                  "User buffer too small (%ld < %ld)\n",
221                                  vb2_plane_size(vb, i), size);
222                         return -EINVAL;
223                 }
224                 vb2_set_plane_payload(vb, i, size);
225         }
226
227         return 0;
228 }
229
230 static void buffer_queue(struct vb2_buffer *vb)
231 {
232         struct fimc_vid_buffer *buf
233                 = container_of(vb, struct fimc_vid_buffer, vb);
234         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
235         struct fimc_dev *fimc = ctx->fimc_dev;
236         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
237         unsigned long flags;
238         int min_bufs;
239
240         spin_lock_irqsave(&fimc->slock, flags);
241         fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
242
243         if (!test_bit(ST_CAPT_STREAM, &fimc->state)
244              && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
245                 /* Setup the buffer directly for processing. */
246                 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
247                                 vid_cap->buf_index;
248
249                 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
250                 buf->index = vid_cap->buf_index;
251                 active_queue_add(vid_cap, buf);
252
253                 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
254                         vid_cap->buf_index = 0;
255         } else {
256                 fimc_pending_queue_add(vid_cap, buf);
257         }
258
259         min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
260
261
262         if (vb2_is_streaming(&vid_cap->vbq) &&
263             vid_cap->active_buf_cnt >= min_bufs &&
264             !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
265                 fimc_activate_capture(ctx);
266                 spin_unlock_irqrestore(&fimc->slock, flags);
267
268                 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
269                         fimc_pipeline_s_stream(fimc, 1);
270                 return;
271         }
272         spin_unlock_irqrestore(&fimc->slock, flags);
273 }
274
275 static void fimc_lock(struct vb2_queue *vq)
276 {
277         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
278         mutex_lock(&ctx->fimc_dev->lock);
279 }
280
281 static void fimc_unlock(struct vb2_queue *vq)
282 {
283         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
284         mutex_unlock(&ctx->fimc_dev->lock);
285 }
286
287 static struct vb2_ops fimc_capture_qops = {
288         .queue_setup            = queue_setup,
289         .buf_prepare            = buffer_prepare,
290         .buf_queue              = buffer_queue,
291         .wait_prepare           = fimc_unlock,
292         .wait_finish            = fimc_lock,
293         .start_streaming        = start_streaming,
294         .stop_streaming         = stop_streaming,
295 };
296
297 /**
298  * fimc_capture_ctrls_create - initialize the control handler
299  * Initialize the capture video node control handler and fill it
300  * with the FIMC controls. Inherit any sensor's controls if the
301  * 'user_subdev_api' flag is false (default behaviour).
302  * This function need to be called with the graph mutex held.
303  */
304 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
305 {
306         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
307         int ret;
308
309         if (WARN_ON(vid_cap->ctx == NULL))
310                 return -ENXIO;
311         if (vid_cap->ctx->ctrls_rdy)
312                 return 0;
313
314         ret = fimc_ctrls_create(vid_cap->ctx);
315         if (ret || vid_cap->user_subdev_api)
316                 return ret;
317
318         return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
319                                     fimc->pipeline.sensor->ctrl_handler);
320 }
321
322 static int fimc_capture_open(struct file *file)
323 {
324         struct fimc_dev *fimc = video_drvdata(file);
325         int ret = v4l2_fh_open(file);
326
327         if (ret)
328                 return ret;
329
330         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
331
332         /* Return if the corresponding video mem2mem node is already opened. */
333         if (fimc_m2m_active(fimc))
334                 return -EBUSY;
335
336         pm_runtime_get_sync(&fimc->pdev->dev);
337
338         if (++fimc->vid_cap.refcnt == 1) {
339                 ret = fimc_pipeline_initialize(fimc,
340                                &fimc->vid_cap.vfd->entity, true);
341                 if (ret < 0) {
342                         dev_err(&fimc->pdev->dev,
343                                 "Video pipeline initialization failed\n");
344                         pm_runtime_put_sync(&fimc->pdev->dev);
345                         fimc->vid_cap.refcnt--;
346                         v4l2_fh_release(file);
347                         return ret;
348                 }
349                 ret = fimc_capture_ctrls_create(fimc);
350         }
351         return ret;
352 }
353
354 static int fimc_capture_close(struct file *file)
355 {
356         struct fimc_dev *fimc = video_drvdata(file);
357
358         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
359
360         if (--fimc->vid_cap.refcnt == 0) {
361                 fimc_stop_capture(fimc);
362                 fimc_pipeline_shutdown(fimc);
363                 fimc_ctrls_delete(fimc->vid_cap.ctx);
364                 vb2_queue_release(&fimc->vid_cap.vbq);
365         }
366
367         pm_runtime_put(&fimc->pdev->dev);
368
369         return v4l2_fh_release(file);
370 }
371
372 static unsigned int fimc_capture_poll(struct file *file,
373                                       struct poll_table_struct *wait)
374 {
375         struct fimc_dev *fimc = video_drvdata(file);
376
377         return vb2_poll(&fimc->vid_cap.vbq, file, wait);
378 }
379
380 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
381 {
382         struct fimc_dev *fimc = video_drvdata(file);
383
384         return vb2_mmap(&fimc->vid_cap.vbq, vma);
385 }
386
387 /* video device file operations */
388 static const struct v4l2_file_operations fimc_capture_fops = {
389         .owner          = THIS_MODULE,
390         .open           = fimc_capture_open,
391         .release        = fimc_capture_close,
392         .poll           = fimc_capture_poll,
393         .unlocked_ioctl = video_ioctl2,
394         .mmap           = fimc_capture_mmap,
395 };
396
397 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
398                                         struct v4l2_capability *cap)
399 {
400         struct fimc_dev *fimc = video_drvdata(file);
401
402         strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
403         strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
404         cap->bus_info[0] = 0;
405         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
406                             V4L2_CAP_VIDEO_CAPTURE_MPLANE;
407
408         return 0;
409 }
410
411 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
412                                     struct v4l2_fmtdesc *f)
413 {
414         struct fimc_fmt *fmt;
415
416         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
417                                f->index);
418         if (!fmt)
419                 return -EINVAL;
420         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
421         f->pixelformat = fmt->fourcc;
422         if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
423                 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
424         return 0;
425 }
426
427
428
429
430
431
432
433 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
434                                  struct v4l2_format *f)
435 {
436         struct fimc_dev *fimc = video_drvdata(file);
437         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
438
439         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
440                 return -EINVAL;
441
442         return fimc_fill_format(&ctx->d_frame, f);
443 }
444
445 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
446                                    struct v4l2_format *f)
447 {
448         struct fimc_dev *fimc = video_drvdata(file);
449         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
450
451         return 0;
452 }
453
454 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
455                                  struct v4l2_format *f)
456 {
457         struct fimc_dev *fimc = video_drvdata(file);
458         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
459         struct v4l2_pix_format_mplane *pix;
460         struct fimc_frame *frame;
461         int ret;
462         int i;
463
464         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
465                 return -EINVAL;
466
467         if (vb2_is_busy(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
468                 return -EBUSY;
469
470         frame = &ctx->d_frame;
471
472         pix = &f->fmt.pix_mp;
473         frame->fmt = fimc_find_format(&pix->pixelformat, NULL,
474                                       FMT_FLAGS_M2M | FMT_FLAGS_CAM, 0);
475         if (WARN(frame->fmt == NULL, "Pixel format lookup failed\n"))
476                 return -EINVAL;
477
478         for (i = 0; i < frame->fmt->colplanes; i++) {
479                 frame->payload[i] =
480                         (pix->width * pix->height * frame->fmt->depth[i]) >> 3;
481         }
482
483         /* Output DMA frame pixel size and offsets. */
484         frame->f_width = pix->plane_fmt[0].bytesperline * 8
485                         / frame->fmt->depth[0];
486         frame->f_height = pix->height;
487         frame->width    = pix->width;
488         frame->height   = pix->height;
489         frame->o_width  = pix->width;
490         frame->o_height = pix->height;
491         frame->offs_h   = 0;
492         frame->offs_v   = 0;
493
494         ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
495
496         return ret;
497 }
498
499 static int fimc_cap_enum_input(struct file *file, void *priv,
500                                struct v4l2_input *i)
501 {
502         struct fimc_dev *fimc = video_drvdata(file);
503         struct v4l2_subdev *sd = fimc->pipeline.sensor;
504
505         if (i->index != 0)
506                 return -EINVAL;
507
508         i->type = V4L2_INPUT_TYPE_CAMERA;
509         if (sd)
510                 strlcpy(i->name, sd->name, sizeof(i->name));
511         return 0;
512 }
513
514 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
515 {
516         return i == 0 ? i : -EINVAL;
517 }
518
519 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
520 {
521         *i = 0;
522         return 0;
523 }
524
525 static int fimc_cap_streamon(struct file *file, void *priv,
526                              enum v4l2_buf_type type)
527 {
528         struct fimc_dev *fimc = video_drvdata(file);
529         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
530         struct fimc_pipeline *p = &fimc->pipeline;
531
532         if (fimc_capture_active(fimc))
533                 return -EBUSY;
534
535         if (!(ctx->state & FIMC_DST_FMT)) {
536                 v4l2_err(fimc->vid_cap.vfd, "Format is not set\n");
537                 return -EINVAL;
538         }
539         media_entity_pipeline_start(&p->sensor->entity, p->pipe);
540
541         return vb2_streamon(&fimc->vid_cap.vbq, type);
542 }
543
544 static int fimc_cap_streamoff(struct file *file, void *priv,
545                             enum v4l2_buf_type type)
546 {
547         struct fimc_dev *fimc = video_drvdata(file);
548         struct v4l2_subdev *sd = fimc->pipeline.sensor;
549         int ret;
550
551         ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
552         if (ret == 0)
553                 media_entity_pipeline_stop(&sd->entity);
554         return ret;
555 }
556
557 static int fimc_cap_reqbufs(struct file *file, void *priv,
558                             struct v4l2_requestbuffers *reqbufs)
559 {
560         struct fimc_dev *fimc = video_drvdata(file);
561         int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
562
563         if (!ret)
564                 fimc->vid_cap.reqbufs_count = reqbufs->count;
565         return ret;
566 }
567
568 static int fimc_cap_querybuf(struct file *file, void *priv,
569                            struct v4l2_buffer *buf)
570 {
571         struct fimc_dev *fimc = video_drvdata(file);
572
573         return vb2_querybuf(&fimc->vid_cap.vbq, buf);
574 }
575
576 static int fimc_cap_qbuf(struct file *file, void *priv,
577                           struct v4l2_buffer *buf)
578 {
579         struct fimc_dev *fimc = video_drvdata(file);
580
581         return vb2_qbuf(&fimc->vid_cap.vbq, buf);
582 }
583
584 static int fimc_cap_dqbuf(struct file *file, void *priv,
585                            struct v4l2_buffer *buf)
586 {
587         struct fimc_dev *fimc = video_drvdata(file);
588
589         return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
590 }
591
592 static int fimc_cap_cropcap(struct file *file, void *fh,
593                             struct v4l2_cropcap *cr)
594 {
595         struct fimc_dev *fimc = video_drvdata(file);
596         struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
597
598         if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
599                 return -EINVAL;
600
601         cr->bounds.left         = 0;
602         cr->bounds.top          = 0;
603         cr->bounds.width        = f->o_width;
604         cr->bounds.height       = f->o_height;
605         cr->defrect             = cr->bounds;
606
607         return 0;
608 }
609
610 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
611 {
612         struct fimc_dev *fimc = video_drvdata(file);
613         struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
614
615         cr->c.left      = f->offs_h;
616         cr->c.top       = f->offs_v;
617         cr->c.width     = f->width;
618         cr->c.height    = f->height;
619
620         return 0;
621 }
622
623 static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
624 {
625         struct fimc_dev *fimc = video_drvdata(file);
626         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
627         struct fimc_frame *f;
628         int ret = -EINVAL;
629
630         if (fimc_capture_active(fimc))
631                 return -EBUSY;
632
633         ret = fimc_try_crop(ctx, cr);
634         if (ret)
635                 return ret;
636
637         if (!(ctx->state & FIMC_DST_FMT)) {
638                 v4l2_err(fimc->vid_cap.vfd, "Capture format is not set\n");
639                 return -EINVAL;
640         }
641
642         f = &ctx->s_frame;
643         /* Check for the pixel scaling ratio when cropping input image. */
644         ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height,
645                                       ctx->d_frame.width, ctx->d_frame.height,
646                                       ctx->rotation);
647         if (ret) {
648                 v4l2_err(fimc->vid_cap.vfd, "Out of the scaler range\n");
649                 return ret;
650         }
651
652         f->offs_h = cr->c.left;
653         f->offs_v = cr->c.top;
654         f->width  = cr->c.width;
655         f->height = cr->c.height;
656
657         return 0;
658 }
659
660 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
661         .vidioc_querycap                = fimc_vidioc_querycap_capture,
662
663         .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
664         .vidioc_try_fmt_vid_cap_mplane  = fimc_cap_try_fmt_mplane,
665         .vidioc_s_fmt_vid_cap_mplane    = fimc_cap_s_fmt_mplane,
666         .vidioc_g_fmt_vid_cap_mplane    = fimc_cap_g_fmt_mplane,
667
668         .vidioc_reqbufs                 = fimc_cap_reqbufs,
669         .vidioc_querybuf                = fimc_cap_querybuf,
670
671         .vidioc_qbuf                    = fimc_cap_qbuf,
672         .vidioc_dqbuf                   = fimc_cap_dqbuf,
673
674         .vidioc_streamon                = fimc_cap_streamon,
675         .vidioc_streamoff               = fimc_cap_streamoff,
676
677         .vidioc_g_crop                  = fimc_cap_g_crop,
678         .vidioc_s_crop                  = fimc_cap_s_crop,
679         .vidioc_cropcap                 = fimc_cap_cropcap,
680
681         .vidioc_enum_input              = fimc_cap_enum_input,
682         .vidioc_s_input                 = fimc_cap_s_input,
683         .vidioc_g_input                 = fimc_cap_g_input,
684 };
685
686 /* Media operations */
687 static int fimc_link_setup(struct media_entity *entity,
688                            const struct media_pad *local,
689                            const struct media_pad *remote, u32 flags)
690 {
691         struct video_device *vd = media_entity_to_video_device(entity);
692         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(remote->entity);
693         struct fimc_dev *fimc = video_get_drvdata(vd);
694
695         if (WARN_ON(fimc == NULL))
696                 return 0;
697
698         dbg("%s --> %s, flags: 0x%x. input: 0x%x",
699             local->entity->name, remote->entity->name, flags,
700             fimc->vid_cap.input);
701
702         if (flags & MEDIA_LNK_FL_ENABLED) {
703                 if (fimc->vid_cap.input != 0)
704                         return -EBUSY;
705                 fimc->vid_cap.input = sd->grp_id;
706                 return 0;
707         }
708
709         fimc->vid_cap.input = 0;
710         return 0;
711 }
712
713 static const struct media_entity_operations fimc_media_ops = {
714         .link_setup = fimc_link_setup,
715 };
716
717 /* fimc->lock must be already initialized */
718 int fimc_register_capture_device(struct fimc_dev *fimc,
719                                  struct v4l2_device *v4l2_dev)
720 {
721         struct video_device *vfd;
722         struct fimc_vid_cap *vid_cap;
723         struct fimc_ctx *ctx;
724         struct fimc_frame *fr;
725         struct vb2_queue *q;
726         int ret = -ENOMEM;
727
728         ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
729         if (!ctx)
730                 return -ENOMEM;
731
732         ctx->fimc_dev    = fimc;
733         ctx->in_path     = FIMC_CAMERA;
734         ctx->out_path    = FIMC_DMA;
735         ctx->state       = FIMC_CTX_CAP;
736
737         /* Default format of the output frames */
738         fr = &ctx->d_frame;
739         fr->fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
740         fr->width = fr->f_width = fr->o_width = 640;
741         fr->height = fr->f_height = fr->o_height = 480;
742
743         vfd = video_device_alloc();
744         if (!vfd) {
745                 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
746                 goto err_vd_alloc;
747         }
748
749         snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
750                  dev_name(&fimc->pdev->dev));
751
752         vfd->fops       = &fimc_capture_fops;
753         vfd->ioctl_ops  = &fimc_capture_ioctl_ops;
754         vfd->v4l2_dev   = v4l2_dev;
755         vfd->minor      = -1;
756         vfd->release    = video_device_release;
757         vfd->lock       = &fimc->lock;
758         video_set_drvdata(vfd, fimc);
759
760         vid_cap = &fimc->vid_cap;
761         vid_cap->vfd = vfd;
762         vid_cap->active_buf_cnt = 0;
763         vid_cap->reqbufs_count  = 0;
764         vid_cap->refcnt = 0;
765
766         INIT_LIST_HEAD(&vid_cap->pending_buf_q);
767         INIT_LIST_HEAD(&vid_cap->active_buf_q);
768         spin_lock_init(&ctx->slock);
769         vid_cap->ctx = ctx;
770
771         q = &fimc->vid_cap.vbq;
772         memset(q, 0, sizeof(*q));
773         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
774         q->io_modes = VB2_MMAP | VB2_USERPTR;
775         q->drv_priv = fimc->vid_cap.ctx;
776         q->ops = &fimc_capture_qops;
777         q->mem_ops = &vb2_dma_contig_memops;
778         q->buf_struct_size = sizeof(struct fimc_vid_buffer);
779
780         vb2_queue_init(q);
781
782         fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
783         ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
784         if (ret)
785                 goto err_ent;
786
787         vfd->entity.ops = &fimc_media_ops;
788         vfd->ctrl_handler = &ctx->ctrl_handler;
789         return 0;
790
791 err_ent:
792         video_device_release(vfd);
793 err_vd_alloc:
794         kfree(ctx);
795         return ret;
796 }
797
798 void fimc_unregister_capture_device(struct fimc_dev *fimc)
799 {
800         struct video_device *vfd = fimc->vid_cap.vfd;
801
802         if (vfd) {
803                 media_entity_cleanup(&vfd->entity);
804                 /* Can also be called if video device was
805                    not registered */
806                 video_unregister_device(vfd);
807         }
808         kfree(fimc->vid_cap.ctx);
809         fimc->vid_cap.ctx = NULL;
810 }