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-core.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
= "ARGB8888, 32 bpp",
60 .fourcc
= V4L2_PIX_FMT_RGB32
,
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
= V4L2_MBUS_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
= V4L2_MBUS_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
= V4L2_MBUS_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
= V4L2_MBUS_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
= V4L2_MBUS_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
= V4L2_MBUS_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
= V4L2_MBUS_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 void __fimc_vidioc_querycap(struct device
*dev
, struct v4l2_capability
*cap
,
219 strlcpy(cap
->driver
, dev
->driver
->name
, sizeof(cap
->driver
));
220 strlcpy(cap
->card
, dev
->driver
->name
, sizeof(cap
->card
));
221 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
222 "platform:%s", dev_name(dev
));
223 cap
->device_caps
= caps
;
224 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
227 int fimc_check_scaler_ratio(struct fimc_ctx
*ctx
, int sw
, int sh
,
228 int dw
, int dh
, int rotation
)
230 if (rotation
== 90 || rotation
== 270)
233 if (!ctx
->scaler
.enabled
)
234 return (sw
== dw
&& sh
== dh
) ? 0 : -EINVAL
;
236 if ((sw
>= SCALER_MAX_HRATIO
* dw
) || (sh
>= SCALER_MAX_VRATIO
* dh
))
242 static int fimc_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
251 if (src
>= tar
* tmp
) {
252 *shift
= sh
, *ratio
= tmp
;
256 *shift
= 0, *ratio
= 1;
260 int fimc_set_scaler_info(struct fimc_ctx
*ctx
)
262 const struct fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
263 struct device
*dev
= &ctx
->fimc_dev
->pdev
->dev
;
264 struct fimc_scaler
*sc
= &ctx
->scaler
;
265 struct fimc_frame
*s_frame
= &ctx
->s_frame
;
266 struct fimc_frame
*d_frame
= &ctx
->d_frame
;
270 if (ctx
->rotation
== 90 || ctx
->rotation
== 270) {
272 tx
= d_frame
->height
;
275 ty
= d_frame
->height
;
277 if (tx
<= 0 || ty
<= 0) {
278 dev_err(dev
, "Invalid target size: %dx%d\n", tx
, ty
);
283 sy
= s_frame
->height
;
284 if (sx
<= 0 || sy
<= 0) {
285 dev_err(dev
, "Invalid source size: %dx%d\n", sx
, sy
);
289 sc
->real_height
= sy
;
291 ret
= fimc_get_scaler_factor(sx
, tx
, &sc
->pre_hratio
, &sc
->hfactor
);
295 ret
= fimc_get_scaler_factor(sy
, ty
, &sc
->pre_vratio
, &sc
->vfactor
);
299 sc
->pre_dst_width
= sx
/ sc
->pre_hratio
;
300 sc
->pre_dst_height
= sy
/ sc
->pre_vratio
;
302 if (variant
->has_mainscaler_ext
) {
303 sc
->main_hratio
= (sx
<< 14) / (tx
<< sc
->hfactor
);
304 sc
->main_vratio
= (sy
<< 14) / (ty
<< sc
->vfactor
);
306 sc
->main_hratio
= (sx
<< 8) / (tx
<< sc
->hfactor
);
307 sc
->main_vratio
= (sy
<< 8) / (ty
<< sc
->vfactor
);
311 sc
->scaleup_h
= (tx
>= sx
) ? 1 : 0;
312 sc
->scaleup_v
= (ty
>= sy
) ? 1 : 0;
314 /* check to see if input and output size/format differ */
315 if (s_frame
->fmt
->color
== d_frame
->fmt
->color
316 && s_frame
->width
== d_frame
->width
317 && s_frame
->height
== d_frame
->height
)
325 static irqreturn_t
fimc_irq_handler(int irq
, void *priv
)
327 struct fimc_dev
*fimc
= priv
;
328 struct fimc_ctx
*ctx
;
330 fimc_hw_clear_irq(fimc
);
332 spin_lock(&fimc
->slock
);
334 if (test_and_clear_bit(ST_M2M_PEND
, &fimc
->state
)) {
335 if (test_and_clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
)) {
336 set_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
337 wake_up(&fimc
->irq_queue
);
340 ctx
= v4l2_m2m_get_curr_priv(fimc
->m2m
.m2m_dev
);
342 spin_unlock(&fimc
->slock
);
343 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_DONE
);
345 if (ctx
->state
& FIMC_CTX_SHUT
) {
346 ctx
->state
&= ~FIMC_CTX_SHUT
;
347 wake_up(&fimc
->irq_queue
);
351 } else if (test_bit(ST_CAPT_PEND
, &fimc
->state
)) {
352 int last_buf
= test_bit(ST_CAPT_JPEG
, &fimc
->state
) &&
353 fimc
->vid_cap
.reqbufs_count
== 1;
354 fimc_capture_irq_handler(fimc
, !last_buf
);
357 spin_unlock(&fimc
->slock
);
361 /* The color format (colplanes, memplanes) must be already configured. */
362 int fimc_prepare_addr(struct fimc_ctx
*ctx
, struct vb2_buffer
*vb
,
363 struct fimc_frame
*frame
, struct fimc_addr
*paddr
)
368 if (vb
== NULL
|| frame
== NULL
)
371 pix_size
= frame
->width
* frame
->height
;
373 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
374 frame
->fmt
->memplanes
, frame
->fmt
->colplanes
, pix_size
);
376 paddr
->y
= vb2_dma_contig_plane_dma_addr(vb
, 0);
378 if (frame
->fmt
->memplanes
== 1) {
379 switch (frame
->fmt
->colplanes
) {
385 /* decompose Y into Y/Cb */
386 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
390 paddr
->cb
= (u32
)(paddr
->y
+ pix_size
);
391 /* decompose Y into Y/Cb/Cr */
392 if (FIMC_FMT_YCBCR420
== frame
->fmt
->color
)
393 paddr
->cr
= (u32
)(paddr
->cb
396 paddr
->cr
= (u32
)(paddr
->cb
402 } else if (!frame
->fmt
->mdataplanes
) {
403 if (frame
->fmt
->memplanes
>= 2)
404 paddr
->cb
= vb2_dma_contig_plane_dma_addr(vb
, 1);
406 if (frame
->fmt
->memplanes
== 3)
407 paddr
->cr
= vb2_dma_contig_plane_dma_addr(vb
, 2);
410 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
411 paddr
->y
, paddr
->cb
, paddr
->cr
, ret
);
416 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
417 void fimc_set_yuv_order(struct fimc_ctx
*ctx
)
419 /* The one only mode supported in SoC. */
420 ctx
->in_order_2p
= FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB
;
421 ctx
->out_order_2p
= FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB
;
423 /* Set order for 1 plane input formats. */
424 switch (ctx
->s_frame
.fmt
->color
) {
425 case FIMC_FMT_YCRYCB422
:
426 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_YCRYCB
;
428 case FIMC_FMT_CBYCRY422
:
429 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_CBYCRY
;
431 case FIMC_FMT_CRYCBY422
:
432 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_CRYCBY
;
434 case FIMC_FMT_YCBYCR422
:
436 ctx
->in_order_1p
= FIMC_REG_MSCTRL_ORDER422_YCBYCR
;
439 dbg("ctx->in_order_1p= %d", ctx
->in_order_1p
);
441 switch (ctx
->d_frame
.fmt
->color
) {
442 case FIMC_FMT_YCRYCB422
:
443 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_YCRYCB
;
445 case FIMC_FMT_CBYCRY422
:
446 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_CBYCRY
;
448 case FIMC_FMT_CRYCBY422
:
449 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_CRYCBY
;
451 case FIMC_FMT_YCBYCR422
:
453 ctx
->out_order_1p
= FIMC_REG_CIOCTRL_ORDER422_YCBYCR
;
456 dbg("ctx->out_order_1p= %d", ctx
->out_order_1p
);
459 void fimc_prepare_dma_offset(struct fimc_ctx
*ctx
, struct fimc_frame
*f
)
461 bool pix_hoff
= ctx
->fimc_dev
->drv_data
->dma_pix_hoff
;
464 for (i
= 0; i
< f
->fmt
->colplanes
; i
++)
465 depth
+= f
->fmt
->depth
[i
];
467 f
->dma_offset
.y_h
= f
->offs_h
;
469 f
->dma_offset
.y_h
*= (depth
>> 3);
471 f
->dma_offset
.y_v
= f
->offs_v
;
473 f
->dma_offset
.cb_h
= f
->offs_h
;
474 f
->dma_offset
.cb_v
= f
->offs_v
;
476 f
->dma_offset
.cr_h
= f
->offs_h
;
477 f
->dma_offset
.cr_v
= f
->offs_v
;
480 if (f
->fmt
->colplanes
== 3) {
481 f
->dma_offset
.cb_h
>>= 1;
482 f
->dma_offset
.cr_h
>>= 1;
484 if (f
->fmt
->color
== FIMC_FMT_YCBCR420
) {
485 f
->dma_offset
.cb_v
>>= 1;
486 f
->dma_offset
.cr_v
>>= 1;
490 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
491 f
->fmt
->color
, f
->dma_offset
.y_h
, f
->dma_offset
.y_v
);
494 static int fimc_set_color_effect(struct fimc_ctx
*ctx
, enum v4l2_colorfx colorfx
)
496 struct fimc_effect
*effect
= &ctx
->effect
;
499 case V4L2_COLORFX_NONE
:
500 effect
->type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
502 case V4L2_COLORFX_BW
:
503 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
504 effect
->pat_cb
= 128;
505 effect
->pat_cr
= 128;
507 case V4L2_COLORFX_SEPIA
:
508 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
509 effect
->pat_cb
= 115;
510 effect
->pat_cr
= 145;
512 case V4L2_COLORFX_NEGATIVE
:
513 effect
->type
= FIMC_REG_CIIMGEFF_FIN_NEGATIVE
;
515 case V4L2_COLORFX_EMBOSS
:
516 effect
->type
= FIMC_REG_CIIMGEFF_FIN_EMBOSSING
;
518 case V4L2_COLORFX_ART_FREEZE
:
519 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARTFREEZE
;
521 case V4L2_COLORFX_SILHOUETTE
:
522 effect
->type
= FIMC_REG_CIIMGEFF_FIN_SILHOUETTE
;
524 case V4L2_COLORFX_SET_CBCR
:
525 effect
->type
= FIMC_REG_CIIMGEFF_FIN_ARBITRARY
;
526 effect
->pat_cb
= ctx
->ctrls
.colorfx_cbcr
->val
>> 8;
527 effect
->pat_cr
= ctx
->ctrls
.colorfx_cbcr
->val
& 0xff;
537 * V4L2 controls handling
539 #define ctrl_to_ctx(__ctrl) \
540 container_of((__ctrl)->handler, struct fimc_ctx, ctrls.handler)
542 static int __fimc_s_ctrl(struct fimc_ctx
*ctx
, struct v4l2_ctrl
*ctrl
)
544 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
545 const struct fimc_variant
*variant
= fimc
->variant
;
548 if (ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
)
553 ctx
->hflip
= ctrl
->val
;
557 ctx
->vflip
= ctrl
->val
;
560 case V4L2_CID_ROTATE
:
561 if (fimc_capture_pending(fimc
)) {
562 ret
= fimc_check_scaler_ratio(ctx
, ctx
->s_frame
.width
,
563 ctx
->s_frame
.height
, ctx
->d_frame
.width
,
564 ctx
->d_frame
.height
, ctrl
->val
);
568 if ((ctrl
->val
== 90 || ctrl
->val
== 270) &&
569 !variant
->has_out_rot
)
572 ctx
->rotation
= ctrl
->val
;
575 case V4L2_CID_ALPHA_COMPONENT
:
576 ctx
->d_frame
.alpha
= ctrl
->val
;
579 case V4L2_CID_COLORFX
:
580 ret
= fimc_set_color_effect(ctx
, ctrl
->val
);
586 ctx
->state
|= FIMC_PARAMS
;
587 set_bit(ST_CAPT_APPLY_CFG
, &fimc
->state
);
591 static int fimc_s_ctrl(struct v4l2_ctrl
*ctrl
)
593 struct fimc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
597 spin_lock_irqsave(&ctx
->fimc_dev
->slock
, flags
);
598 ret
= __fimc_s_ctrl(ctx
, ctrl
);
599 spin_unlock_irqrestore(&ctx
->fimc_dev
->slock
, flags
);
604 static const struct v4l2_ctrl_ops fimc_ctrl_ops
= {
605 .s_ctrl
= fimc_s_ctrl
,
608 int fimc_ctrls_create(struct fimc_ctx
*ctx
)
610 unsigned int max_alpha
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
611 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
612 struct v4l2_ctrl_handler
*handler
= &ctrls
->handler
;
614 if (ctx
->ctrls
.ready
)
617 v4l2_ctrl_handler_init(handler
, 6);
619 ctrls
->rotate
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
620 V4L2_CID_ROTATE
, 0, 270, 90, 0);
621 ctrls
->hflip
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
622 V4L2_CID_HFLIP
, 0, 1, 1, 0);
623 ctrls
->vflip
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
624 V4L2_CID_VFLIP
, 0, 1, 1, 0);
626 if (ctx
->fimc_dev
->drv_data
->alpha_color
)
627 ctrls
->alpha
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
628 V4L2_CID_ALPHA_COMPONENT
,
633 ctrls
->colorfx
= v4l2_ctrl_new_std_menu(handler
, &fimc_ctrl_ops
,
634 V4L2_CID_COLORFX
, V4L2_COLORFX_SET_CBCR
,
635 ~0x983f, V4L2_COLORFX_NONE
);
637 ctrls
->colorfx_cbcr
= v4l2_ctrl_new_std(handler
, &fimc_ctrl_ops
,
638 V4L2_CID_COLORFX_CBCR
, 0, 0xffff, 1, 0);
640 ctx
->effect
.type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
642 if (!handler
->error
) {
643 v4l2_ctrl_cluster(2, &ctrls
->colorfx
);
647 return handler
->error
;
650 void fimc_ctrls_delete(struct fimc_ctx
*ctx
)
652 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
655 v4l2_ctrl_handler_free(&ctrls
->handler
);
656 ctrls
->ready
= false;
661 void fimc_ctrls_activate(struct fimc_ctx
*ctx
, bool active
)
663 unsigned int has_alpha
= ctx
->d_frame
.fmt
->flags
& FMT_HAS_ALPHA
;
664 struct fimc_ctrls
*ctrls
= &ctx
->ctrls
;
669 mutex_lock(ctrls
->handler
.lock
);
670 v4l2_ctrl_activate(ctrls
->rotate
, active
);
671 v4l2_ctrl_activate(ctrls
->hflip
, active
);
672 v4l2_ctrl_activate(ctrls
->vflip
, active
);
673 v4l2_ctrl_activate(ctrls
->colorfx
, active
);
675 v4l2_ctrl_activate(ctrls
->alpha
, active
&& has_alpha
);
678 fimc_set_color_effect(ctx
, ctrls
->colorfx
->cur
.val
);
679 ctx
->rotation
= ctrls
->rotate
->val
;
680 ctx
->hflip
= ctrls
->hflip
->val
;
681 ctx
->vflip
= ctrls
->vflip
->val
;
683 ctx
->effect
.type
= FIMC_REG_CIIMGEFF_FIN_BYPASS
;
688 mutex_unlock(ctrls
->handler
.lock
);
691 /* Update maximum value of the alpha color control */
692 void fimc_alpha_ctrl_update(struct fimc_ctx
*ctx
)
694 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
695 struct v4l2_ctrl
*ctrl
= ctx
->ctrls
.alpha
;
697 if (ctrl
== NULL
|| !fimc
->drv_data
->alpha_color
)
700 v4l2_ctrl_lock(ctrl
);
701 ctrl
->maximum
= fimc_get_alpha_mask(ctx
->d_frame
.fmt
);
703 if (ctrl
->cur
.val
> ctrl
->maximum
)
704 ctrl
->cur
.val
= ctrl
->maximum
;
706 v4l2_ctrl_unlock(ctrl
);
709 void __fimc_get_format(struct fimc_frame
*frame
, struct v4l2_format
*f
)
711 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
714 pixm
->width
= frame
->o_width
;
715 pixm
->height
= frame
->o_height
;
716 pixm
->field
= V4L2_FIELD_NONE
;
717 pixm
->pixelformat
= frame
->fmt
->fourcc
;
718 pixm
->colorspace
= V4L2_COLORSPACE_JPEG
;
719 pixm
->num_planes
= frame
->fmt
->memplanes
;
721 for (i
= 0; i
< pixm
->num_planes
; ++i
) {
722 pixm
->plane_fmt
[i
].bytesperline
= frame
->bytesperline
[i
];
723 pixm
->plane_fmt
[i
].sizeimage
= frame
->payload
[i
];
728 * fimc_adjust_mplane_format - adjust bytesperline/sizeimage for each plane
729 * @fmt: fimc pixel format description (input)
730 * @width: requested pixel width
731 * @height: requested pixel height
732 * @pix: multi-plane format to adjust
734 void fimc_adjust_mplane_format(struct fimc_fmt
*fmt
, u32 width
, u32 height
,
735 struct v4l2_pix_format_mplane
*pix
)
737 u32 bytesperline
= 0;
740 pix
->colorspace
= V4L2_COLORSPACE_JPEG
;
741 pix
->field
= V4L2_FIELD_NONE
;
742 pix
->num_planes
= fmt
->memplanes
;
743 pix
->pixelformat
= fmt
->fourcc
;
744 pix
->height
= height
;
747 for (i
= 0; i
< pix
->num_planes
; ++i
) {
748 struct v4l2_plane_pix_format
*plane_fmt
= &pix
->plane_fmt
[i
];
749 u32 bpl
= plane_fmt
->bytesperline
;
751 if (fmt
->colplanes
> 1 && (bpl
== 0 || bpl
< pix
->width
))
752 bpl
= pix
->width
; /* Planar */
754 if (fmt
->colplanes
== 1 && /* Packed */
755 (bpl
== 0 || ((bpl
* 8) / fmt
->depth
[i
]) < pix
->width
))
756 bpl
= (pix
->width
* fmt
->depth
[0]) / 8;
758 * Currently bytesperline for each plane is same, except
759 * V4L2_PIX_FMT_YUV420M format. This calculation may need
760 * to be changed when other multi-planar formats are added
761 * to the fimc_formats[] array.
765 else if (i
== 1 && fmt
->memplanes
== 3)
768 plane_fmt
->bytesperline
= bytesperline
;
769 plane_fmt
->sizeimage
= max((pix
->width
* pix
->height
*
770 fmt
->depth
[i
]) / 8, plane_fmt
->sizeimage
);
775 * fimc_find_format - lookup fimc color format by fourcc or media bus format
776 * @pixelformat: fourcc to match, ignored if null
777 * @mbus_code: media bus code to match, ignored if null
778 * @mask: the color flags to match
779 * @index: offset in the fimc_formats array, ignored if negative
781 struct fimc_fmt
*fimc_find_format(const u32
*pixelformat
, const u32
*mbus_code
,
782 unsigned int mask
, int index
)
784 struct fimc_fmt
*fmt
, *def_fmt
= NULL
;
788 if (index
>= (int)ARRAY_SIZE(fimc_formats
))
791 for (i
= 0; i
< ARRAY_SIZE(fimc_formats
); ++i
) {
792 fmt
= &fimc_formats
[i
];
793 if (!(fmt
->flags
& mask
))
795 if (pixelformat
&& fmt
->fourcc
== *pixelformat
)
797 if (mbus_code
&& fmt
->mbus_code
== *mbus_code
)
806 static void fimc_clk_put(struct fimc_dev
*fimc
)
809 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++) {
810 if (IS_ERR(fimc
->clock
[i
]))
812 clk_unprepare(fimc
->clock
[i
]);
813 clk_put(fimc
->clock
[i
]);
814 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
818 static int fimc_clk_get(struct fimc_dev
*fimc
)
822 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++)
823 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
825 for (i
= 0; i
< MAX_FIMC_CLOCKS
; i
++) {
826 fimc
->clock
[i
] = clk_get(&fimc
->pdev
->dev
, fimc_clocks
[i
]);
827 if (IS_ERR(fimc
->clock
[i
])) {
828 ret
= PTR_ERR(fimc
->clock
[i
]);
831 ret
= clk_prepare(fimc
->clock
[i
]);
833 clk_put(fimc
->clock
[i
]);
834 fimc
->clock
[i
] = ERR_PTR(-EINVAL
);
841 dev_err(&fimc
->pdev
->dev
, "failed to get clock: %s\n",
846 static int fimc_m2m_suspend(struct fimc_dev
*fimc
)
851 spin_lock_irqsave(&fimc
->slock
, flags
);
852 if (!fimc_m2m_pending(fimc
)) {
853 spin_unlock_irqrestore(&fimc
->slock
, flags
);
856 clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
);
857 set_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
858 spin_unlock_irqrestore(&fimc
->slock
, flags
);
860 timeout
= wait_event_timeout(fimc
->irq_queue
,
861 test_bit(ST_M2M_SUSPENDED
, &fimc
->state
),
862 FIMC_SHUTDOWN_TIMEOUT
);
864 clear_bit(ST_M2M_SUSPENDING
, &fimc
->state
);
865 return timeout
== 0 ? -EAGAIN
: 0;
868 static int fimc_m2m_resume(struct fimc_dev
*fimc
)
870 struct fimc_ctx
*ctx
;
873 spin_lock_irqsave(&fimc
->slock
, flags
);
874 /* Clear for full H/W setup in first run after resume */
876 fimc
->m2m
.ctx
= NULL
;
877 spin_unlock_irqrestore(&fimc
->slock
, flags
);
879 if (test_and_clear_bit(ST_M2M_SUSPENDED
, &fimc
->state
))
880 fimc_m2m_job_finish(ctx
, VB2_BUF_STATE_ERROR
);
885 static const struct of_device_id fimc_of_match
[];
887 static int fimc_parse_dt(struct fimc_dev
*fimc
, u32
*clk_freq
)
889 struct device
*dev
= &fimc
->pdev
->dev
;
890 struct device_node
*node
= dev
->of_node
;
891 const struct of_device_id
*of_id
;
892 struct fimc_variant
*v
;
893 struct fimc_pix_limit
*lim
;
894 u32 args
[FIMC_PIX_LIMITS_MAX
];
897 if (of_property_read_bool(node
, "samsung,lcd-wb"))
900 v
= devm_kzalloc(dev
, sizeof(*v
) + sizeof(*lim
), GFP_KERNEL
);
904 of_id
= of_match_node(fimc_of_match
, node
);
907 fimc
->drv_data
= of_id
->data
;
908 ret
= of_property_read_u32_array(node
, "samsung,pix-limits",
909 args
, FIMC_PIX_LIMITS_MAX
);
913 lim
= (struct fimc_pix_limit
*)&v
[1];
915 lim
->scaler_en_w
= args
[0];
916 lim
->scaler_dis_w
= args
[1];
917 lim
->out_rot_en_w
= args
[2];
918 lim
->out_rot_dis_w
= args
[3];
921 ret
= of_property_read_u32_array(node
, "samsung,min-pix-sizes",
923 v
->min_inp_pixsize
= ret
? FIMC_DEF_MIN_SIZE
: args
[0];
924 v
->min_out_pixsize
= ret
? FIMC_DEF_MIN_SIZE
: args
[1];
925 ret
= of_property_read_u32_array(node
, "samsung,min-pix-alignment",
927 v
->min_vsize_align
= ret
? FIMC_DEF_HEIGHT_ALIGN
: args
[0];
928 v
->hor_offs_align
= ret
? FIMC_DEF_HOR_OFFS_ALIGN
: args
[1];
930 ret
= of_property_read_u32(node
, "samsung,rotators", &args
[1]);
931 v
->has_inp_rot
= ret
? 1 : args
[1] & 0x01;
932 v
->has_out_rot
= ret
? 1 : args
[1] & 0x10;
933 v
->has_mainscaler_ext
= of_property_read_bool(node
,
934 "samsung,mainscaler-ext");
936 v
->has_isp_wb
= of_property_read_bool(node
, "samsung,isp-wb");
937 v
->has_cam_if
= of_property_read_bool(node
, "samsung,cam-if");
938 of_property_read_u32(node
, "clock-frequency", clk_freq
);
939 fimc
->id
= of_alias_get_id(node
, "fimc");
945 static int fimc_probe(struct platform_device
*pdev
)
947 struct device
*dev
= &pdev
->dev
;
949 struct fimc_dev
*fimc
;
950 struct resource
*res
;
953 fimc
= devm_kzalloc(dev
, sizeof(*fimc
), GFP_KERNEL
);
960 ret
= fimc_parse_dt(fimc
, &lclk_freq
);
964 fimc
->drv_data
= fimc_get_drvdata(pdev
);
967 if (!fimc
->drv_data
|| fimc
->id
>= fimc
->drv_data
->num_entities
||
969 dev_err(dev
, "Invalid driver data or device id (%d)\n",
974 fimc
->variant
= fimc
->drv_data
->variant
[fimc
->id
];
976 init_waitqueue_head(&fimc
->irq_queue
);
977 spin_lock_init(&fimc
->slock
);
978 mutex_init(&fimc
->lock
);
980 fimc
->sysreg
= fimc_get_sysreg_regmap(dev
->of_node
);
981 if (IS_ERR(fimc
->sysreg
))
982 return PTR_ERR(fimc
->sysreg
);
984 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
985 fimc
->regs
= devm_ioremap_resource(dev
, res
);
986 if (IS_ERR(fimc
->regs
))
987 return PTR_ERR(fimc
->regs
);
989 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
991 dev_err(dev
, "Failed to get IRQ resource\n");
995 ret
= fimc_clk_get(fimc
);
1000 lclk_freq
= fimc
->drv_data
->lclk_frequency
;
1002 ret
= clk_set_rate(fimc
->clock
[CLK_BUS
], lclk_freq
);
1006 ret
= clk_enable(fimc
->clock
[CLK_BUS
]);
1010 ret
= devm_request_irq(dev
, res
->start
, fimc_irq_handler
,
1011 0, dev_name(dev
), fimc
);
1013 dev_err(dev
, "failed to install irq (%d)\n", ret
);
1017 ret
= fimc_initialize_capture_subdev(fimc
);
1021 platform_set_drvdata(pdev
, fimc
);
1022 pm_runtime_enable(dev
);
1023 ret
= pm_runtime_get_sync(dev
);
1026 /* Initialize contiguous memory allocator */
1027 fimc
->alloc_ctx
= vb2_dma_contig_init_ctx(dev
);
1028 if (IS_ERR(fimc
->alloc_ctx
)) {
1029 ret
= PTR_ERR(fimc
->alloc_ctx
);
1033 dev_dbg(dev
, "FIMC.%d registered successfully\n", fimc
->id
);
1035 pm_runtime_put(dev
);
1038 pm_runtime_put(dev
);
1040 fimc_unregister_capture_subdev(fimc
);
1042 clk_disable(fimc
->clock
[CLK_BUS
]);
1047 static int fimc_runtime_resume(struct device
*dev
)
1049 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1051 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1053 /* Enable clocks and perform basic initalization */
1054 clk_enable(fimc
->clock
[CLK_GATE
]);
1055 fimc_hw_reset(fimc
);
1057 /* Resume the capture or mem-to-mem device */
1058 if (fimc_capture_busy(fimc
))
1059 return fimc_capture_resume(fimc
);
1061 return fimc_m2m_resume(fimc
);
1064 static int fimc_runtime_suspend(struct device
*dev
)
1066 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1069 if (fimc_capture_busy(fimc
))
1070 ret
= fimc_capture_suspend(fimc
);
1072 ret
= fimc_m2m_suspend(fimc
);
1074 clk_disable(fimc
->clock
[CLK_GATE
]);
1076 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1080 #ifdef CONFIG_PM_SLEEP
1081 static int fimc_resume(struct device
*dev
)
1083 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1084 unsigned long flags
;
1086 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1088 /* Do not resume if the device was idle before system suspend */
1089 spin_lock_irqsave(&fimc
->slock
, flags
);
1090 if (!test_and_clear_bit(ST_LPM
, &fimc
->state
) ||
1091 (!fimc_m2m_active(fimc
) && !fimc_capture_busy(fimc
))) {
1092 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1095 fimc_hw_reset(fimc
);
1096 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1098 if (fimc_capture_busy(fimc
))
1099 return fimc_capture_resume(fimc
);
1101 return fimc_m2m_resume(fimc
);
1104 static int fimc_suspend(struct device
*dev
)
1106 struct fimc_dev
*fimc
= dev_get_drvdata(dev
);
1108 dbg("fimc%d: state: 0x%lx", fimc
->id
, fimc
->state
);
1110 if (test_and_set_bit(ST_LPM
, &fimc
->state
))
1112 if (fimc_capture_busy(fimc
))
1113 return fimc_capture_suspend(fimc
);
1115 return fimc_m2m_suspend(fimc
);
1117 #endif /* CONFIG_PM_SLEEP */
1119 static int fimc_remove(struct platform_device
*pdev
)
1121 struct fimc_dev
*fimc
= platform_get_drvdata(pdev
);
1123 pm_runtime_disable(&pdev
->dev
);
1124 pm_runtime_set_suspended(&pdev
->dev
);
1126 fimc_unregister_capture_subdev(fimc
);
1127 vb2_dma_contig_cleanup_ctx(fimc
->alloc_ctx
);
1129 clk_disable(fimc
->clock
[CLK_BUS
]);
1132 dev_info(&pdev
->dev
, "driver unloaded\n");
1136 /* Image pixel limits, similar across several FIMC HW revisions. */
1137 static const struct fimc_pix_limit s5p_pix_limit
[4] = {
1139 .scaler_en_w
= 3264,
1140 .scaler_dis_w
= 8192,
1141 .out_rot_en_w
= 1920,
1142 .out_rot_dis_w
= 4224,
1145 .scaler_en_w
= 4224,
1146 .scaler_dis_w
= 8192,
1147 .out_rot_en_w
= 1920,
1148 .out_rot_dis_w
= 4224,
1151 .scaler_en_w
= 1920,
1152 .scaler_dis_w
= 8192,
1153 .out_rot_en_w
= 1280,
1154 .out_rot_dis_w
= 1920,
1158 static const struct fimc_variant fimc0_variant_s5p
= {
1162 .min_inp_pixsize
= 16,
1163 .min_out_pixsize
= 16,
1164 .hor_offs_align
= 8,
1165 .min_vsize_align
= 16,
1166 .pix_limit
= &s5p_pix_limit
[0],
1169 static const struct fimc_variant fimc2_variant_s5p
= {
1171 .min_inp_pixsize
= 16,
1172 .min_out_pixsize
= 16,
1173 .hor_offs_align
= 8,
1174 .min_vsize_align
= 16,
1175 .pix_limit
= &s5p_pix_limit
[1],
1178 static const struct fimc_variant fimc0_variant_s5pv210
= {
1182 .min_inp_pixsize
= 16,
1183 .min_out_pixsize
= 16,
1184 .hor_offs_align
= 8,
1185 .min_vsize_align
= 16,
1186 .pix_limit
= &s5p_pix_limit
[1],
1189 static const struct fimc_variant fimc1_variant_s5pv210
= {
1193 .has_mainscaler_ext
= 1,
1194 .min_inp_pixsize
= 16,
1195 .min_out_pixsize
= 16,
1196 .hor_offs_align
= 1,
1197 .min_vsize_align
= 1,
1198 .pix_limit
= &s5p_pix_limit
[2],
1201 static const struct fimc_variant fimc2_variant_s5pv210
= {
1203 .min_inp_pixsize
= 16,
1204 .min_out_pixsize
= 16,
1205 .hor_offs_align
= 8,
1206 .min_vsize_align
= 16,
1207 .pix_limit
= &s5p_pix_limit
[2],
1211 static const struct fimc_drvdata fimc_drvdata_s5p
= {
1213 [0] = &fimc0_variant_s5p
,
1214 [1] = &fimc0_variant_s5p
,
1215 [2] = &fimc2_variant_s5p
,
1218 .lclk_frequency
= 133000000UL,
1222 /* S5PV210, S5PC110 */
1223 static const struct fimc_drvdata fimc_drvdata_s5pv210
= {
1225 [0] = &fimc0_variant_s5pv210
,
1226 [1] = &fimc1_variant_s5pv210
,
1227 [2] = &fimc2_variant_s5pv210
,
1230 .lclk_frequency
= 166000000UL,
1235 /* EXYNOS4210, S5PV310, S5PC210 */
1236 static const struct fimc_drvdata fimc_drvdata_exynos4210
= {
1238 .lclk_frequency
= 166000000UL,
1242 .out_buf_count
= 32,
1245 /* EXYNOS4212, EXYNOS4412 */
1246 static const struct fimc_drvdata fimc_drvdata_exynos4x12
= {
1248 .lclk_frequency
= 166000000UL,
1252 .out_buf_count
= 32,
1255 static const struct platform_device_id fimc_driver_ids
[] = {
1258 .driver_data
= (unsigned long)&fimc_drvdata_s5p
,
1260 .name
= "s5pv210-fimc",
1261 .driver_data
= (unsigned long)&fimc_drvdata_s5pv210
,
1263 .name
= "exynos4-fimc",
1264 .driver_data
= (unsigned long)&fimc_drvdata_exynos4210
,
1266 .name
= "exynos4x12-fimc",
1267 .driver_data
= (unsigned long)&fimc_drvdata_exynos4x12
,
1272 static const struct of_device_id fimc_of_match
[] = {
1274 .compatible
= "samsung,s5pv210-fimc",
1275 .data
= &fimc_drvdata_s5pv210
,
1277 .compatible
= "samsung,exynos4210-fimc",
1278 .data
= &fimc_drvdata_exynos4210
,
1280 .compatible
= "samsung,exynos4212-fimc",
1281 .data
= &fimc_drvdata_exynos4x12
,
1286 static const struct dev_pm_ops fimc_pm_ops
= {
1287 SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend
, fimc_resume
)
1288 SET_RUNTIME_PM_OPS(fimc_runtime_suspend
, fimc_runtime_resume
, NULL
)
1291 static struct platform_driver fimc_driver
= {
1292 .probe
= fimc_probe
,
1293 .remove
= fimc_remove
,
1294 .id_table
= fimc_driver_ids
,
1296 .of_match_table
= fimc_of_match
,
1297 .name
= FIMC_DRIVER_NAME
,
1298 .owner
= THIS_MODULE
,
1303 int __init
fimc_register_driver(void)
1305 return platform_driver_register(&fimc_driver
);
1308 void __exit
fimc_unregister_driver(void)
1310 platform_driver_unregister(&fimc_driver
);