exynos_drm_crtc.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8
/* exynos_drm_crtc.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 * Authors:
 *	Inki Dae <inki.dae@samsung.com>
 *	Joonyoung Shim <jy0922.shim@samsung.com>
 *	Seung-Woo Kim <sw0312.kim@samsung.com>
 *
9 10 11 12
 * 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 14
 */

15 16
#include <drm/drmP.h>
#include <drm/drm_crtc_helper.h>
17

M
Mark Brown 已提交
18
#include "exynos_drm_crtc.h"
19 20
#include "exynos_drm_drv.h"
#include "exynos_drm_encoder.h"
21
#include "exynos_drm_plane.h"
22 23 24

static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
{
25
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
26

27 28
	DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);

29 30 31 32 33
	if (exynos_crtc->dpms == mode) {
		DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
		return;
	}

34 35
	if (mode > DRM_MODE_DPMS_ON) {
		/* wait for the completion of page flip. */
36 37 38 39
		if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
				!atomic_read(&exynos_crtc->pending_flip),
				HZ/20))
			atomic_set(&exynos_crtc->pending_flip, 0);
40
		drm_crtc_vblank_off(crtc);
41 42
	}

43 44
	if (exynos_crtc->ops->dpms)
		exynos_crtc->ops->dpms(exynos_crtc, mode);
45

46
	exynos_crtc->dpms = mode;
47 48 49

	if (mode == DRM_MODE_DPMS_ON)
		drm_crtc_vblank_on(crtc);
50 51 52 53 54 55 56 57 58
}

static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
{
	/* drm framework doesn't check NULL. */
}

static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
{
59
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
60
	struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
61

62
	exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
63

64 65
	if (exynos_crtc->ops->win_commit)
		exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
66

67 68
	if (exynos_crtc->ops->commit)
		exynos_crtc->ops->commit(exynos_crtc);
69

70
	exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_ON);
71 72 73 74
}

static bool
exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
75
			    const struct drm_display_mode *mode,
76 77
			    struct drm_display_mode *adjusted_mode)
{
78 79
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);

80 81 82
	if (exynos_crtc->ops->mode_fixup)
		return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
						    adjusted_mode);
83

84 85 86 87 88 89 90 91
	return true;
}

static int
exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
			  struct drm_display_mode *adjusted_mode, int x, int y,
			  struct drm_framebuffer *old_fb)
{
92
	struct drm_framebuffer *fb = crtc->primary->fb;
93 94
	unsigned int crtc_w;
	unsigned int crtc_h;
95

96 97 98 99 100
	/*
	 * copy the mode data adjusted by mode_fixup() into crtc->mode
	 * so that hardware can be seet to proper mode.
	 */
	memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
101

102 103 104 105
	crtc_w = fb->width - x;
	crtc_h = fb->height - y;
	return exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
				     crtc_w, crtc_h, x, y, crtc_w, crtc_h);
106 107
}

108
static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
109 110
					  struct drm_framebuffer *old_fb)
{
111
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
112
	struct drm_framebuffer *fb = crtc->primary->fb;
113 114
	unsigned int crtc_w;
	unsigned int crtc_h;
115

116 117 118 119 120 121
	/* when framebuffer changing is requested, crtc's dpms should be on */
	if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
		DRM_ERROR("failed framebuffer changing request.\n");
		return -EPERM;
	}

122 123
	crtc_w = fb->width - x;
	crtc_h = fb->height - y;
124

125 126
	return exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
				   crtc_w, crtc_h, x, y, crtc_w, crtc_h);
127 128
}

129 130
static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
{
131 132
	struct drm_plane *plane;
	int ret;
133 134

	exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
135

136
	drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
137 138 139 140 141 142 143
		if (plane->crtc != crtc)
			continue;

		ret = plane->funcs->disable_plane(plane);
		if (ret)
			DRM_ERROR("Failed to disable plane %d\n", ret);
	}
144 145
}

146 147 148 149 150 151 152
static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
	.dpms		= exynos_drm_crtc_dpms,
	.prepare	= exynos_drm_crtc_prepare,
	.commit		= exynos_drm_crtc_commit,
	.mode_fixup	= exynos_drm_crtc_mode_fixup,
	.mode_set	= exynos_drm_crtc_mode_set,
	.mode_set_base	= exynos_drm_crtc_mode_set_base,
153
	.disable	= exynos_drm_crtc_disable,
154 155 156
};

static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
157 158 159
				     struct drm_framebuffer *fb,
				     struct drm_pending_vblank_event *event,
				     uint32_t page_flip_flags)
160 161 162 163
{
	struct drm_device *dev = crtc->dev;
	struct exynos_drm_private *dev_priv = dev->dev_private;
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
164
	struct drm_framebuffer *old_fb = crtc->primary->fb;
165
	unsigned int crtc_w, crtc_h;
166 167
	int ret = -EINVAL;

168 169 170 171 172 173
	/* when the page flip is requested, crtc's dpms should be on */
	if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
		DRM_ERROR("failed page flip request.\n");
		return -EINVAL;
	}

174 175
	mutex_lock(&dev->struct_mutex);

I
Inki Dae 已提交
176 177 178 179 180 181 182
	if (event) {
		/*
		 * the pipe from user always is 0 so we can set pipe number
		 * of current owner to event.
		 */
		event->pipe = exynos_crtc->pipe;

183 184 185
		ret = drm_vblank_get(dev, exynos_crtc->pipe);
		if (ret) {
			DRM_DEBUG("failed to acquire vblank counter\n");
I
Inki Dae 已提交
186

187 188 189
			goto out;
		}

190
		spin_lock_irq(&dev->event_lock);
I
Inki Dae 已提交
191 192
		list_add_tail(&event->base.link,
				&dev_priv->pageflip_event_list);
193
		atomic_set(&exynos_crtc->pending_flip, 1);
194
		spin_unlock_irq(&dev->event_lock);
I
Inki Dae 已提交
195

196
		crtc->primary->fb = fb;
197 198 199 200 201
		crtc_w = fb->width - crtc->x;
		crtc_h = fb->height - crtc->y;
		ret = exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
					  crtc_w, crtc_h, crtc->x, crtc->y,
					  crtc_w, crtc_h);
202
		if (ret) {
203
			crtc->primary->fb = old_fb;
204 205

			spin_lock_irq(&dev->event_lock);
206
			drm_vblank_put(dev, exynos_crtc->pipe);
I
Inki Dae 已提交
207
			list_del(&event->base.link);
208
			atomic_set(&exynos_crtc->pending_flip, 0);
209
			spin_unlock_irq(&dev->event_lock);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

			goto out;
		}
	}
out:
	mutex_unlock(&dev->struct_mutex);
	return ret;
}

static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
{
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
	struct exynos_drm_private *private = crtc->dev->dev_private;

	private->crtc[exynos_crtc->pipe] = NULL;

	drm_crtc_cleanup(crtc);
	kfree(exynos_crtc);
}

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
					struct drm_property *property,
					uint64_t val)
{
	struct drm_device *dev = crtc->dev;
	struct exynos_drm_private *dev_priv = dev->dev_private;
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);

	if (property == dev_priv->crtc_mode_property) {
		enum exynos_crtc_mode mode = val;

		if (mode == exynos_crtc->mode)
			return 0;

		exynos_crtc->mode = mode;

		switch (mode) {
		case CRTC_MODE_NORMAL:
			exynos_drm_crtc_commit(crtc);
			break;
		case CRTC_MODE_BLANK:
251
			exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_OFF);
252 253 254 255 256 257 258 259 260 261 262
			break;
		default:
			break;
		}

		return 0;
	}

	return -EINVAL;
}

263 264 265 266
static struct drm_crtc_funcs exynos_crtc_funcs = {
	.set_config	= drm_crtc_helper_set_config,
	.page_flip	= exynos_drm_crtc_page_flip,
	.destroy	= exynos_drm_crtc_destroy,
267 268 269 270 271 272
	.set_property	= exynos_drm_crtc_set_property,
};

static const struct drm_prop_enum_list mode_names[] = {
	{ CRTC_MODE_NORMAL, "normal" },
	{ CRTC_MODE_BLANK, "blank" },
273 274
};

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct exynos_drm_private *dev_priv = dev->dev_private;
	struct drm_property *prop;

	prop = dev_priv->crtc_mode_property;
	if (!prop) {
		prop = drm_property_create_enum(dev, 0, "mode", mode_names,
						ARRAY_SIZE(mode_names));
		if (!prop)
			return;

		dev_priv->crtc_mode_property = prop;
	}

	drm_object_attach_property(&crtc->base, prop, 0);
}

294 295 296 297 298
struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
					       int pipe,
					       enum exynos_drm_output_type type,
					       struct exynos_drm_crtc_ops *ops,
					       void *ctx)
299 300
{
	struct exynos_drm_crtc *exynos_crtc;
301
	struct drm_plane *plane;
302
	struct exynos_drm_private *private = drm_dev->dev_private;
303
	struct drm_crtc *crtc;
304
	int ret;
305 306

	exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
307
	if (!exynos_crtc)
308
		return ERR_PTR(-ENOMEM);
309

310 311
	init_waitqueue_head(&exynos_crtc->pending_flip_queue);
	atomic_set(&exynos_crtc->pending_flip, 0);
312 313

	exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
314
	exynos_crtc->pipe = pipe;
315
	exynos_crtc->type = type;
316 317
	exynos_crtc->ops = ops;
	exynos_crtc->ctx = ctx;
318
	plane = exynos_plane_init(drm_dev, 1 << pipe,
319 320 321 322
				  DRM_PLANE_TYPE_PRIMARY);
	if (IS_ERR(plane)) {
		ret = PTR_ERR(plane);
		goto err_plane;
323 324
	}

325
	crtc = &exynos_crtc->base;
326

327
	private->crtc[pipe] = crtc;
328

329
	ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
330 331 332 333
					&exynos_crtc_funcs);
	if (ret < 0)
		goto err_crtc;

334 335
	drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);

336 337
	exynos_drm_crtc_attach_mode_property(crtc);

338
	return exynos_crtc;
339 340 341 342 343

err_crtc:
	plane->funcs->destroy(plane);
err_plane:
	kfree(exynos_crtc);
344
	return ERR_PTR(ret);
345 346
}

347
int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
348 349
{
	struct exynos_drm_private *private = dev->dev_private;
350
	struct exynos_drm_crtc *exynos_crtc =
351
		to_exynos_crtc(private->crtc[pipe]);
352

353 354 355
	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
		return -EPERM;

356 357
	if (exynos_crtc->ops->enable_vblank)
		exynos_crtc->ops->enable_vblank(exynos_crtc);
358 359 360 361

	return 0;
}

362
void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
363 364
{
	struct exynos_drm_private *private = dev->dev_private;
365
	struct exynos_drm_crtc *exynos_crtc =
366
		to_exynos_crtc(private->crtc[pipe]);
367

368 369 370
	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
		return;

371 372
	if (exynos_crtc->ops->disable_vblank)
		exynos_crtc->ops->disable_vblank(exynos_crtc);
373
}
374

375
void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
376 377 378
{
	struct exynos_drm_private *dev_priv = dev->dev_private;
	struct drm_pending_vblank_event *e, *t;
379
	struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
380
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
381 382 383 384 385 386 387
	unsigned long flags;

	spin_lock_irqsave(&dev->event_lock, flags);

	list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
			base.link) {
		/* if event's pipe isn't same as crtc then ignore it. */
388
		if (pipe != e->pipe)
389 390
			continue;

391 392
		list_del(&e->base.link);
		drm_send_vblank_event(dev, -1, e);
393
		drm_vblank_put(dev, pipe);
394 395
		atomic_set(&exynos_crtc->pending_flip, 0);
		wake_up(&exynos_crtc->pending_flip_queue);
396 397 398 399
	}

	spin_unlock_irqrestore(&dev->event_lock, flags);
}
400 401 402

void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
{
403
	struct exynos_drm_crtc *exynos_crtc;
404 405 406 407 408 409 410 411
	struct drm_device *dev = fb->dev;
	struct drm_crtc *crtc;

	/*
	 * make sure that overlay data are updated to real hardware
	 * for all encoders.
	 */
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
412
		exynos_crtc = to_exynos_crtc(crtc);
413 414 415 416 417 418

		/*
		 * wait for vblank interrupt
		 * - this makes sure that overlay data are updated to
		 *	real hardware.
		 */
419 420
		if (exynos_crtc->ops->wait_for_vblank)
			exynos_crtc->ops->wait_for_vblank(exynos_crtc);
421 422
	}
}
423 424 425 426 427 428 429 430 431 432

int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
					unsigned int out_type)
{
	struct drm_crtc *crtc;

	list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
		struct exynos_drm_crtc *exynos_crtc;

		exynos_crtc = to_exynos_crtc(crtc);
433
		if (exynos_crtc->type == out_type)
434
			return exynos_crtc->pipe;
435 436 437 438
	}

	return -EPERM;
}
439 440 441

void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
{
442
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
443

444 445
	if (exynos_crtc->ops->te_handler)
		exynos_crtc->ops->te_handler(exynos_crtc);
446
}