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