Commit | Line | Data |
---|---|---|
1c248b7d ID |
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 | * | |
d81aecb5 ID |
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. | |
1c248b7d ID |
13 | */ |
14 | ||
760285e7 DH |
15 | #include <drm/drmP.h> |
16 | #include <drm/drm_crtc_helper.h> | |
1c248b7d ID |
17 | |
18 | #include "exynos_drm_drv.h" | |
1c248b7d | 19 | #include "exynos_drm_encoder.h" |
b5d2eb3b | 20 | #include "exynos_drm_plane.h" |
1c248b7d ID |
21 | |
22 | #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\ | |
23 | drm_crtc) | |
24 | ||
3b8d1cf8 JS |
25 | enum exynos_crtc_mode { |
26 | CRTC_MODE_NORMAL, /* normal mode */ | |
27 | CRTC_MODE_BLANK, /* The private plane of crtc is blank */ | |
28 | }; | |
29 | ||
1c248b7d ID |
30 | /* |
31 | * Exynos specific crtc structure. | |
32 | * | |
33 | * @drm_crtc: crtc object. | |
b5d2eb3b | 34 | * @drm_plane: pointer of private plane object for this crtc |
1c248b7d ID |
35 | * @pipe: a crtc index created at load() with a new crtc object creation |
36 | * and the crtc object would be set to private->crtc array | |
37 | * to get a crtc object corresponding to this pipe from private->crtc | |
38 | * array when irq interrupt occured. the reason of using this pipe is that | |
39 | * drm framework doesn't support multiple irq yet. | |
40 | * we can refer to the crtc to current hardware interrupt occured through | |
41 | * this pipe value. | |
ec05da95 | 42 | * @dpms: store the crtc dpms value |
3b8d1cf8 | 43 | * @mode: store the crtc mode value |
1c248b7d ID |
44 | */ |
45 | struct exynos_drm_crtc { | |
46 | struct drm_crtc drm_crtc; | |
b5d2eb3b | 47 | struct drm_plane *plane; |
1c248b7d | 48 | unsigned int pipe; |
ec05da95 | 49 | unsigned int dpms; |
3b8d1cf8 | 50 | enum exynos_crtc_mode mode; |
20cd2640 ID |
51 | wait_queue_head_t pending_flip_queue; |
52 | atomic_t pending_flip; | |
1c248b7d ID |
53 | }; |
54 | ||
1c248b7d ID |
55 | static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode) |
56 | { | |
d2716c89 | 57 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
1c248b7d | 58 | |
d2716c89 JS |
59 | DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode); |
60 | ||
ec05da95 ID |
61 | if (exynos_crtc->dpms == mode) { |
62 | DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n"); | |
63 | return; | |
64 | } | |
65 | ||
20cd2640 ID |
66 | if (mode > DRM_MODE_DPMS_ON) { |
67 | /* wait for the completion of page flip. */ | |
68 | wait_event(exynos_crtc->pending_flip_queue, | |
69 | atomic_read(&exynos_crtc->pending_flip) == 0); | |
70 | drm_vblank_off(crtc->dev, exynos_crtc->pipe); | |
71 | } | |
72 | ||
cf5188ac JS |
73 | exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms); |
74 | exynos_crtc->dpms = mode; | |
1c248b7d ID |
75 | } |
76 | ||
77 | static void exynos_drm_crtc_prepare(struct drm_crtc *crtc) | |
78 | { | |
79 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
80 | ||
81 | /* drm framework doesn't check NULL. */ | |
82 | } | |
83 | ||
84 | static void exynos_drm_crtc_commit(struct drm_crtc *crtc) | |
85 | { | |
d2716c89 JS |
86 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
87 | ||
1c248b7d ID |
88 | DRM_DEBUG_KMS("%s\n", __FILE__); |
89 | ||
50caf25c | 90 | exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON); |
4070d212 | 91 | exynos_plane_commit(exynos_crtc->plane); |
cf5188ac | 92 | exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON); |
1c248b7d ID |
93 | } |
94 | ||
95 | static bool | |
96 | exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc, | |
e811f5ae | 97 | const struct drm_display_mode *mode, |
1c248b7d ID |
98 | struct drm_display_mode *adjusted_mode) |
99 | { | |
100 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
101 | ||
102 | /* drm framework doesn't check NULL */ | |
103 | return true; | |
104 | } | |
105 | ||
106 | static int | |
107 | exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode, | |
108 | struct drm_display_mode *adjusted_mode, int x, int y, | |
109 | struct drm_framebuffer *old_fb) | |
110 | { | |
aeb29224 | 111 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
4070d212 JS |
112 | struct drm_plane *plane = exynos_crtc->plane; |
113 | unsigned int crtc_w; | |
114 | unsigned int crtc_h; | |
d249ce02 | 115 | int pipe = exynos_crtc->pipe; |
aeb29224 JS |
116 | int ret; |
117 | ||
1c248b7d ID |
118 | DRM_DEBUG_KMS("%s\n", __FILE__); |
119 | ||
1de425b0 ID |
120 | /* |
121 | * copy the mode data adjusted by mode_fixup() into crtc->mode | |
122 | * so that hardware can be seet to proper mode. | |
123 | */ | |
124 | memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode)); | |
1c248b7d | 125 | |
4070d212 JS |
126 | crtc_w = crtc->fb->width - x; |
127 | crtc_h = crtc->fb->height - y; | |
128 | ||
129 | ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h, | |
130 | x, y, crtc_w, crtc_h); | |
aeb29224 JS |
131 | if (ret) |
132 | return ret; | |
133 | ||
4070d212 JS |
134 | plane->crtc = crtc; |
135 | plane->fb = crtc->fb; | |
136 | ||
d249ce02 | 137 | exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe); |
aeb29224 JS |
138 | |
139 | return 0; | |
1c248b7d ID |
140 | } |
141 | ||
142 | static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, | |
143 | struct drm_framebuffer *old_fb) | |
144 | { | |
4070d212 JS |
145 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
146 | struct drm_plane *plane = exynos_crtc->plane; | |
147 | unsigned int crtc_w; | |
148 | unsigned int crtc_h; | |
1c248b7d ID |
149 | int ret; |
150 | ||
151 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
152 | ||
32aeab17 ID |
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 | ||
4070d212 JS |
159 | crtc_w = crtc->fb->width - x; |
160 | crtc_h = crtc->fb->height - y; | |
161 | ||
162 | ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h, | |
163 | x, y, crtc_w, crtc_h); | |
1c248b7d ID |
164 | if (ret) |
165 | return ret; | |
166 | ||
bebab8ff | 167 | exynos_drm_crtc_commit(crtc); |
1c248b7d | 168 | |
4070d212 | 169 | return 0; |
1c248b7d ID |
170 | } |
171 | ||
a365d9eb JS |
172 | static void exynos_drm_crtc_disable(struct drm_crtc *crtc) |
173 | { | |
174 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); | |
175 | ||
176 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
177 | ||
178 | exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF); | |
179 | exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); | |
180 | } | |
181 | ||
1c248b7d ID |
182 | static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = { |
183 | .dpms = exynos_drm_crtc_dpms, | |
184 | .prepare = exynos_drm_crtc_prepare, | |
185 | .commit = exynos_drm_crtc_commit, | |
186 | .mode_fixup = exynos_drm_crtc_mode_fixup, | |
187 | .mode_set = exynos_drm_crtc_mode_set, | |
188 | .mode_set_base = exynos_drm_crtc_mode_set_base, | |
a365d9eb | 189 | .disable = exynos_drm_crtc_disable, |
1c248b7d ID |
190 | }; |
191 | ||
192 | static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc, | |
193 | struct drm_framebuffer *fb, | |
194 | struct drm_pending_vblank_event *event) | |
195 | { | |
196 | struct drm_device *dev = crtc->dev; | |
197 | struct exynos_drm_private *dev_priv = dev->dev_private; | |
198 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); | |
199 | struct drm_framebuffer *old_fb = crtc->fb; | |
200 | int ret = -EINVAL; | |
201 | ||
202 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
203 | ||
ef6223dc ID |
204 | /* when the page flip is requested, crtc's dpms should be on */ |
205 | if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) { | |
206 | DRM_ERROR("failed page flip request.\n"); | |
207 | return -EINVAL; | |
208 | } | |
209 | ||
1c248b7d ID |
210 | mutex_lock(&dev->struct_mutex); |
211 | ||
ccf4d883 ID |
212 | if (event) { |
213 | /* | |
214 | * the pipe from user always is 0 so we can set pipe number | |
215 | * of current owner to event. | |
216 | */ | |
217 | event->pipe = exynos_crtc->pipe; | |
218 | ||
1c248b7d ID |
219 | ret = drm_vblank_get(dev, exynos_crtc->pipe); |
220 | if (ret) { | |
221 | DRM_DEBUG("failed to acquire vblank counter\n"); | |
ccf4d883 | 222 | |
1c248b7d ID |
223 | goto out; |
224 | } | |
225 | ||
85473328 | 226 | spin_lock_irq(&dev->event_lock); |
c5614ae3 ID |
227 | list_add_tail(&event->base.link, |
228 | &dev_priv->pageflip_event_list); | |
20cd2640 | 229 | atomic_set(&exynos_crtc->pending_flip, 1); |
85473328 | 230 | spin_unlock_irq(&dev->event_lock); |
c5614ae3 | 231 | |
1c248b7d | 232 | crtc->fb = fb; |
4070d212 JS |
233 | ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y, |
234 | NULL); | |
1c248b7d ID |
235 | if (ret) { |
236 | crtc->fb = old_fb; | |
85473328 ID |
237 | |
238 | spin_lock_irq(&dev->event_lock); | |
1c248b7d | 239 | drm_vblank_put(dev, exynos_crtc->pipe); |
ccf4d883 | 240 | list_del(&event->base.link); |
85473328 | 241 | spin_unlock_irq(&dev->event_lock); |
1c248b7d ID |
242 | |
243 | goto out; | |
244 | } | |
1c248b7d ID |
245 | } |
246 | out: | |
247 | mutex_unlock(&dev->struct_mutex); | |
248 | return ret; | |
249 | } | |
250 | ||
251 | static void exynos_drm_crtc_destroy(struct drm_crtc *crtc) | |
252 | { | |
253 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); | |
254 | struct exynos_drm_private *private = crtc->dev->dev_private; | |
255 | ||
256 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
257 | ||
258 | private->crtc[exynos_crtc->pipe] = NULL; | |
259 | ||
260 | drm_crtc_cleanup(crtc); | |
261 | kfree(exynos_crtc); | |
262 | } | |
263 | ||
3b8d1cf8 JS |
264 | static int exynos_drm_crtc_set_property(struct drm_crtc *crtc, |
265 | struct drm_property *property, | |
266 | uint64_t val) | |
267 | { | |
268 | struct drm_device *dev = crtc->dev; | |
269 | struct exynos_drm_private *dev_priv = dev->dev_private; | |
270 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); | |
271 | ||
272 | DRM_DEBUG_KMS("%s\n", __func__); | |
273 | ||
274 | if (property == dev_priv->crtc_mode_property) { | |
275 | enum exynos_crtc_mode mode = val; | |
276 | ||
277 | if (mode == exynos_crtc->mode) | |
278 | return 0; | |
279 | ||
280 | exynos_crtc->mode = mode; | |
281 | ||
282 | switch (mode) { | |
283 | case CRTC_MODE_NORMAL: | |
284 | exynos_drm_crtc_commit(crtc); | |
285 | break; | |
286 | case CRTC_MODE_BLANK: | |
287 | exynos_plane_dpms(exynos_crtc->plane, | |
288 | DRM_MODE_DPMS_OFF); | |
289 | break; | |
290 | default: | |
291 | break; | |
292 | } | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | return -EINVAL; | |
298 | } | |
299 | ||
1c248b7d ID |
300 | static struct drm_crtc_funcs exynos_crtc_funcs = { |
301 | .set_config = drm_crtc_helper_set_config, | |
302 | .page_flip = exynos_drm_crtc_page_flip, | |
303 | .destroy = exynos_drm_crtc_destroy, | |
3b8d1cf8 JS |
304 | .set_property = exynos_drm_crtc_set_property, |
305 | }; | |
306 | ||
307 | static const struct drm_prop_enum_list mode_names[] = { | |
308 | { CRTC_MODE_NORMAL, "normal" }, | |
309 | { CRTC_MODE_BLANK, "blank" }, | |
1c248b7d ID |
310 | }; |
311 | ||
3b8d1cf8 JS |
312 | static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc) |
313 | { | |
314 | struct drm_device *dev = crtc->dev; | |
315 | struct exynos_drm_private *dev_priv = dev->dev_private; | |
316 | struct drm_property *prop; | |
317 | ||
318 | DRM_DEBUG_KMS("%s\n", __func__); | |
319 | ||
320 | prop = dev_priv->crtc_mode_property; | |
321 | if (!prop) { | |
322 | prop = drm_property_create_enum(dev, 0, "mode", mode_names, | |
323 | ARRAY_SIZE(mode_names)); | |
324 | if (!prop) | |
325 | return; | |
326 | ||
327 | dev_priv->crtc_mode_property = prop; | |
328 | } | |
329 | ||
330 | drm_object_attach_property(&crtc->base, prop, 0); | |
331 | } | |
332 | ||
1c248b7d ID |
333 | int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr) |
334 | { | |
335 | struct exynos_drm_crtc *exynos_crtc; | |
336 | struct exynos_drm_private *private = dev->dev_private; | |
337 | struct drm_crtc *crtc; | |
338 | ||
339 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
340 | ||
341 | exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL); | |
342 | if (!exynos_crtc) { | |
343 | DRM_ERROR("failed to allocate exynos crtc\n"); | |
344 | return -ENOMEM; | |
345 | } | |
346 | ||
347 | exynos_crtc->pipe = nr; | |
ec05da95 | 348 | exynos_crtc->dpms = DRM_MODE_DPMS_OFF; |
20cd2640 ID |
349 | init_waitqueue_head(&exynos_crtc->pending_flip_queue); |
350 | atomic_set(&exynos_crtc->pending_flip, 0); | |
b5d2eb3b JS |
351 | exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true); |
352 | if (!exynos_crtc->plane) { | |
353 | kfree(exynos_crtc); | |
354 | return -ENOMEM; | |
355 | } | |
356 | ||
1c248b7d ID |
357 | crtc = &exynos_crtc->drm_crtc; |
358 | ||
359 | private->crtc[nr] = crtc; | |
360 | ||
361 | drm_crtc_init(dev, crtc, &exynos_crtc_funcs); | |
362 | drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs); | |
363 | ||
3b8d1cf8 JS |
364 | exynos_drm_crtc_attach_mode_property(crtc); |
365 | ||
1c248b7d ID |
366 | return 0; |
367 | } | |
368 | ||
369 | int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc) | |
370 | { | |
371 | struct exynos_drm_private *private = dev->dev_private; | |
ec05da95 ID |
372 | struct exynos_drm_crtc *exynos_crtc = |
373 | to_exynos_crtc(private->crtc[crtc]); | |
1c248b7d ID |
374 | |
375 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
376 | ||
ec05da95 ID |
377 | if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) |
378 | return -EPERM; | |
379 | ||
1c248b7d ID |
380 | exynos_drm_fn_encoder(private->crtc[crtc], &crtc, |
381 | exynos_drm_enable_vblank); | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc) | |
387 | { | |
388 | struct exynos_drm_private *private = dev->dev_private; | |
ec05da95 ID |
389 | struct exynos_drm_crtc *exynos_crtc = |
390 | to_exynos_crtc(private->crtc[crtc]); | |
1c248b7d ID |
391 | |
392 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
393 | ||
ec05da95 ID |
394 | if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) |
395 | return; | |
396 | ||
1c248b7d ID |
397 | exynos_drm_fn_encoder(private->crtc[crtc], &crtc, |
398 | exynos_drm_disable_vblank); | |
399 | } | |
663d8766 RS |
400 | |
401 | void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int crtc) | |
402 | { | |
403 | struct exynos_drm_private *dev_priv = dev->dev_private; | |
404 | struct drm_pending_vblank_event *e, *t; | |
20cd2640 ID |
405 | struct drm_crtc *drm_crtc = dev_priv->crtc[crtc]; |
406 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc); | |
663d8766 RS |
407 | unsigned long flags; |
408 | ||
409 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
410 | ||
411 | spin_lock_irqsave(&dev->event_lock, flags); | |
412 | ||
413 | list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list, | |
414 | base.link) { | |
415 | /* if event's pipe isn't same as crtc then ignore it. */ | |
416 | if (crtc != e->pipe) | |
417 | continue; | |
418 | ||
c5cca97f RC |
419 | list_del(&e->base.link); |
420 | drm_send_vblank_event(dev, -1, e); | |
663d8766 | 421 | drm_vblank_put(dev, crtc); |
20cd2640 ID |
422 | atomic_set(&exynos_crtc->pending_flip, 0); |
423 | wake_up(&exynos_crtc->pending_flip_queue); | |
663d8766 RS |
424 | } |
425 | ||
426 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
427 | } |