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