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 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 
20 #include "exynos_drm_crtc.h"
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_plane.h"
23 
24 static void exynos_drm_crtc_enable(struct drm_crtc *crtc)
25 {
26 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
27 
28 	if (exynos_crtc->ops->enable)
29 		exynos_crtc->ops->enable(exynos_crtc);
30 
31 	drm_crtc_vblank_on(crtc);
32 }
33 
34 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
35 {
36 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
37 
38 	drm_crtc_vblank_off(crtc);
39 
40 	if (exynos_crtc->ops->disable)
41 		exynos_crtc->ops->disable(exynos_crtc);
42 
43 	if (crtc->state->event && !crtc->state->active) {
44 		spin_lock_irq(&crtc->dev->event_lock);
45 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
46 		spin_unlock_irq(&crtc->dev->event_lock);
47 
48 		crtc->state->event = NULL;
49 	}
50 }
51 
52 static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
53 				     struct drm_crtc_state *state)
54 {
55 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
56 
57 	if (!state->enable)
58 		return 0;
59 
60 	if (exynos_crtc->ops->atomic_check)
61 		return exynos_crtc->ops->atomic_check(exynos_crtc, state);
62 
63 	return 0;
64 }
65 
66 static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
67 				     struct drm_crtc_state *old_crtc_state)
68 {
69 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
70 
71 	if (exynos_crtc->ops->atomic_begin)
72 		exynos_crtc->ops->atomic_begin(exynos_crtc);
73 }
74 
75 static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
76 				     struct drm_crtc_state *old_crtc_state)
77 {
78 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
79 
80 	if (exynos_crtc->ops->atomic_flush)
81 		exynos_crtc->ops->atomic_flush(exynos_crtc);
82 }
83 
84 static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
85 	.enable		= exynos_drm_crtc_enable,
86 	.disable	= exynos_drm_crtc_disable,
87 	.atomic_check	= exynos_crtc_atomic_check,
88 	.atomic_begin	= exynos_crtc_atomic_begin,
89 	.atomic_flush	= exynos_crtc_atomic_flush,
90 };
91 
92 void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
93 {
94 	struct drm_crtc *crtc = &exynos_crtc->base;
95 	struct drm_pending_vblank_event *event = crtc->state->event;
96 	unsigned long flags;
97 
98 	if (!event)
99 		return;
100 	crtc->state->event = NULL;
101 
102 	WARN_ON(drm_crtc_vblank_get(crtc) != 0);
103 
104 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
105 	drm_crtc_arm_vblank_event(crtc, event);
106 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
107 }
108 
109 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
110 {
111 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
112 
113 	drm_crtc_cleanup(crtc);
114 	kfree(exynos_crtc);
115 }
116 
117 static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
118 {
119 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
120 
121 	if (exynos_crtc->ops->enable_vblank)
122 		return exynos_crtc->ops->enable_vblank(exynos_crtc);
123 
124 	return 0;
125 }
126 
127 static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
128 {
129 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
130 
131 	if (exynos_crtc->ops->disable_vblank)
132 		exynos_crtc->ops->disable_vblank(exynos_crtc);
133 }
134 
135 static u32 exynos_drm_crtc_get_vblank_counter(struct drm_crtc *crtc)
136 {
137 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
138 
139 	if (exynos_crtc->ops->get_vblank_counter)
140 		return exynos_crtc->ops->get_vblank_counter(exynos_crtc);
141 
142 	return 0;
143 }
144 
145 static const struct drm_crtc_funcs exynos_crtc_funcs = {
146 	.set_config	= drm_atomic_helper_set_config,
147 	.page_flip	= drm_atomic_helper_page_flip,
148 	.destroy	= exynos_drm_crtc_destroy,
149 	.reset = drm_atomic_helper_crtc_reset,
150 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
151 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
152 	.enable_vblank = exynos_drm_crtc_enable_vblank,
153 	.disable_vblank = exynos_drm_crtc_disable_vblank,
154 	.get_vblank_counter = exynos_drm_crtc_get_vblank_counter,
155 };
156 
157 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
158 					struct drm_plane *plane,
159 					enum exynos_drm_output_type type,
160 					const struct exynos_drm_crtc_ops *ops,
161 					void *ctx)
162 {
163 	struct exynos_drm_crtc *exynos_crtc;
164 	struct drm_crtc *crtc;
165 	int ret;
166 
167 	exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
168 	if (!exynos_crtc)
169 		return ERR_PTR(-ENOMEM);
170 
171 	exynos_crtc->type = type;
172 	exynos_crtc->ops = ops;
173 	exynos_crtc->ctx = ctx;
174 
175 	crtc = &exynos_crtc->base;
176 
177 	ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
178 					&exynos_crtc_funcs, NULL);
179 	if (ret < 0)
180 		goto err_crtc;
181 
182 	drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
183 
184 	return exynos_crtc;
185 
186 err_crtc:
187 	plane->funcs->destroy(plane);
188 	kfree(exynos_crtc);
189 	return ERR_PTR(ret);
190 }
191 
192 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
193 				       enum exynos_drm_output_type out_type)
194 {
195 	struct drm_crtc *crtc;
196 
197 	drm_for_each_crtc(crtc, drm_dev)
198 		if (to_exynos_crtc(crtc)->type == out_type)
199 			return drm_crtc_index(crtc);
200 
201 	return -EPERM;
202 }
203 
204 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
205 {
206 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
207 
208 	if (exynos_crtc->ops->te_handler)
209 		exynos_crtc->ops->te_handler(exynos_crtc);
210 }
211