2 * Copyright (C) 2011 Samsung Electronics Co.Ltd
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.
16 #include <linux/kernel.h>
17 #include <linux/wait.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
22 #include <drm/exynos_drm.h>
24 #include "exynos_drm_drv.h"
25 #include "exynos_drm_hdmi.h"
27 #define to_context(dev) platform_get_drvdata(to_platform_device(dev))
28 #define to_subdrv(dev) to_context(dev)
29 #define get_ctx_from_subdrv(subdrv) container_of(subdrv,\
30 struct drm_hdmi_context, subdrv);
32 /* platform device pointer for common drm hdmi device. */
33 static struct platform_device *exynos_drm_hdmi_pdev;
35 /* Common hdmi subdrv needs to access the hdmi and mixer though context.
36 * These should be initialied by the repective drivers */
37 static struct exynos_drm_hdmi_context *hdmi_ctx;
38 static struct exynos_drm_hdmi_context *mixer_ctx;
40 /* these callback points shoud be set by specific drivers. */
41 static struct exynos_hdmi_ops *hdmi_ops;
42 static struct exynos_mixer_ops *mixer_ops;
44 struct drm_hdmi_context {
45 struct exynos_drm_subdrv subdrv;
46 struct exynos_drm_hdmi_context *hdmi_ctx;
47 struct exynos_drm_hdmi_context *mixer_ctx;
49 bool enabled[MIXER_WIN_NR];
52 int exynos_platform_device_hdmi_register(void)
54 struct platform_device *pdev;
56 if (exynos_drm_hdmi_pdev)
59 pdev = platform_device_register_simple(
60 "exynos-drm-hdmi", -1, NULL, 0);
64 exynos_drm_hdmi_pdev = pdev;
69 void exynos_platform_device_hdmi_unregister(void)
71 if (exynos_drm_hdmi_pdev) {
72 platform_device_unregister(exynos_drm_hdmi_pdev);
73 exynos_drm_hdmi_pdev = NULL;
77 void exynos_hdmi_drv_attach(struct exynos_drm_hdmi_context *ctx)
83 void exynos_mixer_drv_attach(struct exynos_drm_hdmi_context *ctx)
89 void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
95 void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
101 static bool drm_hdmi_is_connected(struct device *dev)
103 struct drm_hdmi_context *ctx = to_context(dev);
105 if (hdmi_ops && hdmi_ops->is_connected)
106 return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
111 static struct edid *drm_hdmi_get_edid(struct device *dev,
112 struct drm_connector *connector)
114 struct drm_hdmi_context *ctx = to_context(dev);
116 if (hdmi_ops && hdmi_ops->get_edid)
117 return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector);
122 static int drm_hdmi_check_mode(struct device *dev,
123 struct drm_display_mode *mode)
125 struct drm_hdmi_context *ctx = to_context(dev);
129 * Both, mixer and hdmi should be able to handle the requested mode.
130 * If any of the two fails, return mode as BAD.
133 if (mixer_ops && mixer_ops->check_mode)
134 ret = mixer_ops->check_mode(ctx->mixer_ctx->ctx, mode);
139 if (hdmi_ops && hdmi_ops->check_mode)
140 return hdmi_ops->check_mode(ctx->hdmi_ctx->ctx, mode);
145 static int drm_hdmi_power_on(struct device *dev, int mode)
147 struct drm_hdmi_context *ctx = to_context(dev);
149 if (hdmi_ops && hdmi_ops->power_on)
150 return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
155 static struct exynos_drm_display_ops drm_hdmi_display_ops = {
156 .type = EXYNOS_DISPLAY_TYPE_HDMI,
157 .is_connected = drm_hdmi_is_connected,
158 .get_edid = drm_hdmi_get_edid,
159 .check_mode = drm_hdmi_check_mode,
160 .power_on = drm_hdmi_power_on,
163 static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
165 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
166 struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
167 struct exynos_drm_manager *manager = subdrv->manager;
169 if (mixer_ops && mixer_ops->enable_vblank)
170 return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
176 static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
178 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
180 if (mixer_ops && mixer_ops->disable_vblank)
181 return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
184 static void drm_hdmi_wait_for_vblank(struct device *subdrv_dev)
186 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
188 if (mixer_ops && mixer_ops->wait_for_vblank)
189 mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
192 static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
193 struct drm_connector *connector,
194 const struct drm_display_mode *mode,
195 struct drm_display_mode *adjusted_mode)
197 struct drm_display_mode *m;
200 drm_mode_set_crtcinfo(adjusted_mode, 0);
202 mode_ok = drm_hdmi_check_mode(subdrv_dev, adjusted_mode);
204 /* just return if user desired mode exists. */
209 * otherwise, find the most suitable mode among modes and change it
212 list_for_each_entry(m, &connector->modes, head) {
213 mode_ok = drm_hdmi_check_mode(subdrv_dev, m);
216 struct drm_mode_object base;
217 struct list_head head;
219 DRM_INFO("desired mode doesn't exist so\n");
220 DRM_INFO("use the most suitable mode among modes.\n");
222 DRM_DEBUG_KMS("Adjusted Mode: [%d]x[%d] [%d]Hz\n",
223 m->hdisplay, m->vdisplay, m->vrefresh);
225 /* preserve display mode header while copying. */
226 head = adjusted_mode->head;
227 base = adjusted_mode->base;
228 memcpy(adjusted_mode, m, sizeof(*m));
229 adjusted_mode->head = head;
230 adjusted_mode->base = base;
236 static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
238 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
240 if (hdmi_ops && hdmi_ops->mode_set)
241 hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
244 static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
245 unsigned int *width, unsigned int *height)
247 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
249 if (hdmi_ops && hdmi_ops->get_max_resol)
250 hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
253 static void drm_hdmi_commit(struct device *subdrv_dev)
255 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
257 if (hdmi_ops && hdmi_ops->commit)
258 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
261 static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
263 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
265 if (mixer_ops && mixer_ops->dpms)
266 mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
268 if (hdmi_ops && hdmi_ops->dpms)
269 hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
272 static void drm_hdmi_apply(struct device *subdrv_dev)
274 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
277 for (i = 0; i < MIXER_WIN_NR; i++) {
278 if (!ctx->enabled[i])
280 if (mixer_ops && mixer_ops->win_commit)
281 mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
284 if (hdmi_ops && hdmi_ops->commit)
285 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
288 static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
289 .dpms = drm_hdmi_dpms,
290 .apply = drm_hdmi_apply,
291 .enable_vblank = drm_hdmi_enable_vblank,
292 .disable_vblank = drm_hdmi_disable_vblank,
293 .wait_for_vblank = drm_hdmi_wait_for_vblank,
294 .mode_fixup = drm_hdmi_mode_fixup,
295 .mode_set = drm_hdmi_mode_set,
296 .get_max_resol = drm_hdmi_get_max_resol,
297 .commit = drm_hdmi_commit,
300 static void drm_mixer_mode_set(struct device *subdrv_dev,
301 struct exynos_drm_overlay *overlay)
303 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
305 if (mixer_ops && mixer_ops->win_mode_set)
306 mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
309 static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
311 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
312 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
314 if (win < 0 || win >= MIXER_WIN_NR) {
315 DRM_ERROR("mixer window[%d] is wrong\n", win);
319 if (mixer_ops && mixer_ops->win_commit)
320 mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
322 ctx->enabled[win] = true;
325 static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
327 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
328 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
330 if (win < 0 || win >= MIXER_WIN_NR) {
331 DRM_ERROR("mixer window[%d] is wrong\n", win);
335 if (mixer_ops && mixer_ops->win_disable)
336 mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
338 ctx->enabled[win] = false;
341 static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
342 .mode_set = drm_mixer_mode_set,
343 .commit = drm_mixer_commit,
344 .disable = drm_mixer_disable,
347 static struct exynos_drm_manager hdmi_manager = {
349 .ops = &drm_hdmi_manager_ops,
350 .overlay_ops = &drm_hdmi_overlay_ops,
351 .display_ops = &drm_hdmi_display_ops,
354 static int hdmi_subdrv_probe(struct drm_device *drm_dev,
357 struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
358 struct drm_hdmi_context *ctx;
361 DRM_ERROR("hdmi context not initialized.\n");
366 DRM_ERROR("mixer context not initialized.\n");
370 ctx = get_ctx_from_subdrv(subdrv);
373 DRM_ERROR("no drm hdmi context.\n");
377 ctx->hdmi_ctx = hdmi_ctx;
378 ctx->mixer_ctx = mixer_ctx;
380 ctx->hdmi_ctx->drm_dev = drm_dev;
381 ctx->mixer_ctx->drm_dev = drm_dev;
383 if (mixer_ops->iommu_on)
384 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);
389 static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
391 struct drm_hdmi_context *ctx;
392 struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
394 ctx = get_ctx_from_subdrv(subdrv);
396 if (mixer_ops->iommu_on)
397 mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
400 static int exynos_drm_hdmi_probe(struct platform_device *pdev)
402 struct device *dev = &pdev->dev;
403 struct exynos_drm_subdrv *subdrv;
404 struct drm_hdmi_context *ctx;
406 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
408 DRM_LOG_KMS("failed to alloc common hdmi context.\n");
412 subdrv = &ctx->subdrv;
415 subdrv->manager = &hdmi_manager;
416 subdrv->probe = hdmi_subdrv_probe;
417 subdrv->remove = hdmi_subdrv_remove;
419 platform_set_drvdata(pdev, subdrv);
421 exynos_drm_subdrv_register(subdrv);
426 static int exynos_drm_hdmi_remove(struct platform_device *pdev)
428 struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
430 exynos_drm_subdrv_unregister(&ctx->subdrv);
435 struct platform_driver exynos_drm_common_hdmi_driver = {
436 .probe = exynos_drm_hdmi_probe,
437 .remove = exynos_drm_hdmi_remove,
439 .name = "exynos-drm-hdmi",
440 .owner = THIS_MODULE,