2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
5 * 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_capture_hw_init(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
);
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
);
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.
84 static int fimc_capture_state_cleanup(struct fimc_dev
*fimc
, bool suspend
)
86 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
87 struct fimc_vid_buffer
*buf
;
91 spin_lock_irqsave(&fimc
->slock
, flags
);
92 streaming
= fimc
->state
& (1 << ST_CAPT_ISP_STREAM
);
94 fimc
->state
&= ~(1 << ST_CAPT_RUN
| 1 << ST_CAPT_SHUT
|
95 1 << ST_CAPT_STREAM
| 1 << ST_CAPT_ISP_STREAM
);
97 fimc
->state
|= (1 << ST_CAPT_SUSPENDED
);
99 fimc
->state
&= ~(1 << ST_CAPT_PEND
| 1 << ST_CAPT_SUSPENDED
);
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
);
106 /* If suspending put unused buffers onto pending queue */
107 while (!list_empty(&cap
->active_buf_q
)) {
108 buf
= fimc_active_queue_pop(cap
);
110 fimc_pending_queue_add(cap
, buf
);
112 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
118 spin_unlock_irqrestore(&fimc
->slock
, flags
);
121 return fimc_pipeline_s_stream(&fimc
->pipeline
, 0);
126 static int fimc_stop_capture(struct fimc_dev
*fimc
, bool suspend
)
130 if (!fimc_capture_active(fimc
))
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
);
138 wait_event_timeout(fimc
->irq_queue
,
139 !test_bit(ST_CAPT_SHUT
, &fimc
->state
),
140 (2*HZ
/10)); /* 200 ms */
142 return fimc_capture_state_cleanup(fimc
, suspend
);
146 * fimc_capture_config_update - apply the camera interface configuration
148 * To be called from within the interrupt handler with fimc.slock
149 * spinlock held. It updates the camera pixel crop, rotation and
152 static int fimc_capture_config_update(struct fimc_ctx
*ctx
)
154 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
157 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
159 ret
= fimc_set_scaler_info(ctx
);
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
);
173 clear_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
177 void fimc_capture_irq_handler(struct fimc_dev
*fimc
, int deq_buf
)
179 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
180 struct fimc_vid_buffer
*v_buf
;
184 if (test_and_clear_bit(ST_CAPT_SHUT
, &fimc
->state
)) {
185 wake_up(&fimc
->irq_queue
);
189 if (!list_empty(&cap
->active_buf_q
) &&
190 test_bit(ST_CAPT_RUN
, &fimc
->state
) && deq_buf
) {
191 ktime_get_real_ts(&ts
);
193 v_buf
= fimc_active_queue_pop(cap
);
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
++;
200 vb2_buffer_done(&v_buf
->vb
, VB2_BUF_STATE_DONE
);
203 if (!list_empty(&cap
->pending_buf_q
)) {
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
;
209 /* Move the buffer to the capture active queue */
210 fimc_active_queue_add(cap
, v_buf
);
212 dbg("next frame: %d, done frame: %d",
213 fimc_hw_get_frame_index(fimc
), v_buf
->index
);
215 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
219 if (cap
->active_buf_cnt
== 0) {
221 clear_bit(ST_CAPT_RUN
, &fimc
->state
);
223 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
226 set_bit(ST_CAPT_RUN
, &fimc
->state
);
229 if (test_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
))
230 fimc_capture_config_update(cap
->ctx
);
232 if (cap
->active_buf_cnt
== 1) {
233 fimc_deactivate_capture(fimc
);
234 clear_bit(ST_CAPT_STREAM
, &fimc
->state
);
237 dbg("frame: %d, active_buf_cnt: %d",
238 fimc_hw_get_frame_index(fimc
), cap
->active_buf_cnt
);
242 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
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
;
250 vid_cap
->frame_count
= 0;
252 ret
= fimc_capture_hw_init(fimc
);
254 fimc_capture_state_cleanup(fimc
, false);
258 set_bit(ST_CAPT_PEND
, &fimc
->state
);
260 min_bufs
= fimc
->vid_cap
.reqbufs_count
> 1 ? 2 : 1;
262 if (vid_cap
->active_buf_cnt
>= min_bufs
&&
263 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
264 fimc_activate_capture(ctx
);
266 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
267 fimc_pipeline_s_stream(&fimc
->pipeline
, 1);
273 static int stop_streaming(struct vb2_queue
*q
)
275 struct fimc_ctx
*ctx
= q
->drv_priv
;
276 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
278 if (!fimc_capture_active(fimc
))
281 return fimc_stop_capture(fimc
, false);
284 int fimc_capture_suspend(struct fimc_dev
*fimc
)
286 bool suspend
= fimc_capture_busy(fimc
);
288 int ret
= fimc_stop_capture(fimc
, suspend
);
291 return fimc_pipeline_shutdown(&fimc
->pipeline
);
294 static void buffer_queue(struct vb2_buffer
*vb
);
296 int fimc_capture_resume(struct fimc_dev
*fimc
)
298 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
299 struct fimc_vid_buffer
*buf
;
302 if (!test_and_clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
))
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
,
309 fimc_capture_hw_init(fimc
);
311 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
313 for (i
= 0; i
< vid_cap
->reqbufs_count
; i
++) {
314 if (list_empty(&vid_cap
->pending_buf_q
))
316 buf
= fimc_pending_queue_pop(vid_cap
);
317 buffer_queue(&buf
->vb
);
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
[])
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
;
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
;
340 wh
= frame
->f_width
* frame
->f_height
;
346 *num_planes
= fmt
->memplanes
;
348 for (i
= 0; i
< fmt
->memplanes
; i
++) {
349 unsigned int size
= (wh
* fmt
->depth
[i
]) / 8;
351 sizes
[i
] = max(size
, pixm
->plane_fmt
[i
].sizeimage
);
354 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
360 static int buffer_prepare(struct vb2_buffer
*vb
)
362 struct vb2_queue
*vq
= vb
->vb2_queue
;
363 struct fimc_ctx
*ctx
= vq
->drv_priv
;
366 if (ctx
->d_frame
.fmt
== NULL
)
369 for (i
= 0; i
< ctx
->d_frame
.fmt
->memplanes
; i
++) {
370 unsigned long size
= ctx
->d_frame
.payload
[i
];
372 if (vb2_plane_size(vb
, i
) < size
) {
373 v4l2_err(ctx
->fimc_dev
->vid_cap
.vfd
,
374 "User buffer too small (%ld < %ld)\n",
375 vb2_plane_size(vb
, i
), size
);
378 vb2_set_plane_payload(vb
, i
, size
);
384 static void buffer_queue(struct vb2_buffer
*vb
)
386 struct fimc_vid_buffer
*buf
387 = container_of(vb
, struct fimc_vid_buffer
, vb
);
388 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
389 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
390 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
394 spin_lock_irqsave(&fimc
->slock
, flags
);
395 fimc_prepare_addr(ctx
, &buf
->vb
, &ctx
->d_frame
, &buf
->paddr
);
397 if (!test_bit(ST_CAPT_SUSPENDED
, &fimc
->state
) &&
398 !test_bit(ST_CAPT_STREAM
, &fimc
->state
) &&
399 vid_cap
->active_buf_cnt
< FIMC_MAX_OUT_BUFS
) {
400 /* Setup the buffer directly for processing. */
401 int buf_id
= (vid_cap
->reqbufs_count
== 1) ? -1 :
404 fimc_hw_set_output_addr(fimc
, &buf
->paddr
, buf_id
);
405 buf
->index
= vid_cap
->buf_index
;
406 fimc_active_queue_add(vid_cap
, buf
);
408 if (++vid_cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
409 vid_cap
->buf_index
= 0;
411 fimc_pending_queue_add(vid_cap
, buf
);
414 min_bufs
= vid_cap
->reqbufs_count
> 1 ? 2 : 1;
417 if (vb2_is_streaming(&vid_cap
->vbq
) &&
418 vid_cap
->active_buf_cnt
>= min_bufs
&&
419 !test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
420 fimc_activate_capture(ctx
);
421 spin_unlock_irqrestore(&fimc
->slock
, flags
);
423 if (!test_and_set_bit(ST_CAPT_ISP_STREAM
, &fimc
->state
))
424 fimc_pipeline_s_stream(&fimc
->pipeline
, 1);
427 spin_unlock_irqrestore(&fimc
->slock
, flags
);
430 static void fimc_lock(struct vb2_queue
*vq
)
432 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
433 mutex_lock(&ctx
->fimc_dev
->lock
);
436 static void fimc_unlock(struct vb2_queue
*vq
)
438 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
439 mutex_unlock(&ctx
->fimc_dev
->lock
);
442 static struct vb2_ops fimc_capture_qops
= {
443 .queue_setup
= queue_setup
,
444 .buf_prepare
= buffer_prepare
,
445 .buf_queue
= buffer_queue
,
446 .wait_prepare
= fimc_unlock
,
447 .wait_finish
= fimc_lock
,
448 .start_streaming
= start_streaming
,
449 .stop_streaming
= stop_streaming
,
453 * fimc_capture_ctrls_create - initialize the control handler
454 * Initialize the capture video node control handler and fill it
455 * with the FIMC controls. Inherit any sensor's controls if the
456 * 'user_subdev_api' flag is false (default behaviour).
457 * This function need to be called with the graph mutex held.
459 int fimc_capture_ctrls_create(struct fimc_dev
*fimc
)
461 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
464 if (WARN_ON(vid_cap
->ctx
== NULL
))
466 if (vid_cap
->ctx
->ctrls
.ready
)
469 ret
= fimc_ctrls_create(vid_cap
->ctx
);
470 if (ret
|| vid_cap
->user_subdev_api
|| !vid_cap
->ctx
->ctrls
.ready
)
473 return v4l2_ctrl_add_handler(&vid_cap
->ctx
->ctrls
.handler
,
474 fimc
->pipeline
.subdevs
[IDX_SENSOR
]->ctrl_handler
);
477 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
);
479 static int fimc_capture_open(struct file
*file
)
481 struct fimc_dev
*fimc
= video_drvdata(file
);
482 int ret
= v4l2_fh_open(file
);
487 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
489 /* Return if the corresponding video mem2mem node is already opened. */
490 if (fimc_m2m_active(fimc
))
493 set_bit(ST_CAPT_BUSY
, &fimc
->state
);
494 pm_runtime_get_sync(&fimc
->pdev
->dev
);
496 if (++fimc
->vid_cap
.refcnt
== 1) {
497 ret
= fimc_pipeline_initialize(&fimc
->pipeline
,
498 &fimc
->vid_cap
.vfd
->entity
, true);
500 dev_err(&fimc
->pdev
->dev
,
501 "Video pipeline initialization failed\n");
502 pm_runtime_put_sync(&fimc
->pdev
->dev
);
503 fimc
->vid_cap
.refcnt
--;
504 v4l2_fh_release(file
);
505 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
508 ret
= fimc_capture_ctrls_create(fimc
);
510 if (!ret
&& !fimc
->vid_cap
.user_subdev_api
)
511 ret
= fimc_capture_set_default_format(fimc
);
516 static int fimc_capture_close(struct file
*file
)
518 struct fimc_dev
*fimc
= video_drvdata(file
);
520 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
522 if (--fimc
->vid_cap
.refcnt
== 0) {
523 clear_bit(ST_CAPT_BUSY
, &fimc
->state
);
524 fimc_stop_capture(fimc
, false);
525 fimc_pipeline_shutdown(&fimc
->pipeline
);
526 clear_bit(ST_CAPT_SUSPENDED
, &fimc
->state
);
529 pm_runtime_put(&fimc
->pdev
->dev
);
531 if (fimc
->vid_cap
.refcnt
== 0) {
532 vb2_queue_release(&fimc
->vid_cap
.vbq
);
533 fimc_ctrls_delete(fimc
->vid_cap
.ctx
);
535 return v4l2_fh_release(file
);
538 static unsigned int fimc_capture_poll(struct file
*file
,
539 struct poll_table_struct
*wait
)
541 struct fimc_dev
*fimc
= video_drvdata(file
);
543 return vb2_poll(&fimc
->vid_cap
.vbq
, file
, wait
);
546 static int fimc_capture_mmap(struct file
*file
, struct vm_area_struct
*vma
)
548 struct fimc_dev
*fimc
= video_drvdata(file
);
550 return vb2_mmap(&fimc
->vid_cap
.vbq
, vma
);
553 static const struct v4l2_file_operations fimc_capture_fops
= {
554 .owner
= THIS_MODULE
,
555 .open
= fimc_capture_open
,
556 .release
= fimc_capture_close
,
557 .poll
= fimc_capture_poll
,
558 .unlocked_ioctl
= video_ioctl2
,
559 .mmap
= fimc_capture_mmap
,
563 * Format and crop negotiation helpers
566 static struct fimc_fmt
*fimc_capture_try_format(struct fimc_ctx
*ctx
,
567 u32
*width
, u32
*height
,
568 u32
*code
, u32
*fourcc
, int pad
)
570 bool rotation
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
571 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
572 struct fimc_variant
*var
= fimc
->variant
;
573 struct fimc_pix_limit
*pl
= var
->pix_limit
;
574 struct fimc_frame
*dst
= &ctx
->d_frame
;
575 u32 depth
, min_w
, max_w
, min_h
, align_h
= 3;
576 u32 mask
= FMT_FLAGS_CAM
;
577 struct fimc_fmt
*ffmt
;
579 /* Color conversion from/to JPEG is not supported */
580 if (code
&& ctx
->s_frame
.fmt
&& pad
== FIMC_SD_PAD_SOURCE
&&
581 fimc_fmt_is_jpeg(ctx
->s_frame
.fmt
->color
))
582 *code
= V4L2_MBUS_FMT_JPEG_1X8
;
584 if (fourcc
&& *fourcc
!= V4L2_PIX_FMT_JPEG
&& pad
!= FIMC_SD_PAD_SINK
)
585 mask
|= FMT_FLAGS_M2M
;
587 ffmt
= fimc_find_format(fourcc
, code
, mask
, 0);
591 *code
= ffmt
->mbus_code
;
593 *fourcc
= ffmt
->fourcc
;
595 if (pad
== FIMC_SD_PAD_SINK
) {
596 max_w
= fimc_fmt_is_jpeg(ffmt
->color
) ?
597 pl
->scaler_dis_w
: pl
->scaler_en_w
;
598 /* Apply the camera input interface pixel constraints */
599 v4l_bound_align_image(width
, max_t(u32
, *width
, 32), max_w
, 4,
600 height
, max_t(u32
, *height
, 32),
601 FIMC_CAMIF_MAX_HEIGHT
,
602 fimc_fmt_is_jpeg(ffmt
->color
) ? 3 : 1,
606 /* Can't scale or crop in transparent (JPEG) transfer mode */
607 if (fimc_fmt_is_jpeg(ffmt
->color
)) {
608 *width
= ctx
->s_frame
.f_width
;
609 *height
= ctx
->s_frame
.f_height
;
612 /* Apply the scaler and the output DMA constraints */
613 max_w
= rotation
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
;
614 if (ctx
->state
& FIMC_COMPOSE
) {
615 min_w
= dst
->offs_h
+ dst
->width
;
616 min_h
= dst
->offs_v
+ dst
->height
;
618 min_w
= var
->min_out_pixsize
;
619 min_h
= var
->min_out_pixsize
;
621 if (var
->min_vsize_align
== 1 && !rotation
)
622 align_h
= fimc_fmt_is_rgb(ffmt
->color
) ? 0 : 1;
624 depth
= fimc_get_format_depth(ffmt
);
625 v4l_bound_align_image(width
, min_w
, max_w
,
626 ffs(var
->min_out_pixsize
) - 1,
627 height
, min_h
, FIMC_CAMIF_MAX_HEIGHT
,
629 64/(ALIGN(depth
, 8)));
631 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
632 pad
, code
? *code
: 0, *width
, *height
,
633 dst
->f_width
, dst
->f_height
);
638 static void fimc_capture_try_selection(struct fimc_ctx
*ctx
,
642 bool rotate
= ctx
->rotation
== 90 || ctx
->rotation
== 270;
643 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
644 struct fimc_variant
*var
= fimc
->variant
;
645 struct fimc_pix_limit
*pl
= var
->pix_limit
;
646 struct fimc_frame
*sink
= &ctx
->s_frame
;
647 u32 max_w
, max_h
, min_w
= 0, min_h
= 0, min_sz
;
648 u32 align_sz
= 0, align_h
= 4;
649 u32 max_sc_h
, max_sc_v
;
651 /* In JPEG transparent transfer mode cropping is not supported */
652 if (fimc_fmt_is_jpeg(ctx
->d_frame
.fmt
->color
)) {
653 r
->width
= sink
->f_width
;
654 r
->height
= sink
->f_height
;
655 r
->left
= r
->top
= 0;
658 if (target
== V4L2_SEL_TGT_COMPOSE
) {
659 if (ctx
->rotation
!= 90 && ctx
->rotation
!= 270)
661 max_sc_h
= min(SCALER_MAX_HRATIO
, 1 << (ffs(sink
->width
) - 3));
662 max_sc_v
= min(SCALER_MAX_VRATIO
, 1 << (ffs(sink
->height
) - 1));
663 min_sz
= var
->min_out_pixsize
;
665 u32 depth
= fimc_get_format_depth(sink
->fmt
);
666 align_sz
= 64/ALIGN(depth
, 8);
667 min_sz
= var
->min_inp_pixsize
;
668 min_w
= min_h
= min_sz
;
669 max_sc_h
= max_sc_v
= 1;
672 * For the compose rectangle the following constraints must be met:
673 * - it must fit in the sink pad format rectangle (f_width/f_height);
674 * - maximum downscaling ratio is 64;
675 * - maximum crop size depends if the rotator is used or not;
676 * - the sink pad format width/height must be 4 multiple of the
677 * prescaler ratios determined by sink pad size and source pad crop,
678 * the prescaler ratio is returned by fimc_get_scaler_factor().
681 rotate
? pl
->out_rot_en_w
: pl
->out_rot_dis_w
,
682 rotate
? sink
->f_height
: sink
->f_width
);
683 max_h
= min_t(u32
, FIMC_CAMIF_MAX_HEIGHT
, sink
->f_height
);
685 if (target
== V4L2_SEL_TGT_COMPOSE
) {
686 min_w
= min_t(u32
, max_w
, sink
->f_width
/ max_sc_h
);
687 min_h
= min_t(u32
, max_h
, sink
->f_height
/ max_sc_v
);
689 swap(max_sc_h
, max_sc_v
);
693 v4l_bound_align_image(&r
->width
, min_w
, max_w
, ffs(min_sz
) - 1,
694 &r
->height
, min_h
, max_h
, align_h
,
696 /* Adjust left/top if crop/compose rectangle is out of bounds */
697 r
->left
= clamp_t(u32
, r
->left
, 0, sink
->f_width
- r
->width
);
698 r
->top
= clamp_t(u32
, r
->top
, 0, sink
->f_height
- r
->height
);
699 r
->left
= round_down(r
->left
, var
->hor_offs_align
);
701 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
702 target
, r
->left
, r
->top
, r
->width
, r
->height
,
703 sink
->f_width
, sink
->f_height
);
707 * The video node ioctl operations
709 static int fimc_vidioc_querycap_capture(struct file
*file
, void *priv
,
710 struct v4l2_capability
*cap
)
712 struct fimc_dev
*fimc
= video_drvdata(file
);
714 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
715 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
716 cap
->bus_info
[0] = 0;
717 cap
->capabilities
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE_MPLANE
;
722 static int fimc_cap_enum_fmt_mplane(struct file
*file
, void *priv
,
723 struct v4l2_fmtdesc
*f
)
725 struct fimc_fmt
*fmt
;
727 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
| FMT_FLAGS_M2M
,
731 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
732 f
->pixelformat
= fmt
->fourcc
;
733 if (fmt
->fourcc
== V4L2_MBUS_FMT_JPEG_1X8
)
734 f
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
739 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
741 * @ctx: FIMC capture context
742 * @tfmt: media bus format to try/set on subdevs
743 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
744 * @set: true to set format on subdevs, false to try only
746 static int fimc_pipeline_try_format(struct fimc_ctx
*ctx
,
747 struct v4l2_mbus_framefmt
*tfmt
,
748 struct fimc_fmt
**fmt_id
,
751 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
752 struct v4l2_subdev
*sd
= fimc
->pipeline
.subdevs
[IDX_SENSOR
];
753 struct v4l2_subdev
*csis
= fimc
->pipeline
.subdevs
[IDX_CSIS
];
754 struct v4l2_subdev_format sfmt
;
755 struct v4l2_mbus_framefmt
*mf
= &sfmt
.format
;
756 struct fimc_fmt
*ffmt
= NULL
;
759 if (WARN_ON(!sd
|| !tfmt
))
762 memset(&sfmt
, 0, sizeof(sfmt
));
765 sfmt
.which
= set
? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY
;
767 ffmt
= fimc_find_format(NULL
, mf
->code
!= 0 ? &mf
->code
: NULL
,
771 * Notify user-space if common pixel code for
772 * host and sensor does not exist.
776 mf
->code
= tfmt
->code
= ffmt
->mbus_code
;
778 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, NULL
, &sfmt
);
781 if (mf
->code
!= tfmt
->code
) {
785 if (mf
->width
!= tfmt
->width
|| mf
->height
!= tfmt
->height
) {
786 u32 fcc
= ffmt
->fourcc
;
787 tfmt
->width
= mf
->width
;
788 tfmt
->height
= mf
->height
;
789 ffmt
= fimc_capture_try_format(ctx
,
790 &tfmt
->width
, &tfmt
->height
,
791 NULL
, &fcc
, FIMC_SD_PAD_SOURCE
);
792 if (ffmt
&& ffmt
->mbus_code
)
793 mf
->code
= ffmt
->mbus_code
;
794 if (mf
->width
!= tfmt
->width
||
795 mf
->height
!= tfmt
->height
)
797 tfmt
->code
= mf
->code
;
800 ret
= v4l2_subdev_call(csis
, pad
, set_fmt
, NULL
, &sfmt
);
802 if (mf
->code
== tfmt
->code
&&
803 mf
->width
== tfmt
->width
&& mf
->height
== tfmt
->height
)
811 dbg("code: 0x%x, %dx%d, %p", mf
->code
, mf
->width
, mf
->height
, ffmt
);
815 static int fimc_cap_g_fmt_mplane(struct file
*file
, void *fh
,
816 struct v4l2_format
*f
)
818 struct fimc_dev
*fimc
= video_drvdata(file
);
819 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
821 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
824 return fimc_fill_format(&ctx
->d_frame
, f
);
827 static int fimc_cap_try_fmt_mplane(struct file
*file
, void *fh
,
828 struct v4l2_format
*f
)
830 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
831 struct fimc_dev
*fimc
= video_drvdata(file
);
832 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
833 struct v4l2_mbus_framefmt mf
;
834 struct fimc_fmt
*ffmt
= NULL
;
836 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
839 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
840 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
841 NULL
, &pix
->pixelformat
,
843 ctx
->s_frame
.f_width
= pix
->width
;
844 ctx
->s_frame
.f_height
= pix
->height
;
846 ffmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
847 NULL
, &pix
->pixelformat
,
852 if (!fimc
->vid_cap
.user_subdev_api
) {
853 mf
.width
= pix
->width
;
854 mf
.height
= pix
->height
;
855 mf
.code
= ffmt
->mbus_code
;
856 fimc_md_graph_lock(fimc
);
857 fimc_pipeline_try_format(ctx
, &mf
, &ffmt
, false);
858 fimc_md_graph_unlock(fimc
);
860 pix
->width
= mf
.width
;
861 pix
->height
= mf
.height
;
863 pix
->pixelformat
= ffmt
->fourcc
;
866 fimc_adjust_mplane_format(ffmt
, pix
->width
, pix
->height
, pix
);
870 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx
*ctx
, bool jpeg
)
872 ctx
->scaler
.enabled
= !jpeg
;
873 fimc_ctrls_activate(ctx
, !jpeg
);
876 set_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
878 clear_bit(ST_CAPT_JPEG
, &ctx
->fimc_dev
->state
);
881 static int fimc_capture_set_format(struct fimc_dev
*fimc
, struct v4l2_format
*f
)
883 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
884 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
885 struct v4l2_mbus_framefmt
*mf
= &fimc
->vid_cap
.mf
;
886 struct fimc_frame
*ff
= &ctx
->d_frame
;
887 struct fimc_fmt
*s_fmt
= NULL
;
890 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
892 if (vb2_is_busy(&fimc
->vid_cap
.vbq
))
895 /* Pre-configure format at camera interface input, for JPEG only */
896 if (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
897 fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
898 NULL
, &pix
->pixelformat
,
900 ctx
->s_frame
.f_width
= pix
->width
;
901 ctx
->s_frame
.f_height
= pix
->height
;
903 /* Try the format at the scaler and the DMA output */
904 ff
->fmt
= fimc_capture_try_format(ctx
, &pix
->width
, &pix
->height
,
905 NULL
, &pix
->pixelformat
,
910 /* Update RGB Alpha control state and value range */
911 fimc_alpha_ctrl_update(ctx
);
913 /* Try to match format at the host and the sensor */
914 if (!fimc
->vid_cap
.user_subdev_api
) {
915 mf
->code
= ff
->fmt
->mbus_code
;
916 mf
->width
= pix
->width
;
917 mf
->height
= pix
->height
;
919 fimc_md_graph_lock(fimc
);
920 ret
= fimc_pipeline_try_format(ctx
, mf
, &s_fmt
, true);
921 fimc_md_graph_unlock(fimc
);
924 pix
->width
= mf
->width
;
925 pix
->height
= mf
->height
;
927 fimc_adjust_mplane_format(ff
->fmt
, pix
->width
, pix
->height
, pix
);
928 for (i
= 0; i
< ff
->fmt
->colplanes
; i
++)
930 (pix
->width
* pix
->height
* ff
->fmt
->depth
[i
]) / 8;
932 set_frame_bounds(ff
, pix
->width
, pix
->height
);
933 /* Reset the composition rectangle if not yet configured */
934 if (!(ctx
->state
& FIMC_COMPOSE
))
935 set_frame_crop(ff
, 0, 0, pix
->width
, pix
->height
);
937 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ff
->fmt
->color
));
939 /* Reset cropping and set format at the camera interface input */
940 if (!fimc
->vid_cap
.user_subdev_api
) {
941 ctx
->s_frame
.fmt
= s_fmt
;
942 set_frame_bounds(&ctx
->s_frame
, pix
->width
, pix
->height
);
943 set_frame_crop(&ctx
->s_frame
, 0, 0, pix
->width
, pix
->height
);
949 static int fimc_cap_s_fmt_mplane(struct file
*file
, void *priv
,
950 struct v4l2_format
*f
)
952 struct fimc_dev
*fimc
= video_drvdata(file
);
954 return fimc_capture_set_format(fimc
, f
);
957 static int fimc_cap_enum_input(struct file
*file
, void *priv
,
958 struct v4l2_input
*i
)
960 struct fimc_dev
*fimc
= video_drvdata(file
);
961 struct v4l2_subdev
*sd
= fimc
->pipeline
.subdevs
[IDX_SENSOR
];
966 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
968 strlcpy(i
->name
, sd
->name
, sizeof(i
->name
));
972 static int fimc_cap_s_input(struct file
*file
, void *priv
, unsigned int i
)
974 return i
== 0 ? i
: -EINVAL
;
977 static int fimc_cap_g_input(struct file
*file
, void *priv
, unsigned int *i
)
984 * fimc_pipeline_validate - check for formats inconsistencies
985 * between source and sink pad of each link
987 * Return 0 if all formats match or -EPIPE otherwise.
989 static int fimc_pipeline_validate(struct fimc_dev
*fimc
)
991 struct v4l2_subdev_format sink_fmt
, src_fmt
;
992 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
993 struct v4l2_subdev
*sd
;
994 struct media_pad
*pad
;
997 /* Start with the video capture node pad */
998 pad
= media_entity_remote_source(&vid_cap
->vd_pad
);
1001 /* FIMC.{N} subdevice */
1002 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
1005 /* Retrieve format at the sink pad */
1006 pad
= &sd
->entity
.pads
[0];
1007 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
1009 /* Don't call FIMC subdev operation to avoid nested locking */
1010 if (sd
== &fimc
->vid_cap
.subdev
) {
1011 struct fimc_frame
*ff
= &vid_cap
->ctx
->s_frame
;
1012 sink_fmt
.format
.width
= ff
->f_width
;
1013 sink_fmt
.format
.height
= ff
->f_height
;
1014 sink_fmt
.format
.code
= ff
->fmt
? ff
->fmt
->mbus_code
: 0;
1016 sink_fmt
.pad
= pad
->index
;
1017 sink_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
1018 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &sink_fmt
);
1019 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
1022 /* Retrieve format at the source pad */
1023 pad
= media_entity_remote_source(pad
);
1025 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1028 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
1029 src_fmt
.pad
= pad
->index
;
1030 src_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
1031 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &src_fmt
);
1032 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
1035 if (src_fmt
.format
.width
!= sink_fmt
.format
.width
||
1036 src_fmt
.format
.height
!= sink_fmt
.format
.height
||
1037 src_fmt
.format
.code
!= sink_fmt
.format
.code
)
1043 static int fimc_cap_streamon(struct file
*file
, void *priv
,
1044 enum v4l2_buf_type type
)
1046 struct fimc_dev
*fimc
= video_drvdata(file
);
1047 struct fimc_pipeline
*p
= &fimc
->pipeline
;
1050 if (fimc_capture_active(fimc
))
1053 media_entity_pipeline_start(&p
->subdevs
[IDX_SENSOR
]->entity
,
1056 if (fimc
->vid_cap
.user_subdev_api
) {
1057 ret
= fimc_pipeline_validate(fimc
);
1061 return vb2_streamon(&fimc
->vid_cap
.vbq
, type
);
1064 static int fimc_cap_streamoff(struct file
*file
, void *priv
,
1065 enum v4l2_buf_type type
)
1067 struct fimc_dev
*fimc
= video_drvdata(file
);
1068 struct v4l2_subdev
*sd
= fimc
->pipeline
.subdevs
[IDX_SENSOR
];
1071 ret
= vb2_streamoff(&fimc
->vid_cap
.vbq
, type
);
1073 media_entity_pipeline_stop(&sd
->entity
);
1077 static int fimc_cap_reqbufs(struct file
*file
, void *priv
,
1078 struct v4l2_requestbuffers
*reqbufs
)
1080 struct fimc_dev
*fimc
= video_drvdata(file
);
1081 int ret
= vb2_reqbufs(&fimc
->vid_cap
.vbq
, reqbufs
);
1084 fimc
->vid_cap
.reqbufs_count
= reqbufs
->count
;
1088 static int fimc_cap_querybuf(struct file
*file
, void *priv
,
1089 struct v4l2_buffer
*buf
)
1091 struct fimc_dev
*fimc
= video_drvdata(file
);
1093 return vb2_querybuf(&fimc
->vid_cap
.vbq
, buf
);
1096 static int fimc_cap_qbuf(struct file
*file
, void *priv
,
1097 struct v4l2_buffer
*buf
)
1099 struct fimc_dev
*fimc
= video_drvdata(file
);
1101 return vb2_qbuf(&fimc
->vid_cap
.vbq
, buf
);
1104 static int fimc_cap_dqbuf(struct file
*file
, void *priv
,
1105 struct v4l2_buffer
*buf
)
1107 struct fimc_dev
*fimc
= video_drvdata(file
);
1109 return vb2_dqbuf(&fimc
->vid_cap
.vbq
, buf
, file
->f_flags
& O_NONBLOCK
);
1112 static int fimc_cap_create_bufs(struct file
*file
, void *priv
,
1113 struct v4l2_create_buffers
*create
)
1115 struct fimc_dev
*fimc
= video_drvdata(file
);
1117 return vb2_create_bufs(&fimc
->vid_cap
.vbq
, create
);
1120 static int fimc_cap_prepare_buf(struct file
*file
, void *priv
,
1121 struct v4l2_buffer
*b
)
1123 struct fimc_dev
*fimc
= video_drvdata(file
);
1125 return vb2_prepare_buf(&fimc
->vid_cap
.vbq
, b
);
1128 static int fimc_cap_g_selection(struct file
*file
, void *fh
,
1129 struct v4l2_selection
*s
)
1131 struct fimc_dev
*fimc
= video_drvdata(file
);
1132 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1133 struct fimc_frame
*f
= &ctx
->s_frame
;
1135 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1138 switch (s
->target
) {
1139 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
1140 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1142 case V4L2_SEL_TGT_CROP_BOUNDS
:
1143 case V4L2_SEL_TGT_CROP_DEFAULT
:
1146 s
->r
.width
= f
->o_width
;
1147 s
->r
.height
= f
->o_height
;
1150 case V4L2_SEL_TGT_COMPOSE
:
1152 case V4L2_SEL_TGT_CROP
:
1153 s
->r
.left
= f
->offs_h
;
1154 s
->r
.top
= f
->offs_v
;
1155 s
->r
.width
= f
->width
;
1156 s
->r
.height
= f
->height
;
1163 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1164 static int enclosed_rectangle(struct v4l2_rect
*a
, struct v4l2_rect
*b
)
1166 if (a
->left
< b
->left
|| a
->top
< b
->top
)
1168 if (a
->left
+ a
->width
> b
->left
+ b
->width
)
1170 if (a
->top
+ a
->height
> b
->top
+ b
->height
)
1176 static int fimc_cap_s_selection(struct file
*file
, void *fh
,
1177 struct v4l2_selection
*s
)
1179 struct fimc_dev
*fimc
= video_drvdata(file
);
1180 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1181 struct v4l2_rect rect
= s
->r
;
1182 struct fimc_frame
*f
;
1183 unsigned long flags
;
1185 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1188 if (s
->target
== V4L2_SEL_TGT_COMPOSE
)
1190 else if (s
->target
== V4L2_SEL_TGT_CROP
)
1195 fimc_capture_try_selection(ctx
, &rect
, s
->target
);
1197 if (s
->flags
& V4L2_SEL_FLAG_LE
&&
1198 !enclosed_rectangle(&rect
, &s
->r
))
1201 if (s
->flags
& V4L2_SEL_FLAG_GE
&&
1202 !enclosed_rectangle(&s
->r
, &rect
))
1206 spin_lock_irqsave(&fimc
->slock
, flags
);
1207 set_frame_crop(f
, s
->r
.left
, s
->r
.top
, s
->r
.width
,
1209 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1211 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1215 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops
= {
1216 .vidioc_querycap
= fimc_vidioc_querycap_capture
,
1218 .vidioc_enum_fmt_vid_cap_mplane
= fimc_cap_enum_fmt_mplane
,
1219 .vidioc_try_fmt_vid_cap_mplane
= fimc_cap_try_fmt_mplane
,
1220 .vidioc_s_fmt_vid_cap_mplane
= fimc_cap_s_fmt_mplane
,
1221 .vidioc_g_fmt_vid_cap_mplane
= fimc_cap_g_fmt_mplane
,
1223 .vidioc_reqbufs
= fimc_cap_reqbufs
,
1224 .vidioc_querybuf
= fimc_cap_querybuf
,
1226 .vidioc_qbuf
= fimc_cap_qbuf
,
1227 .vidioc_dqbuf
= fimc_cap_dqbuf
,
1229 .vidioc_prepare_buf
= fimc_cap_prepare_buf
,
1230 .vidioc_create_bufs
= fimc_cap_create_bufs
,
1232 .vidioc_streamon
= fimc_cap_streamon
,
1233 .vidioc_streamoff
= fimc_cap_streamoff
,
1235 .vidioc_g_selection
= fimc_cap_g_selection
,
1236 .vidioc_s_selection
= fimc_cap_s_selection
,
1238 .vidioc_enum_input
= fimc_cap_enum_input
,
1239 .vidioc_s_input
= fimc_cap_s_input
,
1240 .vidioc_g_input
= fimc_cap_g_input
,
1243 /* Capture subdev media entity operations */
1244 static int fimc_link_setup(struct media_entity
*entity
,
1245 const struct media_pad
*local
,
1246 const struct media_pad
*remote
, u32 flags
)
1248 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1249 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1251 if (media_entity_type(remote
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1254 if (WARN_ON(fimc
== NULL
))
1257 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1258 local
->entity
->name
, remote
->entity
->name
, flags
,
1259 fimc
->vid_cap
.input
);
1261 if (flags
& MEDIA_LNK_FL_ENABLED
) {
1262 if (fimc
->vid_cap
.input
!= 0)
1264 fimc
->vid_cap
.input
= sd
->grp_id
;
1268 fimc
->vid_cap
.input
= 0;
1272 static const struct media_entity_operations fimc_sd_media_ops
= {
1273 .link_setup
= fimc_link_setup
,
1277 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1278 * @sd: pointer to a subdev generating the notification
1279 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1280 * @arg: pointer to an u32 type integer that stores the frame payload value
1282 * The End Of Frame notification sent by sensor subdev in its still capture
1283 * mode. If there is only a single VSYNC generated by the sensor at the
1284 * beginning of a frame transmission, FIMC does not issue the LastIrq
1285 * (end of frame) interrupt. And this notification is used to complete the
1286 * frame capture and returning a buffer to user-space. Subdev drivers should
1287 * call this notification from their last 'End of frame capture' interrupt.
1289 void fimc_sensor_notify(struct v4l2_subdev
*sd
, unsigned int notification
,
1292 struct fimc_sensor_info
*sensor
;
1293 struct fimc_vid_buffer
*buf
;
1294 struct fimc_md
*fmd
;
1295 struct fimc_dev
*fimc
;
1296 unsigned long flags
;
1301 sensor
= v4l2_get_subdev_hostdata(sd
);
1302 fmd
= entity_to_fimc_mdev(&sd
->entity
);
1304 spin_lock_irqsave(&fmd
->slock
, flags
);
1305 fimc
= sensor
? sensor
->host
: NULL
;
1307 if (fimc
&& arg
&& notification
== S5P_FIMC_TX_END_NOTIFY
&&
1308 test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
1309 unsigned long irq_flags
;
1310 spin_lock_irqsave(&fimc
->slock
, irq_flags
);
1311 if (!list_empty(&fimc
->vid_cap
.active_buf_q
)) {
1312 buf
= list_entry(fimc
->vid_cap
.active_buf_q
.next
,
1313 struct fimc_vid_buffer
, list
);
1314 vb2_set_plane_payload(&buf
->vb
, 0, *((u32
*)arg
));
1316 fimc_capture_irq_handler(fimc
, 1);
1317 fimc_deactivate_capture(fimc
);
1318 spin_unlock_irqrestore(&fimc
->slock
, irq_flags
);
1320 spin_unlock_irqrestore(&fmd
->slock
, flags
);
1323 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev
*sd
,
1324 struct v4l2_subdev_fh
*fh
,
1325 struct v4l2_subdev_mbus_code_enum
*code
)
1327 struct fimc_fmt
*fmt
;
1329 fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, code
->index
);
1332 code
->code
= fmt
->mbus_code
;
1336 static int fimc_subdev_get_fmt(struct v4l2_subdev
*sd
,
1337 struct v4l2_subdev_fh
*fh
,
1338 struct v4l2_subdev_format
*fmt
)
1340 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1341 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1342 struct v4l2_mbus_framefmt
*mf
;
1343 struct fimc_frame
*ff
;
1345 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1346 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1351 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1352 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
? &ctx
->s_frame
: &ctx
->d_frame
;
1354 mutex_lock(&fimc
->lock
);
1355 /* The pixel code is same on both input and output pad */
1356 if (!WARN_ON(ctx
->s_frame
.fmt
== NULL
))
1357 mf
->code
= ctx
->s_frame
.fmt
->mbus_code
;
1358 mf
->width
= ff
->f_width
;
1359 mf
->height
= ff
->f_height
;
1360 mutex_unlock(&fimc
->lock
);
1365 static int fimc_subdev_set_fmt(struct v4l2_subdev
*sd
,
1366 struct v4l2_subdev_fh
*fh
,
1367 struct v4l2_subdev_format
*fmt
)
1369 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1370 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1371 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1372 struct fimc_frame
*ff
;
1373 struct fimc_fmt
*ffmt
;
1375 dbg("pad%d: code: 0x%x, %dx%d",
1376 fmt
->pad
, mf
->code
, mf
->width
, mf
->height
);
1378 if (fmt
->pad
== FIMC_SD_PAD_SOURCE
&&
1379 vb2_is_busy(&fimc
->vid_cap
.vbq
))
1382 mutex_lock(&fimc
->lock
);
1383 ffmt
= fimc_capture_try_format(ctx
, &mf
->width
, &mf
->height
,
1384 &mf
->code
, NULL
, fmt
->pad
);
1385 mutex_unlock(&fimc
->lock
);
1386 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
1388 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1389 mf
= v4l2_subdev_get_try_format(fh
, fmt
->pad
);
1393 /* Update RGB Alpha control state and value range */
1394 fimc_alpha_ctrl_update(ctx
);
1396 fimc_capture_mark_jpeg_xfer(ctx
, fimc_fmt_is_jpeg(ffmt
->color
));
1398 ff
= fmt
->pad
== FIMC_SD_PAD_SINK
?
1399 &ctx
->s_frame
: &ctx
->d_frame
;
1401 mutex_lock(&fimc
->lock
);
1402 set_frame_bounds(ff
, mf
->width
, mf
->height
);
1403 fimc
->vid_cap
.mf
= *mf
;
1406 /* Reset the crop rectangle if required. */
1407 if (!(fmt
->pad
== FIMC_SD_PAD_SOURCE
&& (ctx
->state
& FIMC_COMPOSE
)))
1408 set_frame_crop(ff
, 0, 0, mf
->width
, mf
->height
);
1410 if (fmt
->pad
== FIMC_SD_PAD_SINK
)
1411 ctx
->state
&= ~FIMC_COMPOSE
;
1412 mutex_unlock(&fimc
->lock
);
1416 static int fimc_subdev_get_selection(struct v4l2_subdev
*sd
,
1417 struct v4l2_subdev_fh
*fh
,
1418 struct v4l2_subdev_selection
*sel
)
1420 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1421 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1422 struct fimc_frame
*f
= &ctx
->s_frame
;
1423 struct v4l2_rect
*r
= &sel
->r
;
1424 struct v4l2_rect
*try_sel
;
1426 if (sel
->pad
!= FIMC_SD_PAD_SINK
)
1429 mutex_lock(&fimc
->lock
);
1431 switch (sel
->target
) {
1432 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS
:
1434 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS
:
1435 r
->width
= f
->o_width
;
1436 r
->height
= f
->o_height
;
1439 mutex_unlock(&fimc
->lock
);
1442 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL
:
1443 try_sel
= v4l2_subdev_get_try_crop(fh
, sel
->pad
);
1445 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL
:
1446 try_sel
= v4l2_subdev_get_try_compose(fh
, sel
->pad
);
1450 mutex_unlock(&fimc
->lock
);
1454 if (sel
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1457 r
->left
= f
->offs_h
;
1459 r
->width
= f
->width
;
1460 r
->height
= f
->height
;
1463 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1464 sel
->pad
, r
->left
, r
->top
, r
->width
, r
->height
,
1465 f
->f_width
, f
->f_height
);
1467 mutex_unlock(&fimc
->lock
);
1471 static int fimc_subdev_set_selection(struct v4l2_subdev
*sd
,
1472 struct v4l2_subdev_fh
*fh
,
1473 struct v4l2_subdev_selection
*sel
)
1475 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1476 struct fimc_ctx
*ctx
= fimc
->vid_cap
.ctx
;
1477 struct fimc_frame
*f
= &ctx
->s_frame
;
1478 struct v4l2_rect
*r
= &sel
->r
;
1479 struct v4l2_rect
*try_sel
;
1480 unsigned long flags
;
1482 if (sel
->pad
!= FIMC_SD_PAD_SINK
)
1485 mutex_lock(&fimc
->lock
);
1486 fimc_capture_try_selection(ctx
, r
, V4L2_SEL_TGT_CROP
);
1488 switch (sel
->target
) {
1489 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS
:
1491 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS
:
1492 r
->width
= f
->o_width
;
1493 r
->height
= f
->o_height
;
1496 mutex_unlock(&fimc
->lock
);
1499 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL
:
1500 try_sel
= v4l2_subdev_get_try_crop(fh
, sel
->pad
);
1502 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL
:
1503 try_sel
= v4l2_subdev_get_try_compose(fh
, sel
->pad
);
1507 mutex_unlock(&fimc
->lock
);
1511 if (sel
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1514 spin_lock_irqsave(&fimc
->slock
, flags
);
1515 set_frame_crop(f
, r
->left
, r
->top
, r
->width
, r
->height
);
1516 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
1517 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1518 if (sel
->target
== V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL
)
1519 ctx
->state
|= FIMC_COMPOSE
;
1522 dbg("target %#x: (%d,%d)/%dx%d", sel
->target
, r
->left
, r
->top
,
1523 r
->width
, r
->height
);
1525 mutex_unlock(&fimc
->lock
);
1529 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops
= {
1530 .enum_mbus_code
= fimc_subdev_enum_mbus_code
,
1531 .get_selection
= fimc_subdev_get_selection
,
1532 .set_selection
= fimc_subdev_set_selection
,
1533 .get_fmt
= fimc_subdev_get_fmt
,
1534 .set_fmt
= fimc_subdev_set_fmt
,
1537 static struct v4l2_subdev_ops fimc_subdev_ops
= {
1538 .pad
= &fimc_subdev_pad_ops
,
1541 /* Set default format at the sensor and host interface */
1542 static int fimc_capture_set_default_format(struct fimc_dev
*fimc
)
1544 struct v4l2_format fmt
= {
1545 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
,
1549 .pixelformat
= V4L2_PIX_FMT_YUYV
,
1550 .field
= V4L2_FIELD_NONE
,
1551 .colorspace
= V4L2_COLORSPACE_JPEG
,
1555 return fimc_capture_set_format(fimc
, &fmt
);
1558 /* fimc->lock must be already initialized */
1559 static int fimc_register_capture_device(struct fimc_dev
*fimc
,
1560 struct v4l2_device
*v4l2_dev
)
1562 struct video_device
*vfd
;
1563 struct fimc_vid_cap
*vid_cap
;
1564 struct fimc_ctx
*ctx
;
1565 struct vb2_queue
*q
;
1568 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1572 ctx
->fimc_dev
= fimc
;
1573 ctx
->in_path
= FIMC_IO_CAMERA
;
1574 ctx
->out_path
= FIMC_IO_DMA
;
1575 ctx
->state
= FIMC_CTX_CAP
;
1576 ctx
->s_frame
.fmt
= fimc_find_format(NULL
, NULL
, FMT_FLAGS_CAM
, 0);
1577 ctx
->d_frame
.fmt
= ctx
->s_frame
.fmt
;
1579 vfd
= video_device_alloc();
1581 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1585 snprintf(vfd
->name
, sizeof(vfd
->name
), "fimc.%d.capture", fimc
->id
);
1587 vfd
->fops
= &fimc_capture_fops
;
1588 vfd
->ioctl_ops
= &fimc_capture_ioctl_ops
;
1589 vfd
->v4l2_dev
= v4l2_dev
;
1591 vfd
->release
= video_device_release
;
1592 vfd
->lock
= &fimc
->lock
;
1593 /* Locking in file operations other than ioctl should be done
1594 by the driver, not the V4L2 core.
1595 This driver needs auditing so that this flag can be removed. */
1596 set_bit(V4L2_FL_LOCK_ALL_FOPS
, &vfd
->flags
);
1597 video_set_drvdata(vfd
, fimc
);
1599 vid_cap
= &fimc
->vid_cap
;
1601 vid_cap
->active_buf_cnt
= 0;
1602 vid_cap
->reqbufs_count
= 0;
1603 vid_cap
->refcnt
= 0;
1605 INIT_LIST_HEAD(&vid_cap
->pending_buf_q
);
1606 INIT_LIST_HEAD(&vid_cap
->active_buf_q
);
1609 q
= &fimc
->vid_cap
.vbq
;
1610 memset(q
, 0, sizeof(*q
));
1611 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1612 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1613 q
->drv_priv
= fimc
->vid_cap
.ctx
;
1614 q
->ops
= &fimc_capture_qops
;
1615 q
->mem_ops
= &vb2_dma_contig_memops
;
1616 q
->buf_struct_size
= sizeof(struct fimc_vid_buffer
);
1620 vid_cap
->vd_pad
.flags
= MEDIA_PAD_FL_SINK
;
1621 ret
= media_entity_init(&vfd
->entity
, 1, &vid_cap
->vd_pad
, 0);
1625 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
1629 v4l2_info(v4l2_dev
, "Registered %s as /dev/%s\n",
1630 vfd
->name
, video_device_node_name(vfd
));
1632 vfd
->ctrl_handler
= &ctx
->ctrls
.handler
;
1636 media_entity_cleanup(&vfd
->entity
);
1638 video_device_release(vfd
);
1644 static int fimc_capture_subdev_registered(struct v4l2_subdev
*sd
)
1646 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1649 ret
= fimc_register_m2m_device(fimc
, sd
->v4l2_dev
);
1653 ret
= fimc_register_capture_device(fimc
, sd
->v4l2_dev
);
1655 fimc_unregister_m2m_device(fimc
);
1660 static void fimc_capture_subdev_unregistered(struct v4l2_subdev
*sd
)
1662 struct fimc_dev
*fimc
= v4l2_get_subdevdata(sd
);
1667 fimc_unregister_m2m_device(fimc
);
1669 if (fimc
->vid_cap
.vfd
) {
1670 media_entity_cleanup(&fimc
->vid_cap
.vfd
->entity
);
1671 video_unregister_device(fimc
->vid_cap
.vfd
);
1672 fimc
->vid_cap
.vfd
= NULL
;
1675 kfree(fimc
->vid_cap
.ctx
);
1676 fimc
->vid_cap
.ctx
= NULL
;
1679 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops
= {
1680 .registered
= fimc_capture_subdev_registered
,
1681 .unregistered
= fimc_capture_subdev_unregistered
,
1684 int fimc_initialize_capture_subdev(struct fimc_dev
*fimc
)
1686 struct v4l2_subdev
*sd
= &fimc
->vid_cap
.subdev
;
1689 v4l2_subdev_init(sd
, &fimc_subdev_ops
);
1690 sd
->flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1691 snprintf(sd
->name
, sizeof(sd
->name
), "FIMC.%d", fimc
->pdev
->id
);
1693 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1694 fimc
->vid_cap
.sd_pads
[FIMC_SD_PAD_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
1695 ret
= media_entity_init(&sd
->entity
, FIMC_SD_PADS_NUM
,
1696 fimc
->vid_cap
.sd_pads
, 0);
1700 sd
->entity
.ops
= &fimc_sd_media_ops
;
1701 sd
->internal_ops
= &fimc_capture_sd_internal_ops
;
1702 v4l2_set_subdevdata(sd
, fimc
);
1706 void fimc_unregister_capture_subdev(struct fimc_dev
*fimc
)
1708 struct v4l2_subdev
*sd
= &fimc
->vid_cap
.subdev
;
1710 v4l2_device_unregister_subdev(sd
);
1711 media_entity_cleanup(&sd
->entity
);
1712 v4l2_set_subdevdata(sd
, NULL
);