]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/exynos/exynos_drm_fimd.c
drm/exynos: Clean up FIMD power on/off routines
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpu / drm / exynos / exynos_drm_fimd.c
CommitLineData
1c248b7d
ID
1/* exynos_drm_fimd.c
2 *
3 * Copyright (C) 2011 Samsung Electronics Co.Ltd
4 * Authors:
5 * Joonyoung Shim <jy0922.shim@samsung.com>
6 * Inki Dae <inki.dae@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 */
760285e7 14#include <drm/drmP.h>
1c248b7d
ID
15
16#include <linux/kernel.h>
1c248b7d
ID
17#include <linux/platform_device.h>
18#include <linux/clk.h>
3f1c781d 19#include <linux/of.h>
d636ead8 20#include <linux/of_device.h>
cb91f6a0 21#include <linux/pm_runtime.h>
1c248b7d 22
7f4596f4 23#include <video/of_display_timing.h>
111e6055 24#include <video/of_videomode.h>
5a213a55 25#include <video/samsung_fimd.h>
1c248b7d 26#include <drm/exynos_drm.h>
1c248b7d
ID
27
28#include "exynos_drm_drv.h"
29#include "exynos_drm_fbdev.h"
30#include "exynos_drm_crtc.h"
bcc5cd1c 31#include "exynos_drm_iommu.h"
1c248b7d
ID
32
33/*
b8654b37 34 * FIMD stands for Fully Interactive Mobile Display and
1c248b7d
ID
35 * as a display controller, it transfers contents drawn on memory
36 * to a LCD Panel through Display Interfaces such as RGB or
37 * CPU Interface.
38 */
39
111e6055
AH
40#define FIMD_DEFAULT_FRAMERATE 60
41
1c248b7d
ID
42/* position control register for hardware window 0, 2 ~ 4.*/
43#define VIDOSD_A(win) (VIDOSD_BASE + 0x00 + (win) * 16)
44#define VIDOSD_B(win) (VIDOSD_BASE + 0x04 + (win) * 16)
0f10cf14
LKA
45/*
46 * size control register for hardware windows 0 and alpha control register
47 * for hardware windows 1 ~ 4
48 */
49#define VIDOSD_C(win) (VIDOSD_BASE + 0x08 + (win) * 16)
50/* size control register for hardware windows 1 ~ 2. */
1c248b7d
ID
51#define VIDOSD_D(win) (VIDOSD_BASE + 0x0C + (win) * 16)
52
53#define VIDWx_BUF_START(win, buf) (VIDW_BUF_START(buf) + (win) * 8)
54#define VIDWx_BUF_END(win, buf) (VIDW_BUF_END(buf) + (win) * 8)
55#define VIDWx_BUF_SIZE(win, buf) (VIDW_BUF_SIZE(buf) + (win) * 4)
56
57/* color key control register for hardware window 1 ~ 4. */
0f10cf14 58#define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
1c248b7d 59/* color key value register for hardware window 1 ~ 4. */
0f10cf14 60#define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
1c248b7d
ID
61
62/* FIMD has totally five hardware windows. */
63#define WINDOWS_NR 5
64
bb7704d6 65#define get_fimd_manager(mgr) platform_get_drvdata(to_platform_device(dev))
1c248b7d 66
e2e13389
LKA
67struct fimd_driver_data {
68 unsigned int timing_base;
de7af100
TF
69
70 unsigned int has_shadowcon:1;
411d9ed4 71 unsigned int has_clksel:1;
5cc4621a 72 unsigned int has_limited_fmt:1;
e2e13389
LKA
73};
74
725ddead
TF
75static struct fimd_driver_data s3c64xx_fimd_driver_data = {
76 .timing_base = 0x0,
77 .has_clksel = 1,
5cc4621a 78 .has_limited_fmt = 1,
725ddead
TF
79};
80
6ecf18f9 81static struct fimd_driver_data exynos4_fimd_driver_data = {
e2e13389 82 .timing_base = 0x0,
de7af100 83 .has_shadowcon = 1,
e2e13389
LKA
84};
85
6ecf18f9 86static struct fimd_driver_data exynos5_fimd_driver_data = {
e2e13389 87 .timing_base = 0x20000,
de7af100 88 .has_shadowcon = 1,
e2e13389
LKA
89};
90
1c248b7d
ID
91struct fimd_win_data {
92 unsigned int offset_x;
93 unsigned int offset_y;
19c8b834
ID
94 unsigned int ovl_width;
95 unsigned int ovl_height;
96 unsigned int fb_width;
97 unsigned int fb_height;
1c248b7d 98 unsigned int bpp;
a4f38a80 99 unsigned int pixel_format;
2c871127 100 dma_addr_t dma_addr;
1c248b7d
ID
101 unsigned int buf_offsize;
102 unsigned int line_size; /* bytes */
ec05da95 103 bool enabled;
db7e55ae 104 bool resume;
1c248b7d
ID
105};
106
107struct fimd_context {
bb7704d6 108 struct device *dev;
40c8ab4b 109 struct drm_device *drm_dev;
1c248b7d
ID
110 struct clk *bus_clk;
111 struct clk *lcd_clk;
1c248b7d 112 void __iomem *regs;
a968e727 113 struct drm_display_mode mode;
1c248b7d 114 struct fimd_win_data win_data[WINDOWS_NR];
1c248b7d
ID
115 unsigned int default_win;
116 unsigned long irq_flags;
117 u32 vidcon0;
118 u32 vidcon1;
cb91f6a0 119 bool suspended;
080be03d 120 int pipe;
01ce113c
P
121 wait_queue_head_t wait_vsync_queue;
122 atomic_t wait_vsync_event;
1c248b7d 123
562ad9f4 124 struct exynos_drm_panel_info panel;
18873465 125 struct fimd_driver_data *driver_data;
1c248b7d
ID
126};
127
d636ead8 128static const struct of_device_id fimd_driver_dt_match[] = {
725ddead
TF
129 { .compatible = "samsung,s3c6400-fimd",
130 .data = &s3c64xx_fimd_driver_data },
5830daf8 131 { .compatible = "samsung,exynos4210-fimd",
d636ead8 132 .data = &exynos4_fimd_driver_data },
5830daf8 133 { .compatible = "samsung,exynos5250-fimd",
d636ead8
JS
134 .data = &exynos5_fimd_driver_data },
135 {},
136};
d636ead8 137
e2e13389
LKA
138static inline struct fimd_driver_data *drm_fimd_get_driver_data(
139 struct platform_device *pdev)
140{
d636ead8
JS
141 const struct of_device_id *of_id =
142 of_match_device(fimd_driver_dt_match, &pdev->dev);
143
2d3f173c 144 return (struct fimd_driver_data *)of_id->data;
e2e13389
LKA
145}
146
bb7704d6 147static int fimd_mgr_initialize(struct exynos_drm_manager *mgr,
080be03d 148 struct drm_device *drm_dev, int pipe)
40c8ab4b 149{
bb7704d6 150 struct fimd_context *ctx = mgr->ctx;
40c8ab4b
SP
151
152 ctx->drm_dev = drm_dev;
080be03d 153 ctx->pipe = pipe;
40c8ab4b 154
080be03d
SP
155 /*
156 * enable drm irq mode.
157 * - with irq_enabled = true, we can use the vblank feature.
158 *
159 * P.S. note that we wouldn't use drm irq handler but
160 * just specific driver own one instead because
161 * drm framework supports only one irq handler.
162 */
163 drm_dev->irq_enabled = true;
ec05da95 164
080be03d
SP
165 /*
166 * with vblank_disable_allowed = true, vblank interrupt will be disabled
167 * by drm timer once a current process gives up ownership of
168 * vblank event.(after drm_vblank_put function is called)
169 */
170 drm_dev->vblank_disable_allowed = true;
c32b06ef 171
080be03d
SP
172 /* attach this sub driver to iommu mapping if supported. */
173 if (is_drm_iommu_supported(ctx->drm_dev))
174 drm_iommu_attach_device(ctx->drm_dev, ctx->dev);
c32b06ef 175
080be03d 176 return 0;
ec05da95
ID
177}
178
080be03d 179static void fimd_mgr_remove(struct exynos_drm_manager *mgr)
ec05da95 180{
bb7704d6 181 struct fimd_context *ctx = mgr->ctx;
ec05da95 182
080be03d
SP
183 /* detach this sub driver from iommu mapping if supported. */
184 if (is_drm_iommu_supported(ctx->drm_dev))
185 drm_iommu_detach_device(ctx->drm_dev, ctx->dev);
ec05da95
ID
186}
187
a968e727
SP
188static u32 fimd_calc_clkdiv(struct fimd_context *ctx,
189 const struct drm_display_mode *mode)
190{
191 unsigned long ideal_clk = mode->htotal * mode->vtotal * mode->vrefresh;
192 u32 clkdiv;
193
194 /* Find the clock divider value that gets us closest to ideal_clk */
195 clkdiv = DIV_ROUND_UP(clk_get_rate(ctx->lcd_clk), ideal_clk);
196
197 return (clkdiv < 0x100) ? clkdiv : 0xff;
198}
199
200static bool fimd_mode_fixup(struct exynos_drm_manager *mgr,
201 const struct drm_display_mode *mode,
202 struct drm_display_mode *adjusted_mode)
203{
204 if (adjusted_mode->vrefresh == 0)
205 adjusted_mode->vrefresh = FIMD_DEFAULT_FRAMERATE;
206
207 return true;
208}
209
210static void fimd_mode_set(struct exynos_drm_manager *mgr,
211 const struct drm_display_mode *in_mode)
212{
213 struct fimd_context *ctx = mgr->ctx;
214
215 drm_mode_copy(&ctx->mode, in_mode);
216}
217
bb7704d6 218static void fimd_commit(struct exynos_drm_manager *mgr)
1c248b7d 219{
bb7704d6 220 struct fimd_context *ctx = mgr->ctx;
a968e727 221 struct drm_display_mode *mode = &ctx->mode;
e2e13389 222 struct fimd_driver_data *driver_data;
1417f109 223 u32 val, clkdiv, vidcon1;
a968e727 224 int hblank, vblank, vsync_len, vbpd, vfpd, hsync_len, hbpd, hfpd;
1c248b7d 225
18873465 226 driver_data = ctx->driver_data;
e30d4bcf
ID
227 if (ctx->suspended)
228 return;
229
a968e727
SP
230 /* nothing to do if we haven't set the mode yet */
231 if (mode->htotal == 0 || mode->vtotal == 0)
232 return;
233
1417f109
SP
234 /* setup polarity values */
235 vidcon1 = ctx->vidcon1;
236 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
237 vidcon1 |= VIDCON1_INV_VSYNC;
238 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
239 vidcon1 |= VIDCON1_INV_HSYNC;
240 writel(vidcon1, ctx->regs + driver_data->timing_base + VIDCON1);
1c248b7d
ID
241
242 /* setup vertical timing values. */
a968e727
SP
243 vblank = mode->crtc_vblank_end - mode->crtc_vblank_start;
244 vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
245 vbpd = (vblank - vsync_len) / 2;
246 vfpd = vblank - vsync_len - vbpd;
247
248 val = VIDTCON0_VBPD(vbpd - 1) |
249 VIDTCON0_VFPD(vfpd - 1) |
250 VIDTCON0_VSPW(vsync_len - 1);
e2e13389 251 writel(val, ctx->regs + driver_data->timing_base + VIDTCON0);
1c248b7d
ID
252
253 /* setup horizontal timing values. */
a968e727
SP
254 hblank = mode->crtc_hblank_end - mode->crtc_hblank_start;
255 hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
256 hbpd = (hblank - hsync_len) / 2;
257 hfpd = hblank - hsync_len - hbpd;
258
259 val = VIDTCON1_HBPD(hbpd - 1) |
260 VIDTCON1_HFPD(hfpd - 1) |
261 VIDTCON1_HSPW(hsync_len - 1);
e2e13389 262 writel(val, ctx->regs + driver_data->timing_base + VIDTCON1);
1c248b7d
ID
263
264 /* setup horizontal and vertical display size. */
a968e727
SP
265 val = VIDTCON2_LINEVAL(mode->vdisplay - 1) |
266 VIDTCON2_HOZVAL(mode->hdisplay - 1) |
267 VIDTCON2_LINEVAL_E(mode->vdisplay - 1) |
268 VIDTCON2_HOZVAL_E(mode->hdisplay - 1);
e2e13389 269 writel(val, ctx->regs + driver_data->timing_base + VIDTCON2);
1c248b7d
ID
270
271 /* setup clock source, clock divider, enable dma. */
272 val = ctx->vidcon0;
273 val &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
274
411d9ed4
TF
275 if (ctx->driver_data->has_clksel) {
276 val &= ~VIDCON0_CLKSEL_MASK;
277 val |= VIDCON0_CLKSEL_LCD;
278 }
279
a968e727
SP
280 clkdiv = fimd_calc_clkdiv(ctx, mode);
281 if (clkdiv > 1)
282 val |= VIDCON0_CLKVAL_F(clkdiv - 1) | VIDCON0_CLKDIR;
1c248b7d
ID
283 else
284 val &= ~VIDCON0_CLKDIR; /* 1:1 clock */
285
286 /*
287 * fields of register with prefix '_F' would be updated
288 * at vsync(same as dma start)
289 */
290 val |= VIDCON0_ENVID | VIDCON0_ENVID_F;
291 writel(val, ctx->regs + VIDCON0);
292}
293
bb7704d6 294static int fimd_enable_vblank(struct exynos_drm_manager *mgr)
1c248b7d 295{
bb7704d6 296 struct fimd_context *ctx = mgr->ctx;
1c248b7d
ID
297 u32 val;
298
cb91f6a0
JS
299 if (ctx->suspended)
300 return -EPERM;
301
1c248b7d
ID
302 if (!test_and_set_bit(0, &ctx->irq_flags)) {
303 val = readl(ctx->regs + VIDINTCON0);
304
305 val |= VIDINTCON0_INT_ENABLE;
306 val |= VIDINTCON0_INT_FRAME;
307
308 val &= ~VIDINTCON0_FRAMESEL0_MASK;
309 val |= VIDINTCON0_FRAMESEL0_VSYNC;
310 val &= ~VIDINTCON0_FRAMESEL1_MASK;
311 val |= VIDINTCON0_FRAMESEL1_NONE;
312
313 writel(val, ctx->regs + VIDINTCON0);
314 }
315
316 return 0;
317}
318
bb7704d6 319static void fimd_disable_vblank(struct exynos_drm_manager *mgr)
1c248b7d 320{
bb7704d6 321 struct fimd_context *ctx = mgr->ctx;
1c248b7d
ID
322 u32 val;
323
cb91f6a0
JS
324 if (ctx->suspended)
325 return;
326
1c248b7d
ID
327 if (test_and_clear_bit(0, &ctx->irq_flags)) {
328 val = readl(ctx->regs + VIDINTCON0);
329
330 val &= ~VIDINTCON0_INT_FRAME;
331 val &= ~VIDINTCON0_INT_ENABLE;
332
333 writel(val, ctx->regs + VIDINTCON0);
334 }
335}
336
bb7704d6 337static void fimd_wait_for_vblank(struct exynos_drm_manager *mgr)
07033970 338{
bb7704d6 339 struct fimd_context *ctx = mgr->ctx;
07033970 340
01ce113c
P
341 if (ctx->suspended)
342 return;
343
344 atomic_set(&ctx->wait_vsync_event, 1);
345
346 /*
347 * wait for FIMD to signal VSYNC interrupt or return after
348 * timeout which is set to 50ms (refresh rate of 20).
349 */
350 if (!wait_event_timeout(ctx->wait_vsync_queue,
351 !atomic_read(&ctx->wait_vsync_event),
8dd9ad5d 352 HZ/20))
07033970
P
353 DRM_DEBUG_KMS("vblank wait timed out.\n");
354}
355
bb7704d6
SP
356static void fimd_win_mode_set(struct exynos_drm_manager *mgr,
357 struct exynos_drm_overlay *overlay)
1c248b7d 358{
bb7704d6 359 struct fimd_context *ctx = mgr->ctx;
1c248b7d 360 struct fimd_win_data *win_data;
864ee9e6 361 int win;
19c8b834 362 unsigned long offset;
1c248b7d 363
1c248b7d 364 if (!overlay) {
bb7704d6 365 DRM_ERROR("overlay is NULL\n");
1c248b7d
ID
366 return;
367 }
368
864ee9e6
JS
369 win = overlay->zpos;
370 if (win == DEFAULT_ZPOS)
371 win = ctx->default_win;
372
37b006e8 373 if (win < 0 || win >= WINDOWS_NR)
864ee9e6
JS
374 return;
375
19c8b834
ID
376 offset = overlay->fb_x * (overlay->bpp >> 3);
377 offset += overlay->fb_y * overlay->pitch;
378
379 DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, overlay->pitch);
380
864ee9e6 381 win_data = &ctx->win_data[win];
1c248b7d 382
19c8b834
ID
383 win_data->offset_x = overlay->crtc_x;
384 win_data->offset_y = overlay->crtc_y;
385 win_data->ovl_width = overlay->crtc_width;
386 win_data->ovl_height = overlay->crtc_height;
387 win_data->fb_width = overlay->fb_width;
388 win_data->fb_height = overlay->fb_height;
229d3534 389 win_data->dma_addr = overlay->dma_addr[0] + offset;
1c248b7d 390 win_data->bpp = overlay->bpp;
a4f38a80 391 win_data->pixel_format = overlay->pixel_format;
19c8b834
ID
392 win_data->buf_offsize = (overlay->fb_width - overlay->crtc_width) *
393 (overlay->bpp >> 3);
394 win_data->line_size = overlay->crtc_width * (overlay->bpp >> 3);
395
396 DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
397 win_data->offset_x, win_data->offset_y);
398 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
399 win_data->ovl_width, win_data->ovl_height);
ddd8e959 400 DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
19c8b834
ID
401 DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
402 overlay->fb_width, overlay->crtc_width);
1c248b7d
ID
403}
404
bb7704d6 405static void fimd_win_set_pixfmt(struct fimd_context *ctx, unsigned int win)
1c248b7d 406{
1c248b7d
ID
407 struct fimd_win_data *win_data = &ctx->win_data[win];
408 unsigned long val;
409
1c248b7d
ID
410 val = WINCONx_ENWIN;
411
5cc4621a
ID
412 /*
413 * In case of s3c64xx, window 0 doesn't support alpha channel.
414 * So the request format is ARGB8888 then change it to XRGB8888.
415 */
416 if (ctx->driver_data->has_limited_fmt && !win) {
417 if (win_data->pixel_format == DRM_FORMAT_ARGB8888)
418 win_data->pixel_format = DRM_FORMAT_XRGB8888;
419 }
420
a4f38a80
ID
421 switch (win_data->pixel_format) {
422 case DRM_FORMAT_C8:
1c248b7d
ID
423 val |= WINCON0_BPPMODE_8BPP_PALETTE;
424 val |= WINCONx_BURSTLEN_8WORD;
425 val |= WINCONx_BYTSWP;
426 break;
a4f38a80
ID
427 case DRM_FORMAT_XRGB1555:
428 val |= WINCON0_BPPMODE_16BPP_1555;
429 val |= WINCONx_HAWSWP;
430 val |= WINCONx_BURSTLEN_16WORD;
431 break;
432 case DRM_FORMAT_RGB565:
1c248b7d
ID
433 val |= WINCON0_BPPMODE_16BPP_565;
434 val |= WINCONx_HAWSWP;
435 val |= WINCONx_BURSTLEN_16WORD;
436 break;
a4f38a80 437 case DRM_FORMAT_XRGB8888:
1c248b7d
ID
438 val |= WINCON0_BPPMODE_24BPP_888;
439 val |= WINCONx_WSWP;
440 val |= WINCONx_BURSTLEN_16WORD;
441 break;
a4f38a80
ID
442 case DRM_FORMAT_ARGB8888:
443 val |= WINCON1_BPPMODE_25BPP_A1888
1c248b7d
ID
444 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
445 val |= WINCONx_WSWP;
446 val |= WINCONx_BURSTLEN_16WORD;
447 break;
448 default:
449 DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
450
451 val |= WINCON0_BPPMODE_24BPP_888;
452 val |= WINCONx_WSWP;
453 val |= WINCONx_BURSTLEN_16WORD;
454 break;
455 }
456
457 DRM_DEBUG_KMS("bpp = %d\n", win_data->bpp);
458
459 writel(val, ctx->regs + WINCON(win));
460}
461
bb7704d6 462static void fimd_win_set_colkey(struct fimd_context *ctx, unsigned int win)
1c248b7d 463{
1c248b7d
ID
464 unsigned int keycon0 = 0, keycon1 = 0;
465
1c248b7d
ID
466 keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
467 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
468
469 keycon1 = WxKEYCON1_COLVAL(0xffffffff);
470
471 writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
472 writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
473}
474
de7af100
TF
475/**
476 * shadow_protect_win() - disable updating values from shadow registers at vsync
477 *
478 * @win: window to protect registers for
479 * @protect: 1 to protect (disable updates)
480 */
481static void fimd_shadow_protect_win(struct fimd_context *ctx,
482 int win, bool protect)
483{
484 u32 reg, bits, val;
485
486 if (ctx->driver_data->has_shadowcon) {
487 reg = SHADOWCON;
488 bits = SHADOWCON_WINx_PROTECT(win);
489 } else {
490 reg = PRTCON;
491 bits = PRTCON_PROTECT;
492 }
493
494 val = readl(ctx->regs + reg);
495 if (protect)
496 val |= bits;
497 else
498 val &= ~bits;
499 writel(val, ctx->regs + reg);
500}
501
bb7704d6 502static void fimd_win_commit(struct exynos_drm_manager *mgr, int zpos)
1c248b7d 503{
bb7704d6 504 struct fimd_context *ctx = mgr->ctx;
1c248b7d 505 struct fimd_win_data *win_data;
864ee9e6 506 int win = zpos;
1c248b7d 507 unsigned long val, alpha, size;
f56aad3a
JS
508 unsigned int last_x;
509 unsigned int last_y;
1c248b7d 510
e30d4bcf
ID
511 if (ctx->suspended)
512 return;
513
864ee9e6
JS
514 if (win == DEFAULT_ZPOS)
515 win = ctx->default_win;
516
37b006e8 517 if (win < 0 || win >= WINDOWS_NR)
1c248b7d
ID
518 return;
519
520 win_data = &ctx->win_data[win];
521
a43b933b
SP
522 /* If suspended, enable this on resume */
523 if (ctx->suspended) {
524 win_data->resume = true;
525 return;
526 }
527
1c248b7d 528 /*
de7af100 529 * SHADOWCON/PRTCON register is used for enabling timing.
1c248b7d
ID
530 *
531 * for example, once only width value of a register is set,
532 * if the dma is started then fimd hardware could malfunction so
533 * with protect window setting, the register fields with prefix '_F'
534 * wouldn't be updated at vsync also but updated once unprotect window
535 * is set.
536 */
537
538 /* protect windows */
de7af100 539 fimd_shadow_protect_win(ctx, win, true);
1c248b7d
ID
540
541 /* buffer start address */
2c871127 542 val = (unsigned long)win_data->dma_addr;
1c248b7d
ID
543 writel(val, ctx->regs + VIDWx_BUF_START(win, 0));
544
545 /* buffer end address */
19c8b834 546 size = win_data->fb_width * win_data->ovl_height * (win_data->bpp >> 3);
2c871127 547 val = (unsigned long)(win_data->dma_addr + size);
1c248b7d
ID
548 writel(val, ctx->regs + VIDWx_BUF_END(win, 0));
549
550 DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
2c871127 551 (unsigned long)win_data->dma_addr, val, size);
19c8b834
ID
552 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
553 win_data->ovl_width, win_data->ovl_height);
1c248b7d
ID
554
555 /* buffer size */
556 val = VIDW_BUF_SIZE_OFFSET(win_data->buf_offsize) |
ca555e5a
JS
557 VIDW_BUF_SIZE_PAGEWIDTH(win_data->line_size) |
558 VIDW_BUF_SIZE_OFFSET_E(win_data->buf_offsize) |
559 VIDW_BUF_SIZE_PAGEWIDTH_E(win_data->line_size);
1c248b7d
ID
560 writel(val, ctx->regs + VIDWx_BUF_SIZE(win, 0));
561
562 /* OSD position */
563 val = VIDOSDxA_TOPLEFT_X(win_data->offset_x) |
ca555e5a
JS
564 VIDOSDxA_TOPLEFT_Y(win_data->offset_y) |
565 VIDOSDxA_TOPLEFT_X_E(win_data->offset_x) |
566 VIDOSDxA_TOPLEFT_Y_E(win_data->offset_y);
1c248b7d
ID
567 writel(val, ctx->regs + VIDOSD_A(win));
568
f56aad3a
JS
569 last_x = win_data->offset_x + win_data->ovl_width;
570 if (last_x)
571 last_x--;
572 last_y = win_data->offset_y + win_data->ovl_height;
573 if (last_y)
574 last_y--;
575
ca555e5a
JS
576 val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y) |
577 VIDOSDxB_BOTRIGHT_X_E(last_x) | VIDOSDxB_BOTRIGHT_Y_E(last_y);
578
1c248b7d
ID
579 writel(val, ctx->regs + VIDOSD_B(win));
580
19c8b834 581 DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
f56aad3a 582 win_data->offset_x, win_data->offset_y, last_x, last_y);
1c248b7d
ID
583
584 /* hardware window 0 doesn't support alpha channel. */
585 if (win != 0) {
586 /* OSD alpha */
587 alpha = VIDISD14C_ALPHA1_R(0xf) |
588 VIDISD14C_ALPHA1_G(0xf) |
589 VIDISD14C_ALPHA1_B(0xf);
590
591 writel(alpha, ctx->regs + VIDOSD_C(win));
592 }
593
594 /* OSD size */
595 if (win != 3 && win != 4) {
596 u32 offset = VIDOSD_D(win);
597 if (win == 0)
0f10cf14 598 offset = VIDOSD_C(win);
19c8b834 599 val = win_data->ovl_width * win_data->ovl_height;
1c248b7d
ID
600 writel(val, ctx->regs + offset);
601
602 DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val);
603 }
604
bb7704d6 605 fimd_win_set_pixfmt(ctx, win);
1c248b7d
ID
606
607 /* hardware window 0 doesn't support color key. */
608 if (win != 0)
bb7704d6 609 fimd_win_set_colkey(ctx, win);
1c248b7d 610
ec05da95
ID
611 /* wincon */
612 val = readl(ctx->regs + WINCON(win));
613 val |= WINCONx_ENWIN;
614 writel(val, ctx->regs + WINCON(win));
615
1c248b7d 616 /* Enable DMA channel and unprotect windows */
de7af100
TF
617 fimd_shadow_protect_win(ctx, win, false);
618
619 if (ctx->driver_data->has_shadowcon) {
620 val = readl(ctx->regs + SHADOWCON);
621 val |= SHADOWCON_CHx_ENABLE(win);
622 writel(val, ctx->regs + SHADOWCON);
623 }
ec05da95
ID
624
625 win_data->enabled = true;
1c248b7d
ID
626}
627
bb7704d6 628static void fimd_win_disable(struct exynos_drm_manager *mgr, int zpos)
1c248b7d 629{
bb7704d6 630 struct fimd_context *ctx = mgr->ctx;
ec05da95 631 struct fimd_win_data *win_data;
864ee9e6 632 int win = zpos;
1c248b7d
ID
633 u32 val;
634
864ee9e6
JS
635 if (win == DEFAULT_ZPOS)
636 win = ctx->default_win;
637
37b006e8 638 if (win < 0 || win >= WINDOWS_NR)
1c248b7d
ID
639 return;
640
ec05da95
ID
641 win_data = &ctx->win_data[win];
642
db7e55ae
P
643 if (ctx->suspended) {
644 /* do not resume this window*/
645 win_data->resume = false;
646 return;
647 }
648
1c248b7d 649 /* protect windows */
de7af100 650 fimd_shadow_protect_win(ctx, win, true);
1c248b7d
ID
651
652 /* wincon */
653 val = readl(ctx->regs + WINCON(win));
654 val &= ~WINCONx_ENWIN;
655 writel(val, ctx->regs + WINCON(win));
656
657 /* unprotect windows */
de7af100
TF
658 if (ctx->driver_data->has_shadowcon) {
659 val = readl(ctx->regs + SHADOWCON);
660 val &= ~SHADOWCON_CHx_ENABLE(win);
661 writel(val, ctx->regs + SHADOWCON);
662 }
663
664 fimd_shadow_protect_win(ctx, win, false);
ec05da95
ID
665
666 win_data->enabled = false;
1c248b7d
ID
667}
668
a43b933b
SP
669static void fimd_clear_win(struct fimd_context *ctx, int win)
670{
671 writel(0, ctx->regs + WINCON(win));
672 writel(0, ctx->regs + VIDOSD_A(win));
673 writel(0, ctx->regs + VIDOSD_B(win));
674 writel(0, ctx->regs + VIDOSD_C(win));
675
676 if (win == 1 || win == 2)
677 writel(0, ctx->regs + VIDOSD_D(win));
678
679 fimd_shadow_protect_win(ctx, win, false);
680}
681
682static void fimd_window_suspend(struct exynos_drm_manager *mgr)
683{
684 struct fimd_context *ctx = mgr->ctx;
685 struct fimd_win_data *win_data;
686 int i;
687
688 for (i = 0; i < WINDOWS_NR; i++) {
689 win_data = &ctx->win_data[i];
690 win_data->resume = win_data->enabled;
691 if (win_data->enabled)
692 fimd_win_disable(mgr, i);
693 }
694 fimd_wait_for_vblank(mgr);
695}
696
697static void fimd_window_resume(struct exynos_drm_manager *mgr)
698{
699 struct fimd_context *ctx = mgr->ctx;
700 struct fimd_win_data *win_data;
701 int i;
702
703 for (i = 0; i < WINDOWS_NR; i++) {
704 win_data = &ctx->win_data[i];
705 win_data->enabled = win_data->resume;
706 win_data->resume = false;
707 }
708}
709
710static void fimd_apply(struct exynos_drm_manager *mgr)
711{
712 struct fimd_context *ctx = mgr->ctx;
713 struct fimd_win_data *win_data;
714 int i;
715
716 for (i = 0; i < WINDOWS_NR; i++) {
717 win_data = &ctx->win_data[i];
718 if (win_data->enabled)
719 fimd_win_commit(mgr, i);
720 }
721
722 fimd_commit(mgr);
723}
724
725static int fimd_poweron(struct exynos_drm_manager *mgr)
726{
727 struct fimd_context *ctx = mgr->ctx;
728 int ret;
729
730 if (!ctx->suspended)
731 return 0;
732
733 ctx->suspended = false;
734
735 ret = clk_prepare_enable(ctx->bus_clk);
736 if (ret < 0) {
737 DRM_ERROR("Failed to prepare_enable the bus clk [%d]\n", ret);
738 goto bus_clk_err;
739 }
740
741 ret = clk_prepare_enable(ctx->lcd_clk);
742 if (ret < 0) {
743 DRM_ERROR("Failed to prepare_enable the lcd clk [%d]\n", ret);
744 goto lcd_clk_err;
745 }
746
747 /* if vblank was enabled status, enable it again. */
748 if (test_and_clear_bit(0, &ctx->irq_flags)) {
749 ret = fimd_enable_vblank(mgr);
750 if (ret) {
751 DRM_ERROR("Failed to re-enable vblank [%d]\n", ret);
752 goto enable_vblank_err;
753 }
754 }
755
756 fimd_window_resume(mgr);
757
758 fimd_apply(mgr);
759
760 return 0;
761
762enable_vblank_err:
763 clk_disable_unprepare(ctx->lcd_clk);
764lcd_clk_err:
765 clk_disable_unprepare(ctx->bus_clk);
766bus_clk_err:
767 ctx->suspended = true;
768 return ret;
769}
770
771static int fimd_poweroff(struct exynos_drm_manager *mgr)
772{
773 struct fimd_context *ctx = mgr->ctx;
774
775 if (ctx->suspended)
776 return 0;
777
778 /*
779 * We need to make sure that all windows are disabled before we
780 * suspend that connector. Otherwise we might try to scan from
781 * a destroyed buffer later.
782 */
783 fimd_window_suspend(mgr);
784
785 clk_disable_unprepare(ctx->lcd_clk);
786 clk_disable_unprepare(ctx->bus_clk);
787
788 ctx->suspended = true;
789 return 0;
790}
791
080be03d
SP
792static void fimd_dpms(struct exynos_drm_manager *mgr, int mode)
793{
794 struct fimd_context *ctx = mgr->ctx;
795
796 DRM_DEBUG_KMS("%d\n", mode);
797
080be03d
SP
798 switch (mode) {
799 case DRM_MODE_DPMS_ON:
800 /*
801 * enable fimd hardware only if suspended status.
802 *
803 * P.S. fimd_dpms function would be called at booting time so
804 * clk_enable could be called double time.
805 */
806 if (ctx->suspended)
807 pm_runtime_get_sync(ctx->dev);
808 break;
809 case DRM_MODE_DPMS_STANDBY:
810 case DRM_MODE_DPMS_SUSPEND:
811 case DRM_MODE_DPMS_OFF:
812 if (!ctx->suspended)
813 pm_runtime_put_sync(ctx->dev);
814 break;
815 default:
816 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
817 break;
818 }
080be03d
SP
819}
820
1c6244c3 821static struct exynos_drm_manager_ops fimd_manager_ops = {
40c8ab4b 822 .initialize = fimd_mgr_initialize,
080be03d 823 .remove = fimd_mgr_remove,
1c6244c3 824 .dpms = fimd_dpms,
a968e727
SP
825 .mode_fixup = fimd_mode_fixup,
826 .mode_set = fimd_mode_set,
1c6244c3
SP
827 .commit = fimd_commit,
828 .enable_vblank = fimd_enable_vblank,
829 .disable_vblank = fimd_disable_vblank,
830 .wait_for_vblank = fimd_wait_for_vblank,
831 .win_mode_set = fimd_win_mode_set,
832 .win_commit = fimd_win_commit,
833 .win_disable = fimd_win_disable,
1c248b7d
ID
834};
835
677e84c1 836static struct exynos_drm_manager fimd_manager = {
080be03d
SP
837 .type = EXYNOS_DISPLAY_TYPE_LCD,
838 .ops = &fimd_manager_ops,
677e84c1
JS
839};
840
1c248b7d
ID
841static irqreturn_t fimd_irq_handler(int irq, void *dev_id)
842{
843 struct fimd_context *ctx = (struct fimd_context *)dev_id;
1c248b7d
ID
844 u32 val;
845
846 val = readl(ctx->regs + VIDINTCON1);
847
848 if (val & VIDINTCON1_INT_FRAME)
849 /* VSYNC interrupt */
850 writel(VIDINTCON1_INT_FRAME, ctx->regs + VIDINTCON1);
851
ec05da95 852 /* check the crtc is detached already from encoder */
080be03d 853 if (ctx->pipe < 0 || !ctx->drm_dev)
ec05da95 854 goto out;
483b88f8 855
080be03d
SP
856 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
857 exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
1c248b7d 858
01ce113c
P
859 /* set wait vsync event to zero and wake up queue. */
860 if (atomic_read(&ctx->wait_vsync_event)) {
861 atomic_set(&ctx->wait_vsync_event, 0);
8dd9ad5d 862 wake_up(&ctx->wait_vsync_queue);
01ce113c 863 }
ec05da95 864out:
1c248b7d
ID
865 return IRQ_HANDLED;
866}
867
562ad9f4
AH
868static int fimd_probe(struct platform_device *pdev)
869{
870 struct device *dev = &pdev->dev;
871 struct fimd_context *ctx;
562ad9f4
AH
872 struct resource *res;
873 int win;
874 int ret = -EINVAL;
1c248b7d 875
2d3f173c
SK
876 if (!dev->of_node)
877 return -ENODEV;
878
d873ab99 879 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1c248b7d
ID
880 if (!ctx)
881 return -ENOMEM;
882
bb7704d6 883 ctx->dev = dev;
a43b933b 884 ctx->suspended = true;
bb7704d6 885
1417f109
SP
886 if (of_property_read_bool(dev->of_node, "samsung,invert-vden"))
887 ctx->vidcon1 |= VIDCON1_INV_VDEN;
888 if (of_property_read_bool(dev->of_node, "samsung,invert-vclk"))
889 ctx->vidcon1 |= VIDCON1_INV_VCLK;
562ad9f4 890
a968e727
SP
891 ctx->bus_clk = devm_clk_get(dev, "fimd");
892 if (IS_ERR(ctx->bus_clk)) {
893 dev_err(dev, "failed to get bus clock\n");
894 return PTR_ERR(ctx->bus_clk);
895 }
896
897 ctx->lcd_clk = devm_clk_get(dev, "sclk_fimd");
898 if (IS_ERR(ctx->lcd_clk)) {
899 dev_err(dev, "failed to get lcd clock\n");
900 return PTR_ERR(ctx->lcd_clk);
901 }
1c248b7d 902
1c248b7d 903 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1c248b7d 904
d873ab99 905 ctx->regs = devm_ioremap_resource(dev, res);
d4ed6025
TR
906 if (IS_ERR(ctx->regs))
907 return PTR_ERR(ctx->regs);
1c248b7d 908
1977e6d8 909 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "vsync");
1c248b7d
ID
910 if (!res) {
911 dev_err(dev, "irq request failed.\n");
a4d8de5f 912 return -ENXIO;
1c248b7d
ID
913 }
914
055e0c06 915 ret = devm_request_irq(dev, res->start, fimd_irq_handler,
edc57266
SK
916 0, "drm_fimd", ctx);
917 if (ret) {
1c248b7d 918 dev_err(dev, "irq request failed.\n");
a4d8de5f 919 return ret;
1c248b7d
ID
920 }
921
18873465 922 ctx->driver_data = drm_fimd_get_driver_data(pdev);
57ed0f7b 923 init_waitqueue_head(&ctx->wait_vsync_queue);
01ce113c 924 atomic_set(&ctx->wait_vsync_event, 0);
1c248b7d 925
bb7704d6 926 platform_set_drvdata(pdev, &fimd_manager);
c32b06ef 927
080be03d
SP
928 fimd_manager.ctx = ctx;
929 exynos_drm_manager_register(&fimd_manager);
930
c32b06ef
ID
931 pm_runtime_enable(dev);
932 pm_runtime_get_sync(dev);
933
934 for (win = 0; win < WINDOWS_NR; win++)
935 fimd_clear_win(ctx, win);
936
1c248b7d 937 return 0;
1c248b7d
ID
938}
939
56550d94 940static int fimd_remove(struct platform_device *pdev)
1c248b7d 941{
cb91f6a0 942 struct device *dev = &pdev->dev;
bb7704d6
SP
943 struct exynos_drm_manager *mgr = platform_get_drvdata(pdev);
944 struct fimd_context *ctx = mgr->ctx;
1c248b7d 945
080be03d 946 exynos_drm_manager_unregister(&fimd_manager);
1c248b7d 947
cb91f6a0
JS
948 if (ctx->suspended)
949 goto out;
950
cb91f6a0
JS
951 pm_runtime_set_suspended(dev);
952 pm_runtime_put_sync(dev);
953
954out:
955 pm_runtime_disable(dev);
956
1c248b7d
ID
957 return 0;
958}
959
e30d4bcf
ID
960#ifdef CONFIG_PM_SLEEP
961static int fimd_suspend(struct device *dev)
962{
bb7704d6 963 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
e30d4bcf 964
373af0c0
ID
965 /*
966 * do not use pm_runtime_suspend(). if pm_runtime_suspend() is
967 * called here, an error would be returned by that interface
968 * because the usage_count of pm runtime is more than 1.
969 */
5d55393a 970 if (!pm_runtime_suspended(dev))
a43b933b 971 return fimd_poweroff(mgr);
5d55393a
ID
972
973 return 0;
e30d4bcf
ID
974}
975
976static int fimd_resume(struct device *dev)
977{
bb7704d6 978 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
e30d4bcf 979
373af0c0
ID
980 /*
981 * if entered to sleep when lcd panel was on, the usage_count
982 * of pm runtime would still be 1 so in this case, fimd driver
983 * should be on directly not drawing on pm runtime interface.
984 */
87244fa6
SP
985 if (pm_runtime_suspended(dev))
986 return 0;
5d55393a 987
a43b933b 988 return fimd_poweron(mgr);
e30d4bcf
ID
989}
990#endif
991
cb91f6a0
JS
992#ifdef CONFIG_PM_RUNTIME
993static int fimd_runtime_suspend(struct device *dev)
994{
bb7704d6 995 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
cb91f6a0 996
a43b933b 997 return fimd_poweroff(mgr);
cb91f6a0
JS
998}
999
1000static int fimd_runtime_resume(struct device *dev)
1001{
bb7704d6 1002 struct exynos_drm_manager *mgr = get_fimd_manager(dev);
cb91f6a0 1003
a43b933b 1004 return fimd_poweron(mgr);
cb91f6a0
JS
1005}
1006#endif
1007
1008static const struct dev_pm_ops fimd_pm_ops = {
e30d4bcf 1009 SET_SYSTEM_SLEEP_PM_OPS(fimd_suspend, fimd_resume)
cb91f6a0
JS
1010 SET_RUNTIME_PM_OPS(fimd_runtime_suspend, fimd_runtime_resume, NULL)
1011};
1012
132a5b91 1013struct platform_driver fimd_driver = {
1c248b7d 1014 .probe = fimd_probe,
56550d94 1015 .remove = fimd_remove,
1c248b7d
ID
1016 .driver = {
1017 .name = "exynos4-fb",
1018 .owner = THIS_MODULE,
cb91f6a0 1019 .pm = &fimd_pm_ops,
2d3f173c 1020 .of_match_table = fimd_driver_dt_match,
1c248b7d
ID
1021 },
1022};