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