1
2
3
4
5
6
7
8
9
10
11
12
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>
24#include <linux/io.h>
25#include <linux/of.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>
32
33#include "fimc-core.h"
34#include "fimc-reg.h"
35#include "media-dev.h"
36
37static char *fimc_clocks[MAX_FIMC_CLOCKS] = {
38 "sclk_fimc", "fimc"
39};
40
41static struct fimc_fmt fimc_formats[] = {
42 {
43 .name = "RGB565",
44 .fourcc = V4L2_PIX_FMT_RGB565,
45 .depth = { 16 },
46 .color = FIMC_FMT_RGB565,
47 .memplanes = 1,
48 .colplanes = 1,
49 .flags = FMT_FLAGS_M2M,
50 }, {
51 .name = "BGR666",
52 .fourcc = V4L2_PIX_FMT_BGR666,
53 .depth = { 32 },
54 .color = FIMC_FMT_RGB666,
55 .memplanes = 1,
56 .colplanes = 1,
57 .flags = FMT_FLAGS_M2M,
58 }, {
59 .name = "BGRA8888, 32 bpp",
60 .fourcc = V4L2_PIX_FMT_BGR32,
61 .depth = { 32 },
62 .color = FIMC_FMT_RGB888,
63 .memplanes = 1,
64 .colplanes = 1,
65 .flags = FMT_FLAGS_M2M | FMT_HAS_ALPHA,
66 }, {
67 .name = "ARGB1555",
68 .fourcc = V4L2_PIX_FMT_RGB555,
69 .depth = { 16 },
70 .color = FIMC_FMT_RGB555,
71 .memplanes = 1,
72 .colplanes = 1,
73 .flags = FMT_FLAGS_M2M_OUT | FMT_HAS_ALPHA,
74 }, {
75 .name = "ARGB4444",
76 .fourcc = V4L2_PIX_FMT_RGB444,
77 .depth = { 16 },
78 .color = FIMC_FMT_RGB444,
79 .memplanes = 1,
80 .colplanes = 1,
81 .flags = FMT_FLAGS_M2M_OUT | FMT_HAS_ALPHA,
82 }, {
83 .name = "YUV 4:4:4",
84 .mbus_code = MEDIA_BUS_FMT_YUV10_1X30,
85 .flags = FMT_FLAGS_WRITEBACK,
86 }, {
87 .name = "YUV 4:2:2 packed, YCbYCr",
88 .fourcc = V4L2_PIX_FMT_YUYV,
89 .depth = { 16 },
90 .color = FIMC_FMT_YCBYCR422,
91 .memplanes = 1,
92 .colplanes = 1,
93 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
94 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
95 }, {
96 .name = "YUV 4:2:2 packed, CbYCrY",
97 .fourcc = V4L2_PIX_FMT_UYVY,
98 .depth = { 16 },
99 .color = FIMC_FMT_CBYCRY422,
100 .memplanes = 1,
101 .colplanes = 1,
102 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
103 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
104 }, {
105 .name = "YUV 4:2:2 packed, CrYCbY",
106 .fourcc = V4L2_PIX_FMT_VYUY,
107 .depth = { 16 },
108 .color = FIMC_FMT_CRYCBY422,
109 .memplanes = 1,
110 .colplanes = 1,
111 .mbus_code = MEDIA_BUS_FMT_VYUY8_2X8,
112 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
113 }, {
114 .name = "YUV 4:2:2 packed, YCrYCb",
115 .fourcc = V4L2_PIX_FMT_YVYU,
116 .depth = { 16 },
117 .color = FIMC_FMT_YCRYCB422,
118 .memplanes = 1,
119 .colplanes = 1,
120 .mbus_code = MEDIA_BUS_FMT_YVYU8_2X8,
121 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
122 }, {
123 .name = "YUV 4:2:2 planar, Y/Cb/Cr",
124 .fourcc = V4L2_PIX_FMT_YUV422P,
125 .depth = { 16 },
126 .color = FIMC_FMT_YCBYCR422,
127 .memplanes = 1,
128 .colplanes = 3,
129 .flags = FMT_FLAGS_M2M,
130 }, {
131 .name = "YUV 4:2:2 planar, Y/CbCr",
132 .fourcc = V4L2_PIX_FMT_NV16,
133 .depth = { 16 },
134 .color = FIMC_FMT_YCBYCR422,
135 .memplanes = 1,
136 .colplanes = 2,
137 .flags = FMT_FLAGS_M2M,
138 }, {
139 .name = "YUV 4:2:2 planar, Y/CrCb",
140 .fourcc = V4L2_PIX_FMT_NV61,
141 .depth = { 16 },
142 .color = FIMC_FMT_YCRYCB422,
143 .memplanes = 1,
144 .colplanes = 2,
145 .flags = FMT_FLAGS_M2M,
146 }, {
147 .name = "YUV 4:2:0 planar, YCbCr",
148 .fourcc = V4L2_PIX_FMT_YUV420,
149 .depth = { 12 },
150 .color = FIMC_FMT_YCBCR420,
151 .memplanes = 1,
152 .colplanes = 3,
153 .flags = FMT_FLAGS_M2M,
154 }, {
155 .name = "YUV 4:2:0 planar, Y/CbCr",
156 .fourcc = V4L2_PIX_FMT_NV12,
157 .depth = { 12 },
158 .color = FIMC_FMT_YCBCR420,
159 .memplanes = 1,
160 .colplanes = 2,
161 .flags = FMT_FLAGS_M2M,
162 }, {
163 .name = "YUV 4:2:0 non-contig. 2p, Y/CbCr",
164 .fourcc = V4L2_PIX_FMT_NV12M,
165 .color = FIMC_FMT_YCBCR420,
166 .depth = { 8, 4 },
167 .memplanes = 2,
168 .colplanes = 2,
169 .flags = FMT_FLAGS_M2M,
170 }, {
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 },
175 .memplanes = 3,
176 .colplanes = 3,
177 .flags = FMT_FLAGS_M2M,
178 }, {
179 .name = "YUV 4:2:0 non-contig. 2p, tiled",
180 .fourcc = V4L2_PIX_FMT_NV12MT,
181 .color = FIMC_FMT_YCBCR420,
182 .depth = { 8, 4 },
183 .memplanes = 2,
184 .colplanes = 2,
185 .flags = FMT_FLAGS_M2M,
186 }, {
187 .name = "JPEG encoded data",
188 .fourcc = V4L2_PIX_FMT_JPEG,
189 .color = FIMC_FMT_JPEG,
190 .depth = { 8 },
191 .memplanes = 1,
192 .colplanes = 1,
193 .mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
194 .flags = FMT_FLAGS_CAM | FMT_FLAGS_COMPRESSED,
195 }, {
196 .name = "S5C73MX interleaved UYVY/JPEG",
197 .fourcc = V4L2_PIX_FMT_S5C_UYVY_JPG,
198 .color = FIMC_FMT_YUYV_JPEG,
199 .depth = { 8 },
200 .memplanes = 2,
201 .colplanes = 1,
202 .mdataplanes = 0x2,
203 .mbus_code = MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8,
204 .flags = FMT_FLAGS_CAM | FMT_FLAGS_COMPRESSED,
205 },
206};
207
208struct fimc_fmt *fimc_get_format(unsigned int index)
209{
210 if (index >= ARRAY_SIZE(fimc_formats))
211 return NULL;
212
213 return &fimc_formats[index];
214}
215
216int fimc_check_scaler_ratio(struct fimc_ctx *ctx, int sw, int sh,
217 int dw, int dh, int rotation)
218{
219 if (rotation == 90 || rotation == 270)
220 swap(dw, dh);
221
222 if (!ctx->scaler.enabled)
223 return (sw == dw && sh == dh) ? 0 : -EINVAL;
224
225 if ((sw >= SCALER_MAX_HRATIO * dw) || (sh >= SCALER_MAX_VRATIO * dh))
226 return -EINVAL;
227
228 return 0;
229}
230
231static int fimc_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
232{
233 u32 sh = 6;
234
235 if (src >= 64 * tar)
236 return -EINVAL;
237
238 while (sh--) {
239 u32 tmp = 1 << sh;
240 if (src >= tar * tmp) {
241 *shift = sh, *ratio = tmp;
242 return 0;
243 }
244 }
245 *shift = 0, *ratio = 1;
246 return 0;
247}
248
249int fimc_set_scaler_info(struct fimc_ctx *ctx)
250{
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;
256 int tx, ty, sx, sy;
257 int ret;
258
259 if (ctx->rotation == 90 || ctx->rotation == 270) {
260 ty = d_frame->width;
261 tx = d_frame->height;
262 } else {
263 tx = d_frame->width;
264 ty = d_frame->height;
265 }
266 if (tx <= 0 || ty <= 0) {
267 dev_err(dev, "Invalid target size: %dx%d\n", tx, ty);
268 return -EINVAL;
269 }
270
271 sx = s_frame->width;
272 sy = s_frame->height;
273 if (sx <= 0 || sy <= 0) {
274 dev_err(dev, "Invalid source size: %dx%d\n", sx, sy);
275 return -EINVAL;
276 }
277 sc->real_width = sx;
278 sc->real_height = sy;
279
280 ret = fimc_get_scaler_factor(sx, tx, &sc->pre_hratio, &sc->hfactor);
281 if (ret)
282 return ret;
283
284 ret = fimc_get_scaler_factor(sy, ty, &sc->pre_vratio, &sc->vfactor);
285 if (ret)
286 return ret;
287
288 sc->pre_dst_width = sx / sc->pre_hratio;
289 sc->pre_dst_height = sy / sc->pre_vratio;
290
291 if (variant->has_mainscaler_ext) {
292 sc->main_hratio = (sx << 14) / (tx << sc->hfactor);
293 sc->main_vratio = (sy << 14) / (ty << sc->vfactor);
294 } else {
295 sc->main_hratio = (sx << 8) / (tx << sc->hfactor);
296 sc->main_vratio = (sy << 8) / (ty << sc->vfactor);
297
298 }
299
300 sc->scaleup_h = (tx >= sx) ? 1 : 0;
301 sc->scaleup_v = (ty >= sy) ? 1 : 0;
302
303
304 if (s_frame->fmt->color == d_frame->fmt->color
305 && s_frame->width == d_frame->width
306 && s_frame->height == d_frame->height)
307 sc->copy_mode = 1;
308 else
309 sc->copy_mode = 0;
310
311 return 0;
312}
313
314static irqreturn_t fimc_irq_handler(int irq, void *priv)
315{
316 struct fimc_dev *fimc = priv;
317 struct fimc_ctx *ctx;
318
319 fimc_hw_clear_irq(fimc);
320
321 spin_lock(&fimc->slock);
322
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);
327 goto out;
328 }
329 ctx = v4l2_m2m_get_curr_priv(fimc->m2m.m2m_dev);
330 if (ctx != NULL) {
331 spin_unlock(&fimc->slock);
332 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_DONE);
333
334 if (ctx->state & FIMC_CTX_SHUT) {
335 ctx->state &= ~FIMC_CTX_SHUT;
336 wake_up(&fimc->irq_queue);
337 }
338 return IRQ_HANDLED;
339 }
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);
344 }
345out:
346 spin_unlock(&fimc->slock);
347 return IRQ_HANDLED;
348}
349
350
351int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
352 struct fimc_frame *frame, struct fimc_addr *paddr)
353{
354 int ret = 0;
355 u32 pix_size;
356
357 if (vb == NULL || frame == NULL)
358 return -EINVAL;
359
360 pix_size = frame->width * frame->height;
361
362 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
363 frame->fmt->memplanes, frame->fmt->colplanes, pix_size);
364
365 paddr->y = vb2_dma_contig_plane_dma_addr(vb, 0);
366
367 if (frame->fmt->memplanes == 1) {
368 switch (frame->fmt->colplanes) {
369 case 1:
370 paddr->cb = 0;
371 paddr->cr = 0;
372 break;
373 case 2:
374
375 paddr->cb = (u32)(paddr->y + pix_size);
376 paddr->cr = 0;
377 break;
378 case 3:
379 paddr->cb = (u32)(paddr->y + pix_size);
380
381 if (FIMC_FMT_YCBCR420 == frame->fmt->color)
382 paddr->cr = (u32)(paddr->cb
383 + (pix_size >> 2));
384 else
385 paddr->cr = (u32)(paddr->cb
386 + (pix_size >> 1));
387 break;
388 default:
389 return -EINVAL;
390 }
391 } else if (!frame->fmt->mdataplanes) {
392 if (frame->fmt->memplanes >= 2)
393 paddr->cb = vb2_dma_contig_plane_dma_addr(vb, 1);
394
395 if (frame->fmt->memplanes == 3)
396 paddr->cr = vb2_dma_contig_plane_dma_addr(vb, 2);
397 }
398
399 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
400 paddr->y, paddr->cb, paddr->cr, ret);
401
402 return ret;
403}
404
405
406void fimc_set_yuv_order(struct fimc_ctx *ctx)
407{
408
409 ctx->in_order_2p = FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB;
410 ctx->out_order_2p = FIMC_REG_CIOCTRL_ORDER422_2P_LSB_CRCB;
411
412
413 switch (ctx->s_frame.fmt->color) {
414 case FIMC_FMT_YCRYCB422:
415 ctx->in_order_1p = FIMC_REG_MSCTRL_ORDER422_YCRYCB;
416 break;
417 case FIMC_FMT_CBYCRY422:
418 ctx->in_order_1p = FIMC_REG_MSCTRL_ORDER422_CBYCRY;
419 break;
420 case FIMC_FMT_CRYCBY422:
421 ctx->in_order_1p = FIMC_REG_MSCTRL_ORDER422_CRYCBY;
422 break;
423 case FIMC_FMT_YCBYCR422:
424 default:
425 ctx->in_order_1p = FIMC_REG_MSCTRL_ORDER422_YCBYCR;
426 break;
427 }
428 dbg("ctx->in_order_1p= %d", ctx->in_order_1p);
429
430 switch (ctx->d_frame.fmt->color) {
431 case FIMC_FMT_YCRYCB422:
432 ctx->out_order_1p = FIMC_REG_CIOCTRL_ORDER422_YCRYCB;
433 break;
434 case FIMC_FMT_CBYCRY422:
435 ctx->out_order_1p = FIMC_REG_CIOCTRL_ORDER422_CBYCRY;
436 break;
437 case FIMC_FMT_CRYCBY422:
438 ctx->out_order_1p = FIMC_REG_CIOCTRL_ORDER422_CRYCBY;
439 break;
440 case FIMC_FMT_YCBYCR422:
441 default:
442 ctx->out_order_1p = FIMC_REG_CIOCTRL_ORDER422_YCBYCR;
443 break;
444 }
445 dbg("ctx->out_order_1p= %d", ctx->out_order_1p);
446}
447
448void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
449{
450 bool pix_hoff = ctx->fimc_dev->drv_data->dma_pix_hoff;
451 u32 i, depth = 0;
452
453 for (i = 0; i < f->fmt->memplanes; i++)
454 depth += f->fmt->depth[i];
455
456 f->dma_offset.y_h = f->offs_h;
457 if (!pix_hoff)
458 f->dma_offset.y_h *= (depth >> 3);
459
460 f->dma_offset.y_v = f->offs_v;
461
462 f->dma_offset.cb_h = f->offs_h;
463 f->dma_offset.cb_v = f->offs_v;
464
465 f->dma_offset.cr_h = f->offs_h;
466 f->dma_offset.cr_v = f->offs_v;
467
468 if (!pix_hoff) {
469 if (f->fmt->colplanes == 3) {
470 f->dma_offset.cb_h >>= 1;
471 f->dma_offset.cr_h >>= 1;
472 }
473 if (f->fmt->color == FIMC_FMT_YCBCR420) {
474 f->dma_offset.cb_v >>= 1;
475 f->dma_offset.cr_v >>= 1;
476 }
477 }
478
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);
481}
482
483static int fimc_set_color_effect(struct fimc_ctx *ctx, enum v4l2_colorfx colorfx)
484{
485 struct fimc_effect *effect = &ctx->effect;
486
487 switch (colorfx) {
488 case V4L2_COLORFX_NONE:
489 effect->type = FIMC_REG_CIIMGEFF_FIN_BYPASS;
490 break;
491 case V4L2_COLORFX_BW:
492 effect->type = FIMC_REG_CIIMGEFF_FIN_ARBITRARY;
493 effect->pat_cb = 128;
494 effect->pat_cr = 128;
495 break;
496 case V4L2_COLORFX_SEPIA:
497 effect->type = FIMC_REG_CIIMGEFF_FIN_ARBITRARY;
498 effect->pat_cb = 115;
499 effect->pat_cr = 145;
500 break;
501 case V4L2_COLORFX_NEGATIVE:
502 effect->type = FIMC_REG_CIIMGEFF_FIN_NEGATIVE;
503 break;
504 case V4L2_COLORFX_EMBOSS:
505 effect->type = FIMC_REG_CIIMGEFF_FIN_EMBOSSING;
506 break;
507 case V4L2_COLORFX_ART_FREEZE:
508 effect->type = FIMC_REG_CIIMGEFF_FIN_ARTFREEZE;
509 break;
510 case V4L2_COLORFX_SILHOUETTE:
511 effect->type = FIMC_REG_CIIMGEFF_FIN_SILHOUETTE;
512 break;
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;
517 break;
518 default:
519 return -EINVAL;
520 }
521
522 return 0;
523}
524
525
526
527
528#define ctrl_to_ctx(__ctrl) \
529 container_of((__ctrl)->handler, struct fimc_ctx, ctrls.handler)
530
531static int __fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_ctrl *ctrl)
532{
533 struct fimc_dev *fimc = ctx->fimc_dev;
534 const struct fimc_variant *variant = fimc->variant;
535 int ret = 0;
536
537 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
538 return 0;
539
540 switch (ctrl->id) {
541 case V4L2_CID_HFLIP:
542 ctx->hflip = ctrl->val;
543 break;
544
545 case V4L2_CID_VFLIP:
546 ctx->vflip = ctrl->val;
547 break;
548
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);
554 if (ret)
555 return -EINVAL;
556 }
557 if ((ctrl->val == 90 || ctrl->val == 270) &&
558 !variant->has_out_rot)
559 return -EINVAL;
560
561 ctx->rotation = ctrl->val;
562 break;
563
564 case V4L2_CID_ALPHA_COMPONENT:
565 ctx->d_frame.alpha = ctrl->val;
566 break;
567
568 case V4L2_CID_COLORFX:
569 ret = fimc_set_color_effect(ctx, ctrl->val);
570 if (ret)
571 return ret;
572 break;
573 }
574
575 ctx->state |= FIMC_PARAMS;
576 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
577 return 0;
578}
579
580static int fimc_s_ctrl(struct v4l2_ctrl *ctrl)
581{
582 struct fimc_ctx *ctx = ctrl_to_ctx(ctrl);
583 unsigned long flags;
584 int ret;
585
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);
589
590 return ret;
591}
592
593static const struct v4l2_ctrl_ops fimc_ctrl_ops = {
594 .s_ctrl = fimc_s_ctrl,
595};
596
597int fimc_ctrls_create(struct fimc_ctx *ctx)
598{
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;
602
603 if (ctx->ctrls.ready)
604 return 0;
605
606 v4l2_ctrl_handler_init(handler, 6);
607
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);
614
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,
618 0, max_alpha, 1, 0);
619 else
620 ctrls->alpha = NULL;
621
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);
625
626 ctrls->colorfx_cbcr = v4l2_ctrl_new_std(handler, &fimc_ctrl_ops,
627 V4L2_CID_COLORFX_CBCR, 0, 0xffff, 1, 0);
628
629 ctx->effect.type = FIMC_REG_CIIMGEFF_FIN_BYPASS;
630
631 if (!handler->error) {
632 v4l2_ctrl_cluster(2, &ctrls->colorfx);
633 ctrls->ready = true;
634 }
635
636 return handler->error;
637}
638
639void fimc_ctrls_delete(struct fimc_ctx *ctx)
640{
641 struct fimc_ctrls *ctrls = &ctx->ctrls;
642
643 if (ctrls->ready) {
644 v4l2_ctrl_handler_free(&ctrls->handler);
645 ctrls->ready = false;
646 ctrls->alpha = NULL;
647 }
648}
649
650void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active)
651{
652 unsigned int has_alpha = ctx->d_frame.fmt->flags & FMT_HAS_ALPHA;
653 struct fimc_ctrls *ctrls = &ctx->ctrls;
654
655 if (!ctrls->ready)
656 return;
657
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);
663 if (ctrls->alpha)
664 v4l2_ctrl_activate(ctrls->alpha, active && has_alpha);
665
666 if (active) {
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;
671 } else {
672 ctx->effect.type = FIMC_REG_CIIMGEFF_FIN_BYPASS;
673 ctx->rotation = 0;
674 ctx->hflip = 0;
675 ctx->vflip = 0;
676 }
677 mutex_unlock(ctrls->handler.lock);
678}
679
680
681void fimc_alpha_ctrl_update(struct fimc_ctx *ctx)
682{
683 struct fimc_dev *fimc = ctx->fimc_dev;
684 struct v4l2_ctrl *ctrl = ctx->ctrls.alpha;
685
686 if (ctrl == NULL || !fimc->drv_data->alpha_color)
687 return;
688
689 v4l2_ctrl_lock(ctrl);
690 ctrl->maximum = fimc_get_alpha_mask(ctx->d_frame.fmt);
691
692 if (ctrl->cur.val > ctrl->maximum)
693 ctrl->cur.val = ctrl->maximum;
694
695 v4l2_ctrl_unlock(ctrl);
696}
697
698void __fimc_get_format(struct fimc_frame *frame, struct v4l2_format *f)
699{
700 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
701 int i;
702
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;
709
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];
713 }
714}
715
716
717
718
719
720
721
722
723void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
724 struct v4l2_pix_format_mplane *pix)
725{
726 u32 bytesperline = 0;
727 int i;
728
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;
734 pix->width = width;
735
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;
739 u32 sizeimage;
740
741 if (fmt->colplanes > 1 && (bpl == 0 || bpl < pix->width))
742 bpl = pix->width;
743
744 if (fmt->colplanes == 1 &&
745 (bpl == 0 || ((bpl * 8) / fmt->depth[i]) < pix->width))
746 bpl = (pix->width * fmt->depth[0]) / 8;
747
748
749
750
751
752
753 if (i == 0)
754 bytesperline = bpl;
755 else if (i == 1 && fmt->memplanes == 3)
756 bytesperline /= 2;
757
758 plane_fmt->bytesperline = bytesperline;
759 sizeimage = pix->width * pix->height * fmt->depth[i] / 8;
760
761
762 if (tiled_fmt(fmt)) {
763
764 u32 row_size = plane_fmt->bytesperline * 32;
765
766 sizeimage = roundup(sizeimage, row_size);
767 }
768
769 plane_fmt->sizeimage = max(sizeimage, plane_fmt->sizeimage);
770 }
771}
772
773
774
775
776
777
778
779
780struct fimc_fmt *fimc_find_format(const u32 *pixelformat, const u32 *mbus_code,
781 unsigned int mask, int index)
782{
783 struct fimc_fmt *fmt, *def_fmt = NULL;
784 unsigned int i;
785 int id = 0;
786
787 if (index >= (int)ARRAY_SIZE(fimc_formats))
788 return NULL;
789
790 for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
791 fmt = &fimc_formats[i];
792 if (!(fmt->flags & mask))
793 continue;
794 if (pixelformat && fmt->fourcc == *pixelformat)
795 return fmt;
796 if (mbus_code && fmt->mbus_code == *mbus_code)
797 return fmt;
798 if (index == id)
799 def_fmt = fmt;
800 id++;
801 }
802 return def_fmt;
803}
804
805static void fimc_clk_put(struct fimc_dev *fimc)
806{
807 int i;
808 for (i = 0; i < MAX_FIMC_CLOCKS; i++) {
809 if (IS_ERR(fimc->clock[i]))
810 continue;
811 clk_unprepare(fimc->clock[i]);
812 clk_put(fimc->clock[i]);
813 fimc->clock[i] = ERR_PTR(-EINVAL);
814 }
815}
816
817static int fimc_clk_get(struct fimc_dev *fimc)
818{
819 int i, ret;
820
821 for (i = 0; i < MAX_FIMC_CLOCKS; i++)
822 fimc->clock[i] = ERR_PTR(-EINVAL);
823
824 for (i = 0; i < MAX_FIMC_CLOCKS; i++) {
825 fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clocks[i]);
826 if (IS_ERR(fimc->clock[i])) {
827 ret = PTR_ERR(fimc->clock[i]);
828 goto err;
829 }
830 ret = clk_prepare(fimc->clock[i]);
831 if (ret < 0) {
832 clk_put(fimc->clock[i]);
833 fimc->clock[i] = ERR_PTR(-EINVAL);
834 goto err;
835 }
836 }
837 return 0;
838err:
839 fimc_clk_put(fimc);
840 dev_err(&fimc->pdev->dev, "failed to get clock: %s\n",
841 fimc_clocks[i]);
842 return -ENXIO;
843}
844
845#ifdef CONFIG_PM
846static int fimc_m2m_suspend(struct fimc_dev *fimc)
847{
848 unsigned long flags;
849 int timeout;
850
851 spin_lock_irqsave(&fimc->slock, flags);
852 if (!fimc_m2m_pending(fimc)) {
853 spin_unlock_irqrestore(&fimc->slock, flags);
854 return 0;
855 }
856 clear_bit(ST_M2M_SUSPENDED, &fimc->state);
857 set_bit(ST_M2M_SUSPENDING, &fimc->state);
858 spin_unlock_irqrestore(&fimc->slock, flags);
859
860 timeout = wait_event_timeout(fimc->irq_queue,
861 test_bit(ST_M2M_SUSPENDED, &fimc->state),
862 FIMC_SHUTDOWN_TIMEOUT);
863
864 clear_bit(ST_M2M_SUSPENDING, &fimc->state);
865 return timeout == 0 ? -EAGAIN : 0;
866}
867
868static int fimc_m2m_resume(struct fimc_dev *fimc)
869{
870 struct fimc_ctx *ctx;
871 unsigned long flags;
872
873 spin_lock_irqsave(&fimc->slock, flags);
874
875 ctx = fimc->m2m.ctx;
876 fimc->m2m.ctx = NULL;
877 spin_unlock_irqrestore(&fimc->slock, flags);
878
879 if (test_and_clear_bit(ST_M2M_SUSPENDED, &fimc->state))
880 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
881
882 return 0;
883}
884#endif
885
886static const struct of_device_id fimc_of_match[];
887
888static int fimc_parse_dt(struct fimc_dev *fimc, u32 *clk_freq)
889{
890 struct device *dev = &fimc->pdev->dev;
891 struct device_node *node = dev->of_node;
892 const struct of_device_id *of_id;
893 struct fimc_variant *v;
894 struct fimc_pix_limit *lim;
895 u32 args[FIMC_PIX_LIMITS_MAX];
896 int ret;
897
898 if (of_property_read_bool(node, "samsung,lcd-wb"))
899 return -ENODEV;
900
901 v = devm_kzalloc(dev, sizeof(*v) + sizeof(*lim), GFP_KERNEL);
902 if (!v)
903 return -ENOMEM;
904
905 of_id = of_match_node(fimc_of_match, node);
906 if (!of_id)
907 return -EINVAL;
908 fimc->drv_data = of_id->data;
909 ret = of_property_read_u32_array(node, "samsung,pix-limits",
910 args, FIMC_PIX_LIMITS_MAX);
911 if (ret < 0)
912 return ret;
913
914 lim = (struct fimc_pix_limit *)&v[1];
915
916 lim->scaler_en_w = args[0];
917 lim->scaler_dis_w = args[1];
918 lim->out_rot_en_w = args[2];
919 lim->out_rot_dis_w = args[3];
920 v->pix_limit = lim;
921
922 ret = of_property_read_u32_array(node, "samsung,min-pix-sizes",
923 args, 2);
924 v->min_inp_pixsize = ret ? FIMC_DEF_MIN_SIZE : args[0];
925 v->min_out_pixsize = ret ? FIMC_DEF_MIN_SIZE : args[1];
926 ret = of_property_read_u32_array(node, "samsung,min-pix-alignment",
927 args, 2);
928 v->min_vsize_align = ret ? FIMC_DEF_HEIGHT_ALIGN : args[0];
929 v->hor_offs_align = ret ? FIMC_DEF_HOR_OFFS_ALIGN : args[1];
930
931 ret = of_property_read_u32(node, "samsung,rotators", &args[1]);
932 v->has_inp_rot = ret ? 1 : args[1] & 0x01;
933 v->has_out_rot = ret ? 1 : args[1] & 0x10;
934 v->has_mainscaler_ext = of_property_read_bool(node,
935 "samsung,mainscaler-ext");
936
937 v->has_isp_wb = of_property_read_bool(node, "samsung,isp-wb");
938 v->has_cam_if = of_property_read_bool(node, "samsung,cam-if");
939 of_property_read_u32(node, "clock-frequency", clk_freq);
940 fimc->id = of_alias_get_id(node, "fimc");
941
942 fimc->variant = v;
943 return 0;
944}
945
946static int fimc_probe(struct platform_device *pdev)
947{
948 struct device *dev = &pdev->dev;
949 u32 lclk_freq = 0;
950 struct fimc_dev *fimc;
951 struct resource *res;
952 int ret = 0;
953
954 fimc = devm_kzalloc(dev, sizeof(*fimc), GFP_KERNEL);
955 if (!fimc)
956 return -ENOMEM;
957
958 fimc->pdev = pdev;
959
960 if (dev->of_node) {
961 ret = fimc_parse_dt(fimc, &lclk_freq);
962 if (ret < 0)
963 return ret;
964 } else {
965 fimc->drv_data = fimc_get_drvdata(pdev);
966 fimc->id = pdev->id;
967 }
968 if (!fimc->drv_data || fimc->id >= fimc->drv_data->num_entities ||
969 fimc->id < 0) {
970 dev_err(dev, "Invalid driver data or device id (%d)\n",
971 fimc->id);
972 return -EINVAL;
973 }
974 if (!dev->of_node)
975 fimc->variant = fimc->drv_data->variant[fimc->id];
976
977 init_waitqueue_head(&fimc->irq_queue);
978 spin_lock_init(&fimc->slock);
979 mutex_init(&fimc->lock);
980
981 fimc->sysreg = fimc_get_sysreg_regmap(dev->of_node);
982 if (IS_ERR(fimc->sysreg))
983 return PTR_ERR(fimc->sysreg);
984
985 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
986 fimc->regs = devm_ioremap_resource(dev, res);
987 if (IS_ERR(fimc->regs))
988 return PTR_ERR(fimc->regs);
989
990 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
991 if (res == NULL) {
992 dev_err(dev, "Failed to get IRQ resource\n");
993 return -ENXIO;
994 }
995
996 ret = fimc_clk_get(fimc);
997 if (ret)
998 return ret;
999
1000 if (lclk_freq == 0)
1001 lclk_freq = fimc->drv_data->lclk_frequency;
1002
1003 ret = clk_set_rate(fimc->clock[CLK_BUS], lclk_freq);
1004 if (ret < 0)
1005 return ret;
1006
1007 ret = clk_enable(fimc->clock[CLK_BUS]);
1008 if (ret < 0)
1009 return ret;
1010
1011 ret = devm_request_irq(dev, res->start, fimc_irq_handler,
1012 0, dev_name(dev), fimc);
1013 if (ret < 0) {
1014 dev_err(dev, "failed to install irq (%d)\n", ret);
1015 goto err_sclk;
1016 }
1017
1018 ret = fimc_initialize_capture_subdev(fimc);
1019 if (ret < 0)
1020 goto err_sclk;
1021
1022 platform_set_drvdata(pdev, fimc);
1023 pm_runtime_enable(dev);
1024
1025 if (!pm_runtime_enabled(dev)) {
1026 ret = clk_enable(fimc->clock[CLK_GATE]);
1027 if (ret < 0)
1028 goto err_sd;
1029 }
1030
1031 vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
1032
1033 dev_dbg(dev, "FIMC.%d registered successfully\n", fimc->id);
1034 return 0;
1035
1036err_sd:
1037 fimc_unregister_capture_subdev(fimc);
1038err_sclk:
1039 clk_disable(fimc->clock[CLK_BUS]);
1040 fimc_clk_put(fimc);
1041 return ret;
1042}
1043
1044#ifdef CONFIG_PM
1045static int fimc_runtime_resume(struct device *dev)
1046{
1047 struct fimc_dev *fimc = dev_get_drvdata(dev);
1048
1049 dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
1050
1051
1052 clk_enable(fimc->clock[CLK_GATE]);
1053 fimc_hw_reset(fimc);
1054
1055
1056 if (fimc_capture_busy(fimc))
1057 return fimc_capture_resume(fimc);
1058
1059 return fimc_m2m_resume(fimc);
1060}
1061
1062static int fimc_runtime_suspend(struct device *dev)
1063{
1064 struct fimc_dev *fimc = dev_get_drvdata(dev);
1065 int ret = 0;
1066
1067 if (fimc_capture_busy(fimc))
1068 ret = fimc_capture_suspend(fimc);
1069 else
1070 ret = fimc_m2m_suspend(fimc);
1071 if (!ret)
1072 clk_disable(fimc->clock[CLK_GATE]);
1073
1074 dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
1075 return ret;
1076}
1077#endif
1078
1079#ifdef CONFIG_PM_SLEEP
1080static int fimc_resume(struct device *dev)
1081{
1082 struct fimc_dev *fimc = dev_get_drvdata(dev);
1083 unsigned long flags;
1084
1085 dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
1086
1087
1088 spin_lock_irqsave(&fimc->slock, flags);
1089 if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
1090 (!fimc_m2m_active(fimc) && !fimc_capture_busy(fimc))) {
1091 spin_unlock_irqrestore(&fimc->slock, flags);
1092 return 0;
1093 }
1094 fimc_hw_reset(fimc);
1095 spin_unlock_irqrestore(&fimc->slock, flags);
1096
1097 if (fimc_capture_busy(fimc))
1098 return fimc_capture_resume(fimc);
1099
1100 return fimc_m2m_resume(fimc);
1101}
1102
1103static int fimc_suspend(struct device *dev)
1104{
1105 struct fimc_dev *fimc = dev_get_drvdata(dev);
1106
1107 dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
1108
1109 if (test_and_set_bit(ST_LPM, &fimc->state))
1110 return 0;
1111 if (fimc_capture_busy(fimc))
1112 return fimc_capture_suspend(fimc);
1113
1114 return fimc_m2m_suspend(fimc);
1115}
1116#endif
1117
1118static int fimc_remove(struct platform_device *pdev)
1119{
1120 struct fimc_dev *fimc = platform_get_drvdata(pdev);
1121
1122 pm_runtime_disable(&pdev->dev);
1123 if (!pm_runtime_status_suspended(&pdev->dev))
1124 clk_disable(fimc->clock[CLK_GATE]);
1125 pm_runtime_set_suspended(&pdev->dev);
1126
1127 fimc_unregister_capture_subdev(fimc);
1128 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
1129
1130 clk_disable(fimc->clock[CLK_BUS]);
1131 fimc_clk_put(fimc);
1132
1133 dev_info(&pdev->dev, "driver unloaded\n");
1134 return 0;
1135}
1136
1137
1138static const struct fimc_pix_limit s5p_pix_limit[4] = {
1139 [0] = {
1140 .scaler_en_w = 3264,
1141 .scaler_dis_w = 8192,
1142 .out_rot_en_w = 1920,
1143 .out_rot_dis_w = 4224,
1144 },
1145 [1] = {
1146 .scaler_en_w = 4224,
1147 .scaler_dis_w = 8192,
1148 .out_rot_en_w = 1920,
1149 .out_rot_dis_w = 4224,
1150 },
1151 [2] = {
1152 .scaler_en_w = 1920,
1153 .scaler_dis_w = 8192,
1154 .out_rot_en_w = 1280,
1155 .out_rot_dis_w = 1920,
1156 },
1157};
1158
1159static const struct fimc_variant fimc0_variant_s5pv210 = {
1160 .has_inp_rot = 1,
1161 .has_out_rot = 1,
1162 .has_cam_if = 1,
1163 .min_inp_pixsize = 16,
1164 .min_out_pixsize = 16,
1165 .hor_offs_align = 8,
1166 .min_vsize_align = 16,
1167 .pix_limit = &s5p_pix_limit[1],
1168};
1169
1170static const struct fimc_variant fimc1_variant_s5pv210 = {
1171 .has_inp_rot = 1,
1172 .has_out_rot = 1,
1173 .has_cam_if = 1,
1174 .has_mainscaler_ext = 1,
1175 .min_inp_pixsize = 16,
1176 .min_out_pixsize = 16,
1177 .hor_offs_align = 1,
1178 .min_vsize_align = 1,
1179 .pix_limit = &s5p_pix_limit[2],
1180};
1181
1182static const struct fimc_variant fimc2_variant_s5pv210 = {
1183 .has_cam_if = 1,
1184 .min_inp_pixsize = 16,
1185 .min_out_pixsize = 16,
1186 .hor_offs_align = 8,
1187 .min_vsize_align = 16,
1188 .pix_limit = &s5p_pix_limit[2],
1189};
1190
1191
1192static const struct fimc_drvdata fimc_drvdata_s5pv210 = {
1193 .variant = {
1194 [0] = &fimc0_variant_s5pv210,
1195 [1] = &fimc1_variant_s5pv210,
1196 [2] = &fimc2_variant_s5pv210,
1197 },
1198 .num_entities = 3,
1199 .lclk_frequency = 166000000UL,
1200 .out_buf_count = 4,
1201 .dma_pix_hoff = 1,
1202};
1203
1204
1205static const struct fimc_drvdata fimc_drvdata_exynos4210 = {
1206 .num_entities = 4,
1207 .lclk_frequency = 166000000UL,
1208 .dma_pix_hoff = 1,
1209 .cistatus2 = 1,
1210 .alpha_color = 1,
1211 .out_buf_count = 32,
1212};
1213
1214
1215static const struct fimc_drvdata fimc_drvdata_exynos4x12 = {
1216 .num_entities = 4,
1217 .lclk_frequency = 166000000UL,
1218 .dma_pix_hoff = 1,
1219 .cistatus2 = 1,
1220 .alpha_color = 1,
1221 .out_buf_count = 32,
1222};
1223
1224static const struct of_device_id fimc_of_match[] = {
1225 {
1226 .compatible = "samsung,s5pv210-fimc",
1227 .data = &fimc_drvdata_s5pv210,
1228 }, {
1229 .compatible = "samsung,exynos4210-fimc",
1230 .data = &fimc_drvdata_exynos4210,
1231 }, {
1232 .compatible = "samsung,exynos4212-fimc",
1233 .data = &fimc_drvdata_exynos4x12,
1234 },
1235 { },
1236};
1237
1238static const struct dev_pm_ops fimc_pm_ops = {
1239 SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend, fimc_resume)
1240 SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
1241};
1242
1243static struct platform_driver fimc_driver = {
1244 .probe = fimc_probe,
1245 .remove = fimc_remove,
1246 .driver = {
1247 .of_match_table = fimc_of_match,
1248 .name = FIMC_DRIVER_NAME,
1249 .pm = &fimc_pm_ops,
1250 }
1251};
1252
1253int __init fimc_register_driver(void)
1254{
1255 return platform_driver_register(&fimc_driver);
1256}
1257
1258void __exit fimc_unregister_driver(void)
1259{
1260 platform_driver_unregister(&fimc_driver);
1261}
1262