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 if (exynos_drm_hdmi_pdev)
57 exynos_drm_hdmi_pdev = platform_device_register_simple(
58 "exynos-drm-hdmi", -1, NULL, 0);
59 if (IS_ERR_OR_NULL(exynos_drm_hdmi_pdev))
60 return PTR_ERR(exynos_drm_hdmi_pdev);
65 void exynos_platform_device_hdmi_unregister(void)
67 if (exynos_drm_hdmi_pdev)
68 platform_device_unregister(exynos_drm_hdmi_pdev);
71 void exynos_hdmi_drv_attach(struct exynos_drm_hdmi_context *ctx)
77 void exynos_mixer_drv_attach(struct exynos_drm_hdmi_context *ctx)
83 void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
85 DRM_DEBUG_KMS("%s\n", __FILE__);
91 void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
93 DRM_DEBUG_KMS("%s\n", __FILE__);
99 static bool drm_hdmi_is_connected(struct device *dev)
101 struct drm_hdmi_context *ctx = to_context(dev);
103 DRM_DEBUG_KMS("%s\n", __FILE__);
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 DRM_DEBUG_KMS("%s\n", __FILE__);
118 if (hdmi_ops && hdmi_ops->get_edid)
119 return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector);
124 static int drm_hdmi_check_timing(struct device *dev, void *timing)
126 struct drm_hdmi_context *ctx = to_context(dev);
129 DRM_DEBUG_KMS("%s\n", __FILE__);
132 * Both, mixer and hdmi should be able to handle the requested mode.
133 * If any of the two fails, return mode as BAD.
136 if (mixer_ops && mixer_ops->check_timing)
137 ret = mixer_ops->check_timing(ctx->mixer_ctx->ctx, timing);
142 if (hdmi_ops && hdmi_ops->check_timing)
143 return hdmi_ops->check_timing(ctx->hdmi_ctx->ctx, timing);
148 static int drm_hdmi_power_on(struct device *dev, int mode)
150 struct drm_hdmi_context *ctx = to_context(dev);
152 DRM_DEBUG_KMS("%s\n", __FILE__);
154 if (hdmi_ops && hdmi_ops->power_on)
155 return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
160 static struct exynos_drm_display_ops drm_hdmi_display_ops = {
161 .type = EXYNOS_DISPLAY_TYPE_HDMI,
162 .is_connected = drm_hdmi_is_connected,
163 .get_edid = drm_hdmi_get_edid,
164 .check_timing = drm_hdmi_check_timing,
165 .power_on = drm_hdmi_power_on,
168 static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
170 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
171 struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
172 struct exynos_drm_manager *manager = subdrv->manager;
174 DRM_DEBUG_KMS("%s\n", __FILE__);
176 if (mixer_ops && mixer_ops->enable_vblank)
177 return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
183 static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
185 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
187 DRM_DEBUG_KMS("%s\n", __FILE__);
189 if (mixer_ops && mixer_ops->disable_vblank)
190 return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
193 static void drm_hdmi_wait_for_vblank(struct device *subdrv_dev)
195 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
197 DRM_DEBUG_KMS("%s\n", __FILE__);
199 if (mixer_ops && mixer_ops->wait_for_vblank)
200 mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
203 static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
204 struct drm_connector *connector,
205 const struct drm_display_mode *mode,
206 struct drm_display_mode *adjusted_mode)
208 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
210 DRM_DEBUG_KMS("%s\n", __FILE__);
212 if (hdmi_ops && hdmi_ops->mode_fixup)
213 hdmi_ops->mode_fixup(ctx->hdmi_ctx->ctx, connector, mode,
217 static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
219 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
221 DRM_DEBUG_KMS("%s\n", __FILE__);
223 if (hdmi_ops && hdmi_ops->mode_set)
224 hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
227 static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
228 unsigned int *width, unsigned int *height)
230 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
232 DRM_DEBUG_KMS("%s\n", __FILE__);
234 if (hdmi_ops && hdmi_ops->get_max_resol)
235 hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
238 static void drm_hdmi_commit(struct device *subdrv_dev)
240 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
242 DRM_DEBUG_KMS("%s\n", __FILE__);
244 if (hdmi_ops && hdmi_ops->commit)
245 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
248 static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
250 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
252 DRM_DEBUG_KMS("%s\n", __FILE__);
254 if (mixer_ops && mixer_ops->dpms)
255 mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
257 if (hdmi_ops && hdmi_ops->dpms)
258 hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
261 static void drm_hdmi_apply(struct device *subdrv_dev)
263 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
266 DRM_DEBUG_KMS("%s\n", __FILE__);
268 for (i = 0; i < MIXER_WIN_NR; i++) {
269 if (!ctx->enabled[i])
271 if (mixer_ops && mixer_ops->win_commit)
272 mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
275 if (hdmi_ops && hdmi_ops->commit)
276 hdmi_ops->commit(ctx->hdmi_ctx->ctx);
279 static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
280 .dpms = drm_hdmi_dpms,
281 .apply = drm_hdmi_apply,
282 .enable_vblank = drm_hdmi_enable_vblank,
283 .disable_vblank = drm_hdmi_disable_vblank,
284 .wait_for_vblank = drm_hdmi_wait_for_vblank,
285 .mode_fixup = drm_hdmi_mode_fixup,
286 .mode_set = drm_hdmi_mode_set,
287 .get_max_resol = drm_hdmi_get_max_resol,
288 .commit = drm_hdmi_commit,
291 static void drm_mixer_mode_set(struct device *subdrv_dev,
292 struct exynos_drm_overlay *overlay)
294 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
296 DRM_DEBUG_KMS("%s\n", __FILE__);
298 if (mixer_ops && mixer_ops->win_mode_set)
299 mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
302 static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
304 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
305 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
307 DRM_DEBUG_KMS("%s\n", __FILE__);
309 if (win < 0 || win > MIXER_WIN_NR) {
310 DRM_ERROR("mixer window[%d] is wrong\n", win);
314 if (mixer_ops && mixer_ops->win_commit)
315 mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
317 ctx->enabled[win] = true;
320 static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
322 struct drm_hdmi_context *ctx = to_context(subdrv_dev);
323 int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
325 DRM_DEBUG_KMS("%s\n", __FILE__);
327 if (win < 0 || win > MIXER_WIN_NR) {
328 DRM_ERROR("mixer window[%d] is wrong\n", win);
332 if (mixer_ops && mixer_ops->win_disable)
333 mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
335 ctx->enabled[win] = false;
338 static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
339 .mode_set = drm_mixer_mode_set,
340 .commit = drm_mixer_commit,
341 .disable = drm_mixer_disable,
344 static struct exynos_drm_manager hdmi_manager = {
346 .ops = &drm_hdmi_manager_ops,
347 .overlay_ops = &drm_hdmi_overlay_ops,
348 .display_ops = &drm_hdmi_display_ops,
351 static int hdmi_subdrv_probe(struct drm_device *drm_dev,
354 struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
355 struct drm_hdmi_context *ctx;
357 DRM_DEBUG_KMS("%s\n", __FILE__);
360 DRM_ERROR("hdmi context not initialized.\n");
365 DRM_ERROR("mixer context not initialized.\n");
369 ctx = get_ctx_from_subdrv(subdrv);
372 DRM_ERROR("no drm hdmi context.\n");
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);
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 DRM_DEBUG_KMS("%s\n", __FILE__);
407 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
409 DRM_LOG_KMS("failed to alloc common hdmi context.\n");
413 subdrv = &ctx->subdrv;
416 subdrv->manager = &hdmi_manager;
417 subdrv->probe = hdmi_subdrv_probe;
418 subdrv->remove = hdmi_subdrv_remove;
420 platform_set_drvdata(pdev, subdrv);
422 exynos_drm_subdrv_register(subdrv);
427 static int exynos_drm_hdmi_remove(struct platform_device *pdev)
429 struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
431 DRM_DEBUG_KMS("%s\n", __FILE__);
433 exynos_drm_subdrv_unregister(&ctx->subdrv);
438 struct platform_driver exynos_drm_common_hdmi_driver = {
439 .probe = exynos_drm_hdmi_probe,
440 .remove = exynos_drm_hdmi_remove,
442 .name = "exynos-drm-hdmi",
443 .owner = THIS_MODULE,