x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / gpu / drm / exynos / exynos_drm_hdmi.c
blob8548b974bd59715389ce41b411c121a17877cb46
1 /*
2 * Copyright (C) 2011 Samsung Electronics Co.Ltd
3 * Authors:
4 * Inki Dae <inki.dae@samsung.com>
5 * Seung-Woo Kim <sw0312.kim@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
14 #include <drm/drmP.h>
16 #include <linux/kernel.h>
17 #include <linux/wait.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
21 #include <drm/exynos_drm.h>
23 #include "exynos_drm_drv.h"
24 #include "exynos_drm_hdmi.h"
26 #define to_context(dev) platform_get_drvdata(to_platform_device(dev))
27 #define to_subdrv(dev) to_context(dev)
28 #define get_ctx_from_subdrv(subdrv) container_of(subdrv,\
29 struct drm_hdmi_context, subdrv);
31 /* platform device pointer for common drm hdmi device. */
32 static struct platform_device *exynos_drm_hdmi_pdev;
34 /* Common hdmi subdrv needs to access the hdmi and mixer though context.
35 * These should be initialied by the repective drivers */
36 static struct exynos_drm_hdmi_context *hdmi_ctx;
37 static struct exynos_drm_hdmi_context *mixer_ctx;
39 /* these callback points shoud be set by specific drivers. */
40 static struct exynos_hdmi_ops *hdmi_ops;
41 static struct exynos_mixer_ops *mixer_ops;
43 struct drm_hdmi_context {
44 struct exynos_drm_subdrv subdrv;
45 struct exynos_drm_hdmi_context *hdmi_ctx;
46 struct exynos_drm_hdmi_context *mixer_ctx;
48 bool enabled[MIXER_WIN_NR];
51 int exynos_platform_device_hdmi_register(void)
53 struct platform_device *pdev;
55 if (exynos_drm_hdmi_pdev)
56 return -EEXIST;
58 pdev = platform_device_register_simple(
59 "exynos-drm-hdmi", -1, NULL, 0);
60 if (IS_ERR(pdev))
61 return PTR_ERR(pdev);
63 exynos_drm_hdmi_pdev = pdev;
65 return 0;
68 void exynos_platform_device_hdmi_unregister(void)
70 if (exynos_drm_hdmi_pdev) {
71 platform_device_unregister(exynos_drm_hdmi_pdev);
72 exynos_drm_hdmi_pdev = NULL;
76 void exynos_hdmi_drv_attach(struct exynos_drm_hdmi_context *ctx)
78 if (ctx)
79 hdmi_ctx = ctx;
82 void exynos_mixer_drv_attach(struct exynos_drm_hdmi_context *ctx)
84 if (ctx)
85 mixer_ctx = ctx;
88 void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
90 if (ops)
91 hdmi_ops = ops;
94 void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
96 if (ops)
97 mixer_ops = ops;
100 static bool drm_hdmi_is_connected(struct device *dev)
102 struct drm_hdmi_context *ctx = to_context(dev);
104 if (hdmi_ops && hdmi_ops->is_connected)
105 return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
107 return false;
110 static struct edid *drm_hdmi_get_edid(struct device *dev,
111 struct drm_connector *connector)
113 struct drm_hdmi_context *ctx = to_context(dev);
115 if (hdmi_ops && hdmi_ops->get_edid)
116 return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector);
118 return NULL;
121 static int drm_hdmi_check_mode(struct device *dev,
122 struct drm_display_mode *mode)
124 struct drm_hdmi_context *ctx = to_context(dev);
125 int ret = 0;
128 * Both, mixer and hdmi should be able to handle the requested mode.
129 * If any of the two fails, return mode as BAD.
132 if (mixer_ops && mixer_ops->check_mode)
133 ret = mixer_ops->check_mode(ctx->mixer_ctx->ctx, mode);
135 if (ret)
136 return ret;
138 if (hdmi_ops && hdmi_ops->check_mode)
139 return hdmi_ops->check_mode(ctx->hdmi_ctx->ctx, mode);
141 return 0;
144 static int drm_hdmi_power_on(struct device *dev, int mode)
146 struct drm_hdmi_context *ctx = to_context(dev);
148 if (hdmi_ops && hdmi_ops->power_on)
149 return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
151 return 0;
154 static struct exynos_drm_display_ops drm_hdmi_display_ops = {
155 .type = EXYNOS_DISPLAY_TYPE_HDMI,
156 .is_connected = drm_hdmi_is_connected,
157 .get_edid = drm_hdmi_get_edid,
158 .check_mode = drm_hdmi_check_mode,
159 .power_on = drm_hdmi_power_on,
162 static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
164 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
165 struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
166 struct exynos_drm_manager *manager = subdrv->manager;
168 if (mixer_ops && mixer_ops->enable_vblank)
169 return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
170 manager->pipe);
172 return 0;
175 static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
177 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
179 if (mixer_ops && mixer_ops->disable_vblank)
180 return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
183 static void drm_hdmi_wait_for_vblank(struct device *subdrv_dev)
185 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
187 if (mixer_ops && mixer_ops->wait_for_vblank)
188 mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
191 static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
192 struct drm_connector *connector,
193 const struct drm_display_mode *mode,
194 struct drm_display_mode *adjusted_mode)
196 struct drm_display_mode *m;
197 int mode_ok;
199 drm_mode_set_crtcinfo(adjusted_mode, 0);
201 mode_ok = drm_hdmi_check_mode(subdrv_dev, adjusted_mode);
203 /* just return if user desired mode exists. */
204 if (mode_ok == 0)
205 return;
208 * otherwise, find the most suitable mode among modes and change it
209 * to adjusted_mode.
211 list_for_each_entry(m, &connector->modes, head) {
212 mode_ok = drm_hdmi_check_mode(subdrv_dev, m);
214 if (mode_ok == 0) {
215 struct drm_mode_object base;
216 struct list_head head;
218 DRM_INFO("desired mode doesn't exist so\n");
219 DRM_INFO("use the most suitable mode among modes.\n");
221 DRM_DEBUG_KMS("Adjusted Mode: [%d]x[%d] [%d]Hz\n",
222 m->hdisplay, m->vdisplay, m->vrefresh);
224 /* preserve display mode header while copying. */
225 head = adjusted_mode->head;
226 base = adjusted_mode->base;
227 memcpy(adjusted_mode, m, sizeof(*m));
228 adjusted_mode->head = head;
229 adjusted_mode->base = base;
230 break;
235 static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
237 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
239 if (hdmi_ops && hdmi_ops->mode_set)
240 hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
243 static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
244 unsigned int *width, unsigned int *height)
246 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
248 if (hdmi_ops && hdmi_ops->get_max_resol)
249 hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
252 static void drm_hdmi_commit(struct device *subdrv_dev)
254 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
256 if (hdmi_ops && hdmi_ops->commit)
257 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
260 static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
262 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
264 if (mixer_ops && mixer_ops->dpms)
265 mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
267 if (hdmi_ops && hdmi_ops->dpms)
268 hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
271 static void drm_hdmi_apply(struct device *subdrv_dev)
273 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
274 int i;
276 for (i = 0; i < MIXER_WIN_NR; i++) {
277 if (!ctx->enabled[i])
278 continue;
279 if (mixer_ops && mixer_ops->win_commit)
280 mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
283 if (hdmi_ops && hdmi_ops->commit)
284 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
287 static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
288 .dpms = drm_hdmi_dpms,
289 .apply = drm_hdmi_apply,
290 .enable_vblank = drm_hdmi_enable_vblank,
291 .disable_vblank = drm_hdmi_disable_vblank,
292 .wait_for_vblank = drm_hdmi_wait_for_vblank,
293 .mode_fixup = drm_hdmi_mode_fixup,
294 .mode_set = drm_hdmi_mode_set,
295 .get_max_resol = drm_hdmi_get_max_resol,
296 .commit = drm_hdmi_commit,
299 static void drm_mixer_mode_set(struct device *subdrv_dev,
300 struct exynos_drm_overlay *overlay)
302 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
304 if (mixer_ops && mixer_ops->win_mode_set)
305 mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
308 static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
310 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
311 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
313 if (win < 0 || win >= MIXER_WIN_NR) {
314 DRM_ERROR("mixer window[%d] is wrong\n", win);
315 return;
318 if (mixer_ops && mixer_ops->win_commit)
319 mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
321 ctx->enabled[win] = true;
324 static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
326 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
327 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
329 if (win < 0 || win >= MIXER_WIN_NR) {
330 DRM_ERROR("mixer window[%d] is wrong\n", win);
331 return;
334 if (mixer_ops && mixer_ops->win_disable)
335 mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
337 ctx->enabled[win] = false;
340 static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
341 .mode_set = drm_mixer_mode_set,
342 .commit = drm_mixer_commit,
343 .disable = drm_mixer_disable,
346 static struct exynos_drm_manager hdmi_manager = {
347 .pipe = -1,
348 .ops = &drm_hdmi_manager_ops,
349 .overlay_ops = &drm_hdmi_overlay_ops,
350 .display_ops = &drm_hdmi_display_ops,
353 static int hdmi_subdrv_probe(struct drm_device *drm_dev,
354 struct device *dev)
356 struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
357 struct drm_hdmi_context *ctx;
359 if (!hdmi_ctx) {
360 DRM_ERROR("hdmi context not initialized.\n");
361 return -EFAULT;
364 if (!mixer_ctx) {
365 DRM_ERROR("mixer context not initialized.\n");
366 return -EFAULT;
369 ctx = get_ctx_from_subdrv(subdrv);
371 if (!ctx) {
372 DRM_ERROR("no drm hdmi context.\n");
373 return -EFAULT;
376 ctx->hdmi_ctx = hdmi_ctx;
377 ctx->mixer_ctx = mixer_ctx;
379 ctx->hdmi_ctx->drm_dev = drm_dev;
380 ctx->mixer_ctx->drm_dev = drm_dev;
382 if (mixer_ops->iommu_on)
383 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);
385 return 0;
388 static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
390 struct drm_hdmi_context *ctx;
391 struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
393 ctx = get_ctx_from_subdrv(subdrv);
395 if (mixer_ops->iommu_on)
396 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
399 static int exynos_drm_hdmi_probe(struct platform_device *pdev)
401 struct device *dev = &pdev->dev;
402 struct exynos_drm_subdrv *subdrv;
403 struct drm_hdmi_context *ctx;
405 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
406 if (!ctx)
407 return -ENOMEM;
409 subdrv = &ctx->subdrv;
411 subdrv->dev = dev;
412 subdrv->manager = &hdmi_manager;
413 subdrv->probe = hdmi_subdrv_probe;
414 subdrv->remove = hdmi_subdrv_remove;
416 platform_set_drvdata(pdev, subdrv);
418 exynos_drm_subdrv_register(subdrv);
420 return 0;
423 static int exynos_drm_hdmi_remove(struct platform_device *pdev)
425 struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
427 exynos_drm_subdrv_unregister(&ctx->subdrv);
429 return 0;
432 struct platform_driver exynos_drm_common_hdmi_driver = {
433 .probe = exynos_drm_hdmi_probe,
434 .remove = exynos_drm_hdmi_remove,
435 .driver = {
436 .name = "exynos-drm-hdmi",
437 .owner = THIS_MODULE,