GNU Linux-libre 4.14.303-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         if (crtc->state->event && !crtc->state->active) {
47                 spin_lock_irq(&crtc->dev->event_lock);
48                 drm_crtc_send_vblank_event(crtc, crtc->state->event);
49                 spin_unlock_irq(&crtc->dev->event_lock);
50
51                 crtc->state->event = NULL;
52         }
53 }
54
55 static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
56                                      struct drm_crtc_state *state)
57 {
58         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
59
60         if (!state->enable)
61                 return 0;
62
63         if (exynos_crtc->ops->atomic_check)
64                 return exynos_crtc->ops->atomic_check(exynos_crtc, state);
65
66         return 0;
67 }
68
69 static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
70                                      struct drm_crtc_state *old_crtc_state)
71 {
72         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
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 enum drm_mode_status exynos_crtc_mode_valid(struct drm_crtc *crtc,
88         const struct drm_display_mode *mode)
89 {
90         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
91
92         if (exynos_crtc->ops->mode_valid)
93                 return exynos_crtc->ops->mode_valid(exynos_crtc, mode);
94
95         return MODE_OK;
96 }
97
98 static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
99         .mode_valid     = exynos_crtc_mode_valid,
100         .atomic_check   = exynos_crtc_atomic_check,
101         .atomic_begin   = exynos_crtc_atomic_begin,
102         .atomic_flush   = exynos_crtc_atomic_flush,
103         .atomic_enable  = exynos_drm_crtc_atomic_enable,
104         .atomic_disable = exynos_drm_crtc_atomic_disable,
105 };
106
107 void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
108 {
109         struct drm_crtc *crtc = &exynos_crtc->base;
110         struct drm_pending_vblank_event *event = crtc->state->event;
111         unsigned long flags;
112
113         if (!event)
114                 return;
115         crtc->state->event = NULL;
116
117         WARN_ON(drm_crtc_vblank_get(crtc) != 0);
118
119         spin_lock_irqsave(&crtc->dev->event_lock, flags);
120         drm_crtc_arm_vblank_event(crtc, event);
121         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
122 }
123
124 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
125 {
126         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
127
128         drm_crtc_cleanup(crtc);
129         kfree(exynos_crtc);
130 }
131
132 static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
133 {
134         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
135
136         if (exynos_crtc->ops->enable_vblank)
137                 return exynos_crtc->ops->enable_vblank(exynos_crtc);
138
139         return 0;
140 }
141
142 static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
143 {
144         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
145
146         if (exynos_crtc->ops->disable_vblank)
147                 exynos_crtc->ops->disable_vblank(exynos_crtc);
148 }
149
150 static const struct drm_crtc_funcs exynos_crtc_funcs = {
151         .set_config     = drm_atomic_helper_set_config,
152         .page_flip      = drm_atomic_helper_page_flip,
153         .destroy        = exynos_drm_crtc_destroy,
154         .reset = drm_atomic_helper_crtc_reset,
155         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
156         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
157         .enable_vblank = exynos_drm_crtc_enable_vblank,
158         .disable_vblank = exynos_drm_crtc_disable_vblank,
159 };
160
161 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
162                                         struct drm_plane *plane,
163                                         enum exynos_drm_output_type type,
164                                         const struct exynos_drm_crtc_ops *ops,
165                                         void *ctx)
166 {
167         struct exynos_drm_crtc *exynos_crtc;
168         struct drm_crtc *crtc;
169         int ret;
170
171         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
172         if (!exynos_crtc)
173                 return ERR_PTR(-ENOMEM);
174
175         exynos_crtc->type = type;
176         exynos_crtc->ops = ops;
177         exynos_crtc->ctx = ctx;
178
179         crtc = &exynos_crtc->base;
180
181         ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
182                                         &exynos_crtc_funcs, NULL);
183         if (ret < 0)
184                 goto err_crtc;
185
186         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
187
188         return exynos_crtc;
189
190 err_crtc:
191         plane->funcs->destroy(plane);
192         kfree(exynos_crtc);
193         return ERR_PTR(ret);
194 }
195
196 struct exynos_drm_crtc *exynos_drm_crtc_get_by_type(struct drm_device *drm_dev,
197                                        enum exynos_drm_output_type out_type)
198 {
199         struct drm_crtc *crtc;
200
201         drm_for_each_crtc(crtc, drm_dev)
202                 if (to_exynos_crtc(crtc)->type == out_type)
203                         return to_exynos_crtc(crtc);
204
205         return ERR_PTR(-EPERM);
206 }
207
208 int exynos_drm_set_possible_crtcs(struct drm_encoder *encoder,
209                 enum exynos_drm_output_type out_type)
210 {
211         struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(encoder->dev,
212                                                 out_type);
213
214         if (IS_ERR(crtc))
215                 return PTR_ERR(crtc);
216
217         encoder->possible_crtcs = drm_crtc_mask(&crtc->base);
218
219         return 0;
220 }
221
222 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
223 {
224         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
225
226         if (exynos_crtc->ops->te_handler)
227                 exynos_crtc->ops->te_handler(exynos_crtc);
228 }