2 * Samsung S5P/EXYNOS4 SoC series camera interface (video postprocessor) driver
4 * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
5 * Contact: 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 as published
9 * by the Free Software Foundation, either version 2 of the License,
10 * or (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/bug.h>
18 #include <linux/interrupt.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/clk.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "fimc-core.h"
31 #include "fimc-mdevice.h"
33 static char *fimc_clocks
[MAX_FIMC_CLOCKS
] = {
37 static struct fimc_fmt fimc_formats
[] = {
40 .fourcc
= V4L2_PIX_FMT_RGB565
,
42 .color
= S5P_FIMC_RGB565
,
45 .flags
= FMT_FLAGS_M2M
,
48 .fourcc
= V4L2_PIX_FMT_BGR666
,
50 .color
= S5P_FIMC_RGB666
,
53 .flags
= FMT_FLAGS_M2M
,
55 .name
= "ARGB8888, 32 bpp",
56 .fourcc
= V4L2_PIX_FMT_RGB32
,
58 .color
= S5P_FIMC_RGB888
,
61 .flags
= FMT_FLAGS_M2M
| FMT_HAS_ALPHA
,
64 .fourcc
= V4L2_PIX_FMT_RGB555
,
66 .color
= S5P_FIMC_RGB555
,
69 .flags
= FMT_FLAGS_M2M_OUT
| FMT_HAS_ALPHA
,
72 .fourcc
= V4L2_PIX_FMT_RGB444
,
74 .color
= S5P_FIMC_RGB444
,
77 .flags
= FMT_FLAGS_M2M_OUT
| FMT_HAS_ALPHA
,
79 .name
= "YUV 4:2:2 packed, YCbYCr",
80 .fourcc
= V4L2_PIX_FMT_YUYV
,
82 .color
= S5P_FIMC_YCBYCR422
,
85 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
86 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
88 .name
= "YUV 4:2:2 packed, CbYCrY",
89 .fourcc
= V4L2_PIX_FMT_UYVY
,
91 .color
= S5P_FIMC_CBYCRY422
,
94 .mbus_code
= V4L2_MBUS_FMT_UYVY8_2X8
,
95 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
97 .name
= "YUV 4:2:2 packed, CrYCbY",
98 .fourcc
= V4L2_PIX_FMT_VYUY
,
100 .color
= S5P_FIMC_CRYCBY422
,
103 .mbus_code
= V4L2_MBUS_FMT_VYUY8_2X8
,
104 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
106 .name
= "YUV 4:2:2 packed, YCrYCb",
107 .fourcc
= V4L2_PIX_FMT_YVYU
,
109 .color
= S5P_FIMC_YCRYCB422
,
112 .mbus_code
= V4L2_MBUS_FMT_YVYU8_2X8
,
113 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
115 .name
= "YUV 4:2:2 planar, Y/Cb/Cr",
116 .fourcc
= V4L2_PIX_FMT_YUV422P
,
118 .color
= S5P_FIMC_YCBYCR422
,
121 .flags
= FMT_FLAGS_M2M
,
123 .name
= "YUV 4:2:2 planar, Y/CbCr",
124 .fourcc
= V4L2_PIX_FMT_NV16
,
126 .color
= S5P_FIMC_YCBYCR422
,
129 .flags
= FMT_FLAGS_M2M
,
131 .name
= "YUV 4:2:2 planar, Y/CrCb",
132 .fourcc
= V4L2_PIX_FMT_NV61
,
134 .color
= S5P_FIMC_YCRYCB422
,
137 .flags
= FMT_FLAGS_M2M
,
139 .name
= "YUV 4:2:0 planar, YCbCr",
140 .fourcc
= V4L2_PIX_FMT_YUV420
,
142 .color
= S5P_FIMC_YCBCR420
,
145 .flags
= FMT_FLAGS_M2M
,
147 .name
= "YUV 4:2:0 planar, Y/CbCr",
148 .fourcc
= V4L2_PIX_FMT_NV12
,
150 .color
= S5P_FIMC_YCBCR420
,
153 .flags
= FMT_FLAGS_M2M
,
155 .name
= "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr",
156 .fourcc
= V4L2_PIX_FMT_NV12M
,
157 .color
= S5P_FIMC_YCBCR420
,
161 .flags
= FMT_FLAGS_M2M
,
163 .name
= "YUV 4:2:0 non-contiguous 3-planar, Y/Cb/Cr",
164 .fourcc
= V4L2_PIX_FMT_YUV420M
,
165 .color
= S5P_FIMC_YCBCR420
,
166 .depth
= { 8, 2, 2 },
169 .flags
= FMT_FLAGS_M2M
,
171 .name
= "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr, tiled",
172 .fourcc
= V4L2_PIX_FMT_NV12MT
,
173 .color
= S5P_FIMC_YCBCR420
,
177 .flags
= FMT_FLAGS_M2M
,
179 .name
= "JPEG encoded data",
180 .fourcc
= V4L2_PIX_FMT_JPEG
,
181 .color
= S5P_FIMC_JPEG
,
185 .mbus_code
= V4L2_MBUS_FMT_JPEG_1X8
,
186 .flags
= FMT_FLAGS_CAM
,
190 static unsigned int get_m2m_fmt_flags(unsigned int stream_type
)
192 if (stream_type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
193 return FMT_FLAGS_M2M_IN
;
195 return FMT_FLAGS_M2M_OUT
;
198 int fimc_check_scaler_ratio(struct fimc_ctx
*ctx
, int sw
, int sh
,
199 int dw
, int dh
, int rotation
)
201 if (rotation
== 90 || rotation
== 270)
204 if (!ctx
->scaler
.enabled
)
205 return (sw
== dw
&& sh
== dh
) ? 0 : -EINVAL
;
207 if ((sw
>= SCALER_MAX_HRATIO
* dw
) || (sh
>= SCALER_MAX_VRATIO
* dh
))
213 static int fimc_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
222 if (src
>= tar
* tmp
) {
223 *shift
= sh
, *ratio
= tmp
;
227 *shift
= 0, *ratio
= 1;
231 int fimc_set_scaler_info(struct fimc_ctx
*ctx
)
233 struct samsung_fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
234 struct device
*dev
= &ctx
->fimc_dev
->pdev
->dev
;
235 struct fimc_scaler
*sc
= &ctx
->scaler
;
236 struct fimc_frame
*s_frame
= &ctx
->s_frame
;
237 struct fimc_frame
*d_frame
= &ctx
->d_frame
;
241 if (ctx
->rotation
== 90 || ctx
->rotation
== 270) {
243 tx
= d_frame
->height
;
246 ty
= d_frame
->height
;
248 if (tx
<= 0 || ty
<= 0) {
249 dev_err(dev
, "Invalid target size: %dx%d", tx
, ty
);
254 sy
= s_frame
->height
;
255 if (sx
<= 0 || sy
<= 0) {
256 dev_err(dev
, "Invalid source size: %dx%d", sx
, sy
);
260 sc
->real_height
= sy
;
262 ret
= fimc_get_scaler_factor(sx
, tx
, &sc
->pre_hratio
, &sc
->hfactor
);
266 ret
= fimc_get_scaler_factor(sy
, ty
, &sc
->pre_vratio
, &sc
->vfactor
);
270 sc
->pre_dst_width
= sx
/ sc
->pre_hratio
;
271 sc
->pre_dst_height
= sy
/ sc
->pre_vratio
;
273 if (variant
->has_mainscaler_ext
) {
274 sc
->main_hratio
= (sx
<< 14) / (tx
<< sc
->hfactor
);
275 sc
->main_vratio
= (sy
<< 14) / (ty
<< sc
->vfactor
);
277 sc
->main_hratio
= (sx
<< 8) / (tx
<< sc
->hfactor
);
278 sc
->main_vratio
= (sy
<< 8) / (ty
<< sc
->vfactor
);
282 sc
->scaleup_h
= (tx
>= sx
) ? 1 : 0;
283 sc
->scaleup_v
= (ty
>= sy
) ? 1 : 0;
285 /* check to see if input and output size/format differ */
286 if (s_frame
->fmt
->color
== d_frame
->fmt
->color
287 && s_frame
->width
== d_frame
->width
288 && s_frame
->height
== d_frame
->height
)
296 static void fimc_m2m_job_finish(struct fimc_ctx
*ctx
, int vb_state
)
298 struct vb2_buffer
*src_vb
, *dst_vb
;
300 if (!ctx
|| !ctx
->m2m_ctx
)
303 src_vb
= v4l2_m2m_src_buf_remove(ctx
->m2m_ctx
);
304 dst_vb
= v4l2_m2m_dst_buf_remove(ctx
->m2m_ctx
);
306 if (src_vb
&& dst_vb
) {
307 v4l2_m2m_buf_done(src_vb
, vb_state
);
308 v4l2_m2m_buf_done(dst_vb
, vb_state
);
309 v4l2_m2m_job_finish(ctx
->fimc_dev
->m2m
.m2m_dev
,
314 /* Complete the transaction which has been scheduled for execution. */
315 static int fimc_m2m_shutdown(struct fimc_ctx
*ctx
)
317 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
320 if (!fimc_m2m_pending(fimc
))
323 fimc_ctx_state_set(FIMC_CTX_SHUT
, ctx
);
325 ret
= wait_event_timeout(fimc
->irq_queue
,
326 !fimc_ctx_state_is_set(FIMC_CTX_SHUT
, ctx
),
327 FIMC_SHUTDOWN_TIMEOUT
);
329 return ret
== 0 ? -ETIMEDOUT
: ret
;
332 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
334 struct fimc_ctx
*ctx
= q
->drv_priv
;
337 ret
= pm_runtime_get_sync(&ctx
->fimc_dev
->pdev
->dev
);
338 return ret
> 0 ? 0 : ret
;
341 static int stop_streaming(struct vb2_queue
*q
)
343 struct fimc_ctx
*ctx
= q
->drv_priv
;
346 ret
= fimc_m2m_shutdown(ctx
);
347 if (ret
== -ETIMEDOUT
)
348 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_ERROR
);
350 pm_runtime_put(&ctx
->fimc_dev
->pdev
->dev
);
354 void fimc_capture_irq_handler(struct fimc_dev
*fimc
, bool final
)
356 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
357 struct fimc_vid_buffer
*v_buf
;
361 if (test_and_clear_bit(ST_CAPT_SHUT
, &fimc
->state
)) {
362 wake_up(&fimc
->irq_queue
);
366 if (!list_empty(&cap
->active_buf_q
) &&
367 test_bit(ST_CAPT_RUN
, &fimc
->state
) && final
) {
368 ktime_get_real_ts(&ts
);
370 v_buf
= fimc_active_queue_pop(cap
);
372 tv
= &v_buf
->vb
.v4l2_buf
.timestamp
;
373 tv
->tv_sec
= ts
.tv_sec
;
374 tv
->tv_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
375 v_buf
->vb
.v4l2_buf
.sequence
= cap
->frame_count
++;
377 vb2_buffer_done(&v_buf
->vb
, VB2_BUF_STATE_DONE
);
380 if (!list_empty(&cap
->pending_buf_q
)) {
382 v_buf
= fimc_pending_queue_pop(cap
);
383 fimc_hw_set_output_addr(fimc
, &v_buf
->paddr
, cap
->buf_index
);
384 v_buf
->index
= cap
->buf_index
;
386 /* Move the buffer to the capture active queue */
387 fimc_active_queue_add(cap
, v_buf
);
389 dbg("next frame: %d, done frame: %d",
390 fimc_hw_get_frame_index(fimc
), v_buf
->index
);
392 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
396 if (cap
->active_buf_cnt
== 0) {
398 clear_bit(ST_CAPT_RUN
, &fimc
->state
);
400 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
403 set_bit(ST_CAPT_RUN
, &fimc
->state
);
406 fimc_capture_config_update(cap
->ctx
);
408 dbg("frame: %d, active_buf_cnt: %d",
409 fimc_hw_get_frame_index(fimc
), cap
->active_buf_cnt
);
412 static irqreturn_t
fimc_irq_handler(int irq
, void *priv
)
414 struct fimc_dev
*fimc
= priv
;
415 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
416 struct fimc_ctx
*ctx
;
418 fimc_hw_clear_irq(fimc
);
420 spin_lock(&fimc
->slock
);
422 if (test_and_clear_bit(ST_M2M_PEND
, &fimc
->state
)) {
423 if (test_and_clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
)) {
424 set_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
425 wake_up(&fimc
->irq_queue
);
428 ctx
= v4l2_m2m_get_curr_priv(fimc
->m2m
.m2m_dev
);
430 spin_unlock(&fimc
->slock
);
431 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_DONE
);
433 if (ctx
->state
& FIMC_CTX_SHUT
) {
434 ctx
->state
&= ~FIMC_CTX_SHUT
;
435 wake_up(&fimc
->irq_queue
);
439 } else if (test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
440 fimc_capture_irq_handler(fimc
,
441 !test_bit(ST_CAPT_JPEG
, &fimc
->state
));
442 if (cap
->active_buf_cnt
== 1) {
443 fimc_deactivate_capture(fimc
);
444 clear_bit(ST_CAPT_STREAM
, &fimc
->state
);
448 spin_unlock(&fimc
->slock
);
452 /* The color format (colplanes, memplanes) must be already configured. */
453 int fimc_prepare_addr(struct fimc_ctx
*ctx
, struct vb2_buffer
*vb
,
454 struct fimc_frame
*frame
, struct fimc_addr
*paddr
)
459 if (vb
== NULL
|| frame
== NULL
)
462 pix_size
= frame
->width
* frame
->height
;
464 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
465 frame
->fmt
->memplanes
, frame
->fmt
->colplanes
, pix_size
);
467 paddr
->y
= vb2_dma_contig_plane_dma_addr(vb
, 0);
469 if (frame
->fmt
->memplanes
== 1) {
470 switch (frame
->fmt
->colplanes
) {
476 /* decompose Y into Y/Cb */
477 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
481 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
482 /* decompose Y into Y/Cb/Cr */
483 if (S5P_FIMC_YCBCR420
== frame
->fmt
->color
)
484 paddr
->cr
= (u32
)(paddr
->cb
487 paddr
->cr
= (u32
)(paddr
->cb
494 if (frame
->fmt
->memplanes
>= 2)
495 paddr
->cb
= vb2_dma_contig_plane_dma_addr(vb
, 1);
497 if (frame
->fmt
->memplanes
== 3)
498 paddr
->cr
= vb2_dma_contig_plane_dma_addr(vb
, 2);
501 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
502 paddr
->y
, paddr
->cb
, paddr
->cr
, ret
);
507 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
508 void fimc_set_yuv_order(struct fimc_ctx
*ctx
)
510 /* The one only mode supported in SoC. */
511 ctx
->in_order_2p
= S5P_FIMC_LSB_CRCB
;
512 ctx
->out_order_2p
= S5P_FIMC_LSB_CRCB
;
514 /* Set order for 1 plane input formats. */
515 switch (ctx
->s_frame
.fmt
->color
) {
516 case S5P_FIMC_YCRYCB422
:
517 ctx
->in_order_1p
= S5P_MSCTRL_ORDER422_CBYCRY
;
519 case S5P_FIMC_CBYCRY422
:
520 ctx
->in_order_1p
= S5P_MSCTRL_ORDER422_YCRYCB
;
522 case S5P_FIMC_CRYCBY422
:
523 ctx
->in_order_1p
= S5P_MSCTRL_ORDER422_YCBYCR
;
525 case S5P_FIMC_YCBYCR422
:
527 ctx
->in_order_1p
= S5P_MSCTRL_ORDER422_CRYCBY
;
530 dbg("ctx->in_order_1p= %d", ctx
->in_order_1p
);
532 switch (ctx
->d_frame
.fmt
->color
) {
533 case S5P_FIMC_YCRYCB422
:
534 ctx
->out_order_1p
= S5P_CIOCTRL_ORDER422_CBYCRY
;
536 case S5P_FIMC_CBYCRY422
:
537 ctx
->out_order_1p
= S5P_CIOCTRL_ORDER422_YCRYCB
;
539 case S5P_FIMC_CRYCBY422
:
540 ctx
->out_order_1p
= S5P_CIOCTRL_ORDER422_YCBYCR
;
542 case S5P_FIMC_YCBYCR422
:
544 ctx
->out_order_1p
= S5P_CIOCTRL_ORDER422_CRYCBY
;
547 dbg("ctx->out_order_1p= %d", ctx
->out_order_1p
);
550 void fimc_prepare_dma_offset(struct fimc_ctx
*ctx
, struct fimc_frame
*f
)
552 struct samsung_fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
555 for (i
= 0; i
< f
->fmt
->colplanes
; i
++)
556 depth
+= f
->fmt
->depth
[i
];
558 f
->dma_offset
.y_h
= f
->offs_h
;
559 if (!variant
->pix_hoff
)
560 f
->dma_offset
.y_h
*= (depth
>> 3);
562 f
->dma_offset
.y_v
= f
->offs_v
;
564 f
->dma_offset
.cb_h
= f
->offs_h
;
565 f
->dma_offset
.cb_v
= f
->offs_v
;
567 f
->dma_offset
.cr_h
= f
->offs_h
;
568 f
->dma_offset
.cr_v
= f
->offs_v
;
570 if (!variant
->pix_hoff
) {
571 if (f
->fmt
->colplanes
== 3) {
572 f
->dma_offset
.cb_h
>>= 1;
573 f
->dma_offset
.cr_h
>>= 1;
575 if (f
->fmt
->color
== S5P_FIMC_YCBCR420
) {
576 f
->dma_offset
.cb_v
>>= 1;
577 f
->dma_offset
.cr_v
>>= 1;
581 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
582 f
->fmt
->color
, f
->dma_offset
.y_h
, f
->dma_offset
.y_v
);
586 * fimc_prepare_config - check dimensions, operation and color mode
587 * and pre-calculate offset and the scaling coefficients.
589 * @ctx: hardware context information
590 * @flags: flags indicating which parameters to check/update
592 * Return: 0 if dimensions are valid or non zero otherwise.
594 int fimc_prepare_config(struct fimc_ctx
*ctx
, u32 flags
)
596 struct fimc_frame
*s_frame
, *d_frame
;
597 struct vb2_buffer
*vb
= NULL
;
600 s_frame
= &ctx
->s_frame
;
601 d_frame
= &ctx
->d_frame
;
603 if (flags
& FIMC_PARAMS
) {
604 /* Prepare the DMA offset ratios for scaler. */
605 fimc_prepare_dma_offset(ctx
, &ctx
->s_frame
);
606 fimc_prepare_dma_offset(ctx
, &ctx
->d_frame
);
608 if (s_frame
->height
> (SCALER_MAX_VRATIO
* d_frame
->height
) ||
609 s_frame
->width
> (SCALER_MAX_HRATIO
* d_frame
->width
)) {
610 err("out of scaler range");
613 fimc_set_yuv_order(ctx
);
616 if (flags
& FIMC_SRC_ADDR
) {
617 vb
= v4l2_m2m_next_src_buf(ctx
->m2m_ctx
);
618 ret
= fimc_prepare_addr(ctx
, vb
, s_frame
, &s_frame
->paddr
);
623 if (flags
& FIMC_DST_ADDR
) {
624 vb
= v4l2_m2m_next_dst_buf(ctx
->m2m_ctx
);
625 ret
= fimc_prepare_addr(ctx
, vb
, d_frame
, &d_frame
->paddr
);
631 static void fimc_dma_run(void *priv
)
633 struct fimc_ctx
*ctx
= priv
;
634 struct fimc_dev
*fimc
;
638 if (WARN(!ctx
, "null hardware context\n"))
641 fimc
= ctx
->fimc_dev
;
642 spin_lock_irqsave(&fimc
->slock
, flags
);
643 set_bit(ST_M2M_PEND
, &fimc
->state
);
645 ctx
->state
|= (FIMC_SRC_ADDR
| FIMC_DST_ADDR
);
646 ret
= fimc_prepare_config(ctx
, ctx
->state
);
650 /* Reconfigure hardware if the context has changed. */
651 if (fimc
->m2m
.ctx
!= ctx
) {
652 ctx
->state
|= FIMC_PARAMS
;
655 fimc_hw_set_input_addr(fimc
, &ctx
->s_frame
.paddr
);
657 if (ctx
->state
& FIMC_PARAMS
) {
658 fimc_hw_set_input_path(ctx
);
659 fimc_hw_set_in_dma(ctx
);
660 ret
= fimc_set_scaler_info(ctx
);
663 fimc_hw_set_prescaler(ctx
);
664 fimc_hw_set_mainscaler(ctx
);
665 fimc_hw_set_target_format(ctx
);
666 fimc_hw_set_rotation(ctx
);
667 fimc_hw_set_effect(ctx
, false);
670 fimc_hw_set_output_path(ctx
);
671 if (ctx
->state
& (FIMC_DST_ADDR
| FIMC_PARAMS
))
672 fimc_hw_set_output_addr(fimc
, &ctx
->d_frame
.paddr
, -1);
674 if (ctx
->state
& FIMC_PARAMS
) {
675 fimc_hw_set_out_dma(ctx
);
676 if (fimc
->variant
->has_alpha
)
677 fimc_hw_set_rgb_alpha(ctx
);
680 fimc_activate_capture(ctx
);
682 ctx
->state
&= (FIMC_CTX_M2M
| FIMC_CTX_CAP
|
683 FIMC_SRC_FMT
| FIMC_DST_FMT
);
684 fimc_hw_activate_input_dma(fimc
, true);
686 spin_unlock_irqrestore(&fimc
->slock
, flags
);
689 static void fimc_job_abort(void *priv
)
691 fimc_m2m_shutdown(priv
);
694 static int fimc_queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*fmt
,
695 unsigned int *num_buffers
, unsigned int *num_planes
,
696 unsigned int sizes
[], void *allocators
[])
698 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
699 struct fimc_frame
*f
;
702 f
= ctx_get_frame(ctx
, vq
->type
);
706 * Return number of non-contigous planes (plane buffers)
707 * depending on the configured color format.
712 *num_planes
= f
->fmt
->memplanes
;
713 for (i
= 0; i
< f
->fmt
->memplanes
; i
++) {
714 sizes
[i
] = (f
->f_width
* f
->f_height
* f
->fmt
->depth
[i
]) / 8;
715 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
720 static int fimc_buf_prepare(struct vb2_buffer
*vb
)
722 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
723 struct fimc_frame
*frame
;
726 frame
= ctx_get_frame(ctx
, vb
->vb2_queue
->type
);
728 return PTR_ERR(frame
);
730 for (i
= 0; i
< frame
->fmt
->memplanes
; i
++)
731 vb2_set_plane_payload(vb
, i
, frame
->payload
[i
]);
736 static void fimc_buf_queue(struct vb2_buffer
*vb
)
738 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
740 dbg("ctx: %p, ctx->state: 0x%x", ctx
, ctx
->state
);
743 v4l2_m2m_buf_queue(ctx
->m2m_ctx
, vb
);
746 static void fimc_lock(struct vb2_queue
*vq
)
748 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
749 mutex_lock(&ctx
->fimc_dev
->lock
);
752 static void fimc_unlock(struct vb2_queue
*vq
)
754 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
755 mutex_unlock(&ctx
->fimc_dev
->lock
);
758 static struct vb2_ops fimc_qops
= {
759 .queue_setup
= fimc_queue_setup
,
760 .buf_prepare
= fimc_buf_prepare
,
761 .buf_queue
= fimc_buf_queue
,
762 .wait_prepare
= fimc_unlock
,
763 .wait_finish
= fimc_lock
,
764 .stop_streaming
= stop_streaming
,
765 .start_streaming
= start_streaming
,
769 * V4L2 controls handling
771 #define ctrl_to_ctx(__ctrl) \
772 container_of((__ctrl)->handler, struct fimc_ctx, ctrl_handler)
774 static int __fimc_s_ctrl(struct fimc_ctx
*ctx
, struct v4l2_ctrl
*ctrl
)
776 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
777 struct samsung_fimc_variant
*variant
= fimc
->variant
;
778 unsigned int flags
= FIMC_DST_FMT
| FIMC_SRC_FMT
;
781 if (ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
)
786 ctx
->hflip
= ctrl
->val
;
790 ctx
->vflip
= ctrl
->val
;
793 case V4L2_CID_ROTATE
:
794 if (fimc_capture_pending(fimc
) ||
795 (ctx
->state
& flags
) == flags
) {
796 ret
= fimc_check_scaler_ratio(ctx
, ctx
->s_frame
.width
,
797 ctx
->s_frame
.height
, ctx
->d_frame
.width
,
798 ctx
->d_frame
.height
, ctrl
->val
);
802 if ((ctrl
->val
== 90 || ctrl
->val
== 270) &&
803 !variant
->has_out_rot
)
806 ctx
->rotation
= ctrl
->val
;
809 case V4L2_CID_ALPHA_COMPONENT
:
810 ctx
->d_frame
.alpha
= ctrl
->val
;
813 ctx
->state
|= FIMC_PARAMS
;
814 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
818 static int fimc_s_ctrl(struct v4l2_ctrl
*ctrl
)
820 struct fimc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
824 spin_lock_irqsave(&ctx
->fimc_dev
->slock
, flags
);
825 ret
= __fimc_s_ctrl(ctx
, ctrl
);
826 spin_unlock_irqrestore(&ctx
->fimc_dev
->slock
, flags
);
831 static const struct v4l2_ctrl_ops fimc_ctrl_ops
= {
832 .s_ctrl
= fimc_s_ctrl
,
835 int fimc_ctrls_create(struct fimc_ctx
*ctx
)
837 struct samsung_fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
838 unsigned int max_alpha
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
842 v4l2_ctrl_handler_init(&ctx
->ctrl_handler
, 4);
844 ctx
->ctrl_rotate
= v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &fimc_ctrl_ops
,
845 V4L2_CID_ROTATE
, 0, 270, 90, 0);
846 ctx
->ctrl_hflip
= v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &fimc_ctrl_ops
,
847 V4L2_CID_HFLIP
, 0, 1, 1, 0);
848 ctx
->ctrl_vflip
= v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &fimc_ctrl_ops
,
849 V4L2_CID_VFLIP
, 0, 1, 1, 0);
850 if (variant
->has_alpha
)
851 ctx
->ctrl_alpha
= v4l2_ctrl_new_std(&ctx
->ctrl_handler
,
852 &fimc_ctrl_ops
, V4L2_CID_ALPHA_COMPONENT
,
855 ctx
->ctrl_alpha
= NULL
;
857 ctx
->ctrls_rdy
= ctx
->ctrl_handler
.error
== 0;
859 return ctx
->ctrl_handler
.error
;
862 void fimc_ctrls_delete(struct fimc_ctx
*ctx
)
864 if (ctx
->ctrls_rdy
) {
865 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
866 ctx
->ctrls_rdy
= false;
867 ctx
->ctrl_alpha
= NULL
;
871 void fimc_ctrls_activate(struct fimc_ctx
*ctx
, bool active
)
873 unsigned int has_alpha
= ctx
->d_frame
.fmt
->flags
& FMT_HAS_ALPHA
;
878 mutex_lock(&ctx
->ctrl_handler
.lock
);
879 v4l2_ctrl_activate(ctx
->ctrl_rotate
, active
);
880 v4l2_ctrl_activate(ctx
->ctrl_hflip
, active
);
881 v4l2_ctrl_activate(ctx
->ctrl_vflip
, active
);
883 v4l2_ctrl_activate(ctx
->ctrl_alpha
, active
&& has_alpha
);
886 ctx
->rotation
= ctx
->ctrl_rotate
->val
;
887 ctx
->hflip
= ctx
->ctrl_hflip
->val
;
888 ctx
->vflip
= ctx
->ctrl_vflip
->val
;
894 mutex_unlock(&ctx
->ctrl_handler
.lock
);
897 /* Update maximum value of the alpha color control */
898 void fimc_alpha_ctrl_update(struct fimc_ctx
*ctx
)
900 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
901 struct v4l2_ctrl
*ctrl
= ctx
->ctrl_alpha
;
903 if (ctrl
== NULL
|| !fimc
->variant
->has_alpha
)
906 v4l2_ctrl_lock(ctrl
);
907 ctrl
->maximum
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
909 if (ctrl
->cur
.val
> ctrl
->maximum
)
910 ctrl
->cur
.val
= ctrl
->maximum
;
912 v4l2_ctrl_unlock(ctrl
);
916 * V4L2 ioctl handlers
918 static int fimc_m2m_querycap(struct file
*file
, void *fh
,
919 struct v4l2_capability
*cap
)
921 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
922 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
924 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
925 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
926 cap
->bus_info
[0] = 0;
927 cap
->capabilities
= V4L2_CAP_STREAMING
|
928 V4L2_CAP_VIDEO_CAPTURE_MPLANE
| V4L2_CAP_VIDEO_OUTPUT_MPLANE
;
933 static int fimc_m2m_enum_fmt_mplane(struct file
*file
, void *priv
,
934 struct v4l2_fmtdesc
*f
)
936 struct fimc_fmt
*fmt
;
938 fmt
= fimc_find_format(NULL
, NULL
, get_m2m_fmt_flags(f
->type
),
943 strncpy(f
->description
, fmt
->name
, sizeof(f
->description
) - 1);
944 f
->pixelformat
= fmt
->fourcc
;
948 int fimc_fill_format(struct fimc_frame
*frame
, struct v4l2_format
*f
)
950 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
953 pixm
->width
= frame
->o_width
;
954 pixm
->height
= frame
->o_height
;
955 pixm
->field
= V4L2_FIELD_NONE
;
956 pixm
->pixelformat
= frame
->fmt
->fourcc
;
957 pixm
->colorspace
= V4L2_COLORSPACE_JPEG
;
958 pixm
->num_planes
= frame
->fmt
->memplanes
;
960 for (i
= 0; i
< pixm
->num_planes
; ++i
) {
961 int bpl
= frame
->f_width
;
962 if (frame
->fmt
->colplanes
== 1) /* packed formats */
963 bpl
= (bpl
* frame
->fmt
->depth
[0]) / 8;
964 pixm
->plane_fmt
[i
].bytesperline
= bpl
;
965 pixm
->plane_fmt
[i
].sizeimage
= (frame
->o_width
*
966 frame
->o_height
* frame
->fmt
->depth
[i
]) / 8;
971 void fimc_fill_frame(struct fimc_frame
*frame
, struct v4l2_format
*f
)
973 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
975 frame
->f_width
= pixm
->plane_fmt
[0].bytesperline
;
976 if (frame
->fmt
->colplanes
== 1)
977 frame
->f_width
= (frame
->f_width
* 8) / frame
->fmt
->depth
[0];
978 frame
->f_height
= pixm
->height
;
979 frame
->width
= pixm
->width
;
980 frame
->height
= pixm
->height
;
981 frame
->o_width
= pixm
->width
;
982 frame
->o_height
= pixm
->height
;
988 * fimc_adjust_mplane_format - adjust bytesperline/sizeimage for each plane
989 * @fmt: fimc pixel format description (input)
990 * @width: requested pixel width
991 * @height: requested pixel height
992 * @pix: multi-plane format to adjust
994 void fimc_adjust_mplane_format(struct fimc_fmt
*fmt
, u32 width
, u32 height
,
995 struct v4l2_pix_format_mplane
*pix
)
997 u32 bytesperline
= 0;
1000 pix
->colorspace
= V4L2_COLORSPACE_JPEG
;
1001 pix
->field
= V4L2_FIELD_NONE
;
1002 pix
->num_planes
= fmt
->memplanes
;
1003 pix
->pixelformat
= fmt
->fourcc
;
1004 pix
->height
= height
;
1007 for (i
= 0; i
< pix
->num_planes
; ++i
) {
1008 u32 bpl
= pix
->plane_fmt
[i
].bytesperline
;
1009 u32
*sizeimage
= &pix
->plane_fmt
[i
].sizeimage
;
1011 if (fmt
->colplanes
> 1 && (bpl
== 0 || bpl
< pix
->width
))
1012 bpl
= pix
->width
; /* Planar */
1014 if (fmt
->colplanes
== 1 && /* Packed */
1015 (bpl
== 0 || ((bpl
* 8) / fmt
->depth
[i
]) < pix
->width
))
1016 bpl
= (pix
->width
* fmt
->depth
[0]) / 8;
1018 if (i
== 0) /* Same bytesperline for each plane. */
1021 pix
->plane_fmt
[i
].bytesperline
= bytesperline
;
1022 *sizeimage
= (pix
->width
* pix
->height
* fmt
->depth
[i
]) / 8;
1026 static int fimc_m2m_g_fmt_mplane(struct file
*file
, void *fh
,
1027 struct v4l2_format
*f
)
1029 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1030 struct fimc_frame
*frame
= ctx_get_frame(ctx
, f
->type
);
1033 return PTR_ERR(frame
);
1035 return fimc_fill_format(frame
, f
);
1039 * fimc_find_format - lookup fimc color format by fourcc or media bus format
1040 * @pixelformat: fourcc to match, ignored if null
1041 * @mbus_code: media bus code to match, ignored if null
1042 * @mask: the color flags to match
1043 * @index: offset in the fimc_formats array, ignored if negative
1045 struct fimc_fmt
*fimc_find_format(u32
*pixelformat
, u32
*mbus_code
,
1046 unsigned int mask
, int index
)
1048 struct fimc_fmt
*fmt
, *def_fmt
= NULL
;
1052 if (index
>= ARRAY_SIZE(fimc_formats
))
1055 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
1056 fmt
= &fimc_formats
[i
];
1057 if (!(fmt
->flags
& mask
))
1059 if (pixelformat
&& fmt
->fourcc
== *pixelformat
)
1061 if (mbus_code
&& fmt
->mbus_code
== *mbus_code
)
1070 static int fimc_try_fmt_mplane(struct fimc_ctx
*ctx
, struct v4l2_format
*f
)
1072 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1073 struct samsung_fimc_variant
*variant
= fimc
->variant
;
1074 struct v4l2_pix_format_mplane
*pix
= &f
->fmt
.pix_mp
;
1075 struct fimc_fmt
*fmt
;
1076 u32 max_w
, mod_x
, mod_y
;
1078 if (!IS_M2M(f
->type
))
1081 dbg("w: %d, h: %d", pix
->width
, pix
->height
);
1083 fmt
= fimc_find_format(&pix
->pixelformat
, NULL
,
1084 get_m2m_fmt_flags(f
->type
), 0);
1085 if (WARN(fmt
== NULL
, "Pixel format lookup failed"))
1088 if (pix
->field
== V4L2_FIELD_ANY
)
1089 pix
->field
= V4L2_FIELD_NONE
;
1090 else if (pix
->field
!= V4L2_FIELD_NONE
)
1093 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1094 max_w
= variant
->pix_limit
->scaler_dis_w
;
1095 mod_x
= ffs(variant
->min_inp_pixsize
) - 1;
1097 max_w
= variant
->pix_limit
->out_rot_dis_w
;
1098 mod_x
= ffs(variant
->min_out_pixsize
) - 1;
1101 if (tiled_fmt(fmt
)) {
1102 mod_x
= 6; /* 64 x 32 pixels tile */
1105 if (variant
->min_vsize_align
== 1)
1106 mod_y
= fimc_fmt_is_rgb(fmt
->color
) ? 0 : 1;
1108 mod_y
= ffs(variant
->min_vsize_align
) - 1;
1111 v4l_bound_align_image(&pix
->width
, 16, max_w
, mod_x
,
1112 &pix
->height
, 8, variant
->pix_limit
->scaler_dis_w
, mod_y
, 0);
1114 fimc_adjust_mplane_format(fmt
, pix
->width
, pix
->height
, &f
->fmt
.pix_mp
);
1118 static int fimc_m2m_try_fmt_mplane(struct file
*file
, void *fh
,
1119 struct v4l2_format
*f
)
1121 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1123 return fimc_try_fmt_mplane(ctx
, f
);
1126 static int fimc_m2m_s_fmt_mplane(struct file
*file
, void *fh
,
1127 struct v4l2_format
*f
)
1129 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1130 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1131 struct vb2_queue
*vq
;
1132 struct fimc_frame
*frame
;
1133 struct v4l2_pix_format_mplane
*pix
;
1136 ret
= fimc_try_fmt_mplane(ctx
, f
);
1140 vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
, f
->type
);
1142 if (vb2_is_busy(vq
)) {
1143 v4l2_err(fimc
->m2m
.vfd
, "queue (%d) busy\n", f
->type
);
1147 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1148 frame
= &ctx
->s_frame
;
1150 frame
= &ctx
->d_frame
;
1152 pix
= &f
->fmt
.pix_mp
;
1153 frame
->fmt
= fimc_find_format(&pix
->pixelformat
, NULL
,
1154 get_m2m_fmt_flags(f
->type
), 0);
1158 /* Update RGB Alpha control state and value range */
1159 fimc_alpha_ctrl_update(ctx
);
1161 for (i
= 0; i
< frame
->fmt
->colplanes
; i
++) {
1163 (pix
->width
* pix
->height
* frame
->fmt
->depth
[i
]) / 8;
1166 fimc_fill_frame(frame
, f
);
1168 ctx
->scaler
.enabled
= 1;
1170 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1171 fimc_ctx_state_set(FIMC_PARAMS
| FIMC_DST_FMT
, ctx
);
1173 fimc_ctx_state_set(FIMC_PARAMS
| FIMC_SRC_FMT
, ctx
);
1175 dbg("f_w: %d, f_h: %d", frame
->f_width
, frame
->f_height
);
1180 static int fimc_m2m_reqbufs(struct file
*file
, void *fh
,
1181 struct v4l2_requestbuffers
*reqbufs
)
1183 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1185 return v4l2_m2m_reqbufs(file
, ctx
->m2m_ctx
, reqbufs
);
1188 static int fimc_m2m_querybuf(struct file
*file
, void *fh
,
1189 struct v4l2_buffer
*buf
)
1191 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1193 return v4l2_m2m_querybuf(file
, ctx
->m2m_ctx
, buf
);
1196 static int fimc_m2m_qbuf(struct file
*file
, void *fh
,
1197 struct v4l2_buffer
*buf
)
1199 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1201 return v4l2_m2m_qbuf(file
, ctx
->m2m_ctx
, buf
);
1204 static int fimc_m2m_dqbuf(struct file
*file
, void *fh
,
1205 struct v4l2_buffer
*buf
)
1207 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1209 return v4l2_m2m_dqbuf(file
, ctx
->m2m_ctx
, buf
);
1212 static int fimc_m2m_streamon(struct file
*file
, void *fh
,
1213 enum v4l2_buf_type type
)
1215 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1217 /* The source and target color format need to be set */
1218 if (V4L2_TYPE_IS_OUTPUT(type
)) {
1219 if (!fimc_ctx_state_is_set(FIMC_SRC_FMT
, ctx
))
1221 } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT
, ctx
)) {
1225 return v4l2_m2m_streamon(file
, ctx
->m2m_ctx
, type
);
1228 static int fimc_m2m_streamoff(struct file
*file
, void *fh
,
1229 enum v4l2_buf_type type
)
1231 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1233 return v4l2_m2m_streamoff(file
, ctx
->m2m_ctx
, type
);
1236 static int fimc_m2m_cropcap(struct file
*file
, void *fh
,
1237 struct v4l2_cropcap
*cr
)
1239 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1240 struct fimc_frame
*frame
;
1242 frame
= ctx_get_frame(ctx
, cr
->type
);
1244 return PTR_ERR(frame
);
1246 cr
->bounds
.left
= 0;
1248 cr
->bounds
.width
= frame
->o_width
;
1249 cr
->bounds
.height
= frame
->o_height
;
1250 cr
->defrect
= cr
->bounds
;
1255 static int fimc_m2m_g_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1257 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1258 struct fimc_frame
*frame
;
1260 frame
= ctx_get_frame(ctx
, cr
->type
);
1262 return PTR_ERR(frame
);
1264 cr
->c
.left
= frame
->offs_h
;
1265 cr
->c
.top
= frame
->offs_v
;
1266 cr
->c
.width
= frame
->width
;
1267 cr
->c
.height
= frame
->height
;
1272 static int fimc_m2m_try_crop(struct fimc_ctx
*ctx
, struct v4l2_crop
*cr
)
1274 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1275 struct fimc_frame
*f
;
1276 u32 min_size
, halign
, depth
= 0;
1279 if (cr
->c
.top
< 0 || cr
->c
.left
< 0) {
1280 v4l2_err(fimc
->m2m
.vfd
,
1281 "doesn't support negative values for top & left\n");
1284 if (cr
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
1286 else if (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
1291 min_size
= (f
== &ctx
->s_frame
) ?
1292 fimc
->variant
->min_inp_pixsize
: fimc
->variant
->min_out_pixsize
;
1294 /* Get pixel alignment constraints. */
1295 if (fimc
->variant
->min_vsize_align
== 1)
1296 halign
= fimc_fmt_is_rgb(f
->fmt
->color
) ? 0 : 1;
1298 halign
= ffs(fimc
->variant
->min_vsize_align
) - 1;
1300 for (i
= 0; i
< f
->fmt
->colplanes
; i
++)
1301 depth
+= f
->fmt
->depth
[i
];
1303 v4l_bound_align_image(&cr
->c
.width
, min_size
, f
->o_width
,
1305 &cr
->c
.height
, min_size
, f
->o_height
,
1306 halign
, 64/(ALIGN(depth
, 8)));
1308 /* adjust left/top if cropping rectangle is out of bounds */
1309 if (cr
->c
.left
+ cr
->c
.width
> f
->o_width
)
1310 cr
->c
.left
= f
->o_width
- cr
->c
.width
;
1311 if (cr
->c
.top
+ cr
->c
.height
> f
->o_height
)
1312 cr
->c
.top
= f
->o_height
- cr
->c
.height
;
1314 cr
->c
.left
= round_down(cr
->c
.left
, min_size
);
1315 cr
->c
.top
= round_down(cr
->c
.top
, fimc
->variant
->hor_offs_align
);
1317 dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
1318 cr
->c
.left
, cr
->c
.top
, cr
->c
.width
, cr
->c
.height
,
1319 f
->f_width
, f
->f_height
);
1324 static int fimc_m2m_s_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
1326 struct fimc_ctx
*ctx
= fh_to_ctx(fh
);
1327 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1328 struct fimc_frame
*f
;
1331 ret
= fimc_m2m_try_crop(ctx
, cr
);
1335 f
= (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) ?
1336 &ctx
->s_frame
: &ctx
->d_frame
;
1338 /* Check to see if scaling ratio is within supported range */
1339 if (fimc_ctx_state_is_set(FIMC_DST_FMT
| FIMC_SRC_FMT
, ctx
)) {
1340 if (cr
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1341 ret
= fimc_check_scaler_ratio(ctx
, cr
->c
.width
,
1342 cr
->c
.height
, ctx
->d_frame
.width
,
1343 ctx
->d_frame
.height
, ctx
->rotation
);
1345 ret
= fimc_check_scaler_ratio(ctx
, ctx
->s_frame
.width
,
1346 ctx
->s_frame
.height
, cr
->c
.width
,
1347 cr
->c
.height
, ctx
->rotation
);
1350 v4l2_err(fimc
->m2m
.vfd
, "Out of scaler range\n");
1355 f
->offs_h
= cr
->c
.left
;
1356 f
->offs_v
= cr
->c
.top
;
1357 f
->width
= cr
->c
.width
;
1358 f
->height
= cr
->c
.height
;
1360 fimc_ctx_state_set(FIMC_PARAMS
, ctx
);
1365 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops
= {
1366 .vidioc_querycap
= fimc_m2m_querycap
,
1368 .vidioc_enum_fmt_vid_cap_mplane
= fimc_m2m_enum_fmt_mplane
,
1369 .vidioc_enum_fmt_vid_out_mplane
= fimc_m2m_enum_fmt_mplane
,
1371 .vidioc_g_fmt_vid_cap_mplane
= fimc_m2m_g_fmt_mplane
,
1372 .vidioc_g_fmt_vid_out_mplane
= fimc_m2m_g_fmt_mplane
,
1374 .vidioc_try_fmt_vid_cap_mplane
= fimc_m2m_try_fmt_mplane
,
1375 .vidioc_try_fmt_vid_out_mplane
= fimc_m2m_try_fmt_mplane
,
1377 .vidioc_s_fmt_vid_cap_mplane
= fimc_m2m_s_fmt_mplane
,
1378 .vidioc_s_fmt_vid_out_mplane
= fimc_m2m_s_fmt_mplane
,
1380 .vidioc_reqbufs
= fimc_m2m_reqbufs
,
1381 .vidioc_querybuf
= fimc_m2m_querybuf
,
1383 .vidioc_qbuf
= fimc_m2m_qbuf
,
1384 .vidioc_dqbuf
= fimc_m2m_dqbuf
,
1386 .vidioc_streamon
= fimc_m2m_streamon
,
1387 .vidioc_streamoff
= fimc_m2m_streamoff
,
1389 .vidioc_g_crop
= fimc_m2m_g_crop
,
1390 .vidioc_s_crop
= fimc_m2m_s_crop
,
1391 .vidioc_cropcap
= fimc_m2m_cropcap
1395 static int queue_init(void *priv
, struct vb2_queue
*src_vq
,
1396 struct vb2_queue
*dst_vq
)
1398 struct fimc_ctx
*ctx
= priv
;
1401 memset(src_vq
, 0, sizeof(*src_vq
));
1402 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
1403 src_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1404 src_vq
->drv_priv
= ctx
;
1405 src_vq
->ops
= &fimc_qops
;
1406 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
1407 src_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1409 ret
= vb2_queue_init(src_vq
);
1413 memset(dst_vq
, 0, sizeof(*dst_vq
));
1414 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1415 dst_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1416 dst_vq
->drv_priv
= ctx
;
1417 dst_vq
->ops
= &fimc_qops
;
1418 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
1419 dst_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
1421 return vb2_queue_init(dst_vq
);
1424 static int fimc_m2m_open(struct file
*file
)
1426 struct fimc_dev
*fimc
= video_drvdata(file
);
1427 struct fimc_ctx
*ctx
;
1430 dbg("pid: %d, state: 0x%lx, refcnt: %d",
1431 task_pid_nr(current
), fimc
->state
, fimc
->vid_cap
.refcnt
);
1434 * Return if the corresponding video capture node
1435 * is already opened.
1437 if (fimc
->vid_cap
.refcnt
> 0)
1440 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
1443 v4l2_fh_init(&ctx
->fh
, fimc
->m2m
.vfd
);
1444 ctx
->fimc_dev
= fimc
;
1446 /* Default color format */
1447 ctx
->s_frame
.fmt
= &fimc_formats
[0];
1448 ctx
->d_frame
.fmt
= &fimc_formats
[0];
1450 ret
= fimc_ctrls_create(ctx
);
1454 /* Use separate control handler per file handle */
1455 ctx
->fh
.ctrl_handler
= &ctx
->ctrl_handler
;
1456 file
->private_data
= &ctx
->fh
;
1457 v4l2_fh_add(&ctx
->fh
);
1459 /* Setup the device context for memory-to-memory mode */
1460 ctx
->state
= FIMC_CTX_M2M
;
1462 ctx
->in_path
= FIMC_DMA
;
1463 ctx
->out_path
= FIMC_DMA
;
1465 ctx
->m2m_ctx
= v4l2_m2m_ctx_init(fimc
->m2m
.m2m_dev
, ctx
, queue_init
);
1466 if (IS_ERR(ctx
->m2m_ctx
)) {
1467 ret
= PTR_ERR(ctx
->m2m_ctx
);
1471 if (fimc
->m2m
.refcnt
++ == 0)
1472 set_bit(ST_M2M_RUN
, &fimc
->state
);
1476 fimc_ctrls_delete(ctx
);
1478 v4l2_fh_del(&ctx
->fh
);
1479 v4l2_fh_exit(&ctx
->fh
);
1484 static int fimc_m2m_release(struct file
*file
)
1486 struct fimc_ctx
*ctx
= fh_to_ctx(file
->private_data
);
1487 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
1489 dbg("pid: %d, state: 0x%lx, refcnt= %d",
1490 task_pid_nr(current
), fimc
->state
, fimc
->m2m
.refcnt
);
1492 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
1493 fimc_ctrls_delete(ctx
);
1494 v4l2_fh_del(&ctx
->fh
);
1495 v4l2_fh_exit(&ctx
->fh
);
1497 if (--fimc
->m2m
.refcnt
<= 0)
1498 clear_bit(ST_M2M_RUN
, &fimc
->state
);
1503 static unsigned int fimc_m2m_poll(struct file
*file
,
1504 struct poll_table_struct
*wait
)
1506 struct fimc_ctx
*ctx
= fh_to_ctx(file
->private_data
);
1508 return v4l2_m2m_poll(file
, ctx
->m2m_ctx
, wait
);
1512 static int fimc_m2m_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1514 struct fimc_ctx
*ctx
= fh_to_ctx(file
->private_data
);
1516 return v4l2_m2m_mmap(file
, ctx
->m2m_ctx
, vma
);
1519 static const struct v4l2_file_operations fimc_m2m_fops
= {
1520 .owner
= THIS_MODULE
,
1521 .open
= fimc_m2m_open
,
1522 .release
= fimc_m2m_release
,
1523 .poll
= fimc_m2m_poll
,
1524 .unlocked_ioctl
= video_ioctl2
,
1525 .mmap
= fimc_m2m_mmap
,
1528 static struct v4l2_m2m_ops m2m_ops
= {
1529 .device_run
= fimc_dma_run
,
1530 .job_abort
= fimc_job_abort
,
1533 int fimc_register_m2m_device(struct fimc_dev
*fimc
,
1534 struct v4l2_device
*v4l2_dev
)
1536 struct video_device
*vfd
;
1537 struct platform_device
*pdev
;
1544 fimc
->v4l2_dev
= v4l2_dev
;
1546 vfd
= video_device_alloc();
1548 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
1552 vfd
->fops
= &fimc_m2m_fops
;
1553 vfd
->ioctl_ops
= &fimc_m2m_ioctl_ops
;
1554 vfd
->v4l2_dev
= v4l2_dev
;
1556 vfd
->release
= video_device_release
;
1557 vfd
->lock
= &fimc
->lock
;
1559 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s.m2m", dev_name(&pdev
->dev
));
1560 video_set_drvdata(vfd
, fimc
);
1562 fimc
->m2m
.vfd
= vfd
;
1563 fimc
->m2m
.m2m_dev
= v4l2_m2m_init(&m2m_ops
);
1564 if (IS_ERR(fimc
->m2m
.m2m_dev
)) {
1565 v4l2_err(v4l2_dev
, "failed to initialize v4l2-m2m device\n");
1566 ret
= PTR_ERR(fimc
->m2m
.m2m_dev
);
1570 ret
= media_entity_init(&vfd
->entity
, 0, NULL
, 0);
1574 v4l2_m2m_release(fimc
->m2m
.m2m_dev
);
1576 video_device_release(fimc
->m2m
.vfd
);
1580 void fimc_unregister_m2m_device(struct fimc_dev
*fimc
)
1585 if (fimc
->m2m
.m2m_dev
)
1586 v4l2_m2m_release(fimc
->m2m
.m2m_dev
);
1587 if (fimc
->m2m
.vfd
) {
1588 media_entity_cleanup(&fimc
->m2m
.vfd
->entity
);
1589 /* Can also be called if video device wasn't registered */
1590 video_unregister_device(fimc
->m2m
.vfd
);
1594 static void fimc_clk_put(struct fimc_dev
*fimc
)
1597 for (i
= 0; i
< fimc
->num_clocks
; i
++) {
1598 if (IS_ERR_OR_NULL(fimc
->clock
[i
]))
1600 clk_unprepare(fimc
->clock
[i
]);
1601 clk_put(fimc
->clock
[i
]);
1602 fimc
->clock
[i
] = NULL
;
1606 static int fimc_clk_get(struct fimc_dev
*fimc
)
1610 for (i
= 0; i
< fimc
->num_clocks
; i
++) {
1611 fimc
->clock
[i
] = clk_get(&fimc
->pdev
->dev
, fimc_clocks
[i
]);
1612 if (IS_ERR(fimc
->clock
[i
]))
1614 ret
= clk_prepare(fimc
->clock
[i
]);
1616 clk_put(fimc
->clock
[i
]);
1617 fimc
->clock
[i
] = NULL
;
1624 dev_err(&fimc
->pdev
->dev
, "failed to get clock: %s\n",
1629 static int fimc_m2m_suspend(struct fimc_dev
*fimc
)
1631 unsigned long flags
;
1634 spin_lock_irqsave(&fimc
->slock
, flags
);
1635 if (!fimc_m2m_pending(fimc
)) {
1636 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1639 clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
1640 set_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
1641 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1643 timeout
= wait_event_timeout(fimc
->irq_queue
,
1644 test_bit(ST_M2M_SUSPENDED
, &fimc
->state
),
1645 FIMC_SHUTDOWN_TIMEOUT
);
1647 clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
1648 return timeout
== 0 ? -EAGAIN
: 0;
1651 static int fimc_m2m_resume(struct fimc_dev
*fimc
)
1653 unsigned long flags
;
1655 spin_lock_irqsave(&fimc
->slock
, flags
);
1656 /* Clear for full H/W setup in first run after resume */
1657 fimc
->m2m
.ctx
= NULL
;
1658 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1660 if (test_and_clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
))
1661 fimc_m2m_job_finish(fimc
->m2m
.ctx
,
1662 VB2_BUF_STATE_ERROR
);
1666 static int fimc_probe(struct platform_device
*pdev
)
1668 struct fimc_dev
*fimc
;
1669 struct resource
*res
;
1670 struct samsung_fimc_driverdata
*drv_data
;
1671 struct s5p_platform_fimc
*pdata
;
1674 drv_data
= (struct samsung_fimc_driverdata
*)
1675 platform_get_device_id(pdev
)->driver_data
;
1677 if (pdev
->id
>= drv_data
->num_entities
) {
1678 dev_err(&pdev
->dev
, "Invalid platform device id: %d\n",
1683 fimc
= devm_kzalloc(&pdev
->dev
, sizeof(*fimc
), GFP_KERNEL
);
1687 fimc
->id
= pdev
->id
;
1689 fimc
->variant
= drv_data
->variant
[fimc
->id
];
1691 pdata
= pdev
->dev
.platform_data
;
1692 fimc
->pdata
= pdata
;
1694 init_waitqueue_head(&fimc
->irq_queue
);
1695 spin_lock_init(&fimc
->slock
);
1696 mutex_init(&fimc
->lock
);
1698 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1699 fimc
->regs
= devm_request_and_ioremap(&pdev
->dev
, res
);
1700 if (fimc
->regs
== NULL
) {
1701 dev_err(&pdev
->dev
, "Failed to obtain io memory\n");
1705 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1707 dev_err(&pdev
->dev
, "Failed to get IRQ resource\n");
1710 fimc
->irq
= res
->start
;
1712 fimc
->num_clocks
= MAX_FIMC_CLOCKS
;
1713 ret
= fimc_clk_get(fimc
);
1716 clk_set_rate(fimc
->clock
[CLK_BUS
], drv_data
->lclk_frequency
);
1717 clk_enable(fimc
->clock
[CLK_BUS
]);
1719 platform_set_drvdata(pdev
, fimc
);
1721 ret
= devm_request_irq(&pdev
->dev
, fimc
->irq
, fimc_irq_handler
,
1722 0, pdev
->name
, fimc
);
1724 dev_err(&pdev
->dev
, "failed to install irq (%d)\n", ret
);
1728 pm_runtime_enable(&pdev
->dev
);
1729 ret
= pm_runtime_get_sync(&pdev
->dev
);
1732 /* Initialize contiguous memory allocator */
1733 fimc
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
1734 if (IS_ERR(fimc
->alloc_ctx
)) {
1735 ret
= PTR_ERR(fimc
->alloc_ctx
);
1739 dev_dbg(&pdev
->dev
, "FIMC.%d registered successfully\n", fimc
->id
);
1741 pm_runtime_put(&pdev
->dev
);
1745 pm_runtime_put(&pdev
->dev
);
1751 static int fimc_runtime_resume(struct device
*dev
)
1753 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1755 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1757 /* Enable clocks and perform basic initalization */
1758 clk_enable(fimc
->clock
[CLK_GATE
]);
1759 fimc_hw_reset(fimc
);
1761 /* Resume the capture or mem-to-mem device */
1762 if (fimc_capture_busy(fimc
))
1763 return fimc_capture_resume(fimc
);
1765 return fimc_m2m_resume(fimc
);
1768 static int fimc_runtime_suspend(struct device
*dev
)
1770 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1773 if (fimc_capture_busy(fimc
))
1774 ret
= fimc_capture_suspend(fimc
);
1776 ret
= fimc_m2m_suspend(fimc
);
1778 clk_disable(fimc
->clock
[CLK_GATE
]);
1780 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1784 #ifdef CONFIG_PM_SLEEP
1785 static int fimc_resume(struct device
*dev
)
1787 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1788 unsigned long flags
;
1790 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1792 /* Do not resume if the device was idle before system suspend */
1793 spin_lock_irqsave(&fimc
->slock
, flags
);
1794 if (!test_and_clear_bit(ST_LPM
, &fimc
->state
) ||
1795 (!fimc_m2m_active(fimc
) && !fimc_capture_busy(fimc
))) {
1796 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1799 fimc_hw_reset(fimc
);
1800 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1802 if (fimc_capture_busy(fimc
))
1803 return fimc_capture_resume(fimc
);
1805 return fimc_m2m_resume(fimc
);
1808 static int fimc_suspend(struct device
*dev
)
1810 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1812 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1814 if (test_and_set_bit(ST_LPM
, &fimc
->state
))
1816 if (fimc_capture_busy(fimc
))
1817 return fimc_capture_suspend(fimc
);
1819 return fimc_m2m_suspend(fimc
);
1821 #endif /* CONFIG_PM_SLEEP */
1823 static int __devexit
fimc_remove(struct platform_device
*pdev
)
1825 struct fimc_dev
*fimc
= platform_get_drvdata(pdev
);
1827 pm_runtime_disable(&pdev
->dev
);
1828 pm_runtime_set_suspended(&pdev
->dev
);
1830 vb2_dma_contig_cleanup_ctx(fimc
->alloc_ctx
);
1832 clk_disable(fimc
->clock
[CLK_BUS
]);
1835 dev_info(&pdev
->dev
, "driver unloaded\n");
1839 /* Image pixel limits, similar across several FIMC HW revisions. */
1840 static struct fimc_pix_limit s5p_pix_limit
[4] = {
1842 .scaler_en_w
= 3264,
1843 .scaler_dis_w
= 8192,
1844 .in_rot_en_h
= 1920,
1845 .in_rot_dis_w
= 8192,
1846 .out_rot_en_w
= 1920,
1847 .out_rot_dis_w
= 4224,
1850 .scaler_en_w
= 4224,
1851 .scaler_dis_w
= 8192,
1852 .in_rot_en_h
= 1920,
1853 .in_rot_dis_w
= 8192,
1854 .out_rot_en_w
= 1920,
1855 .out_rot_dis_w
= 4224,
1858 .scaler_en_w
= 1920,
1859 .scaler_dis_w
= 8192,
1860 .in_rot_en_h
= 1280,
1861 .in_rot_dis_w
= 8192,
1862 .out_rot_en_w
= 1280,
1863 .out_rot_dis_w
= 1920,
1866 .scaler_en_w
= 1920,
1867 .scaler_dis_w
= 8192,
1868 .in_rot_en_h
= 1366,
1869 .in_rot_dis_w
= 8192,
1870 .out_rot_en_w
= 1366,
1871 .out_rot_dis_w
= 1920,
1875 static struct samsung_fimc_variant fimc0_variant_s5p
= {
1879 .min_inp_pixsize
= 16,
1880 .min_out_pixsize
= 16,
1881 .hor_offs_align
= 8,
1882 .min_vsize_align
= 16,
1884 .pix_limit
= &s5p_pix_limit
[0],
1887 static struct samsung_fimc_variant fimc2_variant_s5p
= {
1889 .min_inp_pixsize
= 16,
1890 .min_out_pixsize
= 16,
1891 .hor_offs_align
= 8,
1892 .min_vsize_align
= 16,
1894 .pix_limit
= &s5p_pix_limit
[1],
1897 static struct samsung_fimc_variant fimc0_variant_s5pv210
= {
1902 .min_inp_pixsize
= 16,
1903 .min_out_pixsize
= 16,
1904 .hor_offs_align
= 8,
1905 .min_vsize_align
= 16,
1907 .pix_limit
= &s5p_pix_limit
[1],
1910 static struct samsung_fimc_variant fimc1_variant_s5pv210
= {
1915 .has_mainscaler_ext
= 1,
1916 .min_inp_pixsize
= 16,
1917 .min_out_pixsize
= 16,
1918 .hor_offs_align
= 1,
1919 .min_vsize_align
= 1,
1921 .pix_limit
= &s5p_pix_limit
[2],
1924 static struct samsung_fimc_variant fimc2_variant_s5pv210
= {
1927 .min_inp_pixsize
= 16,
1928 .min_out_pixsize
= 16,
1929 .hor_offs_align
= 8,
1930 .min_vsize_align
= 16,
1932 .pix_limit
= &s5p_pix_limit
[2],
1935 static struct samsung_fimc_variant fimc0_variant_exynos4
= {
1941 .has_mainscaler_ext
= 1,
1943 .min_inp_pixsize
= 16,
1944 .min_out_pixsize
= 16,
1945 .hor_offs_align
= 2,
1946 .min_vsize_align
= 1,
1947 .out_buf_count
= 32,
1948 .pix_limit
= &s5p_pix_limit
[1],
1951 static struct samsung_fimc_variant fimc3_variant_exynos4
= {
1955 .has_mainscaler_ext
= 1,
1957 .min_inp_pixsize
= 16,
1958 .min_out_pixsize
= 16,
1959 .hor_offs_align
= 2,
1960 .min_vsize_align
= 1,
1961 .out_buf_count
= 32,
1962 .pix_limit
= &s5p_pix_limit
[3],
1966 static struct samsung_fimc_driverdata fimc_drvdata_s5p
= {
1968 [0] = &fimc0_variant_s5p
,
1969 [1] = &fimc0_variant_s5p
,
1970 [2] = &fimc2_variant_s5p
,
1973 .lclk_frequency
= 133000000UL,
1976 /* S5PV210, S5PC110 */
1977 static struct samsung_fimc_driverdata fimc_drvdata_s5pv210
= {
1979 [0] = &fimc0_variant_s5pv210
,
1980 [1] = &fimc1_variant_s5pv210
,
1981 [2] = &fimc2_variant_s5pv210
,
1984 .lclk_frequency
= 166000000UL,
1987 /* S5PV310, S5PC210 */
1988 static struct samsung_fimc_driverdata fimc_drvdata_exynos4
= {
1990 [0] = &fimc0_variant_exynos4
,
1991 [1] = &fimc0_variant_exynos4
,
1992 [2] = &fimc0_variant_exynos4
,
1993 [3] = &fimc3_variant_exynos4
,
1996 .lclk_frequency
= 166000000UL,
1999 static struct platform_device_id fimc_driver_ids
[] = {
2002 .driver_data
= (unsigned long)&fimc_drvdata_s5p
,
2004 .name
= "s5pv210-fimc",
2005 .driver_data
= (unsigned long)&fimc_drvdata_s5pv210
,
2007 .name
= "exynos4-fimc",
2008 .driver_data
= (unsigned long)&fimc_drvdata_exynos4
,
2012 MODULE_DEVICE_TABLE(platform
, fimc_driver_ids
);
2014 static const struct dev_pm_ops fimc_pm_ops
= {
2015 SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend
, fimc_resume
)
2016 SET_RUNTIME_PM_OPS(fimc_runtime_suspend
, fimc_runtime_resume
, NULL
)
2019 static struct platform_driver fimc_driver
= {
2020 .probe
= fimc_probe
,
2021 .remove
= __devexit_p(fimc_remove
),
2022 .id_table
= fimc_driver_ids
,
2024 .name
= FIMC_MODULE_NAME
,
2025 .owner
= THIS_MODULE
,
2030 int __init
fimc_register_driver(void)
2032 return platform_driver_probe(&fimc_driver
, fimc_probe
);
2035 void __exit
fimc_unregister_driver(void)
2037 platform_driver_unregister(&fimc_driver
);