1 /* exynos_drm_crtc.c
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4  * Authors:
5  *	Inki Dae <inki.dae@samsung.com>
6  *	Joonyoung Shim <jy0922.shim@samsung.com>
7  *	Seung-Woo Kim <sw0312.kim@samsung.com>
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  */
14 
15 #include <drm/drmP.h>
16 #include <drm/drm_crtc_helper.h>
17 
18 #include "exynos_drm_crtc.h"
19 #include "exynos_drm_drv.h"
20 #include "exynos_drm_encoder.h"
21 #include "exynos_drm_plane.h"
22 
23 #define to_exynos_crtc(x)	container_of(x, struct exynos_drm_crtc,\
24 				drm_crtc)
25 
26 enum exynos_crtc_mode {
27 	CRTC_MODE_NORMAL,	/* normal mode */
28 	CRTC_MODE_BLANK,	/* The private plane of crtc is blank */
29 };
30 
31 /*
32  * Exynos specific crtc structure.
33  *
34  * @drm_crtc: crtc object.
35  * @manager: the manager associated with this crtc
36  * @pipe: a crtc index created at load() with a new crtc object creation
37  *	and the crtc object would be set to private->crtc array
38  *	to get a crtc object corresponding to this pipe from private->crtc
39  *	array when irq interrupt occurred. the reason of using this pipe is that
40  *	drm framework doesn't support multiple irq yet.
41  *	we can refer to the crtc to current hardware interrupt occurred through
42  *	this pipe value.
43  * @dpms: store the crtc dpms value
44  * @mode: store the crtc mode value
45  */
46 struct exynos_drm_crtc {
47 	struct drm_crtc			drm_crtc;
48 	struct exynos_drm_manager	*manager;
49 	unsigned int			pipe;
50 	unsigned int			dpms;
51 	enum exynos_crtc_mode		mode;
52 	wait_queue_head_t		pending_flip_queue;
53 	atomic_t			pending_flip;
54 };
55 
56 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
57 {
58 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
59 	struct exynos_drm_manager *manager = exynos_crtc->manager;
60 
61 	DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
62 
63 	if (exynos_crtc->dpms == mode) {
64 		DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
65 		return;
66 	}
67 
68 	if (mode > DRM_MODE_DPMS_ON) {
69 		/* wait for the completion of page flip. */
70 		if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
71 				!atomic_read(&exynos_crtc->pending_flip),
72 				HZ/20))
73 			atomic_set(&exynos_crtc->pending_flip, 0);
74 		drm_vblank_off(crtc->dev, exynos_crtc->pipe);
75 	}
76 
77 	if (manager->ops->dpms)
78 		manager->ops->dpms(manager, mode);
79 
80 	exynos_crtc->dpms = mode;
81 }
82 
83 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
84 {
85 	/* drm framework doesn't check NULL. */
86 }
87 
88 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
89 {
90 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
91 	struct exynos_drm_manager *manager = exynos_crtc->manager;
92 
93 	exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
94 
95 	exynos_plane_commit(crtc->primary);
96 
97 	if (manager->ops->commit)
98 		manager->ops->commit(manager);
99 
100 	exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_ON);
101 }
102 
103 static bool
104 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
105 			    const struct drm_display_mode *mode,
106 			    struct drm_display_mode *adjusted_mode)
107 {
108 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
109 	struct exynos_drm_manager *manager = exynos_crtc->manager;
110 
111 	if (manager->ops->mode_fixup)
112 		return manager->ops->mode_fixup(manager, mode, adjusted_mode);
113 
114 	return true;
115 }
116 
117 static int
118 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
119 			  struct drm_display_mode *adjusted_mode, int x, int y,
120 			  struct drm_framebuffer *old_fb)
121 {
122 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
123 	struct exynos_drm_manager *manager = exynos_crtc->manager;
124 	struct drm_framebuffer *fb = crtc->primary->fb;
125 	unsigned int crtc_w;
126 	unsigned int crtc_h;
127 
128 	/*
129 	 * copy the mode data adjusted by mode_fixup() into crtc->mode
130 	 * so that hardware can be seet to proper mode.
131 	 */
132 	memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
133 
134 	crtc_w = fb->width - x;
135 	crtc_h = fb->height - y;
136 
137 	if (manager->ops->mode_set)
138 		manager->ops->mode_set(manager, &crtc->mode);
139 
140 	return exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
141 				     crtc_w, crtc_h, x, y, crtc_w, crtc_h);
142 }
143 
144 static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
145 					  struct drm_framebuffer *old_fb)
146 {
147 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
148 	struct drm_framebuffer *fb = crtc->primary->fb;
149 	unsigned int crtc_w;
150 	unsigned int crtc_h;
151 	int ret;
152 
153 	/* when framebuffer changing is requested, crtc's dpms should be on */
154 	if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
155 		DRM_ERROR("failed framebuffer changing request.\n");
156 		return -EPERM;
157 	}
158 
159 	crtc_w = fb->width - x;
160 	crtc_h = fb->height - y;
161 
162 	ret = exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
163 				    crtc_w, crtc_h, x, y, crtc_w, crtc_h);
164 	if (ret)
165 		return ret;
166 
167 	exynos_drm_crtc_commit(crtc);
168 
169 	return 0;
170 }
171 
172 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
173 					  struct drm_framebuffer *old_fb)
174 {
175 	return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
176 }
177 
178 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
179 {
180 	struct drm_plane *plane;
181 	int ret;
182 
183 	exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
184 
185 	drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
186 		if (plane->crtc != crtc)
187 			continue;
188 
189 		ret = plane->funcs->disable_plane(plane);
190 		if (ret)
191 			DRM_ERROR("Failed to disable plane %d\n", ret);
192 	}
193 }
194 
195 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
196 	.dpms		= exynos_drm_crtc_dpms,
197 	.prepare	= exynos_drm_crtc_prepare,
198 	.commit		= exynos_drm_crtc_commit,
199 	.mode_fixup	= exynos_drm_crtc_mode_fixup,
200 	.mode_set	= exynos_drm_crtc_mode_set,
201 	.mode_set_base	= exynos_drm_crtc_mode_set_base,
202 	.disable	= exynos_drm_crtc_disable,
203 };
204 
205 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
206 				     struct drm_framebuffer *fb,
207 				     struct drm_pending_vblank_event *event,
208 				     uint32_t page_flip_flags)
209 {
210 	struct drm_device *dev = crtc->dev;
211 	struct exynos_drm_private *dev_priv = dev->dev_private;
212 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
213 	struct drm_framebuffer *old_fb = crtc->primary->fb;
214 	int ret = -EINVAL;
215 
216 	/* when the page flip is requested, crtc's dpms should be on */
217 	if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
218 		DRM_ERROR("failed page flip request.\n");
219 		return -EINVAL;
220 	}
221 
222 	mutex_lock(&dev->struct_mutex);
223 
224 	if (event) {
225 		/*
226 		 * the pipe from user always is 0 so we can set pipe number
227 		 * of current owner to event.
228 		 */
229 		event->pipe = exynos_crtc->pipe;
230 
231 		ret = drm_vblank_get(dev, exynos_crtc->pipe);
232 		if (ret) {
233 			DRM_DEBUG("failed to acquire vblank counter\n");
234 
235 			goto out;
236 		}
237 
238 		spin_lock_irq(&dev->event_lock);
239 		list_add_tail(&event->base.link,
240 				&dev_priv->pageflip_event_list);
241 		atomic_set(&exynos_crtc->pending_flip, 1);
242 		spin_unlock_irq(&dev->event_lock);
243 
244 		crtc->primary->fb = fb;
245 		ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
246 						    NULL);
247 		if (ret) {
248 			crtc->primary->fb = old_fb;
249 
250 			spin_lock_irq(&dev->event_lock);
251 			drm_vblank_put(dev, exynos_crtc->pipe);
252 			list_del(&event->base.link);
253 			atomic_set(&exynos_crtc->pending_flip, 0);
254 			spin_unlock_irq(&dev->event_lock);
255 
256 			goto out;
257 		}
258 	}
259 out:
260 	mutex_unlock(&dev->struct_mutex);
261 	return ret;
262 }
263 
264 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
265 {
266 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
267 	struct exynos_drm_private *private = crtc->dev->dev_private;
268 
269 	private->crtc[exynos_crtc->pipe] = NULL;
270 
271 	drm_crtc_cleanup(crtc);
272 	kfree(exynos_crtc);
273 }
274 
275 static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
276 					struct drm_property *property,
277 					uint64_t val)
278 {
279 	struct drm_device *dev = crtc->dev;
280 	struct exynos_drm_private *dev_priv = dev->dev_private;
281 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
282 
283 	if (property == dev_priv->crtc_mode_property) {
284 		enum exynos_crtc_mode mode = val;
285 
286 		if (mode == exynos_crtc->mode)
287 			return 0;
288 
289 		exynos_crtc->mode = mode;
290 
291 		switch (mode) {
292 		case CRTC_MODE_NORMAL:
293 			exynos_drm_crtc_commit(crtc);
294 			break;
295 		case CRTC_MODE_BLANK:
296 			exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_OFF);
297 			break;
298 		default:
299 			break;
300 		}
301 
302 		return 0;
303 	}
304 
305 	return -EINVAL;
306 }
307 
308 static struct drm_crtc_funcs exynos_crtc_funcs = {
309 	.set_config	= drm_crtc_helper_set_config,
310 	.page_flip	= exynos_drm_crtc_page_flip,
311 	.destroy	= exynos_drm_crtc_destroy,
312 	.set_property	= exynos_drm_crtc_set_property,
313 };
314 
315 static const struct drm_prop_enum_list mode_names[] = {
316 	{ CRTC_MODE_NORMAL, "normal" },
317 	{ CRTC_MODE_BLANK, "blank" },
318 };
319 
320 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
321 {
322 	struct drm_device *dev = crtc->dev;
323 	struct exynos_drm_private *dev_priv = dev->dev_private;
324 	struct drm_property *prop;
325 
326 	prop = dev_priv->crtc_mode_property;
327 	if (!prop) {
328 		prop = drm_property_create_enum(dev, 0, "mode", mode_names,
329 						ARRAY_SIZE(mode_names));
330 		if (!prop)
331 			return;
332 
333 		dev_priv->crtc_mode_property = prop;
334 	}
335 
336 	drm_object_attach_property(&crtc->base, prop, 0);
337 }
338 
339 int exynos_drm_crtc_create(struct exynos_drm_manager *manager)
340 {
341 	struct exynos_drm_crtc *exynos_crtc;
342 	struct drm_plane *plane;
343 	struct exynos_drm_private *private = manager->drm_dev->dev_private;
344 	struct drm_crtc *crtc;
345 	int ret;
346 
347 	exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
348 	if (!exynos_crtc)
349 		return -ENOMEM;
350 
351 	init_waitqueue_head(&exynos_crtc->pending_flip_queue);
352 	atomic_set(&exynos_crtc->pending_flip, 0);
353 
354 	exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
355 	exynos_crtc->manager = manager;
356 	exynos_crtc->pipe = manager->pipe;
357 	plane = exynos_plane_init(manager->drm_dev, 1 << manager->pipe,
358 				  DRM_PLANE_TYPE_PRIMARY);
359 	if (IS_ERR(plane)) {
360 		ret = PTR_ERR(plane);
361 		goto err_plane;
362 	}
363 
364 	manager->crtc = &exynos_crtc->drm_crtc;
365 	crtc = &exynos_crtc->drm_crtc;
366 
367 	private->crtc[manager->pipe] = crtc;
368 
369 	ret = drm_crtc_init_with_planes(manager->drm_dev, crtc, plane, NULL,
370 					&exynos_crtc_funcs);
371 	if (ret < 0)
372 		goto err_crtc;
373 
374 	drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
375 
376 	exynos_drm_crtc_attach_mode_property(crtc);
377 
378 	return 0;
379 
380 err_crtc:
381 	plane->funcs->destroy(plane);
382 err_plane:
383 	kfree(exynos_crtc);
384 	return ret;
385 }
386 
387 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
388 {
389 	struct exynos_drm_private *private = dev->dev_private;
390 	struct exynos_drm_crtc *exynos_crtc =
391 		to_exynos_crtc(private->crtc[pipe]);
392 	struct exynos_drm_manager *manager = exynos_crtc->manager;
393 
394 	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
395 		return -EPERM;
396 
397 	if (manager->ops->enable_vblank)
398 		manager->ops->enable_vblank(manager);
399 
400 	return 0;
401 }
402 
403 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
404 {
405 	struct exynos_drm_private *private = dev->dev_private;
406 	struct exynos_drm_crtc *exynos_crtc =
407 		to_exynos_crtc(private->crtc[pipe]);
408 	struct exynos_drm_manager *manager = exynos_crtc->manager;
409 
410 	if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
411 		return;
412 
413 	if (manager->ops->disable_vblank)
414 		manager->ops->disable_vblank(manager);
415 }
416 
417 void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
418 {
419 	struct exynos_drm_private *dev_priv = dev->dev_private;
420 	struct drm_pending_vblank_event *e, *t;
421 	struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
422 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
423 	unsigned long flags;
424 
425 	spin_lock_irqsave(&dev->event_lock, flags);
426 
427 	list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
428 			base.link) {
429 		/* if event's pipe isn't same as crtc then ignore it. */
430 		if (pipe != e->pipe)
431 			continue;
432 
433 		list_del(&e->base.link);
434 		drm_send_vblank_event(dev, -1, e);
435 		drm_vblank_put(dev, pipe);
436 		atomic_set(&exynos_crtc->pending_flip, 0);
437 		wake_up(&exynos_crtc->pending_flip_queue);
438 	}
439 
440 	spin_unlock_irqrestore(&dev->event_lock, flags);
441 }
442 
443 void exynos_drm_crtc_plane_mode_set(struct drm_crtc *crtc,
444 			struct exynos_drm_overlay *overlay)
445 {
446 	struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
447 
448 	if (manager->ops->win_mode_set)
449 		manager->ops->win_mode_set(manager, overlay);
450 }
451 
452 void exynos_drm_crtc_plane_commit(struct drm_crtc *crtc, int zpos)
453 {
454 	struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
455 
456 	if (manager->ops->win_commit)
457 		manager->ops->win_commit(manager, zpos);
458 }
459 
460 void exynos_drm_crtc_plane_enable(struct drm_crtc *crtc, int zpos)
461 {
462 	struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
463 
464 	if (manager->ops->win_enable)
465 		manager->ops->win_enable(manager, zpos);
466 }
467 
468 void exynos_drm_crtc_plane_disable(struct drm_crtc *crtc, int zpos)
469 {
470 	struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
471 
472 	if (manager->ops->win_disable)
473 		manager->ops->win_disable(manager, zpos);
474 }
475 
476 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
477 {
478 	struct exynos_drm_manager *manager;
479 	struct drm_device *dev = fb->dev;
480 	struct drm_crtc *crtc;
481 
482 	/*
483 	 * make sure that overlay data are updated to real hardware
484 	 * for all encoders.
485 	 */
486 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
487 		manager = to_exynos_crtc(crtc)->manager;
488 
489 		/*
490 		 * wait for vblank interrupt
491 		 * - this makes sure that overlay data are updated to
492 		 *	real hardware.
493 		 */
494 		if (manager->ops->wait_for_vblank)
495 			manager->ops->wait_for_vblank(manager);
496 	}
497 }
498 
499 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
500 					unsigned int out_type)
501 {
502 	struct drm_crtc *crtc;
503 
504 	list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
505 		struct exynos_drm_crtc *exynos_crtc;
506 
507 		exynos_crtc = to_exynos_crtc(crtc);
508 		if (exynos_crtc->manager->type == out_type)
509 			return exynos_crtc->manager->pipe;
510 	}
511 
512 	return -EPERM;
513 }
514 
515 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
516 {
517 	struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
518 
519 	if (manager->ops->te_handler)
520 		manager->ops->te_handler(manager);
521 }
522