blob: a13779915cde34057fc8bd03e209d4b7c43e2084 [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"
Inki Dae1c248b72011-10-04 19:19:01 +090022#include "exynos_drm_encoder.h"
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090023#include "exynos_drm_plane.h"
Inki Dae1c248b72011-10-04 19:19:01 +090024
Inki Dae1c248b72011-10-04 19:19:01 +090025static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
26{
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
Joonyoung Shimd2716c82011-11-04 17:04:45 +090029 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
30
Inki Daeec05da92011-12-06 11:06:54 +090031 if (exynos_crtc->dpms == mode) {
32 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
33 return;
34 }
35
Inki Dae20cd2642013-05-21 16:55:58 +090036 if (mode > DRM_MODE_DPMS_ON) {
37 /* wait for the completion of page flip. */
YoungJun Choe35d7222014-07-17 18:01:17 +090038 if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
Mandeep Singh Bainese7527472015-04-01 13:02:12 -030039 (exynos_crtc->event == NULL), HZ/20))
40 exynos_crtc->event = NULL;
Andrzej Hajdad6948b22014-10-10 14:31:55 +020041 drm_crtc_vblank_off(crtc);
Inki Dae20cd2642013-05-21 16:55:58 +090042 }
43
Gustavo Padovan93bca242015-01-18 18:16:23 +090044 if (exynos_crtc->ops->dpms)
45 exynos_crtc->ops->dpms(exynos_crtc, mode);
Sean Paul080be03d2014-02-19 21:02:55 +090046
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090047 exynos_crtc->dpms = mode;
Andrzej Hajdad6948b22014-10-10 14:31:55 +020048
49 if (mode == DRM_MODE_DPMS_ON)
50 drm_crtc_vblank_on(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090051}
52
Inki Dae1c248b72011-10-04 19:19:01 +090053static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
54{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090055 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan9d5310c2014-11-13 22:17:46 -020056 struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
Joonyoung Shimd2716c82011-11-04 17:04:45 +090057
Inki Dae50caf252012-08-20 21:29:25 +090058 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Sean Paul080be03d2014-02-19 21:02:55 +090059
Gustavo Padovan93bca242015-01-18 18:16:23 +090060 if (exynos_crtc->ops->win_commit)
61 exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
Inki Dae1c248b72011-10-04 19:19:01 +090062}
63
64static bool
65exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020066 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +090067 struct drm_display_mode *adjusted_mode)
68{
Sean Paul4b405262014-01-30 16:19:19 -050069 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Sean Paul4b405262014-01-30 16:19:19 -050070
Gustavo Padovan93bca242015-01-18 18:16:23 +090071 if (exynos_crtc->ops->mode_fixup)
72 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
73 adjusted_mode);
Sean Paul4b405262014-01-30 16:19:19 -050074
Inki Dae1c248b72011-10-04 19:19:01 +090075 return true;
76}
77
Gustavo Padovan199329c2015-06-01 12:04:43 -030078static void
79exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
Inki Dae1c248b72011-10-04 19:19:01 +090080{
Joonyoung Shim4070d212012-06-27 14:27:05 +090081 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090082
Gustavo Padovan199329c2015-06-01 12:04:43 -030083 if (exynos_crtc->ops->commit)
84 exynos_crtc->ops->commit(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090085}
86
Joonyoung Shima365d9e2012-06-27 14:27:10 +090087static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
88{
Sean Paula9c4cd22014-01-30 16:19:17 -050089 struct drm_plane *plane;
90 int ret;
Joonyoung Shima365d9e2012-06-27 14:27:10 +090091
Joonyoung Shima365d9e2012-06-27 14:27:10 +090092 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Sean Paula9c4cd22014-01-30 16:19:17 -050093
Matt Roper08863272014-04-01 15:22:31 -070094 drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
Sean Paula9c4cd22014-01-30 16:19:17 -050095 if (plane->crtc != crtc)
96 continue;
97
98 ret = plane->funcs->disable_plane(plane);
99 if (ret)
100 DRM_ERROR("Failed to disable plane %d\n", ret);
101 }
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900102}
103
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300104static void exynos_crtc_atomic_begin(struct drm_crtc *crtc)
105{
106 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
107
108 if (crtc->state->event) {
109 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
110 exynos_crtc->event = crtc->state->event;
111 }
112}
113
114static void exynos_crtc_atomic_flush(struct drm_crtc *crtc)
115{
116}
117
Inki Dae1c248b72011-10-04 19:19:01 +0900118static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
119 .dpms = exynos_drm_crtc_dpms,
Inki Dae1c248b72011-10-04 19:19:01 +0900120 .commit = exynos_drm_crtc_commit,
121 .mode_fixup = exynos_drm_crtc_mode_fixup,
Gustavo Padovan199329c2015-06-01 12:04:43 -0300122 .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900123 .disable = exynos_drm_crtc_disable,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300124 .atomic_begin = exynos_crtc_atomic_begin,
125 .atomic_flush = exynos_crtc_atomic_flush,
Inki Dae1c248b72011-10-04 19:19:01 +0900126};
127
Inki Dae1c248b72011-10-04 19:19:01 +0900128static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
129{
130 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
131 struct exynos_drm_private *private = crtc->dev->dev_private;
132
Inki Dae1c248b72011-10-04 19:19:01 +0900133 private->crtc[exynos_crtc->pipe] = NULL;
134
135 drm_crtc_cleanup(crtc);
136 kfree(exynos_crtc);
137}
138
139static struct drm_crtc_funcs exynos_crtc_funcs = {
Gustavo Padovan47a7def2015-06-01 12:04:47 -0300140 .set_config = drm_atomic_helper_set_config,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300141 .page_flip = drm_atomic_helper_page_flip,
Inki Dae1c248b72011-10-04 19:19:01 +0900142 .destroy = exynos_drm_crtc_destroy,
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300143 .reset = drm_atomic_helper_crtc_reset,
144 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
145 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Inki Dae1c248b72011-10-04 19:19:01 +0900146};
147
Gustavo Padovan93bca242015-01-18 18:16:23 +0900148struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900149 struct drm_plane *plane,
150 int pipe,
151 enum exynos_drm_output_type type,
152 const struct exynos_drm_crtc_ops *ops,
153 void *ctx)
Inki Dae1c248b72011-10-04 19:19:01 +0900154{
155 struct exynos_drm_crtc *exynos_crtc;
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200156 struct exynos_drm_private *private = drm_dev->dev_private;
Inki Dae1c248b72011-10-04 19:19:01 +0900157 struct drm_crtc *crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200158 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900159
Inki Dae1c248b72011-10-04 19:19:01 +0900160 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900161 if (!exynos_crtc)
Gustavo Padovan93bca242015-01-18 18:16:23 +0900162 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900163
Inki Dae20cd2642013-05-21 16:55:58 +0900164 init_waitqueue_head(&exynos_crtc->pending_flip_queue);
Sean Paul080be03d2014-02-19 21:02:55 +0900165
166 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200167 exynos_crtc->pipe = pipe;
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200168 exynos_crtc->type = type;
Gustavo Padovan93bca242015-01-18 18:16:23 +0900169 exynos_crtc->ops = ops;
170 exynos_crtc->ctx = ctx;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900171
Gustavo Padovan357193c2014-11-03 18:20:29 -0200172 crtc = &exynos_crtc->base;
Inki Dae1c248b72011-10-04 19:19:01 +0900173
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200174 private->crtc[pipe] = crtc;
Inki Dae1c248b72011-10-04 19:19:01 +0900175
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200176 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200177 &exynos_crtc_funcs);
178 if (ret < 0)
179 goto err_crtc;
180
Inki Dae1c248b72011-10-04 19:19:01 +0900181 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
182
Gustavo Padovan93bca242015-01-18 18:16:23 +0900183 return exynos_crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200184
185err_crtc:
186 plane->funcs->destroy(plane);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200187 kfree(exynos_crtc);
Gustavo Padovan93bca242015-01-18 18:16:23 +0900188 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900189}
190
Sean Paul080be03d2014-02-19 21:02:55 +0900191int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900192{
193 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900194 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900195 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900196
Inki Daeec05da92011-12-06 11:06:54 +0900197 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
198 return -EPERM;
199
Gustavo Padovan93bca242015-01-18 18:16:23 +0900200 if (exynos_crtc->ops->enable_vblank)
201 exynos_crtc->ops->enable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900202
203 return 0;
204}
205
Sean Paul080be03d2014-02-19 21:02:55 +0900206void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900207{
208 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900209 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900210 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900211
Inki Daeec05da92011-12-06 11:06:54 +0900212 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
213 return;
214
Gustavo Padovan93bca242015-01-18 18:16:23 +0900215 if (exynos_crtc->ops->disable_vblank)
216 exynos_crtc->ops->disable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900217}
Rahul Sharma663d8762013-01-03 05:44:04 -0500218
Sean Paul080be03d2014-02-19 21:02:55 +0900219void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
Rahul Sharma663d8762013-01-03 05:44:04 -0500220{
221 struct exynos_drm_private *dev_priv = dev->dev_private;
Sean Paul080be03d2014-02-19 21:02:55 +0900222 struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
Inki Dae20cd2642013-05-21 16:55:58 +0900223 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
Rahul Sharma663d8762013-01-03 05:44:04 -0500224 unsigned long flags;
225
Rahul Sharma663d8762013-01-03 05:44:04 -0500226 spin_lock_irqsave(&dev->event_lock, flags);
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300227 if (exynos_crtc->event) {
Rahul Sharma663d8762013-01-03 05:44:04 -0500228
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300229 drm_send_vblank_event(dev, -1, exynos_crtc->event);
Sean Paul080be03d2014-02-19 21:02:55 +0900230 drm_vblank_put(dev, pipe);
Inki Dae20cd2642013-05-21 16:55:58 +0900231 wake_up(&exynos_crtc->pending_flip_queue);
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300232
Rahul Sharma663d8762013-01-03 05:44:04 -0500233 }
234
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300235 exynos_crtc->event = NULL;
Rahul Sharma663d8762013-01-03 05:44:04 -0500236 spin_unlock_irqrestore(&dev->event_lock, flags);
237}
Sean Paul080be03d2014-02-19 21:02:55 +0900238
Sean Paul080be03d2014-02-19 21:02:55 +0900239void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
240{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900241 struct exynos_drm_crtc *exynos_crtc;
Sean Paul080be03d2014-02-19 21:02:55 +0900242 struct drm_device *dev = fb->dev;
243 struct drm_crtc *crtc;
244
245 /*
246 * make sure that overlay data are updated to real hardware
247 * for all encoders.
248 */
249 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Gustavo Padovan93bca242015-01-18 18:16:23 +0900250 exynos_crtc = to_exynos_crtc(crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900251
252 /*
253 * wait for vblank interrupt
254 * - this makes sure that overlay data are updated to
255 * real hardware.
256 */
Gustavo Padovan93bca242015-01-18 18:16:23 +0900257 if (exynos_crtc->ops->wait_for_vblank)
258 exynos_crtc->ops->wait_for_vblank(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900259 }
260}
Inki Daef37cd5e2014-05-09 14:25:20 +0900261
262int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
263 unsigned int out_type)
264{
265 struct drm_crtc *crtc;
266
267 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
268 struct exynos_drm_crtc *exynos_crtc;
269
270 exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200271 if (exynos_crtc->type == out_type)
Gustavo Padovan8a326ed2014-11-04 18:44:47 -0200272 return exynos_crtc->pipe;
Inki Daef37cd5e2014-05-09 14:25:20 +0900273 }
274
275 return -EPERM;
276}
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900277
278void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
279{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900280 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900281
Gustavo Padovan93bca242015-01-18 18:16:23 +0900282 if (exynos_crtc->ops->te_handler)
283 exynos_crtc->ops->te_handler(exynos_crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900284}