]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/exynos/exynos_drm_crtc.c
drm/exynos: atomic phase 1: use drm_plane_helper_update()
[karo-tx-linux.git] / drivers / gpu / drm / exynos / exynos_drm_crtc.c
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 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
24 {
25         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
26
27         DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
28
29         if (exynos_crtc->dpms == mode) {
30                 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
31                 return;
32         }
33
34         if (mode > DRM_MODE_DPMS_ON) {
35                 /* wait for the completion of page flip. */
36                 if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
37                                 (exynos_crtc->event == NULL), HZ/20))
38                         exynos_crtc->event = NULL;
39                 drm_crtc_vblank_off(crtc);
40         }
41
42         if (exynos_crtc->ops->dpms)
43                 exynos_crtc->ops->dpms(exynos_crtc, mode);
44
45         exynos_crtc->dpms = mode;
46
47         if (mode == DRM_MODE_DPMS_ON)
48                 drm_crtc_vblank_on(crtc);
49 }
50
51 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
52 {
53         /* drm framework doesn't check NULL. */
54 }
55
56 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
57 {
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         if (exynos_crtc->ops->win_commit)
64                 exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
65
66         if (exynos_crtc->ops->commit)
67                 exynos_crtc->ops->commit(exynos_crtc);
68 }
69
70 static bool
71 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
72                             const struct drm_display_mode *mode,
73                             struct drm_display_mode *adjusted_mode)
74 {
75         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
76
77         if (exynos_crtc->ops->mode_fixup)
78                 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
79                                                     adjusted_mode);
80
81         return true;
82 }
83
84 static int
85 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
86                           struct drm_display_mode *adjusted_mode, int x, int y,
87                           struct drm_framebuffer *old_fb)
88 {
89         struct drm_framebuffer *fb = crtc->primary->fb;
90         unsigned int crtc_w;
91         unsigned int crtc_h;
92         int ret;
93
94         /*
95          * copy the mode data adjusted by mode_fixup() into crtc->mode
96          * so that hardware can be seet to proper mode.
97          */
98         memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
99
100         ret = exynos_check_plane(crtc->primary, fb);
101         if (ret < 0)
102                 return ret;
103
104         crtc_w = fb->width - x;
105         crtc_h = fb->height - y;
106         exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
107                               crtc_w, crtc_h, x, y, crtc_w, crtc_h);
108
109         return 0;
110 }
111
112 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
113                                           struct drm_framebuffer *old_fb)
114 {
115         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
116         struct drm_framebuffer *fb = crtc->primary->fb;
117         unsigned int crtc_w;
118         unsigned int crtc_h;
119         int ret;
120
121         /* when framebuffer changing is requested, crtc's dpms should be on */
122         if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
123                 DRM_ERROR("failed framebuffer changing request.\n");
124                 return -EPERM;
125         }
126
127         ret = exynos_check_plane(crtc->primary, fb);
128         if (ret)
129                 return ret;
130
131         crtc_w = fb->width - x;
132         crtc_h = fb->height - y;
133         exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
134                             crtc_w, crtc_h, x << 16, y << 16,
135                             crtc_w << 16, crtc_h << 16);
136
137         return 0;
138 }
139
140 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
141 {
142         struct drm_plane *plane;
143         int ret;
144
145         exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
146
147         drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
148                 if (plane->crtc != crtc)
149                         continue;
150
151                 ret = plane->funcs->disable_plane(plane);
152                 if (ret)
153                         DRM_ERROR("Failed to disable plane %d\n", ret);
154         }
155 }
156
157 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
158         .dpms           = exynos_drm_crtc_dpms,
159         .prepare        = exynos_drm_crtc_prepare,
160         .commit         = exynos_drm_crtc_commit,
161         .mode_fixup     = exynos_drm_crtc_mode_fixup,
162         .mode_set       = exynos_drm_crtc_mode_set,
163         .mode_set_base  = exynos_drm_crtc_mode_set_base,
164         .disable        = exynos_drm_crtc_disable,
165 };
166
167 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
168                                      struct drm_framebuffer *fb,
169                                      struct drm_pending_vblank_event *event,
170                                      uint32_t page_flip_flags)
171 {
172         struct drm_device *dev = crtc->dev;
173         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
174         unsigned int crtc_w, crtc_h;
175         int ret;
176
177         /* when the page flip is requested, crtc's dpms should be on */
178         if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
179                 DRM_ERROR("failed page flip request.\n");
180                 return -EINVAL;
181         }
182
183         if (!event)
184                 return -EINVAL;
185
186         spin_lock_irq(&dev->event_lock);
187         if (exynos_crtc->event) {
188                 ret = -EBUSY;
189                 goto out;
190         }
191
192         ret = exynos_check_plane(crtc->primary, fb);
193         if (ret)
194                 goto out;
195
196         ret = drm_vblank_get(dev, exynos_crtc->pipe);
197         if (ret) {
198                 DRM_DEBUG("failed to acquire vblank counter\n");
199                 goto out;
200         }
201
202         exynos_crtc->event = event;
203         spin_unlock_irq(&dev->event_lock);
204
205         /*
206          * the pipe from user always is 0 so we can set pipe number
207          * of current owner to event.
208          */
209         event->pipe = exynos_crtc->pipe;
210
211         crtc->primary->fb = fb;
212         crtc_w = fb->width - crtc->x;
213         crtc_h = fb->height - crtc->y;
214         exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
215                             crtc_w, crtc_h, crtc->x << 16, crtc->y << 16,
216                             crtc_w << 16, crtc_h << 16);
217
218         return 0;
219
220 out:
221         spin_unlock_irq(&dev->event_lock);
222         return ret;
223 }
224
225 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
226 {
227         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
228         struct exynos_drm_private *private = crtc->dev->dev_private;
229
230         private->crtc[exynos_crtc->pipe] = NULL;
231
232         drm_crtc_cleanup(crtc);
233         kfree(exynos_crtc);
234 }
235
236 static struct drm_crtc_funcs exynos_crtc_funcs = {
237         .set_config     = drm_crtc_helper_set_config,
238         .page_flip      = exynos_drm_crtc_page_flip,
239         .destroy        = exynos_drm_crtc_destroy,
240 };
241
242 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
243                                         struct drm_plane *plane,
244                                         int pipe,
245                                         enum exynos_drm_output_type type,
246                                         const struct exynos_drm_crtc_ops *ops,
247                                         void *ctx)
248 {
249         struct exynos_drm_crtc *exynos_crtc;
250         struct exynos_drm_private *private = drm_dev->dev_private;
251         struct drm_crtc *crtc;
252         int ret;
253
254         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
255         if (!exynos_crtc)
256                 return ERR_PTR(-ENOMEM);
257
258         init_waitqueue_head(&exynos_crtc->pending_flip_queue);
259
260         exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
261         exynos_crtc->pipe = pipe;
262         exynos_crtc->type = type;
263         exynos_crtc->ops = ops;
264         exynos_crtc->ctx = ctx;
265
266         crtc = &exynos_crtc->base;
267
268         private->crtc[pipe] = crtc;
269
270         ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
271                                         &exynos_crtc_funcs);
272         if (ret < 0)
273                 goto err_crtc;
274
275         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
276
277         return exynos_crtc;
278
279 err_crtc:
280         plane->funcs->destroy(plane);
281         kfree(exynos_crtc);
282         return ERR_PTR(ret);
283 }
284
285 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
286 {
287         struct exynos_drm_private *private = dev->dev_private;
288         struct exynos_drm_crtc *exynos_crtc =
289                 to_exynos_crtc(private->crtc[pipe]);
290
291         if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
292                 return -EPERM;
293
294         if (exynos_crtc->ops->enable_vblank)
295                 exynos_crtc->ops->enable_vblank(exynos_crtc);
296
297         return 0;
298 }
299
300 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
301 {
302         struct exynos_drm_private *private = dev->dev_private;
303         struct exynos_drm_crtc *exynos_crtc =
304                 to_exynos_crtc(private->crtc[pipe]);
305
306         if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
307                 return;
308
309         if (exynos_crtc->ops->disable_vblank)
310                 exynos_crtc->ops->disable_vblank(exynos_crtc);
311 }
312
313 void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
314 {
315         struct exynos_drm_private *dev_priv = dev->dev_private;
316         struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
317         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
318         unsigned long flags;
319
320         spin_lock_irqsave(&dev->event_lock, flags);
321         if (exynos_crtc->event) {
322
323                 drm_send_vblank_event(dev, -1, exynos_crtc->event);
324                 drm_vblank_put(dev, pipe);
325                 wake_up(&exynos_crtc->pending_flip_queue);
326
327         }
328
329         exynos_crtc->event = NULL;
330         spin_unlock_irqrestore(&dev->event_lock, flags);
331 }
332
333 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
334 {
335         struct exynos_drm_crtc *exynos_crtc;
336         struct drm_device *dev = fb->dev;
337         struct drm_crtc *crtc;
338
339         /*
340          * make sure that overlay data are updated to real hardware
341          * for all encoders.
342          */
343         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
344                 exynos_crtc = to_exynos_crtc(crtc);
345
346                 /*
347                  * wait for vblank interrupt
348                  * - this makes sure that overlay data are updated to
349                  *      real hardware.
350                  */
351                 if (exynos_crtc->ops->wait_for_vblank)
352                         exynos_crtc->ops->wait_for_vblank(exynos_crtc);
353         }
354 }
355
356 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
357                                         unsigned int out_type)
358 {
359         struct drm_crtc *crtc;
360
361         list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
362                 struct exynos_drm_crtc *exynos_crtc;
363
364                 exynos_crtc = to_exynos_crtc(crtc);
365                 if (exynos_crtc->type == out_type)
366                         return exynos_crtc->pipe;
367         }
368
369         return -EPERM;
370 }
371
372 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
373 {
374         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
375
376         if (exynos_crtc->ops->te_handler)
377                 exynos_crtc->ops->te_handler(exynos_crtc);
378 }