2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
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.
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>
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>
30 #include "fimc-mdevice.h"
31 #include "fimc-core.h"
34 static int fimc_init_capture(struct fimc_dev
*fimc
)
36 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
37 struct fimc_pipeline
*p
= &fimc
->pipeline
;
38 struct fimc_sensor_info
*sensor
;
42 if (p
->subdevs
[IDX_SENSOR
] == NULL
|| ctx
== NULL
)
44 if (ctx
->s_frame
.fmt
== NULL
)
47 sensor
= v4l2_get_subdev_hostdata(p
->subdevs
[IDX_SENSOR
]);
49 spin_lock_irqsave(&fimc
->slock
, flags
);
50 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
51 fimc_set_yuv_order(ctx
);
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
);
58 ret
= fimc_set_scaler_info(ctx
);
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
, false);
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
);
72 spin_unlock_irqrestore(&fimc
->slock
, flags
);
76 static int fimc_capture_state_cleanup(struct fimc_dev
*fimc
, bool suspend
)
78 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
79 struct fimc_vid_buffer
*buf
;
83 spin_lock_irqsave(&fimc
->slock
, flags
);
84 streaming
= fimc
->state
& (1 << ST_CAPT_ISP_STREAM
);
86 fimc
->state
&= ~(1 << ST_CAPT_RUN
| 1 << ST_CAPT_SHUT
|
87 1 << ST_CAPT_STREAM
| 1 << ST_CAPT_ISP_STREAM
);
89 fimc
->state
|= (1 << ST_CAPT_SUSPENDED
);
91 fimc
->state
&= ~(1 << ST_CAPT_PEND
| 1 << ST_CAPT_SUSPENDED
);
93 /* Release unused buffers */
94 while (!suspend
&& !list_empty(&cap
->pending_buf_q
)) {
95 buf
= fimc_pending_queue_pop(cap
);
96 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
98 /* If suspending put unused buffers onto pending queue */
99 while (!list_empty(&cap
->active_buf_q
)) {
100 buf
= fimc_active_queue_pop(cap
);
102 fimc_pending_queue_add(cap
, buf
);
104 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
110 spin_unlock_irqrestore(&fimc
->slock
, flags
);
113 return fimc_pipeline_s_stream(&fimc
->pipeline
, 0);
118 static int fimc_stop_capture(struct fimc_dev
*fimc
, bool suspend
)
122 if (!fimc_capture_active(fimc
))
125 spin_lock_irqsave(&fimc
->slock
, flags
);
126 set_bit(ST_CAPT_SHUT
, &fimc
->state
);
127 fimc_deactivate_capture(fimc
);
128 spin_unlock_irqrestore(&fimc
->slock
, flags
);
130 wait_event_timeout(fimc
->irq_queue
,
131 !test_bit(ST_CAPT_SHUT
, &fimc
->state
),
132 (2*HZ
/10)); /* 200 ms */
134 return fimc_capture_state_cleanup(fimc
, suspend
);
138 * fimc_capture_config_update - apply the camera interface configuration
140 * To be called from within the interrupt handler with fimc.slock
141 * spinlock held. It updates the camera pixel crop, rotation and
144 static int fimc_capture_config_update(struct fimc_ctx
*ctx
)
146 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
149 if (!test_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
))
152 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
154 ret
= fimc_set_scaler_info(ctx
);
158 fimc_hw_set_prescaler(ctx
);
159 fimc_hw_set_mainscaler(ctx
);
160 fimc_hw_set_target_format(ctx
);
161 fimc_hw_set_rotation(ctx
);
162 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
163 fimc_hw_set_out_dma(ctx
);
164 if (fimc
->variant
->has_alpha
)
165 fimc_hw_set_rgb_alpha(ctx
);
167 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
171 void fimc_capture_irq_handler(struct fimc_dev
*fimc
, int deq_buf
)
173 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
174 struct fimc_vid_buffer
*v_buf
;
178 if (test_and_clear_bit(ST_CAPT_SHUT
, &fimc
->state
)) {
179 wake_up(&fimc
->irq_queue
);
183 if (!list_empty(&cap
->active_buf_q
) &&
184 test_bit(ST_CAPT_RUN
, &fimc
->state
) && deq_buf
) {
185 ktime_get_real_ts(&ts
);
187 v_buf
= fimc_active_queue_pop(cap
);
189 tv
= &v_buf
->vb
.v4l2_buf
.timestamp
;
190 tv
->tv_sec
= ts
.tv_sec
;
191 tv
->tv_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
192 v_buf
->vb
.v4l2_buf
.sequence
= cap
->frame_count
++;
194 vb2_buffer_done(&v_buf
->vb
, VB2_BUF_STATE_DONE
);
197 if (!list_empty(&cap
->pending_buf_q
)) {
199 v_buf
= fimc_pending_queue_pop(cap
);
200 fimc_hw_set_output_addr(fimc
, &v_buf
->paddr
, cap
->buf_index
);
201 v_buf
->index
= cap
->buf_index
;
203 /* Move the buffer to the capture active queue */
204 fimc_active_queue_add(cap
, v_buf
);
206 dbg("next frame: %d, done frame: %d",
207 fimc_hw_get_frame_index(fimc
), v_buf
->index
);
209 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
213 if (cap
->active_buf_cnt
== 0) {
215 clear_bit(ST_CAPT_RUN
, &fimc
->state
);
217 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
220 set_bit(ST_CAPT_RUN
, &fimc
->state
);
223 fimc_capture_config_update(cap
->ctx
);
225 if (cap
->active_buf_cnt
== 1) {
226 fimc_deactivate_capture(fimc
);
227 clear_bit(ST_CAPT_STREAM
, &fimc
->state
);
230 dbg("frame: %d, active_buf_cnt: %d",
231 fimc_hw_get_frame_index(fimc
), cap
->active_buf_cnt
);
235 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
237 struct fimc_ctx
*ctx
= q
->drv_priv
;
238 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
239 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
243 vid_cap
->frame_count
= 0;
245 ret
= fimc_init_capture(fimc
);
249 set_bit(ST_CAPT_PEND
, &fimc
->state
);
251 min_bufs
= fimc
->vid_cap
.reqbufs_count
> 1 ? 2 : 1;
253 if (vid_cap
->active_buf_cnt
>= min_bufs
&&
254 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
255 fimc_activate_capture(ctx
);
257 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
258 fimc_pipeline_s_stream(&fimc
->pipeline
, 1);
263 fimc_capture_state_cleanup(fimc
, false);
267 static int stop_streaming(struct vb2_queue
*q
)
269 struct fimc_ctx
*ctx
= q
->drv_priv
;
270 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
272 if (!fimc_capture_active(fimc
))
275 return fimc_stop_capture(fimc
, false);
278 int fimc_capture_suspend(struct fimc_dev
*fimc
)
280 bool suspend
= fimc_capture_busy(fimc
);
282 int ret
= fimc_stop_capture(fimc
, suspend
);
285 return fimc_pipeline_shutdown(&fimc
->pipeline
);
288 static void buffer_queue(struct vb2_buffer
*vb
);
290 int fimc_capture_resume(struct fimc_dev
*fimc
)
292 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
293 struct fimc_vid_buffer
*buf
;
296 if (!test_and_clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
))
299 INIT_LIST_HEAD(&fimc
->vid_cap
.active_buf_q
);
300 vid_cap
->buf_index
= 0;
301 fimc_pipeline_initialize(&fimc
->pipeline
, &vid_cap
->vfd
->entity
,
303 fimc_init_capture(fimc
);
305 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
307 for (i
= 0; i
< vid_cap
->reqbufs_count
; i
++) {
308 if (list_empty(&vid_cap
->pending_buf_q
))
310 buf
= fimc_pending_queue_pop(vid_cap
);
311 buffer_queue(&buf
->vb
);
317 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*pfmt
,
318 unsigned int *num_buffers
, unsigned int *num_planes
,
319 unsigned int sizes
[], void *allocators
[])
321 const struct v4l2_pix_format_mplane
*pixm
= NULL
;
322 struct fimc_ctx
*ctx
= vq
->drv_priv
;
323 struct fimc_frame
*frame
= &ctx
->d_frame
;
324 struct fimc_fmt
*fmt
= frame
->fmt
;
329 pixm
= &pfmt
->fmt
.pix_mp
;
330 fmt
= fimc_find_format(&pixm
->pixelformat
, NULL
,
331 FMT_FLAGS_CAM
| FMT_FLAGS_M2M
, -1);
332 wh
= pixm
->width
* pixm
->height
;
334 wh
= frame
->f_width
* frame
->f_height
;
340 *num_planes
= fmt
->memplanes
;
342 for (i
= 0; i
< fmt
->memplanes
; i
++) {
343 unsigned int size
= (wh
* fmt
->depth
[i
]) / 8;
345 sizes
[i
] = max(size
, pixm
->plane_fmt
[i
].sizeimage
);
348 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
354 static int buffer_prepare(struct vb2_buffer
*vb
)
356 struct vb2_queue
*vq
= vb
->vb2_queue
;
357 struct fimc_ctx
*ctx
= vq
->drv_priv
;
360 if (ctx
->d_frame
.fmt
== NULL
)
363 for (i
= 0; i
< ctx
->d_frame
.fmt
->memplanes
; i
++) {
364 unsigned long size
= ctx
->d_frame
.payload
[i
];
366 if (vb2_plane_size(vb
, i
) < size
) {
367 v4l2_err(ctx
->fimc_dev
->vid_cap
.vfd
,
368 "User buffer too small (%ld < %ld)\n",
369 vb2_plane_size(vb
, i
), size
);
372 vb2_set_plane_payload(vb
, i
, size
);
378 static void buffer_queue(struct vb2_buffer
*vb
)
380 struct fimc_vid_buffer
*buf
381 = container_of(vb
, struct fimc_vid_buffer
, vb
);
382 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
383 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
384 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
388 spin_lock_irqsave(&fimc
->slock
, flags
);
389 fimc_prepare_addr(ctx
, &buf
->vb
, &ctx
->d_frame
, &buf
->paddr
);
391 if (!test_bit(ST_CAPT_SUSPENDED
, &fimc
->state
) &&
392 !test_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
393 vid_cap
->active_buf_cnt
< FIMC_MAX_OUT_BUFS
) {
394 /* Setup the buffer directly for processing. */
395 int buf_id
= (vid_cap
->reqbufs_count
== 1) ? -1 :
398 fimc_hw_set_output_addr(fimc
, &buf
->paddr
, buf_id
);
399 buf
->index
= vid_cap
->buf_index
;
400 fimc_active_queue_add(vid_cap
, buf
);
402 if (++vid_cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
403 vid_cap
->buf_index
= 0;
405 fimc_pending_queue_add(vid_cap
, buf
);
408 min_bufs
= vid_cap
->reqbufs_count
> 1 ? 2 : 1;
411 if (vb2_is_streaming(&vid_cap
->vbq
) &&
412 vid_cap
->active_buf_cnt
>= min_bufs
&&
413 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
414 fimc_activate_capture(ctx
);
415 spin_unlock_irqrestore(&fimc
->slock
, flags
);
417 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
418 fimc_pipeline_s_stream(&fimc
->pipeline
, 1);
421 spin_unlock_irqrestore(&fimc
->slock
, flags
);
424 static void fimc_lock(struct vb2_queue
*vq
)
426 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
427 mutex_lock(&ctx
->fimc_dev
->lock
);
430 static void fimc_unlock(struct vb2_queue
*vq
)
432 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
433 mutex_unlock(&ctx
->fimc_dev
->lock
);
436 static struct vb2_ops fimc_capture_qops
= {
437 .queue_setup
= queue_setup
,
438 .buf_prepare
= buffer_prepare
,
439 .buf_queue
= buffer_queue
,
440 .wait_prepare
= fimc_unlock
,
441 .wait_finish
= fimc_lock
,
442 .start_streaming
= start_streaming
,
443 .stop_streaming
= stop_streaming
,
447 * fimc_capture_ctrls_create - initialize the control handler
448 * Initialize the capture video node control handler and fill it
449 * with the FIMC controls. Inherit any sensor's controls if the
450 * 'user_subdev_api' flag is false (default behaviour).
451 * This function need to be called with the graph mutex held.
453 int fimc_capture_ctrls_create(struct fimc_dev
*fimc
)
455 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
458 if (WARN_ON(vid_cap
->ctx
== NULL
))
460 if (vid_cap
->ctx
->ctrls_rdy
)
463 ret
= fimc_ctrls_create(vid_cap
->ctx
);
464 if (ret
|| vid_cap
->user_subdev_api
)
467 return v4l2_ctrl_add_handler(&vid_cap
->ctx
->ctrl_handler
,
468 fimc
->pipeline
.subdevs
[IDX_SENSOR
]->ctrl_handler
);
471 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
);
473 static int fimc_capture_open(struct file
*file
)
475 struct fimc_dev
*fimc
= video_drvdata(file
);
476 int ret
= v4l2_fh_open(file
);
481 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
483 /* Return if the corresponding video mem2mem node is already opened. */
484 if (fimc_m2m_active(fimc
))
487 set_bit(ST_CAPT_BUSY
, &fimc
->state
);
488 pm_runtime_get_sync(&fimc
->pdev
->dev
);
490 if (++fimc
->vid_cap
.refcnt
== 1) {
491 ret
= fimc_pipeline_initialize(&fimc
->pipeline
,
492 &fimc
->vid_cap
.vfd
->entity
, true);
494 dev_err(&fimc
->pdev
->dev
,
495 "Video pipeline initialization failed\n");
496 pm_runtime_put_sync(&fimc
->pdev
->dev
);
497 fimc
->vid_cap
.refcnt
--;
498 v4l2_fh_release(file
);
499 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
502 ret
= fimc_capture_ctrls_create(fimc
);
504 if (!ret
&& !fimc
->vid_cap
.user_subdev_api
)
505 ret
= fimc_capture_set_default_format(fimc
);
510 static int fimc_capture_close(struct file
*file
)
512 struct fimc_dev
*fimc
= video_drvdata(file
);
514 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
516 if (--fimc
->vid_cap
.refcnt
== 0) {
517 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
518 fimc_stop_capture(fimc
, false);
519 fimc_pipeline_shutdown(&fimc
->pipeline
);
520 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
523 pm_runtime_put(&fimc
->pdev
->dev
);
525 if (fimc
->vid_cap
.refcnt
== 0) {
526 vb2_queue_release(&fimc
->vid_cap
.vbq
);
527 fimc_ctrls_delete(fimc
->vid_cap
.ctx
);
529 return v4l2_fh_release(file
);
532 static unsigned int fimc_capture_poll(struct file
*file
,
533 struct poll_table_struct
*wait
)
535 struct fimc_dev
*fimc
= video_drvdata(file
);
537 return vb2_poll(&fimc
->vid_cap
.vbq
, file
, wait
);
540 static int fimc_capture_mmap(struct file
*file
, struct vm_area_struct
*vma
)
542 struct fimc_dev
*fimc
= video_drvdata(file
);
544 return vb2_mmap(&fimc
->vid_cap
.vbq
, vma
);
547 static const struct v4l2_file_operations fimc_capture_fops
= {
548 .owner
= THIS_MODULE
,
549 .open
= fimc_capture_open
,
550 .release
= fimc_capture_close
,
551 .poll
= fimc_capture_poll
,
552 .unlocked_ioctl
= video_ioctl2
,
553 .mmap
= fimc_capture_mmap
,
557 * Format and crop negotiation helpers
560 static struct fimc_fmt
*fimc_capture_try_format(struct fimc_ctx
*ctx
,
561 u32
*width
, u32
*height
,
562 u32
*code
, u32
*fourcc
, int pad
)
564 bool rotation
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
565 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
566 struct samsung_fimc_variant
*var
= fimc
->variant
;
567 struct fimc_pix_limit
*pl
= var
->pix_limit
;
568 struct fimc_frame
*dst
= &ctx
->d_frame
;
569 u32 depth
, min_w
, max_w
, min_h
, align_h
= 3;
570 u32 mask
= FMT_FLAGS_CAM
;
571 struct fimc_fmt
*ffmt
;
573 /* Color conversion from/to JPEG is not supported */
574 if (code
&& ctx
->s_frame
.fmt
&& pad
== FIMC_SD_PAD_SOURCE
&&
575 fimc_fmt_is_jpeg(ctx
->s_frame
.fmt
->color
))
576 *code
= V4L2_MBUS_FMT_JPEG_1X8
;
578 if (fourcc
&& *fourcc
!= V4L2_PIX_FMT_JPEG
&& pad
!= FIMC_SD_PAD_SINK
)
579 mask
|= FMT_FLAGS_M2M
;
581 ffmt
= fimc_find_format(fourcc
, code
, mask
, 0);
585 *code
= ffmt
->mbus_code
;
587 *fourcc
= ffmt
->fourcc
;
589 if (pad
== FIMC_SD_PAD_SINK
) {
590 max_w
= fimc_fmt_is_jpeg(ffmt
->color
) ?
591 pl
->scaler_dis_w
: pl
->scaler_en_w
;
592 /* Apply the camera input interface pixel constraints */
593 v4l_bound_align_image(width
, max_t(u32
, *width
, 32), max_w
, 4,
594 height
, max_t(u32
, *height
, 32),
595 FIMC_CAMIF_MAX_HEIGHT
,
596 fimc_fmt_is_jpeg(ffmt
->color
) ? 3 : 1,
600 /* Can't scale or crop in transparent (JPEG) transfer mode */
601 if (fimc_fmt_is_jpeg(ffmt
->color
)) {
602 *width
= ctx
->s_frame
.f_width
;
603 *height
= ctx
->s_frame
.f_height
;
606 /* Apply the scaler and the output DMA constraints */
607 max_w
= rotation
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
;
608 min_w
= ctx
->state
& FIMC_DST_CROP
? dst
->width
: var
->min_out_pixsize
;
609 min_h
= ctx
->state
& FIMC_DST_CROP
? dst
->height
: var
->min_out_pixsize
;
610 if (var
->min_vsize_align
== 1 && !rotation
)
611 align_h
= fimc_fmt_is_rgb(ffmt
->color
) ? 0 : 1;
613 depth
= fimc_get_format_depth(ffmt
);
614 v4l_bound_align_image(width
, min_w
, max_w
,
615 ffs(var
->min_out_pixsize
) - 1,
616 height
, min_h
, FIMC_CAMIF_MAX_HEIGHT
,
618 64/(ALIGN(depth
, 8)));
620 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
621 pad
, code
? *code
: 0, *width
, *height
,
622 dst
->f_width
, dst
->f_height
);
627 static void fimc_capture_try_crop(struct fimc_ctx
*ctx
, struct v4l2_rect
*r
,
630 bool rotate
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
631 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
632 struct samsung_fimc_variant
*var
= fimc
->variant
;
633 struct fimc_pix_limit
*pl
= var
->pix_limit
;
634 struct fimc_frame
*sink
= &ctx
->s_frame
;
635 u32 max_w
, max_h
, min_w
= 0, min_h
= 0, min_sz
;
636 u32 align_sz
= 0, align_h
= 4;
637 u32 max_sc_h
, max_sc_v
;
639 /* In JPEG transparent transfer mode cropping is not supported */
640 if (fimc_fmt_is_jpeg(ctx
->d_frame
.fmt
->color
)) {
641 r
->width
= sink
->f_width
;
642 r
->height
= sink
->f_height
;
643 r
->left
= r
->top
= 0;
646 if (pad
== FIMC_SD_PAD_SOURCE
) {
647 if (ctx
->rotation
!= 90 && ctx
->rotation
!= 270)
649 max_sc_h
= min(SCALER_MAX_HRATIO
, 1 << (ffs(sink
->width
) - 3));
650 max_sc_v
= min(SCALER_MAX_VRATIO
, 1 << (ffs(sink
->height
) - 1));
651 min_sz
= var
->min_out_pixsize
;
653 u32 depth
= fimc_get_format_depth(sink
->fmt
);
654 align_sz
= 64/ALIGN(depth
, 8);
655 min_sz
= var
->min_inp_pixsize
;
656 min_w
= min_h
= min_sz
;
657 max_sc_h
= max_sc_v
= 1;
660 * For the crop rectangle at source pad the following constraints
662 * - it must fit in the sink pad format rectangle (f_width/f_height);
663 * - maximum downscaling ratio is 64;
664 * - maximum crop size depends if the rotator is used or not;
665 * - the sink pad format width/height must be 4 multiple of the
666 * prescaler ratios determined by sink pad size and source pad crop,
667 * the prescaler ratio is returned by fimc_get_scaler_factor().
670 rotate
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
,
671 rotate
? sink
->f_height
: sink
->f_width
);
672 max_h
= min_t(u32
, FIMC_CAMIF_MAX_HEIGHT
, sink
->f_height
);
673 if (pad
== FIMC_SD_PAD_SOURCE
) {
674 min_w
= min_t(u32
, max_w
, sink
->f_width
/ max_sc_h
);
675 min_h
= min_t(u32
, max_h
, sink
->f_height
/ max_sc_v
);
677 swap(max_sc_h
, max_sc_v
);
681 v4l_bound_align_image(&r
->width
, min_w
, max_w
, ffs(min_sz
) - 1,
682 &r
->height
, min_h
, max_h
, align_h
,
684 /* Adjust left/top if cropping rectangle is out of bounds */
685 r
->left
= clamp_t(u32
, r
->left
, 0, sink
->f_width
- r
->width
);
686 r
->top
= clamp_t(u32
, r
->top
, 0, sink
->f_height
- r
->height
);
687 r
->left
= round_down(r
->left
, var
->hor_offs_align
);
689 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
690 pad
, r
->left
, r
->top
, r
->width
, r
->height
,
691 sink
->f_width
, sink
->f_height
);
695 * The video node ioctl operations
697 static int fimc_vidioc_querycap_capture(struct file
*file
, void *priv
,
698 struct v4l2_capability
*cap
)
700 struct fimc_dev
*fimc
= video_drvdata(file
);
702 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
703 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
704 cap
->bus_info
[0] = 0;
705 cap
->capabilities
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE_MPLANE
;
710 static int fimc_cap_enum_fmt_mplane(struct file
*file
, void *priv
,
711 struct v4l2_fmtdesc
*f
)
713 struct fimc_fmt
*fmt
;
715 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
| FMT_FLAGS_M2M
,
719 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
720 f
->pixelformat
= fmt
->fourcc
;
721 if (fmt
->fourcc
== V4L2_MBUS_FMT_JPEG_1X8
)
722 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
727 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
729 * @ctx: FIMC capture context
730 * @tfmt: media bus format to try/set on subdevs
731 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
732 * @set: true to set format on subdevs, false to try only
734 static int fimc_pipeline_try_format(struct fimc_ctx
*ctx
,
735 struct v4l2_mbus_framefmt
*tfmt
,
736 struct fimc_fmt
**fmt_id
,
739 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
740 struct v4l2_subdev
*sd
= fimc
->pipeline
.subdevs
[IDX_SENSOR
];
741 struct v4l2_subdev
*csis
= fimc
->pipeline
.subdevs
[IDX_CSIS
];
742 struct v4l2_subdev_format sfmt
;
743 struct v4l2_mbus_framefmt
*mf
= &sfmt
.format
;
744 struct fimc_fmt
*ffmt
= NULL
;
747 if (WARN_ON(!sd
|| !tfmt
))
750 memset(&sfmt
, 0, sizeof(sfmt
));
753 sfmt
.which
= set
? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY
;
755 ffmt
= fimc_find_format(NULL
, mf
->code
!= 0 ? &mf
->code
: NULL
,
759 * Notify user-space if common pixel code for
760 * host and sensor does not exist.
764 mf
->code
= tfmt
->code
= ffmt
->mbus_code
;
766 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, NULL
, &sfmt
);
769 if (mf
->code
!= tfmt
->code
) {
773 if (mf
->width
!= tfmt
->width
|| mf
->height
!= tfmt
->height
) {
774 u32 fcc
= ffmt
->fourcc
;
775 tfmt
->width
= mf
->width
;
776 tfmt
->height
= mf
->height
;
777 ffmt
= fimc_capture_try_format(ctx
,
778 &tfmt
->width
, &tfmt
->height
,
779 NULL
, &fcc
, FIMC_SD_PAD_SOURCE
);
780 if (ffmt
&& ffmt
->mbus_code
)
781 mf
->code
= ffmt
->mbus_code
;
782 if (mf
->width
!= tfmt
->width
||
783 mf
->height
!= tfmt
->height
)
785 tfmt
->code
= mf
->code
;
788 ret
= v4l2_subdev_call(csis
, pad
, set_fmt
, NULL
, &sfmt
);
790 if (mf
->code
== tfmt
->code
&&
791 mf
->width
== tfmt
->width
&& mf
->height
== tfmt
->height
)
799 dbg("code: 0x%x, %dx%d, %p", mf
->code
, mf
->width
, mf
->height
, ffmt
);
803 static int fimc_cap_g_fmt_mplane(struct file
*file
, void *fh
,
804 struct v4l2_format
*f
)
806 struct fimc_dev
*fimc
= video_drvdata(file
);
807 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
809 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
812 return fimc_fill_format(&ctx
->d_frame
, f
);
815 static int fimc_cap_try_fmt_mplane(struct file
*file
, void *fh
,
816 struct v4l2_format
*f
)
818 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
819 struct fimc_dev
*fimc
= video_drvdata(file
);
820 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
821 struct v4l2_mbus_framefmt mf
;
822 struct fimc_fmt
*ffmt
= NULL
;
824 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
827 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
828 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
829 NULL
, &pix
->pixelformat
,
831 ctx
->s_frame
.f_width
= pix
->width
;
832 ctx
->s_frame
.f_height
= pix
->height
;
834 ffmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
835 NULL
, &pix
->pixelformat
,
840 if (!fimc
->vid_cap
.user_subdev_api
) {
841 mf
.width
= pix
->width
;
842 mf
.height
= pix
->height
;
843 mf
.code
= ffmt
->mbus_code
;
844 fimc_md_graph_lock(fimc
);
845 fimc_pipeline_try_format(ctx
, &mf
, &ffmt
, false);
846 fimc_md_graph_unlock(fimc
);
848 pix
->width
= mf
.width
;
849 pix
->height
= mf
.height
;
851 pix
->pixelformat
= ffmt
->fourcc
;
854 fimc_adjust_mplane_format(ffmt
, pix
->width
, pix
->height
, pix
);
858 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx
*ctx
, bool jpeg
)
860 ctx
->scaler
.enabled
= !jpeg
;
861 fimc_ctrls_activate(ctx
, !jpeg
);
864 set_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
866 clear_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
869 static int fimc_capture_set_format(struct fimc_dev
*fimc
, struct v4l2_format
*f
)
871 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
872 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
873 struct v4l2_mbus_framefmt
*mf
= &fimc
->vid_cap
.mf
;
874 struct fimc_frame
*ff
= &ctx
->d_frame
;
875 struct fimc_fmt
*s_fmt
= NULL
;
878 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
880 if (vb2_is_busy(&fimc
->vid_cap
.vbq
))
883 /* Pre-configure format at camera interface input, for JPEG only */
884 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
885 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
886 NULL
, &pix
->pixelformat
,
888 ctx
->s_frame
.f_width
= pix
->width
;
889 ctx
->s_frame
.f_height
= pix
->height
;
891 /* Try the format at the scaler and the DMA output */
892 ff
->fmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
893 NULL
, &pix
->pixelformat
,
898 /* Update RGB Alpha control state and value range */
899 fimc_alpha_ctrl_update(ctx
);
901 /* Try to match format at the host and the sensor */
902 if (!fimc
->vid_cap
.user_subdev_api
) {
903 mf
->code
= ff
->fmt
->mbus_code
;
904 mf
->width
= pix
->width
;
905 mf
->height
= pix
->height
;
907 fimc_md_graph_lock(fimc
);
908 ret
= fimc_pipeline_try_format(ctx
, mf
, &s_fmt
, true);
909 fimc_md_graph_unlock(fimc
);
912 pix
->width
= mf
->width
;
913 pix
->height
= mf
->height
;
915 fimc_adjust_mplane_format(ff
->fmt
, pix
->width
, pix
->height
, pix
);
916 for (i
= 0; i
< ff
->fmt
->colplanes
; i
++)
918 (pix
->width
* pix
->height
* ff
->fmt
->depth
[i
]) / 8;
920 set_frame_bounds(ff
, pix
->width
, pix
->height
);
921 /* Reset the composition rectangle if not yet configured */
922 if (!(ctx
->state
& FIMC_DST_CROP
))
923 set_frame_crop(ff
, 0, 0, pix
->width
, pix
->height
);
925 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ff
->fmt
->color
));
927 /* Reset cropping and set format at the camera interface input */
928 if (!fimc
->vid_cap
.user_subdev_api
) {
929 ctx
->s_frame
.fmt
= s_fmt
;
930 set_frame_bounds(&ctx
->s_frame
, pix
->width
, pix
->height
);
931 set_frame_crop(&ctx
->s_frame
, 0, 0, pix
->width
, pix
->height
);
937 static int fimc_cap_s_fmt_mplane(struct file
*file
, void *priv
,
938 struct v4l2_format
*f
)
940 struct fimc_dev
*fimc
= video_drvdata(file
);
942 return fimc_capture_set_format(fimc
, f
);
945 static int fimc_cap_enum_input(struct file
*file
, void *priv
,
946 struct v4l2_input
*i
)
948 struct fimc_dev
*fimc
= video_drvdata(file
);
949 struct v4l2_subdev
*sd
= fimc
->pipeline
.subdevs
[IDX_SENSOR
];
954 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
956 strlcpy(i
->name
, sd
->name
, sizeof(i
->name
));
960 static int fimc_cap_s_input(struct file
*file
, void *priv
, unsigned int i
)
962 return i
== 0 ? i
: -EINVAL
;
965 static int fimc_cap_g_input(struct file
*file
, void *priv
, unsigned int *i
)
972 * fimc_pipeline_validate - check for formats inconsistencies
973 * between source and sink pad of each link
975 * Return 0 if all formats match or -EPIPE otherwise.
977 static int fimc_pipeline_validate(struct fimc_dev
*fimc
)
979 struct v4l2_subdev_format sink_fmt
, src_fmt
;
980 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
981 struct v4l2_subdev
*sd
;
982 struct media_pad
*pad
;
985 /* Start with the video capture node pad */
986 pad
= media_entity_remote_source(&vid_cap
->vd_pad
);
989 /* FIMC.{N} subdevice */
990 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
993 /* Retrieve format at the sink pad */
994 pad
= &sd
->entity
.pads
[0];
995 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
997 /* Don't call FIMC subdev operation to avoid nested locking */
998 if (sd
== &fimc
->vid_cap
.subdev
) {
999 struct fimc_frame
*ff
= &vid_cap
->ctx
->s_frame
;
1000 sink_fmt
.format
.width
= ff
->f_width
;
1001 sink_fmt
.format
.height
= ff
->f_height
;
1002 sink_fmt
.format
.code
= ff
->fmt
? ff
->fmt
->mbus_code
: 0;
1004 sink_fmt
.pad
= pad
->index
;
1005 sink_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
1006 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &sink_fmt
);
1007 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
1010 /* Retrieve format at the source pad */
1011 pad
= media_entity_remote_source(pad
);
1013 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1016 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
1017 src_fmt
.pad
= pad
->index
;
1018 src_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
1019 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &src_fmt
);
1020 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
1023 if (src_fmt
.format
.width
!= sink_fmt
.format
.width
||
1024 src_fmt
.format
.height
!= sink_fmt
.format
.height
||
1025 src_fmt
.format
.code
!= sink_fmt
.format
.code
)
1031 static int fimc_cap_streamon(struct file
*file
, void *priv
,
1032 enum v4l2_buf_type type
)
1034 struct fimc_dev
*fimc
= video_drvdata(file
);
1035 struct fimc_pipeline
*p
= &fimc
->pipeline
;
1038 if (fimc_capture_active(fimc
))
1041 media_entity_pipeline_start(&p
->subdevs
[IDX_SENSOR
]->entity
,
1044 if (fimc
->vid_cap
.user_subdev_api
) {
1045 ret
= fimc_pipeline_validate(fimc
);
1049 return vb2_streamon(&fimc
->vid_cap
.vbq
, type
);
1052 static int fimc_cap_streamoff(struct file
*file
, void *priv
,
1053 enum v4l2_buf_type type
)
1055 struct fimc_dev
*fimc
= video_drvdata(file
);
1056 struct v4l2_subdev
*sd
= fimc
->pipeline
.subdevs
[IDX_SENSOR
];
1059 ret
= vb2_streamoff(&fimc
->vid_cap
.vbq
, type
);
1061 media_entity_pipeline_stop(&sd
->entity
);
1065 static int fimc_cap_reqbufs(struct file
*file
, void *priv
,
1066 struct v4l2_requestbuffers
*reqbufs
)
1068 struct fimc_dev
*fimc
= video_drvdata(file
);
1069 int ret
= vb2_reqbufs(&fimc
->vid_cap
.vbq
, reqbufs
);
1072 fimc
->vid_cap
.reqbufs_count
= reqbufs
->count
;
1076 static int fimc_cap_querybuf(struct file
*file
, void *priv
,
1077 struct v4l2_buffer
*buf
)
1079 struct fimc_dev
*fimc
= video_drvdata(file
);
1081 return vb2_querybuf(&fimc
->vid_cap
.vbq
, buf
);
1084 static int fimc_cap_qbuf(struct file
*file
, void *priv
,
1085 struct v4l2_buffer
*buf
)
1087 struct fimc_dev
*fimc
= video_drvdata(file
);
1089 return vb2_qbuf(&fimc
->vid_cap
.vbq
, buf
);
1092 static int fimc_cap_dqbuf(struct file
*file
, void *priv
,
1093 struct v4l2_buffer
*buf
)
1095 struct fimc_dev
*fimc
= video_drvdata(file
);
1097 return vb2_dqbuf(&fimc
->vid_cap
.vbq
, buf
, file
->f_flags
& O_NONBLOCK
);
1100 static int fimc_cap_create_bufs(struct file
*file
, void *priv
,
1101 struct v4l2_create_buffers
*create
)
1103 struct fimc_dev
*fimc
= video_drvdata(file
);
1105 return vb2_create_bufs(&fimc
->vid_cap
.vbq
, create
);
1108 static int fimc_cap_prepare_buf(struct file
*file
, void *priv
,
1109 struct v4l2_buffer
*b
)
1111 struct fimc_dev
*fimc
= video_drvdata(file
);
1113 return vb2_prepare_buf(&fimc
->vid_cap
.vbq
, b
);
1116 static int fimc_cap_g_selection(struct file
*file
, void *fh
,
1117 struct v4l2_selection
*s
)
1119 struct fimc_dev
*fimc
= video_drvdata(file
);
1120 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1121 struct fimc_frame
*f
= &ctx
->s_frame
;
1123 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1126 switch (s
->target
) {
1127 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1128 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1130 case V4L2_SEL_TGT_CROP_BOUNDS
:
1131 case V4L2_SEL_TGT_CROP_DEFAULT
:
1134 s
->r
.width
= f
->o_width
;
1135 s
->r
.height
= f
->o_height
;
1138 case V4L2_SEL_TGT_COMPOSE_ACTIVE
:
1140 case V4L2_SEL_TGT_CROP_ACTIVE
:
1141 s
->r
.left
= f
->offs_h
;
1142 s
->r
.top
= f
->offs_v
;
1143 s
->r
.width
= f
->width
;
1144 s
->r
.height
= f
->height
;
1151 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1152 int enclosed_rectangle(struct v4l2_rect
*a
, struct v4l2_rect
*b
)
1154 if (a
->left
< b
->left
|| a
->top
< b
->top
)
1156 if (a
->left
+ a
->width
> b
->left
+ b
->width
)
1158 if (a
->top
+ a
->height
> b
->top
+ b
->height
)
1164 static int fimc_cap_s_selection(struct file
*file
, void *fh
,
1165 struct v4l2_selection
*s
)
1167 struct fimc_dev
*fimc
= video_drvdata(file
);
1168 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1169 struct v4l2_rect rect
= s
->r
;
1170 struct fimc_frame
*f
;
1171 unsigned long flags
;
1174 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1177 switch (s
->target
) {
1178 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1179 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1180 case V4L2_SEL_TGT_COMPOSE_ACTIVE
:
1182 pad
= FIMC_SD_PAD_SOURCE
;
1184 case V4L2_SEL_TGT_CROP_BOUNDS
:
1185 case V4L2_SEL_TGT_CROP_DEFAULT
:
1186 case V4L2_SEL_TGT_CROP_ACTIVE
:
1188 pad
= FIMC_SD_PAD_SINK
;
1194 fimc_capture_try_crop(ctx
, &rect
, pad
);
1196 if (s
->flags
& V4L2_SEL_FLAG_LE
&&
1197 !enclosed_rectangle(&rect
, &s
->r
))
1200 if (s
->flags
& V4L2_SEL_FLAG_GE
&&
1201 !enclosed_rectangle(&s
->r
, &rect
))
1205 spin_lock_irqsave(&fimc
->slock
, flags
);
1206 set_frame_crop(f
, s
->r
.left
, s
->r
.top
, s
->r
.width
,
1208 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1210 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1214 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops
= {
1215 .vidioc_querycap
= fimc_vidioc_querycap_capture
,
1217 .vidioc_enum_fmt_vid_cap_mplane
= fimc_cap_enum_fmt_mplane
,
1218 .vidioc_try_fmt_vid_cap_mplane
= fimc_cap_try_fmt_mplane
,
1219 .vidioc_s_fmt_vid_cap_mplane
= fimc_cap_s_fmt_mplane
,
1220 .vidioc_g_fmt_vid_cap_mplane
= fimc_cap_g_fmt_mplane
,
1222 .vidioc_reqbufs
= fimc_cap_reqbufs
,
1223 .vidioc_querybuf
= fimc_cap_querybuf
,
1225 .vidioc_qbuf
= fimc_cap_qbuf
,
1226 .vidioc_dqbuf
= fimc_cap_dqbuf
,
1228 .vidioc_prepare_buf
= fimc_cap_prepare_buf
,
1229 .vidioc_create_bufs
= fimc_cap_create_bufs
,
1231 .vidioc_streamon
= fimc_cap_streamon
,
1232 .vidioc_streamoff
= fimc_cap_streamoff
,
1234 .vidioc_g_selection
= fimc_cap_g_selection
,
1235 .vidioc_s_selection
= fimc_cap_s_selection
,
1237 .vidioc_enum_input
= fimc_cap_enum_input
,
1238 .vidioc_s_input
= fimc_cap_s_input
,
1239 .vidioc_g_input
= fimc_cap_g_input
,
1242 /* Capture subdev media entity operations */
1243 static int fimc_link_setup(struct media_entity
*entity
,
1244 const struct media_pad
*local
,
1245 const struct media_pad
*remote
, u32 flags
)
1247 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1248 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1250 if (media_entity_type(remote
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1253 if (WARN_ON(fimc
== NULL
))
1256 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1257 local
->entity
->name
, remote
->entity
->name
, flags
,
1258 fimc
->vid_cap
.input
);
1260 if (flags
& MEDIA_LNK_FL_ENABLED
) {
1261 if (fimc
->vid_cap
.input
!= 0)
1263 fimc
->vid_cap
.input
= sd
->grp_id
;
1267 fimc
->vid_cap
.input
= 0;
1271 static const struct media_entity_operations fimc_sd_media_ops
= {
1272 .link_setup
= fimc_link_setup
,
1276 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1277 * @sd: pointer to a subdev generating the notification
1278 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1279 * @arg: pointer to an u32 type integer that stores the frame payload value
1281 * The End Of Frame notification sent by sensor subdev in its still capture
1282 * mode. If there is only a single VSYNC generated by the sensor at the
1283 * beginning of a frame transmission, FIMC does not issue the LastIrq
1284 * (end of frame) interrupt. And this notification is used to complete the
1285 * frame capture and returning a buffer to user-space. Subdev drivers should
1286 * call this notification from their last 'End of frame capture' interrupt.
1288 void fimc_sensor_notify(struct v4l2_subdev
*sd
, unsigned int notification
,
1291 struct fimc_sensor_info
*sensor
;
1292 struct fimc_vid_buffer
*buf
;
1293 struct fimc_md
*fmd
;
1294 struct fimc_dev
*fimc
;
1295 unsigned long flags
;
1300 sensor
= v4l2_get_subdev_hostdata(sd
);
1301 fmd
= entity_to_fimc_mdev(&sd
->entity
);
1303 spin_lock_irqsave(&fmd
->slock
, flags
);
1304 fimc
= sensor
? sensor
->host
: NULL
;
1306 if (fimc
&& arg
&& notification
== S5P_FIMC_TX_END_NOTIFY
&&
1307 test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
1308 unsigned long irq_flags
;
1309 spin_lock_irqsave(&fimc
->slock
, irq_flags
);
1310 if (!list_empty(&fimc
->vid_cap
.active_buf_q
)) {
1311 buf
= list_entry(fimc
->vid_cap
.active_buf_q
.next
,
1312 struct fimc_vid_buffer
, list
);
1313 vb2_set_plane_payload(&buf
->vb
, 0, *((u32
*)arg
));
1315 fimc_capture_irq_handler(fimc
, 1);
1316 fimc_deactivate_capture(fimc
);
1317 spin_unlock_irqrestore(&fimc
->slock
, irq_flags
);
1319 spin_unlock_irqrestore(&fmd
->slock
, flags
);
1322 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev
*sd
,
1323 struct v4l2_subdev_fh
*fh
,
1324 struct v4l2_subdev_mbus_code_enum
*code
)
1326 struct fimc_fmt
*fmt
;
1328 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, code
->index
);
1331 code
->code
= fmt
->mbus_code
;
1335 static int fimc_subdev_get_fmt(struct v4l2_subdev
*sd
,
1336 struct v4l2_subdev_fh
*fh
,
1337 struct v4l2_subdev_format
*fmt
)
1339 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1340 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1341 struct v4l2_mbus_framefmt
*mf
;
1342 struct fimc_frame
*ff
;
1344 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1345 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1350 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1351 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
? &ctx
->s_frame
: &ctx
->d_frame
;
1353 mutex_lock(&fimc
->lock
);
1354 /* The pixel code is same on both input and output pad */
1355 if (!WARN_ON(ctx
->s_frame
.fmt
== NULL
))
1356 mf
->code
= ctx
->s_frame
.fmt
->mbus_code
;
1357 mf
->width
= ff
->f_width
;
1358 mf
->height
= ff
->f_height
;
1359 mutex_unlock(&fimc
->lock
);
1364 static int fimc_subdev_set_fmt(struct v4l2_subdev
*sd
,
1365 struct v4l2_subdev_fh
*fh
,
1366 struct v4l2_subdev_format
*fmt
)
1368 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1369 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1370 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1371 struct fimc_frame
*ff
;
1372 struct fimc_fmt
*ffmt
;
1374 dbg("pad%d: code: 0x%x, %dx%d",
1375 fmt
->pad
, mf
->code
, mf
->width
, mf
->height
);
1377 if (fmt
->pad
== FIMC_SD_PAD_SOURCE
&&
1378 vb2_is_busy(&fimc
->vid_cap
.vbq
))
1381 mutex_lock(&fimc
->lock
);
1382 ffmt
= fimc_capture_try_format(ctx
, &mf
->width
, &mf
->height
,
1383 &mf
->code
, NULL
, fmt
->pad
);
1384 mutex_unlock(&fimc
->lock
);
1385 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1387 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1388 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1392 /* Update RGB Alpha control state and value range */
1393 fimc_alpha_ctrl_update(ctx
);
1395 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ffmt
->color
));
1397 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
?
1398 &ctx
->s_frame
: &ctx
->d_frame
;
1400 mutex_lock(&fimc
->lock
);
1401 set_frame_bounds(ff
, mf
->width
, mf
->height
);
1402 fimc
->vid_cap
.mf
= *mf
;
1405 /* Reset the crop rectangle if required. */
1406 if (!(fmt
->pad
== FIMC_SD_PAD_SOURCE
&& (ctx
->state
& FIMC_DST_CROP
)))
1407 set_frame_crop(ff
, 0, 0, mf
->width
, mf
->height
);
1409 if (fmt
->pad
== FIMC_SD_PAD_SINK
)
1410 ctx
->state
&= ~FIMC_DST_CROP
;
1411 mutex_unlock(&fimc
->lock
);
1415 static int fimc_subdev_get_crop(struct v4l2_subdev
*sd
,
1416 struct v4l2_subdev_fh
*fh
,
1417 struct v4l2_subdev_crop
*crop
)
1419 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1420 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1421 struct v4l2_rect
*r
= &crop
->rect
;
1422 struct fimc_frame
*ff
;
1424 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1425 crop
->rect
= *v4l2_subdev_get_try_crop(fh
, crop
->pad
);
1428 ff
= crop
->pad
== FIMC_SD_PAD_SINK
?
1429 &ctx
->s_frame
: &ctx
->d_frame
;
1431 mutex_lock(&fimc
->lock
);
1432 r
->left
= ff
->offs_h
;
1433 r
->top
= ff
->offs_v
;
1434 r
->width
= ff
->width
;
1435 r
->height
= ff
->height
;
1436 mutex_unlock(&fimc
->lock
);
1438 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1439 ff
, crop
->pad
, r
->left
, r
->top
, r
->width
, r
->height
,
1440 ff
->f_width
, ff
->f_height
);
1445 static int fimc_subdev_set_crop(struct v4l2_subdev
*sd
,
1446 struct v4l2_subdev_fh
*fh
,
1447 struct v4l2_subdev_crop
*crop
)
1449 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1450 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1451 struct v4l2_rect
*r
= &crop
->rect
;
1452 struct fimc_frame
*ff
;
1453 unsigned long flags
;
1455 dbg("(%d,%d)/%dx%d", r
->left
, r
->top
, r
->width
, r
->height
);
1457 ff
= crop
->pad
== FIMC_SD_PAD_SOURCE
?
1458 &ctx
->d_frame
: &ctx
->s_frame
;
1460 mutex_lock(&fimc
->lock
);
1461 fimc_capture_try_crop(ctx
, r
, crop
->pad
);
1463 if (crop
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1464 mutex_unlock(&fimc
->lock
);
1465 *v4l2_subdev_get_try_crop(fh
, crop
->pad
) = *r
;
1468 spin_lock_irqsave(&fimc
->slock
, flags
);
1469 set_frame_crop(ff
, r
->left
, r
->top
, r
->width
, r
->height
);
1470 if (crop
->pad
== FIMC_SD_PAD_SOURCE
)
1471 ctx
->state
|= FIMC_DST_CROP
;
1473 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1474 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1476 dbg("pad%d: (%d,%d)/%dx%d", crop
->pad
, r
->left
, r
->top
,
1477 r
->width
, r
->height
);
1479 mutex_unlock(&fimc
->lock
);
1483 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops
= {
1484 .enum_mbus_code
= fimc_subdev_enum_mbus_code
,
1485 .get_fmt
= fimc_subdev_get_fmt
,
1486 .set_fmt
= fimc_subdev_set_fmt
,
1487 .get_crop
= fimc_subdev_get_crop
,
1488 .set_crop
= fimc_subdev_set_crop
,
1491 static struct v4l2_subdev_ops fimc_subdev_ops
= {
1492 .pad
= &fimc_subdev_pad_ops
,
1495 /* Set default format at the sensor and host interface */
1496 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
)
1498 struct v4l2_format fmt
= {
1499 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
,
1503 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1504 .field
= V4L2_FIELD_NONE
,
1505 .colorspace
= V4L2_COLORSPACE_JPEG
,
1509 return fimc_capture_set_format(fimc
, &fmt
);
1512 /* fimc->lock must be already initialized */
1513 static int fimc_register_capture_device(struct fimc_dev
*fimc
,
1514 struct v4l2_device
*v4l2_dev
)
1516 struct video_device
*vfd
;
1517 struct fimc_vid_cap
*vid_cap
;
1518 struct fimc_ctx
*ctx
;
1519 struct vb2_queue
*q
;
1522 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1526 ctx
->fimc_dev
= fimc
;
1527 ctx
->in_path
= FIMC_IO_CAMERA
;
1528 ctx
->out_path
= FIMC_IO_DMA
;
1529 ctx
->state
= FIMC_CTX_CAP
;
1530 ctx
->s_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1531 ctx
->d_frame
.fmt
= ctx
->s_frame
.fmt
;
1533 vfd
= video_device_alloc();
1535 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1539 snprintf(vfd
->name
, sizeof(vfd
->name
), "fimc.%d.capture", fimc
->id
);
1541 vfd
->fops
= &fimc_capture_fops
;
1542 vfd
->ioctl_ops
= &fimc_capture_ioctl_ops
;
1543 vfd
->v4l2_dev
= v4l2_dev
;
1545 vfd
->release
= video_device_release
;
1546 vfd
->lock
= &fimc
->lock
;
1547 /* Locking in file operations other than ioctl should be done
1548 by the driver, not the V4L2 core.
1549 This driver needs auditing so that this flag can be removed. */
1550 set_bit(V4L2_FL_LOCK_ALL_FOPS
, &vfd
->flags
);
1551 video_set_drvdata(vfd
, fimc
);
1553 vid_cap
= &fimc
->vid_cap
;
1555 vid_cap
->active_buf_cnt
= 0;
1556 vid_cap
->reqbufs_count
= 0;
1557 vid_cap
->refcnt
= 0;
1559 INIT_LIST_HEAD(&vid_cap
->pending_buf_q
);
1560 INIT_LIST_HEAD(&vid_cap
->active_buf_q
);
1563 q
= &fimc
->vid_cap
.vbq
;
1564 memset(q
, 0, sizeof(*q
));
1565 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1566 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1567 q
->drv_priv
= fimc
->vid_cap
.ctx
;
1568 q
->ops
= &fimc_capture_qops
;
1569 q
->mem_ops
= &vb2_dma_contig_memops
;
1570 q
->buf_struct_size
= sizeof(struct fimc_vid_buffer
);
1574 vid_cap
->vd_pad
.flags
= MEDIA_PAD_FL_SINK
;
1575 ret
= media_entity_init(&vfd
->entity
, 1, &vid_cap
->vd_pad
, 0);
1579 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
1583 v4l2_info(v4l2_dev
, "Registered %s as /dev/%s\n",
1584 vfd
->name
, video_device_node_name(vfd
));
1586 vfd
->ctrl_handler
= &ctx
->ctrl_handler
;
1590 media_entity_cleanup(&vfd
->entity
);
1592 video_device_release(vfd
);
1598 static int fimc_capture_subdev_registered(struct v4l2_subdev
*sd
)
1600 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1603 ret
= fimc_register_m2m_device(fimc
, sd
->v4l2_dev
);
1607 ret
= fimc_register_capture_device(fimc
, sd
->v4l2_dev
);
1609 fimc_unregister_m2m_device(fimc
);
1614 static void fimc_capture_subdev_unregistered(struct v4l2_subdev
*sd
)
1616 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1621 fimc_unregister_m2m_device(fimc
);
1623 if (fimc
->vid_cap
.vfd
) {
1624 media_entity_cleanup(&fimc
->vid_cap
.vfd
->entity
);
1625 video_unregister_device(fimc
->vid_cap
.vfd
);
1626 fimc
->vid_cap
.vfd
= NULL
;
1629 kfree(fimc
->vid_cap
.ctx
);
1630 fimc
->vid_cap
.ctx
= NULL
;
1633 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops
= {
1634 .registered
= fimc_capture_subdev_registered
,
1635 .unregistered
= fimc_capture_subdev_unregistered
,
1638 int fimc_initialize_capture_subdev(struct fimc_dev
*fimc
)
1640 struct v4l2_subdev
*sd
= &fimc
->vid_cap
.subdev
;
1643 v4l2_subdev_init(sd
, &fimc_subdev_ops
);
1644 sd
->flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1645 snprintf(sd
->name
, sizeof(sd
->name
), "FIMC.%d", fimc
->pdev
->id
);
1647 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1648 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
1649 ret
= media_entity_init(&sd
->entity
, FIMC_SD_PADS_NUM
,
1650 fimc
->vid_cap
.sd_pads
, 0);
1654 sd
->entity
.ops
= &fimc_sd_media_ops
;
1655 sd
->internal_ops
= &fimc_capture_sd_internal_ops
;
1656 v4l2_set_subdevdata(sd
, fimc
);
1660 void fimc_unregister_capture_subdev(struct fimc_dev
*fimc
)
1662 struct v4l2_subdev
*sd
= &fimc
->vid_cap
.subdev
;
1664 v4l2_device_unregister_subdev(sd
);
1665 media_entity_cleanup(&sd
->entity
);
1666 v4l2_set_subdevdata(sd
, NULL
);