[media] s5p-fimc: Configure scaler registers depending on FIMC version
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / s5p-fimc / fimc-core.c
1 /*
2 * S5P camera interface (video postprocessor) driver
3 *
4 * Copyright (c) 2010 Samsung Electronics Co., Ltd
5 *
6 * Sylwester Nawrocki, <s.nawrocki@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation, either version 2 of the License,
11 * or (at your option) any later version.
12 */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/version.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/bug.h>
20 #include <linux/interrupt.h>
21 #include <linux/device.h>
22 #include <linux/platform_device.h>
23 #include <linux/list.h>
24 #include <linux/io.h>
25 #include <linux/slab.h>
26 #include <linux/clk.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/videobuf2-core.h>
29 #include <media/videobuf2-dma-contig.h>
30
31 #include "fimc-core.h"
32
33 static char *fimc_clocks[MAX_FIMC_CLOCKS] = {
34 "sclk_fimc", "fimc", "sclk_cam"
35 };
36
37 static struct fimc_fmt fimc_formats[] = {
38 {
39 .name = "RGB565",
40 .fourcc = V4L2_PIX_FMT_RGB565X,
41 .depth = { 16 },
42 .color = S5P_FIMC_RGB565,
43 .memplanes = 1,
44 .colplanes = 1,
45 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_BE,
46 .flags = FMT_FLAGS_M2M,
47 }, {
48 .name = "BGR666",
49 .fourcc = V4L2_PIX_FMT_BGR666,
50 .depth = { 32 },
51 .color = S5P_FIMC_RGB666,
52 .memplanes = 1,
53 .colplanes = 1,
54 .flags = FMT_FLAGS_M2M,
55 }, {
56 .name = "XRGB-8-8-8-8, 32 bpp",
57 .fourcc = V4L2_PIX_FMT_RGB32,
58 .depth = { 32 },
59 .color = S5P_FIMC_RGB888,
60 .memplanes = 1,
61 .colplanes = 1,
62 .flags = FMT_FLAGS_M2M,
63 }, {
64 .name = "YUV 4:2:2 packed, YCbYCr",
65 .fourcc = V4L2_PIX_FMT_YUYV,
66 .depth = { 16 },
67 .color = S5P_FIMC_YCBYCR422,
68 .memplanes = 1,
69 .colplanes = 1,
70 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
71 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
72 }, {
73 .name = "YUV 4:2:2 packed, CbYCrY",
74 .fourcc = V4L2_PIX_FMT_UYVY,
75 .depth = { 16 },
76 .color = S5P_FIMC_CBYCRY422,
77 .memplanes = 1,
78 .colplanes = 1,
79 .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
80 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
81 }, {
82 .name = "YUV 4:2:2 packed, CrYCbY",
83 .fourcc = V4L2_PIX_FMT_VYUY,
84 .depth = { 16 },
85 .color = S5P_FIMC_CRYCBY422,
86 .memplanes = 1,
87 .colplanes = 1,
88 .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
89 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
90 }, {
91 .name = "YUV 4:2:2 packed, YCrYCb",
92 .fourcc = V4L2_PIX_FMT_YVYU,
93 .depth = { 16 },
94 .color = S5P_FIMC_YCRYCB422,
95 .memplanes = 1,
96 .colplanes = 1,
97 .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
98 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
99 }, {
100 .name = "YUV 4:2:2 planar, Y/Cb/Cr",
101 .fourcc = V4L2_PIX_FMT_YUV422P,
102 .depth = { 12 },
103 .color = S5P_FIMC_YCBYCR422,
104 .memplanes = 1,
105 .colplanes = 3,
106 .flags = FMT_FLAGS_M2M,
107 }, {
108 .name = "YUV 4:2:2 planar, Y/CbCr",
109 .fourcc = V4L2_PIX_FMT_NV16,
110 .depth = { 16 },
111 .color = S5P_FIMC_YCBYCR422,
112 .memplanes = 1,
113 .colplanes = 2,
114 .flags = FMT_FLAGS_M2M,
115 }, {
116 .name = "YUV 4:2:2 planar, Y/CrCb",
117 .fourcc = V4L2_PIX_FMT_NV61,
118 .depth = { 16 },
119 .color = S5P_FIMC_YCRYCB422,
120 .memplanes = 1,
121 .colplanes = 2,
122 .flags = FMT_FLAGS_M2M,
123 }, {
124 .name = "YUV 4:2:0 planar, YCbCr",
125 .fourcc = V4L2_PIX_FMT_YUV420,
126 .depth = { 12 },
127 .color = S5P_FIMC_YCBCR420,
128 .memplanes = 1,
129 .colplanes = 3,
130 .flags = FMT_FLAGS_M2M,
131 }, {
132 .name = "YUV 4:2:0 planar, Y/CbCr",
133 .fourcc = V4L2_PIX_FMT_NV12,
134 .depth = { 12 },
135 .color = S5P_FIMC_YCBCR420,
136 .memplanes = 1,
137 .colplanes = 2,
138 .flags = FMT_FLAGS_M2M,
139 }, {
140 .name = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr",
141 .fourcc = V4L2_PIX_FMT_NV12M,
142 .color = S5P_FIMC_YCBCR420,
143 .depth = { 8, 4 },
144 .memplanes = 2,
145 .colplanes = 2,
146 .flags = FMT_FLAGS_M2M,
147 }, {
148 .name = "YUV 4:2:0 non-contiguous 3-planar, Y/Cb/Cr",
149 .fourcc = V4L2_PIX_FMT_YUV420M,
150 .color = S5P_FIMC_YCBCR420,
151 .depth = { 8, 2, 2 },
152 .memplanes = 3,
153 .colplanes = 3,
154 .flags = FMT_FLAGS_M2M,
155 }, {
156 .name = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr, tiled",
157 .fourcc = V4L2_PIX_FMT_NV12MT,
158 .color = S5P_FIMC_YCBCR420,
159 .depth = { 8, 4 },
160 .memplanes = 2,
161 .colplanes = 2,
162 .flags = FMT_FLAGS_M2M,
163 },
164 };
165
166 static struct v4l2_queryctrl fimc_ctrls[] = {
167 {
168 .id = V4L2_CID_HFLIP,
169 .type = V4L2_CTRL_TYPE_BOOLEAN,
170 .name = "Horizontal flip",
171 .minimum = 0,
172 .maximum = 1,
173 .default_value = 0,
174 }, {
175 .id = V4L2_CID_VFLIP,
176 .type = V4L2_CTRL_TYPE_BOOLEAN,
177 .name = "Vertical flip",
178 .minimum = 0,
179 .maximum = 1,
180 .default_value = 0,
181 }, {
182 .id = V4L2_CID_ROTATE,
183 .type = V4L2_CTRL_TYPE_INTEGER,
184 .name = "Rotation (CCW)",
185 .minimum = 0,
186 .maximum = 270,
187 .step = 90,
188 .default_value = 0,
189 },
190 };
191
192
193 static struct v4l2_queryctrl *get_ctrl(int id)
194 {
195 int i;
196
197 for (i = 0; i < ARRAY_SIZE(fimc_ctrls); ++i)
198 if (id == fimc_ctrls[i].id)
199 return &fimc_ctrls[i];
200 return NULL;
201 }
202
203 int fimc_check_scaler_ratio(struct v4l2_rect *r, struct fimc_frame *f)
204 {
205 if (r->width > f->width) {
206 if (f->width > (r->width * SCALER_MAX_HRATIO))
207 return -EINVAL;
208 } else {
209 if ((f->width * SCALER_MAX_HRATIO) < r->width)
210 return -EINVAL;
211 }
212
213 if (r->height > f->height) {
214 if (f->height > (r->height * SCALER_MAX_VRATIO))
215 return -EINVAL;
216 } else {
217 if ((f->height * SCALER_MAX_VRATIO) < r->height)
218 return -EINVAL;
219 }
220
221 return 0;
222 }
223
224 static int fimc_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
225 {
226 u32 sh = 6;
227
228 if (src >= 64 * tar)
229 return -EINVAL;
230
231 while (sh--) {
232 u32 tmp = 1 << sh;
233 if (src >= tar * tmp) {
234 *shift = sh, *ratio = tmp;
235 return 0;
236 }
237 }
238
239 *shift = 0, *ratio = 1;
240
241 dbg("s: %d, t: %d, shift: %d, ratio: %d",
242 src, tar, *shift, *ratio);
243 return 0;
244 }
245
246 int fimc_set_scaler_info(struct fimc_ctx *ctx)
247 {
248 struct fimc_scaler *sc = &ctx->scaler;
249 struct fimc_frame *s_frame = &ctx->s_frame;
250 struct fimc_frame *d_frame = &ctx->d_frame;
251 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
252 int tx, ty, sx, sy;
253 int ret;
254
255 if (ctx->rotation == 90 || ctx->rotation == 270) {
256 ty = d_frame->width;
257 tx = d_frame->height;
258 } else {
259 tx = d_frame->width;
260 ty = d_frame->height;
261 }
262 if (tx <= 0 || ty <= 0) {
263 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev,
264 "invalid target size: %d x %d", tx, ty);
265 return -EINVAL;
266 }
267
268 sx = s_frame->width;
269 sy = s_frame->height;
270 if (sx <= 0 || sy <= 0) {
271 err("invalid source size: %d x %d", sx, sy);
272 return -EINVAL;
273 }
274
275 sc->real_width = sx;
276 sc->real_height = sy;
277 dbg("sx= %d, sy= %d, tx= %d, ty= %d", sx, sy, tx, ty);
278
279 ret = fimc_get_scaler_factor(sx, tx, &sc->pre_hratio, &sc->hfactor);
280 if (ret)
281 return ret;
282
283 ret = fimc_get_scaler_factor(sy, ty, &sc->pre_vratio, &sc->vfactor);
284 if (ret)
285 return ret;
286
287 sc->pre_dst_width = sx / sc->pre_hratio;
288 sc->pre_dst_height = sy / sc->pre_vratio;
289
290 if (variant->has_mainscaler_ext) {
291 sc->main_hratio = (sx << 14) / (tx << sc->hfactor);
292 sc->main_vratio = (sy << 14) / (ty << sc->vfactor);
293 } else {
294 sc->main_hratio = (sx << 8) / (tx << sc->hfactor);
295 sc->main_vratio = (sy << 8) / (ty << sc->vfactor);
296
297 }
298
299 sc->scaleup_h = (tx >= sx) ? 1 : 0;
300 sc->scaleup_v = (ty >= sy) ? 1 : 0;
301
302 /* check to see if input and output size/format differ */
303 if (s_frame->fmt->color == d_frame->fmt->color
304 && s_frame->width == d_frame->width
305 && s_frame->height == d_frame->height)
306 sc->copy_mode = 1;
307 else
308 sc->copy_mode = 0;
309
310 return 0;
311 }
312
313 static void fimc_capture_handler(struct fimc_dev *fimc)
314 {
315 struct fimc_vid_cap *cap = &fimc->vid_cap;
316 struct fimc_vid_buffer *v_buf = NULL;
317
318 if (!list_empty(&cap->active_buf_q)) {
319 v_buf = active_queue_pop(cap);
320 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
321 }
322
323 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
324 wake_up(&fimc->irq_queue);
325 return;
326 }
327
328 if (!list_empty(&cap->pending_buf_q)) {
329
330 v_buf = pending_queue_pop(cap);
331 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
332 v_buf->index = cap->buf_index;
333
334 dbg("hw ptr: %d, sw ptr: %d",
335 fimc_hw_get_frame_index(fimc), cap->buf_index);
336
337 /* Move the buffer to the capture active queue */
338 active_queue_add(cap, v_buf);
339
340 dbg("next frame: %d, done frame: %d",
341 fimc_hw_get_frame_index(fimc), v_buf->index);
342
343 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
344 cap->buf_index = 0;
345
346 } else if (test_and_clear_bit(ST_CAPT_STREAM, &fimc->state) &&
347 cap->active_buf_cnt <= 1) {
348 fimc_deactivate_capture(fimc);
349 }
350
351 dbg("frame: %d, active_buf_cnt= %d",
352 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
353 }
354
355 static irqreturn_t fimc_isr(int irq, void *priv)
356 {
357 struct fimc_dev *fimc = priv;
358
359 BUG_ON(!fimc);
360 fimc_hw_clear_irq(fimc);
361
362 spin_lock(&fimc->slock);
363
364 if (test_and_clear_bit(ST_M2M_PEND, &fimc->state)) {
365 struct vb2_buffer *src_vb, *dst_vb;
366 struct fimc_ctx *ctx = v4l2_m2m_get_curr_priv(fimc->m2m.m2m_dev);
367
368 if (!ctx || !ctx->m2m_ctx)
369 goto isr_unlock;
370
371 src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
372 dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
373 if (src_vb && dst_vb) {
374 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
375 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
376 v4l2_m2m_job_finish(fimc->m2m.m2m_dev, ctx->m2m_ctx);
377 }
378 goto isr_unlock;
379
380 }
381
382 if (test_bit(ST_CAPT_RUN, &fimc->state))
383 fimc_capture_handler(fimc);
384
385 if (test_and_clear_bit(ST_CAPT_PEND, &fimc->state)) {
386 set_bit(ST_CAPT_RUN, &fimc->state);
387 wake_up(&fimc->irq_queue);
388 }
389
390 isr_unlock:
391 spin_unlock(&fimc->slock);
392 return IRQ_HANDLED;
393 }
394
395 /* The color format (colplanes, memplanes) must be already configured. */
396 int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
397 struct fimc_frame *frame, struct fimc_addr *paddr)
398 {
399 int ret = 0;
400 u32 pix_size;
401
402 if (vb == NULL || frame == NULL)
403 return -EINVAL;
404
405 pix_size = frame->width * frame->height;
406
407 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
408 frame->fmt->memplanes, frame->fmt->colplanes, pix_size);
409
410 paddr->y = vb2_dma_contig_plane_paddr(vb, 0);
411
412 if (frame->fmt->memplanes == 1) {
413 switch (frame->fmt->colplanes) {
414 case 1:
415 paddr->cb = 0;
416 paddr->cr = 0;
417 break;
418 case 2:
419 /* decompose Y into Y/Cb */
420 paddr->cb = (u32)(paddr->y + pix_size);
421 paddr->cr = 0;
422 break;
423 case 3:
424 paddr->cb = (u32)(paddr->y + pix_size);
425 /* decompose Y into Y/Cb/Cr */
426 if (S5P_FIMC_YCBCR420 == frame->fmt->color)
427 paddr->cr = (u32)(paddr->cb
428 + (pix_size >> 2));
429 else /* 422 */
430 paddr->cr = (u32)(paddr->cb
431 + (pix_size >> 1));
432 break;
433 default:
434 return -EINVAL;
435 }
436 } else {
437 if (frame->fmt->memplanes >= 2)
438 paddr->cb = vb2_dma_contig_plane_paddr(vb, 1);
439
440 if (frame->fmt->memplanes == 3)
441 paddr->cr = vb2_dma_contig_plane_paddr(vb, 2);
442 }
443
444 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
445 paddr->y, paddr->cb, paddr->cr, ret);
446
447 return ret;
448 }
449
450 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
451 static void fimc_set_yuv_order(struct fimc_ctx *ctx)
452 {
453 /* The one only mode supported in SoC. */
454 ctx->in_order_2p = S5P_FIMC_LSB_CRCB;
455 ctx->out_order_2p = S5P_FIMC_LSB_CRCB;
456
457 /* Set order for 1 plane input formats. */
458 switch (ctx->s_frame.fmt->color) {
459 case S5P_FIMC_YCRYCB422:
460 ctx->in_order_1p = S5P_MSCTRL_ORDER422_CBYCRY;
461 break;
462 case S5P_FIMC_CBYCRY422:
463 ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCRYCB;
464 break;
465 case S5P_FIMC_CRYCBY422:
466 ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCBYCR;
467 break;
468 case S5P_FIMC_YCBYCR422:
469 default:
470 ctx->in_order_1p = S5P_MSCTRL_ORDER422_CRYCBY;
471 break;
472 }
473 dbg("ctx->in_order_1p= %d", ctx->in_order_1p);
474
475 switch (ctx->d_frame.fmt->color) {
476 case S5P_FIMC_YCRYCB422:
477 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CBYCRY;
478 break;
479 case S5P_FIMC_CBYCRY422:
480 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCRYCB;
481 break;
482 case S5P_FIMC_CRYCBY422:
483 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCBYCR;
484 break;
485 case S5P_FIMC_YCBYCR422:
486 default:
487 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CRYCBY;
488 break;
489 }
490 dbg("ctx->out_order_1p= %d", ctx->out_order_1p);
491 }
492
493 static void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
494 {
495 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
496 u32 i, depth = 0;
497
498 for (i = 0; i < f->fmt->colplanes; i++)
499 depth += f->fmt->depth[i];
500
501 f->dma_offset.y_h = f->offs_h;
502 if (!variant->pix_hoff)
503 f->dma_offset.y_h *= (depth >> 3);
504
505 f->dma_offset.y_v = f->offs_v;
506
507 f->dma_offset.cb_h = f->offs_h;
508 f->dma_offset.cb_v = f->offs_v;
509
510 f->dma_offset.cr_h = f->offs_h;
511 f->dma_offset.cr_v = f->offs_v;
512
513 if (!variant->pix_hoff) {
514 if (f->fmt->colplanes == 3) {
515 f->dma_offset.cb_h >>= 1;
516 f->dma_offset.cr_h >>= 1;
517 }
518 if (f->fmt->color == S5P_FIMC_YCBCR420) {
519 f->dma_offset.cb_v >>= 1;
520 f->dma_offset.cr_v >>= 1;
521 }
522 }
523
524 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
525 f->fmt->color, f->dma_offset.y_h, f->dma_offset.y_v);
526 }
527
528 /**
529 * fimc_prepare_config - check dimensions, operation and color mode
530 * and pre-calculate offset and the scaling coefficients.
531 *
532 * @ctx: hardware context information
533 * @flags: flags indicating which parameters to check/update
534 *
535 * Return: 0 if dimensions are valid or non zero otherwise.
536 */
537 int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags)
538 {
539 struct fimc_frame *s_frame, *d_frame;
540 struct vb2_buffer *vb = NULL;
541 int ret = 0;
542
543 s_frame = &ctx->s_frame;
544 d_frame = &ctx->d_frame;
545
546 if (flags & FIMC_PARAMS) {
547 /* Prepare the DMA offset ratios for scaler. */
548 fimc_prepare_dma_offset(ctx, &ctx->s_frame);
549 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
550
551 if (s_frame->height > (SCALER_MAX_VRATIO * d_frame->height) ||
552 s_frame->width > (SCALER_MAX_HRATIO * d_frame->width)) {
553 err("out of scaler range");
554 return -EINVAL;
555 }
556 fimc_set_yuv_order(ctx);
557 }
558
559 /* Input DMA mode is not allowed when the scaler is disabled. */
560 ctx->scaler.enabled = 1;
561
562 if (flags & FIMC_SRC_ADDR) {
563 vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
564 ret = fimc_prepare_addr(ctx, vb, s_frame, &s_frame->paddr);
565 if (ret)
566 return ret;
567 }
568
569 if (flags & FIMC_DST_ADDR) {
570 vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
571 ret = fimc_prepare_addr(ctx, vb, d_frame, &d_frame->paddr);
572 }
573
574 return ret;
575 }
576
577 static void fimc_dma_run(void *priv)
578 {
579 struct fimc_ctx *ctx = priv;
580 struct fimc_dev *fimc;
581 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
582 unsigned long flags;
583 u32 ret;
584
585 if (WARN(!ctx, "null hardware context\n"))
586 return;
587
588 fimc = ctx->fimc_dev;
589
590 spin_lock_irqsave(&ctx->slock, flags);
591 set_bit(ST_M2M_PEND, &fimc->state);
592
593 ctx->state |= (FIMC_SRC_ADDR | FIMC_DST_ADDR);
594 ret = fimc_prepare_config(ctx, ctx->state);
595 if (ret) {
596 err("Wrong parameters");
597 goto dma_unlock;
598 }
599 /* Reconfigure hardware if the context has changed. */
600 if (fimc->m2m.ctx != ctx) {
601 ctx->state |= FIMC_PARAMS;
602 fimc->m2m.ctx = ctx;
603 }
604
605 fimc_hw_set_input_addr(fimc, &ctx->s_frame.paddr);
606
607 if (ctx->state & FIMC_PARAMS) {
608 fimc_hw_set_input_path(ctx);
609 fimc_hw_set_in_dma(ctx);
610 if (fimc_set_scaler_info(ctx)) {
611 err("Scaler setup error");
612 goto dma_unlock;
613 }
614
615 fimc_hw_set_prescaler(ctx);
616 if (variant->has_mainscaler_ext)
617 fimc_hw_set_mainscaler_ext(ctx);
618 else
619 fimc_hw_set_mainscaler(ctx);
620 fimc_hw_set_target_format(ctx);
621 fimc_hw_set_rotation(ctx);
622 fimc_hw_set_effect(ctx);
623 }
624
625 fimc_hw_set_output_path(ctx);
626 if (ctx->state & (FIMC_DST_ADDR | FIMC_PARAMS))
627 fimc_hw_set_output_addr(fimc, &ctx->d_frame.paddr, -1);
628
629 if (ctx->state & FIMC_PARAMS)
630 fimc_hw_set_out_dma(ctx);
631
632 fimc_activate_capture(ctx);
633
634 ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
635 FIMC_SRC_FMT | FIMC_DST_FMT);
636 fimc_hw_activate_input_dma(fimc, true);
637
638 dma_unlock:
639 spin_unlock_irqrestore(&ctx->slock, flags);
640 }
641
642 static void fimc_job_abort(void *priv)
643 {
644 /* Nothing done in job_abort. */
645 }
646
647 static int fimc_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
648 unsigned int *num_planes, unsigned long sizes[],
649 void *allocators[])
650 {
651 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
652 struct fimc_frame *f;
653 int i;
654
655 f = ctx_get_frame(ctx, vq->type);
656 if (IS_ERR(f))
657 return PTR_ERR(f);
658
659 /*
660 * Return number of non-contigous planes (plane buffers)
661 * depending on the configured color format.
662 */
663 if (f->fmt)
664 *num_planes = f->fmt->memplanes;
665
666 for (i = 0; i < f->fmt->memplanes; i++) {
667 sizes[i] = (f->width * f->height * f->fmt->depth[i]) >> 3;
668 allocators[i] = ctx->fimc_dev->alloc_ctx;
669 }
670
671 if (*num_buffers == 0)
672 *num_buffers = 1;
673
674 return 0;
675 }
676
677 static int fimc_buf_prepare(struct vb2_buffer *vb)
678 {
679 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
680 struct fimc_frame *frame;
681 int i;
682
683 frame = ctx_get_frame(ctx, vb->vb2_queue->type);
684 if (IS_ERR(frame))
685 return PTR_ERR(frame);
686
687 for (i = 0; i < frame->fmt->memplanes; i++)
688 vb2_set_plane_payload(vb, i, frame->payload[i]);
689
690 return 0;
691 }
692
693 static void fimc_buf_queue(struct vb2_buffer *vb)
694 {
695 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
696
697 dbg("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
698
699 if (ctx->m2m_ctx)
700 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
701 }
702
703 static void fimc_lock(struct vb2_queue *vq)
704 {
705 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
706 mutex_lock(&ctx->fimc_dev->lock);
707 }
708
709 static void fimc_unlock(struct vb2_queue *vq)
710 {
711 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
712 mutex_unlock(&ctx->fimc_dev->lock);
713 }
714
715 struct vb2_ops fimc_qops = {
716 .queue_setup = fimc_queue_setup,
717 .buf_prepare = fimc_buf_prepare,
718 .buf_queue = fimc_buf_queue,
719 .wait_prepare = fimc_unlock,
720 .wait_finish = fimc_lock,
721 };
722
723 static int fimc_m2m_querycap(struct file *file, void *priv,
724 struct v4l2_capability *cap)
725 {
726 struct fimc_ctx *ctx = file->private_data;
727 struct fimc_dev *fimc = ctx->fimc_dev;
728
729 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
730 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
731 cap->bus_info[0] = 0;
732 cap->version = KERNEL_VERSION(1, 0, 0);
733 cap->capabilities = V4L2_CAP_STREAMING |
734 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
735 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
736
737 return 0;
738 }
739
740 int fimc_vidioc_enum_fmt_mplane(struct file *file, void *priv,
741 struct v4l2_fmtdesc *f)
742 {
743 struct fimc_fmt *fmt;
744
745 if (f->index >= ARRAY_SIZE(fimc_formats))
746 return -EINVAL;
747
748 fmt = &fimc_formats[f->index];
749 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
750 f->pixelformat = fmt->fourcc;
751
752 return 0;
753 }
754
755 int fimc_vidioc_g_fmt_mplane(struct file *file, void *priv,
756 struct v4l2_format *f)
757 {
758 struct fimc_ctx *ctx = priv;
759 struct fimc_frame *frame;
760
761 frame = ctx_get_frame(ctx, f->type);
762 if (IS_ERR(frame))
763 return PTR_ERR(frame);
764
765 f->fmt.pix.width = frame->width;
766 f->fmt.pix.height = frame->height;
767 f->fmt.pix.field = V4L2_FIELD_NONE;
768 f->fmt.pix.pixelformat = frame->fmt->fourcc;
769
770 return 0;
771 }
772
773 struct fimc_fmt *find_format(struct v4l2_format *f, unsigned int mask)
774 {
775 struct fimc_fmt *fmt;
776 unsigned int i;
777
778 for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
779 fmt = &fimc_formats[i];
780 if (fmt->fourcc == f->fmt.pix.pixelformat &&
781 (fmt->flags & mask))
782 break;
783 }
784
785 return (i == ARRAY_SIZE(fimc_formats)) ? NULL : fmt;
786 }
787
788 struct fimc_fmt *find_mbus_format(struct v4l2_mbus_framefmt *f,
789 unsigned int mask)
790 {
791 struct fimc_fmt *fmt;
792 unsigned int i;
793
794 for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
795 fmt = &fimc_formats[i];
796 if (fmt->mbus_code == f->code && (fmt->flags & mask))
797 break;
798 }
799
800 return (i == ARRAY_SIZE(fimc_formats)) ? NULL : fmt;
801 }
802
803
804 int fimc_vidioc_try_fmt_mplane(struct file *file, void *priv,
805 struct v4l2_format *f)
806 {
807 struct fimc_ctx *ctx = priv;
808 struct fimc_dev *fimc = ctx->fimc_dev;
809 struct samsung_fimc_variant *variant = fimc->variant;
810 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
811 struct fimc_fmt *fmt;
812 u32 max_width, mod_x, mod_y, mask;
813 int i, is_output = 0;
814
815
816 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
817 if (ctx->state & FIMC_CTX_CAP)
818 return -EINVAL;
819 is_output = 1;
820 } else if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
821 return -EINVAL;
822 }
823
824 dbg("w: %d, h: %d", pix->width, pix->height);
825
826 mask = is_output ? FMT_FLAGS_M2M : FMT_FLAGS_M2M | FMT_FLAGS_CAM;
827 fmt = find_format(f, mask);
828 if (!fmt) {
829 v4l2_err(&fimc->m2m.v4l2_dev, "Fourcc format (0x%X) invalid.\n",
830 pix->pixelformat);
831 return -EINVAL;
832 }
833
834 if (pix->field == V4L2_FIELD_ANY)
835 pix->field = V4L2_FIELD_NONE;
836 else if (V4L2_FIELD_NONE != pix->field)
837 return -EINVAL;
838
839 if (is_output) {
840 max_width = variant->pix_limit->scaler_dis_w;
841 mod_x = ffs(variant->min_inp_pixsize) - 1;
842 } else {
843 max_width = variant->pix_limit->out_rot_dis_w;
844 mod_x = ffs(variant->min_out_pixsize) - 1;
845 }
846
847 if (tiled_fmt(fmt)) {
848 mod_x = 6; /* 64 x 32 pixels tile */
849 mod_y = 5;
850 } else {
851 if (fimc->id == 1 && variant->pix_hoff)
852 mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
853 else
854 mod_y = mod_x;
855 }
856
857 dbg("mod_x: %d, mod_y: %d, max_w: %d", mod_x, mod_y, max_width);
858
859 v4l_bound_align_image(&pix->width, 16, max_width, mod_x,
860 &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
861
862 pix->num_planes = fmt->memplanes;
863
864 for (i = 0; i < pix->num_planes; ++i) {
865 int bpl = pix->plane_fmt[i].bytesperline;
866
867 dbg("[%d] bpl: %d, depth: %d, w: %d, h: %d",
868 i, bpl, fmt->depth[i], pix->width, pix->height);
869
870 if (!bpl || (bpl * 8 / fmt->depth[i]) > pix->width)
871 bpl = (pix->width * fmt->depth[0]) >> 3;
872
873 if (!pix->plane_fmt[i].sizeimage)
874 pix->plane_fmt[i].sizeimage = pix->height * bpl;
875
876 pix->plane_fmt[i].bytesperline = bpl;
877
878 dbg("[%d]: bpl: %d, sizeimage: %d",
879 i, pix->plane_fmt[i].bytesperline,
880 pix->plane_fmt[i].sizeimage);
881 }
882
883 return 0;
884 }
885
886 static int fimc_m2m_s_fmt_mplane(struct file *file, void *priv,
887 struct v4l2_format *f)
888 {
889 struct fimc_ctx *ctx = priv;
890 struct fimc_dev *fimc = ctx->fimc_dev;
891 struct vb2_queue *vq;
892 struct fimc_frame *frame;
893 struct v4l2_pix_format_mplane *pix;
894 unsigned long flags;
895 int i, ret = 0;
896 u32 tmp;
897
898 ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
899 if (ret)
900 return ret;
901
902 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
903
904 if (vb2_is_streaming(vq)) {
905 v4l2_err(&fimc->m2m.v4l2_dev, "queue (%d) busy\n", f->type);
906 return -EBUSY;
907 }
908
909 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
910 frame = &ctx->s_frame;
911 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
912 frame = &ctx->d_frame;
913 } else {
914 v4l2_err(&fimc->m2m.v4l2_dev,
915 "Wrong buffer/video queue type (%d)\n", f->type);
916 return -EINVAL;
917 }
918
919 pix = &f->fmt.pix_mp;
920 frame->fmt = find_format(f, FMT_FLAGS_M2M);
921 if (!frame->fmt)
922 return -EINVAL;
923
924 for (i = 0; i < frame->fmt->colplanes; i++)
925 frame->payload[i] = pix->plane_fmt[i].bytesperline * pix->height;
926
927 frame->f_width = pix->plane_fmt[0].bytesperline * 8 /
928 frame->fmt->depth[0];
929 frame->f_height = pix->height;
930 frame->width = pix->width;
931 frame->height = pix->height;
932 frame->o_width = pix->width;
933 frame->o_height = pix->height;
934 frame->offs_h = 0;
935 frame->offs_v = 0;
936
937 spin_lock_irqsave(&ctx->slock, flags);
938 tmp = (frame == &ctx->d_frame) ? FIMC_DST_FMT : FIMC_SRC_FMT;
939 ctx->state |= FIMC_PARAMS | tmp;
940 spin_unlock_irqrestore(&ctx->slock, flags);
941
942 dbg("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
943
944 return 0;
945 }
946
947 static int fimc_m2m_reqbufs(struct file *file, void *priv,
948 struct v4l2_requestbuffers *reqbufs)
949 {
950 struct fimc_ctx *ctx = priv;
951 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
952 }
953
954 static int fimc_m2m_querybuf(struct file *file, void *priv,
955 struct v4l2_buffer *buf)
956 {
957 struct fimc_ctx *ctx = priv;
958 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
959 }
960
961 static int fimc_m2m_qbuf(struct file *file, void *priv,
962 struct v4l2_buffer *buf)
963 {
964 struct fimc_ctx *ctx = priv;
965
966 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
967 }
968
969 static int fimc_m2m_dqbuf(struct file *file, void *priv,
970 struct v4l2_buffer *buf)
971 {
972 struct fimc_ctx *ctx = priv;
973 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
974 }
975
976 static int fimc_m2m_streamon(struct file *file, void *priv,
977 enum v4l2_buf_type type)
978 {
979 struct fimc_ctx *ctx = priv;
980
981 /* The source and target color format need to be set */
982 if (V4L2_TYPE_IS_OUTPUT(type)) {
983 if (~ctx->state & FIMC_SRC_FMT)
984 return -EINVAL;
985 } else if (~ctx->state & FIMC_DST_FMT) {
986 return -EINVAL;
987 }
988
989 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
990 }
991
992 static int fimc_m2m_streamoff(struct file *file, void *priv,
993 enum v4l2_buf_type type)
994 {
995 struct fimc_ctx *ctx = priv;
996 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
997 }
998
999 int fimc_vidioc_queryctrl(struct file *file, void *priv,
1000 struct v4l2_queryctrl *qc)
1001 {
1002 struct fimc_ctx *ctx = priv;
1003 struct v4l2_queryctrl *c;
1004 int ret = -EINVAL;
1005
1006 c = get_ctrl(qc->id);
1007 if (c) {
1008 *qc = *c;
1009 return 0;
1010 }
1011
1012 if (ctx->state & FIMC_CTX_CAP) {
1013 return v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
1014 core, queryctrl, qc);
1015 }
1016 return ret;
1017 }
1018
1019 int fimc_vidioc_g_ctrl(struct file *file, void *priv,
1020 struct v4l2_control *ctrl)
1021 {
1022 struct fimc_ctx *ctx = priv;
1023 struct fimc_dev *fimc = ctx->fimc_dev;
1024
1025 switch (ctrl->id) {
1026 case V4L2_CID_HFLIP:
1027 ctrl->value = (FLIP_X_AXIS & ctx->flip) ? 1 : 0;
1028 break;
1029 case V4L2_CID_VFLIP:
1030 ctrl->value = (FLIP_Y_AXIS & ctx->flip) ? 1 : 0;
1031 break;
1032 case V4L2_CID_ROTATE:
1033 ctrl->value = ctx->rotation;
1034 break;
1035 default:
1036 if (ctx->state & FIMC_CTX_CAP) {
1037 return v4l2_subdev_call(fimc->vid_cap.sd, core,
1038 g_ctrl, ctrl);
1039 } else {
1040 v4l2_err(&fimc->m2m.v4l2_dev,
1041 "Invalid control\n");
1042 return -EINVAL;
1043 }
1044 }
1045 dbg("ctrl->value= %d", ctrl->value);
1046
1047 return 0;
1048 }
1049
1050 int check_ctrl_val(struct fimc_ctx *ctx, struct v4l2_control *ctrl)
1051 {
1052 struct v4l2_queryctrl *c;
1053 c = get_ctrl(ctrl->id);
1054 if (!c)
1055 return -EINVAL;
1056
1057 if (ctrl->value < c->minimum || ctrl->value > c->maximum
1058 || (c->step != 0 && ctrl->value % c->step != 0)) {
1059 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev,
1060 "Invalid control value\n");
1061 return -ERANGE;
1062 }
1063
1064 return 0;
1065 }
1066
1067 int fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_control *ctrl)
1068 {
1069 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
1070 struct fimc_dev *fimc = ctx->fimc_dev;
1071 unsigned long flags;
1072
1073 spin_lock_irqsave(&ctx->slock, flags);
1074
1075 switch (ctrl->id) {
1076 case V4L2_CID_HFLIP:
1077 if (ctrl->value)
1078 ctx->flip |= FLIP_X_AXIS;
1079 else
1080 ctx->flip &= ~FLIP_X_AXIS;
1081 break;
1082
1083 case V4L2_CID_VFLIP:
1084 if (ctrl->value)
1085 ctx->flip |= FLIP_Y_AXIS;
1086 else
1087 ctx->flip &= ~FLIP_Y_AXIS;
1088 break;
1089
1090 case V4L2_CID_ROTATE:
1091 /* Check for the output rotator availability */
1092 if ((ctrl->value == 90 || ctrl->value == 270) &&
1093 (ctx->in_path == FIMC_DMA && !variant->has_out_rot)) {
1094 spin_unlock_irqrestore(&ctx->slock, flags);
1095 return -EINVAL;
1096 } else {
1097 ctx->rotation = ctrl->value;
1098 }
1099 break;
1100
1101 default:
1102 spin_unlock_irqrestore(&ctx->slock, flags);
1103 v4l2_err(&fimc->m2m.v4l2_dev, "Invalid control\n");
1104 return -EINVAL;
1105 }
1106 ctx->state |= FIMC_PARAMS;
1107 spin_unlock_irqrestore(&ctx->slock, flags);
1108
1109 return 0;
1110 }
1111
1112 static int fimc_m2m_s_ctrl(struct file *file, void *priv,
1113 struct v4l2_control *ctrl)
1114 {
1115 struct fimc_ctx *ctx = priv;
1116 int ret = 0;
1117
1118 ret = check_ctrl_val(ctx, ctrl);
1119 if (ret)
1120 return ret;
1121
1122 ret = fimc_s_ctrl(ctx, ctrl);
1123 return 0;
1124 }
1125
1126 static int fimc_m2m_cropcap(struct file *file, void *fh,
1127 struct v4l2_cropcap *cr)
1128 {
1129 struct fimc_frame *frame;
1130 struct fimc_ctx *ctx = fh;
1131
1132 frame = ctx_get_frame(ctx, cr->type);
1133 if (IS_ERR(frame))
1134 return PTR_ERR(frame);
1135
1136 cr->bounds.left = 0;
1137 cr->bounds.top = 0;
1138 cr->bounds.width = frame->f_width;
1139 cr->bounds.height = frame->f_height;
1140 cr->defrect = cr->bounds;
1141
1142 return 0;
1143 }
1144
1145 static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1146 {
1147 struct fimc_frame *frame;
1148 struct fimc_ctx *ctx = file->private_data;
1149
1150 frame = ctx_get_frame(ctx, cr->type);
1151 if (IS_ERR(frame))
1152 return PTR_ERR(frame);
1153
1154 cr->c.left = frame->offs_h;
1155 cr->c.top = frame->offs_v;
1156 cr->c.width = frame->width;
1157 cr->c.height = frame->height;
1158
1159 return 0;
1160 }
1161
1162 int fimc_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
1163 {
1164 struct fimc_dev *fimc = ctx->fimc_dev;
1165 struct fimc_frame *f;
1166 u32 min_size, halign, depth = 0;
1167 int i;
1168
1169 if (cr->c.top < 0 || cr->c.left < 0) {
1170 v4l2_err(&fimc->m2m.v4l2_dev,
1171 "doesn't support negative values for top & left\n");
1172 return -EINVAL;
1173 }
1174
1175 if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1176 f = (ctx->state & FIMC_CTX_CAP) ? &ctx->s_frame : &ctx->d_frame;
1177 else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1178 ctx->state & FIMC_CTX_M2M)
1179 f = &ctx->s_frame;
1180 else
1181 return -EINVAL;
1182
1183 min_size = (f == &ctx->s_frame) ?
1184 fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
1185
1186 if (ctx->state & FIMC_CTX_M2M) {
1187 if (fimc->id == 1 && fimc->variant->pix_hoff)
1188 halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
1189 else
1190 halign = ffs(min_size) - 1;
1191 /* there are more strict aligment requirements at camera interface */
1192 } else {
1193 min_size = 16;
1194 halign = 4;
1195 }
1196
1197 for (i = 0; i < f->fmt->colplanes; i++)
1198 depth += f->fmt->depth[i];
1199
1200 v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
1201 ffs(min_size) - 1,
1202 &cr->c.height, min_size, f->o_height,
1203 halign, 64/(ALIGN(depth, 8)));
1204
1205 /* adjust left/top if cropping rectangle is out of bounds */
1206 if (cr->c.left + cr->c.width > f->o_width)
1207 cr->c.left = f->o_width - cr->c.width;
1208 if (cr->c.top + cr->c.height > f->o_height)
1209 cr->c.top = f->o_height - cr->c.height;
1210
1211 cr->c.left = round_down(cr->c.left, min_size);
1212 cr->c.top = round_down(cr->c.top,
1213 ctx->state & FIMC_CTX_M2M ? 8 : 16);
1214
1215 dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
1216 cr->c.left, cr->c.top, cr->c.width, cr->c.height,
1217 f->f_width, f->f_height);
1218
1219 return 0;
1220 }
1221
1222
1223 static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1224 {
1225 struct fimc_ctx *ctx = file->private_data;
1226 struct fimc_dev *fimc = ctx->fimc_dev;
1227 unsigned long flags;
1228 struct fimc_frame *f;
1229 int ret;
1230
1231 ret = fimc_try_crop(ctx, cr);
1232 if (ret)
1233 return ret;
1234
1235 f = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
1236 &ctx->s_frame : &ctx->d_frame;
1237
1238 spin_lock_irqsave(&ctx->slock, flags);
1239 if (~ctx->state & (FIMC_SRC_FMT | FIMC_DST_FMT)) {
1240 /* Check to see if scaling ratio is within supported range */
1241 if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1242 ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
1243 else
1244 ret = fimc_check_scaler_ratio(&cr->c, &ctx->s_frame);
1245 if (ret) {
1246 v4l2_err(&fimc->m2m.v4l2_dev, "Out of scaler range");
1247 spin_unlock_irqrestore(&ctx->slock, flags);
1248 return -EINVAL;
1249 }
1250 }
1251 ctx->state |= FIMC_PARAMS;
1252
1253 f->offs_h = cr->c.left;
1254 f->offs_v = cr->c.top;
1255 f->width = cr->c.width;
1256 f->height = cr->c.height;
1257
1258 spin_unlock_irqrestore(&ctx->slock, flags);
1259 return 0;
1260 }
1261
1262 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
1263 .vidioc_querycap = fimc_m2m_querycap,
1264
1265 .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
1266 .vidioc_enum_fmt_vid_out_mplane = fimc_vidioc_enum_fmt_mplane,
1267
1268 .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane,
1269 .vidioc_g_fmt_vid_out_mplane = fimc_vidioc_g_fmt_mplane,
1270
1271 .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane,
1272 .vidioc_try_fmt_vid_out_mplane = fimc_vidioc_try_fmt_mplane,
1273
1274 .vidioc_s_fmt_vid_cap_mplane = fimc_m2m_s_fmt_mplane,
1275 .vidioc_s_fmt_vid_out_mplane = fimc_m2m_s_fmt_mplane,
1276
1277 .vidioc_reqbufs = fimc_m2m_reqbufs,
1278 .vidioc_querybuf = fimc_m2m_querybuf,
1279
1280 .vidioc_qbuf = fimc_m2m_qbuf,
1281 .vidioc_dqbuf = fimc_m2m_dqbuf,
1282
1283 .vidioc_streamon = fimc_m2m_streamon,
1284 .vidioc_streamoff = fimc_m2m_streamoff,
1285
1286 .vidioc_queryctrl = fimc_vidioc_queryctrl,
1287 .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
1288 .vidioc_s_ctrl = fimc_m2m_s_ctrl,
1289
1290 .vidioc_g_crop = fimc_m2m_g_crop,
1291 .vidioc_s_crop = fimc_m2m_s_crop,
1292 .vidioc_cropcap = fimc_m2m_cropcap
1293
1294 };
1295
1296 static int queue_init(void *priv, struct vb2_queue *src_vq,
1297 struct vb2_queue *dst_vq)
1298 {
1299 struct fimc_ctx *ctx = priv;
1300 int ret;
1301
1302 memset(src_vq, 0, sizeof(*src_vq));
1303 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1304 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1305 src_vq->drv_priv = ctx;
1306 src_vq->ops = &fimc_qops;
1307 src_vq->mem_ops = &vb2_dma_contig_memops;
1308 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1309
1310 ret = vb2_queue_init(src_vq);
1311 if (ret)
1312 return ret;
1313
1314 memset(dst_vq, 0, sizeof(*dst_vq));
1315 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1316 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1317 dst_vq->drv_priv = ctx;
1318 dst_vq->ops = &fimc_qops;
1319 dst_vq->mem_ops = &vb2_dma_contig_memops;
1320 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1321
1322 return vb2_queue_init(dst_vq);
1323 }
1324
1325 static int fimc_m2m_open(struct file *file)
1326 {
1327 struct fimc_dev *fimc = video_drvdata(file);
1328 struct fimc_ctx *ctx = NULL;
1329
1330 dbg("pid: %d, state: 0x%lx, refcnt: %d",
1331 task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
1332
1333 /*
1334 * Return if the corresponding video capture node
1335 * is already opened.
1336 */
1337 if (fimc->vid_cap.refcnt > 0)
1338 return -EBUSY;
1339
1340 fimc->m2m.refcnt++;
1341 set_bit(ST_OUTDMA_RUN, &fimc->state);
1342
1343 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1344 if (!ctx)
1345 return -ENOMEM;
1346
1347 file->private_data = ctx;
1348 ctx->fimc_dev = fimc;
1349 /* Default color format */
1350 ctx->s_frame.fmt = &fimc_formats[0];
1351 ctx->d_frame.fmt = &fimc_formats[0];
1352 /* Setup the device context for mem2mem mode. */
1353 ctx->state = FIMC_CTX_M2M;
1354 ctx->flags = 0;
1355 ctx->in_path = FIMC_DMA;
1356 ctx->out_path = FIMC_DMA;
1357 spin_lock_init(&ctx->slock);
1358
1359 ctx->m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
1360 if (IS_ERR(ctx->m2m_ctx)) {
1361 int err = PTR_ERR(ctx->m2m_ctx);
1362 kfree(ctx);
1363 return err;
1364 }
1365
1366 return 0;
1367 }
1368
1369 static int fimc_m2m_release(struct file *file)
1370 {
1371 struct fimc_ctx *ctx = file->private_data;
1372 struct fimc_dev *fimc = ctx->fimc_dev;
1373
1374 dbg("pid: %d, state: 0x%lx, refcnt= %d",
1375 task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
1376
1377 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1378 kfree(ctx);
1379 if (--fimc->m2m.refcnt <= 0)
1380 clear_bit(ST_OUTDMA_RUN, &fimc->state);
1381
1382 return 0;
1383 }
1384
1385 static unsigned int fimc_m2m_poll(struct file *file,
1386 struct poll_table_struct *wait)
1387 {
1388 struct fimc_ctx *ctx = file->private_data;
1389
1390 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1391 }
1392
1393
1394 static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
1395 {
1396 struct fimc_ctx *ctx = file->private_data;
1397
1398 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1399 }
1400
1401 static const struct v4l2_file_operations fimc_m2m_fops = {
1402 .owner = THIS_MODULE,
1403 .open = fimc_m2m_open,
1404 .release = fimc_m2m_release,
1405 .poll = fimc_m2m_poll,
1406 .unlocked_ioctl = video_ioctl2,
1407 .mmap = fimc_m2m_mmap,
1408 };
1409
1410 static struct v4l2_m2m_ops m2m_ops = {
1411 .device_run = fimc_dma_run,
1412 .job_abort = fimc_job_abort,
1413 };
1414
1415 static int fimc_register_m2m_device(struct fimc_dev *fimc)
1416 {
1417 struct video_device *vfd;
1418 struct platform_device *pdev;
1419 struct v4l2_device *v4l2_dev;
1420 int ret = 0;
1421
1422 if (!fimc)
1423 return -ENODEV;
1424
1425 pdev = fimc->pdev;
1426 v4l2_dev = &fimc->m2m.v4l2_dev;
1427
1428 /* set name if it is empty */
1429 if (!v4l2_dev->name[0])
1430 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
1431 "%s.m2m", dev_name(&pdev->dev));
1432
1433 ret = v4l2_device_register(&pdev->dev, v4l2_dev);
1434 if (ret)
1435 goto err_m2m_r1;
1436
1437 vfd = video_device_alloc();
1438 if (!vfd) {
1439 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1440 goto err_m2m_r1;
1441 }
1442
1443 vfd->fops = &fimc_m2m_fops;
1444 vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
1445 vfd->minor = -1;
1446 vfd->release = video_device_release;
1447 vfd->lock = &fimc->lock;
1448
1449 snprintf(vfd->name, sizeof(vfd->name), "%s:m2m", dev_name(&pdev->dev));
1450
1451 video_set_drvdata(vfd, fimc);
1452 platform_set_drvdata(pdev, fimc);
1453
1454 fimc->m2m.vfd = vfd;
1455 fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
1456 if (IS_ERR(fimc->m2m.m2m_dev)) {
1457 v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
1458 ret = PTR_ERR(fimc->m2m.m2m_dev);
1459 goto err_m2m_r2;
1460 }
1461
1462 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1463 if (ret) {
1464 v4l2_err(v4l2_dev,
1465 "%s(): failed to register video device\n", __func__);
1466 goto err_m2m_r3;
1467 }
1468 v4l2_info(v4l2_dev,
1469 "FIMC m2m driver registered as /dev/video%d\n", vfd->num);
1470
1471 return 0;
1472
1473 err_m2m_r3:
1474 v4l2_m2m_release(fimc->m2m.m2m_dev);
1475 err_m2m_r2:
1476 video_device_release(fimc->m2m.vfd);
1477 err_m2m_r1:
1478 v4l2_device_unregister(v4l2_dev);
1479
1480 return ret;
1481 }
1482
1483 static void fimc_unregister_m2m_device(struct fimc_dev *fimc)
1484 {
1485 if (fimc) {
1486 v4l2_m2m_release(fimc->m2m.m2m_dev);
1487 video_unregister_device(fimc->m2m.vfd);
1488
1489 v4l2_device_unregister(&fimc->m2m.v4l2_dev);
1490 }
1491 }
1492
1493 static void fimc_clk_release(struct fimc_dev *fimc)
1494 {
1495 int i;
1496 for (i = 0; i < fimc->num_clocks; i++) {
1497 if (fimc->clock[i]) {
1498 clk_disable(fimc->clock[i]);
1499 clk_put(fimc->clock[i]);
1500 }
1501 }
1502 }
1503
1504 static int fimc_clk_get(struct fimc_dev *fimc)
1505 {
1506 int i;
1507 for (i = 0; i < fimc->num_clocks; i++) {
1508 fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clocks[i]);
1509
1510 if (!IS_ERR_OR_NULL(fimc->clock[i])) {
1511 clk_enable(fimc->clock[i]);
1512 continue;
1513 }
1514 dev_err(&fimc->pdev->dev, "failed to get fimc clock: %s\n",
1515 fimc_clocks[i]);
1516 return -ENXIO;
1517 }
1518 return 0;
1519 }
1520
1521 static int fimc_probe(struct platform_device *pdev)
1522 {
1523 struct fimc_dev *fimc;
1524 struct resource *res;
1525 struct samsung_fimc_driverdata *drv_data;
1526 int ret = 0;
1527 int cap_input_index = -1;
1528
1529 dev_dbg(&pdev->dev, "%s():\n", __func__);
1530
1531 drv_data = (struct samsung_fimc_driverdata *)
1532 platform_get_device_id(pdev)->driver_data;
1533
1534 if (pdev->id >= drv_data->num_entities) {
1535 dev_err(&pdev->dev, "Invalid platform device id: %d\n",
1536 pdev->id);
1537 return -EINVAL;
1538 }
1539
1540 fimc = kzalloc(sizeof(struct fimc_dev), GFP_KERNEL);
1541 if (!fimc)
1542 return -ENOMEM;
1543
1544 fimc->id = pdev->id;
1545 fimc->variant = drv_data->variant[fimc->id];
1546 fimc->pdev = pdev;
1547 fimc->pdata = pdev->dev.platform_data;
1548 fimc->state = ST_IDLE;
1549
1550 init_waitqueue_head(&fimc->irq_queue);
1551 spin_lock_init(&fimc->slock);
1552
1553 mutex_init(&fimc->lock);
1554
1555 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1556 if (!res) {
1557 dev_err(&pdev->dev, "failed to find the registers\n");
1558 ret = -ENOENT;
1559 goto err_info;
1560 }
1561
1562 fimc->regs_res = request_mem_region(res->start, resource_size(res),
1563 dev_name(&pdev->dev));
1564 if (!fimc->regs_res) {
1565 dev_err(&pdev->dev, "failed to obtain register region\n");
1566 ret = -ENOENT;
1567 goto err_info;
1568 }
1569
1570 fimc->regs = ioremap(res->start, resource_size(res));
1571 if (!fimc->regs) {
1572 dev_err(&pdev->dev, "failed to map registers\n");
1573 ret = -ENXIO;
1574 goto err_req_region;
1575 }
1576
1577 fimc->num_clocks = MAX_FIMC_CLOCKS - 1;
1578 /*
1579 * Check if vide capture node needs to be registered for this device
1580 * instance.
1581 */
1582 if (fimc->pdata) {
1583 int i;
1584 for (i = 0; i < FIMC_MAX_CAMIF_CLIENTS; ++i)
1585 if (fimc->pdata->isp_info[i])
1586 break;
1587 if (i < FIMC_MAX_CAMIF_CLIENTS) {
1588 cap_input_index = i;
1589 fimc->num_clocks++;
1590 }
1591 }
1592
1593 ret = fimc_clk_get(fimc);
1594 if (ret)
1595 goto err_regs_unmap;
1596 clk_set_rate(fimc->clock[CLK_BUS], drv_data->lclk_frequency);
1597
1598 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1599 if (!res) {
1600 dev_err(&pdev->dev, "failed to get IRQ resource\n");
1601 ret = -ENXIO;
1602 goto err_clk;
1603 }
1604 fimc->irq = res->start;
1605
1606 fimc_hw_reset(fimc);
1607
1608 ret = request_irq(fimc->irq, fimc_isr, 0, pdev->name, fimc);
1609 if (ret) {
1610 dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
1611 goto err_clk;
1612 }
1613
1614 /* Initialize contiguous memory allocator */
1615 fimc->alloc_ctx = vb2_dma_contig_init_ctx(&fimc->pdev->dev);
1616 if (IS_ERR(fimc->alloc_ctx)) {
1617 ret = PTR_ERR(fimc->alloc_ctx);
1618 goto err_irq;
1619 }
1620
1621 ret = fimc_register_m2m_device(fimc);
1622 if (ret)
1623 goto err_irq;
1624
1625 /* At least one camera sensor is required to register capture node */
1626 if (cap_input_index >= 0) {
1627 ret = fimc_register_capture_device(fimc);
1628 if (ret)
1629 goto err_m2m;
1630 clk_disable(fimc->clock[CLK_CAM]);
1631 }
1632 /*
1633 * Exclude the additional output DMA address registers by masking
1634 * them out on HW revisions that provide extended capabilites.
1635 */
1636 if (fimc->variant->out_buf_count > 4)
1637 fimc_hw_set_dma_seq(fimc, 0xF);
1638
1639 dev_dbg(&pdev->dev, "%s(): fimc-%d registered successfully\n",
1640 __func__, fimc->id);
1641
1642 return 0;
1643
1644 err_m2m:
1645 fimc_unregister_m2m_device(fimc);
1646 err_irq:
1647 free_irq(fimc->irq, fimc);
1648 err_clk:
1649 fimc_clk_release(fimc);
1650 err_regs_unmap:
1651 iounmap(fimc->regs);
1652 err_req_region:
1653 release_resource(fimc->regs_res);
1654 kfree(fimc->regs_res);
1655 err_info:
1656 kfree(fimc);
1657
1658 return ret;
1659 }
1660
1661 static int __devexit fimc_remove(struct platform_device *pdev)
1662 {
1663 struct fimc_dev *fimc =
1664 (struct fimc_dev *)platform_get_drvdata(pdev);
1665
1666 free_irq(fimc->irq, fimc);
1667 fimc_hw_reset(fimc);
1668
1669 fimc_unregister_m2m_device(fimc);
1670 fimc_unregister_capture_device(fimc);
1671
1672 fimc_clk_release(fimc);
1673
1674 vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
1675
1676 iounmap(fimc->regs);
1677 release_resource(fimc->regs_res);
1678 kfree(fimc->regs_res);
1679 kfree(fimc);
1680
1681 dev_info(&pdev->dev, "%s driver unloaded\n", pdev->name);
1682 return 0;
1683 }
1684
1685 /* Image pixel limits, similar across several FIMC HW revisions. */
1686 static struct fimc_pix_limit s5p_pix_limit[3] = {
1687 [0] = {
1688 .scaler_en_w = 3264,
1689 .scaler_dis_w = 8192,
1690 .in_rot_en_h = 1920,
1691 .in_rot_dis_w = 8192,
1692 .out_rot_en_w = 1920,
1693 .out_rot_dis_w = 4224,
1694 },
1695 [1] = {
1696 .scaler_en_w = 4224,
1697 .scaler_dis_w = 8192,
1698 .in_rot_en_h = 1920,
1699 .in_rot_dis_w = 8192,
1700 .out_rot_en_w = 1920,
1701 .out_rot_dis_w = 4224,
1702 },
1703 [2] = {
1704 .scaler_en_w = 1920,
1705 .scaler_dis_w = 8192,
1706 .in_rot_en_h = 1280,
1707 .in_rot_dis_w = 8192,
1708 .out_rot_en_w = 1280,
1709 .out_rot_dis_w = 1920,
1710 },
1711 };
1712
1713 static struct samsung_fimc_variant fimc0_variant_s5p = {
1714 .has_inp_rot = 1,
1715 .has_out_rot = 1,
1716 .min_inp_pixsize = 16,
1717 .min_out_pixsize = 16,
1718 .hor_offs_align = 8,
1719 .out_buf_count = 4,
1720 .pix_limit = &s5p_pix_limit[0],
1721 };
1722
1723 static struct samsung_fimc_variant fimc2_variant_s5p = {
1724 .min_inp_pixsize = 16,
1725 .min_out_pixsize = 16,
1726 .hor_offs_align = 8,
1727 .out_buf_count = 4,
1728 .pix_limit = &s5p_pix_limit[1],
1729 };
1730
1731 static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
1732 .pix_hoff = 1,
1733 .has_inp_rot = 1,
1734 .has_out_rot = 1,
1735 .min_inp_pixsize = 16,
1736 .min_out_pixsize = 16,
1737 .hor_offs_align = 8,
1738 .out_buf_count = 4,
1739 .pix_limit = &s5p_pix_limit[1],
1740 };
1741
1742 static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
1743 .pix_hoff = 1,
1744 .has_inp_rot = 1,
1745 .has_out_rot = 1,
1746 .has_mainscaler_ext = 1,
1747 .min_inp_pixsize = 16,
1748 .min_out_pixsize = 16,
1749 .hor_offs_align = 1,
1750 .out_buf_count = 4,
1751 .pix_limit = &s5p_pix_limit[2],
1752 };
1753
1754 static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
1755 .pix_hoff = 1,
1756 .min_inp_pixsize = 16,
1757 .min_out_pixsize = 16,
1758 .hor_offs_align = 8,
1759 .out_buf_count = 4,
1760 .pix_limit = &s5p_pix_limit[2],
1761 };
1762
1763 static struct samsung_fimc_variant fimc0_variant_s5pv310 = {
1764 .pix_hoff = 1,
1765 .has_inp_rot = 1,
1766 .has_out_rot = 1,
1767 .has_cistatus2 = 1,
1768 .has_mainscaler_ext = 1,
1769 .min_inp_pixsize = 16,
1770 .min_out_pixsize = 16,
1771 .hor_offs_align = 1,
1772 .out_buf_count = 32,
1773 .pix_limit = &s5p_pix_limit[1],
1774 };
1775
1776 static struct samsung_fimc_variant fimc2_variant_s5pv310 = {
1777 .pix_hoff = 1,
1778 .has_cistatus2 = 1,
1779 .has_mainscaler_ext = 1,
1780 .min_inp_pixsize = 16,
1781 .min_out_pixsize = 16,
1782 .hor_offs_align = 1,
1783 .out_buf_count = 32,
1784 .pix_limit = &s5p_pix_limit[2],
1785 };
1786
1787 /* S5PC100 */
1788 static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
1789 .variant = {
1790 [0] = &fimc0_variant_s5p,
1791 [1] = &fimc0_variant_s5p,
1792 [2] = &fimc2_variant_s5p,
1793 },
1794 .num_entities = 3,
1795 .lclk_frequency = 133000000UL,
1796 };
1797
1798 /* S5PV210, S5PC110 */
1799 static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
1800 .variant = {
1801 [0] = &fimc0_variant_s5pv210,
1802 [1] = &fimc1_variant_s5pv210,
1803 [2] = &fimc2_variant_s5pv210,
1804 },
1805 .num_entities = 3,
1806 .lclk_frequency = 166000000UL,
1807 };
1808
1809 /* S5PV310, S5PC210 */
1810 static struct samsung_fimc_driverdata fimc_drvdata_s5pv310 = {
1811 .variant = {
1812 [0] = &fimc0_variant_s5pv310,
1813 [1] = &fimc0_variant_s5pv310,
1814 [2] = &fimc0_variant_s5pv310,
1815 [3] = &fimc2_variant_s5pv310,
1816 },
1817 .num_entities = 4,
1818 .lclk_frequency = 166000000UL,
1819 };
1820
1821 static struct platform_device_id fimc_driver_ids[] = {
1822 {
1823 .name = "s5p-fimc",
1824 .driver_data = (unsigned long)&fimc_drvdata_s5p,
1825 }, {
1826 .name = "s5pv210-fimc",
1827 .driver_data = (unsigned long)&fimc_drvdata_s5pv210,
1828 }, {
1829 .name = "s5pv310-fimc",
1830 .driver_data = (unsigned long)&fimc_drvdata_s5pv310,
1831 },
1832 {},
1833 };
1834 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1835
1836 static struct platform_driver fimc_driver = {
1837 .probe = fimc_probe,
1838 .remove = __devexit_p(fimc_remove),
1839 .id_table = fimc_driver_ids,
1840 .driver = {
1841 .name = MODULE_NAME,
1842 .owner = THIS_MODULE,
1843 }
1844 };
1845
1846 static int __init fimc_init(void)
1847 {
1848 int ret = platform_driver_register(&fimc_driver);
1849 if (ret)
1850 err("platform_driver_register failed: %d\n", ret);
1851 return ret;
1852 }
1853
1854 static void __exit fimc_exit(void)
1855 {
1856 platform_driver_unregister(&fimc_driver);
1857 }
1858
1859 module_init(fimc_init);
1860 module_exit(fimc_exit);
1861
1862 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1863 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1864 MODULE_LICENSE("GPL");