drm/exynos: remove drm_dev from struct exynos_drm_manager
[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_base(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 void exynos_drm_crtc_disable(struct drm_crtc *crtc)
138 {
139         struct drm_plane *plane;
140         int ret;
141
142         exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
143
144         drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
145                 if (plane->crtc != crtc)
146                         continue;
147
148                 ret = plane->funcs->disable_plane(plane);
149                 if (ret)
150                         DRM_ERROR("Failed to disable plane %d\n", ret);
151         }
152 }
153
154 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
155         .dpms           = exynos_drm_crtc_dpms,
156         .prepare        = exynos_drm_crtc_prepare,
157         .commit         = exynos_drm_crtc_commit,
158         .mode_fixup     = exynos_drm_crtc_mode_fixup,
159         .mode_set       = exynos_drm_crtc_mode_set,
160         .mode_set_base  = exynos_drm_crtc_mode_set_base,
161         .disable        = exynos_drm_crtc_disable,
162 };
163
164 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
165                                      struct drm_framebuffer *fb,
166                                      struct drm_pending_vblank_event *event,
167                                      uint32_t page_flip_flags)
168 {
169         struct drm_device *dev = crtc->dev;
170         struct exynos_drm_private *dev_priv = dev->dev_private;
171         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
172         struct drm_framebuffer *old_fb = crtc->primary->fb;
173         unsigned int crtc_w, crtc_h;
174         int ret = -EINVAL;
175
176         /* when the page flip is requested, crtc's dpms should be on */
177         if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
178                 DRM_ERROR("failed page flip request.\n");
179                 return -EINVAL;
180         }
181
182         mutex_lock(&dev->struct_mutex);
183
184         if (event) {
185                 /*
186                  * the pipe from user always is 0 so we can set pipe number
187                  * of current owner to event.
188                  */
189                 event->pipe = exynos_crtc->pipe;
190
191                 ret = drm_vblank_get(dev, exynos_crtc->pipe);
192                 if (ret) {
193                         DRM_DEBUG("failed to acquire vblank counter\n");
194
195                         goto out;
196                 }
197
198                 spin_lock_irq(&dev->event_lock);
199                 list_add_tail(&event->base.link,
200                                 &dev_priv->pageflip_event_list);
201                 atomic_set(&exynos_crtc->pending_flip, 1);
202                 spin_unlock_irq(&dev->event_lock);
203
204                 crtc->primary->fb = fb;
205                 crtc_w = fb->width - crtc->x;
206                 crtc_h = fb->height - crtc->y;
207                 ret = exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
208                                           crtc_w, crtc_h, crtc->x, crtc->y,
209                                           crtc_w, crtc_h);
210                 if (ret) {
211                         crtc->primary->fb = old_fb;
212
213                         spin_lock_irq(&dev->event_lock);
214                         drm_vblank_put(dev, exynos_crtc->pipe);
215                         list_del(&event->base.link);
216                         atomic_set(&exynos_crtc->pending_flip, 0);
217                         spin_unlock_irq(&dev->event_lock);
218
219                         goto out;
220                 }
221         }
222 out:
223         mutex_unlock(&dev->struct_mutex);
224         return ret;
225 }
226
227 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
228 {
229         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
230         struct exynos_drm_private *private = crtc->dev->dev_private;
231
232         private->crtc[exynos_crtc->pipe] = NULL;
233
234         drm_crtc_cleanup(crtc);
235         kfree(exynos_crtc);
236 }
237
238 static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
239                                         struct drm_property *property,
240                                         uint64_t val)
241 {
242         struct drm_device *dev = crtc->dev;
243         struct exynos_drm_private *dev_priv = dev->dev_private;
244         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
245
246         if (property == dev_priv->crtc_mode_property) {
247                 enum exynos_crtc_mode mode = val;
248
249                 if (mode == exynos_crtc->mode)
250                         return 0;
251
252                 exynos_crtc->mode = mode;
253
254                 switch (mode) {
255                 case CRTC_MODE_NORMAL:
256                         exynos_drm_crtc_commit(crtc);
257                         break;
258                 case CRTC_MODE_BLANK:
259                         exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_OFF);
260                         break;
261                 default:
262                         break;
263                 }
264
265                 return 0;
266         }
267
268         return -EINVAL;
269 }
270
271 static struct drm_crtc_funcs exynos_crtc_funcs = {
272         .set_config     = drm_crtc_helper_set_config,
273         .page_flip      = exynos_drm_crtc_page_flip,
274         .destroy        = exynos_drm_crtc_destroy,
275         .set_property   = exynos_drm_crtc_set_property,
276 };
277
278 static const struct drm_prop_enum_list mode_names[] = {
279         { CRTC_MODE_NORMAL, "normal" },
280         { CRTC_MODE_BLANK, "blank" },
281 };
282
283 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
284 {
285         struct drm_device *dev = crtc->dev;
286         struct exynos_drm_private *dev_priv = dev->dev_private;
287         struct drm_property *prop;
288
289         prop = dev_priv->crtc_mode_property;
290         if (!prop) {
291                 prop = drm_property_create_enum(dev, 0, "mode", mode_names,
292                                                 ARRAY_SIZE(mode_names));
293                 if (!prop)
294                         return;
295
296                 dev_priv->crtc_mode_property = prop;
297         }
298
299         drm_object_attach_property(&crtc->base, prop, 0);
300 }
301
302 int exynos_drm_crtc_create(struct exynos_drm_manager *manager,
303                            struct drm_device *drm_dev, int pipe,
304                            enum exynos_drm_output_type type)
305 {
306         struct exynos_drm_crtc *exynos_crtc;
307         struct drm_plane *plane;
308         struct exynos_drm_private *private = 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 = pipe;
322         exynos_crtc->type = type;
323         plane = exynos_plane_init(drm_dev, 1 << pipe,
324                                   DRM_PLANE_TYPE_PRIMARY);
325         if (IS_ERR(plane)) {
326                 ret = PTR_ERR(plane);
327                 goto err_plane;
328         }
329
330         manager->crtc = &exynos_crtc->base;
331         crtc = &exynos_crtc->base;
332
333         private->crtc[pipe] = crtc;
334
335         ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
336                                         &exynos_crtc_funcs);
337         if (ret < 0)
338                 goto err_crtc;
339
340         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
341
342         exynos_drm_crtc_attach_mode_property(crtc);
343
344         return 0;
345
346 err_crtc:
347         plane->funcs->destroy(plane);
348 err_plane:
349         kfree(exynos_crtc);
350         return ret;
351 }
352
353 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
354 {
355         struct exynos_drm_private *private = dev->dev_private;
356         struct exynos_drm_crtc *exynos_crtc =
357                 to_exynos_crtc(private->crtc[pipe]);
358         struct exynos_drm_manager *manager = exynos_crtc->manager;
359
360         if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
361                 return -EPERM;
362
363         if (manager->ops->enable_vblank)
364                 manager->ops->enable_vblank(manager);
365
366         return 0;
367 }
368
369 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
370 {
371         struct exynos_drm_private *private = dev->dev_private;
372         struct exynos_drm_crtc *exynos_crtc =
373                 to_exynos_crtc(private->crtc[pipe]);
374         struct exynos_drm_manager *manager = exynos_crtc->manager;
375
376         if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
377                 return;
378
379         if (manager->ops->disable_vblank)
380                 manager->ops->disable_vblank(manager);
381 }
382
383 void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
384 {
385         struct exynos_drm_private *dev_priv = dev->dev_private;
386         struct drm_pending_vblank_event *e, *t;
387         struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
388         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
389         unsigned long flags;
390
391         spin_lock_irqsave(&dev->event_lock, flags);
392
393         list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
394                         base.link) {
395                 /* if event's pipe isn't same as crtc then ignore it. */
396                 if (pipe != e->pipe)
397                         continue;
398
399                 list_del(&e->base.link);
400                 drm_send_vblank_event(dev, -1, e);
401                 drm_vblank_put(dev, pipe);
402                 atomic_set(&exynos_crtc->pending_flip, 0);
403                 wake_up(&exynos_crtc->pending_flip_queue);
404         }
405
406         spin_unlock_irqrestore(&dev->event_lock, flags);
407 }
408
409 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
410 {
411         struct exynos_drm_manager *manager;
412         struct drm_device *dev = fb->dev;
413         struct drm_crtc *crtc;
414
415         /*
416          * make sure that overlay data are updated to real hardware
417          * for all encoders.
418          */
419         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
420                 manager = to_exynos_crtc(crtc)->manager;
421
422                 /*
423                  * wait for vblank interrupt
424                  * - this makes sure that overlay data are updated to
425                  *      real hardware.
426                  */
427                 if (manager->ops->wait_for_vblank)
428                         manager->ops->wait_for_vblank(manager);
429         }
430 }
431
432 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
433                                         unsigned int out_type)
434 {
435         struct drm_crtc *crtc;
436
437         list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
438                 struct exynos_drm_crtc *exynos_crtc;
439
440                 exynos_crtc = to_exynos_crtc(crtc);
441                 if (exynos_crtc->type == out_type)
442                         return exynos_crtc->pipe;
443         }
444
445         return -EPERM;
446 }
447
448 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
449 {
450         struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
451
452         if (manager->ops->te_handler)
453                 manager->ops->te_handler(manager);
454 }