076a97eebad9e9b7555399f785572aefb1cd2259
[linux-2.6-microblaze.git] / drivers / gpu / drm / exynos / exynos_mixer.c
1 /*
2  * Copyright (C) 2011 Samsung Electronics Co.Ltd
3  * Authors:
4  * Seung-Woo Kim <sw0312.kim@samsung.com>
5  *      Inki Dae <inki.dae@samsung.com>
6  *      Joonyoung Shim <jy0922.shim@samsung.com>
7  *
8  * Based on drivers/media/video/s5p-tv/mixer_reg.c
9  *
10  * This program is free software; you can redistribute  it and/or modify it
11  * under  the terms of  the GNU General  Public License as published by the
12  * Free Software Foundation;  either version 2 of the  License, or (at your
13  * option) any later version.
14  *
15  */
16
17 #include <drm/drmP.h>
18
19 #include "regs-mixer.h"
20 #include "regs-vp.h"
21
22 #include <linux/kernel.h>
23 #include <linux/spinlock.h>
24 #include <linux/wait.h>
25 #include <linux/i2c.h>
26 #include <linux/platform_device.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/clk.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/of.h>
34 #include <linux/component.h>
35
36 #include <drm/exynos_drm.h>
37
38 #include "exynos_drm_drv.h"
39 #include "exynos_drm_crtc.h"
40 #include "exynos_drm_plane.h"
41 #include "exynos_drm_iommu.h"
42 #include "exynos_mixer.h"
43
44 #define MIXER_WIN_NR            3
45 #define MIXER_DEFAULT_WIN       0
46
47 /* The pixelformats that are natively supported by the mixer. */
48 #define MXR_FORMAT_RGB565       4
49 #define MXR_FORMAT_ARGB1555     5
50 #define MXR_FORMAT_ARGB4444     6
51 #define MXR_FORMAT_ARGB8888     7
52
53 struct mixer_resources {
54         int                     irq;
55         void __iomem            *mixer_regs;
56         void __iomem            *vp_regs;
57         spinlock_t              reg_slock;
58         struct clk              *mixer;
59         struct clk              *vp;
60         struct clk              *hdmi;
61         struct clk              *sclk_mixer;
62         struct clk              *sclk_hdmi;
63         struct clk              *mout_mixer;
64 };
65
66 enum mixer_version_id {
67         MXR_VER_0_0_0_16,
68         MXR_VER_16_0_33_0,
69         MXR_VER_128_0_0_184,
70 };
71
72 enum mixer_flag_bits {
73         MXR_BIT_POWERED,
74         MXR_BIT_VSYNC,
75 };
76
77 struct mixer_context {
78         struct platform_device *pdev;
79         struct device           *dev;
80         struct drm_device       *drm_dev;
81         struct exynos_drm_crtc  *crtc;
82         struct exynos_drm_plane planes[MIXER_WIN_NR];
83         int                     pipe;
84         unsigned long           flags;
85         bool                    interlace;
86         bool                    vp_enabled;
87         bool                    has_sclk;
88
89         struct mixer_resources  mixer_res;
90         enum mixer_version_id   mxr_ver;
91         wait_queue_head_t       wait_vsync_queue;
92         atomic_t                wait_vsync_event;
93 };
94
95 struct mixer_drv_data {
96         enum mixer_version_id   version;
97         bool                                    is_vp_enabled;
98         bool                                    has_sclk;
99 };
100
101 static const u8 filter_y_horiz_tap8[] = {
102         0,      -1,     -1,     -1,     -1,     -1,     -1,     -1,
103         -1,     -1,     -1,     -1,     -1,     0,      0,      0,
104         0,      2,      4,      5,      6,      6,      6,      6,
105         6,      5,      5,      4,      3,      2,      1,      1,
106         0,      -6,     -12,    -16,    -18,    -20,    -21,    -20,
107         -20,    -18,    -16,    -13,    -10,    -8,     -5,     -2,
108         127,    126,    125,    121,    114,    107,    99,     89,
109         79,     68,     57,     46,     35,     25,     16,     8,
110 };
111
112 static const u8 filter_y_vert_tap4[] = {
113         0,      -3,     -6,     -8,     -8,     -8,     -8,     -7,
114         -6,     -5,     -4,     -3,     -2,     -1,     -1,     0,
115         127,    126,    124,    118,    111,    102,    92,     81,
116         70,     59,     48,     37,     27,     19,     11,     5,
117         0,      5,      11,     19,     27,     37,     48,     59,
118         70,     81,     92,     102,    111,    118,    124,    126,
119         0,      0,      -1,     -1,     -2,     -3,     -4,     -5,
120         -6,     -7,     -8,     -8,     -8,     -8,     -6,     -3,
121 };
122
123 static const u8 filter_cr_horiz_tap4[] = {
124         0,      -3,     -6,     -8,     -8,     -8,     -8,     -7,
125         -6,     -5,     -4,     -3,     -2,     -1,     -1,     0,
126         127,    126,    124,    118,    111,    102,    92,     81,
127         70,     59,     48,     37,     27,     19,     11,     5,
128 };
129
130 static inline u32 vp_reg_read(struct mixer_resources *res, u32 reg_id)
131 {
132         return readl(res->vp_regs + reg_id);
133 }
134
135 static inline void vp_reg_write(struct mixer_resources *res, u32 reg_id,
136                                  u32 val)
137 {
138         writel(val, res->vp_regs + reg_id);
139 }
140
141 static inline void vp_reg_writemask(struct mixer_resources *res, u32 reg_id,
142                                  u32 val, u32 mask)
143 {
144         u32 old = vp_reg_read(res, reg_id);
145
146         val = (val & mask) | (old & ~mask);
147         writel(val, res->vp_regs + reg_id);
148 }
149
150 static inline u32 mixer_reg_read(struct mixer_resources *res, u32 reg_id)
151 {
152         return readl(res->mixer_regs + reg_id);
153 }
154
155 static inline void mixer_reg_write(struct mixer_resources *res, u32 reg_id,
156                                  u32 val)
157 {
158         writel(val, res->mixer_regs + reg_id);
159 }
160
161 static inline void mixer_reg_writemask(struct mixer_resources *res,
162                                  u32 reg_id, u32 val, u32 mask)
163 {
164         u32 old = mixer_reg_read(res, reg_id);
165
166         val = (val & mask) | (old & ~mask);
167         writel(val, res->mixer_regs + reg_id);
168 }
169
170 static void mixer_regs_dump(struct mixer_context *ctx)
171 {
172 #define DUMPREG(reg_id) \
173 do { \
174         DRM_DEBUG_KMS(#reg_id " = %08x\n", \
175                 (u32)readl(ctx->mixer_res.mixer_regs + reg_id)); \
176 } while (0)
177
178         DUMPREG(MXR_STATUS);
179         DUMPREG(MXR_CFG);
180         DUMPREG(MXR_INT_EN);
181         DUMPREG(MXR_INT_STATUS);
182
183         DUMPREG(MXR_LAYER_CFG);
184         DUMPREG(MXR_VIDEO_CFG);
185
186         DUMPREG(MXR_GRAPHIC0_CFG);
187         DUMPREG(MXR_GRAPHIC0_BASE);
188         DUMPREG(MXR_GRAPHIC0_SPAN);
189         DUMPREG(MXR_GRAPHIC0_WH);
190         DUMPREG(MXR_GRAPHIC0_SXY);
191         DUMPREG(MXR_GRAPHIC0_DXY);
192
193         DUMPREG(MXR_GRAPHIC1_CFG);
194         DUMPREG(MXR_GRAPHIC1_BASE);
195         DUMPREG(MXR_GRAPHIC1_SPAN);
196         DUMPREG(MXR_GRAPHIC1_WH);
197         DUMPREG(MXR_GRAPHIC1_SXY);
198         DUMPREG(MXR_GRAPHIC1_DXY);
199 #undef DUMPREG
200 }
201
202 static void vp_regs_dump(struct mixer_context *ctx)
203 {
204 #define DUMPREG(reg_id) \
205 do { \
206         DRM_DEBUG_KMS(#reg_id " = %08x\n", \
207                 (u32) readl(ctx->mixer_res.vp_regs + reg_id)); \
208 } while (0)
209
210         DUMPREG(VP_ENABLE);
211         DUMPREG(VP_SRESET);
212         DUMPREG(VP_SHADOW_UPDATE);
213         DUMPREG(VP_FIELD_ID);
214         DUMPREG(VP_MODE);
215         DUMPREG(VP_IMG_SIZE_Y);
216         DUMPREG(VP_IMG_SIZE_C);
217         DUMPREG(VP_PER_RATE_CTRL);
218         DUMPREG(VP_TOP_Y_PTR);
219         DUMPREG(VP_BOT_Y_PTR);
220         DUMPREG(VP_TOP_C_PTR);
221         DUMPREG(VP_BOT_C_PTR);
222         DUMPREG(VP_ENDIAN_MODE);
223         DUMPREG(VP_SRC_H_POSITION);
224         DUMPREG(VP_SRC_V_POSITION);
225         DUMPREG(VP_SRC_WIDTH);
226         DUMPREG(VP_SRC_HEIGHT);
227         DUMPREG(VP_DST_H_POSITION);
228         DUMPREG(VP_DST_V_POSITION);
229         DUMPREG(VP_DST_WIDTH);
230         DUMPREG(VP_DST_HEIGHT);
231         DUMPREG(VP_H_RATIO);
232         DUMPREG(VP_V_RATIO);
233
234 #undef DUMPREG
235 }
236
237 static inline void vp_filter_set(struct mixer_resources *res,
238                 int reg_id, const u8 *data, unsigned int size)
239 {
240         /* assure 4-byte align */
241         BUG_ON(size & 3);
242         for (; size; size -= 4, reg_id += 4, data += 4) {
243                 u32 val = (data[0] << 24) |  (data[1] << 16) |
244                         (data[2] << 8) | data[3];
245                 vp_reg_write(res, reg_id, val);
246         }
247 }
248
249 static void vp_default_filter(struct mixer_resources *res)
250 {
251         vp_filter_set(res, VP_POLY8_Y0_LL,
252                 filter_y_horiz_tap8, sizeof(filter_y_horiz_tap8));
253         vp_filter_set(res, VP_POLY4_Y0_LL,
254                 filter_y_vert_tap4, sizeof(filter_y_vert_tap4));
255         vp_filter_set(res, VP_POLY4_C0_LL,
256                 filter_cr_horiz_tap4, sizeof(filter_cr_horiz_tap4));
257 }
258
259 static void mixer_vsync_set_update(struct mixer_context *ctx, bool enable)
260 {
261         struct mixer_resources *res = &ctx->mixer_res;
262
263         /* block update on vsync */
264         mixer_reg_writemask(res, MXR_STATUS, enable ?
265                         MXR_STATUS_SYNC_ENABLE : 0, MXR_STATUS_SYNC_ENABLE);
266
267         if (ctx->vp_enabled)
268                 vp_reg_write(res, VP_SHADOW_UPDATE, enable ?
269                         VP_SHADOW_UPDATE_ENABLE : 0);
270 }
271
272 static void mixer_cfg_scan(struct mixer_context *ctx, unsigned int height)
273 {
274         struct mixer_resources *res = &ctx->mixer_res;
275         u32 val;
276
277         /* choosing between interlace and progressive mode */
278         val = (ctx->interlace ? MXR_CFG_SCAN_INTERLACE :
279                                 MXR_CFG_SCAN_PROGRESSIVE);
280
281         if (ctx->mxr_ver != MXR_VER_128_0_0_184) {
282                 /* choosing between proper HD and SD mode */
283                 if (height <= 480)
284                         val |= MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD;
285                 else if (height <= 576)
286                         val |= MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD;
287                 else if (height <= 720)
288                         val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
289                 else if (height <= 1080)
290                         val |= MXR_CFG_SCAN_HD_1080 | MXR_CFG_SCAN_HD;
291                 else
292                         val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
293         }
294
295         mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_SCAN_MASK);
296 }
297
298 static void mixer_cfg_rgb_fmt(struct mixer_context *ctx, unsigned int height)
299 {
300         struct mixer_resources *res = &ctx->mixer_res;
301         u32 val;
302
303         if (height == 480) {
304                 val = MXR_CFG_RGB601_0_255;
305         } else if (height == 576) {
306                 val = MXR_CFG_RGB601_0_255;
307         } else if (height == 720) {
308                 val = MXR_CFG_RGB709_16_235;
309                 mixer_reg_write(res, MXR_CM_COEFF_Y,
310                                 (1 << 30) | (94 << 20) | (314 << 10) |
311                                 (32 << 0));
312                 mixer_reg_write(res, MXR_CM_COEFF_CB,
313                                 (972 << 20) | (851 << 10) | (225 << 0));
314                 mixer_reg_write(res, MXR_CM_COEFF_CR,
315                                 (225 << 20) | (820 << 10) | (1004 << 0));
316         } else if (height == 1080) {
317                 val = MXR_CFG_RGB709_16_235;
318                 mixer_reg_write(res, MXR_CM_COEFF_Y,
319                                 (1 << 30) | (94 << 20) | (314 << 10) |
320                                 (32 << 0));
321                 mixer_reg_write(res, MXR_CM_COEFF_CB,
322                                 (972 << 20) | (851 << 10) | (225 << 0));
323                 mixer_reg_write(res, MXR_CM_COEFF_CR,
324                                 (225 << 20) | (820 << 10) | (1004 << 0));
325         } else {
326                 val = MXR_CFG_RGB709_16_235;
327                 mixer_reg_write(res, MXR_CM_COEFF_Y,
328                                 (1 << 30) | (94 << 20) | (314 << 10) |
329                                 (32 << 0));
330                 mixer_reg_write(res, MXR_CM_COEFF_CB,
331                                 (972 << 20) | (851 << 10) | (225 << 0));
332                 mixer_reg_write(res, MXR_CM_COEFF_CR,
333                                 (225 << 20) | (820 << 10) | (1004 << 0));
334         }
335
336         mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
337 }
338
339 static void mixer_cfg_layer(struct mixer_context *ctx, unsigned int win,
340                                 bool enable)
341 {
342         struct mixer_resources *res = &ctx->mixer_res;
343         u32 val = enable ? ~0 : 0;
344
345         switch (win) {
346         case 0:
347                 mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
348                 break;
349         case 1:
350                 mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
351                 break;
352         case 2:
353                 if (ctx->vp_enabled) {
354                         vp_reg_writemask(res, VP_ENABLE, val, VP_ENABLE_ON);
355                         mixer_reg_writemask(res, MXR_CFG, val,
356                                 MXR_CFG_VP_ENABLE);
357
358                         /* control blending of graphic layer 0 */
359                         mixer_reg_writemask(res, MXR_GRAPHIC_CFG(0), val,
360                                         MXR_GRP_CFG_BLEND_PRE_MUL |
361                                         MXR_GRP_CFG_PIXEL_BLEND_EN);
362                 }
363                 break;
364         }
365 }
366
367 static void mixer_run(struct mixer_context *ctx)
368 {
369         struct mixer_resources *res = &ctx->mixer_res;
370
371         mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
372 }
373
374 static void mixer_stop(struct mixer_context *ctx)
375 {
376         struct mixer_resources *res = &ctx->mixer_res;
377         int timeout = 20;
378
379         mixer_reg_writemask(res, MXR_STATUS, 0, MXR_STATUS_REG_RUN);
380
381         while (!(mixer_reg_read(res, MXR_STATUS) & MXR_STATUS_REG_IDLE) &&
382                         --timeout)
383                 usleep_range(10000, 12000);
384 }
385
386 static void vp_video_buffer(struct mixer_context *ctx,
387                             struct exynos_drm_plane *plane)
388 {
389         struct mixer_resources *res = &ctx->mixer_res;
390         struct drm_plane_state *state = plane->base.state;
391         struct drm_framebuffer *fb = state->fb;
392         struct drm_display_mode *mode = &state->crtc->mode;
393         unsigned long flags;
394         dma_addr_t luma_addr[2], chroma_addr[2];
395         bool tiled_mode = false;
396         bool crcb_mode = false;
397         u32 val;
398
399         switch (fb->pixel_format) {
400         case DRM_FORMAT_NV12:
401                 crcb_mode = false;
402                 break;
403         case DRM_FORMAT_NV21:
404                 crcb_mode = true;
405                 break;
406         default:
407                 DRM_ERROR("pixel format for vp is wrong [%d].\n",
408                                 fb->pixel_format);
409                 return;
410         }
411
412         luma_addr[0] = plane->dma_addr[0];
413         chroma_addr[0] = plane->dma_addr[1];
414
415         if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
416                 ctx->interlace = true;
417                 if (tiled_mode) {
418                         luma_addr[1] = luma_addr[0] + 0x40;
419                         chroma_addr[1] = chroma_addr[0] + 0x40;
420                 } else {
421                         luma_addr[1] = luma_addr[0] + fb->pitches[0];
422                         chroma_addr[1] = chroma_addr[0] + fb->pitches[0];
423                 }
424         } else {
425                 ctx->interlace = false;
426                 luma_addr[1] = 0;
427                 chroma_addr[1] = 0;
428         }
429
430         spin_lock_irqsave(&res->reg_slock, flags);
431         mixer_vsync_set_update(ctx, false);
432
433         /* interlace or progressive scan mode */
434         val = (ctx->interlace ? ~0 : 0);
435         vp_reg_writemask(res, VP_MODE, val, VP_MODE_LINE_SKIP);
436
437         /* setup format */
438         val = (crcb_mode ? VP_MODE_NV21 : VP_MODE_NV12);
439         val |= (tiled_mode ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
440         vp_reg_writemask(res, VP_MODE, val, VP_MODE_FMT_MASK);
441
442         /* setting size of input image */
443         vp_reg_write(res, VP_IMG_SIZE_Y, VP_IMG_HSIZE(fb->pitches[0]) |
444                 VP_IMG_VSIZE(fb->height));
445         /* chroma height has to reduced by 2 to avoid chroma distorions */
446         vp_reg_write(res, VP_IMG_SIZE_C, VP_IMG_HSIZE(fb->pitches[0]) |
447                 VP_IMG_VSIZE(fb->height / 2));
448
449         vp_reg_write(res, VP_SRC_WIDTH, plane->src_width);
450         vp_reg_write(res, VP_SRC_HEIGHT, plane->src_height);
451         vp_reg_write(res, VP_SRC_H_POSITION,
452                         VP_SRC_H_POSITION_VAL(plane->src_x));
453         vp_reg_write(res, VP_SRC_V_POSITION, plane->src_y);
454
455         vp_reg_write(res, VP_DST_WIDTH, plane->crtc_width);
456         vp_reg_write(res, VP_DST_H_POSITION, plane->crtc_x);
457         if (ctx->interlace) {
458                 vp_reg_write(res, VP_DST_HEIGHT, plane->crtc_height / 2);
459                 vp_reg_write(res, VP_DST_V_POSITION, plane->crtc_y / 2);
460         } else {
461                 vp_reg_write(res, VP_DST_HEIGHT, plane->crtc_height);
462                 vp_reg_write(res, VP_DST_V_POSITION, plane->crtc_y);
463         }
464
465         vp_reg_write(res, VP_H_RATIO, plane->h_ratio);
466         vp_reg_write(res, VP_V_RATIO, plane->v_ratio);
467
468         vp_reg_write(res, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
469
470         /* set buffer address to vp */
471         vp_reg_write(res, VP_TOP_Y_PTR, luma_addr[0]);
472         vp_reg_write(res, VP_BOT_Y_PTR, luma_addr[1]);
473         vp_reg_write(res, VP_TOP_C_PTR, chroma_addr[0]);
474         vp_reg_write(res, VP_BOT_C_PTR, chroma_addr[1]);
475
476         mixer_cfg_scan(ctx, mode->vdisplay);
477         mixer_cfg_rgb_fmt(ctx, mode->vdisplay);
478         mixer_cfg_layer(ctx, plane->zpos, true);
479         mixer_run(ctx);
480
481         mixer_vsync_set_update(ctx, true);
482         spin_unlock_irqrestore(&res->reg_slock, flags);
483
484         mixer_regs_dump(ctx);
485         vp_regs_dump(ctx);
486 }
487
488 static void mixer_layer_update(struct mixer_context *ctx)
489 {
490         struct mixer_resources *res = &ctx->mixer_res;
491
492         mixer_reg_writemask(res, MXR_CFG, ~0, MXR_CFG_LAYER_UPDATE);
493 }
494
495 static int mixer_setup_scale(const struct exynos_drm_plane *plane,
496                 unsigned int *x_ratio, unsigned int *y_ratio)
497 {
498         if (plane->crtc_width != plane->src_width) {
499                 if (plane->crtc_width == 2 * plane->src_width)
500                         *x_ratio = 1;
501                 else
502                         goto fail;
503         }
504
505         if (plane->crtc_height != plane->src_height) {
506                 if (plane->crtc_height == 2 * plane->src_height)
507                         *y_ratio = 1;
508                 else
509                         goto fail;
510         }
511
512         return 0;
513
514 fail:
515         DRM_DEBUG_KMS("only 2x width/height scaling of plane supported\n");
516         return -ENOTSUPP;
517 }
518
519 static void mixer_graph_buffer(struct mixer_context *ctx,
520                                struct exynos_drm_plane *plane)
521 {
522         struct mixer_resources *res = &ctx->mixer_res;
523         struct drm_plane_state *state = plane->base.state;
524         struct drm_framebuffer *fb = state->fb;
525         struct drm_display_mode *mode = &state->crtc->mode;
526         unsigned long flags;
527         unsigned int win = plane->zpos;
528         unsigned int x_ratio = 0, y_ratio = 0;
529         unsigned int src_x_offset, src_y_offset, dst_x_offset, dst_y_offset;
530         dma_addr_t dma_addr;
531         unsigned int fmt;
532         u32 val;
533
534         switch (fb->pixel_format) {
535         case DRM_FORMAT_XRGB4444:
536                 fmt = MXR_FORMAT_ARGB4444;
537                 break;
538
539         case DRM_FORMAT_XRGB1555:
540                 fmt = MXR_FORMAT_ARGB1555;
541                 break;
542
543         case DRM_FORMAT_RGB565:
544                 fmt = MXR_FORMAT_RGB565;
545                 break;
546
547         case DRM_FORMAT_XRGB8888:
548         case DRM_FORMAT_ARGB8888:
549                 fmt = MXR_FORMAT_ARGB8888;
550                 break;
551
552         default:
553                 DRM_DEBUG_KMS("pixelformat unsupported by mixer\n");
554                 return;
555         }
556
557         /* check if mixer supports requested scaling setup */
558         if (mixer_setup_scale(plane, &x_ratio, &y_ratio))
559                 return;
560
561         dst_x_offset = plane->crtc_x;
562         dst_y_offset = plane->crtc_y;
563
564         /* converting dma address base and source offset */
565         dma_addr = plane->dma_addr[0]
566                 + (plane->src_x * fb->bits_per_pixel >> 3)
567                 + (plane->src_y * fb->pitches[0]);
568         src_x_offset = 0;
569         src_y_offset = 0;
570
571         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
572                 ctx->interlace = true;
573         else
574                 ctx->interlace = false;
575
576         spin_lock_irqsave(&res->reg_slock, flags);
577         mixer_vsync_set_update(ctx, false);
578
579         /* setup format */
580         mixer_reg_writemask(res, MXR_GRAPHIC_CFG(win),
581                 MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
582
583         /* setup geometry */
584         mixer_reg_write(res, MXR_GRAPHIC_SPAN(win),
585                         fb->pitches[0] / (fb->bits_per_pixel >> 3));
586
587         /* setup display size */
588         if (ctx->mxr_ver == MXR_VER_128_0_0_184 &&
589                 win == MIXER_DEFAULT_WIN) {
590                 val  = MXR_MXR_RES_HEIGHT(mode->vdisplay);
591                 val |= MXR_MXR_RES_WIDTH(mode->hdisplay);
592                 mixer_reg_write(res, MXR_RESOLUTION, val);
593         }
594
595         val  = MXR_GRP_WH_WIDTH(plane->src_width);
596         val |= MXR_GRP_WH_HEIGHT(plane->src_height);
597         val |= MXR_GRP_WH_H_SCALE(x_ratio);
598         val |= MXR_GRP_WH_V_SCALE(y_ratio);
599         mixer_reg_write(res, MXR_GRAPHIC_WH(win), val);
600
601         /* setup offsets in source image */
602         val  = MXR_GRP_SXY_SX(src_x_offset);
603         val |= MXR_GRP_SXY_SY(src_y_offset);
604         mixer_reg_write(res, MXR_GRAPHIC_SXY(win), val);
605
606         /* setup offsets in display image */
607         val  = MXR_GRP_DXY_DX(dst_x_offset);
608         val |= MXR_GRP_DXY_DY(dst_y_offset);
609         mixer_reg_write(res, MXR_GRAPHIC_DXY(win), val);
610
611         /* set buffer address to mixer */
612         mixer_reg_write(res, MXR_GRAPHIC_BASE(win), dma_addr);
613
614         mixer_cfg_scan(ctx, mode->vdisplay);
615         mixer_cfg_rgb_fmt(ctx, mode->vdisplay);
616         mixer_cfg_layer(ctx, win, true);
617
618         /* layer update mandatory for mixer 16.0.33.0 */
619         if (ctx->mxr_ver == MXR_VER_16_0_33_0 ||
620                 ctx->mxr_ver == MXR_VER_128_0_0_184)
621                 mixer_layer_update(ctx);
622
623         mixer_run(ctx);
624
625         mixer_vsync_set_update(ctx, true);
626         spin_unlock_irqrestore(&res->reg_slock, flags);
627
628         mixer_regs_dump(ctx);
629 }
630
631 static void vp_win_reset(struct mixer_context *ctx)
632 {
633         struct mixer_resources *res = &ctx->mixer_res;
634         int tries = 100;
635
636         vp_reg_write(res, VP_SRESET, VP_SRESET_PROCESSING);
637         for (tries = 100; tries; --tries) {
638                 /* waiting until VP_SRESET_PROCESSING is 0 */
639                 if (~vp_reg_read(res, VP_SRESET) & VP_SRESET_PROCESSING)
640                         break;
641                 usleep_range(10000, 12000);
642         }
643         WARN(tries == 0, "failed to reset Video Processor\n");
644 }
645
646 static void mixer_win_reset(struct mixer_context *ctx)
647 {
648         struct mixer_resources *res = &ctx->mixer_res;
649         unsigned long flags;
650         u32 val; /* value stored to register */
651
652         spin_lock_irqsave(&res->reg_slock, flags);
653         mixer_vsync_set_update(ctx, false);
654
655         mixer_reg_writemask(res, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
656
657         /* set output in RGB888 mode */
658         mixer_reg_writemask(res, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
659
660         /* 16 beat burst in DMA */
661         mixer_reg_writemask(res, MXR_STATUS, MXR_STATUS_16_BURST,
662                 MXR_STATUS_BURST_MASK);
663
664         /* setting default layer priority: layer1 > layer0 > video
665          * because typical usage scenario would be
666          * layer1 - OSD
667          * layer0 - framebuffer
668          * video - video overlay
669          */
670         val = MXR_LAYER_CFG_GRP1_VAL(3);
671         val |= MXR_LAYER_CFG_GRP0_VAL(2);
672         if (ctx->vp_enabled)
673                 val |= MXR_LAYER_CFG_VP_VAL(1);
674         mixer_reg_write(res, MXR_LAYER_CFG, val);
675
676         /* setting background color */
677         mixer_reg_write(res, MXR_BG_COLOR0, 0x008080);
678         mixer_reg_write(res, MXR_BG_COLOR1, 0x008080);
679         mixer_reg_write(res, MXR_BG_COLOR2, 0x008080);
680
681         /* setting graphical layers */
682         val  = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
683         val |= MXR_GRP_CFG_WIN_BLEND_EN;
684         val |= MXR_GRP_CFG_ALPHA_VAL(0xff); /* non-transparent alpha */
685
686         /* Don't blend layer 0 onto the mixer background */
687         mixer_reg_write(res, MXR_GRAPHIC_CFG(0), val);
688
689         /* Blend layer 1 into layer 0 */
690         val |= MXR_GRP_CFG_BLEND_PRE_MUL;
691         val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
692         mixer_reg_write(res, MXR_GRAPHIC_CFG(1), val);
693
694         /* setting video layers */
695         val = MXR_GRP_CFG_ALPHA_VAL(0);
696         mixer_reg_write(res, MXR_VIDEO_CFG, val);
697
698         if (ctx->vp_enabled) {
699                 /* configuration of Video Processor Registers */
700                 vp_win_reset(ctx);
701                 vp_default_filter(res);
702         }
703
704         /* disable all layers */
705         mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
706         mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
707         if (ctx->vp_enabled)
708                 mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
709
710         mixer_vsync_set_update(ctx, true);
711         spin_unlock_irqrestore(&res->reg_slock, flags);
712 }
713
714 static irqreturn_t mixer_irq_handler(int irq, void *arg)
715 {
716         struct mixer_context *ctx = arg;
717         struct mixer_resources *res = &ctx->mixer_res;
718         u32 val, base, shadow;
719
720         spin_lock(&res->reg_slock);
721
722         /* read interrupt status for handling and clearing flags for VSYNC */
723         val = mixer_reg_read(res, MXR_INT_STATUS);
724
725         /* handling VSYNC */
726         if (val & MXR_INT_STATUS_VSYNC) {
727                 /* vsync interrupt use different bit for read and clear */
728                 val |= MXR_INT_CLEAR_VSYNC;
729                 val &= ~MXR_INT_STATUS_VSYNC;
730
731                 /* interlace scan need to check shadow register */
732                 if (ctx->interlace) {
733                         base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
734                         shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
735                         if (base != shadow)
736                                 goto out;
737
738                         base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
739                         shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
740                         if (base != shadow)
741                                 goto out;
742                 }
743
744                 drm_crtc_handle_vblank(&ctx->crtc->base);
745                 exynos_drm_crtc_finish_pageflip(ctx->crtc);
746
747                 /* set wait vsync event to zero and wake up queue. */
748                 if (atomic_read(&ctx->wait_vsync_event)) {
749                         atomic_set(&ctx->wait_vsync_event, 0);
750                         wake_up(&ctx->wait_vsync_queue);
751                 }
752         }
753
754 out:
755         /* clear interrupts */
756         mixer_reg_write(res, MXR_INT_STATUS, val);
757
758         spin_unlock(&res->reg_slock);
759
760         return IRQ_HANDLED;
761 }
762
763 static int mixer_resources_init(struct mixer_context *mixer_ctx)
764 {
765         struct device *dev = &mixer_ctx->pdev->dev;
766         struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
767         struct resource *res;
768         int ret;
769
770         spin_lock_init(&mixer_res->reg_slock);
771
772         mixer_res->mixer = devm_clk_get(dev, "mixer");
773         if (IS_ERR(mixer_res->mixer)) {
774                 dev_err(dev, "failed to get clock 'mixer'\n");
775                 return -ENODEV;
776         }
777
778         mixer_res->hdmi = devm_clk_get(dev, "hdmi");
779         if (IS_ERR(mixer_res->hdmi)) {
780                 dev_err(dev, "failed to get clock 'hdmi'\n");
781                 return PTR_ERR(mixer_res->hdmi);
782         }
783
784         mixer_res->sclk_hdmi = devm_clk_get(dev, "sclk_hdmi");
785         if (IS_ERR(mixer_res->sclk_hdmi)) {
786                 dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
787                 return -ENODEV;
788         }
789         res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 0);
790         if (res == NULL) {
791                 dev_err(dev, "get memory resource failed.\n");
792                 return -ENXIO;
793         }
794
795         mixer_res->mixer_regs = devm_ioremap(dev, res->start,
796                                                         resource_size(res));
797         if (mixer_res->mixer_regs == NULL) {
798                 dev_err(dev, "register mapping failed.\n");
799                 return -ENXIO;
800         }
801
802         res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_IRQ, 0);
803         if (res == NULL) {
804                 dev_err(dev, "get interrupt resource failed.\n");
805                 return -ENXIO;
806         }
807
808         ret = devm_request_irq(dev, res->start, mixer_irq_handler,
809                                                 0, "drm_mixer", mixer_ctx);
810         if (ret) {
811                 dev_err(dev, "request interrupt failed.\n");
812                 return ret;
813         }
814         mixer_res->irq = res->start;
815
816         return 0;
817 }
818
819 static int vp_resources_init(struct mixer_context *mixer_ctx)
820 {
821         struct device *dev = &mixer_ctx->pdev->dev;
822         struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
823         struct resource *res;
824
825         mixer_res->vp = devm_clk_get(dev, "vp");
826         if (IS_ERR(mixer_res->vp)) {
827                 dev_err(dev, "failed to get clock 'vp'\n");
828                 return -ENODEV;
829         }
830
831         if (mixer_ctx->has_sclk) {
832                 mixer_res->sclk_mixer = devm_clk_get(dev, "sclk_mixer");
833                 if (IS_ERR(mixer_res->sclk_mixer)) {
834                         dev_err(dev, "failed to get clock 'sclk_mixer'\n");
835                         return -ENODEV;
836                 }
837                 mixer_res->mout_mixer = devm_clk_get(dev, "mout_mixer");
838                 if (IS_ERR(mixer_res->mout_mixer)) {
839                         dev_err(dev, "failed to get clock 'mout_mixer'\n");
840                         return -ENODEV;
841                 }
842
843                 if (mixer_res->sclk_hdmi && mixer_res->mout_mixer)
844                         clk_set_parent(mixer_res->mout_mixer,
845                                        mixer_res->sclk_hdmi);
846         }
847
848         res = platform_get_resource(mixer_ctx->pdev, IORESOURCE_MEM, 1);
849         if (res == NULL) {
850                 dev_err(dev, "get memory resource failed.\n");
851                 return -ENXIO;
852         }
853
854         mixer_res->vp_regs = devm_ioremap(dev, res->start,
855                                                         resource_size(res));
856         if (mixer_res->vp_regs == NULL) {
857                 dev_err(dev, "register mapping failed.\n");
858                 return -ENXIO;
859         }
860
861         return 0;
862 }
863
864 static int mixer_initialize(struct mixer_context *mixer_ctx,
865                         struct drm_device *drm_dev)
866 {
867         int ret;
868         struct exynos_drm_private *priv;
869         priv = drm_dev->dev_private;
870
871         mixer_ctx->drm_dev = drm_dev;
872         mixer_ctx->pipe = priv->pipe++;
873
874         /* acquire resources: regs, irqs, clocks */
875         ret = mixer_resources_init(mixer_ctx);
876         if (ret) {
877                 DRM_ERROR("mixer_resources_init failed ret=%d\n", ret);
878                 return ret;
879         }
880
881         if (mixer_ctx->vp_enabled) {
882                 /* acquire vp resources: regs, irqs, clocks */
883                 ret = vp_resources_init(mixer_ctx);
884                 if (ret) {
885                         DRM_ERROR("vp_resources_init failed ret=%d\n", ret);
886                         return ret;
887                 }
888         }
889
890         ret = drm_iommu_attach_device(drm_dev, mixer_ctx->dev);
891         if (ret)
892                 priv->pipe--;
893
894         return ret;
895 }
896
897 static void mixer_ctx_remove(struct mixer_context *mixer_ctx)
898 {
899         drm_iommu_detach_device(mixer_ctx->drm_dev, mixer_ctx->dev);
900 }
901
902 static int mixer_enable_vblank(struct exynos_drm_crtc *crtc)
903 {
904         struct mixer_context *mixer_ctx = crtc->ctx;
905         struct mixer_resources *res = &mixer_ctx->mixer_res;
906
907         __set_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
908         if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
909                 return 0;
910
911         /* enable vsync interrupt */
912         mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
913         mixer_reg_writemask(res, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
914
915         return 0;
916 }
917
918 static void mixer_disable_vblank(struct exynos_drm_crtc *crtc)
919 {
920         struct mixer_context *mixer_ctx = crtc->ctx;
921         struct mixer_resources *res = &mixer_ctx->mixer_res;
922
923         __clear_bit(MXR_BIT_VSYNC, &mixer_ctx->flags);
924
925         if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
926                 return;
927
928         /* disable vsync interrupt */
929         mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
930         mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
931 }
932
933 static void mixer_update_plane(struct exynos_drm_crtc *crtc,
934                                struct exynos_drm_plane *plane)
935 {
936         struct mixer_context *mixer_ctx = crtc->ctx;
937
938         DRM_DEBUG_KMS("win: %d\n", plane->zpos);
939
940         if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
941                 return;
942
943         if (plane->zpos > 1 && mixer_ctx->vp_enabled)
944                 vp_video_buffer(mixer_ctx, plane);
945         else
946                 mixer_graph_buffer(mixer_ctx, plane);
947 }
948
949 static void mixer_disable_plane(struct exynos_drm_crtc *crtc,
950                                 struct exynos_drm_plane *plane)
951 {
952         struct mixer_context *mixer_ctx = crtc->ctx;
953         struct mixer_resources *res = &mixer_ctx->mixer_res;
954         unsigned long flags;
955
956         DRM_DEBUG_KMS("win: %d\n", plane->zpos);
957
958         if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
959                 return;
960
961         spin_lock_irqsave(&res->reg_slock, flags);
962         mixer_vsync_set_update(mixer_ctx, false);
963
964         mixer_cfg_layer(mixer_ctx, plane->zpos, false);
965
966         mixer_vsync_set_update(mixer_ctx, true);
967         spin_unlock_irqrestore(&res->reg_slock, flags);
968 }
969
970 static void mixer_wait_for_vblank(struct exynos_drm_crtc *crtc)
971 {
972         struct mixer_context *mixer_ctx = crtc->ctx;
973         int err;
974
975         if (!test_bit(MXR_BIT_POWERED, &mixer_ctx->flags))
976                 return;
977
978         err = drm_vblank_get(mixer_ctx->drm_dev, mixer_ctx->pipe);
979         if (err < 0) {
980                 DRM_DEBUG_KMS("failed to acquire vblank counter\n");
981                 return;
982         }
983
984         atomic_set(&mixer_ctx->wait_vsync_event, 1);
985
986         /*
987          * wait for MIXER to signal VSYNC interrupt or return after
988          * timeout which is set to 50ms (refresh rate of 20).
989          */
990         if (!wait_event_timeout(mixer_ctx->wait_vsync_queue,
991                                 !atomic_read(&mixer_ctx->wait_vsync_event),
992                                 HZ/20))
993                 DRM_DEBUG_KMS("vblank wait timed out.\n");
994
995         drm_vblank_put(mixer_ctx->drm_dev, mixer_ctx->pipe);
996 }
997
998 static void mixer_enable(struct exynos_drm_crtc *crtc)
999 {
1000         struct mixer_context *ctx = crtc->ctx;
1001         struct mixer_resources *res = &ctx->mixer_res;
1002         int ret;
1003
1004         if (test_bit(MXR_BIT_POWERED, &ctx->flags))
1005                 return;
1006
1007         pm_runtime_get_sync(ctx->dev);
1008
1009         ret = clk_prepare_enable(res->mixer);
1010         if (ret < 0) {
1011                 DRM_ERROR("Failed to prepare_enable the mixer clk [%d]\n", ret);
1012                 return;
1013         }
1014         ret = clk_prepare_enable(res->hdmi);
1015         if (ret < 0) {
1016                 DRM_ERROR("Failed to prepare_enable the hdmi clk [%d]\n", ret);
1017                 return;
1018         }
1019         if (ctx->vp_enabled) {
1020                 ret = clk_prepare_enable(res->vp);
1021                 if (ret < 0) {
1022                         DRM_ERROR("Failed to prepare_enable the vp clk [%d]\n",
1023                                   ret);
1024                         return;
1025                 }
1026                 if (ctx->has_sclk) {
1027                         ret = clk_prepare_enable(res->sclk_mixer);
1028                         if (ret < 0) {
1029                                 DRM_ERROR("Failed to prepare_enable the " \
1030                                            "sclk_mixer clk [%d]\n",
1031                                           ret);
1032                                 return;
1033                         }
1034                 }
1035         }
1036
1037         set_bit(MXR_BIT_POWERED, &ctx->flags);
1038
1039         mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_SOFT_RESET);
1040
1041         if (test_bit(MXR_BIT_VSYNC, &ctx->flags)) {
1042                 mixer_reg_writemask(res, MXR_INT_STATUS, ~0, MXR_INT_CLEAR_VSYNC);
1043                 mixer_reg_writemask(res, MXR_INT_EN, ~0, MXR_INT_EN_VSYNC);
1044         }
1045         mixer_win_reset(ctx);
1046 }
1047
1048 static void mixer_disable(struct exynos_drm_crtc *crtc)
1049 {
1050         struct mixer_context *ctx = crtc->ctx;
1051         struct mixer_resources *res = &ctx->mixer_res;
1052         int i;
1053
1054         if (!test_bit(MXR_BIT_POWERED, &ctx->flags))
1055                 return;
1056
1057         mixer_stop(ctx);
1058         mixer_regs_dump(ctx);
1059
1060         for (i = 0; i < MIXER_WIN_NR; i++)
1061                 mixer_disable_plane(crtc, &ctx->planes[i]);
1062
1063         clear_bit(MXR_BIT_POWERED, &ctx->flags);
1064
1065         clk_disable_unprepare(res->hdmi);
1066         clk_disable_unprepare(res->mixer);
1067         if (ctx->vp_enabled) {
1068                 clk_disable_unprepare(res->vp);
1069                 if (ctx->has_sclk)
1070                         clk_disable_unprepare(res->sclk_mixer);
1071         }
1072
1073         pm_runtime_put_sync(ctx->dev);
1074 }
1075
1076 /* Only valid for Mixer version 16.0.33.0 */
1077 int mixer_check_mode(struct drm_display_mode *mode)
1078 {
1079         u32 w, h;
1080
1081         w = mode->hdisplay;
1082         h = mode->vdisplay;
1083
1084         DRM_DEBUG_KMS("xres=%d, yres=%d, refresh=%d, intl=%d\n",
1085                 mode->hdisplay, mode->vdisplay, mode->vrefresh,
1086                 (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 1 : 0);
1087
1088         if ((w >= 464 && w <= 720 && h >= 261 && h <= 576) ||
1089                 (w >= 1024 && w <= 1280 && h >= 576 && h <= 720) ||
1090                 (w >= 1664 && w <= 1920 && h >= 936 && h <= 1080))
1091                 return 0;
1092
1093         return -EINVAL;
1094 }
1095
1096 static const struct exynos_drm_crtc_ops mixer_crtc_ops = {
1097         .enable                 = mixer_enable,
1098         .disable                = mixer_disable,
1099         .enable_vblank          = mixer_enable_vblank,
1100         .disable_vblank         = mixer_disable_vblank,
1101         .wait_for_vblank        = mixer_wait_for_vblank,
1102         .update_plane           = mixer_update_plane,
1103         .disable_plane          = mixer_disable_plane,
1104 };
1105
1106 static struct mixer_drv_data exynos5420_mxr_drv_data = {
1107         .version = MXR_VER_128_0_0_184,
1108         .is_vp_enabled = 0,
1109 };
1110
1111 static struct mixer_drv_data exynos5250_mxr_drv_data = {
1112         .version = MXR_VER_16_0_33_0,
1113         .is_vp_enabled = 0,
1114 };
1115
1116 static struct mixer_drv_data exynos4212_mxr_drv_data = {
1117         .version = MXR_VER_0_0_0_16,
1118         .is_vp_enabled = 1,
1119 };
1120
1121 static struct mixer_drv_data exynos4210_mxr_drv_data = {
1122         .version = MXR_VER_0_0_0_16,
1123         .is_vp_enabled = 1,
1124         .has_sclk = 1,
1125 };
1126
1127 static const struct platform_device_id mixer_driver_types[] = {
1128         {
1129                 .name           = "s5p-mixer",
1130                 .driver_data    = (unsigned long)&exynos4210_mxr_drv_data,
1131         }, {
1132                 .name           = "exynos5-mixer",
1133                 .driver_data    = (unsigned long)&exynos5250_mxr_drv_data,
1134         }, {
1135                 /* end node */
1136         }
1137 };
1138
1139 static struct of_device_id mixer_match_types[] = {
1140         {
1141                 .compatible = "samsung,exynos4210-mixer",
1142                 .data   = &exynos4210_mxr_drv_data,
1143         }, {
1144                 .compatible = "samsung,exynos4212-mixer",
1145                 .data   = &exynos4212_mxr_drv_data,
1146         }, {
1147                 .compatible = "samsung,exynos5-mixer",
1148                 .data   = &exynos5250_mxr_drv_data,
1149         }, {
1150                 .compatible = "samsung,exynos5250-mixer",
1151                 .data   = &exynos5250_mxr_drv_data,
1152         }, {
1153                 .compatible = "samsung,exynos5420-mixer",
1154                 .data   = &exynos5420_mxr_drv_data,
1155         }, {
1156                 /* end node */
1157         }
1158 };
1159 MODULE_DEVICE_TABLE(of, mixer_match_types);
1160
1161 static int mixer_bind(struct device *dev, struct device *manager, void *data)
1162 {
1163         struct mixer_context *ctx = dev_get_drvdata(dev);
1164         struct drm_device *drm_dev = data;
1165         struct exynos_drm_plane *exynos_plane;
1166         enum drm_plane_type type;
1167         unsigned int zpos;
1168         int ret;
1169
1170         ret = mixer_initialize(ctx, drm_dev);
1171         if (ret)
1172                 return ret;
1173
1174         for (zpos = 0; zpos < MIXER_WIN_NR; zpos++) {
1175                 type = (zpos == MIXER_DEFAULT_WIN) ? DRM_PLANE_TYPE_PRIMARY :
1176                                                 DRM_PLANE_TYPE_OVERLAY;
1177                 ret = exynos_plane_init(drm_dev, &ctx->planes[zpos],
1178                                         1 << ctx->pipe, type, zpos);
1179                 if (ret)
1180                         return ret;
1181         }
1182
1183         exynos_plane = &ctx->planes[MIXER_DEFAULT_WIN];
1184         ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
1185                                            ctx->pipe, EXYNOS_DISPLAY_TYPE_HDMI,
1186                                            &mixer_crtc_ops, ctx);
1187         if (IS_ERR(ctx->crtc)) {
1188                 mixer_ctx_remove(ctx);
1189                 ret = PTR_ERR(ctx->crtc);
1190                 goto free_ctx;
1191         }
1192
1193         return 0;
1194
1195 free_ctx:
1196         devm_kfree(dev, ctx);
1197         return ret;
1198 }
1199
1200 static void mixer_unbind(struct device *dev, struct device *master, void *data)
1201 {
1202         struct mixer_context *ctx = dev_get_drvdata(dev);
1203
1204         mixer_ctx_remove(ctx);
1205 }
1206
1207 static const struct component_ops mixer_component_ops = {
1208         .bind   = mixer_bind,
1209         .unbind = mixer_unbind,
1210 };
1211
1212 static int mixer_probe(struct platform_device *pdev)
1213 {
1214         struct device *dev = &pdev->dev;
1215         struct mixer_drv_data *drv;
1216         struct mixer_context *ctx;
1217         int ret;
1218
1219         ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1220         if (!ctx) {
1221                 DRM_ERROR("failed to alloc mixer context.\n");
1222                 return -ENOMEM;
1223         }
1224
1225         if (dev->of_node) {
1226                 const struct of_device_id *match;
1227
1228                 match = of_match_node(mixer_match_types, dev->of_node);
1229                 drv = (struct mixer_drv_data *)match->data;
1230         } else {
1231                 drv = (struct mixer_drv_data *)
1232                         platform_get_device_id(pdev)->driver_data;
1233         }
1234
1235         ctx->pdev = pdev;
1236         ctx->dev = dev;
1237         ctx->vp_enabled = drv->is_vp_enabled;
1238         ctx->has_sclk = drv->has_sclk;
1239         ctx->mxr_ver = drv->version;
1240         init_waitqueue_head(&ctx->wait_vsync_queue);
1241         atomic_set(&ctx->wait_vsync_event, 0);
1242
1243         platform_set_drvdata(pdev, ctx);
1244
1245         ret = component_add(&pdev->dev, &mixer_component_ops);
1246         if (!ret)
1247                 pm_runtime_enable(dev);
1248
1249         return ret;
1250 }
1251
1252 static int mixer_remove(struct platform_device *pdev)
1253 {
1254         pm_runtime_disable(&pdev->dev);
1255
1256         component_del(&pdev->dev, &mixer_component_ops);
1257
1258         return 0;
1259 }
1260
1261 struct platform_driver mixer_driver = {
1262         .driver = {
1263                 .name = "exynos-mixer",
1264                 .owner = THIS_MODULE,
1265                 .of_match_table = mixer_match_types,
1266         },
1267         .probe = mixer_probe,
1268         .remove = mixer_remove,
1269         .id_table       = mixer_driver_types,
1270 };