2 * Samsung S5P/EXYNOS4 SoC series FIMC (CAMIF) 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 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"
32 #include "fimc-mdevice.h"
34 static char *fimc_clocks
[MAX_FIMC_CLOCKS
] = {
38 static struct fimc_fmt fimc_formats
[] = {
41 .fourcc
= V4L2_PIX_FMT_RGB565
,
43 .color
= FIMC_FMT_RGB565
,
46 .flags
= FMT_FLAGS_M2M
,
49 .fourcc
= V4L2_PIX_FMT_BGR666
,
51 .color
= FIMC_FMT_RGB666
,
54 .flags
= FMT_FLAGS_M2M
,
56 .name
= "ARGB8888, 32 bpp",
57 .fourcc
= V4L2_PIX_FMT_RGB32
,
59 .color
= FIMC_FMT_RGB888
,
62 .flags
= FMT_FLAGS_M2M
| FMT_HAS_ALPHA
,
65 .fourcc
= V4L2_PIX_FMT_RGB555
,
67 .color
= FIMC_FMT_RGB555
,
70 .flags
= FMT_FLAGS_M2M_OUT
| FMT_HAS_ALPHA
,
73 .fourcc
= V4L2_PIX_FMT_RGB444
,
75 .color
= FIMC_FMT_RGB444
,
78 .flags
= FMT_FLAGS_M2M_OUT
| FMT_HAS_ALPHA
,
80 .name
= "YUV 4:2:2 packed, YCbYCr",
81 .fourcc
= V4L2_PIX_FMT_YUYV
,
83 .color
= FIMC_FMT_YCBYCR422
,
86 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
87 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
89 .name
= "YUV 4:2:2 packed, CbYCrY",
90 .fourcc
= V4L2_PIX_FMT_UYVY
,
92 .color
= FIMC_FMT_CBYCRY422
,
95 .mbus_code
= V4L2_MBUS_FMT_UYVY8_2X8
,
96 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
98 .name
= "YUV 4:2:2 packed, CrYCbY",
99 .fourcc
= V4L2_PIX_FMT_VYUY
,
101 .color
= FIMC_FMT_CRYCBY422
,
104 .mbus_code
= V4L2_MBUS_FMT_VYUY8_2X8
,
105 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
107 .name
= "YUV 4:2:2 packed, YCrYCb",
108 .fourcc
= V4L2_PIX_FMT_YVYU
,
110 .color
= FIMC_FMT_YCRYCB422
,
113 .mbus_code
= V4L2_MBUS_FMT_YVYU8_2X8
,
114 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
116 .name
= "YUV 4:2:2 planar, Y/Cb/Cr",
117 .fourcc
= V4L2_PIX_FMT_YUV422P
,
119 .color
= FIMC_FMT_YCBYCR422
,
122 .flags
= FMT_FLAGS_M2M
,
124 .name
= "YUV 4:2:2 planar, Y/CbCr",
125 .fourcc
= V4L2_PIX_FMT_NV16
,
127 .color
= FIMC_FMT_YCBYCR422
,
130 .flags
= FMT_FLAGS_M2M
,
132 .name
= "YUV 4:2:2 planar, Y/CrCb",
133 .fourcc
= V4L2_PIX_FMT_NV61
,
135 .color
= FIMC_FMT_YCRYCB422
,
138 .flags
= FMT_FLAGS_M2M
,
140 .name
= "YUV 4:2:0 planar, YCbCr",
141 .fourcc
= V4L2_PIX_FMT_YUV420
,
143 .color
= FIMC_FMT_YCBCR420
,
146 .flags
= FMT_FLAGS_M2M
,
148 .name
= "YUV 4:2:0 planar, Y/CbCr",
149 .fourcc
= V4L2_PIX_FMT_NV12
,
151 .color
= FIMC_FMT_YCBCR420
,
154 .flags
= FMT_FLAGS_M2M
,
156 .name
= "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr",
157 .fourcc
= V4L2_PIX_FMT_NV12M
,
158 .color
= FIMC_FMT_YCBCR420
,
162 .flags
= FMT_FLAGS_M2M
,
164 .name
= "YUV 4:2:0 non-contiguous 3-planar, Y/Cb/Cr",
165 .fourcc
= V4L2_PIX_FMT_YUV420M
,
166 .color
= FIMC_FMT_YCBCR420
,
167 .depth
= { 8, 2, 2 },
170 .flags
= FMT_FLAGS_M2M
,
172 .name
= "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr, tiled",
173 .fourcc
= V4L2_PIX_FMT_NV12MT
,
174 .color
= FIMC_FMT_YCBCR420
,
178 .flags
= FMT_FLAGS_M2M
,
180 .name
= "JPEG encoded data",
181 .fourcc
= V4L2_PIX_FMT_JPEG
,
182 .color
= FIMC_FMT_JPEG
,
186 .mbus_code
= V4L2_MBUS_FMT_JPEG_1X8
,
187 .flags
= FMT_FLAGS_CAM
,
191 struct fimc_fmt
*fimc_get_format(unsigned int index
)
193 if (index
>= ARRAY_SIZE(fimc_formats
))
196 return &fimc_formats
[index
];
199 int fimc_check_scaler_ratio(struct fimc_ctx
*ctx
, int sw
, int sh
,
200 int dw
, int dh
, int rotation
)
202 if (rotation
== 90 || rotation
== 270)
205 if (!ctx
->scaler
.enabled
)
206 return (sw
== dw
&& sh
== dh
) ? 0 : -EINVAL
;
208 if ((sw
>= SCALER_MAX_HRATIO
* dw
) || (sh
>= SCALER_MAX_VRATIO
* dh
))
214 static int fimc_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
223 if (src
>= tar
* tmp
) {
224 *shift
= sh
, *ratio
= tmp
;
228 *shift
= 0, *ratio
= 1;
232 int fimc_set_scaler_info(struct fimc_ctx
*ctx
)
234 struct fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
235 struct device
*dev
= &ctx
->fimc_dev
->pdev
->dev
;
236 struct fimc_scaler
*sc
= &ctx
->scaler
;
237 struct fimc_frame
*s_frame
= &ctx
->s_frame
;
238 struct fimc_frame
*d_frame
= &ctx
->d_frame
;
242 if (ctx
->rotation
== 90 || ctx
->rotation
== 270) {
244 tx
= d_frame
->height
;
247 ty
= d_frame
->height
;
249 if (tx
<= 0 || ty
<= 0) {
250 dev_err(dev
, "Invalid target size: %dx%d", tx
, ty
);
255 sy
= s_frame
->height
;
256 if (sx
<= 0 || sy
<= 0) {
257 dev_err(dev
, "Invalid source size: %dx%d", sx
, sy
);
261 sc
->real_height
= sy
;
263 ret
= fimc_get_scaler_factor(sx
, tx
, &sc
->pre_hratio
, &sc
->hfactor
);
267 ret
= fimc_get_scaler_factor(sy
, ty
, &sc
->pre_vratio
, &sc
->vfactor
);
271 sc
->pre_dst_width
= sx
/ sc
->pre_hratio
;
272 sc
->pre_dst_height
= sy
/ sc
->pre_vratio
;
274 if (variant
->has_mainscaler_ext
) {
275 sc
->main_hratio
= (sx
<< 14) / (tx
<< sc
->hfactor
);
276 sc
->main_vratio
= (sy
<< 14) / (ty
<< sc
->vfactor
);
278 sc
->main_hratio
= (sx
<< 8) / (tx
<< sc
->hfactor
);
279 sc
->main_vratio
= (sy
<< 8) / (ty
<< sc
->vfactor
);
283 sc
->scaleup_h
= (tx
>= sx
) ? 1 : 0;
284 sc
->scaleup_v
= (ty
>= sy
) ? 1 : 0;
286 /* check to see if input and output size/format differ */
287 if (s_frame
->fmt
->color
== d_frame
->fmt
->color
288 && s_frame
->width
== d_frame
->width
289 && s_frame
->height
== d_frame
->height
)
297 static irqreturn_t
fimc_irq_handler(int irq
, void *priv
)
299 struct fimc_dev
*fimc
= priv
;
300 struct fimc_ctx
*ctx
;
302 fimc_hw_clear_irq(fimc
);
304 spin_lock(&fimc
->slock
);
306 if (test_and_clear_bit(ST_M2M_PEND
, &fimc
->state
)) {
307 if (test_and_clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
)) {
308 set_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
309 wake_up(&fimc
->irq_queue
);
312 ctx
= v4l2_m2m_get_curr_priv(fimc
->m2m
.m2m_dev
);
314 spin_unlock(&fimc
->slock
);
315 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_DONE
);
317 if (ctx
->state
& FIMC_CTX_SHUT
) {
318 ctx
->state
&= ~FIMC_CTX_SHUT
;
319 wake_up(&fimc
->irq_queue
);
323 } else if (test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
324 int last_buf
= test_bit(ST_CAPT_JPEG
, &fimc
->state
) &&
325 fimc
->vid_cap
.reqbufs_count
== 1;
326 fimc_capture_irq_handler(fimc
, !last_buf
);
329 spin_unlock(&fimc
->slock
);
333 /* The color format (colplanes, memplanes) must be already configured. */
334 int fimc_prepare_addr(struct fimc_ctx
*ctx
, struct vb2_buffer
*vb
,
335 struct fimc_frame
*frame
, struct fimc_addr
*paddr
)
340 if (vb
== NULL
|| frame
== NULL
)
343 pix_size
= frame
->width
* frame
->height
;
345 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
346 frame
->fmt
->memplanes
, frame
->fmt
->colplanes
, pix_size
);
348 paddr
->y
= vb2_dma_contig_plane_dma_addr(vb
, 0);
350 if (frame
->fmt
->memplanes
== 1) {
351 switch (frame
->fmt
->colplanes
) {
357 /* decompose Y into Y/Cb */
358 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
362 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
363 /* decompose Y into Y/Cb/Cr */
364 if (FIMC_FMT_YCBCR420
== frame
->fmt
->color
)
365 paddr
->cr
= (u32
)(paddr
->cb
368 paddr
->cr
= (u32
)(paddr
->cb
375 if (frame
->fmt
->memplanes
>= 2)
376 paddr
->cb
= vb2_dma_contig_plane_dma_addr(vb
, 1);
378 if (frame
->fmt
->memplanes
== 3)
379 paddr
->cr
= vb2_dma_contig_plane_dma_addr(vb
, 2);
382 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
383 paddr
->y
, paddr
->cb
, paddr
->cr
, ret
);
388 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
389 void fimc_set_yuv_order(struct fimc_ctx
*ctx
)
391 /* The one only mode supported in SoC. */
392 ctx
->in_order_2p
= FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB
;
393 ctx
->out_order_2p
= FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB
;
395 /* Set order for 1 plane input formats. */
396 switch (ctx
->s_frame
.fmt
->color
) {
397 case FIMC_FMT_YCRYCB422
:
398 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_CBYCRY
;
400 case FIMC_FMT_CBYCRY422
:
401 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_YCRYCB
;
403 case FIMC_FMT_CRYCBY422
:
404 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_YCBYCR
;
406 case FIMC_FMT_YCBYCR422
:
408 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_CRYCBY
;
411 dbg("ctx->in_order_1p= %d", ctx
->in_order_1p
);
413 switch (ctx
->d_frame
.fmt
->color
) {
414 case FIMC_FMT_YCRYCB422
:
415 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_CBYCRY
;
417 case FIMC_FMT_CBYCRY422
:
418 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_YCRYCB
;
420 case FIMC_FMT_CRYCBY422
:
421 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_YCBYCR
;
423 case FIMC_FMT_YCBYCR422
:
425 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_CRYCBY
;
428 dbg("ctx->out_order_1p= %d", ctx
->out_order_1p
);
431 void fimc_prepare_dma_offset(struct fimc_ctx
*ctx
, struct fimc_frame
*f
)
433 struct fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
436 for (i
= 0; i
< f
->fmt
->colplanes
; i
++)
437 depth
+= f
->fmt
->depth
[i
];
439 f
->dma_offset
.y_h
= f
->offs_h
;
440 if (!variant
->pix_hoff
)
441 f
->dma_offset
.y_h
*= (depth
>> 3);
443 f
->dma_offset
.y_v
= f
->offs_v
;
445 f
->dma_offset
.cb_h
= f
->offs_h
;
446 f
->dma_offset
.cb_v
= f
->offs_v
;
448 f
->dma_offset
.cr_h
= f
->offs_h
;
449 f
->dma_offset
.cr_v
= f
->offs_v
;
451 if (!variant
->pix_hoff
) {
452 if (f
->fmt
->colplanes
== 3) {
453 f
->dma_offset
.cb_h
>>= 1;
454 f
->dma_offset
.cr_h
>>= 1;
456 if (f
->fmt
->color
== FIMC_FMT_YCBCR420
) {
457 f
->dma_offset
.cb_v
>>= 1;
458 f
->dma_offset
.cr_v
>>= 1;
462 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
463 f
->fmt
->color
, f
->dma_offset
.y_h
, f
->dma_offset
.y_v
);
466 int fimc_set_color_effect(struct fimc_ctx
*ctx
, enum v4l2_colorfx colorfx
)
468 struct fimc_effect
*effect
= &ctx
->effect
;
471 case V4L2_COLORFX_NONE
:
472 effect
->type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
474 case V4L2_COLORFX_BW
:
475 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
476 effect
->pat_cb
= 128;
477 effect
->pat_cr
= 128;
479 case V4L2_COLORFX_SEPIA
:
480 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
481 effect
->pat_cb
= 115;
482 effect
->pat_cr
= 145;
484 case V4L2_COLORFX_NEGATIVE
:
485 effect
->type
= FIMC_REG_CIIMGEFF_FIN_NEGATIVE
;
487 case V4L2_COLORFX_EMBOSS
:
488 effect
->type
= FIMC_REG_CIIMGEFF_FIN_EMBOSSING
;
490 case V4L2_COLORFX_ART_FREEZE
:
491 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARTFREEZE
;
493 case V4L2_COLORFX_SILHOUETTE
:
494 effect
->type
= FIMC_REG_CIIMGEFF_FIN_SILHOUETTE
;
496 case V4L2_COLORFX_SET_CBCR
:
497 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
498 effect
->pat_cb
= ctx
->ctrls
.colorfx_cbcr
->val
>> 8;
499 effect
->pat_cr
= ctx
->ctrls
.colorfx_cbcr
->val
& 0xff;
509 * V4L2 controls handling
511 #define ctrl_to_ctx(__ctrl) \
512 container_of((__ctrl)->handler, struct fimc_ctx, ctrls.handler)
514 static int __fimc_s_ctrl(struct fimc_ctx
*ctx
, struct v4l2_ctrl
*ctrl
)
516 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
517 struct fimc_variant
*variant
= fimc
->variant
;
518 unsigned int flags
= FIMC_DST_FMT
| FIMC_SRC_FMT
;
521 if (ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
)
526 ctx
->hflip
= ctrl
->val
;
530 ctx
->vflip
= ctrl
->val
;
533 case V4L2_CID_ROTATE
:
534 if (fimc_capture_pending(fimc
) ||
535 (ctx
->state
& flags
) == flags
) {
536 ret
= fimc_check_scaler_ratio(ctx
, ctx
->s_frame
.width
,
537 ctx
->s_frame
.height
, ctx
->d_frame
.width
,
538 ctx
->d_frame
.height
, ctrl
->val
);
542 if ((ctrl
->val
== 90 || ctrl
->val
== 270) &&
543 !variant
->has_out_rot
)
546 ctx
->rotation
= ctrl
->val
;
549 case V4L2_CID_ALPHA_COMPONENT
:
550 ctx
->d_frame
.alpha
= ctrl
->val
;
553 case V4L2_CID_COLORFX
:
554 ret
= fimc_set_color_effect(ctx
, ctrl
->val
);
560 ctx
->state
|= FIMC_PARAMS
;
561 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
565 static int fimc_s_ctrl(struct v4l2_ctrl
*ctrl
)
567 struct fimc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
571 spin_lock_irqsave(&ctx
->fimc_dev
->slock
, flags
);
572 ret
= __fimc_s_ctrl(ctx
, ctrl
);
573 spin_unlock_irqrestore(&ctx
->fimc_dev
->slock
, flags
);
578 static const struct v4l2_ctrl_ops fimc_ctrl_ops
= {
579 .s_ctrl
= fimc_s_ctrl
,
582 int fimc_ctrls_create(struct fimc_ctx
*ctx
)
584 struct fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
585 unsigned int max_alpha
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
586 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
587 struct v4l2_ctrl_handler
*handler
= &ctrls
->handler
;
589 if (ctx
->ctrls
.ready
)
592 v4l2_ctrl_handler_init(handler
, 6);
594 ctrls
->rotate
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
595 V4L2_CID_ROTATE
, 0, 270, 90, 0);
596 ctrls
->hflip
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
597 V4L2_CID_HFLIP
, 0, 1, 1, 0);
598 ctrls
->vflip
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
599 V4L2_CID_VFLIP
, 0, 1, 1, 0);
601 if (variant
->has_alpha
)
602 ctrls
->alpha
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
603 V4L2_CID_ALPHA_COMPONENT
,
608 ctrls
->colorfx
= v4l2_ctrl_new_std_menu(handler
, &fimc_ctrl_ops
,
609 V4L2_CID_COLORFX
, V4L2_COLORFX_SET_CBCR
,
610 ~0x983f, V4L2_COLORFX_NONE
);
612 ctrls
->colorfx_cbcr
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
613 V4L2_CID_COLORFX_CBCR
, 0, 0xffff, 1, 0);
615 ctx
->effect
.type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
617 if (!handler
->error
) {
618 v4l2_ctrl_cluster(3, &ctrls
->colorfx
);
622 return handler
->error
;
625 void fimc_ctrls_delete(struct fimc_ctx
*ctx
)
627 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
630 v4l2_ctrl_handler_free(&ctrls
->handler
);
631 ctrls
->ready
= false;
636 void fimc_ctrls_activate(struct fimc_ctx
*ctx
, bool active
)
638 unsigned int has_alpha
= ctx
->d_frame
.fmt
->flags
& FMT_HAS_ALPHA
;
639 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
644 mutex_lock(&ctrls
->handler
.lock
);
645 v4l2_ctrl_activate(ctrls
->rotate
, active
);
646 v4l2_ctrl_activate(ctrls
->hflip
, active
);
647 v4l2_ctrl_activate(ctrls
->vflip
, active
);
648 v4l2_ctrl_activate(ctrls
->colorfx
, active
);
650 v4l2_ctrl_activate(ctrls
->alpha
, active
&& has_alpha
);
653 fimc_set_color_effect(ctx
, ctrls
->colorfx
->cur
.val
);
654 ctx
->rotation
= ctrls
->rotate
->val
;
655 ctx
->hflip
= ctrls
->hflip
->val
;
656 ctx
->vflip
= ctrls
->vflip
->val
;
658 ctx
->effect
.type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
663 mutex_unlock(&ctrls
->handler
.lock
);
666 /* Update maximum value of the alpha color control */
667 void fimc_alpha_ctrl_update(struct fimc_ctx
*ctx
)
669 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
670 struct v4l2_ctrl
*ctrl
= ctx
->ctrls
.alpha
;
672 if (ctrl
== NULL
|| !fimc
->variant
->has_alpha
)
675 v4l2_ctrl_lock(ctrl
);
676 ctrl
->maximum
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
678 if (ctrl
->cur
.val
> ctrl
->maximum
)
679 ctrl
->cur
.val
= ctrl
->maximum
;
681 v4l2_ctrl_unlock(ctrl
);
684 int fimc_fill_format(struct fimc_frame
*frame
, struct v4l2_format
*f
)
686 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
689 pixm
->width
= frame
->o_width
;
690 pixm
->height
= frame
->o_height
;
691 pixm
->field
= V4L2_FIELD_NONE
;
692 pixm
->pixelformat
= frame
->fmt
->fourcc
;
693 pixm
->colorspace
= V4L2_COLORSPACE_JPEG
;
694 pixm
->num_planes
= frame
->fmt
->memplanes
;
696 for (i
= 0; i
< pixm
->num_planes
; ++i
) {
697 int bpl
= frame
->f_width
;
698 if (frame
->fmt
->colplanes
== 1) /* packed formats */
699 bpl
= (bpl
* frame
->fmt
->depth
[0]) / 8;
700 pixm
->plane_fmt
[i
].bytesperline
= bpl
;
701 pixm
->plane_fmt
[i
].sizeimage
= (frame
->o_width
*
702 frame
->o_height
* frame
->fmt
->depth
[i
]) / 8;
707 void fimc_fill_frame(struct fimc_frame
*frame
, struct v4l2_format
*f
)
709 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
711 frame
->f_width
= pixm
->plane_fmt
[0].bytesperline
;
712 if (frame
->fmt
->colplanes
== 1)
713 frame
->f_width
= (frame
->f_width
* 8) / frame
->fmt
->depth
[0];
714 frame
->f_height
= pixm
->height
;
715 frame
->width
= pixm
->width
;
716 frame
->height
= pixm
->height
;
717 frame
->o_width
= pixm
->width
;
718 frame
->o_height
= pixm
->height
;
724 * fimc_adjust_mplane_format - adjust bytesperline/sizeimage for each plane
725 * @fmt: fimc pixel format description (input)
726 * @width: requested pixel width
727 * @height: requested pixel height
728 * @pix: multi-plane format to adjust
730 void fimc_adjust_mplane_format(struct fimc_fmt
*fmt
, u32 width
, u32 height
,
731 struct v4l2_pix_format_mplane
*pix
)
733 u32 bytesperline
= 0;
736 pix
->colorspace
= V4L2_COLORSPACE_JPEG
;
737 pix
->field
= V4L2_FIELD_NONE
;
738 pix
->num_planes
= fmt
->memplanes
;
739 pix
->pixelformat
= fmt
->fourcc
;
740 pix
->height
= height
;
743 for (i
= 0; i
< pix
->num_planes
; ++i
) {
744 u32 bpl
= pix
->plane_fmt
[i
].bytesperline
;
745 u32
*sizeimage
= &pix
->plane_fmt
[i
].sizeimage
;
747 if (fmt
->colplanes
> 1 && (bpl
== 0 || bpl
< pix
->width
))
748 bpl
= pix
->width
; /* Planar */
750 if (fmt
->colplanes
== 1 && /* Packed */
751 (bpl
== 0 || ((bpl
* 8) / fmt
->depth
[i
]) < pix
->width
))
752 bpl
= (pix
->width
* fmt
->depth
[0]) / 8;
754 if (i
== 0) /* Same bytesperline for each plane. */
757 pix
->plane_fmt
[i
].bytesperline
= bytesperline
;
758 *sizeimage
= (pix
->width
* pix
->height
* fmt
->depth
[i
]) / 8;
763 * fimc_find_format - lookup fimc color format by fourcc or media bus format
764 * @pixelformat: fourcc to match, ignored if null
765 * @mbus_code: media bus code to match, ignored if null
766 * @mask: the color flags to match
767 * @index: offset in the fimc_formats array, ignored if negative
769 struct fimc_fmt
*fimc_find_format(const u32
*pixelformat
, const u32
*mbus_code
,
770 unsigned int mask
, int index
)
772 struct fimc_fmt
*fmt
, *def_fmt
= NULL
;
776 if (index
>= (int)ARRAY_SIZE(fimc_formats
))
779 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
780 fmt
= &fimc_formats
[i
];
781 if (!(fmt
->flags
& mask
))
783 if (pixelformat
&& fmt
->fourcc
== *pixelformat
)
785 if (mbus_code
&& fmt
->mbus_code
== *mbus_code
)
794 static void fimc_clk_put(struct fimc_dev
*fimc
)
797 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++) {
798 if (IS_ERR_OR_NULL(fimc
->clock
[i
]))
800 clk_unprepare(fimc
->clock
[i
]);
801 clk_put(fimc
->clock
[i
]);
802 fimc
->clock
[i
] = NULL
;
806 static int fimc_clk_get(struct fimc_dev
*fimc
)
810 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++) {
811 fimc
->clock
[i
] = clk_get(&fimc
->pdev
->dev
, fimc_clocks
[i
]);
812 if (IS_ERR(fimc
->clock
[i
]))
814 ret
= clk_prepare(fimc
->clock
[i
]);
816 clk_put(fimc
->clock
[i
]);
817 fimc
->clock
[i
] = NULL
;
824 dev_err(&fimc
->pdev
->dev
, "failed to get clock: %s\n",
829 static int fimc_m2m_suspend(struct fimc_dev
*fimc
)
834 spin_lock_irqsave(&fimc
->slock
, flags
);
835 if (!fimc_m2m_pending(fimc
)) {
836 spin_unlock_irqrestore(&fimc
->slock
, flags
);
839 clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
840 set_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
841 spin_unlock_irqrestore(&fimc
->slock
, flags
);
843 timeout
= wait_event_timeout(fimc
->irq_queue
,
844 test_bit(ST_M2M_SUSPENDED
, &fimc
->state
),
845 FIMC_SHUTDOWN_TIMEOUT
);
847 clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
848 return timeout
== 0 ? -EAGAIN
: 0;
851 static int fimc_m2m_resume(struct fimc_dev
*fimc
)
855 spin_lock_irqsave(&fimc
->slock
, flags
);
856 /* Clear for full H/W setup in first run after resume */
857 fimc
->m2m
.ctx
= NULL
;
858 spin_unlock_irqrestore(&fimc
->slock
, flags
);
860 if (test_and_clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
))
861 fimc_m2m_job_finish(fimc
->m2m
.ctx
,
862 VB2_BUF_STATE_ERROR
);
866 static int fimc_probe(struct platform_device
*pdev
)
868 struct fimc_drvdata
*drv_data
= fimc_get_drvdata(pdev
);
869 struct s5p_platform_fimc
*pdata
;
870 struct fimc_dev
*fimc
;
871 struct resource
*res
;
874 if (pdev
->id
>= drv_data
->num_entities
) {
875 dev_err(&pdev
->dev
, "Invalid platform device id: %d\n",
880 fimc
= devm_kzalloc(&pdev
->dev
, sizeof(*fimc
), GFP_KERNEL
);
886 fimc
->variant
= drv_data
->variant
[fimc
->id
];
888 pdata
= pdev
->dev
.platform_data
;
891 init_waitqueue_head(&fimc
->irq_queue
);
892 spin_lock_init(&fimc
->slock
);
893 mutex_init(&fimc
->lock
);
895 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
896 fimc
->regs
= devm_request_and_ioremap(&pdev
->dev
, res
);
897 if (fimc
->regs
== NULL
) {
898 dev_err(&pdev
->dev
, "Failed to obtain io memory\n");
902 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
904 dev_err(&pdev
->dev
, "Failed to get IRQ resource\n");
908 ret
= fimc_clk_get(fimc
);
911 clk_set_rate(fimc
->clock
[CLK_BUS
], drv_data
->lclk_frequency
);
912 clk_enable(fimc
->clock
[CLK_BUS
]);
914 ret
= devm_request_irq(&pdev
->dev
, res
->start
, fimc_irq_handler
,
915 0, dev_name(&pdev
->dev
), fimc
);
917 dev_err(&pdev
->dev
, "failed to install irq (%d)\n", ret
);
921 ret
= fimc_initialize_capture_subdev(fimc
);
925 platform_set_drvdata(pdev
, fimc
);
926 pm_runtime_enable(&pdev
->dev
);
927 ret
= pm_runtime_get_sync(&pdev
->dev
);
930 /* Initialize contiguous memory allocator */
931 fimc
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
932 if (IS_ERR(fimc
->alloc_ctx
)) {
933 ret
= PTR_ERR(fimc
->alloc_ctx
);
937 dev_dbg(&pdev
->dev
, "FIMC.%d registered successfully\n", fimc
->id
);
939 pm_runtime_put(&pdev
->dev
);
942 pm_runtime_put(&pdev
->dev
);
944 fimc_unregister_capture_subdev(fimc
);
950 static int fimc_runtime_resume(struct device
*dev
)
952 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
954 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
956 /* Enable clocks and perform basic initalization */
957 clk_enable(fimc
->clock
[CLK_GATE
]);
960 /* Resume the capture or mem-to-mem device */
961 if (fimc_capture_busy(fimc
))
962 return fimc_capture_resume(fimc
);
964 return fimc_m2m_resume(fimc
);
967 static int fimc_runtime_suspend(struct device
*dev
)
969 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
972 if (fimc_capture_busy(fimc
))
973 ret
= fimc_capture_suspend(fimc
);
975 ret
= fimc_m2m_suspend(fimc
);
977 clk_disable(fimc
->clock
[CLK_GATE
]);
979 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
983 #ifdef CONFIG_PM_SLEEP
984 static int fimc_resume(struct device
*dev
)
986 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
989 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
991 /* Do not resume if the device was idle before system suspend */
992 spin_lock_irqsave(&fimc
->slock
, flags
);
993 if (!test_and_clear_bit(ST_LPM
, &fimc
->state
) ||
994 (!fimc_m2m_active(fimc
) && !fimc_capture_busy(fimc
))) {
995 spin_unlock_irqrestore(&fimc
->slock
, flags
);
999 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1001 if (fimc_capture_busy(fimc
))
1002 return fimc_capture_resume(fimc
);
1004 return fimc_m2m_resume(fimc
);
1007 static int fimc_suspend(struct device
*dev
)
1009 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1011 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1013 if (test_and_set_bit(ST_LPM
, &fimc
->state
))
1015 if (fimc_capture_busy(fimc
))
1016 return fimc_capture_suspend(fimc
);
1018 return fimc_m2m_suspend(fimc
);
1020 #endif /* CONFIG_PM_SLEEP */
1022 static int __devexit
fimc_remove(struct platform_device
*pdev
)
1024 struct fimc_dev
*fimc
= platform_get_drvdata(pdev
);
1026 pm_runtime_disable(&pdev
->dev
);
1027 pm_runtime_set_suspended(&pdev
->dev
);
1029 fimc_unregister_capture_subdev(fimc
);
1030 vb2_dma_contig_cleanup_ctx(fimc
->alloc_ctx
);
1032 clk_disable(fimc
->clock
[CLK_BUS
]);
1035 dev_info(&pdev
->dev
, "driver unloaded\n");
1039 /* Image pixel limits, similar across several FIMC HW revisions. */
1040 static struct fimc_pix_limit s5p_pix_limit
[4] = {
1042 .scaler_en_w
= 3264,
1043 .scaler_dis_w
= 8192,
1044 .in_rot_en_h
= 1920,
1045 .in_rot_dis_w
= 8192,
1046 .out_rot_en_w
= 1920,
1047 .out_rot_dis_w
= 4224,
1050 .scaler_en_w
= 4224,
1051 .scaler_dis_w
= 8192,
1052 .in_rot_en_h
= 1920,
1053 .in_rot_dis_w
= 8192,
1054 .out_rot_en_w
= 1920,
1055 .out_rot_dis_w
= 4224,
1058 .scaler_en_w
= 1920,
1059 .scaler_dis_w
= 8192,
1060 .in_rot_en_h
= 1280,
1061 .in_rot_dis_w
= 8192,
1062 .out_rot_en_w
= 1280,
1063 .out_rot_dis_w
= 1920,
1066 .scaler_en_w
= 1920,
1067 .scaler_dis_w
= 8192,
1068 .in_rot_en_h
= 1366,
1069 .in_rot_dis_w
= 8192,
1070 .out_rot_en_w
= 1366,
1071 .out_rot_dis_w
= 1920,
1075 static struct fimc_variant fimc0_variant_s5p
= {
1079 .min_inp_pixsize
= 16,
1080 .min_out_pixsize
= 16,
1081 .hor_offs_align
= 8,
1082 .min_vsize_align
= 16,
1084 .pix_limit
= &s5p_pix_limit
[0],
1087 static struct fimc_variant fimc2_variant_s5p
= {
1089 .min_inp_pixsize
= 16,
1090 .min_out_pixsize
= 16,
1091 .hor_offs_align
= 8,
1092 .min_vsize_align
= 16,
1094 .pix_limit
= &s5p_pix_limit
[1],
1097 static struct fimc_variant fimc0_variant_s5pv210
= {
1102 .min_inp_pixsize
= 16,
1103 .min_out_pixsize
= 16,
1104 .hor_offs_align
= 8,
1105 .min_vsize_align
= 16,
1107 .pix_limit
= &s5p_pix_limit
[1],
1110 static struct fimc_variant fimc1_variant_s5pv210
= {
1115 .has_mainscaler_ext
= 1,
1116 .min_inp_pixsize
= 16,
1117 .min_out_pixsize
= 16,
1118 .hor_offs_align
= 1,
1119 .min_vsize_align
= 1,
1121 .pix_limit
= &s5p_pix_limit
[2],
1124 static struct fimc_variant fimc2_variant_s5pv210
= {
1127 .min_inp_pixsize
= 16,
1128 .min_out_pixsize
= 16,
1129 .hor_offs_align
= 8,
1130 .min_vsize_align
= 16,
1132 .pix_limit
= &s5p_pix_limit
[2],
1135 static struct fimc_variant fimc0_variant_exynos4
= {
1141 .has_mainscaler_ext
= 1,
1143 .min_inp_pixsize
= 16,
1144 .min_out_pixsize
= 16,
1145 .hor_offs_align
= 2,
1146 .min_vsize_align
= 1,
1147 .out_buf_count
= 32,
1148 .pix_limit
= &s5p_pix_limit
[1],
1151 static struct fimc_variant fimc3_variant_exynos4
= {
1155 .has_mainscaler_ext
= 1,
1157 .min_inp_pixsize
= 16,
1158 .min_out_pixsize
= 16,
1159 .hor_offs_align
= 2,
1160 .min_vsize_align
= 1,
1161 .out_buf_count
= 32,
1162 .pix_limit
= &s5p_pix_limit
[3],
1166 static struct fimc_drvdata fimc_drvdata_s5p
= {
1168 [0] = &fimc0_variant_s5p
,
1169 [1] = &fimc0_variant_s5p
,
1170 [2] = &fimc2_variant_s5p
,
1173 .lclk_frequency
= 133000000UL,
1176 /* S5PV210, S5PC110 */
1177 static struct fimc_drvdata fimc_drvdata_s5pv210
= {
1179 [0] = &fimc0_variant_s5pv210
,
1180 [1] = &fimc1_variant_s5pv210
,
1181 [2] = &fimc2_variant_s5pv210
,
1184 .lclk_frequency
= 166000000UL,
1187 /* EXYNOS4210, S5PV310, S5PC210 */
1188 static struct fimc_drvdata fimc_drvdata_exynos4
= {
1190 [0] = &fimc0_variant_exynos4
,
1191 [1] = &fimc0_variant_exynos4
,
1192 [2] = &fimc0_variant_exynos4
,
1193 [3] = &fimc3_variant_exynos4
,
1196 .lclk_frequency
= 166000000UL,
1199 static struct platform_device_id fimc_driver_ids
[] = {
1202 .driver_data
= (unsigned long)&fimc_drvdata_s5p
,
1204 .name
= "s5pv210-fimc",
1205 .driver_data
= (unsigned long)&fimc_drvdata_s5pv210
,
1207 .name
= "exynos4-fimc",
1208 .driver_data
= (unsigned long)&fimc_drvdata_exynos4
,
1212 MODULE_DEVICE_TABLE(platform
, fimc_driver_ids
);
1214 static const struct dev_pm_ops fimc_pm_ops
= {
1215 SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend
, fimc_resume
)
1216 SET_RUNTIME_PM_OPS(fimc_runtime_suspend
, fimc_runtime_resume
, NULL
)
1219 static struct platform_driver fimc_driver
= {
1220 .probe
= fimc_probe
,
1221 .remove
= __devexit_p(fimc_remove
),
1222 .id_table
= fimc_driver_ids
,
1224 .name
= FIMC_MODULE_NAME
,
1225 .owner
= THIS_MODULE
,
1230 int __init
fimc_register_driver(void)
1232 return platform_driver_register(&fimc_driver
);
1235 void __exit
fimc_unregister_driver(void)
1237 platform_driver_unregister(&fimc_driver
);