]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/exynos/exynos_drm_fimc.c
drm/exynos/fimc: replace hw access macros with functions
[karo-tx-linux.git] / drivers / gpu / drm / exynos / exynos_drm_fimc.c
1 /*
2  * Copyright (C) 2012 Samsung Electronics Co.Ltd
3  * Authors:
4  *      Eunchul Kim <chulspro.kim@samsung.com>
5  *      Jinyoung Jeon <jy0.jeon@samsung.com>
6  *      Sangmin Lee <lsmin.lee@samsung.com>
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  *
13  */
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/regmap.h>
18 #include <linux/clk.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/of.h>
21
22 #include <drm/drmP.h>
23 #include <drm/exynos_drm.h>
24 #include "regs-fimc.h"
25 #include "exynos_drm_drv.h"
26 #include "exynos_drm_ipp.h"
27 #include "exynos_drm_fimc.h"
28
29 /*
30  * FIMC stands for Fully Interactive Mobile Camera and
31  * supports image scaler/rotator and input/output DMA operations.
32  * input DMA reads image data from the memory.
33  * output DMA writes image data to memory.
34  * FIMC supports image rotation and image effect functions.
35  *
36  * M2M operation : supports crop/scale/rotation/csc so on.
37  * Memory ----> FIMC H/W ----> Memory.
38  * Writeback operation : supports cloned screen with FIMD.
39  * FIMD ----> FIMC H/W ----> Memory.
40  * Output operation : supports direct display using local path.
41  * Memory ----> FIMC H/W ----> FIMD.
42  */
43
44 /*
45  * TODO
46  * 1. check suspend/resume api if needed.
47  * 2. need to check use case platform_device_id.
48  * 3. check src/dst size with, height.
49  * 4. added check_prepare api for right register.
50  * 5. need to add supported list in prop_list.
51  * 6. check prescaler/scaler optimization.
52  */
53
54 #define FIMC_MAX_DEVS   4
55 #define FIMC_MAX_SRC    2
56 #define FIMC_MAX_DST    32
57 #define FIMC_SHFACTOR   10
58 #define FIMC_BUF_STOP   1
59 #define FIMC_BUF_START  2
60 #define FIMC_REG_SZ             32
61 #define FIMC_WIDTH_ITU_709      1280
62 #define FIMC_REFRESH_MAX        60
63 #define FIMC_REFRESH_MIN        12
64 #define FIMC_CROP_MAX   8192
65 #define FIMC_CROP_MIN   32
66 #define FIMC_SCALE_MAX  4224
67 #define FIMC_SCALE_MIN  32
68
69 #define get_fimc_context(dev)   platform_get_drvdata(to_platform_device(dev))
70 #define get_ctx_from_ippdrv(ippdrv)     container_of(ippdrv,\
71                                         struct fimc_context, ippdrv);
72 enum fimc_wb {
73         FIMC_WB_NONE,
74         FIMC_WB_A,
75         FIMC_WB_B,
76 };
77
78 enum {
79         FIMC_CLK_LCLK,
80         FIMC_CLK_GATE,
81         FIMC_CLK_WB_A,
82         FIMC_CLK_WB_B,
83         FIMC_CLK_MUX,
84         FIMC_CLK_PARENT,
85         FIMC_CLKS_MAX
86 };
87
88 static const char * const fimc_clock_names[] = {
89         [FIMC_CLK_LCLK]   = "sclk_fimc",
90         [FIMC_CLK_GATE]   = "fimc",
91         [FIMC_CLK_WB_A]   = "pxl_async0",
92         [FIMC_CLK_WB_B]   = "pxl_async1",
93         [FIMC_CLK_MUX]    = "mux",
94         [FIMC_CLK_PARENT] = "parent",
95 };
96
97 #define FIMC_DEFAULT_LCLK_FREQUENCY 133000000UL
98
99 /*
100  * A structure of scaler.
101  *
102  * @range: narrow, wide.
103  * @bypass: unused scaler path.
104  * @up_h: horizontal scale up.
105  * @up_v: vertical scale up.
106  * @hratio: horizontal ratio.
107  * @vratio: vertical ratio.
108  */
109 struct fimc_scaler {
110         bool    range;
111         bool bypass;
112         bool up_h;
113         bool up_v;
114         u32 hratio;
115         u32 vratio;
116 };
117
118 /*
119  * A structure of scaler capability.
120  *
121  * find user manual table 43-1.
122  * @in_hori: scaler input horizontal size.
123  * @bypass: scaler bypass mode.
124  * @dst_h_wo_rot: target horizontal size without output rotation.
125  * @dst_h_rot: target horizontal size with output rotation.
126  * @rl_w_wo_rot: real width without input rotation.
127  * @rl_h_rot: real height without output rotation.
128  */
129 struct fimc_capability {
130         /* scaler */
131         u32     in_hori;
132         u32     bypass;
133         /* output rotator */
134         u32     dst_h_wo_rot;
135         u32     dst_h_rot;
136         /* input rotator */
137         u32     rl_w_wo_rot;
138         u32     rl_h_rot;
139 };
140
141 /*
142  * A structure of fimc context.
143  *
144  * @ippdrv: prepare initialization using ippdrv.
145  * @regs_res: register resources.
146  * @regs: memory mapped io registers.
147  * @lock: locking of operations.
148  * @clocks: fimc clocks.
149  * @clk_frequency: LCLK clock frequency.
150  * @sysreg: handle to SYSREG block regmap.
151  * @sc: scaler infomations.
152  * @pol: porarity of writeback.
153  * @id: fimc id.
154  * @irq: irq number.
155  * @suspended: qos operations.
156  */
157 struct fimc_context {
158         struct exynos_drm_ippdrv        ippdrv;
159         struct resource *regs_res;
160         void __iomem    *regs;
161         struct mutex    lock;
162         struct clk      *clocks[FIMC_CLKS_MAX];
163         u32             clk_frequency;
164         struct regmap   *sysreg;
165         struct fimc_scaler      sc;
166         struct exynos_drm_ipp_pol       pol;
167         int     id;
168         int     irq;
169         bool    suspended;
170 };
171
172 static u32 fimc_read(struct fimc_context *ctx, u32 reg)
173 {
174         return readl(ctx->regs + reg);
175 }
176
177 static void fimc_write(struct fimc_context *ctx, u32 val, u32 reg)
178 {
179         writel(val, ctx->regs + reg);
180 }
181
182 static void fimc_set_bits(struct fimc_context *ctx, u32 reg, u32 bits)
183 {
184         void __iomem *r = ctx->regs + reg;
185
186         writel(readl(r) | bits, r);
187 }
188
189 static void fimc_clear_bits(struct fimc_context *ctx, u32 reg, u32 bits)
190 {
191         void __iomem *r = ctx->regs + reg;
192
193         writel(readl(r) & ~bits, r);
194 }
195
196 static void fimc_sw_reset(struct fimc_context *ctx)
197 {
198         u32 cfg;
199
200         /* stop dma operation */
201         cfg = fimc_read(ctx, EXYNOS_CISTATUS);
202         if (EXYNOS_CISTATUS_GET_ENVID_STATUS(cfg))
203                 fimc_clear_bits(ctx, EXYNOS_MSCTRL, EXYNOS_MSCTRL_ENVID);
204
205         fimc_set_bits(ctx, EXYNOS_CISRCFMT, EXYNOS_CISRCFMT_ITU601_8BIT);
206
207         /* disable image capture */
208         fimc_clear_bits(ctx, EXYNOS_CIIMGCPT,
209                 EXYNOS_CIIMGCPT_IMGCPTEN_SC | EXYNOS_CIIMGCPT_IMGCPTEN);
210
211         /* s/w reset */
212         fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_SWRST);
213
214         /* s/w reset complete */
215         fimc_clear_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_SWRST);
216
217         /* reset sequence */
218         fimc_write(ctx, 0x0, EXYNOS_CIFCNTSEQ);
219 }
220
221 static int fimc_set_camblk_fimd0_wb(struct fimc_context *ctx)
222 {
223         return regmap_update_bits(ctx->sysreg, SYSREG_CAMERA_BLK,
224                                   SYSREG_FIMD0WB_DEST_MASK,
225                                   ctx->id << SYSREG_FIMD0WB_DEST_SHIFT);
226 }
227
228 static void fimc_set_type_ctrl(struct fimc_context *ctx, enum fimc_wb wb)
229 {
230         u32 cfg;
231
232         DRM_DEBUG_KMS("wb[%d]\n", wb);
233
234         cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
235         cfg &= ~(EXYNOS_CIGCTRL_TESTPATTERN_MASK |
236                 EXYNOS_CIGCTRL_SELCAM_ITU_MASK |
237                 EXYNOS_CIGCTRL_SELCAM_MIPI_MASK |
238                 EXYNOS_CIGCTRL_SELCAM_FIMC_MASK |
239                 EXYNOS_CIGCTRL_SELWB_CAMIF_MASK |
240                 EXYNOS_CIGCTRL_SELWRITEBACK_MASK);
241
242         switch (wb) {
243         case FIMC_WB_A:
244                 cfg |= (EXYNOS_CIGCTRL_SELWRITEBACK_A |
245                         EXYNOS_CIGCTRL_SELWB_CAMIF_WRITEBACK);
246                 break;
247         case FIMC_WB_B:
248                 cfg |= (EXYNOS_CIGCTRL_SELWRITEBACK_B |
249                         EXYNOS_CIGCTRL_SELWB_CAMIF_WRITEBACK);
250                 break;
251         case FIMC_WB_NONE:
252         default:
253                 cfg |= (EXYNOS_CIGCTRL_SELCAM_ITU_A |
254                         EXYNOS_CIGCTRL_SELWRITEBACK_A |
255                         EXYNOS_CIGCTRL_SELCAM_MIPI_A |
256                         EXYNOS_CIGCTRL_SELCAM_FIMC_ITU);
257                 break;
258         }
259
260         fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
261 }
262
263 static void fimc_set_polarity(struct fimc_context *ctx,
264                 struct exynos_drm_ipp_pol *pol)
265 {
266         u32 cfg;
267
268         DRM_DEBUG_KMS("inv_pclk[%d]inv_vsync[%d]\n",
269                 pol->inv_pclk, pol->inv_vsync);
270         DRM_DEBUG_KMS("inv_href[%d]inv_hsync[%d]\n",
271                 pol->inv_href, pol->inv_hsync);
272
273         cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
274         cfg &= ~(EXYNOS_CIGCTRL_INVPOLPCLK | EXYNOS_CIGCTRL_INVPOLVSYNC |
275                  EXYNOS_CIGCTRL_INVPOLHREF | EXYNOS_CIGCTRL_INVPOLHSYNC);
276
277         if (pol->inv_pclk)
278                 cfg |= EXYNOS_CIGCTRL_INVPOLPCLK;
279         if (pol->inv_vsync)
280                 cfg |= EXYNOS_CIGCTRL_INVPOLVSYNC;
281         if (pol->inv_href)
282                 cfg |= EXYNOS_CIGCTRL_INVPOLHREF;
283         if (pol->inv_hsync)
284                 cfg |= EXYNOS_CIGCTRL_INVPOLHSYNC;
285
286         fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
287 }
288
289 static void fimc_handle_jpeg(struct fimc_context *ctx, bool enable)
290 {
291         u32 cfg;
292
293         DRM_DEBUG_KMS("enable[%d]\n", enable);
294
295         cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
296         if (enable)
297                 cfg |= EXYNOS_CIGCTRL_CAM_JPEG;
298         else
299                 cfg &= ~EXYNOS_CIGCTRL_CAM_JPEG;
300
301         fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
302 }
303
304 static void fimc_mask_irq(struct fimc_context *ctx, bool enable)
305 {
306         u32 cfg;
307
308         DRM_DEBUG_KMS("enable[%d]\n", enable);
309
310         cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
311         if (enable) {
312                 cfg &= ~EXYNOS_CIGCTRL_IRQ_OVFEN;
313                 cfg |= EXYNOS_CIGCTRL_IRQ_ENABLE | EXYNOS_CIGCTRL_IRQ_LEVEL;
314         } else
315                 cfg &= ~EXYNOS_CIGCTRL_IRQ_ENABLE;
316         fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
317 }
318
319 static void fimc_clear_irq(struct fimc_context *ctx)
320 {
321         fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_CLR);
322 }
323
324 static bool fimc_check_ovf(struct fimc_context *ctx)
325 {
326         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
327         u32 status, flag;
328
329         status = fimc_read(ctx, EXYNOS_CISTATUS);
330         flag = EXYNOS_CISTATUS_OVFIY | EXYNOS_CISTATUS_OVFICB |
331                 EXYNOS_CISTATUS_OVFICR;
332
333         DRM_DEBUG_KMS("flag[0x%x]\n", flag);
334
335         if (status & flag) {
336                 fimc_set_bits(ctx, EXYNOS_CIWDOFST,
337                         EXYNOS_CIWDOFST_CLROVFIY | EXYNOS_CIWDOFST_CLROVFICB |
338                         EXYNOS_CIWDOFST_CLROVFICR);
339                 fimc_clear_bits(ctx, EXYNOS_CIWDOFST,
340                         EXYNOS_CIWDOFST_CLROVFIY | EXYNOS_CIWDOFST_CLROVFICB |
341                         EXYNOS_CIWDOFST_CLROVFICR);
342
343                 dev_err(ippdrv->dev, "occurred overflow at %d, status 0x%x.\n",
344                         ctx->id, status);
345                 return true;
346         }
347
348         return false;
349 }
350
351 static bool fimc_check_frame_end(struct fimc_context *ctx)
352 {
353         u32 cfg;
354
355         cfg = fimc_read(ctx, EXYNOS_CISTATUS);
356
357         DRM_DEBUG_KMS("cfg[0x%x]\n", cfg);
358
359         if (!(cfg & EXYNOS_CISTATUS_FRAMEEND))
360                 return false;
361
362         cfg &= ~(EXYNOS_CISTATUS_FRAMEEND);
363         fimc_write(ctx, cfg, EXYNOS_CISTATUS);
364
365         return true;
366 }
367
368 static int fimc_get_buf_id(struct fimc_context *ctx)
369 {
370         u32 cfg;
371         int frame_cnt, buf_id;
372
373         cfg = fimc_read(ctx, EXYNOS_CISTATUS2);
374         frame_cnt = EXYNOS_CISTATUS2_GET_FRAMECOUNT_BEFORE(cfg);
375
376         if (frame_cnt == 0)
377                 frame_cnt = EXYNOS_CISTATUS2_GET_FRAMECOUNT_PRESENT(cfg);
378
379         DRM_DEBUG_KMS("present[%d]before[%d]\n",
380                 EXYNOS_CISTATUS2_GET_FRAMECOUNT_PRESENT(cfg),
381                 EXYNOS_CISTATUS2_GET_FRAMECOUNT_BEFORE(cfg));
382
383         if (frame_cnt == 0) {
384                 DRM_ERROR("failed to get frame count.\n");
385                 return -EIO;
386         }
387
388         buf_id = frame_cnt - 1;
389         DRM_DEBUG_KMS("buf_id[%d]\n", buf_id);
390
391         return buf_id;
392 }
393
394 static void fimc_handle_lastend(struct fimc_context *ctx, bool enable)
395 {
396         u32 cfg;
397
398         DRM_DEBUG_KMS("enable[%d]\n", enable);
399
400         cfg = fimc_read(ctx, EXYNOS_CIOCTRL);
401         if (enable)
402                 cfg |= EXYNOS_CIOCTRL_LASTENDEN;
403         else
404                 cfg &= ~EXYNOS_CIOCTRL_LASTENDEN;
405
406         fimc_write(ctx, cfg, EXYNOS_CIOCTRL);
407 }
408
409
410 static int fimc_src_set_fmt_order(struct fimc_context *ctx, u32 fmt)
411 {
412         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
413         u32 cfg;
414
415         DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
416
417         /* RGB */
418         cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
419         cfg &= ~EXYNOS_CISCCTRL_INRGB_FMT_RGB_MASK;
420
421         switch (fmt) {
422         case DRM_FORMAT_RGB565:
423                 cfg |= EXYNOS_CISCCTRL_INRGB_FMT_RGB565;
424                 fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
425                 return 0;
426         case DRM_FORMAT_RGB888:
427         case DRM_FORMAT_XRGB8888:
428                 cfg |= EXYNOS_CISCCTRL_INRGB_FMT_RGB888;
429                 fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
430                 return 0;
431         default:
432                 /* bypass */
433                 break;
434         }
435
436         /* YUV */
437         cfg = fimc_read(ctx, EXYNOS_MSCTRL);
438         cfg &= ~(EXYNOS_MSCTRL_ORDER2P_SHIFT_MASK |
439                 EXYNOS_MSCTRL_C_INT_IN_2PLANE |
440                 EXYNOS_MSCTRL_ORDER422_YCBYCR);
441
442         switch (fmt) {
443         case DRM_FORMAT_YUYV:
444                 cfg |= EXYNOS_MSCTRL_ORDER422_YCBYCR;
445                 break;
446         case DRM_FORMAT_YVYU:
447                 cfg |= EXYNOS_MSCTRL_ORDER422_YCRYCB;
448                 break;
449         case DRM_FORMAT_UYVY:
450                 cfg |= EXYNOS_MSCTRL_ORDER422_CBYCRY;
451                 break;
452         case DRM_FORMAT_VYUY:
453         case DRM_FORMAT_YUV444:
454                 cfg |= EXYNOS_MSCTRL_ORDER422_CRYCBY;
455                 break;
456         case DRM_FORMAT_NV21:
457         case DRM_FORMAT_NV61:
458                 cfg |= (EXYNOS_MSCTRL_ORDER2P_LSB_CRCB |
459                         EXYNOS_MSCTRL_C_INT_IN_2PLANE);
460                 break;
461         case DRM_FORMAT_YUV422:
462         case DRM_FORMAT_YUV420:
463         case DRM_FORMAT_YVU420:
464                 cfg |= EXYNOS_MSCTRL_C_INT_IN_3PLANE;
465                 break;
466         case DRM_FORMAT_NV12:
467         case DRM_FORMAT_NV12MT:
468         case DRM_FORMAT_NV16:
469                 cfg |= (EXYNOS_MSCTRL_ORDER2P_LSB_CBCR |
470                         EXYNOS_MSCTRL_C_INT_IN_2PLANE);
471                 break;
472         default:
473                 dev_err(ippdrv->dev, "inavlid source yuv order 0x%x.\n", fmt);
474                 return -EINVAL;
475         }
476
477         fimc_write(ctx, cfg, EXYNOS_MSCTRL);
478
479         return 0;
480 }
481
482 static int fimc_src_set_fmt(struct device *dev, u32 fmt)
483 {
484         struct fimc_context *ctx = get_fimc_context(dev);
485         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
486         u32 cfg;
487
488         DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
489
490         cfg = fimc_read(ctx, EXYNOS_MSCTRL);
491         cfg &= ~EXYNOS_MSCTRL_INFORMAT_RGB;
492
493         switch (fmt) {
494         case DRM_FORMAT_RGB565:
495         case DRM_FORMAT_RGB888:
496         case DRM_FORMAT_XRGB8888:
497                 cfg |= EXYNOS_MSCTRL_INFORMAT_RGB;
498                 break;
499         case DRM_FORMAT_YUV444:
500                 cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR420;
501                 break;
502         case DRM_FORMAT_YUYV:
503         case DRM_FORMAT_YVYU:
504         case DRM_FORMAT_UYVY:
505         case DRM_FORMAT_VYUY:
506                 cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR422_1PLANE;
507                 break;
508         case DRM_FORMAT_NV16:
509         case DRM_FORMAT_NV61:
510         case DRM_FORMAT_YUV422:
511                 cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR422;
512                 break;
513         case DRM_FORMAT_YUV420:
514         case DRM_FORMAT_YVU420:
515         case DRM_FORMAT_NV12:
516         case DRM_FORMAT_NV21:
517         case DRM_FORMAT_NV12MT:
518                 cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR420;
519                 break;
520         default:
521                 dev_err(ippdrv->dev, "inavlid source format 0x%x.\n", fmt);
522                 return -EINVAL;
523         }
524
525         fimc_write(ctx, cfg, EXYNOS_MSCTRL);
526
527         cfg = fimc_read(ctx, EXYNOS_CIDMAPARAM);
528         cfg &= ~EXYNOS_CIDMAPARAM_R_MODE_MASK;
529
530         if (fmt == DRM_FORMAT_NV12MT)
531                 cfg |= EXYNOS_CIDMAPARAM_R_MODE_64X32;
532         else
533                 cfg |= EXYNOS_CIDMAPARAM_R_MODE_LINEAR;
534
535         fimc_write(ctx, cfg, EXYNOS_CIDMAPARAM);
536
537         return fimc_src_set_fmt_order(ctx, fmt);
538 }
539
540 static int fimc_src_set_transf(struct device *dev,
541                 enum drm_exynos_degree degree,
542                 enum drm_exynos_flip flip, bool *swap)
543 {
544         struct fimc_context *ctx = get_fimc_context(dev);
545         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
546         u32 cfg1, cfg2;
547
548         DRM_DEBUG_KMS("degree[%d]flip[0x%x]\n", degree, flip);
549
550         cfg1 = fimc_read(ctx, EXYNOS_MSCTRL);
551         cfg1 &= ~(EXYNOS_MSCTRL_FLIP_X_MIRROR |
552                 EXYNOS_MSCTRL_FLIP_Y_MIRROR);
553
554         cfg2 = fimc_read(ctx, EXYNOS_CITRGFMT);
555         cfg2 &= ~EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
556
557         switch (degree) {
558         case EXYNOS_DRM_DEGREE_0:
559                 if (flip & EXYNOS_DRM_FLIP_VERTICAL)
560                         cfg1 |= EXYNOS_MSCTRL_FLIP_X_MIRROR;
561                 if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
562                         cfg1 |= EXYNOS_MSCTRL_FLIP_Y_MIRROR;
563                 break;
564         case EXYNOS_DRM_DEGREE_90:
565                 cfg2 |= EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
566                 if (flip & EXYNOS_DRM_FLIP_VERTICAL)
567                         cfg1 |= EXYNOS_MSCTRL_FLIP_X_MIRROR;
568                 if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
569                         cfg1 |= EXYNOS_MSCTRL_FLIP_Y_MIRROR;
570                 break;
571         case EXYNOS_DRM_DEGREE_180:
572                 cfg1 |= (EXYNOS_MSCTRL_FLIP_X_MIRROR |
573                         EXYNOS_MSCTRL_FLIP_Y_MIRROR);
574                 if (flip & EXYNOS_DRM_FLIP_VERTICAL)
575                         cfg1 &= ~EXYNOS_MSCTRL_FLIP_X_MIRROR;
576                 if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
577                         cfg1 &= ~EXYNOS_MSCTRL_FLIP_Y_MIRROR;
578                 break;
579         case EXYNOS_DRM_DEGREE_270:
580                 cfg1 |= (EXYNOS_MSCTRL_FLIP_X_MIRROR |
581                         EXYNOS_MSCTRL_FLIP_Y_MIRROR);
582                 cfg2 |= EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
583                 if (flip & EXYNOS_DRM_FLIP_VERTICAL)
584                         cfg1 &= ~EXYNOS_MSCTRL_FLIP_X_MIRROR;
585                 if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
586                         cfg1 &= ~EXYNOS_MSCTRL_FLIP_Y_MIRROR;
587                 break;
588         default:
589                 dev_err(ippdrv->dev, "inavlid degree value %d.\n", degree);
590                 return -EINVAL;
591         }
592
593         fimc_write(ctx, cfg1, EXYNOS_MSCTRL);
594         fimc_write(ctx, cfg2, EXYNOS_CITRGFMT);
595         *swap = (cfg2 & EXYNOS_CITRGFMT_INROT90_CLOCKWISE) ? 1 : 0;
596
597         return 0;
598 }
599
600 static int fimc_set_window(struct fimc_context *ctx,
601                 struct drm_exynos_pos *pos, struct drm_exynos_sz *sz)
602 {
603         u32 cfg, h1, h2, v1, v2;
604
605         /* cropped image */
606         h1 = pos->x;
607         h2 = sz->hsize - pos->w - pos->x;
608         v1 = pos->y;
609         v2 = sz->vsize - pos->h - pos->y;
610
611         DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]hsize[%d]vsize[%d]\n",
612                 pos->x, pos->y, pos->w, pos->h, sz->hsize, sz->vsize);
613         DRM_DEBUG_KMS("h1[%d]h2[%d]v1[%d]v2[%d]\n", h1, h2, v1, v2);
614
615         /*
616          * set window offset 1, 2 size
617          * check figure 43-21 in user manual
618          */
619         cfg = fimc_read(ctx, EXYNOS_CIWDOFST);
620         cfg &= ~(EXYNOS_CIWDOFST_WINHOROFST_MASK |
621                 EXYNOS_CIWDOFST_WINVEROFST_MASK);
622         cfg |= (EXYNOS_CIWDOFST_WINHOROFST(h1) |
623                 EXYNOS_CIWDOFST_WINVEROFST(v1));
624         cfg |= EXYNOS_CIWDOFST_WINOFSEN;
625         fimc_write(ctx, cfg, EXYNOS_CIWDOFST);
626
627         cfg = (EXYNOS_CIWDOFST2_WINHOROFST2(h2) |
628                 EXYNOS_CIWDOFST2_WINVEROFST2(v2));
629         fimc_write(ctx, cfg, EXYNOS_CIWDOFST2);
630
631         return 0;
632 }
633
634 static int fimc_src_set_size(struct device *dev, int swap,
635                 struct drm_exynos_pos *pos, struct drm_exynos_sz *sz)
636 {
637         struct fimc_context *ctx = get_fimc_context(dev);
638         struct drm_exynos_pos img_pos = *pos;
639         struct drm_exynos_sz img_sz = *sz;
640         u32 cfg;
641
642         DRM_DEBUG_KMS("swap[%d]hsize[%d]vsize[%d]\n",
643                 swap, sz->hsize, sz->vsize);
644
645         /* original size */
646         cfg = (EXYNOS_ORGISIZE_HORIZONTAL(img_sz.hsize) |
647                 EXYNOS_ORGISIZE_VERTICAL(img_sz.vsize));
648
649         fimc_write(ctx, cfg, EXYNOS_ORGISIZE);
650
651         DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]\n", pos->x, pos->y, pos->w, pos->h);
652
653         if (swap) {
654                 img_pos.w = pos->h;
655                 img_pos.h = pos->w;
656                 img_sz.hsize = sz->vsize;
657                 img_sz.vsize = sz->hsize;
658         }
659
660         /* set input DMA image size */
661         cfg = fimc_read(ctx, EXYNOS_CIREAL_ISIZE);
662         cfg &= ~(EXYNOS_CIREAL_ISIZE_HEIGHT_MASK |
663                 EXYNOS_CIREAL_ISIZE_WIDTH_MASK);
664         cfg |= (EXYNOS_CIREAL_ISIZE_WIDTH(img_pos.w) |
665                 EXYNOS_CIREAL_ISIZE_HEIGHT(img_pos.h));
666         fimc_write(ctx, cfg, EXYNOS_CIREAL_ISIZE);
667
668         /*
669          * set input FIFO image size
670          * for now, we support only ITU601 8 bit mode
671          */
672         cfg = (EXYNOS_CISRCFMT_ITU601_8BIT |
673                 EXYNOS_CISRCFMT_SOURCEHSIZE(img_sz.hsize) |
674                 EXYNOS_CISRCFMT_SOURCEVSIZE(img_sz.vsize));
675         fimc_write(ctx, cfg, EXYNOS_CISRCFMT);
676
677         /* offset Y(RGB), Cb, Cr */
678         cfg = (EXYNOS_CIIYOFF_HORIZONTAL(img_pos.x) |
679                 EXYNOS_CIIYOFF_VERTICAL(img_pos.y));
680         fimc_write(ctx, cfg, EXYNOS_CIIYOFF);
681         cfg = (EXYNOS_CIICBOFF_HORIZONTAL(img_pos.x) |
682                 EXYNOS_CIICBOFF_VERTICAL(img_pos.y));
683         fimc_write(ctx, cfg, EXYNOS_CIICBOFF);
684         cfg = (EXYNOS_CIICROFF_HORIZONTAL(img_pos.x) |
685                 EXYNOS_CIICROFF_VERTICAL(img_pos.y));
686         fimc_write(ctx, cfg, EXYNOS_CIICROFF);
687
688         return fimc_set_window(ctx, &img_pos, &img_sz);
689 }
690
691 static int fimc_src_set_addr(struct device *dev,
692                 struct drm_exynos_ipp_buf_info *buf_info, u32 buf_id,
693                 enum drm_exynos_ipp_buf_type buf_type)
694 {
695         struct fimc_context *ctx = get_fimc_context(dev);
696         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
697         struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
698         struct drm_exynos_ipp_property *property;
699         struct drm_exynos_ipp_config *config;
700
701         if (!c_node) {
702                 DRM_ERROR("failed to get c_node.\n");
703                 return -EINVAL;
704         }
705
706         property = &c_node->property;
707
708         DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]buf_type[%d]\n",
709                 property->prop_id, buf_id, buf_type);
710
711         if (buf_id > FIMC_MAX_SRC) {
712                 dev_info(ippdrv->dev, "inavlid buf_id %d.\n", buf_id);
713                 return -ENOMEM;
714         }
715
716         /* address register set */
717         switch (buf_type) {
718         case IPP_BUF_ENQUEUE:
719                 config = &property->config[EXYNOS_DRM_OPS_SRC];
720                 fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_Y],
721                         EXYNOS_CIIYSA(buf_id));
722
723                 if (config->fmt == DRM_FORMAT_YVU420) {
724                         fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CR],
725                                 EXYNOS_CIICBSA(buf_id));
726                         fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CB],
727                                 EXYNOS_CIICRSA(buf_id));
728                 } else {
729                         fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CB],
730                                 EXYNOS_CIICBSA(buf_id));
731                         fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CR],
732                                 EXYNOS_CIICRSA(buf_id));
733                 }
734                 break;
735         case IPP_BUF_DEQUEUE:
736                 fimc_write(ctx, 0x0, EXYNOS_CIIYSA(buf_id));
737                 fimc_write(ctx, 0x0, EXYNOS_CIICBSA(buf_id));
738                 fimc_write(ctx, 0x0, EXYNOS_CIICRSA(buf_id));
739                 break;
740         default:
741                 /* bypass */
742                 break;
743         }
744
745         return 0;
746 }
747
748 static struct exynos_drm_ipp_ops fimc_src_ops = {
749         .set_fmt = fimc_src_set_fmt,
750         .set_transf = fimc_src_set_transf,
751         .set_size = fimc_src_set_size,
752         .set_addr = fimc_src_set_addr,
753 };
754
755 static int fimc_dst_set_fmt_order(struct fimc_context *ctx, u32 fmt)
756 {
757         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
758         u32 cfg;
759
760         DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
761
762         /* RGB */
763         cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
764         cfg &= ~EXYNOS_CISCCTRL_OUTRGB_FMT_RGB_MASK;
765
766         switch (fmt) {
767         case DRM_FORMAT_RGB565:
768                 cfg |= EXYNOS_CISCCTRL_OUTRGB_FMT_RGB565;
769                 fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
770                 return 0;
771         case DRM_FORMAT_RGB888:
772                 cfg |= EXYNOS_CISCCTRL_OUTRGB_FMT_RGB888;
773                 fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
774                 return 0;
775         case DRM_FORMAT_XRGB8888:
776                 cfg |= (EXYNOS_CISCCTRL_OUTRGB_FMT_RGB888 |
777                         EXYNOS_CISCCTRL_EXTRGB_EXTENSION);
778                 fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
779                 break;
780         default:
781                 /* bypass */
782                 break;
783         }
784
785         /* YUV */
786         cfg = fimc_read(ctx, EXYNOS_CIOCTRL);
787         cfg &= ~(EXYNOS_CIOCTRL_ORDER2P_MASK |
788                 EXYNOS_CIOCTRL_ORDER422_MASK |
789                 EXYNOS_CIOCTRL_YCBCR_PLANE_MASK);
790
791         switch (fmt) {
792         case DRM_FORMAT_XRGB8888:
793                 cfg |= EXYNOS_CIOCTRL_ALPHA_OUT;
794                 break;
795         case DRM_FORMAT_YUYV:
796                 cfg |= EXYNOS_CIOCTRL_ORDER422_YCBYCR;
797                 break;
798         case DRM_FORMAT_YVYU:
799                 cfg |= EXYNOS_CIOCTRL_ORDER422_YCRYCB;
800                 break;
801         case DRM_FORMAT_UYVY:
802                 cfg |= EXYNOS_CIOCTRL_ORDER422_CBYCRY;
803                 break;
804         case DRM_FORMAT_VYUY:
805                 cfg |= EXYNOS_CIOCTRL_ORDER422_CRYCBY;
806                 break;
807         case DRM_FORMAT_NV21:
808         case DRM_FORMAT_NV61:
809                 cfg |= EXYNOS_CIOCTRL_ORDER2P_LSB_CRCB;
810                 cfg |= EXYNOS_CIOCTRL_YCBCR_2PLANE;
811                 break;
812         case DRM_FORMAT_YUV422:
813         case DRM_FORMAT_YUV420:
814         case DRM_FORMAT_YVU420:
815                 cfg |= EXYNOS_CIOCTRL_YCBCR_3PLANE;
816                 break;
817         case DRM_FORMAT_NV12:
818         case DRM_FORMAT_NV12MT:
819         case DRM_FORMAT_NV16:
820                 cfg |= EXYNOS_CIOCTRL_ORDER2P_LSB_CBCR;
821                 cfg |= EXYNOS_CIOCTRL_YCBCR_2PLANE;
822                 break;
823         default:
824                 dev_err(ippdrv->dev, "inavlid target yuv order 0x%x.\n", fmt);
825                 return -EINVAL;
826         }
827
828         fimc_write(ctx, cfg, EXYNOS_CIOCTRL);
829
830         return 0;
831 }
832
833 static int fimc_dst_set_fmt(struct device *dev, u32 fmt)
834 {
835         struct fimc_context *ctx = get_fimc_context(dev);
836         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
837         u32 cfg;
838
839         DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
840
841         cfg = fimc_read(ctx, EXYNOS_CIEXTEN);
842
843         if (fmt == DRM_FORMAT_AYUV) {
844                 cfg |= EXYNOS_CIEXTEN_YUV444_OUT;
845                 fimc_write(ctx, cfg, EXYNOS_CIEXTEN);
846         } else {
847                 cfg &= ~EXYNOS_CIEXTEN_YUV444_OUT;
848                 fimc_write(ctx, cfg, EXYNOS_CIEXTEN);
849
850                 cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
851                 cfg &= ~EXYNOS_CITRGFMT_OUTFORMAT_MASK;
852
853                 switch (fmt) {
854                 case DRM_FORMAT_RGB565:
855                 case DRM_FORMAT_RGB888:
856                 case DRM_FORMAT_XRGB8888:
857                         cfg |= EXYNOS_CITRGFMT_OUTFORMAT_RGB;
858                         break;
859                 case DRM_FORMAT_YUYV:
860                 case DRM_FORMAT_YVYU:
861                 case DRM_FORMAT_UYVY:
862                 case DRM_FORMAT_VYUY:
863                         cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR422_1PLANE;
864                         break;
865                 case DRM_FORMAT_NV16:
866                 case DRM_FORMAT_NV61:
867                 case DRM_FORMAT_YUV422:
868                         cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR422;
869                         break;
870                 case DRM_FORMAT_YUV420:
871                 case DRM_FORMAT_YVU420:
872                 case DRM_FORMAT_NV12:
873                 case DRM_FORMAT_NV12MT:
874                 case DRM_FORMAT_NV21:
875                         cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR420;
876                         break;
877                 default:
878                         dev_err(ippdrv->dev, "inavlid target format 0x%x.\n",
879                                 fmt);
880                         return -EINVAL;
881                 }
882
883                 fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
884         }
885
886         cfg = fimc_read(ctx, EXYNOS_CIDMAPARAM);
887         cfg &= ~EXYNOS_CIDMAPARAM_W_MODE_MASK;
888
889         if (fmt == DRM_FORMAT_NV12MT)
890                 cfg |= EXYNOS_CIDMAPARAM_W_MODE_64X32;
891         else
892                 cfg |= EXYNOS_CIDMAPARAM_W_MODE_LINEAR;
893
894         fimc_write(ctx, cfg, EXYNOS_CIDMAPARAM);
895
896         return fimc_dst_set_fmt_order(ctx, fmt);
897 }
898
899 static int fimc_dst_set_transf(struct device *dev,
900                 enum drm_exynos_degree degree,
901                 enum drm_exynos_flip flip, bool *swap)
902 {
903         struct fimc_context *ctx = get_fimc_context(dev);
904         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
905         u32 cfg;
906
907         DRM_DEBUG_KMS("degree[%d]flip[0x%x]\n", degree, flip);
908
909         cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
910         cfg &= ~EXYNOS_CITRGFMT_FLIP_MASK;
911         cfg &= ~EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE;
912
913         switch (degree) {
914         case EXYNOS_DRM_DEGREE_0:
915                 if (flip & EXYNOS_DRM_FLIP_VERTICAL)
916                         cfg |= EXYNOS_CITRGFMT_FLIP_X_MIRROR;
917                 if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
918                         cfg |= EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
919                 break;
920         case EXYNOS_DRM_DEGREE_90:
921                 cfg |= EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE;
922                 if (flip & EXYNOS_DRM_FLIP_VERTICAL)
923                         cfg |= EXYNOS_CITRGFMT_FLIP_X_MIRROR;
924                 if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
925                         cfg |= EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
926                 break;
927         case EXYNOS_DRM_DEGREE_180:
928                 cfg |= (EXYNOS_CITRGFMT_FLIP_X_MIRROR |
929                         EXYNOS_CITRGFMT_FLIP_Y_MIRROR);
930                 if (flip & EXYNOS_DRM_FLIP_VERTICAL)
931                         cfg &= ~EXYNOS_CITRGFMT_FLIP_X_MIRROR;
932                 if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
933                         cfg &= ~EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
934                 break;
935         case EXYNOS_DRM_DEGREE_270:
936                 cfg |= (EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE |
937                         EXYNOS_CITRGFMT_FLIP_X_MIRROR |
938                         EXYNOS_CITRGFMT_FLIP_Y_MIRROR);
939                 if (flip & EXYNOS_DRM_FLIP_VERTICAL)
940                         cfg &= ~EXYNOS_CITRGFMT_FLIP_X_MIRROR;
941                 if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
942                         cfg &= ~EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
943                 break;
944         default:
945                 dev_err(ippdrv->dev, "inavlid degree value %d.\n", degree);
946                 return -EINVAL;
947         }
948
949         fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
950         *swap = (cfg & EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE) ? 1 : 0;
951
952         return 0;
953 }
954
955 static int fimc_set_prescaler(struct fimc_context *ctx, struct fimc_scaler *sc,
956                 struct drm_exynos_pos *src, struct drm_exynos_pos *dst)
957 {
958         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
959         u32 cfg, cfg_ext, shfactor;
960         u32 pre_dst_width, pre_dst_height;
961         u32 hfactor, vfactor;
962         int ret = 0;
963         u32 src_w, src_h, dst_w, dst_h;
964
965         cfg_ext = fimc_read(ctx, EXYNOS_CITRGFMT);
966         if (cfg_ext & EXYNOS_CITRGFMT_INROT90_CLOCKWISE) {
967                 src_w = src->h;
968                 src_h = src->w;
969         } else {
970                 src_w = src->w;
971                 src_h = src->h;
972         }
973
974         if (cfg_ext & EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE) {
975                 dst_w = dst->h;
976                 dst_h = dst->w;
977         } else {
978                 dst_w = dst->w;
979                 dst_h = dst->h;
980         }
981
982         /* fimc_ippdrv_check_property assures that dividers are not null */
983         hfactor = fls(src_w / dst_w / 2);
984         if (hfactor > FIMC_SHFACTOR / 2) {
985                 dev_err(ippdrv->dev, "failed to get ratio horizontal.\n");
986                 return -EINVAL;
987         }
988
989         vfactor = fls(src_h / dst_h / 2);
990         if (vfactor > FIMC_SHFACTOR / 2) {
991                 dev_err(ippdrv->dev, "failed to get ratio vertical.\n");
992                 return -EINVAL;
993         }
994
995         pre_dst_width = src_w >> hfactor;
996         pre_dst_height = src_h >> vfactor;
997         DRM_DEBUG_KMS("pre_dst_width[%d]pre_dst_height[%d]\n",
998                 pre_dst_width, pre_dst_height);
999         DRM_DEBUG_KMS("hfactor[%d]vfactor[%d]\n", hfactor, vfactor);
1000
1001         sc->hratio = (src_w << 14) / (dst_w << hfactor);
1002         sc->vratio = (src_h << 14) / (dst_h << vfactor);
1003         sc->up_h = (dst_w >= src_w) ? true : false;
1004         sc->up_v = (dst_h >= src_h) ? true : false;
1005         DRM_DEBUG_KMS("hratio[%d]vratio[%d]up_h[%d]up_v[%d]\n",
1006                 sc->hratio, sc->vratio, sc->up_h, sc->up_v);
1007
1008         shfactor = FIMC_SHFACTOR - (hfactor + vfactor);
1009         DRM_DEBUG_KMS("shfactor[%d]\n", shfactor);
1010
1011         cfg = (EXYNOS_CISCPRERATIO_SHFACTOR(shfactor) |
1012                 EXYNOS_CISCPRERATIO_PREHORRATIO(1 << hfactor) |
1013                 EXYNOS_CISCPRERATIO_PREVERRATIO(1 << vfactor));
1014         fimc_write(ctx, cfg, EXYNOS_CISCPRERATIO);
1015
1016         cfg = (EXYNOS_CISCPREDST_PREDSTWIDTH(pre_dst_width) |
1017                 EXYNOS_CISCPREDST_PREDSTHEIGHT(pre_dst_height));
1018         fimc_write(ctx, cfg, EXYNOS_CISCPREDST);
1019
1020         return ret;
1021 }
1022
1023 static void fimc_set_scaler(struct fimc_context *ctx, struct fimc_scaler *sc)
1024 {
1025         u32 cfg, cfg_ext;
1026
1027         DRM_DEBUG_KMS("range[%d]bypass[%d]up_h[%d]up_v[%d]\n",
1028                 sc->range, sc->bypass, sc->up_h, sc->up_v);
1029         DRM_DEBUG_KMS("hratio[%d]vratio[%d]\n",
1030                 sc->hratio, sc->vratio);
1031
1032         cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
1033         cfg &= ~(EXYNOS_CISCCTRL_SCALERBYPASS |
1034                 EXYNOS_CISCCTRL_SCALEUP_H | EXYNOS_CISCCTRL_SCALEUP_V |
1035                 EXYNOS_CISCCTRL_MAIN_V_RATIO_MASK |
1036                 EXYNOS_CISCCTRL_MAIN_H_RATIO_MASK |
1037                 EXYNOS_CISCCTRL_CSCR2Y_WIDE |
1038                 EXYNOS_CISCCTRL_CSCY2R_WIDE);
1039
1040         if (sc->range)
1041                 cfg |= (EXYNOS_CISCCTRL_CSCR2Y_WIDE |
1042                         EXYNOS_CISCCTRL_CSCY2R_WIDE);
1043         if (sc->bypass)
1044                 cfg |= EXYNOS_CISCCTRL_SCALERBYPASS;
1045         if (sc->up_h)
1046                 cfg |= EXYNOS_CISCCTRL_SCALEUP_H;
1047         if (sc->up_v)
1048                 cfg |= EXYNOS_CISCCTRL_SCALEUP_V;
1049
1050         cfg |= (EXYNOS_CISCCTRL_MAINHORRATIO((sc->hratio >> 6)) |
1051                 EXYNOS_CISCCTRL_MAINVERRATIO((sc->vratio >> 6)));
1052         fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
1053
1054         cfg_ext = fimc_read(ctx, EXYNOS_CIEXTEN);
1055         cfg_ext &= ~EXYNOS_CIEXTEN_MAINHORRATIO_EXT_MASK;
1056         cfg_ext &= ~EXYNOS_CIEXTEN_MAINVERRATIO_EXT_MASK;
1057         cfg_ext |= (EXYNOS_CIEXTEN_MAINHORRATIO_EXT(sc->hratio) |
1058                 EXYNOS_CIEXTEN_MAINVERRATIO_EXT(sc->vratio));
1059         fimc_write(ctx, cfg_ext, EXYNOS_CIEXTEN);
1060 }
1061
1062 static int fimc_dst_set_size(struct device *dev, int swap,
1063                 struct drm_exynos_pos *pos, struct drm_exynos_sz *sz)
1064 {
1065         struct fimc_context *ctx = get_fimc_context(dev);
1066         struct drm_exynos_pos img_pos = *pos;
1067         struct drm_exynos_sz img_sz = *sz;
1068         u32 cfg;
1069
1070         DRM_DEBUG_KMS("swap[%d]hsize[%d]vsize[%d]\n",
1071                 swap, sz->hsize, sz->vsize);
1072
1073         /* original size */
1074         cfg = (EXYNOS_ORGOSIZE_HORIZONTAL(img_sz.hsize) |
1075                 EXYNOS_ORGOSIZE_VERTICAL(img_sz.vsize));
1076
1077         fimc_write(ctx, cfg, EXYNOS_ORGOSIZE);
1078
1079         DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]\n", pos->x, pos->y, pos->w, pos->h);
1080
1081         /* CSC ITU */
1082         cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
1083         cfg &= ~EXYNOS_CIGCTRL_CSC_MASK;
1084
1085         if (sz->hsize >= FIMC_WIDTH_ITU_709)
1086                 cfg |= EXYNOS_CIGCTRL_CSC_ITU709;
1087         else
1088                 cfg |= EXYNOS_CIGCTRL_CSC_ITU601;
1089
1090         fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
1091
1092         if (swap) {
1093                 img_pos.w = pos->h;
1094                 img_pos.h = pos->w;
1095                 img_sz.hsize = sz->vsize;
1096                 img_sz.vsize = sz->hsize;
1097         }
1098
1099         /* target image size */
1100         cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
1101         cfg &= ~(EXYNOS_CITRGFMT_TARGETH_MASK |
1102                 EXYNOS_CITRGFMT_TARGETV_MASK);
1103         cfg |= (EXYNOS_CITRGFMT_TARGETHSIZE(img_pos.w) |
1104                 EXYNOS_CITRGFMT_TARGETVSIZE(img_pos.h));
1105         fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
1106
1107         /* target area */
1108         cfg = EXYNOS_CITAREA_TARGET_AREA(img_pos.w * img_pos.h);
1109         fimc_write(ctx, cfg, EXYNOS_CITAREA);
1110
1111         /* offset Y(RGB), Cb, Cr */
1112         cfg = (EXYNOS_CIOYOFF_HORIZONTAL(img_pos.x) |
1113                 EXYNOS_CIOYOFF_VERTICAL(img_pos.y));
1114         fimc_write(ctx, cfg, EXYNOS_CIOYOFF);
1115         cfg = (EXYNOS_CIOCBOFF_HORIZONTAL(img_pos.x) |
1116                 EXYNOS_CIOCBOFF_VERTICAL(img_pos.y));
1117         fimc_write(ctx, cfg, EXYNOS_CIOCBOFF);
1118         cfg = (EXYNOS_CIOCROFF_HORIZONTAL(img_pos.x) |
1119                 EXYNOS_CIOCROFF_VERTICAL(img_pos.y));
1120         fimc_write(ctx, cfg, EXYNOS_CIOCROFF);
1121
1122         return 0;
1123 }
1124
1125 static int fimc_dst_get_buf_seq(struct fimc_context *ctx)
1126 {
1127         u32 cfg, i, buf_num = 0;
1128         u32 mask = 0x00000001;
1129
1130         cfg = fimc_read(ctx, EXYNOS_CIFCNTSEQ);
1131
1132         for (i = 0; i < FIMC_REG_SZ; i++)
1133                 if (cfg & (mask << i))
1134                         buf_num++;
1135
1136         DRM_DEBUG_KMS("buf_num[%d]\n", buf_num);
1137
1138         return buf_num;
1139 }
1140
1141 static int fimc_dst_set_buf_seq(struct fimc_context *ctx, u32 buf_id,
1142                 enum drm_exynos_ipp_buf_type buf_type)
1143 {
1144         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
1145         bool enable;
1146         u32 cfg;
1147         u32 mask = 0x00000001 << buf_id;
1148         int ret = 0;
1149
1150         DRM_DEBUG_KMS("buf_id[%d]buf_type[%d]\n", buf_id, buf_type);
1151
1152         mutex_lock(&ctx->lock);
1153
1154         /* mask register set */
1155         cfg = fimc_read(ctx, EXYNOS_CIFCNTSEQ);
1156
1157         switch (buf_type) {
1158         case IPP_BUF_ENQUEUE:
1159                 enable = true;
1160                 break;
1161         case IPP_BUF_DEQUEUE:
1162                 enable = false;
1163                 break;
1164         default:
1165                 dev_err(ippdrv->dev, "invalid buf ctrl parameter.\n");
1166                 ret =  -EINVAL;
1167                 goto err_unlock;
1168         }
1169
1170         /* sequence id */
1171         cfg &= ~mask;
1172         cfg |= (enable << buf_id);
1173         fimc_write(ctx, cfg, EXYNOS_CIFCNTSEQ);
1174
1175         /* interrupt enable */
1176         if (buf_type == IPP_BUF_ENQUEUE &&
1177             fimc_dst_get_buf_seq(ctx) >= FIMC_BUF_START)
1178                 fimc_mask_irq(ctx, true);
1179
1180         /* interrupt disable */
1181         if (buf_type == IPP_BUF_DEQUEUE &&
1182             fimc_dst_get_buf_seq(ctx) <= FIMC_BUF_STOP)
1183                 fimc_mask_irq(ctx, false);
1184
1185 err_unlock:
1186         mutex_unlock(&ctx->lock);
1187         return ret;
1188 }
1189
1190 static int fimc_dst_set_addr(struct device *dev,
1191                 struct drm_exynos_ipp_buf_info *buf_info, u32 buf_id,
1192                 enum drm_exynos_ipp_buf_type buf_type)
1193 {
1194         struct fimc_context *ctx = get_fimc_context(dev);
1195         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
1196         struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
1197         struct drm_exynos_ipp_property *property;
1198         struct drm_exynos_ipp_config *config;
1199
1200         if (!c_node) {
1201                 DRM_ERROR("failed to get c_node.\n");
1202                 return -EINVAL;
1203         }
1204
1205         property = &c_node->property;
1206
1207         DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]buf_type[%d]\n",
1208                 property->prop_id, buf_id, buf_type);
1209
1210         if (buf_id > FIMC_MAX_DST) {
1211                 dev_info(ippdrv->dev, "inavlid buf_id %d.\n", buf_id);
1212                 return -ENOMEM;
1213         }
1214
1215         /* address register set */
1216         switch (buf_type) {
1217         case IPP_BUF_ENQUEUE:
1218                 config = &property->config[EXYNOS_DRM_OPS_DST];
1219
1220                 fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_Y],
1221                         EXYNOS_CIOYSA(buf_id));
1222
1223                 if (config->fmt == DRM_FORMAT_YVU420) {
1224                         fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CR],
1225                                 EXYNOS_CIOCBSA(buf_id));
1226                         fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CB],
1227                                 EXYNOS_CIOCRSA(buf_id));
1228                 } else {
1229                         fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CB],
1230                                 EXYNOS_CIOCBSA(buf_id));
1231                         fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CR],
1232                                 EXYNOS_CIOCRSA(buf_id));
1233                 }
1234                 break;
1235         case IPP_BUF_DEQUEUE:
1236                 fimc_write(ctx, 0x0, EXYNOS_CIOYSA(buf_id));
1237                 fimc_write(ctx, 0x0, EXYNOS_CIOCBSA(buf_id));
1238                 fimc_write(ctx, 0x0, EXYNOS_CIOCRSA(buf_id));
1239                 break;
1240         default:
1241                 /* bypass */
1242                 break;
1243         }
1244
1245         return fimc_dst_set_buf_seq(ctx, buf_id, buf_type);
1246 }
1247
1248 static struct exynos_drm_ipp_ops fimc_dst_ops = {
1249         .set_fmt = fimc_dst_set_fmt,
1250         .set_transf = fimc_dst_set_transf,
1251         .set_size = fimc_dst_set_size,
1252         .set_addr = fimc_dst_set_addr,
1253 };
1254
1255 static int fimc_clk_ctrl(struct fimc_context *ctx, bool enable)
1256 {
1257         DRM_DEBUG_KMS("enable[%d]\n", enable);
1258
1259         if (enable) {
1260                 clk_prepare_enable(ctx->clocks[FIMC_CLK_GATE]);
1261                 clk_prepare_enable(ctx->clocks[FIMC_CLK_WB_A]);
1262                 ctx->suspended = false;
1263         } else {
1264                 clk_disable_unprepare(ctx->clocks[FIMC_CLK_GATE]);
1265                 clk_disable_unprepare(ctx->clocks[FIMC_CLK_WB_A]);
1266                 ctx->suspended = true;
1267         }
1268
1269         return 0;
1270 }
1271
1272 static irqreturn_t fimc_irq_handler(int irq, void *dev_id)
1273 {
1274         struct fimc_context *ctx = dev_id;
1275         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
1276         struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
1277         struct drm_exynos_ipp_event_work *event_work =
1278                 c_node->event_work;
1279         int buf_id;
1280
1281         DRM_DEBUG_KMS("fimc id[%d]\n", ctx->id);
1282
1283         fimc_clear_irq(ctx);
1284         if (fimc_check_ovf(ctx))
1285                 return IRQ_NONE;
1286
1287         if (!fimc_check_frame_end(ctx))
1288                 return IRQ_NONE;
1289
1290         buf_id = fimc_get_buf_id(ctx);
1291         if (buf_id < 0)
1292                 return IRQ_HANDLED;
1293
1294         DRM_DEBUG_KMS("buf_id[%d]\n", buf_id);
1295
1296         if (fimc_dst_set_buf_seq(ctx, buf_id, IPP_BUF_DEQUEUE) < 0) {
1297                 DRM_ERROR("failed to dequeue.\n");
1298                 return IRQ_HANDLED;
1299         }
1300
1301         event_work->ippdrv = ippdrv;
1302         event_work->buf_id[EXYNOS_DRM_OPS_DST] = buf_id;
1303         queue_work(ippdrv->event_workq, (struct work_struct *)event_work);
1304
1305         return IRQ_HANDLED;
1306 }
1307
1308 static int fimc_init_prop_list(struct exynos_drm_ippdrv *ippdrv)
1309 {
1310         struct drm_exynos_ipp_prop_list *prop_list = &ippdrv->prop_list;
1311
1312         prop_list->version = 1;
1313         prop_list->writeback = 1;
1314         prop_list->refresh_min = FIMC_REFRESH_MIN;
1315         prop_list->refresh_max = FIMC_REFRESH_MAX;
1316         prop_list->flip = (1 << EXYNOS_DRM_FLIP_NONE) |
1317                                 (1 << EXYNOS_DRM_FLIP_VERTICAL) |
1318                                 (1 << EXYNOS_DRM_FLIP_HORIZONTAL);
1319         prop_list->degree = (1 << EXYNOS_DRM_DEGREE_0) |
1320                                 (1 << EXYNOS_DRM_DEGREE_90) |
1321                                 (1 << EXYNOS_DRM_DEGREE_180) |
1322                                 (1 << EXYNOS_DRM_DEGREE_270);
1323         prop_list->csc = 1;
1324         prop_list->crop = 1;
1325         prop_list->crop_max.hsize = FIMC_CROP_MAX;
1326         prop_list->crop_max.vsize = FIMC_CROP_MAX;
1327         prop_list->crop_min.hsize = FIMC_CROP_MIN;
1328         prop_list->crop_min.vsize = FIMC_CROP_MIN;
1329         prop_list->scale = 1;
1330         prop_list->scale_max.hsize = FIMC_SCALE_MAX;
1331         prop_list->scale_max.vsize = FIMC_SCALE_MAX;
1332         prop_list->scale_min.hsize = FIMC_SCALE_MIN;
1333         prop_list->scale_min.vsize = FIMC_SCALE_MIN;
1334
1335         return 0;
1336 }
1337
1338 static inline bool fimc_check_drm_flip(enum drm_exynos_flip flip)
1339 {
1340         switch (flip) {
1341         case EXYNOS_DRM_FLIP_NONE:
1342         case EXYNOS_DRM_FLIP_VERTICAL:
1343         case EXYNOS_DRM_FLIP_HORIZONTAL:
1344         case EXYNOS_DRM_FLIP_BOTH:
1345                 return true;
1346         default:
1347                 DRM_DEBUG_KMS("invalid flip\n");
1348                 return false;
1349         }
1350 }
1351
1352 static int fimc_ippdrv_check_property(struct device *dev,
1353                 struct drm_exynos_ipp_property *property)
1354 {
1355         struct fimc_context *ctx = get_fimc_context(dev);
1356         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
1357         struct drm_exynos_ipp_prop_list *pp = &ippdrv->prop_list;
1358         struct drm_exynos_ipp_config *config;
1359         struct drm_exynos_pos *pos;
1360         struct drm_exynos_sz *sz;
1361         bool swap;
1362         int i;
1363
1364         for_each_ipp_ops(i) {
1365                 if ((i == EXYNOS_DRM_OPS_SRC) &&
1366                         (property->cmd == IPP_CMD_WB))
1367                         continue;
1368
1369                 config = &property->config[i];
1370                 pos = &config->pos;
1371                 sz = &config->sz;
1372
1373                 /* check for flip */
1374                 if (!fimc_check_drm_flip(config->flip)) {
1375                         DRM_ERROR("invalid flip.\n");
1376                         goto err_property;
1377                 }
1378
1379                 /* check for degree */
1380                 switch (config->degree) {
1381                 case EXYNOS_DRM_DEGREE_90:
1382                 case EXYNOS_DRM_DEGREE_270:
1383                         swap = true;
1384                         break;
1385                 case EXYNOS_DRM_DEGREE_0:
1386                 case EXYNOS_DRM_DEGREE_180:
1387                         swap = false;
1388                         break;
1389                 default:
1390                         DRM_ERROR("invalid degree.\n");
1391                         goto err_property;
1392                 }
1393
1394                 /* check for buffer bound */
1395                 if ((pos->x + pos->w > sz->hsize) ||
1396                         (pos->y + pos->h > sz->vsize)) {
1397                         DRM_ERROR("out of buf bound.\n");
1398                         goto err_property;
1399                 }
1400
1401                 /* check for crop */
1402                 if ((i == EXYNOS_DRM_OPS_SRC) && (pp->crop)) {
1403                         if (swap) {
1404                                 if ((pos->h < pp->crop_min.hsize) ||
1405                                         (sz->vsize > pp->crop_max.hsize) ||
1406                                         (pos->w < pp->crop_min.vsize) ||
1407                                         (sz->hsize > pp->crop_max.vsize)) {
1408                                         DRM_ERROR("out of crop size.\n");
1409                                         goto err_property;
1410                                 }
1411                         } else {
1412                                 if ((pos->w < pp->crop_min.hsize) ||
1413                                         (sz->hsize > pp->crop_max.hsize) ||
1414                                         (pos->h < pp->crop_min.vsize) ||
1415                                         (sz->vsize > pp->crop_max.vsize)) {
1416                                         DRM_ERROR("out of crop size.\n");
1417                                         goto err_property;
1418                                 }
1419                         }
1420                 }
1421
1422                 /* check for scale */
1423                 if ((i == EXYNOS_DRM_OPS_DST) && (pp->scale)) {
1424                         if (swap) {
1425                                 if ((pos->h < pp->scale_min.hsize) ||
1426                                         (sz->vsize > pp->scale_max.hsize) ||
1427                                         (pos->w < pp->scale_min.vsize) ||
1428                                         (sz->hsize > pp->scale_max.vsize)) {
1429                                         DRM_ERROR("out of scale size.\n");
1430                                         goto err_property;
1431                                 }
1432                         } else {
1433                                 if ((pos->w < pp->scale_min.hsize) ||
1434                                         (sz->hsize > pp->scale_max.hsize) ||
1435                                         (pos->h < pp->scale_min.vsize) ||
1436                                         (sz->vsize > pp->scale_max.vsize)) {
1437                                         DRM_ERROR("out of scale size.\n");
1438                                         goto err_property;
1439                                 }
1440                         }
1441                 }
1442         }
1443
1444         return 0;
1445
1446 err_property:
1447         for_each_ipp_ops(i) {
1448                 if ((i == EXYNOS_DRM_OPS_SRC) &&
1449                         (property->cmd == IPP_CMD_WB))
1450                         continue;
1451
1452                 config = &property->config[i];
1453                 pos = &config->pos;
1454                 sz = &config->sz;
1455
1456                 DRM_ERROR("[%s]f[%d]r[%d]pos[%d %d %d %d]sz[%d %d]\n",
1457                         i ? "dst" : "src", config->flip, config->degree,
1458                         pos->x, pos->y, pos->w, pos->h,
1459                         sz->hsize, sz->vsize);
1460         }
1461
1462         return -EINVAL;
1463 }
1464
1465 static void fimc_clear_addr(struct fimc_context *ctx)
1466 {
1467         int i;
1468
1469         for (i = 0; i < FIMC_MAX_SRC; i++) {
1470                 fimc_write(ctx, 0, EXYNOS_CIIYSA(i));
1471                 fimc_write(ctx, 0, EXYNOS_CIICBSA(i));
1472                 fimc_write(ctx, 0, EXYNOS_CIICRSA(i));
1473         }
1474
1475         for (i = 0; i < FIMC_MAX_DST; i++) {
1476                 fimc_write(ctx, 0, EXYNOS_CIOYSA(i));
1477                 fimc_write(ctx, 0, EXYNOS_CIOCBSA(i));
1478                 fimc_write(ctx, 0, EXYNOS_CIOCRSA(i));
1479         }
1480 }
1481
1482 static int fimc_ippdrv_reset(struct device *dev)
1483 {
1484         struct fimc_context *ctx = get_fimc_context(dev);
1485
1486         /* reset h/w block */
1487         fimc_sw_reset(ctx);
1488
1489         /* reset scaler capability */
1490         memset(&ctx->sc, 0x0, sizeof(ctx->sc));
1491
1492         fimc_clear_addr(ctx);
1493
1494         return 0;
1495 }
1496
1497 static int fimc_ippdrv_start(struct device *dev, enum drm_exynos_ipp_cmd cmd)
1498 {
1499         struct fimc_context *ctx = get_fimc_context(dev);
1500         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
1501         struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
1502         struct drm_exynos_ipp_property *property;
1503         struct drm_exynos_ipp_config *config;
1504         struct drm_exynos_pos   img_pos[EXYNOS_DRM_OPS_MAX];
1505         struct drm_exynos_ipp_set_wb set_wb;
1506         int ret, i;
1507         u32 cfg0, cfg1;
1508
1509         DRM_DEBUG_KMS("cmd[%d]\n", cmd);
1510
1511         if (!c_node) {
1512                 DRM_ERROR("failed to get c_node.\n");
1513                 return -EINVAL;
1514         }
1515
1516         property = &c_node->property;
1517
1518         fimc_mask_irq(ctx, true);
1519
1520         for_each_ipp_ops(i) {
1521                 config = &property->config[i];
1522                 img_pos[i] = config->pos;
1523         }
1524
1525         ret = fimc_set_prescaler(ctx, &ctx->sc,
1526                 &img_pos[EXYNOS_DRM_OPS_SRC],
1527                 &img_pos[EXYNOS_DRM_OPS_DST]);
1528         if (ret) {
1529                 dev_err(dev, "failed to set precalser.\n");
1530                 return ret;
1531         }
1532
1533         /* If set ture, we can save jpeg about screen */
1534         fimc_handle_jpeg(ctx, false);
1535         fimc_set_scaler(ctx, &ctx->sc);
1536         fimc_set_polarity(ctx, &ctx->pol);
1537
1538         switch (cmd) {
1539         case IPP_CMD_M2M:
1540                 fimc_set_type_ctrl(ctx, FIMC_WB_NONE);
1541                 fimc_handle_lastend(ctx, false);
1542
1543                 /* setup dma */
1544                 cfg0 = fimc_read(ctx, EXYNOS_MSCTRL);
1545                 cfg0 &= ~EXYNOS_MSCTRL_INPUT_MASK;
1546                 cfg0 |= EXYNOS_MSCTRL_INPUT_MEMORY;
1547                 fimc_write(ctx, cfg0, EXYNOS_MSCTRL);
1548                 break;
1549         case IPP_CMD_WB:
1550                 fimc_set_type_ctrl(ctx, FIMC_WB_A);
1551                 fimc_handle_lastend(ctx, true);
1552
1553                 /* setup FIMD */
1554                 ret = fimc_set_camblk_fimd0_wb(ctx);
1555                 if (ret < 0) {
1556                         dev_err(dev, "camblk setup failed.\n");
1557                         return ret;
1558                 }
1559
1560                 set_wb.enable = 1;
1561                 set_wb.refresh = property->refresh_rate;
1562                 exynos_drm_ippnb_send_event(IPP_SET_WRITEBACK, (void *)&set_wb);
1563                 break;
1564         case IPP_CMD_OUTPUT:
1565         default:
1566                 ret = -EINVAL;
1567                 dev_err(dev, "invalid operations.\n");
1568                 return ret;
1569         }
1570
1571         /* Reset status */
1572         fimc_write(ctx, 0x0, EXYNOS_CISTATUS);
1573
1574         cfg0 = fimc_read(ctx, EXYNOS_CIIMGCPT);
1575         cfg0 &= ~EXYNOS_CIIMGCPT_IMGCPTEN_SC;
1576         cfg0 |= EXYNOS_CIIMGCPT_IMGCPTEN_SC;
1577
1578         /* Scaler */
1579         cfg1 = fimc_read(ctx, EXYNOS_CISCCTRL);
1580         cfg1 &= ~EXYNOS_CISCCTRL_SCAN_MASK;
1581         cfg1 |= (EXYNOS_CISCCTRL_PROGRESSIVE |
1582                 EXYNOS_CISCCTRL_SCALERSTART);
1583
1584         fimc_write(ctx, cfg1, EXYNOS_CISCCTRL);
1585
1586         /* Enable image capture*/
1587         cfg0 |= EXYNOS_CIIMGCPT_IMGCPTEN;
1588         fimc_write(ctx, cfg0, EXYNOS_CIIMGCPT);
1589
1590         /* Disable frame end irq */
1591         fimc_clear_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_END_DISABLE);
1592
1593         fimc_clear_bits(ctx, EXYNOS_CIOCTRL, EXYNOS_CIOCTRL_WEAVE_MASK);
1594
1595         if (cmd == IPP_CMD_M2M) {
1596                 fimc_set_bits(ctx, EXYNOS_MSCTRL, EXYNOS_MSCTRL_ENVID);
1597
1598                 fimc_set_bits(ctx, EXYNOS_MSCTRL, EXYNOS_MSCTRL_ENVID);
1599         }
1600
1601         return 0;
1602 }
1603
1604 static void fimc_ippdrv_stop(struct device *dev, enum drm_exynos_ipp_cmd cmd)
1605 {
1606         struct fimc_context *ctx = get_fimc_context(dev);
1607         struct drm_exynos_ipp_set_wb set_wb = {0, 0};
1608         u32 cfg;
1609
1610         DRM_DEBUG_KMS("cmd[%d]\n", cmd);
1611
1612         switch (cmd) {
1613         case IPP_CMD_M2M:
1614                 /* Source clear */
1615                 cfg = fimc_read(ctx, EXYNOS_MSCTRL);
1616                 cfg &= ~EXYNOS_MSCTRL_INPUT_MASK;
1617                 cfg &= ~EXYNOS_MSCTRL_ENVID;
1618                 fimc_write(ctx, cfg, EXYNOS_MSCTRL);
1619                 break;
1620         case IPP_CMD_WB:
1621                 exynos_drm_ippnb_send_event(IPP_SET_WRITEBACK, (void *)&set_wb);
1622                 break;
1623         case IPP_CMD_OUTPUT:
1624         default:
1625                 dev_err(dev, "invalid operations.\n");
1626                 break;
1627         }
1628
1629         fimc_mask_irq(ctx, false);
1630
1631         /* reset sequence */
1632         fimc_write(ctx, 0x0, EXYNOS_CIFCNTSEQ);
1633
1634         /* Scaler disable */
1635         fimc_clear_bits(ctx, EXYNOS_CISCCTRL, EXYNOS_CISCCTRL_SCALERSTART);
1636
1637         /* Disable image capture */
1638         fimc_clear_bits(ctx, EXYNOS_CIIMGCPT,
1639                 EXYNOS_CIIMGCPT_IMGCPTEN_SC | EXYNOS_CIIMGCPT_IMGCPTEN);
1640
1641         /* Enable frame end irq */
1642         fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_END_DISABLE);
1643 }
1644
1645 static void fimc_put_clocks(struct fimc_context *ctx)
1646 {
1647         int i;
1648
1649         for (i = 0; i < FIMC_CLKS_MAX; i++) {
1650                 if (IS_ERR(ctx->clocks[i]))
1651                         continue;
1652                 clk_put(ctx->clocks[i]);
1653                 ctx->clocks[i] = ERR_PTR(-EINVAL);
1654         }
1655 }
1656
1657 static int fimc_setup_clocks(struct fimc_context *ctx)
1658 {
1659         struct device *fimc_dev = ctx->ippdrv.dev;
1660         struct device *dev;
1661         int ret, i;
1662
1663         for (i = 0; i < FIMC_CLKS_MAX; i++)
1664                 ctx->clocks[i] = ERR_PTR(-EINVAL);
1665
1666         for (i = 0; i < FIMC_CLKS_MAX; i++) {
1667                 if (i == FIMC_CLK_WB_A || i == FIMC_CLK_WB_B)
1668                         dev = fimc_dev->parent;
1669                 else
1670                         dev = fimc_dev;
1671
1672                 ctx->clocks[i] = clk_get(dev, fimc_clock_names[i]);
1673                 if (IS_ERR(ctx->clocks[i])) {
1674                         if (i >= FIMC_CLK_MUX)
1675                                 break;
1676                         ret = PTR_ERR(ctx->clocks[i]);
1677                         dev_err(fimc_dev, "failed to get clock: %s\n",
1678                                                 fimc_clock_names[i]);
1679                         goto e_clk_free;
1680                 }
1681         }
1682
1683         /* Optional FIMC LCLK parent clock setting */
1684         if (!IS_ERR(ctx->clocks[FIMC_CLK_PARENT])) {
1685                 ret = clk_set_parent(ctx->clocks[FIMC_CLK_MUX],
1686                                      ctx->clocks[FIMC_CLK_PARENT]);
1687                 if (ret < 0) {
1688                         dev_err(fimc_dev, "failed to set parent.\n");
1689                         goto e_clk_free;
1690                 }
1691         }
1692
1693         ret = clk_set_rate(ctx->clocks[FIMC_CLK_LCLK], ctx->clk_frequency);
1694         if (ret < 0)
1695                 goto e_clk_free;
1696
1697         ret = clk_prepare_enable(ctx->clocks[FIMC_CLK_LCLK]);
1698         if (!ret)
1699                 return ret;
1700 e_clk_free:
1701         fimc_put_clocks(ctx);
1702         return ret;
1703 }
1704
1705 static int fimc_parse_dt(struct fimc_context *ctx)
1706 {
1707         struct device_node *node = ctx->ippdrv.dev->of_node;
1708
1709         /* Handle only devices that support the LCD Writeback data path */
1710         if (!of_property_read_bool(node, "samsung,lcd-wb"))
1711                 return -ENODEV;
1712
1713         if (of_property_read_u32(node, "clock-frequency",
1714                                         &ctx->clk_frequency))
1715                 ctx->clk_frequency = FIMC_DEFAULT_LCLK_FREQUENCY;
1716
1717         ctx->id = of_alias_get_id(node, "fimc");
1718
1719         if (ctx->id < 0) {
1720                 dev_err(ctx->ippdrv.dev, "failed to get node alias id.\n");
1721                 return -EINVAL;
1722         }
1723
1724         return 0;
1725 }
1726
1727 static int fimc_probe(struct platform_device *pdev)
1728 {
1729         struct device *dev = &pdev->dev;
1730         struct fimc_context *ctx;
1731         struct resource *res;
1732         struct exynos_drm_ippdrv *ippdrv;
1733         int ret;
1734
1735         if (!dev->of_node) {
1736                 dev_err(dev, "device tree node not found.\n");
1737                 return -ENODEV;
1738         }
1739
1740         ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1741         if (!ctx)
1742                 return -ENOMEM;
1743
1744         ctx->ippdrv.dev = dev;
1745
1746         ret = fimc_parse_dt(ctx);
1747         if (ret < 0)
1748                 return ret;
1749
1750         ctx->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
1751                                                 "samsung,sysreg");
1752         if (IS_ERR(ctx->sysreg)) {
1753                 dev_err(dev, "syscon regmap lookup failed.\n");
1754                 return PTR_ERR(ctx->sysreg);
1755         }
1756
1757         /* resource memory */
1758         ctx->regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1759         ctx->regs = devm_ioremap_resource(dev, ctx->regs_res);
1760         if (IS_ERR(ctx->regs))
1761                 return PTR_ERR(ctx->regs);
1762
1763         /* resource irq */
1764         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1765         if (!res) {
1766                 dev_err(dev, "failed to request irq resource.\n");
1767                 return -ENOENT;
1768         }
1769
1770         ctx->irq = res->start;
1771         ret = devm_request_threaded_irq(dev, ctx->irq, NULL, fimc_irq_handler,
1772                 IRQF_ONESHOT, "drm_fimc", ctx);
1773         if (ret < 0) {
1774                 dev_err(dev, "failed to request irq.\n");
1775                 return ret;
1776         }
1777
1778         ret = fimc_setup_clocks(ctx);
1779         if (ret < 0)
1780                 return ret;
1781
1782         ippdrv = &ctx->ippdrv;
1783         ippdrv->ops[EXYNOS_DRM_OPS_SRC] = &fimc_src_ops;
1784         ippdrv->ops[EXYNOS_DRM_OPS_DST] = &fimc_dst_ops;
1785         ippdrv->check_property = fimc_ippdrv_check_property;
1786         ippdrv->reset = fimc_ippdrv_reset;
1787         ippdrv->start = fimc_ippdrv_start;
1788         ippdrv->stop = fimc_ippdrv_stop;
1789         ret = fimc_init_prop_list(ippdrv);
1790         if (ret < 0) {
1791                 dev_err(dev, "failed to init property list.\n");
1792                 goto err_put_clk;
1793         }
1794
1795         DRM_DEBUG_KMS("id[%d]ippdrv[0x%x]\n", ctx->id, (int)ippdrv);
1796
1797         mutex_init(&ctx->lock);
1798         platform_set_drvdata(pdev, ctx);
1799
1800         pm_runtime_set_active(dev);
1801         pm_runtime_enable(dev);
1802
1803         ret = exynos_drm_ippdrv_register(ippdrv);
1804         if (ret < 0) {
1805                 dev_err(dev, "failed to register drm fimc device.\n");
1806                 goto err_pm_dis;
1807         }
1808
1809         dev_info(dev, "drm fimc registered successfully.\n");
1810
1811         return 0;
1812
1813 err_pm_dis:
1814         pm_runtime_disable(dev);
1815 err_put_clk:
1816         fimc_put_clocks(ctx);
1817
1818         return ret;
1819 }
1820
1821 static int fimc_remove(struct platform_device *pdev)
1822 {
1823         struct device *dev = &pdev->dev;
1824         struct fimc_context *ctx = get_fimc_context(dev);
1825         struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
1826
1827         exynos_drm_ippdrv_unregister(ippdrv);
1828         mutex_destroy(&ctx->lock);
1829
1830         fimc_put_clocks(ctx);
1831         pm_runtime_set_suspended(dev);
1832         pm_runtime_disable(dev);
1833
1834         return 0;
1835 }
1836
1837 #ifdef CONFIG_PM_SLEEP
1838 static int fimc_suspend(struct device *dev)
1839 {
1840         struct fimc_context *ctx = get_fimc_context(dev);
1841
1842         DRM_DEBUG_KMS("id[%d]\n", ctx->id);
1843
1844         if (pm_runtime_suspended(dev))
1845                 return 0;
1846
1847         return fimc_clk_ctrl(ctx, false);
1848 }
1849
1850 static int fimc_resume(struct device *dev)
1851 {
1852         struct fimc_context *ctx = get_fimc_context(dev);
1853
1854         DRM_DEBUG_KMS("id[%d]\n", ctx->id);
1855
1856         if (!pm_runtime_suspended(dev))
1857                 return fimc_clk_ctrl(ctx, true);
1858
1859         return 0;
1860 }
1861 #endif
1862
1863 #ifdef CONFIG_PM_RUNTIME
1864 static int fimc_runtime_suspend(struct device *dev)
1865 {
1866         struct fimc_context *ctx = get_fimc_context(dev);
1867
1868         DRM_DEBUG_KMS("id[%d]\n", ctx->id);
1869
1870         return  fimc_clk_ctrl(ctx, false);
1871 }
1872
1873 static int fimc_runtime_resume(struct device *dev)
1874 {
1875         struct fimc_context *ctx = get_fimc_context(dev);
1876
1877         DRM_DEBUG_KMS("id[%d]\n", ctx->id);
1878
1879         return  fimc_clk_ctrl(ctx, true);
1880 }
1881 #endif
1882
1883 static const struct dev_pm_ops fimc_pm_ops = {
1884         SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend, fimc_resume)
1885         SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
1886 };
1887
1888 static const struct of_device_id fimc_of_match[] = {
1889         { .compatible = "samsung,exynos4210-fimc" },
1890         { .compatible = "samsung,exynos4212-fimc" },
1891         { },
1892 };
1893
1894 struct platform_driver fimc_driver = {
1895         .probe          = fimc_probe,
1896         .remove         = fimc_remove,
1897         .driver         = {
1898                 .of_match_table = fimc_of_match,
1899                 .name   = "exynos-drm-fimc",
1900                 .owner  = THIS_MODULE,
1901                 .pm     = &fimc_pm_ops,
1902         },
1903 };
1904