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