exynos_drm_crtc.c 9.4 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
		if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
37 38
				(exynos_crtc->event == NULL), HZ/20))
			exynos_crtc->event = NULL;
39
		drm_crtc_vblank_off(crtc);
40 41
	}

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

45
	exynos_crtc->dpms = mode;
46 47 48

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

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)
{
58
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
59
	struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
60

61
	exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
62

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

66 67
	if (exynos_crtc->ops->commit)
		exynos_crtc->ops->commit(exynos_crtc);
68 69 70 71
}

static bool
exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
72
			    const struct drm_display_mode *mode,
73 74
			    struct drm_display_mode *adjusted_mode)
{
75 76
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);

77 78 79
	if (exynos_crtc->ops->mode_fixup)
		return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
						    adjusted_mode);
80

81 82 83 84 85 86 87 88
	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)
{
89
	struct drm_framebuffer *fb = crtc->primary->fb;
90 91
	unsigned int crtc_w;
	unsigned int crtc_h;
92
	int ret;
93

94 95 96 97 98
	/*
	 * 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));
99

100 101 102 103
	ret = exynos_check_plane(crtc->primary, fb);
	if (ret < 0)
		return ret;

104 105
	crtc_w = fb->width - x;
	crtc_h = fb->height - y;
106 107 108 109
	exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
			      crtc_w, crtc_h, x, y, crtc_w, crtc_h);

	return 0;
110 111
}

112
static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
113 114
					  struct drm_framebuffer *old_fb)
{
115
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
116
	struct drm_framebuffer *fb = crtc->primary->fb;
117 118
	unsigned int crtc_w;
	unsigned int crtc_h;
119

120 121 122 123 124 125
	/* 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;
	}

126 127
	crtc_w = fb->width - x;
	crtc_h = fb->height - y;
128

129 130
	return exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
				   crtc_w, crtc_h, x, y, crtc_w, crtc_h);
131 132
}

133 134
static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
{
135 136
	struct drm_plane *plane;
	int ret;
137 138

	exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
139

140
	drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
141 142 143 144 145 146 147
		if (plane->crtc != crtc)
			continue;

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

150 151 152 153 154 155 156
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,
157
	.disable	= exynos_drm_crtc_disable,
158 159 160
};

static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
161 162 163
				     struct drm_framebuffer *fb,
				     struct drm_pending_vblank_event *event,
				     uint32_t page_flip_flags)
164 165 166
{
	struct drm_device *dev = crtc->dev;
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
167
	struct drm_framebuffer *old_fb = crtc->primary->fb;
168
	unsigned int crtc_w, crtc_h;
169
	int ret;
170

171 172 173 174 175 176
	/* 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;
	}

177 178
	if (!event)
		return -EINVAL;
179

180 181 182 183 184
	spin_lock_irq(&dev->event_lock);
	if (exynos_crtc->event) {
		ret = -EBUSY;
		goto out;
	}
I
Inki Dae 已提交
185

186 187 188 189 190
	ret = drm_vblank_get(dev, exynos_crtc->pipe);
	if (ret) {
		DRM_DEBUG("failed to acquire vblank counter\n");
		goto out;
	}
I
Inki Dae 已提交
191

192 193
	exynos_crtc->event = event;
	spin_unlock_irq(&dev->event_lock);
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208
	/*
	 * the pipe from user always is 0 so we can set pipe number
	 * of current owner to event.
	 */
	event->pipe = exynos_crtc->pipe;

	crtc->primary->fb = fb;
	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);
	if (ret) {
		crtc->primary->fb = old_fb;
209
		spin_lock_irq(&dev->event_lock);
210 211
		exynos_crtc->event = NULL;
		drm_vblank_put(dev, exynos_crtc->pipe);
212
		spin_unlock_irq(&dev->event_lock);
213
		return ret;
214
	}
215 216 217

	return 0;

218
out:
219
	spin_unlock_irq(&dev->event_lock);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	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);
}

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,
};

240
struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
241 242 243 244 245
					struct drm_plane *plane,
					int pipe,
					enum exynos_drm_output_type type,
					const struct exynos_drm_crtc_ops *ops,
					void *ctx)
246 247
{
	struct exynos_drm_crtc *exynos_crtc;
248
	struct exynos_drm_private *private = drm_dev->dev_private;
249
	struct drm_crtc *crtc;
250
	int ret;
251 252

	exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
253
	if (!exynos_crtc)
254
		return ERR_PTR(-ENOMEM);
255

256
	init_waitqueue_head(&exynos_crtc->pending_flip_queue);
257 258

	exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
259
	exynos_crtc->pipe = pipe;
260
	exynos_crtc->type = type;
261 262
	exynos_crtc->ops = ops;
	exynos_crtc->ctx = ctx;
263

264
	crtc = &exynos_crtc->base;
265

266
	private->crtc[pipe] = crtc;
267

268
	ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
269 270 271 272
					&exynos_crtc_funcs);
	if (ret < 0)
		goto err_crtc;

273 274
	drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);

275
	return exynos_crtc;
276 277 278 279

err_crtc:
	plane->funcs->destroy(plane);
	kfree(exynos_crtc);
280
	return ERR_PTR(ret);
281 282
}

283
int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
284 285
{
	struct exynos_drm_private *private = dev->dev_private;
286
	struct exynos_drm_crtc *exynos_crtc =
287
		to_exynos_crtc(private->crtc[pipe]);
288

289 290 291
	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
		return -EPERM;

292 293
	if (exynos_crtc->ops->enable_vblank)
		exynos_crtc->ops->enable_vblank(exynos_crtc);
294 295 296 297

	return 0;
}

298
void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
299 300
{
	struct exynos_drm_private *private = dev->dev_private;
301
	struct exynos_drm_crtc *exynos_crtc =
302
		to_exynos_crtc(private->crtc[pipe]);
303

304 305 306
	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
		return;

307 308
	if (exynos_crtc->ops->disable_vblank)
		exynos_crtc->ops->disable_vblank(exynos_crtc);
309
}
310

311
void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
312 313
{
	struct exynos_drm_private *dev_priv = dev->dev_private;
314
	struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
315
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
316 317 318
	unsigned long flags;

	spin_lock_irqsave(&dev->event_lock, flags);
319
	if (exynos_crtc->event) {
320

321
		drm_send_vblank_event(dev, -1, exynos_crtc->event);
322
		drm_vblank_put(dev, pipe);
323
		wake_up(&exynos_crtc->pending_flip_queue);
324

325 326
	}

327
	exynos_crtc->event = NULL;
328 329
	spin_unlock_irqrestore(&dev->event_lock, flags);
}
330 331 332

void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
{
333
	struct exynos_drm_crtc *exynos_crtc;
334 335 336 337 338 339 340 341
	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) {
342
		exynos_crtc = to_exynos_crtc(crtc);
343 344 345 346 347 348

		/*
		 * wait for vblank interrupt
		 * - this makes sure that overlay data are updated to
		 *	real hardware.
		 */
349 350
		if (exynos_crtc->ops->wait_for_vblank)
			exynos_crtc->ops->wait_for_vblank(exynos_crtc);
351 352
	}
}
353 354 355 356 357 358 359 360 361 362

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);
363
		if (exynos_crtc->type == out_type)
364
			return exynos_crtc->pipe;
365 366 367 368
	}

	return -EPERM;
}
369 370 371

void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
{
372
	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
373

374 375
	if (exynos_crtc->ops->te_handler)
		exynos_crtc->ops->te_handler(exynos_crtc);
376
}