]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/exynos/exynos_drm_crtc.c
drm/exynos: fix pending update handling
[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 #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 (!state->enable)
59                 return 0;
60
61         if (exynos_crtc->ops->atomic_check)
62                 return exynos_crtc->ops->atomic_check(exynos_crtc, state);
63
64         return 0;
65 }
66
67 static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
68                                      struct drm_crtc_state *old_crtc_state)
69 {
70         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
71
72         exynos_crtc->event = crtc->state->event;
73
74         if (exynos_crtc->ops->atomic_begin)
75                 exynos_crtc->ops->atomic_begin(exynos_crtc);
76 }
77
78 static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
79                                      struct drm_crtc_state *old_crtc_state)
80 {
81         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
82
83         if (exynos_crtc->ops->atomic_flush)
84                 exynos_crtc->ops->atomic_flush(exynos_crtc);
85 }
86
87 static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
88         .enable         = exynos_drm_crtc_enable,
89         .disable        = exynos_drm_crtc_disable,
90         .mode_set_nofb  = exynos_drm_crtc_mode_set_nofb,
91         .atomic_check   = exynos_crtc_atomic_check,
92         .atomic_begin   = exynos_crtc_atomic_begin,
93         .atomic_flush   = exynos_crtc_atomic_flush,
94 };
95
96 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
97 {
98         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
99         struct exynos_drm_private *private = crtc->dev->dev_private;
100
101         private->crtc[exynos_crtc->pipe] = NULL;
102
103         drm_crtc_cleanup(crtc);
104         kfree(exynos_crtc);
105 }
106
107 static const struct drm_crtc_funcs exynos_crtc_funcs = {
108         .set_config     = drm_atomic_helper_set_config,
109         .page_flip      = drm_atomic_helper_page_flip,
110         .destroy        = exynos_drm_crtc_destroy,
111         .reset = drm_atomic_helper_crtc_reset,
112         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
113         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
114 };
115
116 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
117                                         struct drm_plane *plane,
118                                         int pipe,
119                                         enum exynos_drm_output_type type,
120                                         const struct exynos_drm_crtc_ops *ops,
121                                         void *ctx)
122 {
123         struct exynos_drm_crtc *exynos_crtc;
124         struct exynos_drm_private *private = drm_dev->dev_private;
125         struct drm_crtc *crtc;
126         int ret;
127
128         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
129         if (!exynos_crtc)
130                 return ERR_PTR(-ENOMEM);
131
132         exynos_crtc->pipe = pipe;
133         exynos_crtc->type = type;
134         exynos_crtc->ops = ops;
135         exynos_crtc->ctx = ctx;
136
137         crtc = &exynos_crtc->base;
138
139         private->crtc[pipe] = crtc;
140
141         ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
142                                         &exynos_crtc_funcs, NULL);
143         if (ret < 0)
144                 goto err_crtc;
145
146         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
147
148         return exynos_crtc;
149
150 err_crtc:
151         plane->funcs->destroy(plane);
152         kfree(exynos_crtc);
153         return ERR_PTR(ret);
154 }
155
156 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
157 {
158         struct exynos_drm_crtc *exynos_crtc = exynos_drm_crtc_from_pipe(dev,
159                                                                         pipe);
160
161         if (exynos_crtc->ops->enable_vblank)
162                 return exynos_crtc->ops->enable_vblank(exynos_crtc);
163
164         return 0;
165 }
166
167 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, unsigned int pipe)
168 {
169         struct exynos_drm_crtc *exynos_crtc = exynos_drm_crtc_from_pipe(dev,
170                                                                         pipe);
171
172         if (exynos_crtc->ops->disable_vblank)
173                 exynos_crtc->ops->disable_vblank(exynos_crtc);
174 }
175
176 void exynos_drm_crtc_finish_update(struct exynos_drm_crtc *exynos_crtc,
177                                 struct exynos_drm_plane *exynos_plane)
178 {
179         struct drm_crtc *crtc = &exynos_crtc->base;
180         unsigned long flags;
181
182         exynos_plane->pending_fb = NULL;
183
184         spin_lock_irqsave(&crtc->dev->event_lock, flags);
185         if (exynos_crtc->event)
186                 drm_crtc_send_vblank_event(crtc, exynos_crtc->event);
187
188         exynos_crtc->event = NULL;
189         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
190 }
191
192 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
193                                        enum exynos_drm_output_type out_type)
194 {
195         struct drm_crtc *crtc;
196
197         list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
198                 struct exynos_drm_crtc *exynos_crtc;
199
200                 exynos_crtc = to_exynos_crtc(crtc);
201                 if (exynos_crtc->type == out_type)
202                         return exynos_crtc->pipe;
203         }
204
205         return -EPERM;
206 }
207
208 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
209 {
210         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
211
212         if (exynos_crtc->ops->te_handler)
213                 exynos_crtc->ops->te_handler(exynos_crtc);
214 }
215
216 void exynos_drm_crtc_cancel_page_flip(struct drm_crtc *crtc,
217                                         struct drm_file *file)
218 {
219         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
220         struct drm_pending_vblank_event *e;
221         unsigned long flags;
222
223         spin_lock_irqsave(&crtc->dev->event_lock, flags);
224
225         e = exynos_crtc->event;
226         if (e && e->base.file_priv == file)
227                 exynos_crtc->event = NULL;
228
229         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
230
231         if (e && e->base.file_priv == file)
232                 drm_event_cancel_free(crtc->dev, &e->base);
233 }