GNU Linux-libre 4.19.304-gnu1
[releases.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 #include <drm/drm_encoder.h>
20
21 #include "exynos_drm_crtc.h"
22 #include "exynos_drm_drv.h"
23 #include "exynos_drm_plane.h"
24
25 static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc,
26                                           struct drm_crtc_state *old_state)
27 {
28         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
29
30         if (exynos_crtc->ops->enable)
31                 exynos_crtc->ops->enable(exynos_crtc);
32
33         drm_crtc_vblank_on(crtc);
34 }
35
36 static void exynos_drm_crtc_atomic_disable(struct drm_crtc *crtc,
37                                            struct drm_crtc_state *old_state)
38 {
39         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
40
41         drm_crtc_vblank_off(crtc);
42
43         if (exynos_crtc->ops->disable)
44                 exynos_crtc->ops->disable(exynos_crtc);
45
46         spin_lock_irq(&crtc->dev->event_lock);
47         if (crtc->state->event && !crtc->state->active) {
48                 drm_crtc_send_vblank_event(crtc, crtc->state->event);
49                 crtc->state->event = NULL;
50         }
51         spin_unlock_irq(&crtc->dev->event_lock);
52 }
53
54 static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
55                                      struct drm_crtc_state *state)
56 {
57         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
58
59         if (!state->enable)
60                 return 0;
61
62         if (exynos_crtc->ops->atomic_check)
63                 return exynos_crtc->ops->atomic_check(exynos_crtc, state);
64
65         return 0;
66 }
67
68 static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
69                                      struct drm_crtc_state *old_crtc_state)
70 {
71         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
72
73         if (exynos_crtc->ops->atomic_begin)
74                 exynos_crtc->ops->atomic_begin(exynos_crtc);
75 }
76
77 static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
78                                      struct drm_crtc_state *old_crtc_state)
79 {
80         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
81
82         if (exynos_crtc->ops->atomic_flush)
83                 exynos_crtc->ops->atomic_flush(exynos_crtc);
84 }
85
86 static enum drm_mode_status exynos_crtc_mode_valid(struct drm_crtc *crtc,
87         const struct drm_display_mode *mode)
88 {
89         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
90
91         if (exynos_crtc->ops->mode_valid)
92                 return exynos_crtc->ops->mode_valid(exynos_crtc, mode);
93
94         return MODE_OK;
95 }
96
97 static bool exynos_crtc_mode_fixup(struct drm_crtc *crtc,
98                 const struct drm_display_mode *mode,
99                 struct drm_display_mode *adjusted_mode)
100 {
101         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
102
103         if (exynos_crtc->ops->mode_fixup)
104                 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
105                                 adjusted_mode);
106
107         return true;
108 }
109
110
111 static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
112         .mode_valid     = exynos_crtc_mode_valid,
113         .mode_fixup     = exynos_crtc_mode_fixup,
114         .atomic_check   = exynos_crtc_atomic_check,
115         .atomic_begin   = exynos_crtc_atomic_begin,
116         .atomic_flush   = exynos_crtc_atomic_flush,
117         .atomic_enable  = exynos_drm_crtc_atomic_enable,
118         .atomic_disable = exynos_drm_crtc_atomic_disable,
119 };
120
121 void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
122 {
123         struct drm_crtc *crtc = &exynos_crtc->base;
124         struct drm_pending_vblank_event *event = crtc->state->event;
125         unsigned long flags;
126
127         if (!event)
128                 return;
129         crtc->state->event = NULL;
130
131         WARN_ON(drm_crtc_vblank_get(crtc) != 0);
132
133         spin_lock_irqsave(&crtc->dev->event_lock, flags);
134         drm_crtc_arm_vblank_event(crtc, event);
135         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
136 }
137
138 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
139 {
140         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
141
142         drm_crtc_cleanup(crtc);
143         kfree(exynos_crtc);
144 }
145
146 static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
147 {
148         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
149
150         if (exynos_crtc->ops->enable_vblank)
151                 return exynos_crtc->ops->enable_vblank(exynos_crtc);
152
153         return 0;
154 }
155
156 static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
157 {
158         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
159
160         if (exynos_crtc->ops->disable_vblank)
161                 exynos_crtc->ops->disable_vblank(exynos_crtc);
162 }
163
164 static const struct drm_crtc_funcs exynos_crtc_funcs = {
165         .set_config     = drm_atomic_helper_set_config,
166         .page_flip      = drm_atomic_helper_page_flip,
167         .destroy        = exynos_drm_crtc_destroy,
168         .reset = drm_atomic_helper_crtc_reset,
169         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
170         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
171         .enable_vblank = exynos_drm_crtc_enable_vblank,
172         .disable_vblank = exynos_drm_crtc_disable_vblank,
173 };
174
175 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
176                                         struct drm_plane *plane,
177                                         enum exynos_drm_output_type type,
178                                         const struct exynos_drm_crtc_ops *ops,
179                                         void *ctx)
180 {
181         struct exynos_drm_crtc *exynos_crtc;
182         struct drm_crtc *crtc;
183         int ret;
184
185         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
186         if (!exynos_crtc)
187                 return ERR_PTR(-ENOMEM);
188
189         exynos_crtc->type = type;
190         exynos_crtc->ops = ops;
191         exynos_crtc->ctx = ctx;
192
193         crtc = &exynos_crtc->base;
194
195         ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
196                                         &exynos_crtc_funcs, NULL);
197         if (ret < 0)
198                 goto err_crtc;
199
200         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
201
202         return exynos_crtc;
203
204 err_crtc:
205         plane->funcs->destroy(plane);
206         kfree(exynos_crtc);
207         return ERR_PTR(ret);
208 }
209
210 struct exynos_drm_crtc *exynos_drm_crtc_get_by_type(struct drm_device *drm_dev,
211                                        enum exynos_drm_output_type out_type)
212 {
213         struct drm_crtc *crtc;
214
215         drm_for_each_crtc(crtc, drm_dev)
216                 if (to_exynos_crtc(crtc)->type == out_type)
217                         return to_exynos_crtc(crtc);
218
219         return ERR_PTR(-ENODEV);
220 }
221
222 int exynos_drm_set_possible_crtcs(struct drm_encoder *encoder,
223                 enum exynos_drm_output_type out_type)
224 {
225         struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(encoder->dev,
226                                                 out_type);
227
228         if (IS_ERR(crtc))
229                 return PTR_ERR(crtc);
230
231         encoder->possible_crtcs = drm_crtc_mask(&crtc->base);
232
233         return 0;
234 }
235
236 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
237 {
238         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
239
240         if (exynos_crtc->ops->te_handler)
241                 exynos_crtc->ops->te_handler(exynos_crtc);
242 }