blob: 50dec0df160fc270d710486e95c4ed91584f04a5 [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
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020053static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
54 struct drm_crtc_state *old_crtc_state)
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030055{
56 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovand9220d42015-08-24 20:36:59 +090057 struct drm_plane *plane;
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030058
Gustavo Padovanc4533662015-08-15 13:26:18 -030059 exynos_crtc->event = crtc->state->event;
Gustavo Padovand9220d42015-08-24 20:36:59 +090060
61 drm_atomic_crtc_for_each_plane(plane, crtc) {
62 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
63
64 if (exynos_crtc->ops->atomic_begin)
65 exynos_crtc->ops->atomic_begin(exynos_crtc,
66 exynos_plane);
67 }
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030068}
69
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020070static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
71 struct drm_crtc_state *old_crtc_state)
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030072{
Gustavo Padovand9220d42015-08-24 20:36:59 +090073 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
74 struct drm_plane *plane;
75
76 drm_atomic_crtc_for_each_plane(plane, crtc) {
77 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
78
79 if (exynos_crtc->ops->atomic_flush)
80 exynos_crtc->ops->atomic_flush(exynos_crtc,
81 exynos_plane);
82 }
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030083}
84
Inki Dae1c248b72011-10-04 19:19:01 +090085static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
Gustavo Padovan63498e32015-06-01 12:04:53 -030086 .enable = exynos_drm_crtc_enable,
Gustavo Padovan3fc48672015-06-01 12:04:51 -030087 .disable = exynos_drm_crtc_disable,
Gustavo Padovan199329c2015-06-01 12:04:43 -030088 .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030089 .atomic_begin = exynos_crtc_atomic_begin,
90 .atomic_flush = exynos_crtc_atomic_flush,
Inki Dae1c248b72011-10-04 19:19:01 +090091};
92
Inki Dae1c248b72011-10-04 19:19:01 +090093static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
94{
95 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
96 struct exynos_drm_private *private = crtc->dev->dev_private;
97
Inki Dae1c248b72011-10-04 19:19:01 +090098 private->crtc[exynos_crtc->pipe] = NULL;
99
100 drm_crtc_cleanup(crtc);
101 kfree(exynos_crtc);
102}
103
104static struct drm_crtc_funcs exynos_crtc_funcs = {
Gustavo Padovan47a7def2015-06-01 12:04:47 -0300105 .set_config = drm_atomic_helper_set_config,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300106 .page_flip = drm_atomic_helper_page_flip,
Inki Dae1c248b72011-10-04 19:19:01 +0900107 .destroy = exynos_drm_crtc_destroy,
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300108 .reset = drm_atomic_helper_crtc_reset,
109 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
110 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Inki Dae1c248b72011-10-04 19:19:01 +0900111};
112
Gustavo Padovan93bca242015-01-18 18:16:23 +0900113struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900114 struct drm_plane *plane,
115 int pipe,
116 enum exynos_drm_output_type type,
117 const struct exynos_drm_crtc_ops *ops,
118 void *ctx)
Inki Dae1c248b72011-10-04 19:19:01 +0900119{
120 struct exynos_drm_crtc *exynos_crtc;
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200121 struct exynos_drm_private *private = drm_dev->dev_private;
Inki Dae1c248b72011-10-04 19:19:01 +0900122 struct drm_crtc *crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200123 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900124
Inki Dae1c248b72011-10-04 19:19:01 +0900125 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900126 if (!exynos_crtc)
Gustavo Padovan93bca242015-01-18 18:16:23 +0900127 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900128
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200129 exynos_crtc->pipe = pipe;
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200130 exynos_crtc->type = type;
Gustavo Padovan93bca242015-01-18 18:16:23 +0900131 exynos_crtc->ops = ops;
132 exynos_crtc->ctx = ctx;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900133
Gustavo Padovanc4533662015-08-15 13:26:18 -0300134 init_waitqueue_head(&exynos_crtc->wait_update);
135
Gustavo Padovan357193c2014-11-03 18:20:29 -0200136 crtc = &exynos_crtc->base;
Inki Dae1c248b72011-10-04 19:19:01 +0900137
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200138 private->crtc[pipe] = crtc;
Inki Dae1c248b72011-10-04 19:19:01 +0900139
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200140 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200141 &exynos_crtc_funcs);
142 if (ret < 0)
143 goto err_crtc;
144
Inki Dae1c248b72011-10-04 19:19:01 +0900145 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
146
Gustavo Padovan93bca242015-01-18 18:16:23 +0900147 return exynos_crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200148
149err_crtc:
150 plane->funcs->destroy(plane);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200151 kfree(exynos_crtc);
Gustavo Padovan93bca242015-01-18 18:16:23 +0900152 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900153}
154
Thierry Reding88e72712015-09-24 18:35:31 +0200155int exynos_drm_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900156{
157 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900158 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900159 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900160
Gustavo Padovan93bca242015-01-18 18:16:23 +0900161 if (exynos_crtc->ops->enable_vblank)
Gustavo Padovan08dd2002015-07-16 12:23:39 -0300162 return exynos_crtc->ops->enable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900163
164 return 0;
165}
166
Thierry Reding88e72712015-09-24 18:35:31 +0200167void exynos_drm_crtc_disable_vblank(struct drm_device *dev, unsigned int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900168{
169 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900170 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900171 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900172
Gustavo Padovan93bca242015-01-18 18:16:23 +0900173 if (exynos_crtc->ops->disable_vblank)
174 exynos_crtc->ops->disable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900175}
Rahul Sharma663d8762013-01-03 05:44:04 -0500176
Gustavo Padovanc4533662015-08-15 13:26:18 -0300177void exynos_drm_crtc_wait_pending_update(struct exynos_drm_crtc *exynos_crtc)
178{
179 wait_event_timeout(exynos_crtc->wait_update,
180 (atomic_read(&exynos_crtc->pending_update) == 0),
181 msecs_to_jiffies(50));
182}
183
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300184void exynos_drm_crtc_finish_update(struct exynos_drm_crtc *exynos_crtc,
185 struct exynos_drm_plane *exynos_plane)
Rahul Sharma663d8762013-01-03 05:44:04 -0500186{
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300187 struct drm_crtc *crtc = &exynos_crtc->base;
Rahul Sharma663d8762013-01-03 05:44:04 -0500188 unsigned long flags;
189
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300190 exynos_plane->pending_fb = NULL;
191
Gustavo Padovanc4533662015-08-15 13:26:18 -0300192 if (atomic_dec_and_test(&exynos_crtc->pending_update))
193 wake_up(&exynos_crtc->wait_update);
194
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300195 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Gustavo Padovan7cf23ea2015-08-15 13:26:19 -0300196 if (exynos_crtc->event)
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300197 drm_crtc_send_vblank_event(crtc, exynos_crtc->event);
Rahul Sharma663d8762013-01-03 05:44:04 -0500198
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300199 exynos_crtc->event = NULL;
Gustavo Padovaneafd5402015-07-16 12:23:32 -0300200 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Rahul Sharma663d8762013-01-03 05:44:04 -0500201}
Sean Paul080be03d2014-02-19 21:02:55 +0900202
Sean Paul080be03d2014-02-19 21:02:55 +0900203void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
204{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900205 struct exynos_drm_crtc *exynos_crtc;
Sean Paul080be03d2014-02-19 21:02:55 +0900206 struct drm_device *dev = fb->dev;
207 struct drm_crtc *crtc;
208
209 /*
210 * make sure that overlay data are updated to real hardware
211 * for all encoders.
212 */
213 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Gustavo Padovan93bca242015-01-18 18:16:23 +0900214 exynos_crtc = to_exynos_crtc(crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900215
216 /*
217 * wait for vblank interrupt
218 * - this makes sure that overlay data are updated to
219 * real hardware.
220 */
Gustavo Padovan93bca242015-01-18 18:16:23 +0900221 if (exynos_crtc->ops->wait_for_vblank)
222 exynos_crtc->ops->wait_for_vblank(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900223 }
224}
Inki Daef37cd5e2014-05-09 14:25:20 +0900225
226int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
Gustavo Padovancf67cc92015-08-11 17:38:06 +0900227 enum exynos_drm_output_type out_type)
Inki Daef37cd5e2014-05-09 14:25:20 +0900228{
229 struct drm_crtc *crtc;
230
231 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
232 struct exynos_drm_crtc *exynos_crtc;
233
234 exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200235 if (exynos_crtc->type == out_type)
Gustavo Padovan8a326ed2014-11-04 18:44:47 -0200236 return exynos_crtc->pipe;
Inki Daef37cd5e2014-05-09 14:25:20 +0900237 }
238
239 return -EPERM;
240}
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900241
242void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
243{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900244 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900245
Gustavo Padovan93bca242015-01-18 18:16:23 +0900246 if (exynos_crtc->ops->te_handler)
247 exynos_crtc->ops->te_handler(exynos_crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900248}