blob: 0939e4652706be5a0a0ede6fc09ff1d50c92e658 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* 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 */
David Howells760285e2012-10-02 18:01:07 +010014#include <drm/drmP.h>
Inki Dae1c248b72011-10-04 19:19:01 +090015
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/clk.h>
Joonyoung Shimd636ead2012-12-14 15:48:25 +090020#include <linux/of_device.h>
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +090021#include <linux/pm_runtime.h>
Inki Dae1c248b72011-10-04 19:19:01 +090022
Vikas Sajjan7f4596f2013-03-07 12:15:21 +053023#include <video/of_display_timing.h>
Leela Krishna Amudala5a213a52012-08-08 09:44:49 +090024#include <video/samsung_fimd.h>
Inki Dae1c248b72011-10-04 19:19:01 +090025#include <drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090026
27#include "exynos_drm_drv.h"
28#include "exynos_drm_fbdev.h"
29#include "exynos_drm_crtc.h"
Inki Daebcc5cd1c2012-10-19 17:16:36 +090030#include "exynos_drm_iommu.h"
Inki Dae1c248b72011-10-04 19:19:01 +090031
32/*
33 * FIMD is stand for Fully Interactive Mobile Display and
34 * as a display controller, it transfers contents drawn on memory
35 * to a LCD Panel through Display Interfaces such as RGB or
36 * CPU Interface.
37 */
38
39/* position control register for hardware window 0, 2 ~ 4.*/
40#define VIDOSD_A(win) (VIDOSD_BASE + 0x00 + (win) * 16)
41#define VIDOSD_B(win) (VIDOSD_BASE + 0x04 + (win) * 16)
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050042/*
43 * size control register for hardware windows 0 and alpha control register
44 * for hardware windows 1 ~ 4
45 */
46#define VIDOSD_C(win) (VIDOSD_BASE + 0x08 + (win) * 16)
47/* size control register for hardware windows 1 ~ 2. */
Inki Dae1c248b72011-10-04 19:19:01 +090048#define VIDOSD_D(win) (VIDOSD_BASE + 0x0C + (win) * 16)
49
50#define VIDWx_BUF_START(win, buf) (VIDW_BUF_START(buf) + (win) * 8)
51#define VIDWx_BUF_END(win, buf) (VIDW_BUF_END(buf) + (win) * 8)
52#define VIDWx_BUF_SIZE(win, buf) (VIDW_BUF_SIZE(buf) + (win) * 4)
53
54/* color key control register for hardware window 1 ~ 4. */
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050055#define WKEYCON0_BASE(x) ((WKEYCON0 + 0x140) + ((x - 1) * 8))
Inki Dae1c248b72011-10-04 19:19:01 +090056/* color key value register for hardware window 1 ~ 4. */
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -050057#define WKEYCON1_BASE(x) ((WKEYCON1 + 0x140) + ((x - 1) * 8))
Inki Dae1c248b72011-10-04 19:19:01 +090058
59/* FIMD has totally five hardware windows. */
60#define WINDOWS_NR 5
61
62#define get_fimd_context(dev) platform_get_drvdata(to_platform_device(dev))
63
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053064struct fimd_driver_data {
65 unsigned int timing_base;
Tomasz Figade7af102013-05-01 21:02:27 +020066
67 unsigned int has_shadowcon:1;
Tomasz Figa411d9ed2013-05-01 21:02:28 +020068 unsigned int has_clksel:1;
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053069};
70
Tomasz Figa725ddea2013-05-01 21:02:29 +020071static struct fimd_driver_data s3c64xx_fimd_driver_data = {
72 .timing_base = 0x0,
73 .has_clksel = 1,
74};
75
Sachin Kamat6ecf18f2012-11-19 15:22:54 +053076static struct fimd_driver_data exynos4_fimd_driver_data = {
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053077 .timing_base = 0x0,
Tomasz Figade7af102013-05-01 21:02:27 +020078 .has_shadowcon = 1,
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053079};
80
Sachin Kamat6ecf18f2012-11-19 15:22:54 +053081static struct fimd_driver_data exynos5_fimd_driver_data = {
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053082 .timing_base = 0x20000,
Tomasz Figade7af102013-05-01 21:02:27 +020083 .has_shadowcon = 1,
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +053084};
85
Inki Dae1c248b72011-10-04 19:19:01 +090086struct fimd_win_data {
87 unsigned int offset_x;
88 unsigned int offset_y;
Inki Dae19c8b832011-10-14 13:29:46 +090089 unsigned int ovl_width;
90 unsigned int ovl_height;
91 unsigned int fb_width;
92 unsigned int fb_height;
Inki Dae1c248b72011-10-04 19:19:01 +090093 unsigned int bpp;
Inki Dae2c871122011-11-12 15:23:32 +090094 dma_addr_t dma_addr;
Inki Dae1c248b72011-10-04 19:19:01 +090095 unsigned int buf_offsize;
96 unsigned int line_size; /* bytes */
Inki Daeec05da92011-12-06 11:06:54 +090097 bool enabled;
Prathyush Kdb7e55a2012-12-06 20:16:06 +053098 bool resume;
Inki Dae1c248b72011-10-04 19:19:01 +090099};
100
101struct fimd_context {
102 struct exynos_drm_subdrv subdrv;
103 int irq;
104 struct drm_crtc *crtc;
105 struct clk *bus_clk;
106 struct clk *lcd_clk;
Inki Dae1c248b72011-10-04 19:19:01 +0900107 void __iomem *regs;
108 struct fimd_win_data win_data[WINDOWS_NR];
109 unsigned int clkdiv;
110 unsigned int default_win;
111 unsigned long irq_flags;
112 u32 vidcon0;
113 u32 vidcon1;
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900114 bool suspended;
Inki Daec32b06e2011-12-16 21:49:03 +0900115 struct mutex lock;
Prathyush K01ce1132012-12-06 20:16:04 +0530116 wait_queue_head_t wait_vsync_queue;
117 atomic_t wait_vsync_event;
Inki Dae1c248b72011-10-04 19:19:01 +0900118
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900119 struct exynos_drm_panel_info *panel;
Tomasz Figa18873462013-05-01 21:02:26 +0200120 struct fimd_driver_data *driver_data;
Inki Dae1c248b72011-10-04 19:19:01 +0900121};
122
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900123#ifdef CONFIG_OF
124static const struct of_device_id fimd_driver_dt_match[] = {
Tomasz Figa725ddea2013-05-01 21:02:29 +0200125 { .compatible = "samsung,s3c6400-fimd",
126 .data = &s3c64xx_fimd_driver_data },
Vikas Sajjan5830daf2013-02-27 16:02:58 +0530127 { .compatible = "samsung,exynos4210-fimd",
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900128 .data = &exynos4_fimd_driver_data },
Vikas Sajjan5830daf2013-02-27 16:02:58 +0530129 { .compatible = "samsung,exynos5250-fimd",
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900130 .data = &exynos5_fimd_driver_data },
131 {},
132};
133MODULE_DEVICE_TABLE(of, fimd_driver_dt_match);
134#endif
135
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530136static inline struct fimd_driver_data *drm_fimd_get_driver_data(
137 struct platform_device *pdev)
138{
Joonyoung Shimd636ead2012-12-14 15:48:25 +0900139#ifdef CONFIG_OF
140 const struct of_device_id *of_id =
141 of_match_device(fimd_driver_dt_match, &pdev->dev);
142
143 if (of_id)
144 return (struct fimd_driver_data *)of_id->data;
145#endif
146
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530147 return (struct fimd_driver_data *)
148 platform_get_device_id(pdev)->driver_data;
149}
150
Inki Dae1c248b72011-10-04 19:19:01 +0900151static bool fimd_display_is_connected(struct device *dev)
152{
Inki Dae1c248b72011-10-04 19:19:01 +0900153 DRM_DEBUG_KMS("%s\n", __FILE__);
154
155 /* TODO. */
156
157 return true;
158}
159
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900160static void *fimd_get_panel(struct device *dev)
Inki Dae1c248b72011-10-04 19:19:01 +0900161{
162 struct fimd_context *ctx = get_fimd_context(dev);
163
164 DRM_DEBUG_KMS("%s\n", __FILE__);
165
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900166 return ctx->panel;
Inki Dae1c248b72011-10-04 19:19:01 +0900167}
168
Rahul Sharma16844fb2013-06-10 14:50:00 +0530169static int fimd_check_mode(struct device *dev, struct drm_display_mode *mode)
Inki Dae1c248b72011-10-04 19:19:01 +0900170{
Inki Dae1c248b72011-10-04 19:19:01 +0900171 DRM_DEBUG_KMS("%s\n", __FILE__);
172
173 /* TODO. */
174
175 return 0;
176}
177
178static int fimd_display_power_on(struct device *dev, int mode)
179{
Inki Dae1c248b72011-10-04 19:19:01 +0900180 DRM_DEBUG_KMS("%s\n", __FILE__);
181
Inki Daeec05da92011-12-06 11:06:54 +0900182 /* TODO */
Inki Dae1c248b72011-10-04 19:19:01 +0900183
184 return 0;
185}
186
Inki Dae74ccc532011-10-19 17:23:07 +0900187static struct exynos_drm_display_ops fimd_display_ops = {
Inki Dae1c248b72011-10-04 19:19:01 +0900188 .type = EXYNOS_DISPLAY_TYPE_LCD,
189 .is_connected = fimd_display_is_connected,
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900190 .get_panel = fimd_get_panel,
Rahul Sharma16844fb2013-06-10 14:50:00 +0530191 .check_mode = fimd_check_mode,
Inki Dae1c248b72011-10-04 19:19:01 +0900192 .power_on = fimd_display_power_on,
193};
194
Inki Daeec05da92011-12-06 11:06:54 +0900195static void fimd_dpms(struct device *subdrv_dev, int mode)
196{
Inki Daec32b06e2011-12-16 21:49:03 +0900197 struct fimd_context *ctx = get_fimd_context(subdrv_dev);
198
Inki Daeec05da92011-12-06 11:06:54 +0900199 DRM_DEBUG_KMS("%s, %d\n", __FILE__, mode);
200
Inki Daec32b06e2011-12-16 21:49:03 +0900201 mutex_lock(&ctx->lock);
202
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900203 switch (mode) {
204 case DRM_MODE_DPMS_ON:
Inki Daec32b06e2011-12-16 21:49:03 +0900205 /*
206 * enable fimd hardware only if suspended status.
207 *
208 * P.S. fimd_dpms function would be called at booting time so
209 * clk_enable could be called double time.
210 */
211 if (ctx->suspended)
212 pm_runtime_get_sync(subdrv_dev);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900213 break;
214 case DRM_MODE_DPMS_STANDBY:
215 case DRM_MODE_DPMS_SUSPEND:
216 case DRM_MODE_DPMS_OFF:
Inki Dae373af0c2012-01-27 11:54:58 +0900217 if (!ctx->suspended)
218 pm_runtime_put_sync(subdrv_dev);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900219 break;
220 default:
221 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
222 break;
223 }
Inki Daec32b06e2011-12-16 21:49:03 +0900224
225 mutex_unlock(&ctx->lock);
Inki Daeec05da92011-12-06 11:06:54 +0900226}
227
228static void fimd_apply(struct device *subdrv_dev)
229{
230 struct fimd_context *ctx = get_fimd_context(subdrv_dev);
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900231 struct exynos_drm_manager *mgr = ctx->subdrv.manager;
Inki Daeec05da92011-12-06 11:06:54 +0900232 struct exynos_drm_manager_ops *mgr_ops = mgr->ops;
233 struct exynos_drm_overlay_ops *ovl_ops = mgr->overlay_ops;
234 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900235 int i;
Inki Daeec05da92011-12-06 11:06:54 +0900236
237 DRM_DEBUG_KMS("%s\n", __FILE__);
238
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900239 for (i = 0; i < WINDOWS_NR; i++) {
240 win_data = &ctx->win_data[i];
241 if (win_data->enabled && (ovl_ops && ovl_ops->commit))
242 ovl_ops->commit(subdrv_dev, i);
243 }
Inki Daeec05da92011-12-06 11:06:54 +0900244
245 if (mgr_ops && mgr_ops->commit)
246 mgr_ops->commit(subdrv_dev);
247}
248
Inki Dae1c248b72011-10-04 19:19:01 +0900249static void fimd_commit(struct device *dev)
250{
251 struct fimd_context *ctx = get_fimd_context(dev);
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900252 struct exynos_drm_panel_info *panel = ctx->panel;
253 struct fb_videomode *timing = &panel->timing;
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530254 struct fimd_driver_data *driver_data;
Inki Dae1c248b72011-10-04 19:19:01 +0900255 u32 val;
256
Tomasz Figa18873462013-05-01 21:02:26 +0200257 driver_data = ctx->driver_data;
Inki Daee30d4bc2011-12-12 16:35:20 +0900258 if (ctx->suspended)
259 return;
260
Inki Dae1c248b72011-10-04 19:19:01 +0900261 DRM_DEBUG_KMS("%s\n", __FILE__);
262
263 /* setup polarity values from machine code. */
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530264 writel(ctx->vidcon1, ctx->regs + driver_data->timing_base + VIDCON1);
Inki Dae1c248b72011-10-04 19:19:01 +0900265
266 /* setup vertical timing values. */
267 val = VIDTCON0_VBPD(timing->upper_margin - 1) |
268 VIDTCON0_VFPD(timing->lower_margin - 1) |
269 VIDTCON0_VSPW(timing->vsync_len - 1);
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530270 writel(val, ctx->regs + driver_data->timing_base + VIDTCON0);
Inki Dae1c248b72011-10-04 19:19:01 +0900271
272 /* setup horizontal timing values. */
273 val = VIDTCON1_HBPD(timing->left_margin - 1) |
274 VIDTCON1_HFPD(timing->right_margin - 1) |
275 VIDTCON1_HSPW(timing->hsync_len - 1);
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530276 writel(val, ctx->regs + driver_data->timing_base + VIDTCON1);
Inki Dae1c248b72011-10-04 19:19:01 +0900277
278 /* setup horizontal and vertical display size. */
279 val = VIDTCON2_LINEVAL(timing->yres - 1) |
Joonyoung Shimca555e52012-12-14 15:48:24 +0900280 VIDTCON2_HOZVAL(timing->xres - 1) |
281 VIDTCON2_LINEVAL_E(timing->yres - 1) |
282 VIDTCON2_HOZVAL_E(timing->xres - 1);
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +0530283 writel(val, ctx->regs + driver_data->timing_base + VIDTCON2);
Inki Dae1c248b72011-10-04 19:19:01 +0900284
285 /* setup clock source, clock divider, enable dma. */
286 val = ctx->vidcon0;
287 val &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
288
Tomasz Figa411d9ed2013-05-01 21:02:28 +0200289 if (ctx->driver_data->has_clksel) {
290 val &= ~VIDCON0_CLKSEL_MASK;
291 val |= VIDCON0_CLKSEL_LCD;
292 }
293
Inki Dae1c248b72011-10-04 19:19:01 +0900294 if (ctx->clkdiv > 1)
295 val |= VIDCON0_CLKVAL_F(ctx->clkdiv - 1) | VIDCON0_CLKDIR;
296 else
297 val &= ~VIDCON0_CLKDIR; /* 1:1 clock */
298
299 /*
300 * fields of register with prefix '_F' would be updated
301 * at vsync(same as dma start)
302 */
303 val |= VIDCON0_ENVID | VIDCON0_ENVID_F;
304 writel(val, ctx->regs + VIDCON0);
305}
306
307static int fimd_enable_vblank(struct device *dev)
308{
309 struct fimd_context *ctx = get_fimd_context(dev);
310 u32 val;
311
312 DRM_DEBUG_KMS("%s\n", __FILE__);
313
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900314 if (ctx->suspended)
315 return -EPERM;
316
Inki Dae1c248b72011-10-04 19:19:01 +0900317 if (!test_and_set_bit(0, &ctx->irq_flags)) {
318 val = readl(ctx->regs + VIDINTCON0);
319
320 val |= VIDINTCON0_INT_ENABLE;
321 val |= VIDINTCON0_INT_FRAME;
322
323 val &= ~VIDINTCON0_FRAMESEL0_MASK;
324 val |= VIDINTCON0_FRAMESEL0_VSYNC;
325 val &= ~VIDINTCON0_FRAMESEL1_MASK;
326 val |= VIDINTCON0_FRAMESEL1_NONE;
327
328 writel(val, ctx->regs + VIDINTCON0);
329 }
330
331 return 0;
332}
333
334static void fimd_disable_vblank(struct device *dev)
335{
336 struct fimd_context *ctx = get_fimd_context(dev);
337 u32 val;
338
339 DRM_DEBUG_KMS("%s\n", __FILE__);
340
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +0900341 if (ctx->suspended)
342 return;
343
Inki Dae1c248b72011-10-04 19:19:01 +0900344 if (test_and_clear_bit(0, &ctx->irq_flags)) {
345 val = readl(ctx->regs + VIDINTCON0);
346
347 val &= ~VIDINTCON0_INT_FRAME;
348 val &= ~VIDINTCON0_INT_ENABLE;
349
350 writel(val, ctx->regs + VIDINTCON0);
351 }
352}
353
Prathyush K07033972012-12-06 20:16:02 +0530354static void fimd_wait_for_vblank(struct device *dev)
355{
356 struct fimd_context *ctx = get_fimd_context(dev);
Prathyush K07033972012-12-06 20:16:02 +0530357
Prathyush K01ce1132012-12-06 20:16:04 +0530358 if (ctx->suspended)
359 return;
360
361 atomic_set(&ctx->wait_vsync_event, 1);
362
363 /*
364 * wait for FIMD to signal VSYNC interrupt or return after
365 * timeout which is set to 50ms (refresh rate of 20).
366 */
367 if (!wait_event_timeout(ctx->wait_vsync_queue,
368 !atomic_read(&ctx->wait_vsync_event),
369 DRM_HZ/20))
Prathyush K07033972012-12-06 20:16:02 +0530370 DRM_DEBUG_KMS("vblank wait timed out.\n");
371}
372
Inki Dae1c248b72011-10-04 19:19:01 +0900373static struct exynos_drm_manager_ops fimd_manager_ops = {
Inki Daeec05da92011-12-06 11:06:54 +0900374 .dpms = fimd_dpms,
375 .apply = fimd_apply,
Inki Dae1c248b72011-10-04 19:19:01 +0900376 .commit = fimd_commit,
377 .enable_vblank = fimd_enable_vblank,
378 .disable_vblank = fimd_disable_vblank,
Prathyush K07033972012-12-06 20:16:02 +0530379 .wait_for_vblank = fimd_wait_for_vblank,
Inki Dae1c248b72011-10-04 19:19:01 +0900380};
381
382static void fimd_win_mode_set(struct device *dev,
383 struct exynos_drm_overlay *overlay)
384{
385 struct fimd_context *ctx = get_fimd_context(dev);
386 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900387 int win;
Inki Dae19c8b832011-10-14 13:29:46 +0900388 unsigned long offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900389
390 DRM_DEBUG_KMS("%s\n", __FILE__);
391
392 if (!overlay) {
393 dev_err(dev, "overlay is NULL\n");
394 return;
395 }
396
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900397 win = overlay->zpos;
398 if (win == DEFAULT_ZPOS)
399 win = ctx->default_win;
400
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200401 if (win < 0 || win >= WINDOWS_NR)
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900402 return;
403
Inki Dae19c8b832011-10-14 13:29:46 +0900404 offset = overlay->fb_x * (overlay->bpp >> 3);
405 offset += overlay->fb_y * overlay->pitch;
406
407 DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, overlay->pitch);
408
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900409 win_data = &ctx->win_data[win];
Inki Dae1c248b72011-10-04 19:19:01 +0900410
Inki Dae19c8b832011-10-14 13:29:46 +0900411 win_data->offset_x = overlay->crtc_x;
412 win_data->offset_y = overlay->crtc_y;
413 win_data->ovl_width = overlay->crtc_width;
414 win_data->ovl_height = overlay->crtc_height;
415 win_data->fb_width = overlay->fb_width;
416 win_data->fb_height = overlay->fb_height;
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900417 win_data->dma_addr = overlay->dma_addr[0] + offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900418 win_data->bpp = overlay->bpp;
Inki Dae19c8b832011-10-14 13:29:46 +0900419 win_data->buf_offsize = (overlay->fb_width - overlay->crtc_width) *
420 (overlay->bpp >> 3);
421 win_data->line_size = overlay->crtc_width * (overlay->bpp >> 3);
422
423 DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
424 win_data->offset_x, win_data->offset_y);
425 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
426 win_data->ovl_width, win_data->ovl_height);
YoungJun Choddd8e952012-12-10 15:44:58 +0900427 DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
Inki Dae19c8b832011-10-14 13:29:46 +0900428 DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
429 overlay->fb_width, overlay->crtc_width);
Inki Dae1c248b72011-10-04 19:19:01 +0900430}
431
432static void fimd_win_set_pixfmt(struct device *dev, unsigned int win)
433{
434 struct fimd_context *ctx = get_fimd_context(dev);
435 struct fimd_win_data *win_data = &ctx->win_data[win];
436 unsigned long val;
437
438 DRM_DEBUG_KMS("%s\n", __FILE__);
439
440 val = WINCONx_ENWIN;
441
442 switch (win_data->bpp) {
443 case 1:
444 val |= WINCON0_BPPMODE_1BPP;
445 val |= WINCONx_BITSWP;
446 val |= WINCONx_BURSTLEN_4WORD;
447 break;
448 case 2:
449 val |= WINCON0_BPPMODE_2BPP;
450 val |= WINCONx_BITSWP;
451 val |= WINCONx_BURSTLEN_8WORD;
452 break;
453 case 4:
454 val |= WINCON0_BPPMODE_4BPP;
455 val |= WINCONx_BITSWP;
456 val |= WINCONx_BURSTLEN_8WORD;
457 break;
458 case 8:
459 val |= WINCON0_BPPMODE_8BPP_PALETTE;
460 val |= WINCONx_BURSTLEN_8WORD;
461 val |= WINCONx_BYTSWP;
462 break;
463 case 16:
464 val |= WINCON0_BPPMODE_16BPP_565;
465 val |= WINCONx_HAWSWP;
466 val |= WINCONx_BURSTLEN_16WORD;
467 break;
468 case 24:
469 val |= WINCON0_BPPMODE_24BPP_888;
470 val |= WINCONx_WSWP;
471 val |= WINCONx_BURSTLEN_16WORD;
472 break;
473 case 32:
474 val |= WINCON1_BPPMODE_28BPP_A4888
475 | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
476 val |= WINCONx_WSWP;
477 val |= WINCONx_BURSTLEN_16WORD;
478 break;
479 default:
480 DRM_DEBUG_KMS("invalid pixel size so using unpacked 24bpp.\n");
481
482 val |= WINCON0_BPPMODE_24BPP_888;
483 val |= WINCONx_WSWP;
484 val |= WINCONx_BURSTLEN_16WORD;
485 break;
486 }
487
488 DRM_DEBUG_KMS("bpp = %d\n", win_data->bpp);
489
490 writel(val, ctx->regs + WINCON(win));
491}
492
493static void fimd_win_set_colkey(struct device *dev, unsigned int win)
494{
495 struct fimd_context *ctx = get_fimd_context(dev);
496 unsigned int keycon0 = 0, keycon1 = 0;
497
498 DRM_DEBUG_KMS("%s\n", __FILE__);
499
500 keycon0 = ~(WxKEYCON0_KEYBL_EN | WxKEYCON0_KEYEN_F |
501 WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
502
503 keycon1 = WxKEYCON1_COLVAL(0xffffffff);
504
505 writel(keycon0, ctx->regs + WKEYCON0_BASE(win));
506 writel(keycon1, ctx->regs + WKEYCON1_BASE(win));
507}
508
Tomasz Figade7af102013-05-01 21:02:27 +0200509/**
510 * shadow_protect_win() - disable updating values from shadow registers at vsync
511 *
512 * @win: window to protect registers for
513 * @protect: 1 to protect (disable updates)
514 */
515static void fimd_shadow_protect_win(struct fimd_context *ctx,
516 int win, bool protect)
517{
518 u32 reg, bits, val;
519
520 if (ctx->driver_data->has_shadowcon) {
521 reg = SHADOWCON;
522 bits = SHADOWCON_WINx_PROTECT(win);
523 } else {
524 reg = PRTCON;
525 bits = PRTCON_PROTECT;
526 }
527
528 val = readl(ctx->regs + reg);
529 if (protect)
530 val |= bits;
531 else
532 val &= ~bits;
533 writel(val, ctx->regs + reg);
534}
535
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900536static void fimd_win_commit(struct device *dev, int zpos)
Inki Dae1c248b72011-10-04 19:19:01 +0900537{
538 struct fimd_context *ctx = get_fimd_context(dev);
539 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900540 int win = zpos;
Inki Dae1c248b72011-10-04 19:19:01 +0900541 unsigned long val, alpha, size;
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900542 unsigned int last_x;
543 unsigned int last_y;
Inki Dae1c248b72011-10-04 19:19:01 +0900544
545 DRM_DEBUG_KMS("%s\n", __FILE__);
546
Inki Daee30d4bc2011-12-12 16:35:20 +0900547 if (ctx->suspended)
548 return;
549
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900550 if (win == DEFAULT_ZPOS)
551 win = ctx->default_win;
552
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200553 if (win < 0 || win >= WINDOWS_NR)
Inki Dae1c248b72011-10-04 19:19:01 +0900554 return;
555
556 win_data = &ctx->win_data[win];
557
558 /*
Tomasz Figade7af102013-05-01 21:02:27 +0200559 * SHADOWCON/PRTCON register is used for enabling timing.
Inki Dae1c248b72011-10-04 19:19:01 +0900560 *
561 * for example, once only width value of a register is set,
562 * if the dma is started then fimd hardware could malfunction so
563 * with protect window setting, the register fields with prefix '_F'
564 * wouldn't be updated at vsync also but updated once unprotect window
565 * is set.
566 */
567
568 /* protect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200569 fimd_shadow_protect_win(ctx, win, true);
Inki Dae1c248b72011-10-04 19:19:01 +0900570
571 /* buffer start address */
Inki Dae2c871122011-11-12 15:23:32 +0900572 val = (unsigned long)win_data->dma_addr;
Inki Dae1c248b72011-10-04 19:19:01 +0900573 writel(val, ctx->regs + VIDWx_BUF_START(win, 0));
574
575 /* buffer end address */
Inki Dae19c8b832011-10-14 13:29:46 +0900576 size = win_data->fb_width * win_data->ovl_height * (win_data->bpp >> 3);
Inki Dae2c871122011-11-12 15:23:32 +0900577 val = (unsigned long)(win_data->dma_addr + size);
Inki Dae1c248b72011-10-04 19:19:01 +0900578 writel(val, ctx->regs + VIDWx_BUF_END(win, 0));
579
580 DRM_DEBUG_KMS("start addr = 0x%lx, end addr = 0x%lx, size = 0x%lx\n",
Inki Dae2c871122011-11-12 15:23:32 +0900581 (unsigned long)win_data->dma_addr, val, size);
Inki Dae19c8b832011-10-14 13:29:46 +0900582 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
583 win_data->ovl_width, win_data->ovl_height);
Inki Dae1c248b72011-10-04 19:19:01 +0900584
585 /* buffer size */
586 val = VIDW_BUF_SIZE_OFFSET(win_data->buf_offsize) |
Joonyoung Shimca555e52012-12-14 15:48:24 +0900587 VIDW_BUF_SIZE_PAGEWIDTH(win_data->line_size) |
588 VIDW_BUF_SIZE_OFFSET_E(win_data->buf_offsize) |
589 VIDW_BUF_SIZE_PAGEWIDTH_E(win_data->line_size);
Inki Dae1c248b72011-10-04 19:19:01 +0900590 writel(val, ctx->regs + VIDWx_BUF_SIZE(win, 0));
591
592 /* OSD position */
593 val = VIDOSDxA_TOPLEFT_X(win_data->offset_x) |
Joonyoung Shimca555e52012-12-14 15:48:24 +0900594 VIDOSDxA_TOPLEFT_Y(win_data->offset_y) |
595 VIDOSDxA_TOPLEFT_X_E(win_data->offset_x) |
596 VIDOSDxA_TOPLEFT_Y_E(win_data->offset_y);
Inki Dae1c248b72011-10-04 19:19:01 +0900597 writel(val, ctx->regs + VIDOSD_A(win));
598
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900599 last_x = win_data->offset_x + win_data->ovl_width;
600 if (last_x)
601 last_x--;
602 last_y = win_data->offset_y + win_data->ovl_height;
603 if (last_y)
604 last_y--;
605
Joonyoung Shimca555e52012-12-14 15:48:24 +0900606 val = VIDOSDxB_BOTRIGHT_X(last_x) | VIDOSDxB_BOTRIGHT_Y(last_y) |
607 VIDOSDxB_BOTRIGHT_X_E(last_x) | VIDOSDxB_BOTRIGHT_Y_E(last_y);
608
Inki Dae1c248b72011-10-04 19:19:01 +0900609 writel(val, ctx->regs + VIDOSD_B(win));
610
Inki Dae19c8b832011-10-14 13:29:46 +0900611 DRM_DEBUG_KMS("osd pos: tx = %d, ty = %d, bx = %d, by = %d\n",
Joonyoung Shimf56aad32012-12-14 15:48:23 +0900612 win_data->offset_x, win_data->offset_y, last_x, last_y);
Inki Dae1c248b72011-10-04 19:19:01 +0900613
614 /* hardware window 0 doesn't support alpha channel. */
615 if (win != 0) {
616 /* OSD alpha */
617 alpha = VIDISD14C_ALPHA1_R(0xf) |
618 VIDISD14C_ALPHA1_G(0xf) |
619 VIDISD14C_ALPHA1_B(0xf);
620
621 writel(alpha, ctx->regs + VIDOSD_C(win));
622 }
623
624 /* OSD size */
625 if (win != 3 && win != 4) {
626 u32 offset = VIDOSD_D(win);
627 if (win == 0)
Leela Krishna Amudala0f10cf12013-03-07 23:28:52 -0500628 offset = VIDOSD_C(win);
Inki Dae19c8b832011-10-14 13:29:46 +0900629 val = win_data->ovl_width * win_data->ovl_height;
Inki Dae1c248b72011-10-04 19:19:01 +0900630 writel(val, ctx->regs + offset);
631
632 DRM_DEBUG_KMS("osd size = 0x%x\n", (unsigned int)val);
633 }
634
635 fimd_win_set_pixfmt(dev, win);
636
637 /* hardware window 0 doesn't support color key. */
638 if (win != 0)
639 fimd_win_set_colkey(dev, win);
640
Inki Daeec05da92011-12-06 11:06:54 +0900641 /* wincon */
642 val = readl(ctx->regs + WINCON(win));
643 val |= WINCONx_ENWIN;
644 writel(val, ctx->regs + WINCON(win));
645
Inki Dae1c248b72011-10-04 19:19:01 +0900646 /* Enable DMA channel and unprotect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200647 fimd_shadow_protect_win(ctx, win, false);
648
649 if (ctx->driver_data->has_shadowcon) {
650 val = readl(ctx->regs + SHADOWCON);
651 val |= SHADOWCON_CHx_ENABLE(win);
652 writel(val, ctx->regs + SHADOWCON);
653 }
Inki Daeec05da92011-12-06 11:06:54 +0900654
655 win_data->enabled = true;
Inki Dae1c248b72011-10-04 19:19:01 +0900656}
657
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900658static void fimd_win_disable(struct device *dev, int zpos)
Inki Dae1c248b72011-10-04 19:19:01 +0900659{
660 struct fimd_context *ctx = get_fimd_context(dev);
Inki Daeec05da92011-12-06 11:06:54 +0900661 struct fimd_win_data *win_data;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900662 int win = zpos;
Inki Dae1c248b72011-10-04 19:19:01 +0900663 u32 val;
664
665 DRM_DEBUG_KMS("%s\n", __FILE__);
666
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900667 if (win == DEFAULT_ZPOS)
668 win = ctx->default_win;
669
Krzysztof Kozlowski37b006e2013-05-27 11:56:26 +0200670 if (win < 0 || win >= WINDOWS_NR)
Inki Dae1c248b72011-10-04 19:19:01 +0900671 return;
672
Inki Daeec05da92011-12-06 11:06:54 +0900673 win_data = &ctx->win_data[win];
674
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530675 if (ctx->suspended) {
676 /* do not resume this window*/
677 win_data->resume = false;
678 return;
679 }
680
Inki Dae1c248b72011-10-04 19:19:01 +0900681 /* protect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200682 fimd_shadow_protect_win(ctx, win, true);
Inki Dae1c248b72011-10-04 19:19:01 +0900683
684 /* wincon */
685 val = readl(ctx->regs + WINCON(win));
686 val &= ~WINCONx_ENWIN;
687 writel(val, ctx->regs + WINCON(win));
688
689 /* unprotect windows */
Tomasz Figade7af102013-05-01 21:02:27 +0200690 if (ctx->driver_data->has_shadowcon) {
691 val = readl(ctx->regs + SHADOWCON);
692 val &= ~SHADOWCON_CHx_ENABLE(win);
693 writel(val, ctx->regs + SHADOWCON);
694 }
695
696 fimd_shadow_protect_win(ctx, win, false);
Inki Daeec05da92011-12-06 11:06:54 +0900697
698 win_data->enabled = false;
Inki Dae1c248b72011-10-04 19:19:01 +0900699}
700
701static struct exynos_drm_overlay_ops fimd_overlay_ops = {
702 .mode_set = fimd_win_mode_set,
703 .commit = fimd_win_commit,
704 .disable = fimd_win_disable,
705};
706
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900707static struct exynos_drm_manager fimd_manager = {
708 .pipe = -1,
709 .ops = &fimd_manager_ops,
710 .overlay_ops = &fimd_overlay_ops,
711 .display_ops = &fimd_display_ops,
712};
713
Inki Dae1c248b72011-10-04 19:19:01 +0900714static irqreturn_t fimd_irq_handler(int irq, void *dev_id)
715{
716 struct fimd_context *ctx = (struct fimd_context *)dev_id;
717 struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
718 struct drm_device *drm_dev = subdrv->drm_dev;
Joonyoung Shim677e84c2012-04-05 20:49:27 +0900719 struct exynos_drm_manager *manager = subdrv->manager;
Inki Dae1c248b72011-10-04 19:19:01 +0900720 u32 val;
721
722 val = readl(ctx->regs + VIDINTCON1);
723
724 if (val & VIDINTCON1_INT_FRAME)
725 /* VSYNC interrupt */
726 writel(VIDINTCON1_INT_FRAME, ctx->regs + VIDINTCON1);
727
Inki Daeec05da92011-12-06 11:06:54 +0900728 /* check the crtc is detached already from encoder */
729 if (manager->pipe < 0)
730 goto out;
Inki Dae483b88f2011-11-11 21:28:00 +0900731
Inki Dae1c248b72011-10-04 19:19:01 +0900732 drm_handle_vblank(drm_dev, manager->pipe);
Rahul Sharma663d8762013-01-03 05:44:04 -0500733 exynos_drm_crtc_finish_pageflip(drm_dev, manager->pipe);
Inki Dae1c248b72011-10-04 19:19:01 +0900734
Prathyush K01ce1132012-12-06 20:16:04 +0530735 /* set wait vsync event to zero and wake up queue. */
736 if (atomic_read(&ctx->wait_vsync_event)) {
737 atomic_set(&ctx->wait_vsync_event, 0);
738 DRM_WAKEUP(&ctx->wait_vsync_queue);
739 }
Inki Daeec05da92011-12-06 11:06:54 +0900740out:
Inki Dae1c248b72011-10-04 19:19:01 +0900741 return IRQ_HANDLED;
742}
743
Inki Dae41c24342011-10-14 13:29:48 +0900744static int fimd_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
Inki Dae1c248b72011-10-04 19:19:01 +0900745{
Inki Dae1c248b72011-10-04 19:19:01 +0900746 DRM_DEBUG_KMS("%s\n", __FILE__);
747
748 /*
749 * enable drm irq mode.
750 * - with irq_enabled = 1, we can use the vblank feature.
751 *
752 * P.S. note that we wouldn't use drm irq handler but
753 * just specific driver own one instead because
754 * drm framework supports only one irq handler.
755 */
756 drm_dev->irq_enabled = 1;
757
Inki Daeec05da92011-12-06 11:06:54 +0900758 /*
759 * with vblank_disable_allowed = 1, vblank interrupt will be disabled
760 * by drm timer once a current process gives up ownership of
761 * vblank event.(after drm_vblank_put function is called)
762 */
763 drm_dev->vblank_disable_allowed = 1;
764
Inki Daebcc5cd1c2012-10-19 17:16:36 +0900765 /* attach this sub driver to iommu mapping if supported. */
766 if (is_drm_iommu_supported(drm_dev))
767 drm_iommu_attach_device(drm_dev, dev);
768
Inki Dae1c248b72011-10-04 19:19:01 +0900769 return 0;
770}
771
Inki Dae29cb6022012-09-05 14:12:06 +0900772static void fimd_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
Inki Dae1c248b72011-10-04 19:19:01 +0900773{
Inki Dae1c248b72011-10-04 19:19:01 +0900774 DRM_DEBUG_KMS("%s\n", __FILE__);
775
Inki Daebcc5cd1c2012-10-19 17:16:36 +0900776 /* detach this sub driver from iommu mapping if supported. */
777 if (is_drm_iommu_supported(drm_dev))
778 drm_iommu_detach_device(drm_dev, dev);
Inki Dae1c248b72011-10-04 19:19:01 +0900779}
780
781static int fimd_calc_clkdiv(struct fimd_context *ctx,
782 struct fb_videomode *timing)
783{
784 unsigned long clk = clk_get_rate(ctx->lcd_clk);
785 u32 retrace;
786 u32 clkdiv;
787 u32 best_framerate = 0;
788 u32 framerate;
789
790 DRM_DEBUG_KMS("%s\n", __FILE__);
791
792 retrace = timing->left_margin + timing->hsync_len +
793 timing->right_margin + timing->xres;
794 retrace *= timing->upper_margin + timing->vsync_len +
795 timing->lower_margin + timing->yres;
796
797 /* default framerate is 60Hz */
798 if (!timing->refresh)
799 timing->refresh = 60;
800
801 clk /= retrace;
802
803 for (clkdiv = 1; clkdiv < 0x100; clkdiv++) {
804 int tmp;
805
806 /* get best framerate */
807 framerate = clk / clkdiv;
808 tmp = timing->refresh - framerate;
809 if (tmp < 0) {
810 best_framerate = framerate;
811 continue;
812 } else {
813 if (!best_framerate)
814 best_framerate = framerate;
815 else if (tmp < (best_framerate - framerate))
816 best_framerate = framerate;
817 break;
818 }
819 }
820
821 return clkdiv;
822}
823
824static void fimd_clear_win(struct fimd_context *ctx, int win)
825{
Inki Dae1c248b72011-10-04 19:19:01 +0900826 DRM_DEBUG_KMS("%s\n", __FILE__);
827
828 writel(0, ctx->regs + WINCON(win));
829 writel(0, ctx->regs + VIDOSD_A(win));
830 writel(0, ctx->regs + VIDOSD_B(win));
831 writel(0, ctx->regs + VIDOSD_C(win));
832
833 if (win == 1 || win == 2)
834 writel(0, ctx->regs + VIDOSD_D(win));
835
Tomasz Figade7af102013-05-01 21:02:27 +0200836 fimd_shadow_protect_win(ctx, win, false);
Inki Dae1c248b72011-10-04 19:19:01 +0900837}
838
Inki Dae5d553932012-08-17 17:08:04 +0900839static int fimd_clock(struct fimd_context *ctx, bool enable)
Inki Dae373af0c2012-01-27 11:54:58 +0900840{
Inki Dae373af0c2012-01-27 11:54:58 +0900841 DRM_DEBUG_KMS("%s\n", __FILE__);
842
Inki Dae373af0c2012-01-27 11:54:58 +0900843 if (enable) {
844 int ret;
845
Vikas Sajjan11963a62013-04-23 17:19:50 +0530846 ret = clk_prepare_enable(ctx->bus_clk);
Inki Dae373af0c2012-01-27 11:54:58 +0900847 if (ret < 0)
848 return ret;
849
Vikas Sajjan11963a62013-04-23 17:19:50 +0530850 ret = clk_prepare_enable(ctx->lcd_clk);
Inki Dae373af0c2012-01-27 11:54:58 +0900851 if (ret < 0) {
Vikas Sajjan11963a62013-04-23 17:19:50 +0530852 clk_disable_unprepare(ctx->bus_clk);
Inki Dae373af0c2012-01-27 11:54:58 +0900853 return ret;
854 }
Inki Dae5d553932012-08-17 17:08:04 +0900855 } else {
Vikas Sajjan11963a62013-04-23 17:19:50 +0530856 clk_disable_unprepare(ctx->lcd_clk);
857 clk_disable_unprepare(ctx->bus_clk);
Inki Dae5d553932012-08-17 17:08:04 +0900858 }
859
860 return 0;
861}
862
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530863static void fimd_window_suspend(struct device *dev)
864{
865 struct fimd_context *ctx = get_fimd_context(dev);
866 struct fimd_win_data *win_data;
867 int i;
868
869 for (i = 0; i < WINDOWS_NR; i++) {
870 win_data = &ctx->win_data[i];
871 win_data->resume = win_data->enabled;
872 fimd_win_disable(dev, i);
873 }
874 fimd_wait_for_vblank(dev);
875}
876
877static void fimd_window_resume(struct device *dev)
878{
879 struct fimd_context *ctx = get_fimd_context(dev);
880 struct fimd_win_data *win_data;
881 int i;
882
883 for (i = 0; i < WINDOWS_NR; i++) {
884 win_data = &ctx->win_data[i];
885 win_data->enabled = win_data->resume;
886 win_data->resume = false;
887 }
888}
889
Inki Dae5d553932012-08-17 17:08:04 +0900890static int fimd_activate(struct fimd_context *ctx, bool enable)
891{
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530892 struct device *dev = ctx->subdrv.dev;
Inki Dae5d553932012-08-17 17:08:04 +0900893 if (enable) {
894 int ret;
Inki Dae5d553932012-08-17 17:08:04 +0900895
896 ret = fimd_clock(ctx, true);
897 if (ret < 0)
898 return ret;
Inki Dae373af0c2012-01-27 11:54:58 +0900899
900 ctx->suspended = false;
901
902 /* if vblank was enabled status, enable it again. */
903 if (test_and_clear_bit(0, &ctx->irq_flags))
904 fimd_enable_vblank(dev);
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530905
906 fimd_window_resume(dev);
Inki Dae373af0c2012-01-27 11:54:58 +0900907 } else {
Prathyush Kdb7e55a2012-12-06 20:16:06 +0530908 fimd_window_suspend(dev);
909
Inki Dae5d553932012-08-17 17:08:04 +0900910 fimd_clock(ctx, false);
Inki Dae373af0c2012-01-27 11:54:58 +0900911 ctx->suspended = true;
912 }
913
914 return 0;
915}
916
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800917static int fimd_probe(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900918{
919 struct device *dev = &pdev->dev;
920 struct fimd_context *ctx;
921 struct exynos_drm_subdrv *subdrv;
922 struct exynos_drm_fimd_pdata *pdata;
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900923 struct exynos_drm_panel_info *panel;
Inki Dae1c248b72011-10-04 19:19:01 +0900924 struct resource *res;
925 int win;
926 int ret = -EINVAL;
927
928 DRM_DEBUG_KMS("%s\n", __FILE__);
929
Seung-Woo Kimd873ab92013-05-22 21:14:14 +0900930 if (dev->of_node) {
Vikas Sajjan7f4596f2013-03-07 12:15:21 +0530931 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
932 if (!pdata) {
933 DRM_ERROR("memory allocation for pdata failed\n");
934 return -ENOMEM;
935 }
936
937 ret = of_get_fb_videomode(dev->of_node, &pdata->panel.timing,
938 OF_USE_NATIVE_MODE);
939 if (ret) {
940 DRM_ERROR("failed: of_get_fb_videomode() : %d\n", ret);
941 return ret;
942 }
943 } else {
Seung-Woo Kimd873ab92013-05-22 21:14:14 +0900944 pdata = dev->platform_data;
Vikas Sajjan7f4596f2013-03-07 12:15:21 +0530945 if (!pdata) {
946 DRM_ERROR("no platform data specified\n");
947 return -EINVAL;
948 }
Inki Dae1c248b72011-10-04 19:19:01 +0900949 }
950
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900951 panel = &pdata->panel;
952 if (!panel) {
953 dev_err(dev, "panel is null.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900954 return -EINVAL;
955 }
956
Seung-Woo Kimd873ab92013-05-22 21:14:14 +0900957 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Inki Dae1c248b72011-10-04 19:19:01 +0900958 if (!ctx)
959 return -ENOMEM;
960
Sachin Kamata4d8de52012-11-26 09:47:14 +0530961 ctx->bus_clk = devm_clk_get(dev, "fimd");
Inki Dae1c248b72011-10-04 19:19:01 +0900962 if (IS_ERR(ctx->bus_clk)) {
963 dev_err(dev, "failed to get bus clock\n");
Sachin Kamata4d8de52012-11-26 09:47:14 +0530964 return PTR_ERR(ctx->bus_clk);
Inki Dae1c248b72011-10-04 19:19:01 +0900965 }
966
Sachin Kamata4d8de52012-11-26 09:47:14 +0530967 ctx->lcd_clk = devm_clk_get(dev, "sclk_fimd");
Inki Dae1c248b72011-10-04 19:19:01 +0900968 if (IS_ERR(ctx->lcd_clk)) {
969 dev_err(dev, "failed to get lcd clock\n");
Sachin Kamata4d8de52012-11-26 09:47:14 +0530970 return PTR_ERR(ctx->lcd_clk);
Inki Dae1c248b72011-10-04 19:19:01 +0900971 }
972
Inki Dae1c248b72011-10-04 19:19:01 +0900973 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Inki Dae1c248b72011-10-04 19:19:01 +0900974
Seung-Woo Kimd873ab92013-05-22 21:14:14 +0900975 ctx->regs = devm_ioremap_resource(dev, res);
Thierry Redingd4ed6022013-01-21 11:09:02 +0100976 if (IS_ERR(ctx->regs))
977 return PTR_ERR(ctx->regs);
Inki Dae1c248b72011-10-04 19:19:01 +0900978
Vikas Sajjan1977e6d2013-04-02 17:53:01 +0900979 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "vsync");
Inki Dae1c248b72011-10-04 19:19:01 +0900980 if (!res) {
981 dev_err(dev, "irq request failed.\n");
Sachin Kamata4d8de52012-11-26 09:47:14 +0530982 return -ENXIO;
Inki Dae1c248b72011-10-04 19:19:01 +0900983 }
984
985 ctx->irq = res->start;
986
Seung-Woo Kimd873ab92013-05-22 21:14:14 +0900987 ret = devm_request_irq(dev, ctx->irq, fimd_irq_handler,
Sachin Kamatedc57262012-06-19 11:47:39 +0530988 0, "drm_fimd", ctx);
989 if (ret) {
Inki Dae1c248b72011-10-04 19:19:01 +0900990 dev_err(dev, "irq request failed.\n");
Sachin Kamata4d8de52012-11-26 09:47:14 +0530991 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900992 }
993
Tomasz Figa18873462013-05-01 21:02:26 +0200994 ctx->driver_data = drm_fimd_get_driver_data(pdev);
Inki Dae1c248b72011-10-04 19:19:01 +0900995 ctx->vidcon0 = pdata->vidcon0;
996 ctx->vidcon1 = pdata->vidcon1;
997 ctx->default_win = pdata->default_win;
Eun-Chul Kim607c50d2012-02-14 15:59:46 +0900998 ctx->panel = panel;
Prathyush K01ce1132012-12-06 20:16:04 +0530999 DRM_INIT_WAITQUEUE(&ctx->wait_vsync_queue);
1000 atomic_set(&ctx->wait_vsync_event, 0);
Inki Dae1c248b72011-10-04 19:19:01 +09001001
Inki Dae1c248b72011-10-04 19:19:01 +09001002 subdrv = &ctx->subdrv;
1003
Joonyoung Shim677e84c2012-04-05 20:49:27 +09001004 subdrv->dev = dev;
1005 subdrv->manager = &fimd_manager;
Inki Dae1c248b72011-10-04 19:19:01 +09001006 subdrv->probe = fimd_subdrv_probe;
1007 subdrv->remove = fimd_subdrv_remove;
Inki Dae1c248b72011-10-04 19:19:01 +09001008
Inki Daec32b06e2011-12-16 21:49:03 +09001009 mutex_init(&ctx->lock);
1010
Inki Dae1c248b72011-10-04 19:19:01 +09001011 platform_set_drvdata(pdev, ctx);
Inki Daec32b06e2011-12-16 21:49:03 +09001012
Inki Daec32b06e2011-12-16 21:49:03 +09001013 pm_runtime_enable(dev);
1014 pm_runtime_get_sync(dev);
1015
Marek Szyprowski0d8ce3a2012-03-08 10:28:56 +09001016 ctx->clkdiv = fimd_calc_clkdiv(ctx, &panel->timing);
1017 panel->timing.pixclock = clk_get_rate(ctx->lcd_clk) / ctx->clkdiv;
1018
1019 DRM_DEBUG_KMS("pixel clock = %d, clkdiv = %d\n",
1020 panel->timing.pixclock, ctx->clkdiv);
1021
Inki Daec32b06e2011-12-16 21:49:03 +09001022 for (win = 0; win < WINDOWS_NR; win++)
1023 fimd_clear_win(ctx, win);
1024
Inki Dae1c248b72011-10-04 19:19:01 +09001025 exynos_drm_subdrv_register(subdrv);
1026
1027 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +09001028}
1029
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001030static int fimd_remove(struct platform_device *pdev)
Inki Dae1c248b72011-10-04 19:19:01 +09001031{
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001032 struct device *dev = &pdev->dev;
Inki Dae1c248b72011-10-04 19:19:01 +09001033 struct fimd_context *ctx = platform_get_drvdata(pdev);
1034
1035 DRM_DEBUG_KMS("%s\n", __FILE__);
1036
1037 exynos_drm_subdrv_unregister(&ctx->subdrv);
1038
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001039 if (ctx->suspended)
1040 goto out;
1041
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001042 pm_runtime_set_suspended(dev);
1043 pm_runtime_put_sync(dev);
1044
1045out:
1046 pm_runtime_disable(dev);
1047
Inki Dae1c248b72011-10-04 19:19:01 +09001048 return 0;
1049}
1050
Inki Daee30d4bc2011-12-12 16:35:20 +09001051#ifdef CONFIG_PM_SLEEP
1052static int fimd_suspend(struct device *dev)
1053{
Inki Dae373af0c2012-01-27 11:54:58 +09001054 struct fimd_context *ctx = get_fimd_context(dev);
Inki Daee30d4bc2011-12-12 16:35:20 +09001055
Inki Dae373af0c2012-01-27 11:54:58 +09001056 /*
1057 * do not use pm_runtime_suspend(). if pm_runtime_suspend() is
1058 * called here, an error would be returned by that interface
1059 * because the usage_count of pm runtime is more than 1.
1060 */
Inki Dae5d553932012-08-17 17:08:04 +09001061 if (!pm_runtime_suspended(dev))
1062 return fimd_activate(ctx, false);
1063
1064 return 0;
Inki Daee30d4bc2011-12-12 16:35:20 +09001065}
1066
1067static int fimd_resume(struct device *dev)
1068{
Inki Dae373af0c2012-01-27 11:54:58 +09001069 struct fimd_context *ctx = get_fimd_context(dev);
Inki Daee30d4bc2011-12-12 16:35:20 +09001070
Inki Dae373af0c2012-01-27 11:54:58 +09001071 /*
1072 * if entered to sleep when lcd panel was on, the usage_count
1073 * of pm runtime would still be 1 so in this case, fimd driver
1074 * should be on directly not drawing on pm runtime interface.
1075 */
Prathyush K28998af2012-12-27 06:40:13 -05001076 if (!pm_runtime_suspended(dev)) {
Inki Dae5d553932012-08-17 17:08:04 +09001077 int ret;
1078
1079 ret = fimd_activate(ctx, true);
1080 if (ret < 0)
1081 return ret;
1082
1083 /*
1084 * in case of dpms on(standby), fimd_apply function will
1085 * be called by encoder's dpms callback to update fimd's
1086 * registers but in case of sleep wakeup, it's not.
1087 * so fimd_apply function should be called at here.
1088 */
1089 fimd_apply(dev);
1090 }
Inki Daee30d4bc2011-12-12 16:35:20 +09001091
Inki Daee30d4bc2011-12-12 16:35:20 +09001092 return 0;
1093}
1094#endif
1095
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001096#ifdef CONFIG_PM_RUNTIME
1097static int fimd_runtime_suspend(struct device *dev)
1098{
1099 struct fimd_context *ctx = get_fimd_context(dev);
1100
1101 DRM_DEBUG_KMS("%s\n", __FILE__);
1102
Inki Dae5d553932012-08-17 17:08:04 +09001103 return fimd_activate(ctx, false);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001104}
1105
1106static int fimd_runtime_resume(struct device *dev)
1107{
1108 struct fimd_context *ctx = get_fimd_context(dev);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001109
1110 DRM_DEBUG_KMS("%s\n", __FILE__);
1111
Inki Dae5d553932012-08-17 17:08:04 +09001112 return fimd_activate(ctx, true);
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001113}
1114#endif
1115
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +05301116static struct platform_device_id fimd_driver_ids[] = {
1117 {
Tomasz Figa725ddea2013-05-01 21:02:29 +02001118 .name = "s3c64xx-fb",
1119 .driver_data = (unsigned long)&s3c64xx_fimd_driver_data,
1120 }, {
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +05301121 .name = "exynos4-fb",
1122 .driver_data = (unsigned long)&exynos4_fimd_driver_data,
1123 }, {
1124 .name = "exynos5-fb",
1125 .driver_data = (unsigned long)&exynos5_fimd_driver_data,
1126 },
1127 {},
1128};
1129MODULE_DEVICE_TABLE(platform, fimd_driver_ids);
1130
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001131static const struct dev_pm_ops fimd_pm_ops = {
Inki Daee30d4bc2011-12-12 16:35:20 +09001132 SET_SYSTEM_SLEEP_PM_OPS(fimd_suspend, fimd_resume)
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001133 SET_RUNTIME_PM_OPS(fimd_runtime_suspend, fimd_runtime_resume, NULL)
1134};
1135
Joonyoung Shim132a5b92012-03-16 18:47:08 +09001136struct platform_driver fimd_driver = {
Inki Dae1c248b72011-10-04 19:19:01 +09001137 .probe = fimd_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001138 .remove = fimd_remove,
Leela Krishna Amudalae2e13382012-09-21 16:52:15 +05301139 .id_table = fimd_driver_ids,
Inki Dae1c248b72011-10-04 19:19:01 +09001140 .driver = {
1141 .name = "exynos4-fb",
1142 .owner = THIS_MODULE,
Joonyoung Shimcb91f6a2011-12-09 16:52:11 +09001143 .pm = &fimd_pm_ops,
Joonyoung Shimd636ead2012-12-14 15:48:25 +09001144 .of_match_table = of_match_ptr(fimd_driver_dt_match),
Inki Dae1c248b72011-10-04 19:19:01 +09001145 },
1146};