blob: ac544de73c828434ebb27498d6cc807c1941c36a [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>
Andrzej Hajda1ca582f2017-08-24 15:33:51 +020019#include <drm/drm_encoder.h>
Inki Dae1c248b72011-10-04 19:19:01 +090020
Mark Browne30655d2013-08-13 00:46:40 +010021#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090022#include "exynos_drm_drv.h"
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090023#include "exynos_drm_plane.h"
Inki Dae1c248b72011-10-04 19:19:01 +090024
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +030025static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc,
26 struct drm_crtc_state *old_state)
Inki Dae1c248b72011-10-04 19:19:01 +090027{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090028 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090029
Gustavo Padovan3cecda02015-06-01 12:04:55 -030030 if (exynos_crtc->ops->enable)
31 exynos_crtc->ops->enable(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +090032
Gustavo Padovan63498e32015-06-01 12:04:53 -030033 drm_crtc_vblank_on(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090034}
35
Laurent Pinchart64581712017-06-30 12:36:45 +030036static void exynos_drm_crtc_atomic_disable(struct drm_crtc *crtc,
37 struct drm_crtc_state *old_state)
Gustavo Padovan3fc48672015-06-01 12:04:51 -030038{
Gustavo Padovan63498e32015-06-01 12:04:53 -030039 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan3fc48672015-06-01 12:04:51 -030040
Gustavo Padovan63498e32015-06-01 12:04:53 -030041 drm_crtc_vblank_off(crtc);
42
Gustavo Padovan3cecda02015-06-01 12:04:55 -030043 if (exynos_crtc->ops->disable)
44 exynos_crtc->ops->disable(exynos_crtc);
Inki Dae41cbf0f2017-01-20 12:51:41 +090045
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 }
Gustavo Padovan3fc48672015-06-01 12:04:51 -030053}
54
Andrzej Hajda5625b342015-10-26 13:03:39 +010055static 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
Andrzej Hajdac4e07402015-11-27 07:57:31 +010060 if (!state->enable)
61 return 0;
62
Andrzej Hajda5625b342015-10-26 13:03:39 +010063 if (exynos_crtc->ops->atomic_check)
64 return exynos_crtc->ops->atomic_check(exynos_crtc, state);
65
66 return 0;
67}
68
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020069static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
70 struct drm_crtc_state *old_crtc_state)
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030071{
72 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
73
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 = {
Andrzej Hajda5625b342015-10-26 13:03:39 +010088 .atomic_check = exynos_crtc_atomic_check,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030089 .atomic_begin = exynos_crtc_atomic_begin,
90 .atomic_flush = exynos_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +030091 .atomic_enable = exynos_drm_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +030092 .atomic_disable = exynos_drm_crtc_atomic_disable,
Inki Dae1c248b72011-10-04 19:19:01 +090093};
94
Andrzej Hajdaa3922762017-03-14 09:27:56 +010095void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
96{
97 struct drm_crtc *crtc = &exynos_crtc->base;
98 struct drm_pending_vblank_event *event = crtc->state->event;
99 unsigned long flags;
100
Andrzej Hajda73b7b442017-03-15 15:41:01 +0100101 if (!event)
102 return;
103 crtc->state->event = NULL;
Andrzej Hajdaa3922762017-03-14 09:27:56 +0100104
Andrzej Hajda73b7b442017-03-15 15:41:01 +0100105 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
106
107 spin_lock_irqsave(&crtc->dev->event_lock, flags);
108 drm_crtc_arm_vblank_event(crtc, event);
109 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Andrzej Hajdaa3922762017-03-14 09:27:56 +0100110}
111
Inki Dae1c248b72011-10-04 19:19:01 +0900112static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
113{
114 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900115
116 drm_crtc_cleanup(crtc);
117 kfree(exynos_crtc);
118}
119
Shawn Guo64b0e1d2017-02-07 17:16:20 +0800120static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
121{
122 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
123
124 if (exynos_crtc->ops->enable_vblank)
125 return exynos_crtc->ops->enable_vblank(exynos_crtc);
126
127 return 0;
128}
129
130static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
131{
132 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
133
134 if (exynos_crtc->ops->disable_vblank)
135 exynos_crtc->ops->disable_vblank(exynos_crtc);
136}
137
Andrzej Hajda0586feb2017-03-15 15:41:02 +0100138static u32 exynos_drm_crtc_get_vblank_counter(struct drm_crtc *crtc)
139{
140 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
141
142 if (exynos_crtc->ops->get_vblank_counter)
143 return exynos_crtc->ops->get_vblank_counter(exynos_crtc);
144
145 return 0;
146}
147
Ville Syrjälä800ba2b2015-12-15 12:21:06 +0100148static const struct drm_crtc_funcs exynos_crtc_funcs = {
Gustavo Padovan47a7def2015-06-01 12:04:47 -0300149 .set_config = drm_atomic_helper_set_config,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300150 .page_flip = drm_atomic_helper_page_flip,
Inki Dae1c248b72011-10-04 19:19:01 +0900151 .destroy = exynos_drm_crtc_destroy,
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300152 .reset = drm_atomic_helper_crtc_reset,
153 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
154 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Shawn Guo64b0e1d2017-02-07 17:16:20 +0800155 .enable_vblank = exynos_drm_crtc_enable_vblank,
156 .disable_vblank = exynos_drm_crtc_disable_vblank,
Andrzej Hajda0586feb2017-03-15 15:41:02 +0100157 .get_vblank_counter = exynos_drm_crtc_get_vblank_counter,
Inki Dae1c248b72011-10-04 19:19:01 +0900158};
159
Gustavo Padovan93bca242015-01-18 18:16:23 +0900160struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900161 struct drm_plane *plane,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900162 enum exynos_drm_output_type type,
163 const struct exynos_drm_crtc_ops *ops,
164 void *ctx)
Inki Dae1c248b72011-10-04 19:19:01 +0900165{
166 struct exynos_drm_crtc *exynos_crtc;
Inki Dae1c248b72011-10-04 19:19:01 +0900167 struct drm_crtc *crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200168 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900169
Inki Dae1c248b72011-10-04 19:19:01 +0900170 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900171 if (!exynos_crtc)
Gustavo Padovan93bca242015-01-18 18:16:23 +0900172 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900173
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200174 exynos_crtc->type = type;
Gustavo Padovan93bca242015-01-18 18:16:23 +0900175 exynos_crtc->ops = ops;
176 exynos_crtc->ctx = ctx;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900177
Gustavo Padovan357193c2014-11-03 18:20:29 -0200178 crtc = &exynos_crtc->base;
Inki Dae1c248b72011-10-04 19:19:01 +0900179
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200180 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200181 &exynos_crtc_funcs, NULL);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200182 if (ret < 0)
183 goto err_crtc;
184
Inki Dae1c248b72011-10-04 19:19:01 +0900185 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
186
Gustavo Padovan93bca242015-01-18 18:16:23 +0900187 return exynos_crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200188
189err_crtc:
190 plane->funcs->destroy(plane);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200191 kfree(exynos_crtc);
Gustavo Padovan93bca242015-01-18 18:16:23 +0900192 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900193}
194
Andrzej Hajda1ca582f2017-08-24 15:33:51 +0200195struct exynos_drm_crtc *exynos_drm_crtc_get_by_type(struct drm_device *drm_dev,
Gustavo Padovancf67cc92015-08-11 17:38:06 +0900196 enum exynos_drm_output_type out_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900197{
198 struct drm_crtc *crtc;
199
Andrzej Hajdad6449512017-05-29 10:05:25 +0900200 drm_for_each_crtc(crtc, drm_dev)
201 if (to_exynos_crtc(crtc)->type == out_type)
Andrzej Hajda1ca582f2017-08-24 15:33:51 +0200202 return to_exynos_crtc(crtc);
Inki Daef37cd5e2014-05-09 14:25:20 +0900203
Andrzej Hajda1ca582f2017-08-24 15:33:51 +0200204 return ERR_PTR(-EPERM);
205}
206
207int exynos_drm_set_possible_crtcs(struct drm_encoder *encoder,
208 enum exynos_drm_output_type out_type)
209{
210 struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(encoder->dev,
211 out_type);
212
213 if (IS_ERR(crtc))
214 return PTR_ERR(crtc);
215
216 encoder->possible_crtcs = drm_crtc_mask(&crtc->base);
217
218 return 0;
Inki Daef37cd5e2014-05-09 14:25:20 +0900219}
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900220
221void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
222{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900223 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900224
Gustavo Padovan93bca242015-01-18 18:16:23 +0900225 if (exynos_crtc->ops->te_handler)
226 exynos_crtc->ops->te_handler(exynos_crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900227}