exynos_drm_vidi.c 15.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* exynos_drm_vidi.c
 *
 * Copyright (C) 2012 Samsung Electronics Co.Ltd
 * Authors:
 *	Inki Dae <inki.dae@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.
 *
 */
13
#include <drm/drmP.h>
14 15 16 17 18 19

#include <linux/kernel.h>
#include <linux/platform_device.h>

#include <drm/exynos_drm.h>

20 21
#include <drm/drm_edid.h>
#include <drm/drm_crtc_helper.h>
22 23 24 25

#include "exynos_drm_drv.h"
#include "exynos_drm_crtc.h"
#include "exynos_drm_encoder.h"
M
Mark Brown 已提交
26
#include "exynos_drm_vidi.h"
27 28 29 30

/* vidi has totally three virtual windows. */
#define WINDOWS_NR		3

31
#define get_vidi_mgr(dev)	platform_get_drvdata(to_platform_device(dev))
32 33
#define ctx_from_connector(c)	container_of(c, struct vidi_context, \
					connector)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

struct vidi_win_data {
	unsigned int		offset_x;
	unsigned int		offset_y;
	unsigned int		ovl_width;
	unsigned int		ovl_height;
	unsigned int		fb_width;
	unsigned int		fb_height;
	unsigned int		bpp;
	dma_addr_t		dma_addr;
	unsigned int		buf_offsize;
	unsigned int		line_size;	/* bytes */
	bool			enabled;
};

struct vidi_context {
50
	struct drm_device		*drm_dev;
51
	struct drm_crtc			*crtc;
52 53
	struct drm_encoder		*encoder;
	struct drm_connector		connector;
54 55 56 57 58 59 60 61
	struct vidi_win_data		win_data[WINDOWS_NR];
	struct edid			*raw_edid;
	unsigned int			clkdiv;
	unsigned int			default_win;
	unsigned long			irq_flags;
	unsigned int			connected;
	bool				vblank_on;
	bool				suspended;
62
	bool				direct_vblank;
63 64
	struct work_struct		work;
	struct mutex			lock;
65
	int				pipe;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
};

static const char fake_edid_info[] = {
	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x4c, 0x2d, 0x05, 0x05,
	0x00, 0x00, 0x00, 0x00, 0x30, 0x12, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
	0x0a, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54, 0xbd,
	0xee, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x66, 0x21, 0x50, 0xb0, 0x51, 0x00,
	0x1b, 0x30, 0x40, 0x70, 0x36, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e,
	0x01, 0x1d, 0x00, 0x72, 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
	0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18,
	0x4b, 0x1a, 0x44, 0x17, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
	0x00, 0x00, 0x00, 0xfc, 0x00, 0x53, 0x41, 0x4d, 0x53, 0x55, 0x4e, 0x47,
	0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0xbc, 0x02, 0x03, 0x1e, 0xf1,
	0x46, 0x84, 0x05, 0x03, 0x10, 0x20, 0x22, 0x23, 0x09, 0x07, 0x07, 0x83,
	0x01, 0x00, 0x00, 0xe2, 0x00, 0x0f, 0x67, 0x03, 0x0c, 0x00, 0x10, 0x00,
	0xb8, 0x2d, 0x01, 0x1d, 0x80, 0x18, 0x71, 0x1c, 0x16, 0x20, 0x58, 0x2c,
	0x25, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x9e, 0x8c, 0x0a, 0xd0, 0x8a,
	0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xa0, 0x5a, 0x00, 0x00,
	0x00, 0x18, 0x02, 0x3a, 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
	0x45, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x06
};

93
static void vidi_apply(struct exynos_drm_manager *mgr)
94
{
95
	struct vidi_context *ctx = mgr->ctx;
96 97 98 99 100 101
	struct exynos_drm_manager_ops *mgr_ops = mgr->ops;
	struct vidi_win_data *win_data;
	int i;

	for (i = 0; i < WINDOWS_NR; i++) {
		win_data = &ctx->win_data[i];
102
		if (win_data->enabled && (mgr_ops && mgr_ops->win_commit))
103
			mgr_ops->win_commit(mgr, i);
104 105 106
	}

	if (mgr_ops && mgr_ops->commit)
107
		mgr_ops->commit(mgr);
108 109
}

110
static void vidi_commit(struct exynos_drm_manager *mgr)
111
{
112
	struct vidi_context *ctx = mgr->ctx;
113 114 115 116 117

	if (ctx->suspended)
		return;
}

118
static int vidi_enable_vblank(struct exynos_drm_manager *mgr)
119
{
120
	struct vidi_context *ctx = mgr->ctx;
121 122 123 124 125 126 127

	if (ctx->suspended)
		return -EPERM;

	if (!test_and_set_bit(0, &ctx->irq_flags))
		ctx->vblank_on = true;

128 129 130 131 132
	ctx->direct_vblank = true;

	/*
	 * in case of page flip request, vidi_finish_pageflip function
	 * will not be called because direct_vblank is true and then
133
	 * that function will be called by manager_ops->win_commit callback
134 135 136
	 */
	schedule_work(&ctx->work);

137 138 139
	return 0;
}

140
static void vidi_disable_vblank(struct exynos_drm_manager *mgr)
141
{
142
	struct vidi_context *ctx = mgr->ctx;
143 144 145 146 147 148 149 150

	if (ctx->suspended)
		return;

	if (test_and_clear_bit(0, &ctx->irq_flags))
		ctx->vblank_on = false;
}

151 152
static void vidi_win_mode_set(struct exynos_drm_manager *mgr,
			struct exynos_drm_overlay *overlay)
153
{
154
	struct vidi_context *ctx = mgr->ctx;
155 156 157 158 159
	struct vidi_win_data *win_data;
	int win;
	unsigned long offset;

	if (!overlay) {
160
		DRM_ERROR("overlay is NULL\n");
161 162 163 164 165 166 167
		return;
	}

	win = overlay->zpos;
	if (win == DEFAULT_ZPOS)
		win = ctx->default_win;

168
	if (win < 0 || win >= WINDOWS_NR)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		return;

	offset = overlay->fb_x * (overlay->bpp >> 3);
	offset += overlay->fb_y * overlay->pitch;

	DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, overlay->pitch);

	win_data = &ctx->win_data[win];

	win_data->offset_x = overlay->crtc_x;
	win_data->offset_y = overlay->crtc_y;
	win_data->ovl_width = overlay->crtc_width;
	win_data->ovl_height = overlay->crtc_height;
	win_data->fb_width = overlay->fb_width;
	win_data->fb_height = overlay->fb_height;
	win_data->dma_addr = overlay->dma_addr[0] + offset;
	win_data->bpp = overlay->bpp;
	win_data->buf_offsize = (overlay->fb_width - overlay->crtc_width) *
				(overlay->bpp >> 3);
	win_data->line_size = overlay->crtc_width * (overlay->bpp >> 3);

	/*
	 * some parts of win_data should be transferred to user side
	 * through specific ioctl.
	 */

	DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
			win_data->offset_x, win_data->offset_y);
	DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
			win_data->ovl_width, win_data->ovl_height);
199
	DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
200 201 202 203
	DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
			overlay->fb_width, overlay->crtc_width);
}

204
static void vidi_win_commit(struct exynos_drm_manager *mgr, int zpos)
205
{
206
	struct vidi_context *ctx = mgr->ctx;
207 208 209 210 211 212 213 214 215
	struct vidi_win_data *win_data;
	int win = zpos;

	if (ctx->suspended)
		return;

	if (win == DEFAULT_ZPOS)
		win = ctx->default_win;

216
	if (win < 0 || win >= WINDOWS_NR)
217 218 219 220 221 222 223 224 225 226 227 228
		return;

	win_data = &ctx->win_data[win];

	win_data->enabled = true;

	DRM_DEBUG_KMS("dma_addr = 0x%x\n", win_data->dma_addr);

	if (ctx->vblank_on)
		schedule_work(&ctx->work);
}

229
static void vidi_win_disable(struct exynos_drm_manager *mgr, int zpos)
230
{
231
	struct vidi_context *ctx = mgr->ctx;
232 233 234 235 236 237
	struct vidi_win_data *win_data;
	int win = zpos;

	if (win == DEFAULT_ZPOS)
		win = ctx->default_win;

238
	if (win < 0 || win >= WINDOWS_NR)
239 240 241 242 243 244 245 246
		return;

	win_data = &ctx->win_data[win];
	win_data->enabled = false;

	/* TODO. */
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
static int vidi_power_on(struct exynos_drm_manager *mgr, bool enable)
{
	struct vidi_context *ctx = mgr->ctx;

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

	if (enable != false && enable != true)
		return -EINVAL;

	if (enable) {
		ctx->suspended = false;

		/* if vblank was enabled status, enable it again. */
		if (test_and_clear_bit(0, &ctx->irq_flags))
			vidi_enable_vblank(mgr);

		vidi_apply(mgr);
	} else {
		ctx->suspended = true;
	}

	return 0;
}

static void vidi_dpms(struct exynos_drm_manager *mgr, int mode)
{
	struct vidi_context *ctx = mgr->ctx;

	DRM_DEBUG_KMS("%d\n", mode);

	mutex_lock(&ctx->lock);

	switch (mode) {
	case DRM_MODE_DPMS_ON:
		vidi_power_on(mgr, true);
		break;
	case DRM_MODE_DPMS_STANDBY:
	case DRM_MODE_DPMS_SUSPEND:
	case DRM_MODE_DPMS_OFF:
		vidi_power_on(mgr, false);
		break;
	default:
		DRM_DEBUG_KMS("unspecified mode %d\n", mode);
		break;
	}

	mutex_unlock(&ctx->lock);
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
static int vidi_mgr_initialize(struct exynos_drm_manager *mgr,
			struct drm_device *drm_dev, int pipe)
{
	struct vidi_context *ctx = mgr->ctx;

	DRM_ERROR("vidi initialize ct=%p dev=%p pipe=%d\n", ctx, drm_dev, pipe);

	ctx->drm_dev = drm_dev;
	ctx->pipe = pipe;

	/*
	 * enable drm irq mode.
	 * - with irq_enabled = 1, we can use the vblank feature.
	 *
	 * P.S. note that we wouldn't use drm irq handler but
	 *	just specific driver own one instead because
	 *	drm framework supports only one irq handler.
	 */
	drm_dev->irq_enabled = 1;

	/*
	 * with vblank_disable_allowed = 1, vblank interrupt will be disabled
	 * by drm timer once a current process gives up ownership of
	 * vblank event.(after drm_vblank_put function is called)
	 */
	drm_dev->vblank_disable_allowed = 1;

	return 0;
}

326
static struct exynos_drm_manager_ops vidi_manager_ops = {
327
	.initialize = vidi_mgr_initialize,
328 329 330 331 332 333 334
	.dpms = vidi_dpms,
	.commit = vidi_commit,
	.enable_vblank = vidi_enable_vblank,
	.disable_vblank = vidi_disable_vblank,
	.win_mode_set = vidi_win_mode_set,
	.win_commit = vidi_win_commit,
	.win_disable = vidi_win_disable,
335 336
};

337
static struct exynos_drm_manager vidi_manager = {
338 339
	.type = EXYNOS_DISPLAY_TYPE_VIDI,
	.ops = &vidi_manager_ops,
340 341
};

342 343 344 345 346
static void vidi_fake_vblank_handler(struct work_struct *work)
{
	struct vidi_context *ctx = container_of(work, struct vidi_context,
					work);

347
	if (ctx->pipe < 0)
348 349 350 351 352
		return;

	/* refresh rate is about 50Hz. */
	usleep_range(16000, 20000);

353 354 355
	mutex_lock(&ctx->lock);

	if (ctx->direct_vblank) {
356
		drm_handle_vblank(ctx->drm_dev, ctx->pipe);
357 358 359 360 361 362 363
		ctx->direct_vblank = false;
		mutex_unlock(&ctx->lock);
		return;
	}

	mutex_unlock(&ctx->lock);

364
	exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
365 366 367 368 369 370
}

static int vidi_show_connection(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	int rc;
371 372
	struct exynos_drm_manager *mgr = get_vidi_mgr(dev);
	struct vidi_context *ctx = mgr->ctx;
373 374 375 376 377 378 379 380 381 382 383 384 385 386

	mutex_lock(&ctx->lock);

	rc = sprintf(buf, "%d\n", ctx->connected);

	mutex_unlock(&ctx->lock);

	return rc;
}

static int vidi_store_connection(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t len)
{
387 388
	struct exynos_drm_manager *mgr = get_vidi_mgr(dev);
	struct vidi_context *ctx = mgr->ctx;
389 390 391 392 393 394 395 396 397
	int ret;

	ret = kstrtoint(buf, 0, &ctx->connected);
	if (ret)
		return ret;

	if (ctx->connected > 1)
		return -EINVAL;

398 399 400 401
	/* use fake edid data for test. */
	if (!ctx->raw_edid)
		ctx->raw_edid = (struct edid *)fake_edid_info;

402 403 404 405 406 407
	/* if raw_edid isn't same as fake data then it can't be tested. */
	if (ctx->raw_edid != (struct edid *)fake_edid_info) {
		DRM_DEBUG_KMS("edid data is not fake data.\n");
		return -EINVAL;
	}

408 409
	DRM_DEBUG_KMS("requested connection.\n");

410
	drm_helper_hpd_irq_event(ctx->drm_dev);
411 412 413 414 415 416 417 418 419 420 421 422

	return len;
}

static DEVICE_ATTR(connection, 0644, vidi_show_connection,
			vidi_store_connection);

int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
				struct drm_file *file_priv)
{
	struct vidi_context *ctx = NULL;
	struct drm_encoder *encoder;
423
	struct exynos_drm_display *display;
424 425 426 427 428 429 430 431 432 433 434 435 436 437
	struct drm_exynos_vidi_connection *vidi = data;

	if (!vidi) {
		DRM_DEBUG_KMS("user data for vidi is null.\n");
		return -EINVAL;
	}

	if (vidi->connection > 1) {
		DRM_DEBUG_KMS("connection should be 0 or 1.\n");
		return -EINVAL;
	}

	list_for_each_entry(encoder, &drm_dev->mode_config.encoder_list,
								head) {
438
		display = exynos_drm_get_display(encoder);
439

440 441
		if (display->type == EXYNOS_DISPLAY_TYPE_VIDI) {
			ctx = display->ctx;
442 443 444 445 446 447 448 449 450 451 452 453 454 455
			break;
		}
	}

	if (!ctx) {
		DRM_DEBUG_KMS("not found virtual device type encoder.\n");
		return -EINVAL;
	}

	if (ctx->connected == vidi->connection) {
		DRM_DEBUG_KMS("same connection request.\n");
		return -EINVAL;
	}

456
	if (vidi->connection) {
457 458 459
		struct edid *raw_edid  = (struct edid *)(uint32_t)vidi->edid;
		if (!drm_edid_is_valid(raw_edid)) {
			DRM_DEBUG_KMS("edid data is invalid.\n");
460 461
			return -EINVAL;
		}
J
Jani Nikula 已提交
462
		ctx->raw_edid = drm_edid_duplicate(raw_edid);
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
		if (!ctx->raw_edid) {
			DRM_DEBUG_KMS("failed to allocate raw_edid.\n");
			return -ENOMEM;
		}
	} else {
		/*
		 * with connection = 0, free raw_edid
		 * only if raw edid data isn't same as fake data.
		 */
		if (ctx->raw_edid && ctx->raw_edid !=
				(struct edid *)fake_edid_info) {
			kfree(ctx->raw_edid);
			ctx->raw_edid = NULL;
		}
	}
478 479

	ctx->connected = vidi->connection;
480
	drm_helper_hpd_irq_event(ctx->drm_dev);
481 482 483 484

	return 0;
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static enum drm_connector_status vidi_detect(struct drm_connector *connector,
			bool force)
{
	struct vidi_context *ctx = ctx_from_connector(connector);

	/*
	 * connection request would come from user side
	 * to do hotplug through specific ioctl.
	 */
	return ctx->connected ? connector_status_connected :
			connector_status_disconnected;
}

static void vidi_connector_destroy(struct drm_connector *connector)
{
}

static struct drm_connector_funcs vidi_connector_funcs = {
	.dpms = drm_helper_connector_dpms,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.detect = vidi_detect,
	.destroy = vidi_connector_destroy,
};

static int vidi_get_modes(struct drm_connector *connector)
{
	struct vidi_context *ctx = ctx_from_connector(connector);
	struct edid *edid;
	int edid_len;

	/*
	 * the edid data comes from user side and it would be set
	 * to ctx->raw_edid through specific ioctl.
	 */
	if (!ctx->raw_edid) {
		DRM_DEBUG_KMS("raw_edid is null.\n");
		return -EFAULT;
	}

	edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
	edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
	if (!edid) {
		DRM_DEBUG_KMS("failed to allocate edid\n");
		return -ENOMEM;
	}

	drm_mode_connector_update_edid_property(connector, edid);

	return drm_add_edid_modes(connector, edid);
}

static struct drm_encoder *vidi_best_encoder(struct drm_connector *connector)
{
	struct vidi_context *ctx = ctx_from_connector(connector);

	return ctx->encoder;
}

static struct drm_connector_helper_funcs vidi_connector_helper_funcs = {
	.get_modes = vidi_get_modes,
	.best_encoder = vidi_best_encoder,
};

static int vidi_create_connector(struct exynos_drm_display *display,
				struct drm_encoder *encoder)
{
	struct vidi_context *ctx = display->ctx;
	struct drm_connector *connector = &ctx->connector;
	int ret;

	ctx->encoder = encoder;
	connector->polled = DRM_CONNECTOR_POLL_HPD;

	ret = drm_connector_init(ctx->drm_dev, connector,
			&vidi_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
	if (ret) {
		DRM_ERROR("Failed to initialize connector with drm\n");
		return ret;
	}

	drm_connector_helper_add(connector, &vidi_connector_helper_funcs);
	drm_sysfs_connector_add(connector);
	drm_mode_connector_attach_encoder(connector, encoder);

	return 0;
}


static struct exynos_drm_display_ops vidi_display_ops = {
	.create_connector = vidi_create_connector,
};

static struct exynos_drm_display vidi_display = {
	.type = EXYNOS_DISPLAY_TYPE_VIDI,
	.ops = &vidi_display_ops,
};

582
static int vidi_probe(struct platform_device *pdev)
583 584 585 586 587
{
	struct device *dev = &pdev->dev;
	struct vidi_context *ctx;
	int ret;

588
	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
589 590 591 592 593 594 595
	if (!ctx)
		return -ENOMEM;

	ctx->default_win = 0;

	INIT_WORK(&ctx->work, vidi_fake_vblank_handler);

596
	vidi_manager.ctx = ctx;
597
	vidi_display.ctx = ctx;
598 599 600

	mutex_init(&ctx->lock);

601
	platform_set_drvdata(pdev, &vidi_manager);
602

603
	ret = device_create_file(dev, &dev_attr_connection);
604 605 606
	if (ret < 0)
		DRM_INFO("failed to create connection sysfs.\n");

607 608
	exynos_drm_manager_register(&vidi_manager);
	exynos_drm_display_register(&vidi_display);
609 610 611 612

	return 0;
}

613
static int vidi_remove(struct platform_device *pdev)
614 615 616
{
	struct vidi_context *ctx = platform_get_drvdata(pdev);

617 618
	exynos_drm_display_unregister(&vidi_display);
	exynos_drm_manager_unregister(&vidi_manager);
619

620 621 622 623 624
	if (ctx->raw_edid != (struct edid *)fake_edid_info) {
		kfree(ctx->raw_edid);
		ctx->raw_edid = NULL;
	}

625 626 627 628 629
	return 0;
}

struct platform_driver vidi_driver = {
	.probe		= vidi_probe,
630
	.remove		= vidi_remove,
631 632 633 634 635
	.driver		= {
		.name	= "exynos-drm-vidi",
		.owner	= THIS_MODULE,
	},
};