blob: e36579c1c025bc97b2ae2c7b59e2cd46931a54ee [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
Gustavo Padovan199329c2015-06-01 12:04:43 -030044static void
45exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
Inki Dae1c248b72011-10-04 19:19:01 +090046{
Joonyoung Shim4070d212012-06-27 14:27:05 +090047 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090048
Gustavo Padovan199329c2015-06-01 12:04:43 -030049 if (exynos_crtc->ops->commit)
50 exynos_crtc->ops->commit(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090051}
52
Andrzej Hajda5625b342015-10-26 13:03:39 +010053static 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
Andrzej Hajdac4e07402015-11-27 07:57:31 +010058 if (!state->enable)
59 return 0;
60
Andrzej Hajda5625b342015-10-26 13:03:39 +010061 if (exynos_crtc->ops->atomic_check)
62 return exynos_crtc->ops->atomic_check(exynos_crtc, state);
63
64 return 0;
65}
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);
71
Gustavo Padovanc4533662015-08-15 13:26:18 -030072 exynos_crtc->event = crtc->state->event;
Gustavo Padovand9220d42015-08-24 20:36:59 +090073
Marek Szyprowskid29c2c12016-01-05 13:52:51 +010074 if (exynos_crtc->ops->atomic_begin)
75 exynos_crtc->ops->atomic_begin(exynos_crtc);
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030076}
77
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020078static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
79 struct drm_crtc_state *old_crtc_state)
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030080{
Gustavo Padovand9220d42015-08-24 20:36:59 +090081 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovand9220d42015-08-24 20:36:59 +090082
Marek Szyprowskid29c2c12016-01-05 13:52:51 +010083 if (exynos_crtc->ops->atomic_flush)
84 exynos_crtc->ops->atomic_flush(exynos_crtc);
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030085}
86
Ville Syrjälä800ba2b2015-12-15 12:21:06 +010087static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
Gustavo Padovan63498e32015-06-01 12:04:53 -030088 .enable = exynos_drm_crtc_enable,
Gustavo Padovan3fc48672015-06-01 12:04:51 -030089 .disable = exynos_drm_crtc_disable,
Gustavo Padovan199329c2015-06-01 12:04:43 -030090 .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
Andrzej Hajda5625b342015-10-26 13:03:39 +010091 .atomic_check = exynos_crtc_atomic_check,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030092 .atomic_begin = exynos_crtc_atomic_begin,
93 .atomic_flush = exynos_crtc_atomic_flush,
Inki Dae1c248b72011-10-04 19:19:01 +090094};
95
Inki Dae1c248b72011-10-04 19:19:01 +090096static 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
Inki Dae1c248b72011-10-04 19:19:01 +0900101 private->crtc[exynos_crtc->pipe] = NULL;
102
103 drm_crtc_cleanup(crtc);
104 kfree(exynos_crtc);
105}
106
Ville Syrjälä800ba2b2015-12-15 12:21:06 +0100107static const struct drm_crtc_funcs exynos_crtc_funcs = {
Gustavo Padovan47a7def2015-06-01 12:04:47 -0300108 .set_config = drm_atomic_helper_set_config,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300109 .page_flip = drm_atomic_helper_page_flip,
Inki Dae1c248b72011-10-04 19:19:01 +0900110 .destroy = exynos_drm_crtc_destroy,
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300111 .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,
Inki Dae1c248b72011-10-04 19:19:01 +0900114};
115
Gustavo Padovan93bca242015-01-18 18:16:23 +0900116struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900117 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)
Inki Dae1c248b72011-10-04 19:19:01 +0900122{
123 struct exynos_drm_crtc *exynos_crtc;
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200124 struct exynos_drm_private *private = drm_dev->dev_private;
Inki Dae1c248b72011-10-04 19:19:01 +0900125 struct drm_crtc *crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200126 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900127
Inki Dae1c248b72011-10-04 19:19:01 +0900128 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900129 if (!exynos_crtc)
Gustavo Padovan93bca242015-01-18 18:16:23 +0900130 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900131
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200132 exynos_crtc->pipe = pipe;
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200133 exynos_crtc->type = type;
Gustavo Padovan93bca242015-01-18 18:16:23 +0900134 exynos_crtc->ops = ops;
135 exynos_crtc->ctx = ctx;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900136
Gustavo Padovanc4533662015-08-15 13:26:18 -0300137 init_waitqueue_head(&exynos_crtc->wait_update);
138
Gustavo Padovan357193c2014-11-03 18:20:29 -0200139 crtc = &exynos_crtc->base;
Inki Dae1c248b72011-10-04 19:19:01 +0900140
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200141 private->crtc[pipe] = crtc;
Inki Dae1c248b72011-10-04 19:19:01 +0900142
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200143 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200144 &exynos_crtc_funcs, NULL);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200145 if (ret < 0)
146 goto err_crtc;
147
Inki Dae1c248b72011-10-04 19:19:01 +0900148 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
149
Gustavo Padovan93bca242015-01-18 18:16:23 +0900150 return exynos_crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200151
152err_crtc:
153 plane->funcs->destroy(plane);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200154 kfree(exynos_crtc);
Gustavo Padovan93bca242015-01-18 18:16:23 +0900155 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900156}
157
Thierry Reding88e72712015-09-24 18:35:31 +0200158int exynos_drm_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900159{
160 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900161 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900162 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900163
Gustavo Padovan93bca242015-01-18 18:16:23 +0900164 if (exynos_crtc->ops->enable_vblank)
Gustavo Padovan08dd2002015-07-16 12:23:39 -0300165 return exynos_crtc->ops->enable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900166
167 return 0;
168}
169
Thierry Reding88e72712015-09-24 18:35:31 +0200170void exynos_drm_crtc_disable_vblank(struct drm_device *dev, unsigned 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->disable_vblank)
177 exynos_crtc->ops->disable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900178}
Rahul Sharma663d8762013-01-03 05:44:04 -0500179
Gustavo Padovanc4533662015-08-15 13:26:18 -0300180void exynos_drm_crtc_wait_pending_update(struct exynos_drm_crtc *exynos_crtc)
181{
182 wait_event_timeout(exynos_crtc->wait_update,
183 (atomic_read(&exynos_crtc->pending_update) == 0),
184 msecs_to_jiffies(50));
185}
186
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300187void exynos_drm_crtc_finish_update(struct exynos_drm_crtc *exynos_crtc,
188 struct exynos_drm_plane *exynos_plane)
Rahul Sharma663d8762013-01-03 05:44:04 -0500189{
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300190 struct drm_crtc *crtc = &exynos_crtc->base;
Rahul Sharma663d8762013-01-03 05:44:04 -0500191 unsigned long flags;
192
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300193 exynos_plane->pending_fb = NULL;
194
Gustavo Padovanc4533662015-08-15 13:26:18 -0300195 if (atomic_dec_and_test(&exynos_crtc->pending_update))
196 wake_up(&exynos_crtc->wait_update);
197
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300198 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Gustavo Padovan7cf23ea2015-08-15 13:26:19 -0300199 if (exynos_crtc->event)
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300200 drm_crtc_send_vblank_event(crtc, exynos_crtc->event);
Rahul Sharma663d8762013-01-03 05:44:04 -0500201
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300202 exynos_crtc->event = NULL;
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300203 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Rahul Sharma663d8762013-01-03 05:44:04 -0500204}
Sean Paul080be03d2014-02-19 21:02:55 +0900205
Inki Daef37cd5e2014-05-09 14:25:20 +0900206int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
Gustavo Padovancf67cc92015-08-11 17:38:06 +0900207 enum exynos_drm_output_type out_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900208{
209 struct drm_crtc *crtc;
210
211 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
212 struct exynos_drm_crtc *exynos_crtc;
213
214 exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200215 if (exynos_crtc->type == out_type)
Gustavo Padovan8a326ed2014-11-04 18:44:47 -0200216 return exynos_crtc->pipe;
Inki Daef37cd5e2014-05-09 14:25:20 +0900217 }
218
219 return -EPERM;
220}
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900221
222void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
223{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900224 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900225
Gustavo Padovan93bca242015-01-18 18:16:23 +0900226 if (exynos_crtc->ops->te_handler)
227 exynos_crtc->ops->te_handler(exynos_crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900228}
Inki Daec74d8eb2016-01-05 19:50:31 +0900229
230void exynos_drm_crtc_cancel_page_flip(struct drm_crtc *crtc,
231 struct drm_file *file)
232{
233 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
234 struct drm_pending_vblank_event *e;
235 unsigned long flags;
236
237 spin_lock_irqsave(&crtc->dev->event_lock, flags);
238 e = exynos_crtc->event;
239 if (e && e->base.file_priv == file) {
240 exynos_crtc->event = NULL;
241 /*
242 * event will be destroyed by core part
243 * so below line should be removed later with core changes
244 */
245 e->base.destroy(&e->base);
246 /*
247 * event_space will be increased by core part
248 * so below line should be removed later with core changes.
249 */
250 file->event_space += sizeof(e->event);
251 atomic_dec(&exynos_crtc->pending_update);
252 }
253 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
254}