]> Pileus Git - ~andy/linux/blob - drivers/media/video/s5p-fimc/fimc-capture.c
[media] s5p-fimc: Add subdev for the FIMC processing block
[~andy/linux] / 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);
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 = 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 = 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                 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 int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
735 {
736         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
737         struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
738         struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
739         struct fimc_frame *ff = &ctx->d_frame;
740         struct fimc_fmt *s_fmt = NULL;
741         int ret, i;
742
743         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
744                 return -EINVAL;
745         if (vb2_is_busy(&fimc->vid_cap.vbq))
746                 return -EBUSY;
747
748         /* Pre-configure format at camera interface input, for JPEG only */
749         if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
750                 fimc_capture_try_format(ctx, &pix->width, &pix->height,
751                                         NULL, &pix->pixelformat,
752                                         FIMC_SD_PAD_SINK);
753                 ctx->s_frame.f_width  = pix->width;
754                 ctx->s_frame.f_height = pix->height;
755         }
756         /* Try the format at the scaler and the DMA output */
757         ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
758                                           NULL, &pix->pixelformat,
759                                           FIMC_SD_PAD_SOURCE);
760         if (!ff->fmt)
761                 return -EINVAL;
762         /* Try to match format at the host and the sensor */
763         if (!fimc->vid_cap.user_subdev_api) {
764                 mf->code   = ff->fmt->mbus_code;
765                 mf->width  = pix->width;
766                 mf->height = pix->height;
767
768                 fimc_md_graph_lock(fimc);
769                 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
770                 fimc_md_graph_unlock(fimc);
771                 if (ret)
772                         return ret;
773                 pix->width  = mf->width;
774                 pix->height = mf->height;
775         }
776         fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
777         for (i = 0; i < ff->fmt->colplanes; i++)
778                 ff->payload[i] =
779                         (pix->width * pix->height * ff->fmt->depth[i]) / 8;
780
781         set_frame_bounds(ff, pix->width, pix->height);
782         /* Reset the composition rectangle if not yet configured */
783         if (!(ctx->state & FIMC_DST_CROP))
784                 set_frame_crop(ff, 0, 0, pix->width, pix->height);
785
786         /* Reset cropping and set format at the camera interface input */
787         if (!fimc->vid_cap.user_subdev_api) {
788                 ctx->s_frame.fmt = s_fmt;
789                 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
790                 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
791         }
792
793         return ret;
794 }
795
796 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
797                                  struct v4l2_format *f)
798 {
799         struct fimc_dev *fimc = video_drvdata(file);
800
801         return fimc_capture_set_format(fimc, f);
802 }
803
804 static int fimc_cap_enum_input(struct file *file, void *priv,
805                                struct v4l2_input *i)
806 {
807         struct fimc_dev *fimc = video_drvdata(file);
808         struct v4l2_subdev *sd = fimc->pipeline.sensor;
809
810         if (i->index != 0)
811                 return -EINVAL;
812
813         i->type = V4L2_INPUT_TYPE_CAMERA;
814         if (sd)
815                 strlcpy(i->name, sd->name, sizeof(i->name));
816         return 0;
817 }
818
819 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
820 {
821         return i == 0 ? i : -EINVAL;
822 }
823
824 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
825 {
826         *i = 0;
827         return 0;
828 }
829
830 /**
831  * fimc_pipeline_validate - check for formats inconsistencies
832  *                          between source and sink pad of each link
833  *
834  * Return 0 if all formats match or -EPIPE otherwise.
835  */
836 static int fimc_pipeline_validate(struct fimc_dev *fimc)
837 {
838         struct v4l2_subdev_format sink_fmt, src_fmt;
839         struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
840         struct v4l2_subdev *sd;
841         struct media_pad *pad;
842         int ret;
843
844         /* Start with the video capture node pad */
845         pad = media_entity_remote_source(&vid_cap->vd_pad);
846         if (pad == NULL)
847                 return -EPIPE;
848         /* FIMC.{N} subdevice */
849         sd = media_entity_to_v4l2_subdev(pad->entity);
850
851         while (1) {
852                 /* Retrieve format at the sink pad */
853                 pad = &sd->entity.pads[0];
854                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
855                         break;
856                 /* Don't call FIMC subdev operation to avoid nested locking */
857                 if (sd == fimc->vid_cap.subdev) {
858                         struct fimc_frame *ff = &vid_cap->ctx->s_frame;
859                         sink_fmt.format.width = ff->f_width;
860                         sink_fmt.format.height = ff->f_height;
861                         sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
862                 } else {
863                         sink_fmt.pad = pad->index;
864                         sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
865                         ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
866                         if (ret < 0 && ret != -ENOIOCTLCMD)
867                                 return -EPIPE;
868                 }
869                 /* Retrieve format at the source pad */
870                 pad = media_entity_remote_source(pad);
871                 if (pad == NULL ||
872                     media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
873                         break;
874
875                 sd = media_entity_to_v4l2_subdev(pad->entity);
876                 src_fmt.pad = pad->index;
877                 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
878                 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
879                 if (ret < 0 && ret != -ENOIOCTLCMD)
880                         return -EPIPE;
881
882                 if (src_fmt.format.width != sink_fmt.format.width ||
883                     src_fmt.format.height != sink_fmt.format.height ||
884                     src_fmt.format.code != sink_fmt.format.code)
885                         return -EPIPE;
886         }
887         return 0;
888 }
889
890 static int fimc_cap_streamon(struct file *file, void *priv,
891                              enum v4l2_buf_type type)
892 {
893         struct fimc_dev *fimc = video_drvdata(file);
894         struct fimc_pipeline *p = &fimc->pipeline;
895         int ret;
896
897         if (fimc_capture_active(fimc))
898                 return -EBUSY;
899
900         media_entity_pipeline_start(&p->sensor->entity, p->pipe);
901
902         if (fimc->vid_cap.user_subdev_api) {
903                 ret = fimc_pipeline_validate(fimc);
904                 if (ret)
905                         return ret;
906         }
907         return vb2_streamon(&fimc->vid_cap.vbq, type);
908 }
909
910 static int fimc_cap_streamoff(struct file *file, void *priv,
911                             enum v4l2_buf_type type)
912 {
913         struct fimc_dev *fimc = video_drvdata(file);
914         struct v4l2_subdev *sd = fimc->pipeline.sensor;
915         int ret;
916
917         ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
918         if (ret == 0)
919                 media_entity_pipeline_stop(&sd->entity);
920         return ret;
921 }
922
923 static int fimc_cap_reqbufs(struct file *file, void *priv,
924                             struct v4l2_requestbuffers *reqbufs)
925 {
926         struct fimc_dev *fimc = video_drvdata(file);
927         int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
928
929         if (!ret)
930                 fimc->vid_cap.reqbufs_count = reqbufs->count;
931         return ret;
932 }
933
934 static int fimc_cap_querybuf(struct file *file, void *priv,
935                            struct v4l2_buffer *buf)
936 {
937         struct fimc_dev *fimc = video_drvdata(file);
938
939         return vb2_querybuf(&fimc->vid_cap.vbq, buf);
940 }
941
942 static int fimc_cap_qbuf(struct file *file, void *priv,
943                           struct v4l2_buffer *buf)
944 {
945         struct fimc_dev *fimc = video_drvdata(file);
946
947         return vb2_qbuf(&fimc->vid_cap.vbq, buf);
948 }
949
950 static int fimc_cap_dqbuf(struct file *file, void *priv,
951                            struct v4l2_buffer *buf)
952 {
953         struct fimc_dev *fimc = video_drvdata(file);
954
955         return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
956 }
957
958 static int fimc_cap_cropcap(struct file *file, void *fh,
959                             struct v4l2_cropcap *cr)
960 {
961         struct fimc_dev *fimc = video_drvdata(file);
962         struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
963
964         if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
965                 return -EINVAL;
966
967         cr->bounds.left         = 0;
968         cr->bounds.top          = 0;
969         cr->bounds.width        = f->o_width;
970         cr->bounds.height       = f->o_height;
971         cr->defrect             = cr->bounds;
972
973         return 0;
974 }
975
976 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
977 {
978         struct fimc_dev *fimc = video_drvdata(file);
979         struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
980
981         cr->c.left      = f->offs_h;
982         cr->c.top       = f->offs_v;
983         cr->c.width     = f->width;
984         cr->c.height    = f->height;
985
986         return 0;
987 }
988
989 static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
990 {
991         struct fimc_dev *fimc = video_drvdata(file);
992         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
993         struct fimc_frame *ff;
994         unsigned long flags;
995
996         fimc_capture_try_crop(ctx, &cr->c, FIMC_SD_PAD_SINK);
997         ff = &ctx->s_frame;
998
999         spin_lock_irqsave(&fimc->slock, flags);
1000         set_frame_crop(ff, cr->c.left, cr->c.top, cr->c.width, cr->c.height);
1001         set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1002         spin_unlock_irqrestore(&fimc->slock, flags);
1003
1004         return 0;
1005 }
1006
1007 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1008         .vidioc_querycap                = fimc_vidioc_querycap_capture,
1009
1010         .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1011         .vidioc_try_fmt_vid_cap_mplane  = fimc_cap_try_fmt_mplane,
1012         .vidioc_s_fmt_vid_cap_mplane    = fimc_cap_s_fmt_mplane,
1013         .vidioc_g_fmt_vid_cap_mplane    = fimc_cap_g_fmt_mplane,
1014
1015         .vidioc_reqbufs                 = fimc_cap_reqbufs,
1016         .vidioc_querybuf                = fimc_cap_querybuf,
1017
1018         .vidioc_qbuf                    = fimc_cap_qbuf,
1019         .vidioc_dqbuf                   = fimc_cap_dqbuf,
1020
1021         .vidioc_streamon                = fimc_cap_streamon,
1022         .vidioc_streamoff               = fimc_cap_streamoff,
1023
1024         .vidioc_g_crop                  = fimc_cap_g_crop,
1025         .vidioc_s_crop                  = fimc_cap_s_crop,
1026         .vidioc_cropcap                 = fimc_cap_cropcap,
1027
1028         .vidioc_enum_input              = fimc_cap_enum_input,
1029         .vidioc_s_input                 = fimc_cap_s_input,
1030         .vidioc_g_input                 = fimc_cap_g_input,
1031 };
1032
1033 /* Capture subdev media entity operations */
1034 static int fimc_link_setup(struct media_entity *entity,
1035                            const struct media_pad *local,
1036                            const struct media_pad *remote, u32 flags)
1037 {
1038         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1039         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1040
1041         if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1042                 return -EINVAL;
1043
1044         if (WARN_ON(fimc == NULL))
1045                 return 0;
1046
1047         dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1048             local->entity->name, remote->entity->name, flags,
1049             fimc->vid_cap.input);
1050
1051         if (flags & MEDIA_LNK_FL_ENABLED) {
1052                 if (fimc->vid_cap.input != 0)
1053                         return -EBUSY;
1054                 fimc->vid_cap.input = sd->grp_id;
1055                 return 0;
1056         }
1057
1058         fimc->vid_cap.input = 0;
1059         return 0;
1060 }
1061
1062 static const struct media_entity_operations fimc_sd_media_ops = {
1063         .link_setup = fimc_link_setup,
1064 };
1065
1066 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1067                                       struct v4l2_subdev_fh *fh,
1068                                       struct v4l2_subdev_mbus_code_enum *code)
1069 {
1070         struct fimc_fmt *fmt;
1071
1072         fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1073         if (!fmt)
1074                 return -EINVAL;
1075         code->code = fmt->mbus_code;
1076         return 0;
1077 }
1078
1079 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1080                                struct v4l2_subdev_fh *fh,
1081                                struct v4l2_subdev_format *fmt)
1082 {
1083         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1084         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1085         struct v4l2_mbus_framefmt *mf;
1086         struct fimc_frame *ff;
1087
1088         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1089                 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1090                 fmt->format = *mf;
1091                 return 0;
1092         }
1093         mf = &fmt->format;
1094         mf->colorspace = V4L2_COLORSPACE_JPEG;
1095         ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1096
1097         mutex_lock(&fimc->lock);
1098         /* The pixel code is same on both input and output pad */
1099         if (!WARN_ON(ctx->s_frame.fmt == NULL))
1100                 mf->code = ctx->s_frame.fmt->mbus_code;
1101         mf->width  = ff->f_width;
1102         mf->height = ff->f_height;
1103         mutex_unlock(&fimc->lock);
1104
1105         return 0;
1106 }
1107
1108 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1109                                struct v4l2_subdev_fh *fh,
1110                                struct v4l2_subdev_format *fmt)
1111 {
1112         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1113         struct v4l2_mbus_framefmt *mf = &fmt->format;
1114         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1115         struct fimc_frame *ff;
1116         struct fimc_fmt *ffmt;
1117
1118         dbg("pad%d: code: 0x%x, %dx%d",
1119             fmt->pad, mf->code, mf->width, mf->height);
1120
1121         if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1122             vb2_is_busy(&fimc->vid_cap.vbq))
1123                 return -EBUSY;
1124
1125         mutex_lock(&fimc->lock);
1126         ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1127                                        &mf->code, NULL, fmt->pad);
1128         mutex_unlock(&fimc->lock);
1129         mf->colorspace = V4L2_COLORSPACE_JPEG;
1130
1131         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1132                 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1133                 *mf = fmt->format;
1134                 return 0;
1135         }
1136         ff = fmt->pad == FIMC_SD_PAD_SINK ?
1137                 &ctx->s_frame : &ctx->d_frame;
1138
1139         mutex_lock(&fimc->lock);
1140         set_frame_bounds(ff, mf->width, mf->height);
1141         ff->fmt = ffmt;
1142
1143         /* Reset the crop rectangle if required. */
1144         if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1145                 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1146
1147         if (fmt->pad == FIMC_SD_PAD_SINK)
1148                 ctx->state &= ~FIMC_DST_CROP;
1149         mutex_unlock(&fimc->lock);
1150         return 0;
1151 }
1152
1153 static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1154                                 struct v4l2_subdev_fh *fh,
1155                                 struct v4l2_subdev_crop *crop)
1156 {
1157         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1158         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1159         struct v4l2_rect *r = &crop->rect;
1160         struct fimc_frame *ff;
1161
1162         if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1163                 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1164                 return 0;
1165         }
1166         ff = crop->pad == FIMC_SD_PAD_SINK ?
1167                 &ctx->s_frame : &ctx->d_frame;
1168
1169         mutex_lock(&fimc->lock);
1170         r->left   = ff->offs_h;
1171         r->top    = ff->offs_v;
1172         r->width  = ff->width;
1173         r->height = ff->height;
1174         mutex_unlock(&fimc->lock);
1175
1176         dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1177             ff, crop->pad, r->left, r->top, r->width, r->height,
1178             ff->f_width, ff->f_height);
1179
1180         return 0;
1181 }
1182
1183 static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1184                                 struct v4l2_subdev_fh *fh,
1185                                 struct v4l2_subdev_crop *crop)
1186 {
1187         struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1188         struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1189         struct v4l2_rect *r = &crop->rect;
1190         struct fimc_frame *ff;
1191         unsigned long flags;
1192
1193         dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1194
1195         ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1196                 &ctx->d_frame : &ctx->s_frame;
1197
1198         mutex_lock(&fimc->lock);
1199         fimc_capture_try_crop(ctx, r, crop->pad);
1200
1201         if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1202                 mutex_lock(&fimc->lock);
1203                 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1204                 return 0;
1205         }
1206         spin_lock_irqsave(&fimc->slock, flags);
1207         set_frame_crop(ff, r->left, r->top, r->width, r->height);
1208         if (crop->pad == FIMC_SD_PAD_SOURCE)
1209                 ctx->state |= FIMC_DST_CROP;
1210
1211         set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1212         spin_unlock_irqrestore(&fimc->slock, flags);
1213
1214         dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1215             r->width, r->height);
1216
1217         mutex_unlock(&fimc->lock);
1218         return 0;
1219 }
1220
1221 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1222         .enum_mbus_code = fimc_subdev_enum_mbus_code,
1223         .get_fmt = fimc_subdev_get_fmt,
1224         .set_fmt = fimc_subdev_set_fmt,
1225         .get_crop = fimc_subdev_get_crop,
1226         .set_crop = fimc_subdev_set_crop,
1227 };
1228
1229 static struct v4l2_subdev_ops fimc_subdev_ops = {
1230         .pad = &fimc_subdev_pad_ops,
1231 };
1232
1233 static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1234                                       struct v4l2_device *v4l2_dev)
1235 {
1236         struct v4l2_subdev *sd;
1237         int ret;
1238
1239         sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1240         if (!sd)
1241                 return -ENOMEM;
1242
1243         v4l2_subdev_init(sd, &fimc_subdev_ops);
1244         sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1245         snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1246
1247         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1248         fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1249         ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1250                                 fimc->vid_cap.sd_pads, 0);
1251         if (ret)
1252                 goto me_err;
1253         ret = v4l2_device_register_subdev(v4l2_dev, sd);
1254         if (ret)
1255                 goto sd_err;
1256
1257         fimc->vid_cap.subdev = sd;
1258         v4l2_set_subdevdata(sd, fimc);
1259         sd->entity.ops = &fimc_sd_media_ops;
1260         return 0;
1261 sd_err:
1262         media_entity_cleanup(&sd->entity);
1263 me_err:
1264         kfree(sd);
1265         return ret;
1266 }
1267
1268 static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1269 {
1270         struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1271
1272         if (!sd)
1273                 return;
1274         media_entity_cleanup(&sd->entity);
1275         v4l2_device_unregister_subdev(sd);
1276         kfree(sd);
1277         sd = NULL;
1278 }
1279
1280 /* Set default format at the sensor and host interface */
1281 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1282 {
1283         struct v4l2_format fmt = {
1284                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1285                 .fmt.pix_mp = {
1286                         .width          = 640,
1287                         .height         = 480,
1288                         .pixelformat    = V4L2_PIX_FMT_YUYV,
1289                         .field          = V4L2_FIELD_NONE,
1290                         .colorspace     = V4L2_COLORSPACE_JPEG,
1291                 },
1292         };
1293
1294         return fimc_capture_set_format(fimc, &fmt);
1295 }
1296
1297 /* fimc->lock must be already initialized */
1298 int fimc_register_capture_device(struct fimc_dev *fimc,
1299                                  struct v4l2_device *v4l2_dev)
1300 {
1301         struct video_device *vfd;
1302         struct fimc_vid_cap *vid_cap;
1303         struct fimc_ctx *ctx;
1304         struct vb2_queue *q;
1305         int ret = -ENOMEM;
1306
1307         ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1308         if (!ctx)
1309                 return -ENOMEM;
1310
1311         ctx->fimc_dev    = fimc;
1312         ctx->in_path     = FIMC_CAMERA;
1313         ctx->out_path    = FIMC_DMA;
1314         ctx->state       = FIMC_CTX_CAP;
1315         ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1316         ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1317
1318         vfd = video_device_alloc();
1319         if (!vfd) {
1320                 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1321                 goto err_vd_alloc;
1322         }
1323
1324         snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1325                  dev_name(&fimc->pdev->dev));
1326
1327         vfd->fops       = &fimc_capture_fops;
1328         vfd->ioctl_ops  = &fimc_capture_ioctl_ops;
1329         vfd->v4l2_dev   = v4l2_dev;
1330         vfd->minor      = -1;
1331         vfd->release    = video_device_release;
1332         vfd->lock       = &fimc->lock;
1333         video_set_drvdata(vfd, fimc);
1334
1335         vid_cap = &fimc->vid_cap;
1336         vid_cap->vfd = vfd;
1337         vid_cap->active_buf_cnt = 0;
1338         vid_cap->reqbufs_count  = 0;
1339         vid_cap->refcnt = 0;
1340
1341         INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1342         INIT_LIST_HEAD(&vid_cap->active_buf_q);
1343         spin_lock_init(&ctx->slock);
1344         vid_cap->ctx = ctx;
1345
1346         q = &fimc->vid_cap.vbq;
1347         memset(q, 0, sizeof(*q));
1348         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1349         q->io_modes = VB2_MMAP | VB2_USERPTR;
1350         q->drv_priv = fimc->vid_cap.ctx;
1351         q->ops = &fimc_capture_qops;
1352         q->mem_ops = &vb2_dma_contig_memops;
1353         q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1354
1355         vb2_queue_init(q);
1356
1357         fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1358         ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1359         if (ret)
1360                 goto err_ent;
1361         ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1362         if (ret)
1363                 goto err_sd_reg;
1364
1365         vfd->ctrl_handler = &ctx->ctrl_handler;
1366         return 0;
1367
1368 err_sd_reg:
1369         media_entity_cleanup(&vfd->entity);
1370 err_ent:
1371         video_device_release(vfd);
1372 err_vd_alloc:
1373         kfree(ctx);
1374         return ret;
1375 }
1376
1377 void fimc_unregister_capture_device(struct fimc_dev *fimc)
1378 {
1379         struct video_device *vfd = fimc->vid_cap.vfd;
1380
1381         if (vfd) {
1382                 media_entity_cleanup(&vfd->entity);
1383                 /* Can also be called if video device was
1384                    not registered */
1385                 video_unregister_device(vfd);
1386         }
1387         fimc_destroy_capture_subdev(fimc);
1388         kfree(fimc->vid_cap.ctx);
1389         fimc->vid_cap.ctx = NULL;
1390 }