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