blob: 0872aa2f450f273a992bc414081e8501e11bf787 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* 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 *
Inki Daed81aecb2012-12-18 02:30:17 +09009 * 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.
Inki Dae1c248b72011-10-04 19:19:01 +090013 */
14
David Howells760285e2012-10-02 18:01:07 +010015#include <drm/drmP.h>
16#include <drm/drm_crtc_helper.h>
Gustavo Padovan4ea95262015-06-01 12:04:44 -030017#include <drm/drm_atomic.h>
18#include <drm/drm_atomic_helper.h>
Inki Dae1c248b72011-10-04 19:19:01 +090019
Mark Browne30655d2013-08-13 00:46:40 +010020#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090021#include "exynos_drm_drv.h"
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090022#include "exynos_drm_plane.h"
Inki Dae1c248b72011-10-04 19:19:01 +090023
Gustavo Padovan63498e32015-06-01 12:04:53 -030024static void exynos_drm_crtc_enable(struct drm_crtc *crtc)
Inki Dae1c248b72011-10-04 19:19:01 +090025{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090026 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090027
Gustavo Padovan3cecda02015-06-01 12:04:55 -030028 if (exynos_crtc->ops->enable)
29 exynos_crtc->ops->enable(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +090030
Gustavo Padovan63498e32015-06-01 12:04:53 -030031 drm_crtc_vblank_on(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090032}
33
Gustavo Padovan3fc48672015-06-01 12:04:51 -030034static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
35{
Gustavo Padovan63498e32015-06-01 12:04:53 -030036 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan3fc48672015-06-01 12:04:51 -030037
Gustavo Padovan63498e32015-06-01 12:04:53 -030038 drm_crtc_vblank_off(crtc);
39
Gustavo Padovan3cecda02015-06-01 12:04:55 -030040 if (exynos_crtc->ops->disable)
41 exynos_crtc->ops->disable(exynos_crtc);
Gustavo Padovan3fc48672015-06-01 12:04:51 -030042}
43
Inki Dae1c248b72011-10-04 19:19:01 +090044static bool
45exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020046 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +090047 struct drm_display_mode *adjusted_mode)
48{
Sean Paul4b405262014-01-30 16:19:19 -050049 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Sean Paul4b405262014-01-30 16:19:19 -050050
Gustavo Padovan93bca242015-01-18 18:16:23 +090051 if (exynos_crtc->ops->mode_fixup)
52 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
53 adjusted_mode);
Sean Paul4b405262014-01-30 16:19:19 -050054
Inki Dae1c248b72011-10-04 19:19:01 +090055 return true;
56}
57
Gustavo Padovan199329c2015-06-01 12:04:43 -030058static void
59exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
Inki Dae1c248b72011-10-04 19:19:01 +090060{
Joonyoung Shim4070d212012-06-27 14:27:05 +090061 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090062
Gustavo Padovan199329c2015-06-01 12:04:43 -030063 if (exynos_crtc->ops->commit)
64 exynos_crtc->ops->commit(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090065}
66
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020067static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
68 struct drm_crtc_state *old_crtc_state)
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030069{
70 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovand9220d42015-08-24 20:36:59 +090071 struct drm_plane *plane;
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030072
Gustavo Padovanc4533662015-08-15 13:26:18 -030073 exynos_crtc->event = crtc->state->event;
Gustavo Padovand9220d42015-08-24 20:36:59 +090074
75 drm_atomic_crtc_for_each_plane(plane, crtc) {
76 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
77
78 if (exynos_crtc->ops->atomic_begin)
79 exynos_crtc->ops->atomic_begin(exynos_crtc,
80 exynos_plane);
81 }
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030082}
83
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020084static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
85 struct drm_crtc_state *old_crtc_state)
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030086{
Gustavo Padovand9220d42015-08-24 20:36:59 +090087 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
88 struct drm_plane *plane;
89
90 drm_atomic_crtc_for_each_plane(plane, crtc) {
91 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
92
93 if (exynos_crtc->ops->atomic_flush)
94 exynos_crtc->ops->atomic_flush(exynos_crtc,
95 exynos_plane);
96 }
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030097}
98
Inki Dae1c248b72011-10-04 19:19:01 +090099static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
Gustavo Padovan63498e32015-06-01 12:04:53 -0300100 .enable = exynos_drm_crtc_enable,
Gustavo Padovan3fc48672015-06-01 12:04:51 -0300101 .disable = exynos_drm_crtc_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900102 .mode_fixup = exynos_drm_crtc_mode_fixup,
Gustavo Padovan199329c2015-06-01 12:04:43 -0300103 .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300104 .atomic_begin = exynos_crtc_atomic_begin,
105 .atomic_flush = exynos_crtc_atomic_flush,
Inki Dae1c248b72011-10-04 19:19:01 +0900106};
107
Inki Dae1c248b72011-10-04 19:19:01 +0900108static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
109{
110 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
111 struct exynos_drm_private *private = crtc->dev->dev_private;
112
Inki Dae1c248b72011-10-04 19:19:01 +0900113 private->crtc[exynos_crtc->pipe] = NULL;
114
115 drm_crtc_cleanup(crtc);
116 kfree(exynos_crtc);
117}
118
119static struct drm_crtc_funcs exynos_crtc_funcs = {
Gustavo Padovan47a7def2015-06-01 12:04:47 -0300120 .set_config = drm_atomic_helper_set_config,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300121 .page_flip = drm_atomic_helper_page_flip,
Inki Dae1c248b72011-10-04 19:19:01 +0900122 .destroy = exynos_drm_crtc_destroy,
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300123 .reset = drm_atomic_helper_crtc_reset,
124 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
125 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Inki Dae1c248b72011-10-04 19:19:01 +0900126};
127
Gustavo Padovan93bca242015-01-18 18:16:23 +0900128struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900129 struct drm_plane *plane,
130 int pipe,
131 enum exynos_drm_output_type type,
132 const struct exynos_drm_crtc_ops *ops,
133 void *ctx)
Inki Dae1c248b72011-10-04 19:19:01 +0900134{
135 struct exynos_drm_crtc *exynos_crtc;
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200136 struct exynos_drm_private *private = drm_dev->dev_private;
Inki Dae1c248b72011-10-04 19:19:01 +0900137 struct drm_crtc *crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200138 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900139
Inki Dae1c248b72011-10-04 19:19:01 +0900140 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900141 if (!exynos_crtc)
Gustavo Padovan93bca242015-01-18 18:16:23 +0900142 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900143
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200144 exynos_crtc->pipe = pipe;
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200145 exynos_crtc->type = type;
Gustavo Padovan93bca242015-01-18 18:16:23 +0900146 exynos_crtc->ops = ops;
147 exynos_crtc->ctx = ctx;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900148
Gustavo Padovanc4533662015-08-15 13:26:18 -0300149 init_waitqueue_head(&exynos_crtc->wait_update);
150
Gustavo Padovan357193c2014-11-03 18:20:29 -0200151 crtc = &exynos_crtc->base;
Inki Dae1c248b72011-10-04 19:19:01 +0900152
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200153 private->crtc[pipe] = crtc;
Inki Dae1c248b72011-10-04 19:19:01 +0900154
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200155 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200156 &exynos_crtc_funcs);
157 if (ret < 0)
158 goto err_crtc;
159
Inki Dae1c248b72011-10-04 19:19:01 +0900160 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
161
Gustavo Padovan93bca242015-01-18 18:16:23 +0900162 return exynos_crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200163
164err_crtc:
165 plane->funcs->destroy(plane);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200166 kfree(exynos_crtc);
Gustavo Padovan93bca242015-01-18 18:16:23 +0900167 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900168}
169
Sean Paul080be03d2014-02-19 21:02:55 +0900170int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900171{
172 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900173 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900174 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900175
Gustavo Padovan93bca242015-01-18 18:16:23 +0900176 if (exynos_crtc->ops->enable_vblank)
Gustavo Padovan08dd2002015-07-16 12:23:39 -0300177 return exynos_crtc->ops->enable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900178
179 return 0;
180}
181
Sean Paul080be03d2014-02-19 21:02:55 +0900182void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900183{
184 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900185 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900186 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900187
Gustavo Padovan93bca242015-01-18 18:16:23 +0900188 if (exynos_crtc->ops->disable_vblank)
189 exynos_crtc->ops->disable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900190}
Rahul Sharma663d8762013-01-03 05:44:04 -0500191
Gustavo Padovanc4533662015-08-15 13:26:18 -0300192void exynos_drm_crtc_wait_pending_update(struct exynos_drm_crtc *exynos_crtc)
193{
194 wait_event_timeout(exynos_crtc->wait_update,
195 (atomic_read(&exynos_crtc->pending_update) == 0),
196 msecs_to_jiffies(50));
197}
198
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300199void exynos_drm_crtc_finish_update(struct exynos_drm_crtc *exynos_crtc,
200 struct exynos_drm_plane *exynos_plane)
Rahul Sharma663d8762013-01-03 05:44:04 -0500201{
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300202 struct drm_crtc *crtc = &exynos_crtc->base;
Rahul Sharma663d8762013-01-03 05:44:04 -0500203 unsigned long flags;
204
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300205 exynos_plane->pending_fb = NULL;
206
Gustavo Padovanc4533662015-08-15 13:26:18 -0300207 if (atomic_dec_and_test(&exynos_crtc->pending_update))
208 wake_up(&exynos_crtc->wait_update);
209
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300210 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Gustavo Padovan7cf23ea2015-08-15 13:26:19 -0300211 if (exynos_crtc->event)
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300212 drm_crtc_send_vblank_event(crtc, exynos_crtc->event);
Rahul Sharma663d8762013-01-03 05:44:04 -0500213
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300214 exynos_crtc->event = NULL;
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300215 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Rahul Sharma663d8762013-01-03 05:44:04 -0500216}
Sean Paul080be03d2014-02-19 21:02:55 +0900217
Sean Paul080be03d2014-02-19 21:02:55 +0900218void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
219{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900220 struct exynos_drm_crtc *exynos_crtc;
Sean Paul080be03d2014-02-19 21:02:55 +0900221 struct drm_device *dev = fb->dev;
222 struct drm_crtc *crtc;
223
224 /*
225 * make sure that overlay data are updated to real hardware
226 * for all encoders.
227 */
228 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Gustavo Padovan93bca242015-01-18 18:16:23 +0900229 exynos_crtc = to_exynos_crtc(crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900230
231 /*
232 * wait for vblank interrupt
233 * - this makes sure that overlay data are updated to
234 * real hardware.
235 */
Gustavo Padovan93bca242015-01-18 18:16:23 +0900236 if (exynos_crtc->ops->wait_for_vblank)
237 exynos_crtc->ops->wait_for_vblank(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900238 }
239}
Inki Daef37cd5e2014-05-09 14:25:20 +0900240
241int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
Gustavo Padovancf67cc92015-08-11 17:38:06 +0900242 enum exynos_drm_output_type out_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900243{
244 struct drm_crtc *crtc;
245
246 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
247 struct exynos_drm_crtc *exynos_crtc;
248
249 exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200250 if (exynos_crtc->type == out_type)
Gustavo Padovan8a326ed2014-11-04 18:44:47 -0200251 return exynos_crtc->pipe;
Inki Daef37cd5e2014-05-09 14:25:20 +0900252 }
253
254 return -EPERM;
255}
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900256
257void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
258{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900259 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900260
Gustavo Padovan93bca242015-01-18 18:16:23 +0900261 if (exynos_crtc->ops->te_handler)
262 exynos_crtc->ops->te_handler(exynos_crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900263}