exynos_drm_hdmi.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (C) 2011 Samsung Electronics Co.Ltd
 * Authors:
 *	Inki Dae <inki.dae@samsung.com>
 *	Seung-Woo Kim <sw0312.kim@samsung.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */

14
#include <drm/drmP.h>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

#include <linux/kernel.h>
#include <linux/wait.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>

#include <drm/exynos_drm.h>

#include "exynos_drm_drv.h"
#include "exynos_drm_hdmi.h"

#define to_context(dev)		platform_get_drvdata(to_platform_device(dev))
#define to_subdrv(dev)		to_context(dev)
#define get_ctx_from_subdrv(subdrv)	container_of(subdrv,\
					struct drm_hdmi_context, subdrv);

32 33 34
/* platform device pointer for common drm hdmi device. */
static struct platform_device *exynos_drm_hdmi_pdev;

35 36 37 38 39
/* Common hdmi subdrv needs to access the hdmi and mixer though context.
* These should be initialied by the repective drivers */
static struct exynos_drm_hdmi_context *hdmi_ctx;
static struct exynos_drm_hdmi_context *mixer_ctx;

40
/* these callback points shoud be set by specific drivers. */
41 42
static struct exynos_hdmi_ops *hdmi_ops;
static struct exynos_mixer_ops *mixer_ops;
43 44 45 46 47

struct drm_hdmi_context {
	struct exynos_drm_subdrv	subdrv;
	struct exynos_drm_hdmi_context	*hdmi_ctx;
	struct exynos_drm_hdmi_context	*mixer_ctx;
J
Joonyoung Shim 已提交
48 49

	bool	enabled[MIXER_WIN_NR];
50 51
};

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
int exynos_platform_device_hdmi_register(void)
{
	if (exynos_drm_hdmi_pdev)
		return -EEXIST;

	exynos_drm_hdmi_pdev = platform_device_register_simple(
			"exynos-drm-hdmi", -1, NULL, 0);
	if (IS_ERR_OR_NULL(exynos_drm_hdmi_pdev))
		return PTR_ERR(exynos_drm_hdmi_pdev);

	return 0;
}

void exynos_platform_device_hdmi_unregister(void)
{
	if (exynos_drm_hdmi_pdev)
		platform_device_unregister(exynos_drm_hdmi_pdev);
}

71 72 73 74 75 76 77 78 79 80 81 82
void exynos_hdmi_drv_attach(struct exynos_drm_hdmi_context *ctx)
{
	if (ctx)
		hdmi_ctx = ctx;
}

void exynos_mixer_drv_attach(struct exynos_drm_hdmi_context *ctx)
{
	if (ctx)
		mixer_ctx = ctx;
}

83
void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
84 85 86
{
	DRM_DEBUG_KMS("%s\n", __FILE__);

87 88
	if (ops)
		hdmi_ops = ops;
89 90
}

91
void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
92 93 94
{
	DRM_DEBUG_KMS("%s\n", __FILE__);

95 96
	if (ops)
		mixer_ops = ops;
97 98 99 100 101 102 103 104
}

static bool drm_hdmi_is_connected(struct device *dev)
{
	struct drm_hdmi_context *ctx = to_context(dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

105 106
	if (hdmi_ops && hdmi_ops->is_connected)
		return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
107 108 109 110 111 112 113 114 115 116 117

	return false;
}

static int drm_hdmi_get_edid(struct device *dev,
		struct drm_connector *connector, u8 *edid, int len)
{
	struct drm_hdmi_context *ctx = to_context(dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

118 119 120
	if (hdmi_ops && hdmi_ops->get_edid)
		return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector, edid,
					  len);
121 122 123 124 125 126 127 128 129 130

	return 0;
}

static int drm_hdmi_check_timing(struct device *dev, void *timing)
{
	struct drm_hdmi_context *ctx = to_context(dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

131 132
	if (hdmi_ops && hdmi_ops->check_timing)
		return hdmi_ops->check_timing(ctx->hdmi_ctx->ctx, timing);
133 134 135 136 137 138 139 140 141 142

	return 0;
}

static int drm_hdmi_power_on(struct device *dev, int mode)
{
	struct drm_hdmi_context *ctx = to_context(dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

143 144
	if (hdmi_ops && hdmi_ops->power_on)
		return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	return 0;
}

static struct exynos_drm_display_ops drm_hdmi_display_ops = {
	.type = EXYNOS_DISPLAY_TYPE_HDMI,
	.is_connected = drm_hdmi_is_connected,
	.get_edid = drm_hdmi_get_edid,
	.check_timing = drm_hdmi_check_timing,
	.power_on = drm_hdmi_power_on,
};

static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);
	struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
161
	struct exynos_drm_manager *manager = subdrv->manager;
162 163 164

	DRM_DEBUG_KMS("%s\n", __FILE__);

165 166 167
	if (mixer_ops && mixer_ops->enable_vblank)
		return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
						manager->pipe);
168 169 170 171 172 173 174 175 176 177

	return 0;
}

static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

178 179
	if (mixer_ops && mixer_ops->disable_vblank)
		return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
180 181
}

182 183
static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
				struct drm_connector *connector,
184
				const struct drm_display_mode *mode,
185 186 187 188 189 190
				struct drm_display_mode *adjusted_mode)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

191 192 193
	if (hdmi_ops && hdmi_ops->mode_fixup)
		hdmi_ops->mode_fixup(ctx->hdmi_ctx->ctx, connector, mode,
				     adjusted_mode);
194 195
}

196 197 198 199 200 201
static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

202 203
	if (hdmi_ops && hdmi_ops->mode_set)
		hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
204 205
}

206 207 208 209 210 211 212
static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
				unsigned int *width, unsigned int *height)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

213 214
	if (hdmi_ops && hdmi_ops->get_max_resol)
		hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
215 216
}

217 218 219 220 221 222
static void drm_hdmi_commit(struct device *subdrv_dev)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

223 224
	if (hdmi_ops && hdmi_ops->commit)
		hdmi_ops->commit(ctx->hdmi_ctx->ctx);
225 226 227 228 229 230 231 232
}

static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

J
Joonyoung Shim 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	if (mixer_ops && mixer_ops->dpms)
		mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);

	if (hdmi_ops && hdmi_ops->dpms)
		hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
}

static void drm_hdmi_apply(struct device *subdrv_dev)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);
	int i;

	DRM_DEBUG_KMS("%s\n", __FILE__);

	for (i = 0; i < MIXER_WIN_NR; i++) {
		if (!ctx->enabled[i])
			continue;
		if (mixer_ops && mixer_ops->win_commit)
			mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
252
	}
J
Joonyoung Shim 已提交
253 254 255

	if (hdmi_ops && hdmi_ops->commit)
		hdmi_ops->commit(ctx->hdmi_ctx->ctx);
256 257 258 259
}

static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
	.dpms = drm_hdmi_dpms,
J
Joonyoung Shim 已提交
260
	.apply = drm_hdmi_apply,
261 262
	.enable_vblank = drm_hdmi_enable_vblank,
	.disable_vblank = drm_hdmi_disable_vblank,
263
	.mode_fixup = drm_hdmi_mode_fixup,
264
	.mode_set = drm_hdmi_mode_set,
265
	.get_max_resol = drm_hdmi_get_max_resol,
266 267 268 269 270 271 272 273 274 275
	.commit = drm_hdmi_commit,
};

static void drm_mixer_mode_set(struct device *subdrv_dev,
		struct exynos_drm_overlay *overlay)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

276 277
	if (mixer_ops && mixer_ops->win_mode_set)
		mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
278 279 280 281 282
}

static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);
J
Joonyoung Shim 已提交
283
	int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
284 285 286

	DRM_DEBUG_KMS("%s\n", __FILE__);

J
Joonyoung Shim 已提交
287 288 289 290 291
	if (win < 0 || win > MIXER_WIN_NR) {
		DRM_ERROR("mixer window[%d] is wrong\n", win);
		return;
	}

292
	if (mixer_ops && mixer_ops->win_commit)
J
Joonyoung Shim 已提交
293 294 295
		mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);

	ctx->enabled[win] = true;
296 297 298 299 300
}

static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);
J
Joonyoung Shim 已提交
301
	int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
302 303 304

	DRM_DEBUG_KMS("%s\n", __FILE__);

J
Joonyoung Shim 已提交
305 306 307 308 309
	if (win < 0 || win > MIXER_WIN_NR) {
		DRM_ERROR("mixer window[%d] is wrong\n", win);
		return;
	}

310
	if (mixer_ops && mixer_ops->win_disable)
J
Joonyoung Shim 已提交
311 312 313
		mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);

	ctx->enabled[win] = false;
314 315
}

316 317 318 319 320 321 322 323 324 325
static void drm_mixer_wait_for_vblank(struct device *subdrv_dev)
{
	struct drm_hdmi_context *ctx = to_context(subdrv_dev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

	if (mixer_ops && mixer_ops->wait_for_vblank)
		mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
}

326 327 328 329
static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
	.mode_set = drm_mixer_mode_set,
	.commit = drm_mixer_commit,
	.disable = drm_mixer_disable,
330
	.wait_for_vblank = drm_mixer_wait_for_vblank,
331 332
};

333 334 335 336 337 338
static struct exynos_drm_manager hdmi_manager = {
	.pipe		= -1,
	.ops		= &drm_hdmi_manager_ops,
	.overlay_ops	= &drm_hdmi_overlay_ops,
	.display_ops	= &drm_hdmi_display_ops,
};
339 340 341 342 343 344 345 346 347

static int hdmi_subdrv_probe(struct drm_device *drm_dev,
		struct device *dev)
{
	struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
	struct drm_hdmi_context *ctx;

	DRM_DEBUG_KMS("%s\n", __FILE__);

348 349
	if (!hdmi_ctx) {
		DRM_ERROR("hdmi context not initialized.\n");
350 351 352
		return -EFAULT;
	}

353 354
	if (!mixer_ctx) {
		DRM_ERROR("mixer context not initialized.\n");
355 356 357 358 359
		return -EFAULT;
	}

	ctx = get_ctx_from_subdrv(subdrv);

360 361
	if (!ctx) {
		DRM_ERROR("no drm hdmi context.\n");
362
		return -EFAULT;
363 364
	}

365 366
	ctx->hdmi_ctx = hdmi_ctx;
	ctx->mixer_ctx = mixer_ctx;
367

368
	ctx->hdmi_ctx->drm_dev = drm_dev;
369 370
	ctx->mixer_ctx->drm_dev = drm_dev;

371 372 373
	if (mixer_ops->iommu_on)
		mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);

374 375 376
	return 0;
}

377 378 379 380 381 382 383 384 385 386 387
static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
{
	struct drm_hdmi_context *ctx;
	struct exynos_drm_subdrv *subdrv = to_subdrv(dev);

	ctx = get_ctx_from_subdrv(subdrv);

	if (mixer_ops->iommu_on)
		mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
}

388 389 390 391 392 393 394 395
static int __devinit exynos_drm_hdmi_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct exynos_drm_subdrv *subdrv;
	struct drm_hdmi_context *ctx;

	DRM_DEBUG_KMS("%s\n", __FILE__);

396
	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
397 398 399 400 401 402 403
	if (!ctx) {
		DRM_LOG_KMS("failed to alloc common hdmi context.\n");
		return -ENOMEM;
	}

	subdrv = &ctx->subdrv;

404 405
	subdrv->dev = dev;
	subdrv->manager = &hdmi_manager;
406
	subdrv->probe = hdmi_subdrv_probe;
407
	subdrv->remove = hdmi_subdrv_remove;
408 409 410

	platform_set_drvdata(pdev, subdrv);

411
	exynos_drm_subdrv_register(subdrv);
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

	return 0;
}

static int __devexit exynos_drm_hdmi_remove(struct platform_device *pdev)
{
	struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);

	DRM_DEBUG_KMS("%s\n", __FILE__);

	exynos_drm_subdrv_unregister(&ctx->subdrv);

	return 0;
}

427
struct platform_driver exynos_drm_common_hdmi_driver = {
428 429 430 431 432 433 434
	.probe		= exynos_drm_hdmi_probe,
	.remove		= __devexit_p(exynos_drm_hdmi_remove),
	.driver		= {
		.name	= "exynos-drm-hdmi",
		.owner	= THIS_MODULE,
	},
};