blob: b7e2fadb6442351c74592e8e3cfdf572fa5cd4c8 [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
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +030024static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc,
25 struct drm_crtc_state *old_state)
Inki Dae1c248b72011-10-04 19:19:01 +090026{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090027 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090028
Gustavo Padovan3cecda02015-06-01 12:04:55 -030029 if (exynos_crtc->ops->enable)
30 exynos_crtc->ops->enable(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +090031
Gustavo Padovan63498e32015-06-01 12:04:53 -030032 drm_crtc_vblank_on(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090033}
34
Gustavo Padovan3fc48672015-06-01 12:04:51 -030035static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
36{
Gustavo Padovan63498e32015-06-01 12:04:53 -030037 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan3fc48672015-06-01 12:04:51 -030038
Gustavo Padovan63498e32015-06-01 12:04:53 -030039 drm_crtc_vblank_off(crtc);
40
Gustavo Padovan3cecda02015-06-01 12:04:55 -030041 if (exynos_crtc->ops->disable)
42 exynos_crtc->ops->disable(exynos_crtc);
Inki Dae41cbf0f2017-01-20 12:51:41 +090043
44 if (crtc->state->event && !crtc->state->active) {
45 spin_lock_irq(&crtc->dev->event_lock);
46 drm_crtc_send_vblank_event(crtc, crtc->state->event);
47 spin_unlock_irq(&crtc->dev->event_lock);
48
49 crtc->state->event = NULL;
50 }
Gustavo Padovan3fc48672015-06-01 12:04:51 -030051}
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
Marek Szyprowskid29c2c12016-01-05 13:52:51 +010072 if (exynos_crtc->ops->atomic_begin)
73 exynos_crtc->ops->atomic_begin(exynos_crtc);
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030074}
75
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020076static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
77 struct drm_crtc_state *old_crtc_state)
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030078{
Gustavo Padovand9220d42015-08-24 20:36:59 +090079 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovand9220d42015-08-24 20:36:59 +090080
Marek Szyprowskid29c2c12016-01-05 13:52:51 +010081 if (exynos_crtc->ops->atomic_flush)
82 exynos_crtc->ops->atomic_flush(exynos_crtc);
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030083}
84
Ville Syrjälä800ba2b2015-12-15 12:21:06 +010085static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
Gustavo Padovan3fc48672015-06-01 12:04:51 -030086 .disable = exynos_drm_crtc_disable,
Andrzej Hajda5625b342015-10-26 13:03:39 +010087 .atomic_check = exynos_crtc_atomic_check,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030088 .atomic_begin = exynos_crtc_atomic_begin,
89 .atomic_flush = exynos_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +030090 .atomic_enable = exynos_drm_crtc_atomic_enable,
Inki Dae1c248b72011-10-04 19:19:01 +090091};
92
Andrzej Hajdaa3922762017-03-14 09:27:56 +010093void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
94{
95 struct drm_crtc *crtc = &exynos_crtc->base;
96 struct drm_pending_vblank_event *event = crtc->state->event;
97 unsigned long flags;
98
Andrzej Hajda73b7b442017-03-15 15:41:01 +010099 if (!event)
100 return;
101 crtc->state->event = NULL;
Andrzej Hajdaa3922762017-03-14 09:27:56 +0100102
Andrzej Hajda73b7b442017-03-15 15:41:01 +0100103 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
104
105 spin_lock_irqsave(&crtc->dev->event_lock, flags);
106 drm_crtc_arm_vblank_event(crtc, event);
107 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Andrzej Hajdaa3922762017-03-14 09:27:56 +0100108}
109
Inki Dae1c248b72011-10-04 19:19:01 +0900110static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
111{
112 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900113
114 drm_crtc_cleanup(crtc);
115 kfree(exynos_crtc);
116}
117
Shawn Guo64b0e1d2017-02-07 17:16:20 +0800118static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
119{
120 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
121
122 if (exynos_crtc->ops->enable_vblank)
123 return exynos_crtc->ops->enable_vblank(exynos_crtc);
124
125 return 0;
126}
127
128static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
129{
130 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
131
132 if (exynos_crtc->ops->disable_vblank)
133 exynos_crtc->ops->disable_vblank(exynos_crtc);
134}
135
Andrzej Hajda0586feb2017-03-15 15:41:02 +0100136static u32 exynos_drm_crtc_get_vblank_counter(struct drm_crtc *crtc)
137{
138 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
139
140 if (exynos_crtc->ops->get_vblank_counter)
141 return exynos_crtc->ops->get_vblank_counter(exynos_crtc);
142
143 return 0;
144}
145
Ville Syrjälä800ba2b2015-12-15 12:21:06 +0100146static const struct drm_crtc_funcs exynos_crtc_funcs = {
Gustavo Padovan47a7def2015-06-01 12:04:47 -0300147 .set_config = drm_atomic_helper_set_config,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300148 .page_flip = drm_atomic_helper_page_flip,
Inki Dae1c248b72011-10-04 19:19:01 +0900149 .destroy = exynos_drm_crtc_destroy,
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300150 .reset = drm_atomic_helper_crtc_reset,
151 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
152 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Shawn Guo64b0e1d2017-02-07 17:16:20 +0800153 .enable_vblank = exynos_drm_crtc_enable_vblank,
154 .disable_vblank = exynos_drm_crtc_disable_vblank,
Andrzej Hajda0586feb2017-03-15 15:41:02 +0100155 .get_vblank_counter = exynos_drm_crtc_get_vblank_counter,
Inki Dae1c248b72011-10-04 19:19:01 +0900156};
157
Gustavo Padovan93bca242015-01-18 18:16:23 +0900158struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900159 struct drm_plane *plane,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900160 enum exynos_drm_output_type type,
161 const struct exynos_drm_crtc_ops *ops,
162 void *ctx)
Inki Dae1c248b72011-10-04 19:19:01 +0900163{
164 struct exynos_drm_crtc *exynos_crtc;
Inki Dae1c248b72011-10-04 19:19:01 +0900165 struct drm_crtc *crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200166 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900167
Inki Dae1c248b72011-10-04 19:19:01 +0900168 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900169 if (!exynos_crtc)
Gustavo Padovan93bca242015-01-18 18:16:23 +0900170 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900171
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200172 exynos_crtc->type = type;
Gustavo Padovan93bca242015-01-18 18:16:23 +0900173 exynos_crtc->ops = ops;
174 exynos_crtc->ctx = ctx;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900175
Gustavo Padovan357193c2014-11-03 18:20:29 -0200176 crtc = &exynos_crtc->base;
Inki Dae1c248b72011-10-04 19:19:01 +0900177
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200178 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200179 &exynos_crtc_funcs, NULL);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200180 if (ret < 0)
181 goto err_crtc;
182
Inki Dae1c248b72011-10-04 19:19:01 +0900183 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
184
Gustavo Padovan93bca242015-01-18 18:16:23 +0900185 return exynos_crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200186
187err_crtc:
188 plane->funcs->destroy(plane);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200189 kfree(exynos_crtc);
Gustavo Padovan93bca242015-01-18 18:16:23 +0900190 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900191}
192
Inki Daef37cd5e2014-05-09 14:25:20 +0900193int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
Gustavo Padovancf67cc92015-08-11 17:38:06 +0900194 enum exynos_drm_output_type out_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900195{
196 struct drm_crtc *crtc;
197
Andrzej Hajdad6449512017-05-29 10:05:25 +0900198 drm_for_each_crtc(crtc, drm_dev)
199 if (to_exynos_crtc(crtc)->type == out_type)
200 return drm_crtc_index(crtc);
Inki Daef37cd5e2014-05-09 14:25:20 +0900201
202 return -EPERM;
203}
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900204
205void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
206{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900207 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900208
Gustavo Padovan93bca242015-01-18 18:16:23 +0900209 if (exynos_crtc->ops->te_handler)
210 exynos_crtc->ops->te_handler(exynos_crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900211}