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>
23 #include <linux/mfd/syscon.h>
26 #include <linux/of_device.h>
27 #include <linux/slab.h>
28 #include <linux/clk.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/videobuf2-v4l2.h>
31 #include <media/videobuf2-dma-contig.h>
33 #include "fimc-core.h"
35 #include "media-dev.h"
37 static char *fimc_clocks
[MAX_FIMC_CLOCKS
] = {
41 static struct fimc_fmt fimc_formats
[] = {
44 .fourcc
= V4L2_PIX_FMT_RGB565
,
46 .color
= FIMC_FMT_RGB565
,
49 .flags
= FMT_FLAGS_M2M
,
52 .fourcc
= V4L2_PIX_FMT_BGR666
,
54 .color
= FIMC_FMT_RGB666
,
57 .flags
= FMT_FLAGS_M2M
,
59 .name
= "BGRA8888, 32 bpp",
60 .fourcc
= V4L2_PIX_FMT_BGR32
,
62 .color
= FIMC_FMT_RGB888
,
65 .flags
= FMT_FLAGS_M2M
| FMT_HAS_ALPHA
,
68 .fourcc
= V4L2_PIX_FMT_RGB555
,
70 .color
= FIMC_FMT_RGB555
,
73 .flags
= FMT_FLAGS_M2M_OUT
| FMT_HAS_ALPHA
,
76 .fourcc
= V4L2_PIX_FMT_RGB444
,
78 .color
= FIMC_FMT_RGB444
,
81 .flags
= FMT_FLAGS_M2M_OUT
| FMT_HAS_ALPHA
,
84 .mbus_code
= MEDIA_BUS_FMT_YUV10_1X30
,
85 .flags
= FMT_FLAGS_WRITEBACK
,
87 .name
= "YUV 4:2:2 packed, YCbYCr",
88 .fourcc
= V4L2_PIX_FMT_YUYV
,
90 .color
= FIMC_FMT_YCBYCR422
,
93 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
94 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
96 .name
= "YUV 4:2:2 packed, CbYCrY",
97 .fourcc
= V4L2_PIX_FMT_UYVY
,
99 .color
= FIMC_FMT_CBYCRY422
,
102 .mbus_code
= MEDIA_BUS_FMT_UYVY8_2X8
,
103 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
105 .name
= "YUV 4:2:2 packed, CrYCbY",
106 .fourcc
= V4L2_PIX_FMT_VYUY
,
108 .color
= FIMC_FMT_CRYCBY422
,
111 .mbus_code
= MEDIA_BUS_FMT_VYUY8_2X8
,
112 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
114 .name
= "YUV 4:2:2 packed, YCrYCb",
115 .fourcc
= V4L2_PIX_FMT_YVYU
,
117 .color
= FIMC_FMT_YCRYCB422
,
120 .mbus_code
= MEDIA_BUS_FMT_YVYU8_2X8
,
121 .flags
= FMT_FLAGS_M2M
| FMT_FLAGS_CAM
,
123 .name
= "YUV 4:2:2 planar, Y/Cb/Cr",
124 .fourcc
= V4L2_PIX_FMT_YUV422P
,
126 .color
= FIMC_FMT_YCBYCR422
,
129 .flags
= FMT_FLAGS_M2M
,
131 .name
= "YUV 4:2:2 planar, Y/CbCr",
132 .fourcc
= V4L2_PIX_FMT_NV16
,
134 .color
= FIMC_FMT_YCBYCR422
,
137 .flags
= FMT_FLAGS_M2M
,
139 .name
= "YUV 4:2:2 planar, Y/CrCb",
140 .fourcc
= V4L2_PIX_FMT_NV61
,
142 .color
= FIMC_FMT_YCRYCB422
,
145 .flags
= FMT_FLAGS_M2M
,
147 .name
= "YUV 4:2:0 planar, YCbCr",
148 .fourcc
= V4L2_PIX_FMT_YUV420
,
150 .color
= FIMC_FMT_YCBCR420
,
153 .flags
= FMT_FLAGS_M2M
,
155 .name
= "YUV 4:2:0 planar, Y/CbCr",
156 .fourcc
= V4L2_PIX_FMT_NV12
,
158 .color
= FIMC_FMT_YCBCR420
,
161 .flags
= FMT_FLAGS_M2M
,
163 .name
= "YUV 4:2:0 non-contig. 2p, Y/CbCr",
164 .fourcc
= V4L2_PIX_FMT_NV12M
,
165 .color
= FIMC_FMT_YCBCR420
,
169 .flags
= FMT_FLAGS_M2M
,
171 .name
= "YUV 4:2:0 non-contig. 3p, Y/Cb/Cr",
172 .fourcc
= V4L2_PIX_FMT_YUV420M
,
173 .color
= FIMC_FMT_YCBCR420
,
174 .depth
= { 8, 2, 2 },
177 .flags
= FMT_FLAGS_M2M
,
179 .name
= "YUV 4:2:0 non-contig. 2p, tiled",
180 .fourcc
= V4L2_PIX_FMT_NV12MT
,
181 .color
= FIMC_FMT_YCBCR420
,
185 .flags
= FMT_FLAGS_M2M
,
187 .name
= "JPEG encoded data",
188 .fourcc
= V4L2_PIX_FMT_JPEG
,
189 .color
= FIMC_FMT_JPEG
,
193 .mbus_code
= MEDIA_BUS_FMT_JPEG_1X8
,
194 .flags
= FMT_FLAGS_CAM
| FMT_FLAGS_COMPRESSED
,
196 .name
= "S5C73MX interleaved UYVY/JPEG",
197 .fourcc
= V4L2_PIX_FMT_S5C_UYVY_JPG
,
198 .color
= FIMC_FMT_YUYV_JPEG
,
202 .mdataplanes
= 0x2, /* plane 1 holds frame meta data */
203 .mbus_code
= MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8
,
204 .flags
= FMT_FLAGS_CAM
| FMT_FLAGS_COMPRESSED
,
208 struct fimc_fmt
*fimc_get_format(unsigned int index
)
210 if (index
>= ARRAY_SIZE(fimc_formats
))
213 return &fimc_formats
[index
];
216 int fimc_check_scaler_ratio(struct fimc_ctx
*ctx
, int sw
, int sh
,
217 int dw
, int dh
, int rotation
)
219 if (rotation
== 90 || rotation
== 270)
222 if (!ctx
->scaler
.enabled
)
223 return (sw
== dw
&& sh
== dh
) ? 0 : -EINVAL
;
225 if ((sw
>= SCALER_MAX_HRATIO
* dw
) || (sh
>= SCALER_MAX_VRATIO
* dh
))
231 static int fimc_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
240 if (src
>= tar
* tmp
) {
241 *shift
= sh
, *ratio
= tmp
;
245 *shift
= 0, *ratio
= 1;
249 int fimc_set_scaler_info(struct fimc_ctx
*ctx
)
251 const struct fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
252 struct device
*dev
= &ctx
->fimc_dev
->pdev
->dev
;
253 struct fimc_scaler
*sc
= &ctx
->scaler
;
254 struct fimc_frame
*s_frame
= &ctx
->s_frame
;
255 struct fimc_frame
*d_frame
= &ctx
->d_frame
;
259 if (ctx
->rotation
== 90 || ctx
->rotation
== 270) {
261 tx
= d_frame
->height
;
264 ty
= d_frame
->height
;
266 if (tx
<= 0 || ty
<= 0) {
267 dev_err(dev
, "Invalid target size: %dx%d\n", tx
, ty
);
272 sy
= s_frame
->height
;
273 if (sx
<= 0 || sy
<= 0) {
274 dev_err(dev
, "Invalid source size: %dx%d\n", sx
, sy
);
278 sc
->real_height
= sy
;
280 ret
= fimc_get_scaler_factor(sx
, tx
, &sc
->pre_hratio
, &sc
->hfactor
);
284 ret
= fimc_get_scaler_factor(sy
, ty
, &sc
->pre_vratio
, &sc
->vfactor
);
288 sc
->pre_dst_width
= sx
/ sc
->pre_hratio
;
289 sc
->pre_dst_height
= sy
/ sc
->pre_vratio
;
291 if (variant
->has_mainscaler_ext
) {
292 sc
->main_hratio
= (sx
<< 14) / (tx
<< sc
->hfactor
);
293 sc
->main_vratio
= (sy
<< 14) / (ty
<< sc
->vfactor
);
295 sc
->main_hratio
= (sx
<< 8) / (tx
<< sc
->hfactor
);
296 sc
->main_vratio
= (sy
<< 8) / (ty
<< sc
->vfactor
);
300 sc
->scaleup_h
= (tx
>= sx
) ? 1 : 0;
301 sc
->scaleup_v
= (ty
>= sy
) ? 1 : 0;
303 /* check to see if input and output size/format differ */
304 if (s_frame
->fmt
->color
== d_frame
->fmt
->color
305 && s_frame
->width
== d_frame
->width
306 && s_frame
->height
== d_frame
->height
)
314 static irqreturn_t
fimc_irq_handler(int irq
, void *priv
)
316 struct fimc_dev
*fimc
= priv
;
317 struct fimc_ctx
*ctx
;
319 fimc_hw_clear_irq(fimc
);
321 spin_lock(&fimc
->slock
);
323 if (test_and_clear_bit(ST_M2M_PEND
, &fimc
->state
)) {
324 if (test_and_clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
)) {
325 set_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
326 wake_up(&fimc
->irq_queue
);
329 ctx
= v4l2_m2m_get_curr_priv(fimc
->m2m
.m2m_dev
);
331 spin_unlock(&fimc
->slock
);
332 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_DONE
);
334 if (ctx
->state
& FIMC_CTX_SHUT
) {
335 ctx
->state
&= ~FIMC_CTX_SHUT
;
336 wake_up(&fimc
->irq_queue
);
340 } else if (test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
341 int last_buf
= test_bit(ST_CAPT_JPEG
, &fimc
->state
) &&
342 fimc
->vid_cap
.reqbufs_count
== 1;
343 fimc_capture_irq_handler(fimc
, !last_buf
);
346 spin_unlock(&fimc
->slock
);
350 /* The color format (colplanes, memplanes) must be already configured. */
351 int fimc_prepare_addr(struct fimc_ctx
*ctx
, struct vb2_buffer
*vb
,
352 struct fimc_frame
*frame
, struct fimc_addr
*paddr
)
357 if (vb
== NULL
|| frame
== NULL
)
360 pix_size
= frame
->width
* frame
->height
;
362 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
363 frame
->fmt
->memplanes
, frame
->fmt
->colplanes
, pix_size
);
365 paddr
->y
= vb2_dma_contig_plane_dma_addr(vb
, 0);
367 if (frame
->fmt
->memplanes
== 1) {
368 switch (frame
->fmt
->colplanes
) {
374 /* decompose Y into Y/Cb */
375 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
379 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
380 /* decompose Y into Y/Cb/Cr */
381 if (FIMC_FMT_YCBCR420
== frame
->fmt
->color
)
382 paddr
->cr
= (u32
)(paddr
->cb
385 paddr
->cr
= (u32
)(paddr
->cb
391 } else if (!frame
->fmt
->mdataplanes
) {
392 if (frame
->fmt
->memplanes
>= 2)
393 paddr
->cb
= vb2_dma_contig_plane_dma_addr(vb
, 1);
395 if (frame
->fmt
->memplanes
== 3)
396 paddr
->cr
= vb2_dma_contig_plane_dma_addr(vb
, 2);
399 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
400 paddr
->y
, paddr
->cb
, paddr
->cr
, ret
);
405 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
406 void fimc_set_yuv_order(struct fimc_ctx
*ctx
)
408 /* The one only mode supported in SoC. */
409 ctx
->in_order_2p
= FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB
;
410 ctx
->out_order_2p
= FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB
;
412 /* Set order for 1 plane input formats. */
413 switch (ctx
->s_frame
.fmt
->color
) {
414 case FIMC_FMT_YCRYCB422
:
415 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_YCRYCB
;
417 case FIMC_FMT_CBYCRY422
:
418 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_CBYCRY
;
420 case FIMC_FMT_CRYCBY422
:
421 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_CRYCBY
;
423 case FIMC_FMT_YCBYCR422
:
425 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_YCBYCR
;
428 dbg("ctx->in_order_1p= %d", ctx
->in_order_1p
);
430 switch (ctx
->d_frame
.fmt
->color
) {
431 case FIMC_FMT_YCRYCB422
:
432 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_YCRYCB
;
434 case FIMC_FMT_CBYCRY422
:
435 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_CBYCRY
;
437 case FIMC_FMT_CRYCBY422
:
438 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_CRYCBY
;
440 case FIMC_FMT_YCBYCR422
:
442 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_YCBYCR
;
445 dbg("ctx->out_order_1p= %d", ctx
->out_order_1p
);
448 void fimc_prepare_dma_offset(struct fimc_ctx
*ctx
, struct fimc_frame
*f
)
450 bool pix_hoff
= ctx
->fimc_dev
->drv_data
->dma_pix_hoff
;
453 for (i
= 0; i
< f
->fmt
->memplanes
; i
++)
454 depth
+= f
->fmt
->depth
[i
];
456 f
->dma_offset
.y_h
= f
->offs_h
;
458 f
->dma_offset
.y_h
*= (depth
>> 3);
460 f
->dma_offset
.y_v
= f
->offs_v
;
462 f
->dma_offset
.cb_h
= f
->offs_h
;
463 f
->dma_offset
.cb_v
= f
->offs_v
;
465 f
->dma_offset
.cr_h
= f
->offs_h
;
466 f
->dma_offset
.cr_v
= f
->offs_v
;
469 if (f
->fmt
->colplanes
== 3) {
470 f
->dma_offset
.cb_h
>>= 1;
471 f
->dma_offset
.cr_h
>>= 1;
473 if (f
->fmt
->color
== FIMC_FMT_YCBCR420
) {
474 f
->dma_offset
.cb_v
>>= 1;
475 f
->dma_offset
.cr_v
>>= 1;
479 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
480 f
->fmt
->color
, f
->dma_offset
.y_h
, f
->dma_offset
.y_v
);
483 static int fimc_set_color_effect(struct fimc_ctx
*ctx
, enum v4l2_colorfx colorfx
)
485 struct fimc_effect
*effect
= &ctx
->effect
;
488 case V4L2_COLORFX_NONE
:
489 effect
->type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
491 case V4L2_COLORFX_BW
:
492 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
493 effect
->pat_cb
= 128;
494 effect
->pat_cr
= 128;
496 case V4L2_COLORFX_SEPIA
:
497 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
498 effect
->pat_cb
= 115;
499 effect
->pat_cr
= 145;
501 case V4L2_COLORFX_NEGATIVE
:
502 effect
->type
= FIMC_REG_CIIMGEFF_FIN_NEGATIVE
;
504 case V4L2_COLORFX_EMBOSS
:
505 effect
->type
= FIMC_REG_CIIMGEFF_FIN_EMBOSSING
;
507 case V4L2_COLORFX_ART_FREEZE
:
508 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARTFREEZE
;
510 case V4L2_COLORFX_SILHOUETTE
:
511 effect
->type
= FIMC_REG_CIIMGEFF_FIN_SILHOUETTE
;
513 case V4L2_COLORFX_SET_CBCR
:
514 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
515 effect
->pat_cb
= ctx
->ctrls
.colorfx_cbcr
->val
>> 8;
516 effect
->pat_cr
= ctx
->ctrls
.colorfx_cbcr
->val
& 0xff;
526 * V4L2 controls handling
528 #define ctrl_to_ctx(__ctrl) \
529 container_of((__ctrl)->handler, struct fimc_ctx, ctrls.handler)
531 static int __fimc_s_ctrl(struct fimc_ctx
*ctx
, struct v4l2_ctrl
*ctrl
)
533 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
534 const struct fimc_variant
*variant
= fimc
->variant
;
537 if (ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
)
542 ctx
->hflip
= ctrl
->val
;
546 ctx
->vflip
= ctrl
->val
;
549 case V4L2_CID_ROTATE
:
550 if (fimc_capture_pending(fimc
)) {
551 ret
= fimc_check_scaler_ratio(ctx
, ctx
->s_frame
.width
,
552 ctx
->s_frame
.height
, ctx
->d_frame
.width
,
553 ctx
->d_frame
.height
, ctrl
->val
);
557 if ((ctrl
->val
== 90 || ctrl
->val
== 270) &&
558 !variant
->has_out_rot
)
561 ctx
->rotation
= ctrl
->val
;
564 case V4L2_CID_ALPHA_COMPONENT
:
565 ctx
->d_frame
.alpha
= ctrl
->val
;
568 case V4L2_CID_COLORFX
:
569 ret
= fimc_set_color_effect(ctx
, ctrl
->val
);
575 ctx
->state
|= FIMC_PARAMS
;
576 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
580 static int fimc_s_ctrl(struct v4l2_ctrl
*ctrl
)
582 struct fimc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
586 spin_lock_irqsave(&ctx
->fimc_dev
->slock
, flags
);
587 ret
= __fimc_s_ctrl(ctx
, ctrl
);
588 spin_unlock_irqrestore(&ctx
->fimc_dev
->slock
, flags
);
593 static const struct v4l2_ctrl_ops fimc_ctrl_ops
= {
594 .s_ctrl
= fimc_s_ctrl
,
597 int fimc_ctrls_create(struct fimc_ctx
*ctx
)
599 unsigned int max_alpha
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
600 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
601 struct v4l2_ctrl_handler
*handler
= &ctrls
->handler
;
603 if (ctx
->ctrls
.ready
)
606 v4l2_ctrl_handler_init(handler
, 6);
608 ctrls
->rotate
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
609 V4L2_CID_ROTATE
, 0, 270, 90, 0);
610 ctrls
->hflip
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
611 V4L2_CID_HFLIP
, 0, 1, 1, 0);
612 ctrls
->vflip
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
613 V4L2_CID_VFLIP
, 0, 1, 1, 0);
615 if (ctx
->fimc_dev
->drv_data
->alpha_color
)
616 ctrls
->alpha
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
617 V4L2_CID_ALPHA_COMPONENT
,
622 ctrls
->colorfx
= v4l2_ctrl_new_std_menu(handler
, &fimc_ctrl_ops
,
623 V4L2_CID_COLORFX
, V4L2_COLORFX_SET_CBCR
,
624 ~0x983f, V4L2_COLORFX_NONE
);
626 ctrls
->colorfx_cbcr
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
627 V4L2_CID_COLORFX_CBCR
, 0, 0xffff, 1, 0);
629 ctx
->effect
.type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
631 if (!handler
->error
) {
632 v4l2_ctrl_cluster(2, &ctrls
->colorfx
);
636 return handler
->error
;
639 void fimc_ctrls_delete(struct fimc_ctx
*ctx
)
641 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
644 v4l2_ctrl_handler_free(&ctrls
->handler
);
645 ctrls
->ready
= false;
650 void fimc_ctrls_activate(struct fimc_ctx
*ctx
, bool active
)
652 unsigned int has_alpha
= ctx
->d_frame
.fmt
->flags
& FMT_HAS_ALPHA
;
653 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
658 mutex_lock(ctrls
->handler
.lock
);
659 v4l2_ctrl_activate(ctrls
->rotate
, active
);
660 v4l2_ctrl_activate(ctrls
->hflip
, active
);
661 v4l2_ctrl_activate(ctrls
->vflip
, active
);
662 v4l2_ctrl_activate(ctrls
->colorfx
, active
);
664 v4l2_ctrl_activate(ctrls
->alpha
, active
&& has_alpha
);
667 fimc_set_color_effect(ctx
, ctrls
->colorfx
->cur
.val
);
668 ctx
->rotation
= ctrls
->rotate
->val
;
669 ctx
->hflip
= ctrls
->hflip
->val
;
670 ctx
->vflip
= ctrls
->vflip
->val
;
672 ctx
->effect
.type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
677 mutex_unlock(ctrls
->handler
.lock
);
680 /* Update maximum value of the alpha color control */
681 void fimc_alpha_ctrl_update(struct fimc_ctx
*ctx
)
683 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
684 struct v4l2_ctrl
*ctrl
= ctx
->ctrls
.alpha
;
686 if (ctrl
== NULL
|| !fimc
->drv_data
->alpha_color
)
689 v4l2_ctrl_lock(ctrl
);
690 ctrl
->maximum
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
692 if (ctrl
->cur
.val
> ctrl
->maximum
)
693 ctrl
->cur
.val
= ctrl
->maximum
;
695 v4l2_ctrl_unlock(ctrl
);
698 void __fimc_get_format(struct fimc_frame
*frame
, struct v4l2_format
*f
)
700 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
703 pixm
->width
= frame
->o_width
;
704 pixm
->height
= frame
->o_height
;
705 pixm
->field
= V4L2_FIELD_NONE
;
706 pixm
->pixelformat
= frame
->fmt
->fourcc
;
707 pixm
->colorspace
= V4L2_COLORSPACE_JPEG
;
708 pixm
->num_planes
= frame
->fmt
->memplanes
;
710 for (i
= 0; i
< pixm
->num_planes
; ++i
) {
711 pixm
->plane_fmt
[i
].bytesperline
= frame
->bytesperline
[i
];
712 pixm
->plane_fmt
[i
].sizeimage
= frame
->payload
[i
];
717 * fimc_adjust_mplane_format - adjust bytesperline/sizeimage for each plane
718 * @fmt: fimc pixel format description (input)
719 * @width: requested pixel width
720 * @height: requested pixel height
721 * @pix: multi-plane format to adjust
723 void fimc_adjust_mplane_format(struct fimc_fmt
*fmt
, u32 width
, u32 height
,
724 struct v4l2_pix_format_mplane
*pix
)
726 u32 bytesperline
= 0;
729 pix
->colorspace
= V4L2_COLORSPACE_JPEG
;
730 pix
->field
= V4L2_FIELD_NONE
;
731 pix
->num_planes
= fmt
->memplanes
;
732 pix
->pixelformat
= fmt
->fourcc
;
733 pix
->height
= height
;
736 for (i
= 0; i
< pix
->num_planes
; ++i
) {
737 struct v4l2_plane_pix_format
*plane_fmt
= &pix
->plane_fmt
[i
];
738 u32 bpl
= plane_fmt
->bytesperline
;
740 if (fmt
->colplanes
> 1 && (bpl
== 0 || bpl
< pix
->width
))
741 bpl
= pix
->width
; /* Planar */
743 if (fmt
->colplanes
== 1 && /* Packed */
744 (bpl
== 0 || ((bpl
* 8) / fmt
->depth
[i
]) < pix
->width
))
745 bpl
= (pix
->width
* fmt
->depth
[0]) / 8;
747 * Currently bytesperline for each plane is same, except
748 * V4L2_PIX_FMT_YUV420M format. This calculation may need
749 * to be changed when other multi-planar formats are added
750 * to the fimc_formats[] array.
754 else if (i
== 1 && fmt
->memplanes
== 3)
757 plane_fmt
->bytesperline
= bytesperline
;
758 plane_fmt
->sizeimage
= max((pix
->width
* pix
->height
*
759 fmt
->depth
[i
]) / 8, plane_fmt
->sizeimage
);
764 * fimc_find_format - lookup fimc color format by fourcc or media bus format
765 * @pixelformat: fourcc to match, ignored if null
766 * @mbus_code: media bus code to match, ignored if null
767 * @mask: the color flags to match
768 * @index: offset in the fimc_formats array, ignored if negative
770 struct fimc_fmt
*fimc_find_format(const u32
*pixelformat
, const u32
*mbus_code
,
771 unsigned int mask
, int index
)
773 struct fimc_fmt
*fmt
, *def_fmt
= NULL
;
777 if (index
>= (int)ARRAY_SIZE(fimc_formats
))
780 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
781 fmt
= &fimc_formats
[i
];
782 if (!(fmt
->flags
& mask
))
784 if (pixelformat
&& fmt
->fourcc
== *pixelformat
)
786 if (mbus_code
&& fmt
->mbus_code
== *mbus_code
)
795 static void fimc_clk_put(struct fimc_dev
*fimc
)
798 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++) {
799 if (IS_ERR(fimc
->clock
[i
]))
801 clk_unprepare(fimc
->clock
[i
]);
802 clk_put(fimc
->clock
[i
]);
803 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
807 static int fimc_clk_get(struct fimc_dev
*fimc
)
811 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++)
812 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
814 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++) {
815 fimc
->clock
[i
] = clk_get(&fimc
->pdev
->dev
, fimc_clocks
[i
]);
816 if (IS_ERR(fimc
->clock
[i
])) {
817 ret
= PTR_ERR(fimc
->clock
[i
]);
820 ret
= clk_prepare(fimc
->clock
[i
]);
822 clk_put(fimc
->clock
[i
]);
823 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
830 dev_err(&fimc
->pdev
->dev
, "failed to get clock: %s\n",
836 static int fimc_m2m_suspend(struct fimc_dev
*fimc
)
841 spin_lock_irqsave(&fimc
->slock
, flags
);
842 if (!fimc_m2m_pending(fimc
)) {
843 spin_unlock_irqrestore(&fimc
->slock
, flags
);
846 clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
847 set_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
848 spin_unlock_irqrestore(&fimc
->slock
, flags
);
850 timeout
= wait_event_timeout(fimc
->irq_queue
,
851 test_bit(ST_M2M_SUSPENDED
, &fimc
->state
),
852 FIMC_SHUTDOWN_TIMEOUT
);
854 clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
855 return timeout
== 0 ? -EAGAIN
: 0;
858 static int fimc_m2m_resume(struct fimc_dev
*fimc
)
860 struct fimc_ctx
*ctx
;
863 spin_lock_irqsave(&fimc
->slock
, flags
);
864 /* Clear for full H/W setup in first run after resume */
866 fimc
->m2m
.ctx
= NULL
;
867 spin_unlock_irqrestore(&fimc
->slock
, flags
);
869 if (test_and_clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
))
870 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_ERROR
);
874 #endif /* CONFIG_PM */
876 static const struct of_device_id fimc_of_match
[];
878 static int fimc_parse_dt(struct fimc_dev
*fimc
, u32
*clk_freq
)
880 struct device
*dev
= &fimc
->pdev
->dev
;
881 struct device_node
*node
= dev
->of_node
;
882 const struct of_device_id
*of_id
;
883 struct fimc_variant
*v
;
884 struct fimc_pix_limit
*lim
;
885 u32 args
[FIMC_PIX_LIMITS_MAX
];
888 if (of_property_read_bool(node
, "samsung,lcd-wb"))
891 v
= devm_kzalloc(dev
, sizeof(*v
) + sizeof(*lim
), GFP_KERNEL
);
895 of_id
= of_match_node(fimc_of_match
, node
);
898 fimc
->drv_data
= of_id
->data
;
899 ret
= of_property_read_u32_array(node
, "samsung,pix-limits",
900 args
, FIMC_PIX_LIMITS_MAX
);
904 lim
= (struct fimc_pix_limit
*)&v
[1];
906 lim
->scaler_en_w
= args
[0];
907 lim
->scaler_dis_w
= args
[1];
908 lim
->out_rot_en_w
= args
[2];
909 lim
->out_rot_dis_w
= args
[3];
912 ret
= of_property_read_u32_array(node
, "samsung,min-pix-sizes",
914 v
->min_inp_pixsize
= ret
? FIMC_DEF_MIN_SIZE
: args
[0];
915 v
->min_out_pixsize
= ret
? FIMC_DEF_MIN_SIZE
: args
[1];
916 ret
= of_property_read_u32_array(node
, "samsung,min-pix-alignment",
918 v
->min_vsize_align
= ret
? FIMC_DEF_HEIGHT_ALIGN
: args
[0];
919 v
->hor_offs_align
= ret
? FIMC_DEF_HOR_OFFS_ALIGN
: args
[1];
921 ret
= of_property_read_u32(node
, "samsung,rotators", &args
[1]);
922 v
->has_inp_rot
= ret
? 1 : args
[1] & 0x01;
923 v
->has_out_rot
= ret
? 1 : args
[1] & 0x10;
924 v
->has_mainscaler_ext
= of_property_read_bool(node
,
925 "samsung,mainscaler-ext");
927 v
->has_isp_wb
= of_property_read_bool(node
, "samsung,isp-wb");
928 v
->has_cam_if
= of_property_read_bool(node
, "samsung,cam-if");
929 of_property_read_u32(node
, "clock-frequency", clk_freq
);
930 fimc
->id
= of_alias_get_id(node
, "fimc");
936 static int fimc_probe(struct platform_device
*pdev
)
938 struct device
*dev
= &pdev
->dev
;
940 struct fimc_dev
*fimc
;
941 struct resource
*res
;
944 fimc
= devm_kzalloc(dev
, sizeof(*fimc
), GFP_KERNEL
);
951 ret
= fimc_parse_dt(fimc
, &lclk_freq
);
955 fimc
->drv_data
= fimc_get_drvdata(pdev
);
958 if (!fimc
->drv_data
|| fimc
->id
>= fimc
->drv_data
->num_entities
||
960 dev_err(dev
, "Invalid driver data or device id (%d)\n",
965 fimc
->variant
= fimc
->drv_data
->variant
[fimc
->id
];
967 init_waitqueue_head(&fimc
->irq_queue
);
968 spin_lock_init(&fimc
->slock
);
969 mutex_init(&fimc
->lock
);
971 fimc
->sysreg
= fimc_get_sysreg_regmap(dev
->of_node
);
972 if (IS_ERR(fimc
->sysreg
))
973 return PTR_ERR(fimc
->sysreg
);
975 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
976 fimc
->regs
= devm_ioremap_resource(dev
, res
);
977 if (IS_ERR(fimc
->regs
))
978 return PTR_ERR(fimc
->regs
);
980 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
982 dev_err(dev
, "Failed to get IRQ resource\n");
986 ret
= fimc_clk_get(fimc
);
991 lclk_freq
= fimc
->drv_data
->lclk_frequency
;
993 ret
= clk_set_rate(fimc
->clock
[CLK_BUS
], lclk_freq
);
997 ret
= clk_enable(fimc
->clock
[CLK_BUS
]);
1001 ret
= devm_request_irq(dev
, res
->start
, fimc_irq_handler
,
1002 0, dev_name(dev
), fimc
);
1004 dev_err(dev
, "failed to install irq (%d)\n", ret
);
1008 ret
= fimc_initialize_capture_subdev(fimc
);
1012 platform_set_drvdata(pdev
, fimc
);
1013 pm_runtime_enable(dev
);
1015 if (!pm_runtime_enabled(dev
)) {
1016 ret
= clk_enable(fimc
->clock
[CLK_GATE
]);
1021 vb2_dma_contig_set_max_seg_size(dev
, DMA_BIT_MASK(32));
1023 dev_dbg(dev
, "FIMC.%d registered successfully\n", fimc
->id
);
1027 fimc_unregister_capture_subdev(fimc
);
1029 clk_disable(fimc
->clock
[CLK_BUS
]);
1035 static int fimc_runtime_resume(struct device
*dev
)
1037 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1039 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1041 /* Enable clocks and perform basic initialization */
1042 clk_enable(fimc
->clock
[CLK_GATE
]);
1043 fimc_hw_reset(fimc
);
1045 /* Resume the capture or mem-to-mem device */
1046 if (fimc_capture_busy(fimc
))
1047 return fimc_capture_resume(fimc
);
1049 return fimc_m2m_resume(fimc
);
1052 static int fimc_runtime_suspend(struct device
*dev
)
1054 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1057 if (fimc_capture_busy(fimc
))
1058 ret
= fimc_capture_suspend(fimc
);
1060 ret
= fimc_m2m_suspend(fimc
);
1062 clk_disable(fimc
->clock
[CLK_GATE
]);
1064 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1069 #ifdef CONFIG_PM_SLEEP
1070 static int fimc_resume(struct device
*dev
)
1072 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1073 unsigned long flags
;
1075 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1077 /* Do not resume if the device was idle before system suspend */
1078 spin_lock_irqsave(&fimc
->slock
, flags
);
1079 if (!test_and_clear_bit(ST_LPM
, &fimc
->state
) ||
1080 (!fimc_m2m_active(fimc
) && !fimc_capture_busy(fimc
))) {
1081 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1084 fimc_hw_reset(fimc
);
1085 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1087 if (fimc_capture_busy(fimc
))
1088 return fimc_capture_resume(fimc
);
1090 return fimc_m2m_resume(fimc
);
1093 static int fimc_suspend(struct device
*dev
)
1095 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1097 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1099 if (test_and_set_bit(ST_LPM
, &fimc
->state
))
1101 if (fimc_capture_busy(fimc
))
1102 return fimc_capture_suspend(fimc
);
1104 return fimc_m2m_suspend(fimc
);
1106 #endif /* CONFIG_PM_SLEEP */
1108 static int fimc_remove(struct platform_device
*pdev
)
1110 struct fimc_dev
*fimc
= platform_get_drvdata(pdev
);
1112 pm_runtime_disable(&pdev
->dev
);
1113 if (!pm_runtime_status_suspended(&pdev
->dev
))
1114 clk_disable(fimc
->clock
[CLK_GATE
]);
1115 pm_runtime_set_suspended(&pdev
->dev
);
1117 fimc_unregister_capture_subdev(fimc
);
1118 vb2_dma_contig_clear_max_seg_size(&pdev
->dev
);
1120 clk_disable(fimc
->clock
[CLK_BUS
]);
1123 dev_info(&pdev
->dev
, "driver unloaded\n");
1127 /* Image pixel limits, similar across several FIMC HW revisions. */
1128 static const struct fimc_pix_limit s5p_pix_limit
[4] = {
1130 .scaler_en_w
= 3264,
1131 .scaler_dis_w
= 8192,
1132 .out_rot_en_w
= 1920,
1133 .out_rot_dis_w
= 4224,
1136 .scaler_en_w
= 4224,
1137 .scaler_dis_w
= 8192,
1138 .out_rot_en_w
= 1920,
1139 .out_rot_dis_w
= 4224,
1142 .scaler_en_w
= 1920,
1143 .scaler_dis_w
= 8192,
1144 .out_rot_en_w
= 1280,
1145 .out_rot_dis_w
= 1920,
1149 static const struct fimc_variant fimc0_variant_s5pv210
= {
1153 .min_inp_pixsize
= 16,
1154 .min_out_pixsize
= 16,
1155 .hor_offs_align
= 8,
1156 .min_vsize_align
= 16,
1157 .pix_limit
= &s5p_pix_limit
[1],
1160 static const struct fimc_variant fimc1_variant_s5pv210
= {
1164 .has_mainscaler_ext
= 1,
1165 .min_inp_pixsize
= 16,
1166 .min_out_pixsize
= 16,
1167 .hor_offs_align
= 1,
1168 .min_vsize_align
= 1,
1169 .pix_limit
= &s5p_pix_limit
[2],
1172 static const struct fimc_variant fimc2_variant_s5pv210
= {
1174 .min_inp_pixsize
= 16,
1175 .min_out_pixsize
= 16,
1176 .hor_offs_align
= 8,
1177 .min_vsize_align
= 16,
1178 .pix_limit
= &s5p_pix_limit
[2],
1181 /* S5PV210, S5PC110 */
1182 static const struct fimc_drvdata fimc_drvdata_s5pv210
= {
1184 [0] = &fimc0_variant_s5pv210
,
1185 [1] = &fimc1_variant_s5pv210
,
1186 [2] = &fimc2_variant_s5pv210
,
1189 .lclk_frequency
= 166000000UL,
1194 /* EXYNOS4210, S5PV310, S5PC210 */
1195 static const struct fimc_drvdata fimc_drvdata_exynos4210
= {
1197 .lclk_frequency
= 166000000UL,
1201 .out_buf_count
= 32,
1204 /* EXYNOS4212, EXYNOS4412 */
1205 static const struct fimc_drvdata fimc_drvdata_exynos4x12
= {
1207 .lclk_frequency
= 166000000UL,
1211 .out_buf_count
= 32,
1214 static const struct of_device_id fimc_of_match
[] = {
1216 .compatible
= "samsung,s5pv210-fimc",
1217 .data
= &fimc_drvdata_s5pv210
,
1219 .compatible
= "samsung,exynos4210-fimc",
1220 .data
= &fimc_drvdata_exynos4210
,
1222 .compatible
= "samsung,exynos4212-fimc",
1223 .data
= &fimc_drvdata_exynos4x12
,
1228 static const struct dev_pm_ops fimc_pm_ops
= {
1229 SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend
, fimc_resume
)
1230 SET_RUNTIME_PM_OPS(fimc_runtime_suspend
, fimc_runtime_resume
, NULL
)
1233 static struct platform_driver fimc_driver
= {
1234 .probe
= fimc_probe
,
1235 .remove
= fimc_remove
,
1237 .of_match_table
= fimc_of_match
,
1238 .name
= FIMC_DRIVER_NAME
,
1243 int __init
fimc_register_driver(void)
1245 return platform_driver_register(&fimc_driver
);
1248 void __exit
fimc_unregister_driver(void)
1250 platform_driver_unregister(&fimc_driver
);