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