drm/exynos: unify plane update on exynos_update_plane()
[firefly-linux-kernel-4.4.55.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         struct exynos_drm_manager *manager = exynos_crtc->manager;
27
28         DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
29
30         if (exynos_crtc->dpms == mode) {
31                 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
32                 return;
33         }
34
35         if (mode > DRM_MODE_DPMS_ON) {
36                 /* wait for the completion of page flip. */
37                 if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
38                                 !atomic_read(&exynos_crtc->pending_flip),
39                                 HZ/20))
40                         atomic_set(&exynos_crtc->pending_flip, 0);
41                 drm_crtc_vblank_off(crtc);
42         }
43
44         if (manager->ops->dpms)
45                 manager->ops->dpms(manager, mode);
46
47         exynos_crtc->dpms = mode;
48
49         if (mode == DRM_MODE_DPMS_ON)
50                 drm_crtc_vblank_on(crtc);
51 }
52
53 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
54 {
55         /* drm framework doesn't check NULL. */
56 }
57
58 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
59 {
60         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
61         struct exynos_drm_manager *manager = exynos_crtc->manager;
62         struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
63
64         exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
65
66         if (manager->ops->win_commit)
67                 manager->ops->win_commit(manager, exynos_plane->zpos);
68
69         if (manager->ops->commit)
70                 manager->ops->commit(manager);
71
72         exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_ON);
73 }
74
75 static bool
76 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
77                             const struct drm_display_mode *mode,
78                             struct drm_display_mode *adjusted_mode)
79 {
80         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
81         struct exynos_drm_manager *manager = exynos_crtc->manager;
82
83         if (manager->ops->mode_fixup)
84                 return manager->ops->mode_fixup(manager, mode, adjusted_mode);
85
86         return true;
87 }
88
89 static int
90 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
91                           struct drm_display_mode *adjusted_mode, int x, int y,
92                           struct drm_framebuffer *old_fb)
93 {
94         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
95         struct exynos_drm_manager *manager = exynos_crtc->manager;
96         struct drm_framebuffer *fb = crtc->primary->fb;
97         unsigned int crtc_w;
98         unsigned int crtc_h;
99
100         /*
101          * copy the mode data adjusted by mode_fixup() into crtc->mode
102          * so that hardware can be seet to proper mode.
103          */
104         memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
105
106         crtc_w = fb->width - x;
107         crtc_h = fb->height - y;
108
109         if (manager->ops->mode_set)
110                 manager->ops->mode_set(manager, &crtc->mode);
111
112         return exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
113                                      crtc_w, crtc_h, x, y, crtc_w, crtc_h);
114 }
115
116 static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
117                                           struct drm_framebuffer *old_fb)
118 {
119         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
120         struct drm_framebuffer *fb = crtc->primary->fb;
121         unsigned int crtc_w;
122         unsigned int crtc_h;
123
124         /* when framebuffer changing is requested, crtc's dpms should be on */
125         if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
126                 DRM_ERROR("failed framebuffer changing request.\n");
127                 return -EPERM;
128         }
129
130         crtc_w = fb->width - x;
131         crtc_h = fb->height - y;
132
133         return exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
134                                    crtc_w, crtc_h, x, y, crtc_w, crtc_h);
135 }
136
137 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
138                                           struct drm_framebuffer *old_fb)
139 {
140         return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
141 }
142
143 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
144 {
145         struct drm_plane *plane;
146         int ret;
147
148         exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
149
150         drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
151                 if (plane->crtc != crtc)
152                         continue;
153
154                 ret = plane->funcs->disable_plane(plane);
155                 if (ret)
156                         DRM_ERROR("Failed to disable plane %d\n", ret);
157         }
158 }
159
160 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
161         .dpms           = exynos_drm_crtc_dpms,
162         .prepare        = exynos_drm_crtc_prepare,
163         .commit         = exynos_drm_crtc_commit,
164         .mode_fixup     = exynos_drm_crtc_mode_fixup,
165         .mode_set       = exynos_drm_crtc_mode_set,
166         .mode_set_base  = exynos_drm_crtc_mode_set_base,
167         .disable        = exynos_drm_crtc_disable,
168 };
169
170 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
171                                      struct drm_framebuffer *fb,
172                                      struct drm_pending_vblank_event *event,
173                                      uint32_t page_flip_flags)
174 {
175         struct drm_device *dev = crtc->dev;
176         struct exynos_drm_private *dev_priv = dev->dev_private;
177         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
178         struct drm_framebuffer *old_fb = crtc->primary->fb;
179         int ret = -EINVAL;
180
181         /* when the page flip is requested, crtc's dpms should be on */
182         if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
183                 DRM_ERROR("failed page flip request.\n");
184                 return -EINVAL;
185         }
186
187         mutex_lock(&dev->struct_mutex);
188
189         if (event) {
190                 /*
191                  * the pipe from user always is 0 so we can set pipe number
192                  * of current owner to event.
193                  */
194                 event->pipe = exynos_crtc->pipe;
195
196                 ret = drm_vblank_get(dev, exynos_crtc->pipe);
197                 if (ret) {
198                         DRM_DEBUG("failed to acquire vblank counter\n");
199
200                         goto out;
201                 }
202
203                 spin_lock_irq(&dev->event_lock);
204                 list_add_tail(&event->base.link,
205                                 &dev_priv->pageflip_event_list);
206                 atomic_set(&exynos_crtc->pending_flip, 1);
207                 spin_unlock_irq(&dev->event_lock);
208
209                 crtc->primary->fb = fb;
210                 ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
211                                                     NULL);
212                 if (ret) {
213                         crtc->primary->fb = old_fb;
214
215                         spin_lock_irq(&dev->event_lock);
216                         drm_vblank_put(dev, exynos_crtc->pipe);
217                         list_del(&event->base.link);
218                         atomic_set(&exynos_crtc->pending_flip, 0);
219                         spin_unlock_irq(&dev->event_lock);
220
221                         goto out;
222                 }
223         }
224 out:
225         mutex_unlock(&dev->struct_mutex);
226         return ret;
227 }
228
229 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
230 {
231         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
232         struct exynos_drm_private *private = crtc->dev->dev_private;
233
234         private->crtc[exynos_crtc->pipe] = NULL;
235
236         drm_crtc_cleanup(crtc);
237         kfree(exynos_crtc);
238 }
239
240 static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
241                                         struct drm_property *property,
242                                         uint64_t val)
243 {
244         struct drm_device *dev = crtc->dev;
245         struct exynos_drm_private *dev_priv = dev->dev_private;
246         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
247
248         if (property == dev_priv->crtc_mode_property) {
249                 enum exynos_crtc_mode mode = val;
250
251                 if (mode == exynos_crtc->mode)
252                         return 0;
253
254                 exynos_crtc->mode = mode;
255
256                 switch (mode) {
257                 case CRTC_MODE_NORMAL:
258                         exynos_drm_crtc_commit(crtc);
259                         break;
260                 case CRTC_MODE_BLANK:
261                         exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_OFF);
262                         break;
263                 default:
264                         break;
265                 }
266
267                 return 0;
268         }
269
270         return -EINVAL;
271 }
272
273 static struct drm_crtc_funcs exynos_crtc_funcs = {
274         .set_config     = drm_crtc_helper_set_config,
275         .page_flip      = exynos_drm_crtc_page_flip,
276         .destroy        = exynos_drm_crtc_destroy,
277         .set_property   = exynos_drm_crtc_set_property,
278 };
279
280 static const struct drm_prop_enum_list mode_names[] = {
281         { CRTC_MODE_NORMAL, "normal" },
282         { CRTC_MODE_BLANK, "blank" },
283 };
284
285 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
286 {
287         struct drm_device *dev = crtc->dev;
288         struct exynos_drm_private *dev_priv = dev->dev_private;
289         struct drm_property *prop;
290
291         prop = dev_priv->crtc_mode_property;
292         if (!prop) {
293                 prop = drm_property_create_enum(dev, 0, "mode", mode_names,
294                                                 ARRAY_SIZE(mode_names));
295                 if (!prop)
296                         return;
297
298                 dev_priv->crtc_mode_property = prop;
299         }
300
301         drm_object_attach_property(&crtc->base, prop, 0);
302 }
303
304 int exynos_drm_crtc_create(struct exynos_drm_manager *manager)
305 {
306         struct exynos_drm_crtc *exynos_crtc;
307         struct drm_plane *plane;
308         struct exynos_drm_private *private = manager->drm_dev->dev_private;
309         struct drm_crtc *crtc;
310         int ret;
311
312         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
313         if (!exynos_crtc)
314                 return -ENOMEM;
315
316         init_waitqueue_head(&exynos_crtc->pending_flip_queue);
317         atomic_set(&exynos_crtc->pending_flip, 0);
318
319         exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
320         exynos_crtc->manager = manager;
321         exynos_crtc->pipe = manager->pipe;
322         plane = exynos_plane_init(manager->drm_dev, 1 << manager->pipe,
323                                   DRM_PLANE_TYPE_PRIMARY);
324         if (IS_ERR(plane)) {
325                 ret = PTR_ERR(plane);
326                 goto err_plane;
327         }
328
329         manager->crtc = &exynos_crtc->drm_crtc;
330         crtc = &exynos_crtc->drm_crtc;
331
332         private->crtc[manager->pipe] = crtc;
333
334         ret = drm_crtc_init_with_planes(manager->drm_dev, crtc, plane, NULL,
335                                         &exynos_crtc_funcs);
336         if (ret < 0)
337                 goto err_crtc;
338
339         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
340
341         exynos_drm_crtc_attach_mode_property(crtc);
342
343         return 0;
344
345 err_crtc:
346         plane->funcs->destroy(plane);
347 err_plane:
348         kfree(exynos_crtc);
349         return ret;
350 }
351
352 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
353 {
354         struct exynos_drm_private *private = dev->dev_private;
355         struct exynos_drm_crtc *exynos_crtc =
356                 to_exynos_crtc(private->crtc[pipe]);
357         struct exynos_drm_manager *manager = exynos_crtc->manager;
358
359         if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
360                 return -EPERM;
361
362         if (manager->ops->enable_vblank)
363                 manager->ops->enable_vblank(manager);
364
365         return 0;
366 }
367
368 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
369 {
370         struct exynos_drm_private *private = dev->dev_private;
371         struct exynos_drm_crtc *exynos_crtc =
372                 to_exynos_crtc(private->crtc[pipe]);
373         struct exynos_drm_manager *manager = exynos_crtc->manager;
374
375         if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
376                 return;
377
378         if (manager->ops->disable_vblank)
379                 manager->ops->disable_vblank(manager);
380 }
381
382 void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
383 {
384         struct exynos_drm_private *dev_priv = dev->dev_private;
385         struct drm_pending_vblank_event *e, *t;
386         struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
387         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
388         unsigned long flags;
389
390         spin_lock_irqsave(&dev->event_lock, flags);
391
392         list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
393                         base.link) {
394                 /* if event's pipe isn't same as crtc then ignore it. */
395                 if (pipe != e->pipe)
396                         continue;
397
398                 list_del(&e->base.link);
399                 drm_send_vblank_event(dev, -1, e);
400                 drm_vblank_put(dev, pipe);
401                 atomic_set(&exynos_crtc->pending_flip, 0);
402                 wake_up(&exynos_crtc->pending_flip_queue);
403         }
404
405         spin_unlock_irqrestore(&dev->event_lock, flags);
406 }
407
408 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
409 {
410         struct exynos_drm_manager *manager;
411         struct drm_device *dev = fb->dev;
412         struct drm_crtc *crtc;
413
414         /*
415          * make sure that overlay data are updated to real hardware
416          * for all encoders.
417          */
418         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
419                 manager = to_exynos_crtc(crtc)->manager;
420
421                 /*
422                  * wait for vblank interrupt
423                  * - this makes sure that overlay data are updated to
424                  *      real hardware.
425                  */
426                 if (manager->ops->wait_for_vblank)
427                         manager->ops->wait_for_vblank(manager);
428         }
429 }
430
431 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
432                                         unsigned int out_type)
433 {
434         struct drm_crtc *crtc;
435
436         list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
437                 struct exynos_drm_crtc *exynos_crtc;
438
439                 exynos_crtc = to_exynos_crtc(crtc);
440                 if (exynos_crtc->manager->type == out_type)
441                         return exynos_crtc->manager->pipe;
442         }
443
444         return -EPERM;
445 }
446
447 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
448 {
449         struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
450
451         if (manager->ops->te_handler)
452                 manager->ops->te_handler(manager);
453 }