]> Pileus Git - ~andy/linux/blob - drivers/media/platform/s5p-fimc/fimc-capture.c
[media] s5p-fimc: Use vb2 ioctl/fop helpers in FIMC capture driver
[~andy/linux] / drivers / media / platform / s5p-fimc / fimc-capture.c
1 /*
2  * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
3  *
4  * Copyright (C) 2010 - 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 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 #include "fimc-reg.h"
33
34 static int fimc_capture_hw_init(struct fimc_dev *fimc)
35 {
36         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
37         struct fimc_pipeline *p = &fimc->pipeline;
38         struct fimc_sensor_info *sensor;
39         unsigned long flags;
40         int ret = 0;
41
42         if (p->subdevs[IDX_SENSOR] == NULL || ctx == NULL)
43                 return -ENXIO;
44         if (ctx->s_frame.fmt == NULL)
45                 return -EINVAL;
46
47         sensor = v4l2_get_subdev_hostdata(p->subdevs[IDX_SENSOR]);
48
49         spin_lock_irqsave(&fimc->slock, flags);
50         fimc_prepare_dma_offset(ctx, &ctx->d_frame);
51         fimc_set_yuv_order(ctx);
52
53         fimc_hw_set_camera_polarity(fimc, &sensor->pdata);
54         fimc_hw_set_camera_type(fimc, &sensor->pdata);
55         fimc_hw_set_camera_source(fimc, &sensor->pdata);
56         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
57
58         ret = fimc_set_scaler_info(ctx);
59         if (!ret) {
60                 fimc_hw_set_input_path(ctx);
61                 fimc_hw_set_prescaler(ctx);
62                 fimc_hw_set_mainscaler(ctx);
63                 fimc_hw_set_target_format(ctx);
64                 fimc_hw_set_rotation(ctx);
65                 fimc_hw_set_effect(ctx);
66                 fimc_hw_set_output_path(ctx);
67                 fimc_hw_set_out_dma(ctx);
68                 if (fimc->variant->has_alpha)
69                         fimc_hw_set_rgb_alpha(ctx);
70                 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
71         }
72         spin_unlock_irqrestore(&fimc->slock, flags);
73         return ret;
74 }
75
76 /*
77  * Reinitialize the driver so it is ready to start the streaming again.
78  * Set fimc->state to indicate stream off and the hardware shut down state.
79  * If not suspending (@suspend is false), return any buffers to videobuf2.
80  * Otherwise put any owned buffers onto the pending buffers queue, so they
81  * can be re-spun when the device is being resumed. Also perform FIMC
82  * software reset and disable streaming on the whole pipeline if required.
83  */
84 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
85 {
86         struct fimc_vid_cap *cap = &fimc->vid_cap;
87         struct fimc_vid_buffer *buf;
88         unsigned long flags;
89         bool streaming;
90
91         spin_lock_irqsave(&fimc->slock, flags);
92         streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
93
94         fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
95                          1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
96         if (suspend)
97                 fimc->state |= (1 << ST_CAPT_SUSPENDED);
98         else
99                 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
100
101         /* Release unused buffers */
102         while (!suspend && !list_empty(&cap->pending_buf_q)) {
103                 buf = fimc_pending_queue_pop(cap);
104                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
105         }
106         /* If suspending put unused buffers onto pending queue */
107         while (!list_empty(&cap->active_buf_q)) {
108                 buf = fimc_active_queue_pop(cap);
109                 if (suspend)
110                         fimc_pending_queue_add(cap, buf);
111                 else
112                         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
113         }
114
115         fimc_hw_reset(fimc);
116         cap->buf_index = 0;
117
118         spin_unlock_irqrestore(&fimc->slock, flags);
119
120         if (streaming)
121                 return fimc_pipeline_call(fimc, set_stream,
122                                           &fimc->pipeline, 0);
123         else
124                 return 0;
125 }
126
127 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
128 {
129         unsigned long flags;
130
131         if (!fimc_capture_active(fimc))
132                 return 0;
133
134         spin_lock_irqsave(&fimc->slock, flags);
135         set_bit(ST_CAPT_SHUT, &fimc->state);
136         fimc_deactivate_capture(fimc);
137         spin_unlock_irqrestore(&fimc->slock, flags);
138
139         wait_event_timeout(fimc->irq_queue,
140                            !test_bit(ST_CAPT_SHUT, &fimc->state),
141                            (2*HZ/10)); /* 200 ms */
142
143         return fimc_capture_state_cleanup(fimc, suspend);
144 }
145
146 /**
147  * fimc_capture_config_update - apply the camera interface configuration
148  *
149  * To be called from within the interrupt handler with fimc.slock
150  * spinlock held. It updates the camera pixel crop, rotation and
151  * image flip in H/W.
152  */
153 static int fimc_capture_config_update(struct fimc_ctx *ctx)
154 {
155         struct fimc_dev *fimc = ctx->fimc_dev;
156         int ret;
157
158         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
159
160         ret = fimc_set_scaler_info(ctx);
161         if (ret)
162                 return ret;
163
164         fimc_hw_set_prescaler(ctx);
165         fimc_hw_set_mainscaler(ctx);
166         fimc_hw_set_target_format(ctx);
167         fimc_hw_set_rotation(ctx);
168         fimc_hw_set_effect(ctx);
169         fimc_prepare_dma_offset(ctx, &ctx->d_frame);
170         fimc_hw_set_out_dma(ctx);
171         if (fimc->variant->has_alpha)
172                 fimc_hw_set_rgb_alpha(ctx);
173
174         clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
175         return ret;
176 }
177
178 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
179 {
180         struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
181         struct fimc_vid_cap *cap = &fimc->vid_cap;
182         struct fimc_frame *f = &cap->ctx->d_frame;
183         struct fimc_vid_buffer *v_buf;
184         struct timeval *tv;
185         struct timespec ts;
186
187         if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
188                 wake_up(&fimc->irq_queue);
189                 goto done;
190         }
191
192         if (!list_empty(&cap->active_buf_q) &&
193             test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
194                 ktime_get_real_ts(&ts);
195
196                 v_buf = fimc_active_queue_pop(cap);
197
198                 tv = &v_buf->vb.v4l2_buf.timestamp;
199                 tv->tv_sec = ts.tv_sec;
200                 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
201                 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
202
203                 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
204         }
205
206         if (!list_empty(&cap->pending_buf_q)) {
207
208                 v_buf = fimc_pending_queue_pop(cap);
209                 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
210                 v_buf->index = cap->buf_index;
211
212                 /* Move the buffer to the capture active queue */
213                 fimc_active_queue_add(cap, v_buf);
214
215                 dbg("next frame: %d, done frame: %d",
216                     fimc_hw_get_frame_index(fimc), v_buf->index);
217
218                 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
219                         cap->buf_index = 0;
220         }
221         /*
222          * Set up a buffer at MIPI-CSIS if current image format
223          * requires the frame embedded data capture.
224          */
225         if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
226                 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
227                 unsigned int size = f->payload[plane];
228                 s32 index = fimc_hw_get_frame_index(fimc);
229                 void *vaddr;
230
231                 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
232                         if (v_buf->index != index)
233                                 continue;
234                         vaddr = vb2_plane_vaddr(&v_buf->vb, plane);
235                         v4l2_subdev_call(csis, video, s_rx_buffer,
236                                          vaddr, &size);
237                         break;
238                 }
239         }
240
241         if (cap->active_buf_cnt == 0) {
242                 if (deq_buf)
243                         clear_bit(ST_CAPT_RUN, &fimc->state);
244
245                 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
246                         cap->buf_index = 0;
247         } else {
248                 set_bit(ST_CAPT_RUN, &fimc->state);
249         }
250
251         if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
252                 fimc_capture_config_update(cap->ctx);
253 done:
254         if (cap->active_buf_cnt == 1) {
255                 fimc_deactivate_capture(fimc);
256                 clear_bit(ST_CAPT_STREAM, &fimc->state);
257         }
258
259         dbg("frame: %d, active_buf_cnt: %d",
260             fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
261 }
262
263
264 static int start_streaming(struct vb2_queue *q, unsigned int count)
265 {
266         struct fimc_ctx *ctx = q->drv_priv;
267         struct fimc_dev *fimc = ctx->fimc_dev;
268         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
269         int min_bufs;
270         int ret;
271
272         vid_cap->frame_count = 0;
273
274         ret = fimc_capture_hw_init(fimc);
275         if (ret) {
276                 fimc_capture_state_cleanup(fimc, false);
277                 return ret;
278         }
279
280         set_bit(ST_CAPT_PEND, &fimc->state);
281
282         min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
283
284         if (vid_cap->active_buf_cnt >= min_bufs &&
285             !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
286                 fimc_activate_capture(ctx);
287
288                 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
289                         fimc_pipeline_call(fimc, set_stream,
290                                            &fimc->pipeline, 1);
291         }
292
293         return 0;
294 }
295
296 static int stop_streaming(struct vb2_queue *q)
297 {
298         struct fimc_ctx *ctx = q->drv_priv;
299         struct fimc_dev *fimc = ctx->fimc_dev;
300
301         if (!fimc_capture_active(fimc))
302                 return -EINVAL;
303
304         return fimc_stop_capture(fimc, false);
305 }
306
307 int fimc_capture_suspend(struct fimc_dev *fimc)
308 {
309         bool suspend = fimc_capture_busy(fimc);
310
311         int ret = fimc_stop_capture(fimc, suspend);
312         if (ret)
313                 return ret;
314         return fimc_pipeline_call(fimc, close, &fimc->pipeline);
315 }
316
317 static void buffer_queue(struct vb2_buffer *vb);
318
319 int fimc_capture_resume(struct fimc_dev *fimc)
320 {
321         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
322         struct fimc_vid_buffer *buf;
323         int i;
324
325         if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
326                 return 0;
327
328         INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
329         vid_cap->buf_index = 0;
330         fimc_pipeline_call(fimc, open, &fimc->pipeline,
331                            &vid_cap->vfd.entity, false);
332         fimc_capture_hw_init(fimc);
333
334         clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
335
336         for (i = 0; i < vid_cap->reqbufs_count; i++) {
337                 if (list_empty(&vid_cap->pending_buf_q))
338                         break;
339                 buf = fimc_pending_queue_pop(vid_cap);
340                 buffer_queue(&buf->vb);
341         }
342         return 0;
343
344 }
345
346 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
347                        unsigned int *num_buffers, unsigned int *num_planes,
348                        unsigned int sizes[], void *allocators[])
349 {
350         const struct v4l2_pix_format_mplane *pixm = NULL;
351         struct fimc_ctx *ctx = vq->drv_priv;
352         struct fimc_frame *frame = &ctx->d_frame;
353         struct fimc_fmt *fmt = frame->fmt;
354         unsigned long wh;
355         int i;
356
357         if (pfmt) {
358                 pixm = &pfmt->fmt.pix_mp;
359                 fmt = fimc_find_format(&pixm->pixelformat, NULL,
360                                        FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
361                 wh = pixm->width * pixm->height;
362         } else {
363                 wh = frame->f_width * frame->f_height;
364         }
365
366         if (fmt == NULL)
367                 return -EINVAL;
368
369         *num_planes = fmt->memplanes;
370
371         for (i = 0; i < fmt->memplanes; i++) {
372                 unsigned int size = (wh * fmt->depth[i]) / 8;
373                 if (pixm)
374                         sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
375                 else if (fimc_fmt_is_user_defined(fmt->color))
376                         sizes[i] = frame->payload[i];
377                 else
378                         sizes[i] = max_t(u32, size, frame->payload[i]);
379
380                 allocators[i] = ctx->fimc_dev->alloc_ctx;
381         }
382
383         return 0;
384 }
385
386 static int buffer_prepare(struct vb2_buffer *vb)
387 {
388         struct vb2_queue *vq = vb->vb2_queue;
389         struct fimc_ctx *ctx = vq->drv_priv;
390         int i;
391
392         if (ctx->d_frame.fmt == NULL)
393                 return -EINVAL;
394
395         for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
396                 unsigned long size = ctx->d_frame.payload[i];
397
398                 if (vb2_plane_size(vb, i) < size) {
399                         v4l2_err(&ctx->fimc_dev->vid_cap.vfd,
400                                  "User buffer too small (%ld < %ld)\n",
401                                  vb2_plane_size(vb, i), size);
402                         return -EINVAL;
403                 }
404                 vb2_set_plane_payload(vb, i, size);
405         }
406
407         return 0;
408 }
409
410 static void buffer_queue(struct vb2_buffer *vb)
411 {
412         struct fimc_vid_buffer *buf
413                 = container_of(vb, struct fimc_vid_buffer, vb);
414         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
415         struct fimc_dev *fimc = ctx->fimc_dev;
416         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
417         unsigned long flags;
418         int min_bufs;
419
420         spin_lock_irqsave(&fimc->slock, flags);
421         fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
422
423         if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
424             !test_bit(ST_CAPT_STREAM, &fimc->state) &&
425             vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
426                 /* Setup the buffer directly for processing. */
427                 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
428                                 vid_cap->buf_index;
429
430                 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
431                 buf->index = vid_cap->buf_index;
432                 fimc_active_queue_add(vid_cap, buf);
433
434                 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
435                         vid_cap->buf_index = 0;
436         } else {
437                 fimc_pending_queue_add(vid_cap, buf);
438         }
439
440         min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
441
442
443         if (vb2_is_streaming(&vid_cap->vbq) &&
444             vid_cap->active_buf_cnt >= min_bufs &&
445             !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
446                 fimc_activate_capture(ctx);
447                 spin_unlock_irqrestore(&fimc->slock, flags);
448
449                 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
450                         fimc_pipeline_call(fimc, set_stream,
451                                            &fimc->pipeline, 1);
452                 return;
453         }
454         spin_unlock_irqrestore(&fimc->slock, flags);
455 }
456
457 static struct vb2_ops fimc_capture_qops = {
458         .queue_setup            = queue_setup,
459         .buf_prepare            = buffer_prepare,
460         .buf_queue              = buffer_queue,
461         .wait_prepare           = vb2_ops_wait_prepare,
462         .wait_finish            = vb2_ops_wait_finish,
463         .start_streaming        = start_streaming,
464         .stop_streaming         = stop_streaming,
465 };
466
467 /**
468  * fimc_capture_ctrls_create - initialize the control handler
469  * Initialize the capture video node control handler and fill it
470  * with the FIMC controls. Inherit any sensor's controls if the
471  * 'user_subdev_api' flag is false (default behaviour).
472  * This function need to be called with the graph mutex held.
473  */
474 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
475 {
476         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
477         struct v4l2_subdev *sensor = fimc->pipeline.subdevs[IDX_SENSOR];
478         int ret;
479
480         if (WARN_ON(vid_cap->ctx == NULL))
481                 return -ENXIO;
482         if (vid_cap->ctx->ctrls.ready)
483                 return 0;
484
485         ret = fimc_ctrls_create(vid_cap->ctx);
486
487         if (ret || vid_cap->user_subdev_api || !sensor ||
488             !vid_cap->ctx->ctrls.ready)
489                 return ret;
490
491         return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrls.handler,
492                                      sensor->ctrl_handler, NULL);
493 }
494
495 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
496
497 static int fimc_capture_open(struct file *file)
498 {
499         struct fimc_dev *fimc = video_drvdata(file);
500         int ret = -EBUSY;
501
502         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
503
504         fimc_md_graph_lock(fimc);
505         mutex_lock(&fimc->lock);
506
507         if (fimc_m2m_active(fimc))
508                 goto unlock;
509
510         set_bit(ST_CAPT_BUSY, &fimc->state);
511         ret = pm_runtime_get_sync(&fimc->pdev->dev);
512         if (ret < 0)
513                 goto unlock;
514
515         ret = v4l2_fh_open(file);
516         if (ret) {
517                 pm_runtime_put(&fimc->pdev->dev);
518                 goto unlock;
519         }
520
521         if (v4l2_fh_is_singular_file(file)) {
522                 ret = fimc_pipeline_call(fimc, open, &fimc->pipeline,
523                                          &fimc->vid_cap.vfd.entity, true);
524
525                 if (!ret && !fimc->vid_cap.user_subdev_api)
526                         ret = fimc_capture_set_default_format(fimc);
527
528                 if (!ret)
529                         ret = fimc_capture_ctrls_create(fimc);
530
531                 if (ret < 0) {
532                         clear_bit(ST_CAPT_BUSY, &fimc->state);
533                         pm_runtime_put_sync(&fimc->pdev->dev);
534                         v4l2_fh_release(file);
535                 } else {
536                         fimc->vid_cap.refcnt++;
537                 }
538         }
539 unlock:
540         mutex_unlock(&fimc->lock);
541         fimc_md_graph_unlock(fimc);
542         return ret;
543 }
544
545 static int fimc_capture_release(struct file *file)
546 {
547         struct fimc_dev *fimc = video_drvdata(file);
548         int ret;
549
550         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
551
552         mutex_lock(&fimc->lock);
553
554         if (v4l2_fh_is_singular_file(file)) {
555                 clear_bit(ST_CAPT_BUSY, &fimc->state);
556                 fimc_stop_capture(fimc, false);
557                 fimc_pipeline_call(fimc, close, &fimc->pipeline);
558                 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
559                 fimc->vid_cap.refcnt--;
560         }
561
562         pm_runtime_put(&fimc->pdev->dev);
563
564         if (v4l2_fh_is_singular_file(file))
565                 fimc_ctrls_delete(fimc->vid_cap.ctx);
566
567         ret = vb2_fop_release(file);
568         mutex_unlock(&fimc->lock);
569
570         return ret;
571 }
572
573 static const struct v4l2_file_operations fimc_capture_fops = {
574         .owner          = THIS_MODULE,
575         .open           = fimc_capture_open,
576         .release        = fimc_capture_release,
577         .poll           = vb2_fop_poll,
578         .unlocked_ioctl = video_ioctl2,
579         .mmap           = vb2_fop_mmap,
580 };
581
582 /*
583  * Format and crop negotiation helpers
584  */
585
586 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
587                                                 u32 *width, u32 *height,
588                                                 u32 *code, u32 *fourcc, int pad)
589 {
590         bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
591         struct fimc_dev *fimc = ctx->fimc_dev;
592         const struct fimc_variant *var = fimc->variant;
593         const struct fimc_pix_limit *pl = var->pix_limit;
594         struct fimc_frame *dst = &ctx->d_frame;
595         u32 depth, min_w, max_w, min_h, align_h = 3;
596         u32 mask = FMT_FLAGS_CAM;
597         struct fimc_fmt *ffmt;
598
599         /* Conversion from/to JPEG or User Defined format is not supported */
600         if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
601             fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
602                 *code = ctx->s_frame.fmt->mbus_code;
603
604         if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
605                 mask |= FMT_FLAGS_M2M;
606
607         ffmt = fimc_find_format(fourcc, code, mask, 0);
608         if (WARN_ON(!ffmt))
609                 return NULL;
610         if (code)
611                 *code = ffmt->mbus_code;
612         if (fourcc)
613                 *fourcc = ffmt->fourcc;
614
615         if (pad == FIMC_SD_PAD_SINK) {
616                 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
617                         pl->scaler_dis_w : pl->scaler_en_w;
618                 /* Apply the camera input interface pixel constraints */
619                 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
620                                       height, max_t(u32, *height, 32),
621                                       FIMC_CAMIF_MAX_HEIGHT,
622                                       fimc_fmt_is_user_defined(ffmt->color) ?
623                                       3 : 1,
624                                       0);
625                 return ffmt;
626         }
627         /* Can't scale or crop in transparent (JPEG) transfer mode */
628         if (fimc_fmt_is_user_defined(ffmt->color)) {
629                 *width  = ctx->s_frame.f_width;
630                 *height = ctx->s_frame.f_height;
631                 return ffmt;
632         }
633         /* Apply the scaler and the output DMA constraints */
634         max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
635         if (ctx->state & FIMC_COMPOSE) {
636                 min_w = dst->offs_h + dst->width;
637                 min_h = dst->offs_v + dst->height;
638         } else {
639                 min_w = var->min_out_pixsize;
640                 min_h = var->min_out_pixsize;
641         }
642         if (var->min_vsize_align == 1 && !rotation)
643                 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
644
645         depth = fimc_get_format_depth(ffmt);
646         v4l_bound_align_image(width, min_w, max_w,
647                               ffs(var->min_out_pixsize) - 1,
648                               height, min_h, FIMC_CAMIF_MAX_HEIGHT,
649                               align_h,
650                               64/(ALIGN(depth, 8)));
651
652         dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
653             pad, code ? *code : 0, *width, *height,
654             dst->f_width, dst->f_height);
655
656         return ffmt;
657 }
658
659 static void fimc_capture_try_selection(struct fimc_ctx *ctx,
660                                        struct v4l2_rect *r,
661                                        int target)
662 {
663         bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
664         struct fimc_dev *fimc = ctx->fimc_dev;
665         const struct fimc_variant *var = fimc->variant;
666         const struct fimc_pix_limit *pl = var->pix_limit;
667         struct fimc_frame *sink = &ctx->s_frame;
668         u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
669         u32 align_sz = 0, align_h = 4;
670         u32 max_sc_h, max_sc_v;
671
672         /* In JPEG transparent transfer mode cropping is not supported */
673         if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
674                 r->width  = sink->f_width;
675                 r->height = sink->f_height;
676                 r->left   = r->top = 0;
677                 return;
678         }
679         if (target == V4L2_SEL_TGT_COMPOSE) {
680                 if (ctx->rotation != 90 && ctx->rotation != 270)
681                         align_h = 1;
682                 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
683                 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
684                 min_sz = var->min_out_pixsize;
685         } else {
686                 u32 depth = fimc_get_format_depth(sink->fmt);
687                 align_sz = 64/ALIGN(depth, 8);
688                 min_sz = var->min_inp_pixsize;
689                 min_w = min_h = min_sz;
690                 max_sc_h = max_sc_v = 1;
691         }
692         /*
693          * For the compose rectangle the following constraints must be met:
694          * - it must fit in the sink pad format rectangle (f_width/f_height);
695          * - maximum downscaling ratio is 64;
696          * - maximum crop size depends if the rotator is used or not;
697          * - the sink pad format width/height must be 4 multiple of the
698          *   prescaler ratios determined by sink pad size and source pad crop,
699          *   the prescaler ratio is returned by fimc_get_scaler_factor().
700          */
701         max_w = min_t(u32,
702                       rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
703                       rotate ? sink->f_height : sink->f_width);
704         max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
705
706         if (target == V4L2_SEL_TGT_COMPOSE) {
707                 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
708                 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
709                 if (rotate) {
710                         swap(max_sc_h, max_sc_v);
711                         swap(min_w, min_h);
712                 }
713         }
714         v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
715                               &r->height, min_h, max_h, align_h,
716                               align_sz);
717         /* Adjust left/top if crop/compose rectangle is out of bounds */
718         r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
719         r->top  = clamp_t(u32, r->top, 0, sink->f_height - r->height);
720         r->left = round_down(r->left, var->hor_offs_align);
721
722         dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
723             target, r->left, r->top, r->width, r->height,
724             sink->f_width, sink->f_height);
725 }
726
727 /*
728  * The video node ioctl operations
729  */
730 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
731                                         struct v4l2_capability *cap)
732 {
733         struct fimc_dev *fimc = video_drvdata(file);
734
735         strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
736         strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
737         cap->bus_info[0] = 0;
738         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
739
740         return 0;
741 }
742
743 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
744                                     struct v4l2_fmtdesc *f)
745 {
746         struct fimc_fmt *fmt;
747
748         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
749                                f->index);
750         if (!fmt)
751                 return -EINVAL;
752         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
753         f->pixelformat = fmt->fourcc;
754         if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
755                 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
756         return 0;
757 }
758
759 static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
760 {
761         struct media_pad *pad = &me->pads[0];
762
763         while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
764                 pad = media_entity_remote_source(pad);
765                 if (!pad)
766                         break;
767                 me = pad->entity;
768                 pad = &me->pads[0];
769         }
770
771         return me;
772 }
773
774 /**
775  * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
776  *                            elements
777  * @ctx: FIMC capture context
778  * @tfmt: media bus format to try/set on subdevs
779  * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
780  * @set: true to set format on subdevs, false to try only
781  */
782 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
783                                     struct v4l2_mbus_framefmt *tfmt,
784                                     struct fimc_fmt **fmt_id,
785                                     bool set)
786 {
787         struct fimc_dev *fimc = ctx->fimc_dev;
788         struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
789         struct v4l2_subdev_format sfmt;
790         struct v4l2_mbus_framefmt *mf = &sfmt.format;
791         struct media_entity *me;
792         struct fimc_fmt *ffmt;
793         struct media_pad *pad;
794         int ret, i = 1;
795         u32 fcc;
796
797         if (WARN_ON(!sd || !tfmt))
798                 return -EINVAL;
799
800         memset(&sfmt, 0, sizeof(sfmt));
801         sfmt.format = *tfmt;
802         sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
803
804         me = fimc_pipeline_get_head(&sd->entity);
805
806         while (1) {
807                 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
808                                         FMT_FLAGS_CAM, i++);
809                 if (ffmt == NULL) {
810                         /*
811                          * Notify user-space if common pixel code for
812                          * host and sensor does not exist.
813                          */
814                         return -EINVAL;
815                 }
816                 mf->code = tfmt->code = ffmt->mbus_code;
817
818                 /* set format on all pipeline subdevs */
819                 while (me != &fimc->vid_cap.subdev.entity) {
820                         sd = media_entity_to_v4l2_subdev(me);
821
822                         sfmt.pad = 0;
823                         ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
824                         if (ret)
825                                 return ret;
826
827                         if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
828                                 sfmt.pad = me->num_pads - 1;
829                                 mf->code = tfmt->code;
830                                 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
831                                                                         &sfmt);
832                                 if (ret)
833                                         return ret;
834                         }
835
836                         pad = media_entity_remote_source(&me->pads[sfmt.pad]);
837                         if (!pad)
838                                 return -EINVAL;
839                         me = pad->entity;
840                 }
841
842                 if (mf->code != tfmt->code)
843                         continue;
844
845                 fcc = ffmt->fourcc;
846                 tfmt->width  = mf->width;
847                 tfmt->height = mf->height;
848                 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
849                                         NULL, &fcc, FIMC_SD_PAD_SINK);
850                 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
851                                         NULL, &fcc, FIMC_SD_PAD_SOURCE);
852                 if (ffmt && ffmt->mbus_code)
853                         mf->code = ffmt->mbus_code;
854                 if (mf->width != tfmt->width || mf->height != tfmt->height)
855                         continue;
856                 tfmt->code = mf->code;
857                 break;
858         }
859
860         if (fmt_id && ffmt)
861                 *fmt_id = ffmt;
862         *tfmt = *mf;
863
864         return 0;
865 }
866
867 /**
868  * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
869  * @sensor: pointer to the sensor subdev
870  * @plane_fmt: provides plane sizes corresponding to the frame layout entries
871  * @try: true to set the frame parameters, false to query only
872  *
873  * This function is used by this driver only for compressed/blob data formats.
874  */
875 static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
876                                       struct v4l2_plane_pix_format *plane_fmt,
877                                       unsigned int num_planes, bool try)
878 {
879         struct v4l2_mbus_frame_desc fd;
880         int i, ret;
881         int pad;
882
883         for (i = 0; i < num_planes; i++)
884                 fd.entry[i].length = plane_fmt[i].sizeimage;
885
886         pad = sensor->entity.num_pads - 1;
887         if (try)
888                 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
889         else
890                 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
891
892         if (ret < 0)
893                 return ret;
894
895         if (num_planes != fd.num_entries)
896                 return -EINVAL;
897
898         for (i = 0; i < num_planes; i++)
899                 plane_fmt[i].sizeimage = fd.entry[i].length;
900
901         if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
902                 v4l2_err(sensor->v4l2_dev,  "Unsupported buffer size: %u\n",
903                          fd.entry[0].length);
904
905                 return -EINVAL;
906         }
907
908         return 0;
909 }
910
911 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
912                                  struct v4l2_format *f)
913 {
914         struct fimc_dev *fimc = video_drvdata(file);
915
916         __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
917         return 0;
918 }
919
920 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
921                                    struct v4l2_format *f)
922 {
923         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
924         struct fimc_dev *fimc = video_drvdata(file);
925         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
926         struct v4l2_mbus_framefmt mf;
927         struct fimc_fmt *ffmt = NULL;
928         int ret = 0;
929
930         fimc_md_graph_lock(fimc);
931         mutex_lock(&fimc->lock);
932
933         if (fimc_jpeg_fourcc(pix->pixelformat)) {
934                 fimc_capture_try_format(ctx, &pix->width, &pix->height,
935                                         NULL, &pix->pixelformat,
936                                         FIMC_SD_PAD_SINK);
937                 ctx->s_frame.f_width  = pix->width;
938                 ctx->s_frame.f_height = pix->height;
939         }
940         ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
941                                        NULL, &pix->pixelformat,
942                                        FIMC_SD_PAD_SOURCE);
943         if (!ffmt) {
944                 ret = -EINVAL;
945                 goto unlock;
946         }
947
948         if (!fimc->vid_cap.user_subdev_api) {
949                 mf.width = pix->width;
950                 mf.height = pix->height;
951                 mf.code = ffmt->mbus_code;
952                 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
953                 pix->width = mf.width;
954                 pix->height = mf.height;
955                 if (ffmt)
956                         pix->pixelformat = ffmt->fourcc;
957         }
958
959         fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
960
961         if (ffmt->flags & FMT_FLAGS_COMPRESSED)
962                 fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
963                                         pix->plane_fmt, ffmt->memplanes, true);
964 unlock:
965         mutex_unlock(&fimc->lock);
966         fimc_md_graph_unlock(fimc);
967
968         return ret;
969 }
970
971 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
972                                         enum fimc_color_fmt color)
973 {
974         bool jpeg = fimc_fmt_is_user_defined(color);
975
976         ctx->scaler.enabled = !jpeg;
977         fimc_ctrls_activate(ctx, !jpeg);
978
979         if (jpeg)
980                 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
981         else
982                 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
983 }
984
985 static int __fimc_capture_set_format(struct fimc_dev *fimc,
986                                      struct v4l2_format *f)
987 {
988         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
989         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
990         struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
991         struct fimc_frame *ff = &ctx->d_frame;
992         struct fimc_fmt *s_fmt = NULL;
993         int ret, i;
994
995         if (vb2_is_busy(&fimc->vid_cap.vbq))
996                 return -EBUSY;
997
998         /* Pre-configure format at camera interface input, for JPEG only */
999         if (fimc_jpeg_fourcc(pix->pixelformat)) {
1000                 fimc_capture_try_format(ctx, &pix->width, &pix->height,
1001                                         NULL, &pix->pixelformat,
1002                                         FIMC_SD_PAD_SINK);
1003                 ctx->s_frame.f_width  = pix->width;
1004                 ctx->s_frame.f_height = pix->height;
1005         }
1006         /* Try the format at the scaler and the DMA output */
1007         ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
1008                                           NULL, &pix->pixelformat,
1009                                           FIMC_SD_PAD_SOURCE);
1010         if (!ff->fmt)
1011                 return -EINVAL;
1012
1013         /* Update RGB Alpha control state and value range */
1014         fimc_alpha_ctrl_update(ctx);
1015
1016         /* Try to match format at the host and the sensor */
1017         if (!fimc->vid_cap.user_subdev_api) {
1018                 mf->code   = ff->fmt->mbus_code;
1019                 mf->width  = pix->width;
1020                 mf->height = pix->height;
1021                 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
1022                 if (ret)
1023                         return ret;
1024
1025                 pix->width  = mf->width;
1026                 pix->height = mf->height;
1027         }
1028
1029         fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
1030
1031         if (ff->fmt->flags & FMT_FLAGS_COMPRESSED) {
1032                 ret = fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
1033                                         pix->plane_fmt, ff->fmt->memplanes,
1034                                         true);
1035                 if (ret < 0)
1036                         return ret;
1037         }
1038
1039         for (i = 0; i < ff->fmt->memplanes; i++) {
1040                 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
1041                 ff->payload[i] = pix->plane_fmt[i].sizeimage;
1042         }
1043
1044         set_frame_bounds(ff, pix->width, pix->height);
1045         /* Reset the composition rectangle if not yet configured */
1046         if (!(ctx->state & FIMC_COMPOSE))
1047                 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1048
1049         fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
1050
1051         /* Reset cropping and set format at the camera interface input */
1052         if (!fimc->vid_cap.user_subdev_api) {
1053                 ctx->s_frame.fmt = s_fmt;
1054                 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1055                 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1056         }
1057
1058         return ret;
1059 }
1060
1061 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1062                                  struct v4l2_format *f)
1063 {
1064         struct fimc_dev *fimc = video_drvdata(file);
1065         int ret;
1066
1067         fimc_md_graph_lock(fimc);
1068         mutex_lock(&fimc->lock);
1069         /*
1070          * The graph is walked within __fimc_capture_set_format() to set
1071          * the format at subdevs thus the graph mutex needs to be held at
1072          * this point and acquired before the video mutex, to avoid  AB-BA
1073          * deadlock when fimc_md_link_notify() is called by other thread.
1074          * Ideally the graph walking and setting format at the whole pipeline
1075          * should be removed from this driver and handled in userspace only.
1076          */
1077         ret = __fimc_capture_set_format(fimc, f);
1078
1079         mutex_unlock(&fimc->lock);
1080         fimc_md_graph_unlock(fimc);
1081         return ret;
1082 }
1083
1084 static int fimc_cap_enum_input(struct file *file, void *priv,
1085                                struct v4l2_input *i)
1086 {
1087         struct fimc_dev *fimc = video_drvdata(file);
1088         struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
1089
1090         if (i->index != 0)
1091                 return -EINVAL;
1092
1093         i->type = V4L2_INPUT_TYPE_CAMERA;
1094         if (sd)
1095                 strlcpy(i->name, sd->name, sizeof(i->name));
1096         return 0;
1097 }
1098
1099 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
1100 {
1101         return i == 0 ? i : -EINVAL;
1102 }
1103
1104 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
1105 {
1106         *i = 0;
1107         return 0;
1108 }
1109
1110 /**
1111  * fimc_pipeline_validate - check for formats inconsistencies
1112  *                          between source and sink pad of each link
1113  *
1114  * Return 0 if all formats match or -EPIPE otherwise.
1115  */
1116 static int fimc_pipeline_validate(struct fimc_dev *fimc)
1117 {
1118         struct v4l2_subdev_format sink_fmt, src_fmt;
1119         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
1120         struct v4l2_subdev *sd;
1121         struct media_pad *pad;
1122         int ret;
1123
1124         /* Start with the video capture node pad */
1125         pad = media_entity_remote_source(&vid_cap->vd_pad);
1126         if (pad == NULL)
1127                 return -EPIPE;
1128         /* FIMC.{N} subdevice */
1129         sd = media_entity_to_v4l2_subdev(pad->entity);
1130
1131         while (1) {
1132                 /* Retrieve format at the sink pad */
1133                 pad = &sd->entity.pads[0];
1134                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
1135                         break;
1136                 /* Don't call FIMC subdev operation to avoid nested locking */
1137                 if (sd == &fimc->vid_cap.subdev) {
1138                         struct fimc_frame *ff = &vid_cap->ctx->s_frame;
1139                         sink_fmt.format.width = ff->f_width;
1140                         sink_fmt.format.height = ff->f_height;
1141                         sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1142                 } else {
1143                         sink_fmt.pad = pad->index;
1144                         sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1145                         ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1146                         if (ret < 0 && ret != -ENOIOCTLCMD)
1147                                 return -EPIPE;
1148                 }
1149                 /* Retrieve format at the source pad */
1150                 pad = media_entity_remote_source(pad);
1151                 if (pad == NULL ||
1152                     media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1153                         break;
1154
1155                 sd = media_entity_to_v4l2_subdev(pad->entity);
1156                 src_fmt.pad = pad->index;
1157                 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1158                 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1159                 if (ret < 0 && ret != -ENOIOCTLCMD)
1160                         return -EPIPE;
1161
1162                 if (src_fmt.format.width != sink_fmt.format.width ||
1163                     src_fmt.format.height != sink_fmt.format.height ||
1164                     src_fmt.format.code != sink_fmt.format.code)
1165                         return -EPIPE;
1166
1167                 if (sd == fimc->pipeline.subdevs[IDX_SENSOR] &&
1168                     fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1169                         struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
1170                         struct fimc_frame *frame = &vid_cap->ctx->d_frame;
1171                         unsigned int i;
1172
1173                         ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1174                                                          frame->fmt->memplanes,
1175                                                          false);
1176                         if (ret < 0)
1177                                 return -EPIPE;
1178
1179                         for (i = 0; i < frame->fmt->memplanes; i++)
1180                                 if (frame->payload[i] < plane_fmt[i].sizeimage)
1181                                         return -EPIPE;
1182                 }
1183         }
1184         return 0;
1185 }
1186
1187 static int fimc_cap_streamon(struct file *file, void *priv,
1188                              enum v4l2_buf_type type)
1189 {
1190         struct fimc_dev *fimc = video_drvdata(file);
1191         struct fimc_pipeline *p = &fimc->pipeline;
1192         struct fimc_vid_cap *vc = &fimc->vid_cap;
1193         struct media_entity *entity = &vc->vfd.entity;
1194         int ret;
1195
1196         if (fimc_capture_active(fimc))
1197                 return -EBUSY;
1198
1199         ret = media_entity_pipeline_start(entity, p->m_pipeline);
1200         if (ret < 0)
1201                 return ret;
1202
1203         if (vc->user_subdev_api) {
1204                 ret = fimc_pipeline_validate(fimc);
1205                 if (ret < 0)
1206                         goto err_p_stop;
1207         }
1208
1209         ret = vb2_ioctl_streamon(file, priv, type);
1210         if (!ret)
1211                 return ret;
1212
1213 err_p_stop:
1214         media_entity_pipeline_stop(entity);
1215         return ret;
1216 }
1217
1218 static int fimc_cap_streamoff(struct file *file, void *priv,
1219                             enum v4l2_buf_type type)
1220 {
1221         struct fimc_dev *fimc = video_drvdata(file);
1222         int ret;
1223
1224         ret = vb2_ioctl_streamoff(file, priv, type);
1225
1226         if (ret == 0)
1227                 media_entity_pipeline_stop(&fimc->vid_cap.vfd.entity);
1228
1229         return ret;
1230 }
1231
1232 static int fimc_cap_reqbufs(struct file *file, void *priv,
1233                             struct v4l2_requestbuffers *reqbufs)
1234 {
1235         struct fimc_dev *fimc = video_drvdata(file);
1236         int ret;
1237
1238         ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
1239
1240         if (!ret)
1241                 fimc->vid_cap.reqbufs_count = reqbufs->count;
1242
1243         return ret;
1244 }
1245
1246 static int fimc_cap_g_selection(struct file *file, void *fh,
1247                                 struct v4l2_selection *s)
1248 {
1249         struct fimc_dev *fimc = video_drvdata(file);
1250         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1251         struct fimc_frame *f = &ctx->s_frame;
1252
1253         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1254                 return -EINVAL;
1255
1256         switch (s->target) {
1257         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1258         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1259                 f = &ctx->d_frame;
1260         case V4L2_SEL_TGT_CROP_BOUNDS:
1261         case V4L2_SEL_TGT_CROP_DEFAULT:
1262                 s->r.left = 0;
1263                 s->r.top = 0;
1264                 s->r.width = f->o_width;
1265                 s->r.height = f->o_height;
1266                 return 0;
1267
1268         case V4L2_SEL_TGT_COMPOSE:
1269                 f = &ctx->d_frame;
1270         case V4L2_SEL_TGT_CROP:
1271                 s->r.left = f->offs_h;
1272                 s->r.top = f->offs_v;
1273                 s->r.width = f->width;
1274                 s->r.height = f->height;
1275                 return 0;
1276         }
1277
1278         return -EINVAL;
1279 }
1280
1281 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1282 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1283 {
1284         if (a->left < b->left || a->top < b->top)
1285                 return 0;
1286         if (a->left + a->width > b->left + b->width)
1287                 return 0;
1288         if (a->top + a->height > b->top + b->height)
1289                 return 0;
1290
1291         return 1;
1292 }
1293
1294 static int fimc_cap_s_selection(struct file *file, void *fh,
1295                                 struct v4l2_selection *s)
1296 {
1297         struct fimc_dev *fimc = video_drvdata(file);
1298         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1299         struct v4l2_rect rect = s->r;
1300         struct fimc_frame *f;
1301         unsigned long flags;
1302
1303         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1304                 return -EINVAL;
1305
1306         if (s->target == V4L2_SEL_TGT_COMPOSE)
1307                 f = &ctx->d_frame;
1308         else if (s->target == V4L2_SEL_TGT_CROP)
1309                 f = &ctx->s_frame;
1310         else
1311                 return -EINVAL;
1312
1313         fimc_capture_try_selection(ctx, &rect, s->target);
1314
1315         if (s->flags & V4L2_SEL_FLAG_LE &&
1316             !enclosed_rectangle(&rect, &s->r))
1317                 return -ERANGE;
1318
1319         if (s->flags & V4L2_SEL_FLAG_GE &&
1320             !enclosed_rectangle(&s->r, &rect))
1321                 return -ERANGE;
1322
1323         s->r = rect;
1324         spin_lock_irqsave(&fimc->slock, flags);
1325         set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1326                        s->r.height);
1327         spin_unlock_irqrestore(&fimc->slock, flags);
1328
1329         set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1330         return 0;
1331 }
1332
1333 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1334         .vidioc_querycap                = fimc_vidioc_querycap_capture,
1335
1336         .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1337         .vidioc_try_fmt_vid_cap_mplane  = fimc_cap_try_fmt_mplane,
1338         .vidioc_s_fmt_vid_cap_mplane    = fimc_cap_s_fmt_mplane,
1339         .vidioc_g_fmt_vid_cap_mplane    = fimc_cap_g_fmt_mplane,
1340
1341         .vidioc_reqbufs                 = fimc_cap_reqbufs,
1342         .vidioc_querybuf                = vb2_ioctl_querybuf,
1343         .vidioc_qbuf                    = vb2_ioctl_qbuf,
1344         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1345         .vidioc_expbuf                  = vb2_ioctl_expbuf,
1346         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
1347         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
1348
1349         .vidioc_streamon                = fimc_cap_streamon,
1350         .vidioc_streamoff               = fimc_cap_streamoff,
1351
1352         .vidioc_g_selection             = fimc_cap_g_selection,
1353         .vidioc_s_selection             = fimc_cap_s_selection,
1354
1355         .vidioc_enum_input              = fimc_cap_enum_input,
1356         .vidioc_s_input                 = fimc_cap_s_input,
1357         .vidioc_g_input                 = fimc_cap_g_input,
1358 };
1359
1360 /* Capture subdev media entity operations */
1361 static int fimc_link_setup(struct media_entity *entity,
1362                            const struct media_pad *local,
1363                            const struct media_pad *remote, u32 flags)
1364 {
1365         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1366         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1367
1368         if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1369                 return -EINVAL;
1370
1371         if (WARN_ON(fimc == NULL))
1372                 return 0;
1373
1374         dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1375             local->entity->name, remote->entity->name, flags,
1376             fimc->vid_cap.input);
1377
1378         if (flags & MEDIA_LNK_FL_ENABLED) {
1379                 if (fimc->vid_cap.input != 0)
1380                         return -EBUSY;
1381                 fimc->vid_cap.input = sd->grp_id;
1382                 return 0;
1383         }
1384
1385         fimc->vid_cap.input = 0;
1386         return 0;
1387 }
1388
1389 static const struct media_entity_operations fimc_sd_media_ops = {
1390         .link_setup = fimc_link_setup,
1391 };
1392
1393 /**
1394  * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1395  * @sd: pointer to a subdev generating the notification
1396  * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1397  * @arg: pointer to an u32 type integer that stores the frame payload value
1398  *
1399  * The End Of Frame notification sent by sensor subdev in its still capture
1400  * mode. If there is only a single VSYNC generated by the sensor at the
1401  * beginning of a frame transmission, FIMC does not issue the LastIrq
1402  * (end of frame) interrupt. And this notification is used to complete the
1403  * frame capture and returning a buffer to user-space. Subdev drivers should
1404  * call this notification from their last 'End of frame capture' interrupt.
1405  */
1406 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1407                         void *arg)
1408 {
1409         struct fimc_sensor_info *sensor;
1410         struct fimc_vid_buffer *buf;
1411         struct fimc_md *fmd;
1412         struct fimc_dev *fimc;
1413         unsigned long flags;
1414
1415         if (sd == NULL)
1416                 return;
1417
1418         sensor = v4l2_get_subdev_hostdata(sd);
1419         fmd = entity_to_fimc_mdev(&sd->entity);
1420
1421         spin_lock_irqsave(&fmd->slock, flags);
1422         fimc = sensor ? sensor->host : NULL;
1423
1424         if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1425             test_bit(ST_CAPT_PEND, &fimc->state)) {
1426                 unsigned long irq_flags;
1427                 spin_lock_irqsave(&fimc->slock, irq_flags);
1428                 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1429                         buf = list_entry(fimc->vid_cap.active_buf_q.next,
1430                                          struct fimc_vid_buffer, list);
1431                         vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1432                 }
1433                 fimc_capture_irq_handler(fimc, 1);
1434                 fimc_deactivate_capture(fimc);
1435                 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1436         }
1437         spin_unlock_irqrestore(&fmd->slock, flags);
1438 }
1439
1440 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1441                                       struct v4l2_subdev_fh *fh,
1442                                       struct v4l2_subdev_mbus_code_enum *code)
1443 {
1444         struct fimc_fmt *fmt;
1445
1446         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1447         if (!fmt)
1448                 return -EINVAL;
1449         code->code = fmt->mbus_code;
1450         return 0;
1451 }
1452
1453 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1454                                struct v4l2_subdev_fh *fh,
1455                                struct v4l2_subdev_format *fmt)
1456 {
1457         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1458         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1459         struct v4l2_mbus_framefmt *mf;
1460         struct fimc_frame *ff;
1461
1462         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1463                 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1464                 fmt->format = *mf;
1465                 return 0;
1466         }
1467         mf = &fmt->format;
1468         mf->colorspace = V4L2_COLORSPACE_JPEG;
1469         ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1470
1471         mutex_lock(&fimc->lock);
1472         /* The pixel code is same on both input and output pad */
1473         if (!WARN_ON(ctx->s_frame.fmt == NULL))
1474                 mf->code = ctx->s_frame.fmt->mbus_code;
1475         mf->width  = ff->f_width;
1476         mf->height = ff->f_height;
1477         mutex_unlock(&fimc->lock);
1478
1479         return 0;
1480 }
1481
1482 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1483                                struct v4l2_subdev_fh *fh,
1484                                struct v4l2_subdev_format *fmt)
1485 {
1486         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1487         struct v4l2_mbus_framefmt *mf = &fmt->format;
1488         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1489         struct fimc_frame *ff;
1490         struct fimc_fmt *ffmt;
1491
1492         dbg("pad%d: code: 0x%x, %dx%d",
1493             fmt->pad, mf->code, mf->width, mf->height);
1494
1495         if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1496             vb2_is_busy(&fimc->vid_cap.vbq))
1497                 return -EBUSY;
1498
1499         mutex_lock(&fimc->lock);
1500         ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1501                                        &mf->code, NULL, fmt->pad);
1502         mutex_unlock(&fimc->lock);
1503         mf->colorspace = V4L2_COLORSPACE_JPEG;
1504
1505         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1506                 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1507                 *mf = fmt->format;
1508                 return 0;
1509         }
1510         /* There must be a bug in the driver if this happens */
1511         if (WARN_ON(ffmt == NULL))
1512                 return -EINVAL;
1513
1514         /* Update RGB Alpha control state and value range */
1515         fimc_alpha_ctrl_update(ctx);
1516
1517         fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1518
1519         ff = fmt->pad == FIMC_SD_PAD_SINK ?
1520                 &ctx->s_frame : &ctx->d_frame;
1521
1522         mutex_lock(&fimc->lock);
1523         set_frame_bounds(ff, mf->width, mf->height);
1524         fimc->vid_cap.mf = *mf;
1525         ff->fmt = ffmt;
1526
1527         /* Reset the crop rectangle if required. */
1528         if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1529                 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1530
1531         if (fmt->pad == FIMC_SD_PAD_SINK)
1532                 ctx->state &= ~FIMC_COMPOSE;
1533         mutex_unlock(&fimc->lock);
1534         return 0;
1535 }
1536
1537 static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1538                                      struct v4l2_subdev_fh *fh,
1539                                      struct v4l2_subdev_selection *sel)
1540 {
1541         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1542         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1543         struct fimc_frame *f = &ctx->s_frame;
1544         struct v4l2_rect *r = &sel->r;
1545         struct v4l2_rect *try_sel;
1546
1547         if (sel->pad != FIMC_SD_PAD_SINK)
1548                 return -EINVAL;
1549
1550         mutex_lock(&fimc->lock);
1551
1552         switch (sel->target) {
1553         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1554                 f = &ctx->d_frame;
1555         case V4L2_SEL_TGT_CROP_BOUNDS:
1556                 r->width = f->o_width;
1557                 r->height = f->o_height;
1558                 r->left = 0;
1559                 r->top = 0;
1560                 mutex_unlock(&fimc->lock);
1561                 return 0;
1562
1563         case V4L2_SEL_TGT_CROP:
1564                 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1565                 break;
1566         case V4L2_SEL_TGT_COMPOSE:
1567                 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1568                 f = &ctx->d_frame;
1569                 break;
1570         default:
1571                 mutex_unlock(&fimc->lock);
1572                 return -EINVAL;
1573         }
1574
1575         if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1576                 sel->r = *try_sel;
1577         } else {
1578                 r->left = f->offs_h;
1579                 r->top = f->offs_v;
1580                 r->width = f->width;
1581                 r->height = f->height;
1582         }
1583
1584         dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1585             sel->pad, r->left, r->top, r->width, r->height,
1586             f->f_width, f->f_height);
1587
1588         mutex_unlock(&fimc->lock);
1589         return 0;
1590 }
1591
1592 static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1593                                      struct v4l2_subdev_fh *fh,
1594                                      struct v4l2_subdev_selection *sel)
1595 {
1596         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1597         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1598         struct fimc_frame *f = &ctx->s_frame;
1599         struct v4l2_rect *r = &sel->r;
1600         struct v4l2_rect *try_sel;
1601         unsigned long flags;
1602
1603         if (sel->pad != FIMC_SD_PAD_SINK)
1604                 return -EINVAL;
1605
1606         mutex_lock(&fimc->lock);
1607         fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1608
1609         switch (sel->target) {
1610         case V4L2_SEL_TGT_CROP:
1611                 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1612                 break;
1613         case V4L2_SEL_TGT_COMPOSE:
1614                 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1615                 f = &ctx->d_frame;
1616                 break;
1617         default:
1618                 mutex_unlock(&fimc->lock);
1619                 return -EINVAL;
1620         }
1621
1622         if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1623                 *try_sel = sel->r;
1624         } else {
1625                 spin_lock_irqsave(&fimc->slock, flags);
1626                 set_frame_crop(f, r->left, r->top, r->width, r->height);
1627                 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1628                 if (sel->target == V4L2_SEL_TGT_COMPOSE)
1629                         ctx->state |= FIMC_COMPOSE;
1630                 spin_unlock_irqrestore(&fimc->slock, flags);
1631         }
1632
1633         dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1634             r->width, r->height);
1635
1636         mutex_unlock(&fimc->lock);
1637         return 0;
1638 }
1639
1640 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1641         .enum_mbus_code = fimc_subdev_enum_mbus_code,
1642         .get_selection = fimc_subdev_get_selection,
1643         .set_selection = fimc_subdev_set_selection,
1644         .get_fmt = fimc_subdev_get_fmt,
1645         .set_fmt = fimc_subdev_set_fmt,
1646 };
1647
1648 static struct v4l2_subdev_ops fimc_subdev_ops = {
1649         .pad = &fimc_subdev_pad_ops,
1650 };
1651
1652 /* Set default format at the sensor and host interface */
1653 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1654 {
1655         struct v4l2_format fmt = {
1656                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1657                 .fmt.pix_mp = {
1658                         .width          = 640,
1659                         .height         = 480,
1660                         .pixelformat    = V4L2_PIX_FMT_YUYV,
1661                         .field          = V4L2_FIELD_NONE,
1662                         .colorspace     = V4L2_COLORSPACE_JPEG,
1663                 },
1664         };
1665
1666         return __fimc_capture_set_format(fimc, &fmt);
1667 }
1668
1669 /* fimc->lock must be already initialized */
1670 static int fimc_register_capture_device(struct fimc_dev *fimc,
1671                                  struct v4l2_device *v4l2_dev)
1672 {
1673         struct video_device *vfd = &fimc->vid_cap.vfd;
1674         struct vb2_queue *q = &fimc->vid_cap.vbq;
1675         struct fimc_ctx *ctx;
1676         struct fimc_vid_cap *vid_cap;
1677         int ret = -ENOMEM;
1678
1679         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1680         if (!ctx)
1681                 return -ENOMEM;
1682
1683         ctx->fimc_dev    = fimc;
1684         ctx->in_path     = FIMC_IO_CAMERA;
1685         ctx->out_path    = FIMC_IO_DMA;
1686         ctx->state       = FIMC_CTX_CAP;
1687         ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1688         ctx->d_frame.fmt = ctx->s_frame.fmt;
1689
1690         memset(vfd, 0, sizeof(*vfd));
1691         snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1692
1693         vfd->fops       = &fimc_capture_fops;
1694         vfd->ioctl_ops  = &fimc_capture_ioctl_ops;
1695         vfd->v4l2_dev   = v4l2_dev;
1696         vfd->minor      = -1;
1697         vfd->release    = video_device_release_empty;
1698         vfd->queue      = q;
1699         vfd->lock       = &fimc->lock;
1700
1701         video_set_drvdata(vfd, fimc);
1702         vid_cap = &fimc->vid_cap;
1703         vid_cap->active_buf_cnt = 0;
1704         vid_cap->reqbufs_count = 0;
1705         vid_cap->ctx = ctx;
1706
1707         INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1708         INIT_LIST_HEAD(&vid_cap->active_buf_q);
1709
1710         memset(q, 0, sizeof(*q));
1711         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1712         q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1713         q->drv_priv = ctx;
1714         q->ops = &fimc_capture_qops;
1715         q->mem_ops = &vb2_dma_contig_memops;
1716         q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1717         q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1718         q->lock = &fimc->lock;
1719
1720         ret = vb2_queue_init(q);
1721         if (ret)
1722                 goto err_ent;
1723
1724         vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1725         ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
1726         if (ret)
1727                 goto err_ent;
1728         /*
1729          * For proper order of acquiring/releasing the video
1730          * and the graph mutex.
1731          */
1732         v4l2_disable_ioctl_locking(vfd, VIDIOC_TRY_FMT);
1733         v4l2_disable_ioctl_locking(vfd, VIDIOC_S_FMT);
1734
1735         ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1736         if (ret)
1737                 goto err_vd;
1738
1739         v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1740                   vfd->name, video_device_node_name(vfd));
1741
1742         vfd->ctrl_handler = &ctx->ctrls.handler;
1743         return 0;
1744
1745 err_vd:
1746         media_entity_cleanup(&vfd->entity);
1747 err_ent:
1748         kfree(ctx);
1749         return ret;
1750 }
1751
1752 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1753 {
1754         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1755         int ret;
1756
1757         if (fimc == NULL)
1758                 return -ENXIO;
1759
1760         ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1761         if (ret)
1762                 return ret;
1763
1764         fimc->pipeline_ops = v4l2_get_subdev_hostdata(sd);
1765
1766         ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1767         if (ret) {
1768                 fimc_unregister_m2m_device(fimc);
1769                 fimc->pipeline_ops = NULL;
1770         }
1771
1772         return ret;
1773 }
1774
1775 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1776 {
1777         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1778
1779         if (fimc == NULL)
1780                 return;
1781
1782         fimc_unregister_m2m_device(fimc);
1783
1784         if (video_is_registered(&fimc->vid_cap.vfd)) {
1785                 video_unregister_device(&fimc->vid_cap.vfd);
1786                 media_entity_cleanup(&fimc->vid_cap.vfd.entity);
1787                 fimc->pipeline_ops = NULL;
1788         }
1789         kfree(fimc->vid_cap.ctx);
1790         fimc->vid_cap.ctx = NULL;
1791 }
1792
1793 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1794         .registered = fimc_capture_subdev_registered,
1795         .unregistered = fimc_capture_subdev_unregistered,
1796 };
1797
1798 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1799 {
1800         struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1801         int ret;
1802
1803         v4l2_subdev_init(sd, &fimc_subdev_ops);
1804         sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1805         snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1806
1807         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1808         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1809         ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1810                                 fimc->vid_cap.sd_pads, 0);
1811         if (ret)
1812                 return ret;
1813
1814         sd->entity.ops = &fimc_sd_media_ops;
1815         sd->internal_ops = &fimc_capture_sd_internal_ops;
1816         v4l2_set_subdevdata(sd, fimc);
1817         return 0;
1818 }
1819
1820 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1821 {
1822         struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1823
1824         v4l2_device_unregister_subdev(sd);
1825         media_entity_cleanup(&sd->entity);
1826         v4l2_set_subdevdata(sd, NULL);
1827 }