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 
44 static void
45 exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
46 {
47 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
48 
49 	if (exynos_crtc->ops->commit)
50 		exynos_crtc->ops->commit(exynos_crtc);
51 }
52 
53 static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
54 				     struct drm_crtc_state *state)
55 {
56 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
57 
58 	if (exynos_crtc->ops->atomic_check)
59 		return exynos_crtc->ops->atomic_check(exynos_crtc, state);
60 
61 	return 0;
62 }
63 
64 static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
65 				     struct drm_crtc_state *old_crtc_state)
66 {
67 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
68 	struct drm_plane *plane;
69 
70 	exynos_crtc->event = crtc->state->event;
71 
72 	drm_atomic_crtc_for_each_plane(plane, crtc) {
73 		struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
74 
75 		if (exynos_crtc->ops->atomic_begin)
76 			exynos_crtc->ops->atomic_begin(exynos_crtc,
77 							exynos_plane);
78 	}
79 }
80 
81 static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
82 				     struct drm_crtc_state *old_crtc_state)
83 {
84 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
85 	struct drm_plane *plane;
86 
87 	drm_atomic_crtc_for_each_plane(plane, crtc) {
88 		struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
89 
90 		if (exynos_crtc->ops->atomic_flush)
91 			exynos_crtc->ops->atomic_flush(exynos_crtc,
92 							exynos_plane);
93 	}
94 }
95 
96 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
97 	.enable		= exynos_drm_crtc_enable,
98 	.disable	= exynos_drm_crtc_disable,
99 	.mode_set_nofb	= exynos_drm_crtc_mode_set_nofb,
100 	.atomic_check	= exynos_crtc_atomic_check,
101 	.atomic_begin	= exynos_crtc_atomic_begin,
102 	.atomic_flush	= exynos_crtc_atomic_flush,
103 };
104 
105 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
106 {
107 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
108 	struct exynos_drm_private *private = crtc->dev->dev_private;
109 
110 	private->crtc[exynos_crtc->pipe] = NULL;
111 
112 	drm_crtc_cleanup(crtc);
113 	kfree(exynos_crtc);
114 }
115 
116 static struct drm_crtc_funcs exynos_crtc_funcs = {
117 	.set_config	= drm_atomic_helper_set_config,
118 	.page_flip	= drm_atomic_helper_page_flip,
119 	.destroy	= exynos_drm_crtc_destroy,
120 	.reset = drm_atomic_helper_crtc_reset,
121 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
122 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
123 };
124 
125 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
126 					struct drm_plane *plane,
127 					int pipe,
128 					enum exynos_drm_output_type type,
129 					const struct exynos_drm_crtc_ops *ops,
130 					void *ctx)
131 {
132 	struct exynos_drm_crtc *exynos_crtc;
133 	struct exynos_drm_private *private = drm_dev->dev_private;
134 	struct drm_crtc *crtc;
135 	int ret;
136 
137 	exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
138 	if (!exynos_crtc)
139 		return ERR_PTR(-ENOMEM);
140 
141 	exynos_crtc->pipe = pipe;
142 	exynos_crtc->type = type;
143 	exynos_crtc->ops = ops;
144 	exynos_crtc->ctx = ctx;
145 
146 	init_waitqueue_head(&exynos_crtc->wait_update);
147 
148 	crtc = &exynos_crtc->base;
149 
150 	private->crtc[pipe] = crtc;
151 
152 	ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
153 					&exynos_crtc_funcs);
154 	if (ret < 0)
155 		goto err_crtc;
156 
157 	drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
158 
159 	return exynos_crtc;
160 
161 err_crtc:
162 	plane->funcs->destroy(plane);
163 	kfree(exynos_crtc);
164 	return ERR_PTR(ret);
165 }
166 
167 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
168 {
169 	struct exynos_drm_private *private = dev->dev_private;
170 	struct exynos_drm_crtc *exynos_crtc =
171 		to_exynos_crtc(private->crtc[pipe]);
172 
173 	if (exynos_crtc->ops->enable_vblank)
174 		return exynos_crtc->ops->enable_vblank(exynos_crtc);
175 
176 	return 0;
177 }
178 
179 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, unsigned int pipe)
180 {
181 	struct exynos_drm_private *private = dev->dev_private;
182 	struct exynos_drm_crtc *exynos_crtc =
183 		to_exynos_crtc(private->crtc[pipe]);
184 
185 	if (exynos_crtc->ops->disable_vblank)
186 		exynos_crtc->ops->disable_vblank(exynos_crtc);
187 }
188 
189 void exynos_drm_crtc_wait_pending_update(struct exynos_drm_crtc *exynos_crtc)
190 {
191 	wait_event_timeout(exynos_crtc->wait_update,
192 			   (atomic_read(&exynos_crtc->pending_update) == 0),
193 			   msecs_to_jiffies(50));
194 }
195 
196 void exynos_drm_crtc_finish_update(struct exynos_drm_crtc *exynos_crtc,
197 				struct exynos_drm_plane *exynos_plane)
198 {
199 	struct drm_crtc *crtc = &exynos_crtc->base;
200 	unsigned long flags;
201 
202 	exynos_plane->pending_fb = NULL;
203 
204 	if (atomic_dec_and_test(&exynos_crtc->pending_update))
205 		wake_up(&exynos_crtc->wait_update);
206 
207 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
208 	if (exynos_crtc->event)
209 		drm_crtc_send_vblank_event(crtc, exynos_crtc->event);
210 
211 	exynos_crtc->event = NULL;
212 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
213 }
214 
215 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
216 {
217 	struct exynos_drm_crtc *exynos_crtc;
218 	struct drm_device *dev = fb->dev;
219 	struct drm_crtc *crtc;
220 
221 	/*
222 	 * make sure that overlay data are updated to real hardware
223 	 * for all encoders.
224 	 */
225 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
226 		exynos_crtc = to_exynos_crtc(crtc);
227 
228 		/*
229 		 * wait for vblank interrupt
230 		 * - this makes sure that overlay data are updated to
231 		 *	real hardware.
232 		 */
233 		if (exynos_crtc->ops->wait_for_vblank)
234 			exynos_crtc->ops->wait_for_vblank(exynos_crtc);
235 	}
236 }
237 
238 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
239 				       enum exynos_drm_output_type out_type)
240 {
241 	struct drm_crtc *crtc;
242 
243 	list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
244 		struct exynos_drm_crtc *exynos_crtc;
245 
246 		exynos_crtc = to_exynos_crtc(crtc);
247 		if (exynos_crtc->type == out_type)
248 			return exynos_crtc->pipe;
249 	}
250 
251 	return -EPERM;
252 }
253 
254 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
255 {
256 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
257 
258 	if (exynos_crtc->ops->te_handler)
259 		exynos_crtc->ops->te_handler(exynos_crtc);
260 }
261