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