Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
[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  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include <drm/drmP.h>
30 #include <drm/drm_crtc_helper.h>
31
32 #include "exynos_drm_drv.h"
33 #include "exynos_drm_encoder.h"
34 #include "exynos_drm_plane.h"
35
36 #define to_exynos_crtc(x)       container_of(x, struct exynos_drm_crtc,\
37                                 drm_crtc)
38
39 enum exynos_crtc_mode {
40         CRTC_MODE_NORMAL,       /* normal mode */
41         CRTC_MODE_BLANK,        /* The private plane of crtc is blank */
42 };
43
44 /*
45  * Exynos specific crtc structure.
46  *
47  * @drm_crtc: crtc object.
48  * @drm_plane: pointer of private plane object for this crtc
49  * @pipe: a crtc index created at load() with a new crtc object creation
50  *      and the crtc object would be set to private->crtc array
51  *      to get a crtc object corresponding to this pipe from private->crtc
52  *      array when irq interrupt occured. the reason of using this pipe is that
53  *      drm framework doesn't support multiple irq yet.
54  *      we can refer to the crtc to current hardware interrupt occured through
55  *      this pipe value.
56  * @dpms: store the crtc dpms value
57  * @mode: store the crtc mode value
58  */
59 struct exynos_drm_crtc {
60         struct drm_crtc                 drm_crtc;
61         struct drm_plane                *plane;
62         unsigned int                    pipe;
63         unsigned int                    dpms;
64         enum exynos_crtc_mode           mode;
65 };
66
67 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
68 {
69         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
70
71         DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
72
73         if (exynos_crtc->dpms == mode) {
74                 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
75                 return;
76         }
77
78         exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
79         exynos_crtc->dpms = mode;
80 }
81
82 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
83 {
84         DRM_DEBUG_KMS("%s\n", __FILE__);
85
86         /* drm framework doesn't check NULL. */
87 }
88
89 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
90 {
91         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
92
93         DRM_DEBUG_KMS("%s\n", __FILE__);
94
95         exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
96         exynos_plane_commit(exynos_crtc->plane);
97         exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
98 }
99
100 static bool
101 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
102                             const struct drm_display_mode *mode,
103                             struct drm_display_mode *adjusted_mode)
104 {
105         DRM_DEBUG_KMS("%s\n", __FILE__);
106
107         /* drm framework doesn't check NULL */
108         return true;
109 }
110
111 static int
112 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
113                           struct drm_display_mode *adjusted_mode, int x, int y,
114                           struct drm_framebuffer *old_fb)
115 {
116         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
117         struct drm_plane *plane = exynos_crtc->plane;
118         unsigned int crtc_w;
119         unsigned int crtc_h;
120         int pipe = exynos_crtc->pipe;
121         int ret;
122
123         DRM_DEBUG_KMS("%s\n", __FILE__);
124
125         /*
126          * copy the mode data adjusted by mode_fixup() into crtc->mode
127          * so that hardware can be seet to proper mode.
128          */
129         memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
130
131         crtc_w = crtc->fb->width - x;
132         crtc_h = crtc->fb->height - y;
133
134         ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
135                                     x, y, crtc_w, crtc_h);
136         if (ret)
137                 return ret;
138
139         plane->crtc = crtc;
140         plane->fb = crtc->fb;
141
142         exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
143
144         return 0;
145 }
146
147 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
148                                           struct drm_framebuffer *old_fb)
149 {
150         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
151         struct drm_plane *plane = exynos_crtc->plane;
152         unsigned int crtc_w;
153         unsigned int crtc_h;
154         int ret;
155
156         DRM_DEBUG_KMS("%s\n", __FILE__);
157
158         /* when framebuffer changing is requested, crtc's dpms should be on */
159         if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
160                 DRM_ERROR("failed framebuffer changing request.\n");
161                 return -EPERM;
162         }
163
164         crtc_w = crtc->fb->width - x;
165         crtc_h = crtc->fb->height - y;
166
167         ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
168                                     x, y, crtc_w, crtc_h);
169         if (ret)
170                 return ret;
171
172         exynos_drm_crtc_commit(crtc);
173
174         return 0;
175 }
176
177 static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
178 {
179         DRM_DEBUG_KMS("%s\n", __FILE__);
180         /* drm framework doesn't check NULL */
181 }
182
183 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
184 {
185         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
186
187         DRM_DEBUG_KMS("%s\n", __FILE__);
188
189         exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
190         exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
191 }
192
193 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
194         .dpms           = exynos_drm_crtc_dpms,
195         .prepare        = exynos_drm_crtc_prepare,
196         .commit         = exynos_drm_crtc_commit,
197         .mode_fixup     = exynos_drm_crtc_mode_fixup,
198         .mode_set       = exynos_drm_crtc_mode_set,
199         .mode_set_base  = exynos_drm_crtc_mode_set_base,
200         .load_lut       = exynos_drm_crtc_load_lut,
201         .disable        = exynos_drm_crtc_disable,
202 };
203
204 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
205                                       struct drm_framebuffer *fb,
206                                       struct drm_pending_vblank_event *event)
207 {
208         struct drm_device *dev = crtc->dev;
209         struct exynos_drm_private *dev_priv = dev->dev_private;
210         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
211         struct drm_framebuffer *old_fb = crtc->fb;
212         int ret = -EINVAL;
213
214         DRM_DEBUG_KMS("%s\n", __FILE__);
215
216         /* when the page flip is requested, crtc's dpms should be on */
217         if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
218                 DRM_ERROR("failed page flip request.\n");
219                 return -EINVAL;
220         }
221
222         mutex_lock(&dev->struct_mutex);
223
224         if (event) {
225                 /*
226                  * the pipe from user always is 0 so we can set pipe number
227                  * of current owner to event.
228                  */
229                 event->pipe = exynos_crtc->pipe;
230
231                 ret = drm_vblank_get(dev, exynos_crtc->pipe);
232                 if (ret) {
233                         DRM_DEBUG("failed to acquire vblank counter\n");
234                         list_del(&event->base.link);
235
236                         goto out;
237                 }
238
239                 spin_lock_irq(&dev->event_lock);
240                 list_add_tail(&event->base.link,
241                                 &dev_priv->pageflip_event_list);
242                 spin_unlock_irq(&dev->event_lock);
243
244                 crtc->fb = fb;
245                 ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y,
246                                                     NULL);
247                 if (ret) {
248                         crtc->fb = old_fb;
249
250                         spin_lock_irq(&dev->event_lock);
251                         drm_vblank_put(dev, exynos_crtc->pipe);
252                         list_del(&event->base.link);
253                         spin_unlock_irq(&dev->event_lock);
254
255                         goto out;
256                 }
257         }
258 out:
259         mutex_unlock(&dev->struct_mutex);
260         return ret;
261 }
262
263 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
264 {
265         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
266         struct exynos_drm_private *private = crtc->dev->dev_private;
267
268         DRM_DEBUG_KMS("%s\n", __FILE__);
269
270         private->crtc[exynos_crtc->pipe] = NULL;
271
272         drm_crtc_cleanup(crtc);
273         kfree(exynos_crtc);
274 }
275
276 static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
277                                         struct drm_property *property,
278                                         uint64_t val)
279 {
280         struct drm_device *dev = crtc->dev;
281         struct exynos_drm_private *dev_priv = dev->dev_private;
282         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
283
284         DRM_DEBUG_KMS("%s\n", __func__);
285
286         if (property == dev_priv->crtc_mode_property) {
287                 enum exynos_crtc_mode mode = val;
288
289                 if (mode == exynos_crtc->mode)
290                         return 0;
291
292                 exynos_crtc->mode = mode;
293
294                 switch (mode) {
295                 case CRTC_MODE_NORMAL:
296                         exynos_drm_crtc_commit(crtc);
297                         break;
298                 case CRTC_MODE_BLANK:
299                         exynos_plane_dpms(exynos_crtc->plane,
300                                           DRM_MODE_DPMS_OFF);
301                         break;
302                 default:
303                         break;
304                 }
305
306                 return 0;
307         }
308
309         return -EINVAL;
310 }
311
312 static struct drm_crtc_funcs exynos_crtc_funcs = {
313         .set_config     = drm_crtc_helper_set_config,
314         .page_flip      = exynos_drm_crtc_page_flip,
315         .destroy        = exynos_drm_crtc_destroy,
316         .set_property   = exynos_drm_crtc_set_property,
317 };
318
319 static const struct drm_prop_enum_list mode_names[] = {
320         { CRTC_MODE_NORMAL, "normal" },
321         { CRTC_MODE_BLANK, "blank" },
322 };
323
324 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
325 {
326         struct drm_device *dev = crtc->dev;
327         struct exynos_drm_private *dev_priv = dev->dev_private;
328         struct drm_property *prop;
329
330         DRM_DEBUG_KMS("%s\n", __func__);
331
332         prop = dev_priv->crtc_mode_property;
333         if (!prop) {
334                 prop = drm_property_create_enum(dev, 0, "mode", mode_names,
335                                                 ARRAY_SIZE(mode_names));
336                 if (!prop)
337                         return;
338
339                 dev_priv->crtc_mode_property = prop;
340         }
341
342         drm_object_attach_property(&crtc->base, prop, 0);
343 }
344
345 int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
346 {
347         struct exynos_drm_crtc *exynos_crtc;
348         struct exynos_drm_private *private = dev->dev_private;
349         struct drm_crtc *crtc;
350
351         DRM_DEBUG_KMS("%s\n", __FILE__);
352
353         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
354         if (!exynos_crtc) {
355                 DRM_ERROR("failed to allocate exynos crtc\n");
356                 return -ENOMEM;
357         }
358
359         exynos_crtc->pipe = nr;
360         exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
361         exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
362         if (!exynos_crtc->plane) {
363                 kfree(exynos_crtc);
364                 return -ENOMEM;
365         }
366
367         crtc = &exynos_crtc->drm_crtc;
368
369         private->crtc[nr] = crtc;
370
371         drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
372         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
373
374         exynos_drm_crtc_attach_mode_property(crtc);
375
376         return 0;
377 }
378
379 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
380 {
381         struct exynos_drm_private *private = dev->dev_private;
382         struct exynos_drm_crtc *exynos_crtc =
383                 to_exynos_crtc(private->crtc[crtc]);
384
385         DRM_DEBUG_KMS("%s\n", __FILE__);
386
387         if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
388                 return -EPERM;
389
390         exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
391                         exynos_drm_enable_vblank);
392
393         return 0;
394 }
395
396 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
397 {
398         struct exynos_drm_private *private = dev->dev_private;
399         struct exynos_drm_crtc *exynos_crtc =
400                 to_exynos_crtc(private->crtc[crtc]);
401
402         DRM_DEBUG_KMS("%s\n", __FILE__);
403
404         if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
405                 return;
406
407         exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
408                         exynos_drm_disable_vblank);
409 }