Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / s5p-fimc / fimc-capture.c
1 /*
2  * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
3  *
4  * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
5  * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
22
23 #include <linux/videodev2.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-mem2mem.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
29
30 #include "fimc-mdevice.h"
31 #include "fimc-core.h"
32
33 static int fimc_init_capture(struct fimc_dev *fimc)
34 {
35         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
36         struct fimc_sensor_info *sensor;
37         unsigned long flags;
38         int ret = 0;
39
40         if (fimc->pipeline.sensor == NULL || ctx == NULL)
41                 return -ENXIO;
42         if (ctx->s_frame.fmt == NULL)
43                 return -EINVAL;
44
45         sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor);
46
47         spin_lock_irqsave(&fimc->slock, flags);
48         fimc_prepare_dma_offset(ctx, &ctx->d_frame);
49         fimc_set_yuv_order(ctx);
50
51         fimc_hw_set_camera_polarity(fimc, sensor->pdata);
52         fimc_hw_set_camera_type(fimc, sensor->pdata);
53         fimc_hw_set_camera_source(fimc, sensor->pdata);
54         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
55
56         ret = fimc_set_scaler_info(ctx);
57         if (!ret) {
58                 fimc_hw_set_input_path(ctx);
59                 fimc_hw_set_prescaler(ctx);
60                 fimc_hw_set_mainscaler(ctx);
61                 fimc_hw_set_target_format(ctx);
62                 fimc_hw_set_rotation(ctx);
63                 fimc_hw_set_effect(ctx, false);
64                 fimc_hw_set_output_path(ctx);
65                 fimc_hw_set_out_dma(ctx);
66                 if (fimc->variant->has_alpha)
67                         fimc_hw_set_rgb_alpha(ctx);
68                 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
69         }
70         spin_unlock_irqrestore(&fimc->slock, flags);
71         return ret;
72 }
73
74 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
75 {
76         struct fimc_vid_cap *cap = &fimc->vid_cap;
77         struct fimc_vid_buffer *buf;
78         unsigned long flags;
79         bool streaming;
80
81         spin_lock_irqsave(&fimc->slock, flags);
82         streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
83
84         fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
85                          1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
86         if (!suspend)
87                 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
88
89         /* Release unused buffers */
90         while (!suspend && !list_empty(&cap->pending_buf_q)) {
91                 buf = fimc_pending_queue_pop(cap);
92                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
93         }
94         /* If suspending put unused buffers onto pending queue */
95         while (!list_empty(&cap->active_buf_q)) {
96                 buf = fimc_active_queue_pop(cap);
97                 if (suspend)
98                         fimc_pending_queue_add(cap, buf);
99                 else
100                         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
101         }
102         set_bit(ST_CAPT_SUSPENDED, &fimc->state);
103
104         fimc_hw_reset(fimc);
105         cap->buf_index = 0;
106
107         spin_unlock_irqrestore(&fimc->slock, flags);
108
109         if (streaming)
110                 return fimc_pipeline_s_stream(fimc, 0);
111         else
112                 return 0;
113 }
114
115 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
116 {
117         unsigned long flags;
118
119         if (!fimc_capture_active(fimc))
120                 return 0;
121
122         spin_lock_irqsave(&fimc->slock, flags);
123         set_bit(ST_CAPT_SHUT, &fimc->state);
124         fimc_deactivate_capture(fimc);
125         spin_unlock_irqrestore(&fimc->slock, flags);
126
127         wait_event_timeout(fimc->irq_queue,
128                            !test_bit(ST_CAPT_SHUT, &fimc->state),
129                            (2*HZ/10)); /* 200 ms */
130
131         return fimc_capture_state_cleanup(fimc, suspend);
132 }
133
134 /**
135  * fimc_capture_config_update - apply the camera interface configuration
136  *
137  * To be called from within the interrupt handler with fimc.slock
138  * spinlock held. It updates the camera pixel crop, rotation and
139  * image flip in H/W.
140  */
141 int fimc_capture_config_update(struct fimc_ctx *ctx)
142 {
143         struct fimc_dev *fimc = ctx->fimc_dev;
144         int ret;
145
146         if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
147                 return 0;
148
149         spin_lock(&ctx->slock);
150         fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
151         ret = fimc_set_scaler_info(ctx);
152         if (ret == 0) {
153                 fimc_hw_set_prescaler(ctx);
154                 fimc_hw_set_mainscaler(ctx);
155                 fimc_hw_set_target_format(ctx);
156                 fimc_hw_set_rotation(ctx);
157                 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
158                 fimc_hw_set_out_dma(ctx);
159                 if (fimc->variant->has_alpha)
160                         fimc_hw_set_rgb_alpha(ctx);
161                 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
162         }
163         spin_unlock(&ctx->slock);
164         return ret;
165 }
166
167 static int start_streaming(struct vb2_queue *q, unsigned int count)
168 {
169         struct fimc_ctx *ctx = q->drv_priv;
170         struct fimc_dev *fimc = ctx->fimc_dev;
171         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
172         int min_bufs;
173         int ret;
174
175         vid_cap->frame_count = 0;
176
177         ret = fimc_init_capture(fimc);
178         if (ret)
179                 goto error;
180
181         set_bit(ST_CAPT_PEND, &fimc->state);
182
183         min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
184
185         if (vid_cap->active_buf_cnt >= min_bufs &&
186             !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
187                 fimc_activate_capture(ctx);
188
189                 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
190                         fimc_pipeline_s_stream(fimc, 1);
191         }
192
193         return 0;
194 error:
195         fimc_capture_state_cleanup(fimc, false);
196         return ret;
197 }
198
199 static int stop_streaming(struct vb2_queue *q)
200 {
201         struct fimc_ctx *ctx = q->drv_priv;
202         struct fimc_dev *fimc = ctx->fimc_dev;
203
204         if (!fimc_capture_active(fimc))
205                 return -EINVAL;
206
207         return fimc_stop_capture(fimc, false);
208 }
209
210 int fimc_capture_suspend(struct fimc_dev *fimc)
211 {
212         bool suspend = fimc_capture_busy(fimc);
213
214         int ret = fimc_stop_capture(fimc, suspend);
215         if (ret)
216                 return ret;
217         return fimc_pipeline_shutdown(fimc);
218 }
219
220 static void buffer_queue(struct vb2_buffer *vb);
221
222 int fimc_capture_resume(struct fimc_dev *fimc)
223 {
224         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
225         struct fimc_vid_buffer *buf;
226         int i;
227
228         if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
229                 return 0;
230
231         INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
232         vid_cap->buf_index = 0;
233         fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity,
234                                  false);
235         fimc_init_capture(fimc);
236
237         clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
238
239         for (i = 0; i < vid_cap->reqbufs_count; i++) {
240                 if (list_empty(&vid_cap->pending_buf_q))
241                         break;
242                 buf = fimc_pending_queue_pop(vid_cap);
243                 buffer_queue(&buf->vb);
244         }
245         return 0;
246
247 }
248
249 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
250                        unsigned int *num_buffers, unsigned int *num_planes,
251                        unsigned int sizes[], void *allocators[])
252 {
253         const struct v4l2_pix_format_mplane *pixm = NULL;
254         struct fimc_ctx *ctx = vq->drv_priv;
255         struct fimc_frame *frame = &ctx->d_frame;
256         struct fimc_fmt *fmt = frame->fmt;
257         unsigned long wh;
258         int i;
259
260         if (pfmt) {
261                 pixm = &pfmt->fmt.pix_mp;
262                 fmt = fimc_find_format(&pixm->pixelformat, NULL,
263                                        FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
264                 wh = pixm->width * pixm->height;
265         } else {
266                 wh = frame->f_width * frame->f_height;
267         }
268
269         if (fmt == NULL)
270                 return -EINVAL;
271
272         *num_planes = fmt->memplanes;
273
274         for (i = 0; i < fmt->memplanes; i++) {
275                 unsigned int size = (wh * fmt->depth[i]) / 8;
276                 if (pixm)
277                         sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
278                 else
279                         sizes[i] = size;
280                 allocators[i] = ctx->fimc_dev->alloc_ctx;
281         }
282
283         return 0;
284 }
285
286 static int buffer_prepare(struct vb2_buffer *vb)
287 {
288         struct vb2_queue *vq = vb->vb2_queue;
289         struct fimc_ctx *ctx = vq->drv_priv;
290         int i;
291
292         if (ctx->d_frame.fmt == NULL)
293                 return -EINVAL;
294
295         for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
296                 unsigned long size = ctx->d_frame.payload[i];
297
298                 if (vb2_plane_size(vb, i) < size) {
299                         v4l2_err(ctx->fimc_dev->vid_cap.vfd,
300                                  "User buffer too small (%ld < %ld)\n",
301                                  vb2_plane_size(vb, i), size);
302                         return -EINVAL;
303                 }
304                 vb2_set_plane_payload(vb, i, size);
305         }
306
307         return 0;
308 }
309
310 static void buffer_queue(struct vb2_buffer *vb)
311 {
312         struct fimc_vid_buffer *buf
313                 = container_of(vb, struct fimc_vid_buffer, vb);
314         struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
315         struct fimc_dev *fimc = ctx->fimc_dev;
316         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
317         unsigned long flags;
318         int min_bufs;
319
320         spin_lock_irqsave(&fimc->slock, flags);
321         fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
322
323         if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
324             !test_bit(ST_CAPT_STREAM, &fimc->state) &&
325             vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
326                 /* Setup the buffer directly for processing. */
327                 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
328                                 vid_cap->buf_index;
329
330                 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
331                 buf->index = vid_cap->buf_index;
332                 fimc_active_queue_add(vid_cap, buf);
333
334                 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
335                         vid_cap->buf_index = 0;
336         } else {
337                 fimc_pending_queue_add(vid_cap, buf);
338         }
339
340         min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
341
342
343         if (vb2_is_streaming(&vid_cap->vbq) &&
344             vid_cap->active_buf_cnt >= min_bufs &&
345             !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
346                 fimc_activate_capture(ctx);
347                 spin_unlock_irqrestore(&fimc->slock, flags);
348
349                 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
350                         fimc_pipeline_s_stream(fimc, 1);
351                 return;
352         }
353         spin_unlock_irqrestore(&fimc->slock, flags);
354 }
355
356 static void fimc_lock(struct vb2_queue *vq)
357 {
358         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
359         mutex_lock(&ctx->fimc_dev->lock);
360 }
361
362 static void fimc_unlock(struct vb2_queue *vq)
363 {
364         struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
365         mutex_unlock(&ctx->fimc_dev->lock);
366 }
367
368 static struct vb2_ops fimc_capture_qops = {
369         .queue_setup            = queue_setup,
370         .buf_prepare            = buffer_prepare,
371         .buf_queue              = buffer_queue,
372         .wait_prepare           = fimc_unlock,
373         .wait_finish            = fimc_lock,
374         .start_streaming        = start_streaming,
375         .stop_streaming         = stop_streaming,
376 };
377
378 /**
379  * fimc_capture_ctrls_create - initialize the control handler
380  * Initialize the capture video node control handler and fill it
381  * with the FIMC controls. Inherit any sensor's controls if the
382  * 'user_subdev_api' flag is false (default behaviour).
383  * This function need to be called with the graph mutex held.
384  */
385 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
386 {
387         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
388         int ret;
389
390         if (WARN_ON(vid_cap->ctx == NULL))
391                 return -ENXIO;
392         if (vid_cap->ctx->ctrls_rdy)
393                 return 0;
394
395         ret = fimc_ctrls_create(vid_cap->ctx);
396         if (ret || vid_cap->user_subdev_api)
397                 return ret;
398
399         return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
400                                     fimc->pipeline.sensor->ctrl_handler);
401 }
402
403 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
404
405 static int fimc_capture_open(struct file *file)
406 {
407         struct fimc_dev *fimc = video_drvdata(file);
408         int ret = v4l2_fh_open(file);
409
410         if (ret)
411                 return ret;
412
413         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
414
415         /* Return if the corresponding video mem2mem node is already opened. */
416         if (fimc_m2m_active(fimc))
417                 return -EBUSY;
418
419         set_bit(ST_CAPT_BUSY, &fimc->state);
420         pm_runtime_get_sync(&fimc->pdev->dev);
421
422         if (++fimc->vid_cap.refcnt == 1) {
423                 ret = fimc_pipeline_initialize(fimc,
424                                &fimc->vid_cap.vfd->entity, true);
425                 if (ret < 0) {
426                         dev_err(&fimc->pdev->dev,
427                                 "Video pipeline initialization failed\n");
428                         pm_runtime_put_sync(&fimc->pdev->dev);
429                         fimc->vid_cap.refcnt--;
430                         v4l2_fh_release(file);
431                         clear_bit(ST_CAPT_BUSY, &fimc->state);
432                         return ret;
433                 }
434                 ret = fimc_capture_ctrls_create(fimc);
435
436                 if (!ret && !fimc->vid_cap.user_subdev_api)
437                         ret = fimc_capture_set_default_format(fimc);
438         }
439         return ret;
440 }
441
442 static int fimc_capture_close(struct file *file)
443 {
444         struct fimc_dev *fimc = video_drvdata(file);
445
446         dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
447
448         if (--fimc->vid_cap.refcnt == 0) {
449                 clear_bit(ST_CAPT_BUSY, &fimc->state);
450                 fimc_stop_capture(fimc, false);
451                 fimc_pipeline_shutdown(fimc);
452                 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
453         }
454
455         pm_runtime_put(&fimc->pdev->dev);
456
457         if (fimc->vid_cap.refcnt == 0) {
458                 vb2_queue_release(&fimc->vid_cap.vbq);
459                 fimc_ctrls_delete(fimc->vid_cap.ctx);
460         }
461         return v4l2_fh_release(file);
462 }
463
464 static unsigned int fimc_capture_poll(struct file *file,
465                                       struct poll_table_struct *wait)
466 {
467         struct fimc_dev *fimc = video_drvdata(file);
468
469         return vb2_poll(&fimc->vid_cap.vbq, file, wait);
470 }
471
472 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
473 {
474         struct fimc_dev *fimc = video_drvdata(file);
475
476         return vb2_mmap(&fimc->vid_cap.vbq, vma);
477 }
478
479 static const struct v4l2_file_operations fimc_capture_fops = {
480         .owner          = THIS_MODULE,
481         .open           = fimc_capture_open,
482         .release        = fimc_capture_close,
483         .poll           = fimc_capture_poll,
484         .unlocked_ioctl = video_ioctl2,
485         .mmap           = fimc_capture_mmap,
486 };
487
488 /*
489  * Format and crop negotiation helpers
490  */
491
492 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
493                                                 u32 *width, u32 *height,
494                                                 u32 *code, u32 *fourcc, int pad)
495 {
496         bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
497         struct fimc_dev *fimc = ctx->fimc_dev;
498         struct samsung_fimc_variant *var = fimc->variant;
499         struct fimc_pix_limit *pl = var->pix_limit;
500         struct fimc_frame *dst = &ctx->d_frame;
501         u32 depth, min_w, max_w, min_h, align_h = 3;
502         u32 mask = FMT_FLAGS_CAM;
503         struct fimc_fmt *ffmt;
504
505         /* Color conversion from/to JPEG is not supported */
506         if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
507             fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
508                 *code = V4L2_MBUS_FMT_JPEG_1X8;
509
510         if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
511                 mask |= FMT_FLAGS_M2M;
512
513         ffmt = fimc_find_format(fourcc, code, mask, 0);
514         if (WARN_ON(!ffmt))
515                 return NULL;
516         if (code)
517                 *code = ffmt->mbus_code;
518         if (fourcc)
519                 *fourcc = ffmt->fourcc;
520
521         if (pad == FIMC_SD_PAD_SINK) {
522                 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
523                         pl->scaler_dis_w : pl->scaler_en_w;
524                 /* Apply the camera input interface pixel constraints */
525                 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
526                                       height, max_t(u32, *height, 32),
527                                       FIMC_CAMIF_MAX_HEIGHT,
528                                       fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
529                                       0);
530                 return ffmt;
531         }
532         /* Can't scale or crop in transparent (JPEG) transfer mode */
533         if (fimc_fmt_is_jpeg(ffmt->color)) {
534                 *width  = ctx->s_frame.f_width;
535                 *height = ctx->s_frame.f_height;
536                 return ffmt;
537         }
538         /* Apply the scaler and the output DMA constraints */
539         max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
540         min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
541         min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
542         if (var->min_vsize_align == 1 && !rotation)
543                 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
544
545         depth = fimc_get_format_depth(ffmt);
546         v4l_bound_align_image(width, min_w, max_w,
547                               ffs(var->min_out_pixsize) - 1,
548                               height, min_h, FIMC_CAMIF_MAX_HEIGHT,
549                               align_h,
550                               64/(ALIGN(depth, 8)));
551
552         dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
553             pad, code ? *code : 0, *width, *height,
554             dst->f_width, dst->f_height);
555
556         return ffmt;
557 }
558
559 static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
560                                   int pad)
561 {
562         bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
563         struct fimc_dev *fimc = ctx->fimc_dev;
564         struct samsung_fimc_variant *var = fimc->variant;
565         struct fimc_pix_limit *pl = var->pix_limit;
566         struct fimc_frame *sink = &ctx->s_frame;
567         u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
568         u32 align_sz = 0, align_h = 4;
569         u32 max_sc_h, max_sc_v;
570
571         /* In JPEG transparent transfer mode cropping is not supported */
572         if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
573                 r->width  = sink->f_width;
574                 r->height = sink->f_height;
575                 r->left   = r->top = 0;
576                 return;
577         }
578         if (pad == FIMC_SD_PAD_SOURCE) {
579                 if (ctx->rotation != 90 && ctx->rotation != 270)
580                         align_h = 1;
581                 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
582                 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
583                 min_sz = var->min_out_pixsize;
584         } else {
585                 u32 depth = fimc_get_format_depth(sink->fmt);
586                 align_sz = 64/ALIGN(depth, 8);
587                 min_sz = var->min_inp_pixsize;
588                 min_w = min_h = min_sz;
589                 max_sc_h = max_sc_v = 1;
590         }
591         /*
592          * For the crop rectangle at source pad the following constraints
593          * must be met:
594          * - it must fit in the sink pad format rectangle (f_width/f_height);
595          * - maximum downscaling ratio is 64;
596          * - maximum crop size depends if the rotator is used or not;
597          * - the sink pad format width/height must be 4 multiple of the
598          *   prescaler ratios determined by sink pad size and source pad crop,
599          *   the prescaler ratio is returned by fimc_get_scaler_factor().
600          */
601         max_w = min_t(u32,
602                       rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
603                       rotate ? sink->f_height : sink->f_width);
604         max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
605         if (pad == FIMC_SD_PAD_SOURCE) {
606                 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
607                 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
608                 if (rotate) {
609                         swap(max_sc_h, max_sc_v);
610                         swap(min_w, min_h);
611                 }
612         }
613         v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
614                               &r->height, min_h, max_h, align_h,
615                               align_sz);
616         /* Adjust left/top if cropping rectangle is out of bounds */
617         r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
618         r->top  = clamp_t(u32, r->top, 0, sink->f_height - r->height);
619         r->left = round_down(r->left, var->hor_offs_align);
620
621         dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
622             pad, r->left, r->top, r->width, r->height,
623             sink->f_width, sink->f_height);
624 }
625
626 /*
627  * The video node ioctl operations
628  */
629 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
630                                         struct v4l2_capability *cap)
631 {
632         struct fimc_dev *fimc = video_drvdata(file);
633
634         strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
635         strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
636         cap->bus_info[0] = 0;
637         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
638
639         return 0;
640 }
641
642 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
643                                     struct v4l2_fmtdesc *f)
644 {
645         struct fimc_fmt *fmt;
646
647         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
648                                f->index);
649         if (!fmt)
650                 return -EINVAL;
651         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
652         f->pixelformat = fmt->fourcc;
653         if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
654                 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
655         return 0;
656 }
657
658 /**
659  * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
660  *                            elements
661  * @ctx: FIMC capture context
662  * @tfmt: media bus format to try/set on subdevs
663  * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
664  * @set: true to set format on subdevs, false to try only
665  */
666 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
667                                     struct v4l2_mbus_framefmt *tfmt,
668                                     struct fimc_fmt **fmt_id,
669                                     bool set)
670 {
671         struct fimc_dev *fimc = ctx->fimc_dev;
672         struct v4l2_subdev *sd = fimc->pipeline.sensor;
673         struct v4l2_subdev *csis = fimc->pipeline.csis;
674         struct v4l2_subdev_format sfmt;
675         struct v4l2_mbus_framefmt *mf = &sfmt.format;
676         struct fimc_fmt *ffmt = NULL;
677         int ret, i = 0;
678
679         if (WARN_ON(!sd || !tfmt))
680                 return -EINVAL;
681
682         memset(&sfmt, 0, sizeof(sfmt));
683         sfmt.format = *tfmt;
684
685         sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
686         while (1) {
687                 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
688                                         FMT_FLAGS_CAM, i++);
689                 if (ffmt == NULL) {
690                         /*
691                          * Notify user-space if common pixel code for
692                          * host and sensor does not exist.
693                          */
694                         return -EINVAL;
695                 }
696                 mf->code = tfmt->code = ffmt->mbus_code;
697
698                 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
699                 if (ret)
700                         return ret;
701                 if (mf->code != tfmt->code) {
702                         mf->code = 0;
703                         continue;
704                 }
705                 if (mf->width != tfmt->width || mf->height != tfmt->height) {
706                         u32 fcc = ffmt->fourcc;
707                         tfmt->width  = mf->width;
708                         tfmt->height = mf->height;
709                         ffmt = fimc_capture_try_format(ctx,
710                                                &tfmt->width, &tfmt->height,
711                                                NULL, &fcc, FIMC_SD_PAD_SOURCE);
712                         if (ffmt && ffmt->mbus_code)
713                                 mf->code = ffmt->mbus_code;
714                         if (mf->width != tfmt->width ||
715                             mf->height != tfmt->height)
716                                 continue;
717                         tfmt->code = mf->code;
718                 }
719                 if (csis)
720                         ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
721
722                 if (mf->code == tfmt->code &&
723                     mf->width == tfmt->width && mf->height == tfmt->height)
724                         break;
725         }
726
727         if (fmt_id && ffmt)
728                 *fmt_id = ffmt;
729         *tfmt = *mf;
730
731         dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
732         return 0;
733 }
734
735 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
736                                  struct v4l2_format *f)
737 {
738         struct fimc_dev *fimc = video_drvdata(file);
739         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
740
741         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
742                 return -EINVAL;
743
744         return fimc_fill_format(&ctx->d_frame, f);
745 }
746
747 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
748                                    struct v4l2_format *f)
749 {
750         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
751         struct fimc_dev *fimc = video_drvdata(file);
752         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
753         struct v4l2_mbus_framefmt mf;
754         struct fimc_fmt *ffmt = NULL;
755
756         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
757                 return -EINVAL;
758
759         if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
760                 fimc_capture_try_format(ctx, &pix->width, &pix->height,
761                                         NULL, &pix->pixelformat,
762                                         FIMC_SD_PAD_SINK);
763                 ctx->s_frame.f_width  = pix->width;
764                 ctx->s_frame.f_height = pix->height;
765         }
766         ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
767                                        NULL, &pix->pixelformat,
768                                        FIMC_SD_PAD_SOURCE);
769         if (!ffmt)
770                 return -EINVAL;
771
772         if (!fimc->vid_cap.user_subdev_api) {
773                 mf.width  = pix->width;
774                 mf.height = pix->height;
775                 mf.code   = ffmt->mbus_code;
776                 fimc_md_graph_lock(fimc);
777                 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
778                 fimc_md_graph_unlock(fimc);
779
780                 pix->width       = mf.width;
781                 pix->height      = mf.height;
782                 if (ffmt)
783                         pix->pixelformat = ffmt->fourcc;
784         }
785
786         fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
787         return 0;
788 }
789
790 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
791 {
792         ctx->scaler.enabled = !jpeg;
793         fimc_ctrls_activate(ctx, !jpeg);
794
795         if (jpeg)
796                 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
797         else
798                 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
799 }
800
801 static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
802 {
803         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
804         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
805         struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
806         struct fimc_frame *ff = &ctx->d_frame;
807         struct fimc_fmt *s_fmt = NULL;
808         int ret, i;
809
810         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
811                 return -EINVAL;
812         if (vb2_is_busy(&fimc->vid_cap.vbq))
813                 return -EBUSY;
814
815         /* Pre-configure format at camera interface input, for JPEG only */
816         if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
817                 fimc_capture_try_format(ctx, &pix->width, &pix->height,
818                                         NULL, &pix->pixelformat,
819                                         FIMC_SD_PAD_SINK);
820                 ctx->s_frame.f_width  = pix->width;
821                 ctx->s_frame.f_height = pix->height;
822         }
823         /* Try the format at the scaler and the DMA output */
824         ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
825                                           NULL, &pix->pixelformat,
826                                           FIMC_SD_PAD_SOURCE);
827         if (!ff->fmt)
828                 return -EINVAL;
829
830         /* Update RGB Alpha control state and value range */
831         fimc_alpha_ctrl_update(ctx);
832
833         /* Try to match format at the host and the sensor */
834         if (!fimc->vid_cap.user_subdev_api) {
835                 mf->code   = ff->fmt->mbus_code;
836                 mf->width  = pix->width;
837                 mf->height = pix->height;
838
839                 fimc_md_graph_lock(fimc);
840                 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
841                 fimc_md_graph_unlock(fimc);
842                 if (ret)
843                         return ret;
844                 pix->width  = mf->width;
845                 pix->height = mf->height;
846         }
847         fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
848         for (i = 0; i < ff->fmt->colplanes; i++)
849                 ff->payload[i] =
850                         (pix->width * pix->height * ff->fmt->depth[i]) / 8;
851
852         set_frame_bounds(ff, pix->width, pix->height);
853         /* Reset the composition rectangle if not yet configured */
854         if (!(ctx->state & FIMC_DST_CROP))
855                 set_frame_crop(ff, 0, 0, pix->width, pix->height);
856
857         fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
858
859         /* Reset cropping and set format at the camera interface input */
860         if (!fimc->vid_cap.user_subdev_api) {
861                 ctx->s_frame.fmt = s_fmt;
862                 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
863                 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
864         }
865
866         return ret;
867 }
868
869 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
870                                  struct v4l2_format *f)
871 {
872         struct fimc_dev *fimc = video_drvdata(file);
873
874         return fimc_capture_set_format(fimc, f);
875 }
876
877 static int fimc_cap_enum_input(struct file *file, void *priv,
878                                struct v4l2_input *i)
879 {
880         struct fimc_dev *fimc = video_drvdata(file);
881         struct v4l2_subdev *sd = fimc->pipeline.sensor;
882
883         if (i->index != 0)
884                 return -EINVAL;
885
886         i->type = V4L2_INPUT_TYPE_CAMERA;
887         if (sd)
888                 strlcpy(i->name, sd->name, sizeof(i->name));
889         return 0;
890 }
891
892 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
893 {
894         return i == 0 ? i : -EINVAL;
895 }
896
897 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
898 {
899         *i = 0;
900         return 0;
901 }
902
903 /**
904  * fimc_pipeline_validate - check for formats inconsistencies
905  *                          between source and sink pad of each link
906  *
907  * Return 0 if all formats match or -EPIPE otherwise.
908  */
909 static int fimc_pipeline_validate(struct fimc_dev *fimc)
910 {
911         struct v4l2_subdev_format sink_fmt, src_fmt;
912         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
913         struct v4l2_subdev *sd;
914         struct media_pad *pad;
915         int ret;
916
917         /* Start with the video capture node pad */
918         pad = media_entity_remote_source(&vid_cap->vd_pad);
919         if (pad == NULL)
920                 return -EPIPE;
921         /* FIMC.{N} subdevice */
922         sd = media_entity_to_v4l2_subdev(pad->entity);
923
924         while (1) {
925                 /* Retrieve format at the sink pad */
926                 pad = &sd->entity.pads[0];
927                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
928                         break;
929                 /* Don't call FIMC subdev operation to avoid nested locking */
930                 if (sd == fimc->vid_cap.subdev) {
931                         struct fimc_frame *ff = &vid_cap->ctx->s_frame;
932                         sink_fmt.format.width = ff->f_width;
933                         sink_fmt.format.height = ff->f_height;
934                         sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
935                 } else {
936                         sink_fmt.pad = pad->index;
937                         sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
938                         ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
939                         if (ret < 0 && ret != -ENOIOCTLCMD)
940                                 return -EPIPE;
941                 }
942                 /* Retrieve format at the source pad */
943                 pad = media_entity_remote_source(pad);
944                 if (pad == NULL ||
945                     media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
946                         break;
947
948                 sd = media_entity_to_v4l2_subdev(pad->entity);
949                 src_fmt.pad = pad->index;
950                 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
951                 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
952                 if (ret < 0 && ret != -ENOIOCTLCMD)
953                         return -EPIPE;
954
955                 if (src_fmt.format.width != sink_fmt.format.width ||
956                     src_fmt.format.height != sink_fmt.format.height ||
957                     src_fmt.format.code != sink_fmt.format.code)
958                         return -EPIPE;
959         }
960         return 0;
961 }
962
963 static int fimc_cap_streamon(struct file *file, void *priv,
964                              enum v4l2_buf_type type)
965 {
966         struct fimc_dev *fimc = video_drvdata(file);
967         struct fimc_pipeline *p = &fimc->pipeline;
968         int ret;
969
970         if (fimc_capture_active(fimc))
971                 return -EBUSY;
972
973         media_entity_pipeline_start(&p->sensor->entity, p->pipe);
974
975         if (fimc->vid_cap.user_subdev_api) {
976                 ret = fimc_pipeline_validate(fimc);
977                 if (ret)
978                         return ret;
979         }
980         return vb2_streamon(&fimc->vid_cap.vbq, type);
981 }
982
983 static int fimc_cap_streamoff(struct file *file, void *priv,
984                             enum v4l2_buf_type type)
985 {
986         struct fimc_dev *fimc = video_drvdata(file);
987         struct v4l2_subdev *sd = fimc->pipeline.sensor;
988         int ret;
989
990         ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
991         if (ret == 0)
992                 media_entity_pipeline_stop(&sd->entity);
993         return ret;
994 }
995
996 static int fimc_cap_reqbufs(struct file *file, void *priv,
997                             struct v4l2_requestbuffers *reqbufs)
998 {
999         struct fimc_dev *fimc = video_drvdata(file);
1000         int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
1001
1002         if (!ret)
1003                 fimc->vid_cap.reqbufs_count = reqbufs->count;
1004         return ret;
1005 }
1006
1007 static int fimc_cap_querybuf(struct file *file, void *priv,
1008                            struct v4l2_buffer *buf)
1009 {
1010         struct fimc_dev *fimc = video_drvdata(file);
1011
1012         return vb2_querybuf(&fimc->vid_cap.vbq, buf);
1013 }
1014
1015 static int fimc_cap_qbuf(struct file *file, void *priv,
1016                           struct v4l2_buffer *buf)
1017 {
1018         struct fimc_dev *fimc = video_drvdata(file);
1019
1020         return vb2_qbuf(&fimc->vid_cap.vbq, buf);
1021 }
1022
1023 static int fimc_cap_dqbuf(struct file *file, void *priv,
1024                            struct v4l2_buffer *buf)
1025 {
1026         struct fimc_dev *fimc = video_drvdata(file);
1027
1028         return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
1029 }
1030
1031 static int fimc_cap_create_bufs(struct file *file, void *priv,
1032                                 struct v4l2_create_buffers *create)
1033 {
1034         struct fimc_dev *fimc = video_drvdata(file);
1035
1036         return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1037 }
1038
1039 static int fimc_cap_prepare_buf(struct file *file, void *priv,
1040                                 struct v4l2_buffer *b)
1041 {
1042         struct fimc_dev *fimc = video_drvdata(file);
1043
1044         return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1045 }
1046
1047 static int fimc_cap_g_selection(struct file *file, void *fh,
1048                                 struct v4l2_selection *s)
1049 {
1050         struct fimc_dev *fimc = video_drvdata(file);
1051         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1052         struct fimc_frame *f = &ctx->s_frame;
1053
1054         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1055                 return -EINVAL;
1056
1057         switch (s->target) {
1058         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1059         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1060                 f = &ctx->d_frame;
1061         case V4L2_SEL_TGT_CROP_BOUNDS:
1062         case V4L2_SEL_TGT_CROP_DEFAULT:
1063                 s->r.left = 0;
1064                 s->r.top = 0;
1065                 s->r.width = f->o_width;
1066                 s->r.height = f->o_height;
1067                 return 0;
1068
1069         case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1070                 f = &ctx->d_frame;
1071         case V4L2_SEL_TGT_CROP_ACTIVE:
1072                 s->r.left = f->offs_h;
1073                 s->r.top = f->offs_v;
1074                 s->r.width = f->width;
1075                 s->r.height = f->height;
1076                 return 0;
1077         }
1078
1079         return -EINVAL;
1080 }
1081
1082 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1083 int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1084 {
1085         if (a->left < b->left || a->top < b->top)
1086                 return 0;
1087         if (a->left + a->width > b->left + b->width)
1088                 return 0;
1089         if (a->top + a->height > b->top + b->height)
1090                 return 0;
1091
1092         return 1;
1093 }
1094
1095 static int fimc_cap_s_selection(struct file *file, void *fh,
1096                                 struct v4l2_selection *s)
1097 {
1098         struct fimc_dev *fimc = video_drvdata(file);
1099         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1100         struct v4l2_rect rect = s->r;
1101         struct fimc_frame *f;
1102         unsigned long flags;
1103         unsigned int pad;
1104
1105         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1106                 return -EINVAL;
1107
1108         switch (s->target) {
1109         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1110         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1111         case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1112                 f = &ctx->d_frame;
1113                 pad = FIMC_SD_PAD_SOURCE;
1114                 break;
1115         case V4L2_SEL_TGT_CROP_BOUNDS:
1116         case V4L2_SEL_TGT_CROP_DEFAULT:
1117         case V4L2_SEL_TGT_CROP_ACTIVE:
1118                 f = &ctx->s_frame;
1119                 pad = FIMC_SD_PAD_SINK;
1120                 break;
1121         default:
1122                 return -EINVAL;
1123         }
1124
1125         fimc_capture_try_crop(ctx, &rect, pad);
1126
1127         if (s->flags & V4L2_SEL_FLAG_LE &&
1128             !enclosed_rectangle(&rect, &s->r))
1129                 return -ERANGE;
1130
1131         if (s->flags & V4L2_SEL_FLAG_GE &&
1132             !enclosed_rectangle(&s->r, &rect))
1133                 return -ERANGE;
1134
1135         s->r = rect;
1136         spin_lock_irqsave(&fimc->slock, flags);
1137         set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1138                        s->r.height);
1139         spin_unlock_irqrestore(&fimc->slock, flags);
1140
1141         set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1142         return 0;
1143 }
1144
1145 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1146         .vidioc_querycap                = fimc_vidioc_querycap_capture,
1147
1148         .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1149         .vidioc_try_fmt_vid_cap_mplane  = fimc_cap_try_fmt_mplane,
1150         .vidioc_s_fmt_vid_cap_mplane    = fimc_cap_s_fmt_mplane,
1151         .vidioc_g_fmt_vid_cap_mplane    = fimc_cap_g_fmt_mplane,
1152
1153         .vidioc_reqbufs                 = fimc_cap_reqbufs,
1154         .vidioc_querybuf                = fimc_cap_querybuf,
1155
1156         .vidioc_qbuf                    = fimc_cap_qbuf,
1157         .vidioc_dqbuf                   = fimc_cap_dqbuf,
1158
1159         .vidioc_prepare_buf             = fimc_cap_prepare_buf,
1160         .vidioc_create_bufs             = fimc_cap_create_bufs,
1161
1162         .vidioc_streamon                = fimc_cap_streamon,
1163         .vidioc_streamoff               = fimc_cap_streamoff,
1164
1165         .vidioc_g_selection             = fimc_cap_g_selection,
1166         .vidioc_s_selection             = fimc_cap_s_selection,
1167
1168         .vidioc_enum_input              = fimc_cap_enum_input,
1169         .vidioc_s_input                 = fimc_cap_s_input,
1170         .vidioc_g_input                 = fimc_cap_g_input,
1171 };
1172
1173 /* Capture subdev media entity operations */
1174 static int fimc_link_setup(struct media_entity *entity,
1175                            const struct media_pad *local,
1176                            const struct media_pad *remote, u32 flags)
1177 {
1178         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1179         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1180
1181         if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1182                 return -EINVAL;
1183
1184         if (WARN_ON(fimc == NULL))
1185                 return 0;
1186
1187         dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1188             local->entity->name, remote->entity->name, flags,
1189             fimc->vid_cap.input);
1190
1191         if (flags & MEDIA_LNK_FL_ENABLED) {
1192                 if (fimc->vid_cap.input != 0)
1193                         return -EBUSY;
1194                 fimc->vid_cap.input = sd->grp_id;
1195                 return 0;
1196         }
1197
1198         fimc->vid_cap.input = 0;
1199         return 0;
1200 }
1201
1202 static const struct media_entity_operations fimc_sd_media_ops = {
1203         .link_setup = fimc_link_setup,
1204 };
1205
1206 /**
1207  * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1208  * @sd: pointer to a subdev generating the notification
1209  * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1210  * @arg: pointer to an u32 type integer that stores the frame payload value
1211  *
1212  * The End Of Frame notification sent by sensor subdev in its still capture
1213  * mode. If there is only a single VSYNC generated by the sensor at the
1214  * beginning of a frame transmission, FIMC does not issue the LastIrq
1215  * (end of frame) interrupt. And this notification is used to complete the
1216  * frame capture and returning a buffer to user-space. Subdev drivers should
1217  * call this notification from their last 'End of frame capture' interrupt.
1218  */
1219 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1220                         void *arg)
1221 {
1222         struct fimc_sensor_info *sensor;
1223         struct fimc_vid_buffer *buf;
1224         struct fimc_md *fmd;
1225         struct fimc_dev *fimc;
1226         unsigned long flags;
1227
1228         if (sd == NULL)
1229                 return;
1230
1231         sensor = v4l2_get_subdev_hostdata(sd);
1232         fmd = entity_to_fimc_mdev(&sd->entity);
1233
1234         spin_lock_irqsave(&fmd->slock, flags);
1235         fimc = sensor ? sensor->host : NULL;
1236
1237         if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1238             test_bit(ST_CAPT_PEND, &fimc->state)) {
1239                 unsigned long irq_flags;
1240                 spin_lock_irqsave(&fimc->slock, irq_flags);
1241                 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1242                         buf = list_entry(fimc->vid_cap.active_buf_q.next,
1243                                          struct fimc_vid_buffer, list);
1244                         vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1245                 }
1246                 fimc_capture_irq_handler(fimc, true);
1247                 fimc_deactivate_capture(fimc);
1248                 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1249         }
1250         spin_unlock_irqrestore(&fmd->slock, flags);
1251 }
1252
1253 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1254                                       struct v4l2_subdev_fh *fh,
1255                                       struct v4l2_subdev_mbus_code_enum *code)
1256 {
1257         struct fimc_fmt *fmt;
1258
1259         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1260         if (!fmt)
1261                 return -EINVAL;
1262         code->code = fmt->mbus_code;
1263         return 0;
1264 }
1265
1266 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1267                                struct v4l2_subdev_fh *fh,
1268                                struct v4l2_subdev_format *fmt)
1269 {
1270         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1271         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1272         struct v4l2_mbus_framefmt *mf;
1273         struct fimc_frame *ff;
1274
1275         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1276                 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1277                 fmt->format = *mf;
1278                 return 0;
1279         }
1280         mf = &fmt->format;
1281         mf->colorspace = V4L2_COLORSPACE_JPEG;
1282         ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1283
1284         mutex_lock(&fimc->lock);
1285         /* The pixel code is same on both input and output pad */
1286         if (!WARN_ON(ctx->s_frame.fmt == NULL))
1287                 mf->code = ctx->s_frame.fmt->mbus_code;
1288         mf->width  = ff->f_width;
1289         mf->height = ff->f_height;
1290         mutex_unlock(&fimc->lock);
1291
1292         return 0;
1293 }
1294
1295 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1296                                struct v4l2_subdev_fh *fh,
1297                                struct v4l2_subdev_format *fmt)
1298 {
1299         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1300         struct v4l2_mbus_framefmt *mf = &fmt->format;
1301         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1302         struct fimc_frame *ff;
1303         struct fimc_fmt *ffmt;
1304
1305         dbg("pad%d: code: 0x%x, %dx%d",
1306             fmt->pad, mf->code, mf->width, mf->height);
1307
1308         if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1309             vb2_is_busy(&fimc->vid_cap.vbq))
1310                 return -EBUSY;
1311
1312         mutex_lock(&fimc->lock);
1313         ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1314                                        &mf->code, NULL, fmt->pad);
1315         mutex_unlock(&fimc->lock);
1316         mf->colorspace = V4L2_COLORSPACE_JPEG;
1317
1318         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1319                 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1320                 *mf = fmt->format;
1321                 return 0;
1322         }
1323         /* Update RGB Alpha control state and value range */
1324         fimc_alpha_ctrl_update(ctx);
1325
1326         fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1327
1328         ff = fmt->pad == FIMC_SD_PAD_SINK ?
1329                 &ctx->s_frame : &ctx->d_frame;
1330
1331         mutex_lock(&fimc->lock);
1332         set_frame_bounds(ff, mf->width, mf->height);
1333         fimc->vid_cap.mf = *mf;
1334         ff->fmt = ffmt;
1335
1336         /* Reset the crop rectangle if required. */
1337         if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1338                 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1339
1340         if (fmt->pad == FIMC_SD_PAD_SINK)
1341                 ctx->state &= ~FIMC_DST_CROP;
1342         mutex_unlock(&fimc->lock);
1343         return 0;
1344 }
1345
1346 static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1347                                 struct v4l2_subdev_fh *fh,
1348                                 struct v4l2_subdev_crop *crop)
1349 {
1350         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1351         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1352         struct v4l2_rect *r = &crop->rect;
1353         struct fimc_frame *ff;
1354
1355         if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1356                 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1357                 return 0;
1358         }
1359         ff = crop->pad == FIMC_SD_PAD_SINK ?
1360                 &ctx->s_frame : &ctx->d_frame;
1361
1362         mutex_lock(&fimc->lock);
1363         r->left   = ff->offs_h;
1364         r->top    = ff->offs_v;
1365         r->width  = ff->width;
1366         r->height = ff->height;
1367         mutex_unlock(&fimc->lock);
1368
1369         dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1370             ff, crop->pad, r->left, r->top, r->width, r->height,
1371             ff->f_width, ff->f_height);
1372
1373         return 0;
1374 }
1375
1376 static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1377                                 struct v4l2_subdev_fh *fh,
1378                                 struct v4l2_subdev_crop *crop)
1379 {
1380         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1381         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1382         struct v4l2_rect *r = &crop->rect;
1383         struct fimc_frame *ff;
1384         unsigned long flags;
1385
1386         dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1387
1388         ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1389                 &ctx->d_frame : &ctx->s_frame;
1390
1391         mutex_lock(&fimc->lock);
1392         fimc_capture_try_crop(ctx, r, crop->pad);
1393
1394         if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1395                 mutex_unlock(&fimc->lock);
1396                 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1397                 return 0;
1398         }
1399         spin_lock_irqsave(&fimc->slock, flags);
1400         set_frame_crop(ff, r->left, r->top, r->width, r->height);
1401         if (crop->pad == FIMC_SD_PAD_SOURCE)
1402                 ctx->state |= FIMC_DST_CROP;
1403
1404         set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1405         spin_unlock_irqrestore(&fimc->slock, flags);
1406
1407         dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1408             r->width, r->height);
1409
1410         mutex_unlock(&fimc->lock);
1411         return 0;
1412 }
1413
1414 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1415         .enum_mbus_code = fimc_subdev_enum_mbus_code,
1416         .get_fmt = fimc_subdev_get_fmt,
1417         .set_fmt = fimc_subdev_set_fmt,
1418         .get_crop = fimc_subdev_get_crop,
1419         .set_crop = fimc_subdev_set_crop,
1420 };
1421
1422 static struct v4l2_subdev_ops fimc_subdev_ops = {
1423         .pad = &fimc_subdev_pad_ops,
1424 };
1425
1426 static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1427                                       struct v4l2_device *v4l2_dev)
1428 {
1429         struct v4l2_subdev *sd;
1430         int ret;
1431
1432         sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1433         if (!sd)
1434                 return -ENOMEM;
1435
1436         v4l2_subdev_init(sd, &fimc_subdev_ops);
1437         sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1438         snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1439
1440         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1441         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1442         ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1443                                 fimc->vid_cap.sd_pads, 0);
1444         if (ret)
1445                 goto me_err;
1446         ret = v4l2_device_register_subdev(v4l2_dev, sd);
1447         if (ret)
1448                 goto sd_err;
1449
1450         fimc->vid_cap.subdev = sd;
1451         v4l2_set_subdevdata(sd, fimc);
1452         sd->entity.ops = &fimc_sd_media_ops;
1453         return 0;
1454 sd_err:
1455         media_entity_cleanup(&sd->entity);
1456 me_err:
1457         kfree(sd);
1458         return ret;
1459 }
1460
1461 static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1462 {
1463         struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1464
1465         if (!sd)
1466                 return;
1467         media_entity_cleanup(&sd->entity);
1468         v4l2_device_unregister_subdev(sd);
1469         kfree(sd);
1470         fimc->vid_cap.subdev = NULL;
1471 }
1472
1473 /* Set default format at the sensor and host interface */
1474 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1475 {
1476         struct v4l2_format fmt = {
1477                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1478                 .fmt.pix_mp = {
1479                         .width          = 640,
1480                         .height         = 480,
1481                         .pixelformat    = V4L2_PIX_FMT_YUYV,
1482                         .field          = V4L2_FIELD_NONE,
1483                         .colorspace     = V4L2_COLORSPACE_JPEG,
1484                 },
1485         };
1486
1487         return fimc_capture_set_format(fimc, &fmt);
1488 }
1489
1490 /* fimc->lock must be already initialized */
1491 int fimc_register_capture_device(struct fimc_dev *fimc,
1492                                  struct v4l2_device *v4l2_dev)
1493 {
1494         struct video_device *vfd;
1495         struct fimc_vid_cap *vid_cap;
1496         struct fimc_ctx *ctx;
1497         struct vb2_queue *q;
1498         int ret = -ENOMEM;
1499
1500         ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1501         if (!ctx)
1502                 return -ENOMEM;
1503
1504         ctx->fimc_dev    = fimc;
1505         ctx->in_path     = FIMC_CAMERA;
1506         ctx->out_path    = FIMC_DMA;
1507         ctx->state       = FIMC_CTX_CAP;
1508         ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1509         ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1510
1511         vfd = video_device_alloc();
1512         if (!vfd) {
1513                 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1514                 goto err_vd_alloc;
1515         }
1516
1517         snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1518                  dev_name(&fimc->pdev->dev));
1519
1520         vfd->fops       = &fimc_capture_fops;
1521         vfd->ioctl_ops  = &fimc_capture_ioctl_ops;
1522         vfd->v4l2_dev   = v4l2_dev;
1523         vfd->minor      = -1;
1524         vfd->release    = video_device_release;
1525         vfd->lock       = &fimc->lock;
1526         video_set_drvdata(vfd, fimc);
1527
1528         vid_cap = &fimc->vid_cap;
1529         vid_cap->vfd = vfd;
1530         vid_cap->active_buf_cnt = 0;
1531         vid_cap->reqbufs_count  = 0;
1532         vid_cap->refcnt = 0;
1533
1534         INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1535         INIT_LIST_HEAD(&vid_cap->active_buf_q);
1536         spin_lock_init(&ctx->slock);
1537         vid_cap->ctx = ctx;
1538
1539         q = &fimc->vid_cap.vbq;
1540         memset(q, 0, sizeof(*q));
1541         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1542         q->io_modes = VB2_MMAP | VB2_USERPTR;
1543         q->drv_priv = fimc->vid_cap.ctx;
1544         q->ops = &fimc_capture_qops;
1545         q->mem_ops = &vb2_dma_contig_memops;
1546         q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1547
1548         vb2_queue_init(q);
1549
1550         fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1551         ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1552         if (ret)
1553                 goto err_ent;
1554         ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1555         if (ret)
1556                 goto err_sd_reg;
1557
1558         vfd->ctrl_handler = &ctx->ctrl_handler;
1559         return 0;
1560
1561 err_sd_reg:
1562         media_entity_cleanup(&vfd->entity);
1563 err_ent:
1564         video_device_release(vfd);
1565 err_vd_alloc:
1566         kfree(ctx);
1567         return ret;
1568 }
1569
1570 void fimc_unregister_capture_device(struct fimc_dev *fimc)
1571 {
1572         struct video_device *vfd = fimc->vid_cap.vfd;
1573
1574         if (vfd) {
1575                 media_entity_cleanup(&vfd->entity);
1576                 /* Can also be called if video device was
1577                    not registered */
1578                 video_unregister_device(vfd);
1579         }
1580         fimc_destroy_capture_subdev(fimc);
1581         kfree(fimc->vid_cap.ctx);
1582         fimc->vid_cap.ctx = NULL;
1583 }